Gazebo | Ignition | Community
Ask Your Question
0

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

asked 2022-09-27 09:18:38 -0600

Hirak Basumatary 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

1 Answer

Sort by » oldest newest most voted
0

answered 2022-09-29 02:46:53 -0600

Veerachart gravatar image

Plugin class has a function Reset() which will be called when the model is reset (which is the same as when the world is reset). You need to implement the function to put your related variables (x and count?) back to the initial state, and probably should set the mass back as well.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-09-27 09:18:38 -0600

Seen: 227 times

Last updated: Sep 29 '22