ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
8

Launch file - how to write it?

asked 2011-12-05 00:32:03 -0500

Felix Tr gravatar image

updated 2015-11-13 11:53:14 -0500

lucasw gravatar image

Hello All,

I am relatively new user of ROS, and this is the first time I should write a launch file. I would be happy if you could navigate me to the best way to do it.

What I do is: I subscribe to recorded topics (bag files) using image_transport package, I republish their content to different topics (from compressed to raw), and eventually I subscribe to those topics with my subscriber (the only node I have created). The shell command lines I use in order to run my program without a launch file are:

roscore

rosbag play -l left_camrea.bag

rosbag play -l rigth_camrea.bag

rosrun image_transport republish compressed in:=/bb2/left/image_rect raw 
  out:=lefttopic

rosrun image_transport republish compressed in:=/bb2/right/image_rect raw     
  out:=righttopic

rosrun myPackage myExecutibleFile

I also need to configure parameters for my program via this launch file.

What from the above can/should be included in the launch file? What should not?

What is the conceptual structure of my file should be like?

Thank you for your attention, Felix,

edit retag flag offensive close merge delete

5 Answers

Sort by » oldest newest most voted
20

answered 2011-12-05 00:49:54 -0500

DimitriProsser gravatar image

Here is a good starting place for writing launch files. In general, all of your nodes/parameters can and should be run via launch files. This allows for the most control over your programs. Though, at times, it might not be advisable to restart all processes when you're trying to debug a single node. In that instance, you can use the command line to launch that process separately, just so you can kill/restart it at your leisure.

Here is an example of how to launch a bagfile via roslaunch. The one thing that you should also note is that if your program relies on the timestamp of incoming messages, you should set the use_sim_time parameter in your launch file so that your nodes will use the bagfile's timestamp instead of the PC's. You can do that by putting the following in your launch file:

<param name="use_sim_time" value="true" />

And just to help you out, here is example of how to use the republish node via launchfile (it's not explicitly obvious from reading the tutorials I've provided.

<node name="republish" type="republish" pkg="image_transport" output="screen" args="compressed in:=/axis_camera raw out:=/axis_camera/image_raw" />

As for using multiple bagfiles at the same time, you might run into some problems, since rosbag has limitations on recording the time exactly. As such, you might find that your bag times don't sync up. If this is the case, you can try using the rosbag API and the associated Cookbook to try and solve your problem.

edit flag offensive delete link more

Comments

Thanks a lot, your answer helped me, and now I have the needed launch file :)
Felix Tr gravatar image Felix Tr  ( 2011-12-05 03:54:32 -0500 )edit
6

answered 2011-12-05 23:01:45 -0500

Thomas gravatar image

updated 2011-12-06 00:16:42 -0500

First, you have to understand that there is no magic in roslaunch, this is just a way to start processes automatically.

For instance, the code you provided in the previous post is more or less rewritten into the following shell command:

ENV_EXAMPLE="some value" rosrun rospy_tutorials talker _talker_1_param:=a_value

And that's all!

So there is no "connection" between C and launch files. A launch file "launches" binaries and that's all, it also monitors their status but that's all. In particular, the node will never e able to influence in any way on the launch script.

The second part is the environment variables. Roslaunch allows you to define some variables to configure your scripts, it is particularly interesting when you need to start binaries which are not ROS nodes. Otherwise if you want to change the behavior of a launch file, I recommend that you use <arg ...=""/> instead. For ROS nodes, use <param ...=""/> or <rosparam/> instead.

Then, finally, the parameters. I found the doc on the parameters a bit obscure so let me explain again:

When you start the master, it maintains a map key -> value of parameters that you can set and get from any node (it is shared among all the nodes). Use rosparam list / rosparam set / rosparam get to see what parameters are available.

The parameters are public by default, but there is also "private parameters". These parameters are also shared but are put in a separate namespace to avoid naming collisions.

Example: you have a foo node which have two parameters: bar which is public and baz which is private.

You will see the following:

$ rosparam list
/bar          # the public parameter
/foo/baz      # the private parameter

Again, ALL parameters are shared among all nodes and are visible through rosparam for instance.

However, one interesting thing with private parameters is that they can be set on the command line when you start the node:

rosrun fooPkg foo _baz:="a value"

Here you set the private parameter baz to "some value".

The equivalent syntax when using a launch file is through the <param .../> tag. <rosparam .../> allows to load private parameters from a YAML file to avoid making your launch files too verbose.

Another thing which is confusing in the documentation is that the private parameters are documented as ~baz on the wiki for instance, so you have to know that the ~ is replaced by _ on the command line and disappear in the launch file.

edit: as for the C++ part, you can read the roscpp tutorial here about the parameters to know how to retrieve them and use them in your code. Again, I do not recommend that you rely on environment variables to configure your node.

edit flag offensive delete link more

Comments

1
Hello Thomas, wanted to thank you again. Your answer was very useful :).
Felix Tr gravatar image Felix Tr  ( 2011-12-06 22:30:31 -0500 )edit
3

answered 2011-12-05 00:40:37 -0500

Thomas gravatar image

Hello, what you want to achieve seems a lot like a launch file I've written before: https://github.com/laas/visp_tracker/blob/master/launch/tutorial.launch

Here I launch a bag file and a couple of nodes. roscore will be started automatically.

Put a copy of this file into the launch directory of your package, then modify the path to the bag files and remove the other nodes. Check with rostopic list that it works, then add your nodes one by one. This should be straight forward.

edit flag offensive delete link more

Comments

Thanks a lot, your answer helped me, and now I have the needed launch file :)
Felix Tr gravatar image Felix Tr  ( 2011-12-05 03:54:21 -0500 )edit
2

answered 2011-12-05 03:03:20 -0500

Filip gravatar image

Nice timing, today I release the rxDeveloper ( http://code.google.com/p/rxdeveloper-ros-pkg/) for graphical launchfile creation. Of course you should know the basics from the other answers, but this graphical tool can help you and will create correct XML-files that you can test from within the tool.

edit flag offensive delete link more

Comments

I still had no use in your advice, but I wanted to thank you for your answer :)
Felix Tr gravatar image Felix Tr  ( 2011-12-05 03:55:23 -0500 )edit
1

answered 2011-12-05 21:25:11 -0500

Felix Tr gravatar image

Hello again,

This time I would like to ask you about variables/parameters that I want to deliver to a my program (node) via launch file. My purpose is to configure the number of the cameras, that will appear in a GUI that I create, and I want this number to be configurable in a launch file. I have found this example in the web: http://www.ros.org/wiki/roslaunch/XML, but I don´t exactly understand the connection between the launch file and the C code...

For example, in the next code:

<node pkg="rospy_tutorials" type="talker" name="talker">
  <!-- set a private parameter for the node -->
  <param name="talker_1_param" value="a value" />
  <!-- you can set environment variables for a node -->
  <env name="ENV_EXAMPLE" value="some value" />
</node>
  1. Do I need "to have" a variable named "talker_1_param" in my code?

  2. How actually I CONNECT between the launch file and the C code?

  3. Actually what is the difference between variable and parameter in the context of launch files?

  4. What is the difference between "parameter" and "environment variables" in the above code?

  5. In my node there are several functions, when the writer of the above code says in the comment that this is a "private parameter", what does he mean?

  6. Is there a way to distinguish between global and local variables?

Thank you all in advance,

Have a nice day,

Felix.

edit flag offensive delete link more

Comments

2
please don't ask follow up questions as "answers" to your previous question. It's much clearer to ask them as thier own question.
tfoote gravatar image tfoote  ( 2011-12-06 07:14:16 -0500 )edit
1
Thank you for your remark.
Felix Tr gravatar image Felix Tr  ( 2011-12-08 21:42:32 -0500 )edit

Did you ask this question anywhere. I am looking for answers to this.

Gudjohnson gravatar image Gudjohnson  ( 2013-07-23 03:46:29 -0500 )edit

Question Tools

4 followers

Stats

Asked: 2011-12-05 00:32:03 -0500

Seen: 61,525 times

Last updated: Nov 13 '15