🤖
Introduction to ROS2 and Robotics
  • Introduction
  • ROS2
    • Index
    • IDE and CMake Setup
      • How to add additional include search path
    • ROS2 Building Blocks
      • ROS Workspace and Package Layout
      • Launch File
      • tf2
      • Quality of Service
      • Configurations
        • Rviz Configuration
      • Built-in Types
        • Built-in Message Type
    • ROS Architecture
      • Intra-process Communication
    • Navigation and Planning
      • Navigation Stack and Concepts
      • Navigation2 Implementation Overview
        • 🏗️Cost Map
        • Obstacle Avoidance and DWB Controller
      • DWB Controller
      • Page 5
    • How to launch the Nav2 stack
    • ROS2 Control
      • Online Resources
      • Overview of Codebase
    • 🍳Cookbook
      • Useful Commands
      • How to specify parameters
      • How to build the workspace
      • 🏗️How to publish message to a topic from command line?
      • How to inspect service and make a service call
      • How to properly terminate ROS and Gazebo
      • How to add and remove models in Gazebo simulation dynamically
      • 🚧How to spin nodes
    • 🛒Tutorials
      • Services and Communication between ROS2 and Gazebo
      • Subscription and Message Filters Demo
      • Executor and Spin Explained
      • Lifecycle Node Demo
      • Robotic Arm Demo
      • ⚒️Multiple Robotic Arms Simulation Demo
      • 🚧Introduction to xacro
    • Page
    • 🍺Tech Blog
      • Difference between URDF and SDF and how to convert
  • Gazebo
    • Index
    • Terminology
    • GUI
    • World Frame and Axis
    • Cookbook
    • Page 1
  • Programming in Robotics
    • C++
      • CMake
    • Python
    • Rust
  • Mathematics in Robotics
    • Linear Algebra
    • Matrix Properties
    • Probability
      • Expectation-Maximization Algorithm
    • Multivariable Function and Derivatives
  • Physics in Robotics
  • Control of Dynamic Systems
    • Dynamic Response and Transfer Function
    • Block Diagram
    • PID Controller
  • Robot Modeling and Control
    • Rotation and Homogeneous Transformation
  • Probabilistic Robotics
    • Bayes Filter
    • Kalman Filter
    • Particle Filter
    • Discrete Bayes Filter
    • Motion Model
    • Perception Model
    • Localization
    • SLAM
  • Miscellany
  • Concept Index
    • Quaternions
Powered by GitBook
On this page
  1. ROS2
  2. Cookbook

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:

ign service -s <service-name> --reqtype <request-type> --reptyp <response-type> --timeout <timeout> --req <request>

It's recommended to use one-line command. You may run into issues if you break the command into multiple lines with "\"

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

ign service -is /world/demo/create

It produces:

Service providers [Address, Request Message Type, Response Message Type]:
  tcp://10.0.0.22:32865, ignition.msgs.EntityFactory, ignition.msgs.Boolean

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

ign msg -i ignition.msgs.EntityFactory

and it produces:

Name: ignition.msgs.EntityFactory
File: ignition/msgs/entity_factory.proto

message EntityFactory {
  .ignition.msgs.Header header = 1;
  oneof from {
    string sdf = 2;
    string sdf_filename = 3;
    .ignition.msgs.Model model = 4;
    .ignition.msgs.Light light = 5;
    string clone_name = 6;
  }
  .ignition.msgs.Pose pose = 7;
  string name = 8;
  bool allow_renaming = 9;
  string relative_to = 10;
  .ignition.msgs.SphericalCoordinates spherical_coordinates = 11;
}

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.

Name: ignition.msgs.Pose
File: ignition/msgs/pose.proto

message Pose {
  .ignition.msgs.Header header = 1;
  string name = 2;
  uint32 id = 3;
  .ignition.msgs.Vector3d position = 4;
  .ignition.msgs.Quaternion orientation = 5;
}

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)

ign service -s /world/demo/create --reqtype ignition.msgs.EntityFactory --reptype ignition.msgs.Boolean --timeout 5000 --req 'name: "test"; sdf_filename: "diff_drive/model.sdf"; pose: {position: {x: 10, y: 10, z: 0}, orientation: {x: 0, y: 0, z: 0.7071, w: 0.7071}}'

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

ign service -s /world/demo/remove --reqtype ignition.msgs.Entity --reptype ignition.msgs.Boolean --timeout 5000 --req 'type: MODEL; name: "test"'

PreviousHow to properly terminate ROS and GazeboNextHow to spin nodes

Last updated 1 year ago

🍳