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

I want to spawn my robot to a random position but failed modifying the launch file.

asked 2022-01-25 08:12:06 -0500

cubpaw gravatar image

updated 2022-01-25 09:12:03 -0500

Hi,

I'm pretty new to ros, and is struggling to find a way to spawn my robot in a random position every time I launch my robot.

The file I want to launch looks like this

  <launch>
  <arg name="mav_name" default="firefly"/>
  <arg name="world_name" default="basic"/>
  <arg name="enable_logging" default="false"/>
  <arg name="enable_ground_truth" default="true"/>
  <arg name="log_file" default="$(arg mav_name)"/>
  <arg name="debug" default="false"/>
  <arg name="gui" default="true"/>
  <arg name="paused" default="true"/>
  <!-- The following line causes gzmsg and gzerr messages to be printed to the console
      (even when Gazebo is started through roslaunch) -->
  <arg name="verbose" default="false"/>

  <env name="GAZEBO_MODEL_PATH" value="${GAZEBO_MODEL_PATH}:$(find rotors_gazebo)/models"/>
  <env name="GAZEBO_RESOURCE_PATH" value="${GAZEBO_RESOURCE_PATH}:$(find rotors_gazebo)/models"/>
  <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="world_name" value="$(find rotors_gazebo)/worlds/$(arg world_name).world"/>
    <arg name="debug" value="$(arg debug)" />
    <arg name="paused" value="$(arg paused)" />
    <arg name="gui" value="$(arg gui)" />
    <arg name="verbose" value="$(arg verbose)"/>
  </include>

  <group ns="$(arg mav_name)">
    <include file="$(find rotors_gazebo)/launch/spawn_mav.launch">
      <arg name="mav_name" value="$(arg mav_name)" />
      <arg name="model" value="$(find rotors_description)/urdf/$(arg mav_name)_base.xacro" />
      <arg name="enable_logging" value="$(arg enable_logging)" />
      <arg name="enable_ground_truth" value="$(arg enable_ground_truth)" />
      <arg name="log_file" value="$(arg log_file)"/>
    </include>
  </group>
</launch>

And this calls spawn_mav.launch file, which I want to pass my random values to

    <?xml version="1.0"?>

<launch>
  <arg name="mav_name" default="firefly"/>
  <arg name="namespace" default="$(arg mav_name)"/>
  <arg name="model" default="$(find rotors_description)/urdf/$(arg mav_name)_base.xacro"/>
  <arg name="tf_prefix" default="$(optenv ROS_NAMESPACE)"/>
  <arg name="x" default="0"/>
  <arg name="y" default="0"/>
  <arg name="z" default="3"/>
  <arg name="enable_logging" default="false"/>
  <arg name="enable_ground_truth" default="true"/>
  <arg name="log_file" default="$(arg mav_name)"/>
  <arg name="wait_to_record_bag" default="false"/>
  <arg name="enable_mavlink_interface" default="false"/>

  <!-- send the robot XML to param server -->
  <param name="robot_description" command="
    $(find xacro)/xacro '$(arg model)'
    enable_logging:=$(arg enable_logging)
    enable_ground_truth:=$(arg enable_ground_truth)
    enable_mavlink_interface:=$(arg enable_mavlink_interface)
    log_file:=$(arg log_file)
    wait_to_record_bag:=$(arg wait_to_record_bag)
    mav_name:=$(arg mav_name)
    namespace:=$(arg namespace)"
  />
  <param name="tf_prefix" type="string" value="$(arg tf_prefix)"/>
  <!-- push robot_description to factory and spawn robot in gazebo -->
  <node name="spawn_$(arg namespace)" pkg="gazebo_ros" type="spawn_model"
   args="-urdf
         -model $(arg namespace)
         -x $(arg x)
         -y $(arg y)
         -z $(arg z)
         -param robot_description"
   respawn="false" output="screen">
  </node>
</launch>

I want to pass random values generated from my python file to the args -x -y -z, but have no idea how to do achieve this. I have tried to define a param like thie post https://answers.ros.org/question/3666... and add the -param random_postion to the args of the node, but seems like it's not recognizing it ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-01-26 22:18:57 -0500

404RobotNotFound gravatar image

What you are looking to use are launch arguments (arg tags) and not parameters (param tags). Here is a link to the roslaunch arg tags.

Basically, what you are trying to do is add an argument to your higher level launch file that you pass down. Here is a decent guide on them for more detail.

The answer to the question you link uses bash (command line) to generate a random number and pass it as a launch argument to their launch file. How it would look like for you:

High level launch file:

  <launch>
  ... other args ...
  <arg name="x" default="0"/>
  <arg name="y" default="0"/>
  <arg name="z" default="3"/>

 ... empty world include ...

  <group ns="$(arg mav_name)">
    <include file="$(find rotors_gazebo)/launch/spawn_mav.launch">
      <arg name="mav_name" value="$(arg mav_name)" />
      <arg name="model" value="$(find rotors_description)/urdf/$(arg mav_name)_base.xacro" />
      <arg name="enable_logging" value="$(arg enable_logging)" />
      <arg name="enable_ground_truth" value="$(arg enable_ground_truth)" />
      <arg name="log_file" value="$(arg log_file)"/>
      <arg name="x" value="$(arg x)"/>
      <arg name="y" value="$(arg y)"/>
      <arg name="z" value="$(arg z)"/>
    </include>
  </group>
</launch>

And then after that, since the arguments are already in the spawn_mav.launch file, you just need to call roslaunch.

roslaunch package file.launch x:=1 y:=2 z:=3

This will spawn the robot at 1,2,3. Change the input as you see fit.

As described in the other question you link, you can randomize it based on some bash input. The comments for the answer mention the roslaunch Python API. I have not used it, but might be worth exploring to generate the random numbers with Python.

edit flag offensive delete link more

Comments

First of all, thanks for your answer. However unfortunately I still haven't succeeded to make my robot respawn from a random position every time I reset my ros env. I guess I have to figure it out by myself. Thanks again though :D

cubpaw gravatar image cubpaw  ( 2022-02-03 12:40:29 -0500 )edit

@cubpaw What do you mean by "every time I reset my ros env"? Is it every time that you re-launch the launch file? If so, then that is where my comment "The answer to the question you link uses bash (command line) to generate a random number and pass it as a launch argument to their launch file" comes into play. You basically can send it different X, Y, and Z values, you just need to supply them.

404RobotNotFound gravatar image 404RobotNotFound  ( 2022-02-03 14:57:25 -0500 )edit

Question Tools

Stats

Asked: 2022-01-25 08:07:41 -0500

Seen: 514 times

Last updated: Jan 26 '22