Gazebo is a free and open source robot simulation environment developed by Willow Garage. As a multifunctional tool for ROS robot developers, Gazebo supports the following:
In this section, we will load our two-wheeled robot URDF into Gazebo to visualize it in a 3D environment. Gazebo allows you to take control of some aspects of our model without an external control program. In the later chapters, we will be using simulated versions of robots in Gazebo to control joints, visualize sensor data, and test control algorithms.
To run Gazebo requires a powerful graphics card and the appropriate drivers be installed on your computer.
If you have trouble with running Gazebo, refer to http://answers.gazebosim.org/questions/ or search the ROS forum at http://answers.ros.org/questions/.
Gazebo should have been installed on your computer as part of the ros-indigo-desktop-full installation, as described in the Installing and launching ROS section in Chapter 1, Getting Started with ROS. Gazebo 2 is the default version of Gazebo for ROS-Indigo/Ubuntu-Trusty and is the version recommended for the exercises in this book.
To test whether Gazebo has been installed correctly, open a terminal window and type the following command:
$ gazebo
This should display an environment similar to the following screenshot:
If Gazebo has not been installed, refer to the ROS-Indigo installation instructions at http://wiki.ros.org/indigo/Installation/Ubuntu or the general Gazebo installation instructions at http://gazebosim.org/tutorials?cat=install. Make sure that you install the Gazebo 2 version.
The $ gazebo
command runs two different executables: the Gazebo server and the Gazebo client. The Gazebo server gzserver
will execute the simulation process, including the physics update loop and sensor data generation. This process is the core of Gazebo and can be used independently of any graphical interface. The Gazebo client gzclient
command runs the Gazebo GUI. This GUI provides a nice visualization of simulation and handy controls for an assortment of simulation properties.
Tutorials for Gazebo can be found at http://gazebosim.org/tutorials.
Use the Ctrl + C keys to kill the terminal window process after you have closed the Gazebo window.
Important commands: If at any time, your command generates a warning or error command, type $ rosnode list
to determine whether there are any active nodes still lingering after you have attempted to shut down Gazebo. If there are still active nodes, use the $ rosnode kill
command to list the active nodes. Next, select the number of the ROS nodes that you wish to kill.
Roslaunch is a standard method used to start Gazebo with world files and robot URDF models. To perform a basic test of Gazebo, an empty Gazebo world can be brought up for the following command:
$ roslaunch gazebo_ros empty_world.launch
This test will verify that Gazebo has been installed properly. If you wish to try other demo worlds, including the gazebo_ros
package, try substituting one of the following with empty_world.launch
in the previous command:
willowgarage_world.launch
mud_world.launch
shapes_world.launch
rubble_world.launch
For the exercises in this chapter, we created our own world, ddrobot.world
. This world consists of a ground plane and two construction cones for you to drive the robot around. You will find this file in the ros_robotics
package under ros_robotics/worlds
. We will launch our dd_robot
model into this world using the ddrobot_gazebo.launch
launch file found in the ros_robotics/launch
directory.
The Gazebo GUI is similar to rviz in many ways. The central window provides the view for Gazebo's 3D world environment. The grid is typically configured to be the ground plane of the environment on which all the models are held due to gravity in the environment.
Gazebo also has the same cursor/mouse control as rviz, described in the Using rviz section. For Gazebo keyboard shortcuts, visit http://gazebosim.org/hotkeys.
Double-clicking on a spot in the environment will cause the display to be zoomed in to that spot.
The main screen of Gazebo is divided into four main display areas: the central window, the World and Insert panels to the left, the Joints panel to the right, and the Simulation panel at the bottom. Across the top of these display areas are the Environment toolbar and the main screen menu bar. Each of the display areas of Gazebo will be described in the following sections. This overview should provide basic familiarity with the Gazebo GUI.
The toolbar at the top of the Gazebo environment display provides the following functionalities:
x
, y
, and z
plane. (Hitting the T key will also activate this mode.)/home/<username>/.gazebo/pictures
directory.The World panel to the left of the 3D environment provides access to all of the environment elements. These environment elements are Scene, Physics, Models, and Lights. By clicking on any of these labels, a properties panel will appear below the World panel with a properties list specific for that element.
The Scene selections allow the user to alter the ambient environment, the background and the shadows. The Physics selections check whether the physics engine is enabled. If the physics engine is enabled, the user can control the real-time update rate, gravity, and constraints under the Physics tab as well as other properties. The Models list will display all models active in the environment. When the $ gazebo
command is used, the only active model will be the ground_plane
model. By clicking on the ground_plane label, the properties displayed will be the model name, a checkbox to make the model static, the model's pose (x, y, z, roll, pitch, and yaw) in the environment, and its link information.
These details are lengthy so you can explore them at your leisure. The last element, Lights, displays all the light sources for the environment. For our default environment, the sun is the only source. The properties of the sun are its pose, diffuse, specular, range, and attenuation. Our primary interest for this book will be the Models.
The Insert tab is behind the World panel. The Insert panel accesses two locations to allow the user to select from a number of predefined models to be added to the environment. The first location, /home/<username>/.gazebo/models
, is the user's repository of Gazebo models that they have selected from the main Gazebo repository. This repository is the second selection available at http://gazebosim.org/models/.
The panels to the right and left of the center display can be revealed or hidden using the three tiny rectangles in the black vertical strip. On the right side, the user can click and drag these three tiny rectangles to reveal the Joints panel. Under the Joints panel, there are three tabs labeled: Force, Position, and Velocity. There is also a Reset button to return your model to its original state (if possible).
To use these controls, the model must be selected to reveal the available model joints. For the joint control, we have the following values:
The top-most main window menu bar provides options under the basic File, Edit, View, Window, and Help headings as shown here:
These selections can be very useful. For example, if you wish to check the center of mass for your URDF in Gazebo, click on the View heading and select both the Wireframe and the Center of Mass options.
Gazebo expects the robot model file to be in SDF format. SDF is similar to the URDF, using some of the same XML descriptive tags. With the following modifications, Gazebo will automatically convert the URDF code into an SDF robot description. The following sections will describe the steps to be taken.
The <gazebo>
tag must be added to the URDF to specify additional elements needed for simulation in Gazebo. This tag allows for identifying elements found in the SDF format that are not found in the URDF format. If a <gazebo>
tag is used without a reference=""
property, it is assumed that the <gazebo>
elements refer to the whole robot model. The reference parameter usually refers to a specific robot link.
Other <gazebo>
elements for both the links and joints can be applied to your robot but are not described in this book because of the extensive list and explanations of how they applied to the physics of Gazebo.
Refer to the Gazebo tutorial at http://gazebosim.org/tutorials/?tut=ros_urdf for a list of these elements and their usage.
The method of specifying link colors in rviz does not work in Gazebo since Gazebo has adopted OGRE's material scripts for coloring and texturing links. Therefore, a Gazebo <material> tag must be specified for each link. These tags can be placed in the model file just before the ending </robot>
tag:
<gazebo reference="base_link"> <material>Gazebo/Blue</material> </gazebo> <gazebo reference="right_wheel"> <material>Gazebo/Black</material> </gazebo> <gazebo reference="left_wheel"> <material>Gazebo/Black</material> </gazebo>
Gazebo will not use <visual>
elements the same as <collision>
elements if you do not explicitly specify them for each link. Instead, Gazebo will treat your link as invisible to laser scanners and collision checking. If your model ends up partially embedded in Gazebo's ground plane, you should check your <collision>
elements.
The dd_robot
URDF has been updated with the <gazebo>
tags and <material>
elements, as described earlier, and is stored in the downloaded file, dd_robot.gazebo
. The .gazebo
extension is used by the author to signify that this file is ready for use in Gazebo. An easy tool exists to check whether your URDF can be properly converted into an SDF. Simply run the following command:
$ gzsdf print dd_robot.gazebo
or
$ gzsdf print $(rospack find ros_robotics)/urdf/dd_robot.gazebo
This command outputs the entire SDF to the screen so you may wish to redirect the output to a file. The output will show you the SDF that has been generated from your input URDF as well as any warnings about the missing information required to generate the SDF.
Viewing the dd_robot
model in Gazebo requires a launch file obtained or created by one of the following ways:
ddrobot_gazebo.launch
file from the ros_robotics/launch
directory from the book's websiteddrobot_gazebo.launch
file from the following XML code:<launch> <!-- We resume the logic in gazebo_ros package empty_world.launch, changing only the name of the world to be launched --> <include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="world_name" value="$(find ros_robotics)/worlds/ddrobot.world"/> <arg name="paused" default="false"/> <arg name="use_sim_time" default="true"/> <arg name="gui" default="true"/> <arg name="headless" default="false"/> <arg name="debug" default="false"/> </include> <!-- Spawn dd_robot into Gazebo --> <node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" output="screen" args="-file $(find ros_robotics)/urdf/dd_robot.gazebo -urdf -model ddrobot" /> </launch>
This launch file inherits most of the necessary functionality from empty_world.launch
from the gazebo_ros
package. The only parameter that is changed is the world_name
parameter by substituting the ddrobot.world
world file. In addition to this, our URDF-based dd_robot
model is launched into Gazebo using the ROS spawn_model
service from the gazebo_ros
ROS node. If you plan to reuse this code or share it, it is recommended that you add the dependency to your package.xml
file for the ros_robotics
package. The following statement should be added under dependencies:
<exec_depend>gazebo_ros</exec_depend>
The ddrobot.world
world file contains a ground plane and two construction cones. This file can be found in the ros_robotics/worlds
directory on the book's website, or you can create the ddrobot.world
file from the following code:
<?xml version="1.0" ?> <sdf version="1.4"> <world name="default"> <include> <uri>model://ground_plane</uri> </include> <include> <uri>model://sun</uri> </include> <include> <uri>model://construction_cone</uri> <name>construction_cone</name> <pose>-3.0 0 0 0 0 0</pose> </include> <include> <uri>model://construction_cone</uri> <name>construction_cone</name> <pose>3.0 0 0 0 0 0</pose> </include> </world> </sdf>
Now we are ready to launch our dd_robot
model in Gazebo by typing the following command:
$ roslaunch ros_robotics ddrobot_gazebo.launch
This command will launch both the Gazebo server and GUI client with the dd_robot
model and world automatically launched inside the Gazebo environment. Gazebo will look similar to the following screenshot:
If your robot model behaves unexpectedly within Gazebo, it is likely because your model URDF needs further tuning to accurately represent its physics in Gazebo. Refer to the SDF user guide at http://sdformat.org/spec for more information on various properties available in Gazebo, which are also available in the URDF via the <gazebo>
tag.
To understand the physics of Gazebo, it is important to play with your model in Gazebo. Use the Selection, Translation, and Rotation modes on the Environment toolbar to move your model to different positions, and then watch how the gravity model works. If you are brave, you can even manipulate the environment to experiment with the relationship of the elements. For example, remove the ground plane and see what happens.
Simple joint control of our dd_robot
model is possible by using the Joints panel, which is to the right of the center environment. In selection mode, click on the ddrobot model and the model will be highlighted with a white outline box. The joint_left_wheel and joint_right_wheel will appear under the tabbed sections with a value of 0.000 for each of the input windows. We will experiment by changing the values of the left and right wheel joints to see the dd_robot
model move around on the ground plane. Values for Force and Position cause the robot to move; Velocity causes the robot to collapse. The following screenshot shows our dd_robot
ready to be controlled via the Joints panel:
A greater control of our model can be achieved by adding transmission blocks to the URDF for the model joints. Gazebo plugins are also needed to simulate controllers that publish ROS messages for motor commands. A discussion of these advanced topics will be delayed until Chapter 5, Creating Your First Robot Arm (in Simulation) when the reader has a better understanding of ROS messages for control of mobile robots. Chapter 5, Creating Your First Robot Arm (in Simulation) will walk you through the construction of a URDF/SDF for a robot arm with a joint control implemented via Gazebo plugins. The implementation of transmission blocks and plugins for our dd_robot
model is left as an exercise on completion of Chapter 5, Creating Your First Robot Arm (in Simulation).
Gazebo is only one simulator that can interface to ROS and ROS models. A list of other simulators, both open source and commercial, is provided along with a website reference: