[Gazebo in 5 minutes] 004 – How to create a gazebo model using SDF

[Gazebo in 5 minutes] 004 - How to create a gazebo model using SDF

Written by Marco Arruda

02/05/2020

What you will learn

  • Learn how to  create a gazebo model using the SDF Format

List of resources used in this post:

Preparing the environment

In order to load gazebo using ROS, you need to have Gazebo and ROS installed. If you don’t want to install everything, we highly recommend using ROSDS (ROS Development Studio) which gives you access to an online environment with ROS already installed. Indeed, we are going to use that tool for easiness. You can follow the same steps on your computer as well if you don’t want to use ROSDS.

To use ROSDS, you can just create an account and start using it.

Once you have your account created, you have to create a ROSject by clicking on the New ROSject button:

Creating a new ROSject in ROSDS

Creating a new ROSject in ROSDS

Once you have the ROSject, you can open it by clicking Open:

Opening a ROSject in ROSDS

Creating the ROS Package

In order to run anything using ROS, we need a ROS package, so, let’s create one. For that, you are going to need a terminal/shell. In ROSDS, you can have a terminal by clicking Tools -> Shell.

Let’s first create the workspace. In this case, let’s call it ~/simulation_ws

mkdir ~/simulation_ws/src -p

Now let’s then compile our empty workspace

source /opt/ros/kinetic/setup.bash 
source /usr/share/gazebo/setup.sh

cd ~/simulation_ws/

catkin_make

Now let’s create our ROS package. Let’s call it my_simulations:

source ~/simulation_ws/devel/setup.bash

cd ~/simulation_ws/src

catkin_create_pkg my_simulations

Now, let’s create a launch and a world folder inside the my_simulations package.

cd my_simulations

mkdir launch world

Now, inside the launch folder, let’s create a file named my_world.launch:

touch launch/my_world.launch

In that file, let’s put the following content:

<?xml version="1.0" encoding="UTF-8" ?>
<launch>
        <!-- overwriting these args -->
        <arg name="debug" default="false" />
        <arg name="gui" default="true" />
        <arg name="pause" default="false" />
        <arg name="world" default="$(find my_simulations)/world/empty_world.world" />

        <!-- include gazebo_ros launcher -->
        <include file="$(find gazebo_ros)/launch/empty_world.launch">
                <arg name="world_name" value="$(arg world)" />
                <arg name="debug" value="$(arg debug)" />
                <arg name="gui" value="$(arg gui)" />
                <arg name="paused" value="$(arg pause)" />
                <arg name="use_sim_time" value="true" />
        </include>
</launch>

In order to put the content on that file, you can do it using the code editor. For that, click on Tools -> IDE

"Tools

Now, let’s create a file named empty_world.world in the world folder:

touch world/empty_world.world

In that file, let’s add the following content:

<?xml version="1.0" ?>

<sdf version="1.5">
	<world name="default">
		<!-- A global light source -->
		<include>
			<uri>model://sun</uri>
		</include>

		<!-- A ground plane -->
		<include>
			<uri>model://ground_plane</uri>
		</include>
	</world>
</sdf>

 

If you followed the instructions correctly, you should have the following structure:

user:~/simulation_ws/src$ tree .
.
|-- CMakeLists.txt -> /opt/ros/kinetic/share/catkin/cmake/toplevel.cmake
`-- my_simulations
    |-- CMakeLists.txt
    |-- launch
    |   `-- my_world.launch
    |-- package.xml
    `-- world
        `-- empty_world.world

3 directories, 5 files

Creating our model

Let’s create a model named my1stmodel on the my_simulations package:

~/simulation_ws/src/my_simulations/
mkdir -p models/my1stmodel

Inside the my1stmodel folder, let’s create two files, one named model.config and another named model.sdf.

cd models/my1stmodel/
touch model.config model.sdf

These two files, model.config and model.sdf, are required for every gazebo model. You can find those files in the ground_plane model provided by gazebo, for example.

Let’s paste the following content on our my1stmodel/model.config

<?xml version="1.0"?>

<model>
  <name>My first model</name>
  <version>1.0</version>
  <sdf version="1.5">model.sdf</sdf>

  <author>
    <name>Your name</name>
    <email>your@email.com</email>
  </author>

  <description>
	My first model for gazebo
  </description>
</model>

And in our models/my1stmodel/model.sdf, let’s add the following:

<?xml version="1.0" ?>
<sdf version="1.5">
  <model name="my1stmodel">
    <static>false</static>
    <link name="link">
      <collision name="collision">
        <geometry>
          <box>
            <size>3 2 5</size>
          </gox>
        </geometry>
        <surface>
          <friction>
            <ode>
              <mu>100</mu>
              <mu2>50</mu2>
            </ode>
          </friction>
        </surface>
      </collision>
      <visual name="visual">
        <geometry>
          <box>
            <size>3 2 5</size>
          </gox>
        </geometry>
        <material>
          <script>
            <uri>file://media/materials/scripts/gazebo.material</uri>
            <name>Gazebo/Grey</name>
          </script>
        </material>
      </visual>
    </link>
  </model>
</sdf>

Launching our Gazebo model

Let’s modify our world/empty_world.world file created earlier to add the following:

<!-- A ground plane -->
<include>
 <uri>model://my1stmodel</uri>
</include>

In the end, the final content of the file would be:

<?xml version="1.0" ?>

<sdf version="1.5">
	<world name="default">
		<!-- A global light source -->
		<include>
			<uri>model://sun</uri>
		</include>

		<!-- A ground plane -->
		<include>
			<uri>model://ground_plane</uri>
		</include>

        <!-- Our custom model -->
		<include>
			<uri>model://my1stmodel</uri>
		</include>
        
	</world>
</sdf>

Now that we have everything in place, we can run our package in two ways:

Option one: Click Simulation -> Choose File, then select my_world.launch. This should automatically load the web version of gazebo, called gzweb.

Custom gazebo robot model running in ROSDS

Custom gazebo robot model running in ROSDS

Option two: If you are running the tests on your computer, or you want to manually run the simulation, you can just:

source ~/simulation_ws/devel/setup.bash
roslaunch my_simulations my_world.launch --screen

If you are in ROSDS and chose to run the simulation manually, then you have to manually open the Gazebo Web (gzweb) by clicking on Tools -> Gazebo.

Congratulations. You have successfully launched your first custom Gazebo World using ROS.

Youtube video

If you didn’t understand well all the steps explained here or need more understanding of the files we created, remember that we have the live version of this post on YouTube. Also, if you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.


 

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

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This