Creating a World in ROS with Gazebo - dhanushshettigar/Getting-Started-With-ROS2 GitHub Wiki
Creating a custom world in Gazebo provides a foundational setup to include models, physics, and plugins. This guide walks you through creating and saving a basic world file in Gazebo, inserting models from the Gazebo Fuel App, and inspecting your custom world.
Starting with an empty world gives us a simple SDF file, which we can customize with external models, physics settings, or plugins. Each world in Gazebo requires an SDF file for configuration, so this step prepares us for further modifications.
To launch Gazebo Sim, open a terminal and use the following command:
export QT_QPA_PLATFORM=xcb && gz sim
In the Gazebo Sim interface, select Empty World from the quick start menu.
Once the empty world opens, save it in SDF format by following these steps:
- Go to File > Save As and ensure the file extension is .sdf.
Note: Saving in SDF format is essential for compatibility with custom models and plugins.
After saving, locate the file and open it in a text editor. The initial structure of the SDF file will look like this:
<?xml version="1.0" ?>
<sdf version="1.6">
<world name="default">
<!-- World configuration goes here -->
</world>
</sdf>
This base structure is what we'll expand upon to add models and customize the environment.
To add models to your world, visit the Gazebo Fuel App and search for the model you want to use.
Once you've found a model, click on it. Use the <> button to copy an SDF snippet of the model.
The snippet will look like this:
<include>
<uri>
https://fuel.gazebosim.org/1.0/OpenRobotics/models/Mine Cart Engine
</uri>
</include>
Open the saved SDF file from the earlier steps and paste the SDF snippet inside the tags, like this:
<?xml version="1.0" ?>
<sdf version="1.6">
<world name="default">
<include>
<uri>
https://fuel.gazebosim.org/1.0/OpenRobotics/models/Mine Cart Engine
</uri>
</include>
</world>
</sdf>
This addition will load the external model into your world when you start the simulation.
To launch the modified world in Gazebo, use the following command in your terminal, replacing with the path to your saved SDF file:
export QT_QPA_PLATFORM=xcb && gz sim "<filename>.sdf"
This command opens Gazebo with your custom world, complete with external models.
After inserting a model, you may want to adjust its position, orientation, or other properties. In the SDF snippet, you can define these attributes within and other tags.
-
Position and Orientation: To set the model’s position in the world, add a
<pose>
tag. Position values are specified in meters (x, y, z), and orientation in radians (roll, pitch, yaw).
Example:
<include>
<uri>https://fuel.gazebosim.org/1.0/OpenRobotics/models/Mine Cart Engine</uri>
<pose>2 0 0 0 0 1.57</pose> <!-- x=2m, y=0, z=0; yaw=1.57 radians (90 degrees) -->
</include>
- Scale: If you need to resize the model, you can use the tag.
Example:
<include>
<uri>https://fuel.gazebosim.org/1.0/OpenRobotics/models/Mine Cart Engine</uri>
<pose>2 0 0 0 0 1.57</pose>
<scale>1.5 1.5 1.5</scale> <!-- Scales the model 1.5 times its original size in all directions -->
</include>
- Other Properties: Depending on the model, additional tags such as can set the model to be immovable (useful for objects like buildings).
Example:
<include>
<uri>https://fuel.gazebosim.org/1.0/OpenRobotics/models/Mine Cart Engine</uri>
<pose>2 0 0 0 0 1.57</pose>
<static>true</static> <!-- Sets the model as static (it won’t move or respond to physics) -->
</include>
After making these adjustments, save your SDF file and reopen it in Gazebo to see your changes.