Ask Your Question

How to modify the values of arguments in launch file?

asked 2020-04-08 04:03:55 -0500

kane_choigo gravatar image

Hello, I'm using ROS melodic on Ubuntu 18.04.

I'd like to spawn some robots on the Gazebo simulation with a random initial spawn pose.

Unfortunately, the only way I know to access this kind of work is to modify some lines in something kind of gazebo launch file like,

<arg name="init_pose" value="-x 3.0 -y 1.0 -z 0 -R 0 -P 0 -Y 0.0" />

What I want to do is to set the initial pose randomly, not with the pre-set pose.

I also can edit the file wrt gazebo spawn, but I have a feeling that that would spoil my work-environment.

Can I get the random initial pose argument in launch file?

Thanks in advance. :)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-04-08 10:45:11 -0500

Weasfas gravatar image

Hi @kane_choigo,

You can use the roslaunch API to launch your files programatically. Besides, in the script you can import numpy to generate random numbers that you can fetch to the launch arguments.

I will provide an example to solve this problem:

Imagine you have a launch file named my_launch.launch with the init_pose argument under the package my_pkg

<?xml version="1.0"?>
  <arg name="init_pose" default="-x 0.0 -y 0.0 -z 0 -R 0 -P 0 -Y 0.0"/>

You can use the roslaunch API to implement a script like:

#!/usr/bin/env python
import roslaunch
import rospy
import numpy as np

value ="-x 0.0 -y 0.0 -z 0.0 -R 0.0 -P 0.0 -Y 0.0"

x_pos = np.random.uniform(low=0.0, high=10.0)
y_pos = np.random.uniform(low=0.0, high=10.0)
z_pos = np.random.uniform(low=0.0, high=10.0)

R_pos = np.random.uniform(low=0.0, high=3.14)
P_pos = np.random.uniform(low=0.0, high=3.14)
Y_pos = np.random.uniform(low=0.0, high=3.14)

value = "-x " + str(x_pos) + " -y " + str(y_pos) + " -z " + str(z_pos) + " -R " +str(R_pos) + " -P " + str(P_pos) + " -Y " +str (Y_pos)

arg_1 = 'init_pose:=' + value 

uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)

cli_args = ['my_pkg', 'my_launch.launch', arg_1]
roslaunch_args = cli_args[2:]
roslaunch_file = [(roslaunch.rlutil.resolve_launch_arguments(cli_args)[0], roslaunch_args)]

parent = roslaunch.parent.ROSLaunchParent(uuid, roslaunch_file)


edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2020-04-08 04:03:55 -0500

Seen: 219 times

Last updated: Apr 08 '20