How to spawn a Gazebo robot using XML launch files

How to spawn a Gazebo robot using XML launch files

Written by Ruben Alves

28/07/2023

What we are going to learn

  1. How to start Gazebo
  2. How to spawn a robot to Gazebo
  3. How to run the Robot State Publisher node
  4. How to start Rviz configured

List of resources used in this post

  1. Use the rosject: https://app.theconstructsim.com/l/56476c77/
  2. The Construct: https://app.theconstructsim.com/
  3. ROS2 Courses –▸
    1. ROS2 Basics in 5 Days Humble (Python): https://app.theconstructsim.com/Course/132
    2. ROS2 Basics in 5 Days Humble (C++): https://app.theconstructsim.com/Course/133

Overview

While many examples demonstrate how to spawn Gazebo robots using Python launch files, in this post, we will be learning how to achieve the same result using XML launch files. Let’s get started!

ROS Inside!

ROS Inside

ROS Inside

Before anything else, if you want to use the logo above on your own robot or computer, feel free to download it and attach it to your robot. It is really free. Find it in the link below:

ROS Inside logo

Opening the rosject

In order to follow this tutorial, we need to have ROS2 installed in our system, and ideally a ros2_ws (ROS2 Workspace). To make your life easier, we have already prepared a rosject with a simulation for that: https://app.theconstructsim.com/l/56476c77/.

You can download the rosject on your own computer if you want to work locally, but just by copying the rosject (clicking the link), you will have a setup already prepared for you.

After the rosject has been successfully copied to your own area, you should see a Run button. Just click that button to launch the rosject (below you have a rosject example).

Learn ROS2 Parameters - Run rosject

How to spawn a Gazebo robot using XML launch files – Run rosject (example of the RUN button)

 

After pressing the Run button, you should have the rosject loaded. Now, let’s head to the next section to get some real practice.

Compiling the workspace

As you may already know, instead of using a real robot, we are going to use a simulation. In order to spawn that simulated robot, we need to have our workspace compiled, and for that, we need a terminal.

Let’s open a terminal by clicking the Open a new terminal button.

 

Open a new Terminal

Open a new Terminal

 

Once inside the first terminal, let’s run the commands below, to compile the workspace

cd ~/ros2_ws
colcon build
source install/setup.bash
There may be some warning messages when running “colcon build”. Let’s just ignore those messages for now.
If everything went well, you should have a message saying that 3 packages were compiled:
How to spawn a Gazebo robot using XML launch files - ros2_ws compiled

How to spawn a Gazebo robot using XML launch files – ros2_ws compiled

Starting the Gazebo simulator

Now that our workspace is compiled, let’s run a gazebo simulation and RViz using normal python launch files.

For that, run the following command in the terminal:

ros2 launch minimal_diff_drive_robot gazebo_and_rviz.launch.py
Again, you may see some error messages. As long as the simulation appears, you can just ignore those error messages.
Now, in a second terminal, let’s also launch the Robot State Publisher, so that we can properly see the robot in RViz (Robot Visualization tool).

 

ros2 run joint_state_publisher joint_state_publisher
Now you should be able to see both Gazebo simulator and RViz, similar to what we can see in the image below:
How to spawn a Gazebo robot using XML launch files - Gazebo and RViz launched

How to spawn a Gazebo robot using XML launch files – Gazebo and RViz launched

In case you want to know, the content of the file used to spawn the robot can be seen with:

cat ~/ros2_ws/src/minimal_diff_drive_robot/launch/gazebo_and_rviz.launch.py

 

Moving the robot around

To make sure everything is working as expected so far, you can also run a new command to move the robot around using the keyboard. For that, open a third terminal, and run the following command:

ros2 run teleop_twist_keyboard teleop_twist_keyboard

Now, to move the robot around just press the keys “i“, “k“, or other keys presented in the terminal where you launched the teleop_twist_keyboard command.

The XML file for launching spawning the robot

As we mentioned earlier, the code for the python launch file can be found seen with:

cat ~/ros2_ws/src/minimal_diff_drive_robot/launch/gazebo_and_rviz.launch.py

 

and for the XML file? The XML file is in the exact same folder, but the file has an XML extension. The content of the file can be seen with:

cat ~/ros2_ws/src/minimal_diff_drive_robot/launch/gazebo_and_rviz.launch.xml
The command above outputs the following:
<?xml version="1.0"?>

<launch>
  <arg name="model" default="$(find-pkg-share minimal_diff_drive_robot)/urdf/minimal_diff_drive_robot.urdf" />

  <arg name="start_gazebo" default="true" />
  <arg name="start_rviz" default="true" />

  <!-- Start Gazebo -->
  <group if="$(var start_gazebo)">
    <include file="$(find-pkg-share gazebo_ros)/launch/gazebo.launch.py">
      <!--arg name="paused" value="true"/>
      <arg name="use_sim_time" value="true"/>
      <arg name="gui" value="true"/>
      <arg name="recording" value="false"/>
      <arg name="debug" value="false"/>
      <arg name="verbose" value="true"/-->
    </include>

    <!-- Spawn robot in Gazebo -->
    <node name="spawn_robot_urdf" pkg="gazebo_ros" exec="spawn_entity.py"
      args="-file $(var model) -x 0.0 -y 0.0 -z 0.0 -entity my_robot" output="screen" />
  </group>

  <!-- TF description -->
  <node name="robot_state_publisher" pkg="robot_state_publisher" exec="robot_state_publisher" output="screen">
    <param name="robot_description" value="$(command 'cat $(var model)')"/>
    <param name="use_sim_time" value="true" />
  </node>

  <!-- Show in Rviz   -->
  <group if="$(var start_rviz)">
    <node name="rviz" pkg="rviz2" exec="rviz2" args="-d $(find-pkg-share minimal_diff_drive_robot)/config/robot.rviz">
      <param name="use_sim_time" value="true" />
    </node>
  </group>

</launch>

If we check carefully the output above, we can see that we start launching the Gazebo simulator, and in the same <group> we spawn the robot in Gazebo by calling spawn_entity.py

Then we launch the Robot State Publisher to be able to see the robot in RViz, and finally, we launch RViz itself.

When launching RViz, we tell it to use a file named config/robot.rviz, as we can see at:

$(find-pkg-share minimal_diff_drive_robot)/config/robot.rviz
That “command” translates to the following path:
cat ~/ros2_ws/src/minimal_diff_drive_robot/config/robot.rviz
Feel free to check the content of that file, be it through the Code Editor, or in the terminal by checking what the cat command outputs.

Spawning the robot in Gazebo using XML launch files

Similar to what we did with Python, you can just run the following command to spawn the robot using XML file.

Please, remember to kill the previous programs by pressing CTRL+C in the terminals where you launched the commands previously.

Assuming that now all previous programs are terminated, let’s spawn gazebo using XML in the first terminal:

ros2 launch minimal_diff_drive_robot gazebo_and_rviz.launch.xml
Now, in the second terminal, let’s launch the Joint State Publisher to be able to correctly see the robot wheels in RViz:
ros2 run joint_state_publisher joint_state_publisher

And on the third terminal, you can start the command to move the robot around:

ros2 run teleop_twist_keyboard teleop_twist_keyboard

And that is basically it

Congratulations. Now you know how to spawn a robot in Gazebo using Python and also using XML launch files.

We hope this post was really helpful to you. If you want a live version of this post with more details, please check the video in the next section.

Youtube video

So this is the post for today. Remember that we have the live version of this post on YouTube. If you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.

Keep pushing your ROS Learning.

Related Courses & Training

If you want to learn more about ROS and ROS2, we recommend the following courses:

Get ROS2 Industrial Ready- Hands-On Training by The Construct cover.png

Get ROS2 Industrial Ready- Hands-On Training by The Construct cover.png

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

0 Comments

Pin It on Pinterest

Share This