1
$\begingroup$

My Gazebo version is Garden. I want to use ROS2 command to delete the model in Gazebo simulation.

What command should I enter?

$\endgroup$

2 Answers 2

2
$\begingroup$

I believe the previous answer is correct for gazebo classic. However it is different for gazebo garden.

As of right now there is no ROS interface that directly exposes the delete entity service. There is a draft pull request to add those features.

You could also develop a system plugin and call the RequestRemoveEntity() method for the entity component manager. It is possible to embed a ros2 node inside of a gz system plugin. So you could create a ros service that could be called to remove an entity inside of the system plugin.

Here is an example showing how to embed a ros2 node inside of a gazebo system plugin.

$\endgroup$
1
  • $\begingroup$ +1 - You are most likely correct... I must admit that I didn't look for Garden specific examples. I should have made that clear in my answer. This should probably be the accepted answer. $\endgroup$ Commented Apr 8 at 22:32
1
$\begingroup$

There are a number of ways, depending on how you want to do it.

Command line

You could use the command line

rosservice call gazebo/delete_model '{model_name: <model_name>}'

In Python

Or something like this in Python:

from gazebo_msgs.srv import DeleteModel # For deleting models from the environment

def del_model( modelName ): # FIXME: Freezes Python, DO NOT USE!
    """ Remove the model with 'modelName' from the Gazebo scene """
    # delete_model : gazebo_msgs/DeleteModel
    del_model_prox = rospy.ServiceProxy('gazebo/delete_model', DeleteModel) # Handle to model spawner
    # rospy.wait_for_service('gazebo/delete_model') # Wait for the model loader to be ready 
    # FREEZES EITHER WAY
    del_model_prox(str(modelName)) # Remove from Gazebo

Note: You need to pass del_model_prox a string, else it will crash. So it is safer to use

del_model_prox(str(modelName))

rather than just

del_model_prox(str(modelName))

References

In C++

From this answer to Problem with Deleting Models

 #include "gazebo/DeleteModel.h"
    remove(std::string model_name)
         {
           ros::NodeHandle nh("");
           ros::service::waitForService("gazebo/delete_model");
           ros::ServiceClient deleteModelClient = nh.serviceClient<gazebo::DeleteModel>("gazebo/delete_model");
           gazebo::DeleteModel deleteModel;
           deleteModel.request.model_name = model_name;
           deleteModelClient.call(deleteModel);
         }

and call it (from main() or where ever), with

remove(<model_name>);

See also gazebo_msgs/DeleteModel Service

File: gazebo_msgs/DeleteModel.srv

Raw Message Definition

string model_name                 # name of the Gazebo Model to be deleted
---
bool success                      # return true if deletion is successful
string status_message             # comments if available
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.