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

Load parameter in launchfile

asked 2020-11-30 05:21:22 -0500

Herbert gravatar image

updated 2020-11-30 11:10:46 -0500

Hello, I want to load a randomized spawn location from the parameter server.

I created a python script, which stores the location of some predefined strings '-x1 -y1 -z1 -Y0'. A random function selects one of those strings:

rospy.set_param('/robot_spawn_location', randomString)
rospy.get_param("/robot_spawn_location")

This works fine, with "rosparam list" and "rosparam get robot_spawn_location" it is accessible.

But i am confused on how to load the parameter in my launch file. In my node, i want to load the String-Parameter in the args="-p1 -p2 -pN -robot_spawn_location"

<node pkg="gazebo_ros" type="spawn_model" name="spawn_urdf" args="-urdf -model turtlebot3_waffle_pi -robot_spawn_location -param robot_description" />

I tried to load the parameter with the help of serveral tutorial, but none worked. From that source i got my Inspiration.

Anybody got a hint?

System:

  • Ros Noetic
  • Ubuntu LTS 20.04

Edit: Workflow: I created a bash-file to start all necessary launchfiles and starting a roscore (easier to work with). With my maze.launch i want to start in a gazebo map (it is a maze) the turtlebot at a random location. The location is currently only 1 Position (=string) (which when implemented in the launch works just fine). To spawn randomly I created a python file which chooses a string from a list and this random string is then set as a ros parameter. In the launchfile I wanted to load that parameter.

python code randomizer (short version):

startPosition = [
        '-x 0.625 -y 0.625 -z 0 -Y 0',
        '-x 0.625 -y 0.625 -z 0 -Y 0',
        '-x 0.625 -y 0.625 -z 0 -Y 0',]
    getRandomPosition = random.choice(startPosition)
    rospy.set_param('/robot_spawn_location', getRandomPosition)
    if rospy.has_param('/robot_spawn_location'):
        rospy.get_param("/robot_spawn_location")
    else:
        print("No Spawn Location Set")
        return -1

code launchfile:

<?xml version="1.0" ?>
<launch>

    <include file="$(find gazebo_ros)launch/empty_world.launch" >
        <arg name="world_name" value="$(find myPkg)/maps/Maze.world" />
        <arg name="paused" value="false" />
        <arg name="use_sim_time" value="true" />
        <arg name="gui" value="false" /> 
    </include>

    <include file="$(find turtlebot3_bringup)launch/turtlebot3_remote.launch" /> 

    <!-- Set Parameter -->
    <param name="robot_spawn_location" command="python3 $(find myPkg)/launch/randomizer.py"/>
    <param name="robot_description" command="$(find xacro)/xacro --inorder $(find turtlebot3_description)/urdf/turtlebot3_waffle_pi.urdf.xacro" />

    <node pkg="gazebo_ros" type="spawn_model" name="spawn_urdf" args="
               -urdf 
               -model turtlebot3_waffle_pi 
               -param robot_spawn_location 
               -param robot_description" />    

    <!--ORIGINAL: node pkg="gazebo_ros" type="spawn_model" name="spawn_urdf" args="-urdf -model turtlebot3_waffle_pi -x 0.625 -y 0.625 -z 0 -Y 0 -param robot_description" / -->

</launch>

With that it did not load the parameter (seen in Parameter List of roslaunch) and spawns therefore at its default (i guess) location in the map. The parameter is there, but empty. While testing the python file, i started a roscore and then run the script and verified if the parameter was set correctly, and it is. With that i don't get an error, but the handling doesn't work.

edit retag flag offensive close merge delete

Comments

Your current workflow is not entirely clear to me. Can you post your code? What error(s) are you getting when you try the solution in the answer you linked?

tryan gravatar image tryan  ( 2020-11-30 10:10:37 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-12-02 09:09:56 -0500

Herbert gravatar image

updated 2020-12-02 09:11:06 -0500

Solution with different approach

Different approach is to generate the position in the bash file and pass them as arguments in the launchfile:

FIRST: In the bashfile use, and replace [RANGE] with the numeric range.

x_pos=$(bc <<< "scale=2;$(($RANDOM % 10))*[RANGE]")
y_pos=$(bc <<< "scale=2;$(($RANDOM % 10))*[RANGE]")

SECOND: Then call the roslaunch:

roslaunch your_pkg project.launch x_pos:=$x_pos y_pos:=$y_pos&

THIRD: In the project.lauch init the pos:

  <!-- declare arg to be passed in -->
  <arg name="x_pos" /> 
  <arg name="y_pos" />

... other launch stuff ...

LAST: Get the randomized parameter with:

   <node pkg="gazebo_ros" type="spawn_model" name="spawn_urdf" args="-urdf -model turtlebot3_waffle_pi -x $(arg x_pos) -y $(arg y_pos) -z 0 -Y 0 -param robot_description" />

It is also possible to write the generated values in the terminal via echo random x: $x_pos in the bash file. This works now just fine and is also portable.

edit flag offensive delete link more

Comments

If you're willing to start a script before launching, you might consider just launching from the script, where you could set the arguments directly. You could even do that in Python, using the roslaunch API.

tryan gravatar image tryan  ( 2020-12-02 10:32:38 -0500 )edit

oh okay thats nice to know, thanks i will have a look!

Herbert gravatar image Herbert  ( 2020-12-02 10:35:53 -0500 )edit
0

answered 2020-11-30 11:29:14 -0500

lucascoelho gravatar image

Your randomizer.py script needs to output the randomly chosen string:

if rospy.has_param('/robot_spawn_location'):
        print(rospy.get_param("/robot_spawn_location"))

So then it will be loaded on the param robot_spawn_location

edit flag offensive delete link more

Comments

thanks for the idea, i tried that as well before, but it still does not work. The output in the terminal is i guess correctly:

 * /robot_spawn_location: -x 0.625 -y 0.625...

What i just saw, that with the output via rosparam get robot_spawn_location is actually 2 lines:

'-x 0.625 -y 0.625 -z 0 -Y 0

  '

Is that a problem?

Herbert gravatar image Herbert  ( 2020-11-30 12:11:33 -0500 )edit

print will automatically put a newline character at the end of the string. You can try printing without the newline with the instructions here: https://careerkarma.com/blog/python-p...

lucascoelho gravatar image lucascoelho  ( 2020-11-30 12:56:28 -0500 )edit

unfortunate this wasn't the issue ... but thanks anyway

Herbert gravatar image Herbert  ( 2020-11-30 16:20:40 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-11-30 05:21:22 -0500

Seen: 391 times

Last updated: Dec 02 '20