Resetting the simulation after writing custom Model Plugin/ How to reset model plugin

asked 2022-09-27 09:24:55 -0500

Hirak231992 gravatar image

I have written a model plugin that increases the mass of an object(“cyl_object”) during runtime. But after I run the command “rosservice call /gazebo/reset_simulation”, the mass of the object does not return to its initial mass. Can you please point out where my mistake is? I would appreciate any help. I have attached my code of the model plugin

#include <functional>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <unistd.h>


    float x = 0.5;
    int count = 0;


    namespace gazebo
    {
      class Weight_Change : public ModelPlugin
      {
      public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
        {   

        this->model = _parent;
        this->world= this->model->GetWorld();
        this->cyl=this->world->ModelByName("cyl");
        this->cyl_object = this->cyl->GetLink("cyl_object");


    this->updateConnection = event::Events::ConnectWorldUpdateBegin(boost::bind(&Weight_Change::OnUpdate, this, _1));
    }

    public: void OnUpdate(const common::UpdateInfo &_info)
        {
          count = count + 1;
          if(count > 15000){
            x = 1;
          }
          if(count > 16500){
            x = 3;
          }
          if(count > 17500){
            x = 5;
          }
          if(count > 18500){
            x = 10;
          }
          cyl_object -> GetInertial() -> SetMass(x);
          cyl_object -> UpdateMass();

        }

      private: 

          physics::ModelPtr model;
          physics::ModelPtr cyl;
          physics::WorldPtr world;
          event::ConnectionPtr updateConnection;   
          physics::LinkPtr  cyl_object;

      };

      // Register this plugin with the simulator
    GZ_REGISTER_MODEL_PLUGIN(Weight_Change)
  }
edit retag flag offensive close merge delete