Launch files are very common in ROS to both users and developers. They provide a convenient way to start up multiple nodes and a master, as well as other initialization requirements such as setting parameters.
roslaunch is used to open launch files. This can be done by either specifying the package the launch files are contained in followed by the name of the launch file, or by specifying the file path to the launch file.
roslaunch <package> <launch_file> roslaunch </PATH/TO/LAUNCH_FILE/launch_file>
Note: roslaunch will also start roscore if no master has been set. Pushing Ctrl-C in a terminal with a launch file running will close all nodes that were started with that launch files.
Writing a .launch File¶
Launch files are of the format .launch and use a specific XML format. They can be placed anywhere within a package directory, but it is common to make a directory named “Launch” inside the workspace directory to organize all your launch files. The contents of a launch file must be contained between a pair of launch tags
<launch> ... </launch>
To actually start a node, the <node> tags are used, the pkg, type and name argument are required.
<node pkg="..." type="..." name="..." respawn=true ns="..."/>
pkg/type/name: The argument pkg points to the package associated with the node that is to be launched, while “type” refers to the name of the node executable file. It is also possible to overwrite the name of the node with the name argument, this will take priority over the name that is given to the node in the code.
Respawn/Required: However optional, it’s common to either have a respawn argument or a required argument, but not both. If respawn=true, then this particular node will be restarted if for some reason it closed. Required=true will do the opposite, that is, it will shut down all the nodes associated with a launch file if this particular node comes down. There are other optional argument available on the ROS wiki.
ns: Another common use for a launch file is to launch a node inside a namespace. This is useful when using multiple instances of the same node. You can specify a namespace by using the “ns” argument.
arg: Sometimes it is necessary to use a local variable in launch files. This can be done using
<arg name="..." value="...">
Now let’s take a look at the launch file we use on our Husky’s on-board PC on startup to get things going.
<launch> <arg name="port" default="$(optenv HUSKY_PORT /dev/prolific)" /> <node pkg="clearpath_base" type="kinematic_node" name="husky_kinematic" ns="husky"> <param name="port" value="$(arg port)" /> <rosparam> cmd_fill: True data: system_status: 10 safety_status: 10 encoders: 10 differential_speed: 10 differential_output: 10 power_status: 1 </rosparam> </node> <!-- Publish diagnostics information from low-level MCU outputs --> <node pkg="husky_base" name="husky_base_diagnostics" type="diagnostics_publisher" /> <!-- Publish wheel odometry from MCU encoder data --> <node pkg="husky_base" name="husky_basic_odom" type="basic_odom_publisher" /> <!-- Diagnostic Aggregator --> <node pkg="diagnostic_aggregator" type="aggregator_node" name="diagnostic_aggregator"> <rosparam command="load" file="$(find husky_base)/config/diagnostics.yaml"/> </node> </launch>
The first thing to notice is the <launch> tags which are required for all launch files. The next line finds what port Husky is connected to, and saves it to an argument named “port”. The node “kinematic_node” from the package”clearpath_base” is then started in the “husky” name space.
Parameters within the <node> tags are private to that namepace. The “port” argument that was defined earlier is set to the port parameter. Several other parameters are populated using the YAML format with <parameter> tags.
Along with the kinematic_node node, this launch file also starts husky_base_diagnositcs and husky_base_odom. You can see that the parameters for the diagnostics_aggregator node are loaded from a YAML file.
This should cover most of what you will need to write your own launch files, but for more information on launch files visit the ROS wiki.