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

How do I create dynamic launch files?

asked 2016-03-18 10:38:58 -0500

inusO gravatar image

I developed a node which represent an agent in an emergent system that implements a collision avoidance algorithm. Problem is, I need to perform multiple simulations with N robots, I know I can pass N as arg to launch file, but I can't find a way to launch N nodes in the launch file.

At the moment the only solution I have is to edit the launch file.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
9

answered 2018-04-26 02:20:31 -0500

MartinK gravatar image

You could use eval (as of Kinetic) to create a recursive launchfile:

<launch> 

   <!-- start node -->
   <arg name="nr" default="1"/> 
   <group ns="$(eval 'turtle' + str(arg('nr')))">
      <node name="turtle" pkg="turtlesim" type="turtlesim_node"/>
   </group>

   <!-- recursively start new node -->
   <include file="$(find your_package)/launch/this_launch_file.launch" if="$(eval arg('nr') - 1 > 0)">
       <arg name="nr" value="$(eval arg('nr') - 1)"/>
   </include>

</launch>

As of Lunar you could also use $(dirname) instead of $(find your_package)/launch.

edit flag offensive delete link more

Comments

Does eval support the smaller than symbol, i.e. < ? I tried several time this code snippet, didn't work.

raphael gravatar image raphael  ( 2018-10-20 03:23:27 -0500 )edit

For me, '>' does not work. However, I was able to use '>' with switched arguments, which is equivalent. Though I have no idea, why the first option doesn't work and throws Invalid XML syntax...

m.bahno gravatar image m.bahno  ( 2020-09-22 08:22:49 -0500 )edit

I'm sorry, but I fail to understand how this is recursive? I tried this script by setting nr:=5 and it just launched the turtlesim node and my launch file once each.

cgnarendiran gravatar image cgnarendiran  ( 2021-05-12 08:11:49 -0500 )edit

hey cgnarendiran. are you sure you set the 'this_launch_file.launch' correctly? the name must be same as your desired launch file. For example, if you have an example.launch file and want to launch it 5 times, you should set the nr=5 as the default. Then you should re-launch your example.launch file in part. Include the example.launch file in that part by writing it's package, path and name correctly. This should work.

harunresul gravatar image harunresul  ( 2021-09-02 03:13:49 -0500 )edit

This is very very smart!

Mehdi. gravatar image Mehdi.  ( 2021-10-27 09:04:34 -0500 )edit
3

answered 2016-03-18 17:39:17 -0500

al-dev gravatar image

I am not aware of a way to launch N instances of the node by passing N as a roslaunch parameter, however you can follow the approach given here : put the section of the roslaunch file that you want to replicate inside a namespace ns tag, and pass the value of this namespace as a command line argument. Then you can have a shell script that calls your roslaunch command (e.g roslaunch youbot_gazebo youbot_gazebo ns:=/robot$i ) in a loop, each time with a different namespace value.

edit flag offensive delete link more
3

answered 2016-03-22 08:36:53 -0500

inusO gravatar image

updated 2016-03-22 08:37:20 -0500

As far as I found out, there is no way to do it with a launch file. I solved it by writing a simple python program using the roslaunch API ( http://wiki.ros.org/roslaunch/API%20U... )

edit flag offensive delete link more

Comments

Thank you so so so much for this, this solved a problem we were facing for quite a while!

HatchEm27998 gravatar image HatchEm27998  ( 2021-03-10 06:08:59 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-18 10:38:58 -0500

Seen: 5,165 times

Last updated: Apr 26 '18