Skip to content

Commit

Permalink
Add Simulation Setup (#53)
Browse files Browse the repository at this point in the history
* Add Carla Server

* Add CARLA ROS Bridge

* CarlaViz Integration

* Add base images

* Modify workflow

* Try fix workflow

* Add documentation

* Remove non-ros base images

* Add carla notebooks

* Add carla sample node

* Try to fix workflow error

* Remove CARLA Version ARG

* Revert workflow changes

* Address PR comments

* Remove accidental commenting

* Address PR comments

* Change cache_from tags

* Fix linter errors

* Fix code style issues with clang_format

* Update carla_sample_node.Dockerfile

* Clone from tag instead of hash

* Delete carla_sample_node tests

* Keep sample node autopilot disabled by default

---------

Co-authored-by: WATonomousAdmin <[email protected]>
  • Loading branch information
VishGit1234 and WATonomousAdmin authored Mar 5, 2024
1 parent adecb09 commit b3cd2db
Show file tree
Hide file tree
Showing 39 changed files with 1,502 additions and 9 deletions.
23 changes: 23 additions & 0 deletions docker/simulation/carla_notebooks/carla_notebooks.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ARG CARLA_VERSION=0.9.13
FROM carlasim/carla:${CARLA_VERSION} AS wato_carla_api

FROM python:3.8.16-slim-bullseye
ARG CARLA_VERSION=0.9.13

RUN pip3 install carla==${CARLA_VERSION} jupyter tensorflow-probability
RUN apt-get update && apt-get install -y curl git wget unzip && apt remove python3-networkx

COPY --from=wato_carla_api --chown=root /home/carla/PythonAPI/carla/agents /usr/local/lib/python3.8/site-packages/agents

WORKDIR /home/bolty/carla_notebooks
COPY src/simulation/carla_notebooks /home/bolty/carla_notebooks

WORKDIR /home/bolty
# Setup CARLA Scenario Runner
# The last sed command replaces hero (default ego vehicle name) with another ego vehicle name
RUN git clone https://github.com/carla-simulator/scenario_runner.git && \
cd scenario_runner && pip3 install -r requirements.txt && \
sed -i s/hero/ego/g /home/bolty/scenario_runner/srunner/tools/scenario_parser.py
WORKDIR /home/bolty

WORKDIR /home/bolty/carla_notebooks
109 changes: 109 additions & 0 deletions docker/simulation/carla_ros_bridge/carla_ros_bridge.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
ARG BASE_IMAGE=ghcr.io/watonomous/wato_monorepo/base:foxy-ubuntu20.04

ARG CARLA_VERSION=0.9.13
FROM carlasim/carla:${CARLA_VERSION} AS wato_carla_api

################################ Source ################################
FROM ${BASE_IMAGE} as source

WORKDIR ${AMENT_WS}/src

# Download ROS Bridge
RUN git clone --depth 1 --branch master --recurse-submodules https://github.com/carla-simulator/ros-bridge.git && \
cd ros-bridge && \
git checkout e9063d97ff5a724f76adbb1b852dc71da1dcfeec && \
cd ..

# Fix an error in the ackermann_control node
RUN sed -i s/simple_pid.PID/simple_pid.pid/g ./ros-bridge/carla_ackermann_control/src/carla_ackermann_control/carla_ackermann_control_node.py

# Copy in source code
COPY src/simulation/carla_config carla_config
COPY src/wato_msgs/simulation ros_msgs

# Scan for rosdeps
RUN apt-get -qq update && rosdep update --rosdistro foxy && \
rosdep install --from-paths . -r -s \
| grep 'apt-get install' \
| awk '{print $3}' \
| sort > /tmp/colcon_install_list

################################# Dependencies ################################
FROM ${BASE_IMAGE} as dependencies

# Install dependencies
RUN apt-get update && \
apt-fast install -qq -y --no-install-recommends lsb-release \
libglu1-mesa-dev xorg-dev \
software-properties-common \
build-essential \
python3-rosdep \
python3-rospkg \
python3-colcon-common-extensions \
python3-pygame \
ros-$ROS_DISTRO-tf2-geometry-msgs \
ros-$ROS_DISTRO-tf2-eigen \
ros-$ROS_DISTRO-ackermann-msgs \
ros-$ROS_DISTRO-derived-object-msgs \
ros-$ROS_DISTRO-cv-bridge \
ros-$ROS_DISTRO-vision-opencv \
ros-$ROS_DISTRO-rqt-image-view \
ros-$ROS_DISTRO-rqt-gui-py \
qt5-default \
ros-$ROS_DISTRO-pcl-conversions \
ros-$ROS_DISTRO-resource-retriever \
ros-$ROS_DISTRO-yaml-cpp-vendor \
ros-$ROS_DISTRO-urdf \
ros-$ROS_DISTRO-map-msgs \
ros-$ROS_DISTRO-laser-geometry \
ros-$ROS_DISTRO-interactive-markers \
ros-$ROS_DISTRO-rviz2

# Install Rosdep requirements
COPY --from=source /tmp/colcon_install_list /tmp/colcon_install_list
RUN apt-fast install -qq -y --no-install-recommends $(cat /tmp/colcon_install_list)

# Copy in source code from source stage
WORKDIR ${AMENT_WS}
COPY --from=source ${AMENT_WS}/src src

# Dependency Cleanup
WORKDIR /
RUN apt-get -qq autoremove -y && apt-get -qq autoclean && apt-get -qq clean && \
rm -rf /root/* /root/.ros /tmp/* /var/lib/apt/lists/* /usr/share/doc/*

################################ Build ################################
FROM dependencies as build

ARG CARLA_VERSION

#Install Python Carla API
COPY --from=wato_carla_api --chown=root /home/carla/PythonAPI/carla /opt/carla/PythonAPI
WORKDIR /opt/carla/PythonAPI
RUN python3.8 -m easy_install pip && \
pip3 install carla==${CARLA_VERSION} && \
pip install simple-pid==2.0.0 && \
pip install transforms3d==0.4.1 && \
pip install pexpect==4.9.0 && \
pip install networkx==3.1

WORKDIR ${AMENT_WS}/src

# Build ROS2 packages
WORKDIR ${AMENT_WS}
RUN . /opt/ros/$ROS_DISTRO/setup.sh && \
colcon build \
--cmake-args -DCMAKE_BUILD_TYPE=Release

# Entrypoint will run before any CMD on launch. Sources ~/opt/<ROS_DISTRO>/setup.bash and ~/ament_ws/install/setup.bash
COPY docker/wato_ros_entrypoint.sh ${AMENT_WS}/wato_ros_entrypoint.sh
ENTRYPOINT ["./wato_ros_entrypoint.sh"]

################################ Prod ################################
FROM build as deploy

# Source Cleanup and Security Setup
RUN chown -R $USER:$USER ${AMENT_WS}
RUN rm -rf src/*

USER ${USER}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ FROM ${BASE_IMAGE} as source
WORKDIR ${AMENT_WS}/src

# Copy in source code
COPY src/simulation/carla_sim carla_sim
COPY src/wato_msgs/sample_msgs sample_msgs
COPY src/simulation/carla_sample_node carla_sample_node
COPY src/wato_msgs/simulation/embedded_msgs embedded_msgs
COPY src/wato_msgs/simulation/path_planning_msgs path_planning_msgs

# Carla specific messages

RUN git clone --depth 1 https://github.com/ros-drivers/ackermann_msgs.git --branch 2.0.2

RUN git clone --depth 1 https://github.com/ros-perception/image_common.git --branch 3.1.8

RUN git clone --depth 1 https://github.com/carla-simulator/ros-carla-msgs.git --branch 1.3.0

# Scan for rosdeps
RUN apt-get -qq update && rosdep update && \
Expand Down
14 changes: 14 additions & 0 deletions docker/simulation/carla_viz/carla_viz.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM mjxu96/carlaviz:0.9.13

ENV CARLAVIZ_BACKEND_HOST localhost
ENV CARLAVIZ_BACKEND_PORT 8081
ENV CARLA_SERVER_HOST localhost
ENV CARLA_SERVER_PORT 2000

WORKDIR /home/carla/carlaviz

COPY docker/simulation/carla_viz/carlaviz_entrypoint.sh /home/carla/carlaviz/docker/carlaviz_entrypoint.sh

RUN chmod +x ./docker/carlaviz_entrypoint.sh

ENTRYPOINT ["/bin/bash", "-c", "./docker/carlaviz_entrypoint.sh > /dev/null 2>&1"]
53 changes: 53 additions & 0 deletions docker/simulation/carla_viz/carlaviz_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

is_backend_up=""

function wait_for_backend_up() {
max_wait_time=5
for ((i=0; i<=$max_wait_time; ++i)) do
cat output.txt | grep -q "Connected to Carla Server"
if [ $? == 0 ]; then
is_backend_up="1"
break
fi
sleep 1
done
}

function cleanup_backend() {
backend_pid=$(pidof backend)
kill -9 $backend_pid
echo "Killed Backend process $backend_pid"
}

echo -e "CARLAVIZ_BACKEND_HOST=${CARLAVIZ_BACKEND_HOST}" >> /home/carla/.env
echo -e "CARLAVIZ_BACKEND_PORT=${CARLAVIZ_BACKEND_PORT}" >> /home/carla/.env

echo "Make sure you have launched the Carla server."
echo "Launching backend."
./backend/bin/backend ${CARLA_SERVER_HOST} ${CARLA_SERVER_PORT} |& tee output.txt &
wait_for_backend_up
if [[ -z "$is_backend_up" ]]; then
echo "Backend is not launched. Please check if you have already started the Carla server."
cleanup_backend
exit 1
fi

echo "Backend launched."

echo "Launching frontend"
# enable nginx
service nginx restart
echo "Frontend launched. Please open your browser"
sleep 10
sed -i s/:8081/:$CARLAVIZ_BACKEND_PORT/g /var/www/carlaviz/bundle.js

while [ "$is_backend_up" = "1" ]
do
cat output.txt | grep -q "time-out of"
if [ $? == 0 ]; then
is_backend_up=""
cleanup_backend
exit 1
fi
sleep 5
done
67 changes: 67 additions & 0 deletions docs/carla.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# CARLA Setup in Monorepo
CARLA is an open-source autonomous driving simulator based on Unreal Engine 4. The primary ways of interacting with the CARLA setup are through the Python API or with ROS2 (via the CARLA ROS Bridge). Both of these methods are explained in greater detail further down in this document.

The goal of the CARLA setup is to provide an easy way for WATonomous members to interact with the simulator without having to setup anything themselves. So, if you have any questions or suggestions regarding any part of the CARLA setup (or questions about CARLA in general) please bring them up in Discord in #simulation-general or contact Vishal Jayakumar (masteroooogway on Discord or at [[email protected]](mailto:[email protected])).

- [CARLA WATonomous Documentation](#using-carla-setup-in-monorepo)
- [Initial Setup](#initial-setup)
- [Interacting with CARLA using the Python API](#interacting-with-carla-using-the-python-api)
- [Using a ROS Node to interact with CARLA](#using-a-ros-node-to-interact-with-carla)
- [CARLA Visualization using Foxglove Studio](#carla-visualization-using-foxglove-studio)
- [CARLA Visualization using CarlaViz](#carla-visualization-using-carlaviz)
- [FAQ](#faq)
- [CARLA is running very slow (approx. 3 fps)](#carla-is-running-very-slow-approx-3-fps)

**Make sure you are confortable with navigating the monorepo before reading**

## Initial Setup

To run CARLA and all associated containers first add `simulation` as an `ACTIVE_MODULE` in `watod-config.sh`. This will cause the following containers to launch when `watod up` is run: `carla_server`, `carla_viz`, `carla_ros_bridge`, `carla_notebooks`, `carla_sample_node`.

## Interacting with CARLA using the Python API

This is the simplest way of interacting with CARLA and most importantly does not require the usage or knowledge of ROS. The documentation below will only show the setup procedure for using the CARLA Python API in the WATOnomous server (via Jupyter Notebook). The full CARLA Python API documentation can be found here ([CARLA Python API Documentation](https://carla.readthedocs.io/en/0.9.13/python_api/)).

**On the WATOnomous Server**

Forward the port specified by the variable `CARLA_NOTEBOOKS_PORT` (which can be found in the `.env` file in the `modules` folder once `watod up` is run) using either the `ports` section of VS Code or by running the command `ssh -L 8888:localhost:8888 <username>@<machine>-ubuntu1.watocluster.local` on your local machine (replace 8888 with the port specified in the `CARLA_NOTEBOOKS_PORT` variable). If the port is auto-forwarded by VS Code then you do not need to add it manually.

**On your local machine (your personal laptop/pc)**

Open any web browser (Chrome, Firefox, Edge etc.) and type `localhost:8888` (replace 8888 with the port specified in the `CARLA_NOTEBOOKS_PORT` variable) and click enter. You should be automatically redirected to the Jupyter Notebook home screen where you should see the file `carla_notebooks.ipynb`. Open that file and follow the instructions in it. Either edit the `carla_notebooks.ipynb` file directly or create a new file and copy the first code block in `carla_notebooks.ipynb`.

## Using a ROS Node to interact with CARLA

**Ensure that you have a good understanding of ROS and writing ROS nodes before proceeding**

Currently there is a CARLA sample node setup (written in Python) shows how to publish a message to enable (and keep enabled) autopilot, subsribe to the GNSS sensor topic and output the data to the console. What is probably most helpful from the sample node is the Dockerfile located in `wato_monorepo_v2/docker/simulation/carla_sample_node/carla_sample_node.Dockerfile`, where you can find all the CARLA related messages that should be installed when building your own ROS nodes that interact with CARLA.

## CARLA Visualization using Foxglove Studio

Foxglove Studio is a tool to visualize and interact with ROS messages. Using Foxglove Studio, data such as Camera, LiDAR, Radar etc. can be visualized. Foxglove Studio also allows for messages to be published to topics, for example to enable autopilot or set vehicle speed. The documentation below will only show the setup procedure for using Foxglove Studio with the WATOnomous server. Further documentation regarding how to use Foxglove Studio and all its features can be found here ([Foxglove Studio Documentation](https://foxglove.dev/docs/studio))

**On the WATOnomous Server**

Add `vis_tools` as an `ACTIVE_PROFILE`. This will launch the `foxglove.Dockerfile` container with an open port when `watod up` is ran.

It exposes the port specified by the `FOXGLOVE_BRIDGE_PORT` variable, which will be defined in the `.env` file in the `profiles` folder (the `.env` file is populated after `watod up` is run). This port may be auto-forwarded by VS Code automatically but if not the port will need to be forwarded manually to your local machine. This can either be done in the `ports` section of VS Code or by running the command `ssh -L 8765:localhost:8765 <username>@<machine>-ubuntu1.watocluster.local` on your local machine (replace 8765 with the port specified in the `FOXGLOVE_BRIDGE_PORT` variable).

**On your local machine (your personal laptop/pc)**

Open Foxglove Studio on your local machine using either the desktop app ([Download Here](https://foxglove.dev/studio)) or the [Web Application](https://studio.foxglove.dev/) (only supported on Chrome). Click on Open Connection then replace the default port (8765) with the port you specified in the `FOXGLOVE_BRIDGE_PORT` variable. Then click the Open button and after a few seconds Foxglove Studio should connect. You should now be able to access any of the topic being published or subscribed by the CARLA ROS Bridge.

## CARLA Visualization using CarlaViz

**On the WATOnomous Server**

CarlaViz is a visualization tool that is useful when you are only using the CARLA Python API through a Jupyter Notebook and you don't want the overhead that comes with the ROS Bridge + Foxglove method of visualization. To use CarlaViz forward the ports specified by the variables `CARLAVIZ_PORT` and `CARLAVIZ_PORT_2` defined in the `.env` file in the `profiles` folder (make sure you forward **both** ports). If you want to stop the ROS Bridge from running (for the reasons I mentioned in the previous sentences) then make sure to comment out the `carla_ros_bridge` service in the `docker-compose.carla.yaml` file in the `profiles` folder.

**On your local machine (your personal laptop/pc)**

In any browser go to `localhost:8081` (replace 8081 with the port specified in the `CARLAVIZ_PORT` variable) and after waiting 10 seconds or so CarlaViz should appear.

## FAQ

### CARLA is running very slow (approx. 3 fps)

This is expected. The ROS bridge causes CARLA to render all the sensor data which slows down the simulation considerably. While this may be annoying when viewing real-time camera output or trying to control the car manual, the simulation is still running accurately. If you wish to replay scenes with a configurable playback speed, check out the docs on [CARLA's recorder](https://carla.readthedocs.io/en/0.9.13/adv_recorder/) functionality.
29 changes: 27 additions & 2 deletions modules/dev_overrides/docker-compose.simulation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,35 @@ x-fixuid: &fixuid

services:
carla_sim:
<<: *fixuid
extends:
file: ../docker-compose.simulation.yaml
service: carla_sim

carla_ros_bridge:
<<: *fixuid
extends:
file: ../docker-compose.simulation.yaml
service: carla_ros_bridge
command: tail -F anything
volumes:
- ${MONO_DIR}/src/simulation/carla_config:/home/bolty/ament_ws/src/carla_config
# command: /bin/bash -c "ros2 launch carla_config carla.launch.py"

carla_viz:
extends:
file: ../docker-compose.simulation.yaml
service: carla_viz

carla_sample_node:
<<: *fixuid
extends:
file: ../docker-compose.simulation.yaml
service: carla_sample_node
command: tail -F anything
volumes:
- ${MONO_DIR}/src/simulation/carla_sim:/home/bolty/ament_ws/src/carla_sim
- ${MONO_DIR}/src/simulation/carla_sample_node:/home/bolty/ament_ws/src/carla_sample_node

carla_notebooks:
extends:
file: ../docker-compose.simulation.yaml
service: carla_notebooks
Loading

0 comments on commit b3cd2db

Please sign in to comment.