Resetting the simulation after writing custom Model Plugin/ How to reset model plugin
I have written a model plugin that increases the mass of an object(“cylobject”) during runtime. But after I run the command “rosservice call /gazebo/resetsimulation”, 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)
}
Asked by Hirak Basumatary on 2022-09-27 09:18:38 UTC
Answers
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.
Asked by Veerachart on 2022-09-29 02:46:53 UTC
Comments