Tutorial: Build and run a new ROS package#

🏁 Goal of this tutorial

By the end of this tutorial, you will know how to develop a new ROS package (C++ or Python) inside a Docker image.

Pre-requisites#

You need to be already familiar with ROS: if not, check the ROS tutorials.

In particular, you might want to check the catkin tutorials if you are new to catkin.

Steps#

This exercise can be done both in your development computer or inside the ARI docker.

From your main computer create a new catkin workspace:

1mkdir -p ~/example_ws/src

If you want to use the docker, run the ARI docker, mounting the newly created workspace. Make sure to map the workspace properly. Then load the ROS environment.

Note

For your convenience, we have published some scripts that simplify the launch of a docker with GPU acceleration. Follow the instructions at pal_docker_utils to properly set up your environment with nvidia-docker. If you do not follow the steps properly, you will not be able to run gazebo, rviz or other graphical applications from within the docker container.

Once logged and after configuring pal_docker_utils, you will need to execute the pal_docker.sh script with the name of the image and the application you want to start.

1./pal_docker.sh -it -v ~/docker_mounts/gallium:/home/user/ -v /home/user/example_ws:/home/user/example_ws/ PATH_TO_YOUR_DOCKER_IMAGE bash
2
3source /opt/pal/gallium/setup.bash

If you are working on your development computer, only source the ROS environment, in this case, ROS Noetic:

1source /opt/ros/noetic/setup.bash

Create a catkin package

1cd /home/user/example_ws/src/
2catkin_create_pkg hello_world roscpp rospy

For a C++ example:

Edit the CMakeLists.txt file with the contents in the figure below.

../_images/CMakeLists.png

Edit the src/hello_world_node.cpp file with the contents in the figure below

../_images/Hello_world_cpp.png

For a Python example:

Edit the CMakeLists.txt file with the contents in the figure below.

../_images/CMakeLists_python.png

Edit the scripts/hello_world.py file with the contents in the figure below

../_images/hello_world_python.png

Build the workspace in the same way, regardless if it is a Python or C++ script.

1cd ~/example_ws
2catkin build

The expected output is shown in the figure below.

../_images/hello_world_build.png

In order to run the node export the robot’s ROS_MASTER_URI and source the workspace:

1export ROS_MASTER_URI=http://ari-0c:11311
2
3source devel/setup.bash
4
5rosrun hello_world hello_world_node

For the Python node:

1rosrun hello_world hello_world.py

Check the message is correctly published by opening a new terminal. If it is the development computer:

1export ROS_MASTER_URI=http://ari-0c:11311
2rostopic echo /hello

Note, in this last step, if you are using a docker, open a new docker instance first:

1docker exec -it IMAGE_ID bash
2
3source /opt/pal/gallium/setup.bash
4
5export ROS_MASTER_URI=http://ari-0c:11311
6
7rostopic echo /hello

Where the IMAGE_ID is obtained by listing available dockers.

../_images/docker_exec.png

If no message is obtained, make sure the ROS_IP is correctly set. Check inside the robot its IP address with ifconfig.

1export ROS_IP=192.168.1.109
../_images/ifconfig.png