Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Delete a model from within its own modelPlugin

I wrote a ModelPlugin to delete the model to which it is attached after a certain period of time. The problem is that when the model is deleted, gazebo stops working, it just freezes.

Plugin code:

#include <ignition/math/Pose3.hh>
#include <gazebo/common/Plugin.hh>
#include <gazebo/physics/Model.hh>
#include "gazebo_msgs/DeleteModel.h"
#include "ros/ros.h"
#include <thread>
#include <chrono>

namespace gazebo{
  class VictimPlugin : public ModelPlugin{
    physics::ModelPtr model;

    int drowning_time_ = 0;
    std::thread timer_thread_;

    void timerThreadFunction(){

      gazebo_msgs::DeleteModel del_model;
      del_model.request.model_name = this->model->GetName();
      ros::service::call("/gazebo/delete_model", del_model);


    VictimPlugin() : ModelPlugin(){}

    void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf){
      model = _model;

      if (!_sdf->HasElement("drowningTime")){
        this->drowning_time_ = 5000;
        ROS_DEBUG("Victim plugin missing <drowningTime>, default to 5000ms");
        this->drowning_time_ = _sdf->Get<int>("drowningTime");

      this->timer_thread_ =
        std::thread(std::bind(&VictimPlugin::timerThreadFunction, this));

I am adding the model to the world file like this:

  <pose>7 17.5 0.1 -1.57 0 0</pose>
  <plugin name="victim" filename="">

I am using gazebo version 9.13.1