My Gazebo version is Garden. I want to use ROS2 command to delete the model in Gazebo simulation.
What command should I enter?
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.
There are a number of ways, depending on how you want to do it.
You could use the command line
rosservice call gazebo/delete_model '{model_name: <model_name>}'
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))
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