A simple introduction to launch files
This is the second in a series of articles about the robotics platform ROS. The first article can be found here. In this article I will talk about launch files. For anyone interested, please see cat.
The standard way to run nodes in ROS is using the rosrun command. But if we need to run several nodes at the same time, we will have to run the rosrun command for each node in a new terminal.
launch files allow you to automate this process so that you can launch several ROS and rosmaster nodes with one command.
For this purpose, ROS has a special package roslaunch. The launch file looks like this:
roslaunch package_name launch_file.launch
launch files are files in xml format with the extension launch.
You can also launch the launch file by specifying the path to the launch file
roslaunch path/to/launch/file/launch_file.launch
roslaunch automatically launches roscore if it is not already running. To prevent roscore from starting, you can specify the –wait parameter. In this case, roslaunch will wait until a running roscore is detected.
Launch file syntax
launch files support a set of tags that are intended to configure various aspects of the launched process. Tag node designed to run ROS nodes. In tag attributes node you can specify the name of the package, node, etc.
Example of a node tag in a launch file:
<node pkg="turtlesim" type="turtlesim_node" name="turtlesim" respawn="true"/>
Here attribute pkg specifies the package name. type specifies the name of the executable file (the node name that we set in CMakeLists.txt in the add_executable statement and which follows the package name when starting the node). respawn=”true” means that the node will be automatically restarted after completion. A similar command to start a node looks like this
rosrun turtlesim turtlesim_node
We can use the node tag to start playing the rosbag file
<node pkg="rosbag" type="play" name="player" output="screen" args="-l --hz=10 /home/vlad/Downloads/rgbd_dataset_freiburg1_rpy-2hz-with-pointclouds.bag"/>
This instruction is similar to the command:
rosbag play -l --hz=10 /home/vlad/Downloads/rgbd_dataset_freiburg1_rpy-2hz-with-pointclouds.bag
Within a tag you can use the remap, param, env and rosparam tags.
Tag remap allows you to set an alias (alias) for a topic. Thanks to this tag, the node will think that it has subscribed to the topic /topic1, although in fact it will subscribe to the topic /topic2. This can be convenient when the target topic has a long or non-standard name. For example, if the standard topic for receiving a video stream from a camera is /usb_cam/image_raw, and instead of a real camera we want to use a rosbag file that publishes it to some of our own topics. remap will look like this
<remap from="~camera_info" to="/image_publisher_1727120104720172865/camera_info" />
Tag param needed to set parameters. An example of a tag for setting a topic:
<param name="topic" value="/camera/depth_registered/points" />
You can set a boolean value
<param name="calculate_points_density" type="bool" value="true" />
You can set global variables in the arg tag
<arg name="framerate" default="20" />
and then access this variable in another tag using $(arg framerate):
<param name="framerate" value="$(arg framerate)" />
You can import other launch files launch file using the tag include
<include file="$(find pkg-name)/path/filename.launch"/>
Debugging and profiling launch files
We can run the launch file in debug mode gdb or profiling using Valgrind.
To run the launch file in gdb debug mode you need to add the l attributelaunch-prefix=”gdb -ex run –args” to the node tag.
Launch file example
<launch>
<node name="test_node" pkg="test_slam_metrics" type="test_metrics_node" output="screen" launch-prefix="gdb -ex run --args"/>
</launch>
When you run the launch file, the gdb terminal will automatically launch.
You can also run the node in Valgrind profiler mode. Install Valgrind and KCachegrind:
sudo apt install valgrind kcachegrind
Now let's add an attribute to the launch file:
<launch>
<node name="test_node" pkg="test_slam_metrics" type="test_metrics_node" output="screen" launch-prefix="valgrind --tool=callgrind --callgrind-out-file=outfile"/>
</launch>
Let's run the launch file and get the following output:
To visualize profiler data Valgrind let's launch KCachegrind
kcachegrind ~/.ros/outfile
The KCachegrind program window will open.
You can find many guides on working with KCachegrind on the Internet, for example here this.
In conclusion, I will show an example of a real launch file for the camera driver node:
<?xml version="1.0" ?>
<launch>
<arg name="name" default="camera_front" />
<arg name="framerate" default="20" />
<arg name="camera_info" default="package://my_cam_driver/ost.yaml" />
<node name="$(arg name)" pkg="my_cam_driver" type="my_cam_driver_node" output="screen">
<param name="framerate" value="$(arg framerate)" />
<param name="camera_info_url" value="$(arg camera_info)" />
</node>
</launch>
There are several global arg variables here that are used for node parameters inside the node tag.
You can read more about roslaunch on the official page. There you can also find a series of educational tutorials.
That's all for now.