How to add and remove models in Gazebo simulation dynamically

In this article, we will present how we can add and remove models in Gazebo simulation dynamically. By dynamically, we meana the ability to add and remove models after the Gazebo process is launched.

Suppose we define a world called demo in the sdf file and launch a Gazebo instance with it. When the Gazebo is up and running, we should see a few services are created automatically. For instance, if we run the command:

ign service -l | grep "world"

It produces the following output:

/gazebo/worlds
/world/demo/control
/world/demo/control/state
/world/demo/create
/world/demo/create_multiple
/world/demo/declare_parameter
/world/demo/disable_collision
/world/demo/enable_collision
/world/demo/entity/system/add
/world/demo/generate_world_sdf
/world/demo/get_parameter
/world/demo/gui/info
/world/demo/level/set_performer
/world/demo/light_config
/world/demo/list_parameters
/world/demo/playback/control
/world/demo/remove
/world/demo/scene/graph
/world/demo/scene/info
/world/demo/set_parameter
/world/demo/set_physics
/world/demo/set_pose
/world/demo/set_pose_vector
/world/demo/set_spherical_coordinates
/world/demo/state
/world/demo/state_async
/world/demo/system/info
/world/demo/visual_config
/world/demo/wheel_slip

Our focus in this article is on /world/demo/create and /world/demo/remove.

The command we will use is:

To find out the request type and response type of the service, we can use command ign service -is. For example:

It produces:

We can check the detials of the message with command ign msg -i. For example:

and it produces:

As expected, we need to provide the following information to spawn a new model instance:

  • name: Model nam. This is also the "identifier" of the model instance and needs to be unique.

  • model source: In this article, we use sdf_filename option and will provide a path of the model sdf file in the request.

  • pose: The pose of the model instance.

Examine the .ignition.msgs.Pose message type and it shows we can specify both position and the orientation of the model instance.

The command below creates a new model instance in Gazebo with the following attributes:

  • The model name is test

  • The model definition sdf file is diff_drive/model.sdf

  • The initial pose is at (10, 10, 0) with orientation (0, 0, 0.7071, 0.7071) (i.e. theta=90 degree)

To remove the model instance, we can send a request to /world/demo/remove service. For instance:

Last updated