Lab 1.4: Create Launch file for ROS


This lab is to write a .launch file which can activate a bunch of ROS nodes together without open new terminal.

Writing three nodes

Please compile three nodes using previous example.

Node 1: Publish I am node1 string message as topic message, name the node IamPub

Node 2: Publish I am node2 string message as topic message2, name the node IamPub2

Node 3: Subscribe both message and print in on the terminal, name the node IamSub2

What happen using previous rosrun command

You will have to open four terminals, the first one is to run master and following three run three nodes individually.

Create .launch file to simultaneously run many nodes

First go to bin folder of your package and create a .launch file, you can use:

roscd my_ex1/bin
gedit ex1.launch

Paste following script in the file:

<?xml version=”1.0″?>
<node pkg=”my_ex1″ type=”IamPub” name=”Node1″/>
<node pkg=”my_ex1″ type=”IamPub2″ name=”Node2″/>
<node pkg=”my_ex1″ type=”IamSub” name=”Node3″/>

Save the file and close all terminal, then open a new terminal:

roslaunch my_ex1 ex1.launch

Then you can see all three nodes includes master are running.

Rename the topic

When you want to use the same node (ex: querying two GPS module), you have to rename the topic for second node, otherwise, it will overwrite previous one.

Use previous compiled node IamPub but rename its topic from message to message2.

<?xml version=”1.0″?>
<node pkg=”my_ex1″ type=”IamPub” name=”Node1″/>
<node pkg=”my_ex1″ type=”IamPub” name=”Node2″>
<remap from=”message” to=”message2″/>
<node pkg=”my_ex1″ type=”IamSub2″ name=”Node3″/>