ROS2 Basics Exercise¶
Motivation¶
Our goal for this exercise is to go through the basic ROS2 commands to understand how they work. We will not be writing any code during this exercise, instead we will just run through a few commands in the shell.
Workspaces and packages¶
Start by creating a new ROS2 workspace. As with catkin in ROS1, a workspace consists of a folder with a
src/subdirectory.mkdir -p ~/ros2_ws/src
Clone the ROS2 demos examples repositories which we will use to run some examples. When building existing packages from source, it is important to verify the code version as the most recent version may have changes incompatible with the current release. Here we checkout the repositories to the
eloquentrelease branch when cloned.cd ~/ros2_ws/src git clone -b eloquent git@github.com:ros2/demos.git git clone -b eloquent git@github.com:ros2/examples.git
Install any required dependencies for the repositories using rosdep. This step is the same as in ROS1 since rosdep is installed as a system tool.
cd ~/ros2_ws rosdep install --ignore-src --from-paths src/
Building packages¶
ROS2 uses colcon as the build tool for ROS packages. Colcon is installed separately from ROS
distributions as a pure Python package. It is always available in the user’s PATH without sourcing a
workspace setup file.
Run
colcon -hto see a short help summary and a list of verbs that can be used. Runcolcon build -hto see the help description for thebuildverb.Run
colcon listandcolcon infoto see infomation about what packages are currently in the workspace. Colcon can be directed to ignore packages if a directory contains an empty file namedCOLCON_IGNORE.Build the workspace. This will require a setup script to be sourced so Ament can find the required dependent ROS packages. Check your
.bashrcfile and either change or remove any line that sources a ROS1 setup file at the end of the file. Remember if you change your.bashrcto start a terminal for it to take effect.source /opt/ros/eloquent/setup.bash colcon build
Important: Unlike catkin tools, colcon looks for packages and will create its output folders wherever you run it. Be sure you are in the workspace root before running it.
Colcon does not currently have a
cleanverb. To rebuild a workspace from scratch, you must remove the generated outputs manually:rm -r build/ install/ log/
Source the newly-built workspace. Inside the new
install/folder will be bothsetup.bashandlocal_setup.bashfiles. Thesetup.bashwill configure a terminal environment to see the workspace packages as well as the environment the workspace was built in.local_setup.bashwill add the workspace packages to the current environment.If you keep your
.bashrcto source/opt/ros/eloquent/setup.bashthen you should only need to runsource install/local_setup.bashOtherwise, run
source install/setup.bash
The ros2 command¶
ROS2 replaces the set of ros* command line tools with subcommands of a single ros2 command.
Briefly we’ll run through some of ones that may be useful for a developer.
ros2 pkg¶
Shows information about ROS packages visible in the current environment
Use
ros2 pkg listto see a list of all visible packagesUse
ros2 pkg executablesto see a list of executables made available by a package. E.g.,ros2 pkg executables tf2_rosUse
ros2 pkg prefixto get the installation location of package
ros2 run¶
Runs executables provided by a package (ros2 run <package_name> <executable_file>)
Open two terminals sourced to the
ros2_wsworkspace.In the first one, run
ros2 run demo_nodes_cpp listener. Tab completion should be available so you don’t have to type the full package and executable names.In the second, run
ros2 run demo_nodes_cpp talker. You should now start to see a string message start to be printed repeatedly on both terminals. Leave both nodes running.
Note: You did not need to start a ROS master process before running nodes. The two nodes discovered each other on the network in a decentralized way as they started up.
ros2 node¶
Shows information about currently running ROS nodes.
Open a third terminal sourced to the
ros_wsworkspace.Run
ros2 node listto see a list of running nodesRun
ros2 node info /talkerto see the topics and services the talker node is using
ros2 topic¶
Shows information about topics currently used by one or more nodes.
Run
ros2 topic listto see currently published topicsRun
ros2 topic info /chatterto see information about the topic the talker node is publishingRun
ros2 topic echo /chatterto locally subscribe to the/chattertopic and print it on the terminalRun
ros2 topic -hfor a list of more available subcommands
ros2 service/srv¶
Shows information about ROS services.
Either stop the talker and listener nodes with
Ctrl-Cor open two more terminals.In the first terminal, run
ros2 run demo_nodes_cpp add_two_ints_serverIn the second terminal, run
ros2 service list -tto show currently available services and what type they are.Run
ros2 srv show example_interfaces/srv/AddTwoIntsto see the service definition of the/add_two_intsservice. Notice the names and types of the fields used in the service request.Run
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts '{a: 1, b: 2}'to manually call the service. The fields of the service request must be set using a YAML dictionary that specifies the field names and values.
ros2 launch¶
Start an installed launch file (ros2 launch <package_name> <launch_file>)
Close any currently running nodes
Run
ros2 launch dummy_robot_bringup dummy_robot_bringup.launch.py. Again, tab completion should be able to help here. Notice the.pysuffix on the launch file, it’s just a python script.
This starts five nodes that can inspected with the above command line tools. You might notice a
/joint_states topic publishing changing values and a /scan topic publishing simulated 2D laser
scanner data. Let’s visualize them.
RViz¶
RViz has been renamed to rviz2 and substantially rewritten for ROS2 but appears more-or-less the
same from the user’s point-of-view.
In a free terminal, run
rviz2while the dummy robot launch file is still running.Change the ‘Fixed Frame’ to ‘world’
Add a
RobotModeldisplay type. Expand the display options and set ‘Description Source’ to ‘Topic’ and ‘Description Topic’ to ‘/robot_description’. You should now see a robot moving on your screen.Add a
LaserScandisplay and have it display the ‘/scan’ topic. You should start to see 2D laser scan data appear as if the scanner were attached to the end of the robot. Getting intermittent transform errors is normal.