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

Revision history [back]

click to hide/show revision 1
initial version

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()