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

How to modify the values of arguments in launch file?

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

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
0

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

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"?>
<launch>
  <arg name="init_pose" default="-x 0.0 -y 0.0 -z 0 -R 0 -P 0 -Y 0.0"/>
  ...
</launch>

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)
roslaunch.configure_logging(uuid)

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)

parent.start()

try:
  parent.spin()
finally:
  parent.shutdown()
edit flag offensive delete link more

Question Tools

2 followers

Stats

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

Seen: 836 times

Last updated: Apr 08 '20