'Gazebo how can i change model weigth during runtime?

I want to decrease model weigth in runtime. I am currently working on a plugin but it doesn't seem to work. I am using iris model with pixhawk 4 sitl. My purpose is to simulate a liquid spraying drone like firefighter. To do that I have to decrease the weigth of the drone in time. Thank you. Here is the code that I have:

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

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

        this->model = _parent;
        this->world= this->model->GetWorld();
        this->iris=this->world->ModelByName("iris");
        this->base_link = this->iris->GetLink("base_link");

        base_link->GetInertial()->SetMass(800000);    // Changing the mass


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

public:
    void OnUpdate(const common::UpdateInfo &_info)
    {

    }

private:

    physics::ModelPtr model;
    physics::ModelPtr iris;
    physics::WorldPtr world;
    event::ConnectionPtr updateConnection;
    physics::LinkPtr  base_link;

};

// Register this plugin with the simulator
GZ_REGISTER_MODEL_PLUGIN(joint_c)
}


Solution 1:[1]

I finally figured it out.

I used base_link->GetInertial()->SetMass(new_mass); configuration in OnUpdate() function as kiner_shah told in the comments. But that only changed visual value. In order to apply the change I put base_link -> UpdateMass();

Then I add the plugin path and the plugin to the model sdf file like described in here Gazebo model plugin

Here is my code:

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

    float x = 25;
    int count = 0;


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

        this->model = _parent;
        this->world= this->model->GetWorld();
        this->iris=this->world->ModelByName("iris");
        this->base_link = this->iris->GetLink("base_link");


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

    public: void OnUpdate(const common::UpdateInfo &_info)
        {
          count = count + 1;
          if(count > 10000){
            x = 22;
          }
          if(count > 13000){
            x = 19;
          }
          if(count > 16000){
            x = 17;
          }
          if(count > 18000){
            x = 15;
          }
          base_link -> GetInertial() -> SetMass(x);
          base_link -> UpdateMass();
        }

      private: 
          
          physics::ModelPtr model;
          physics::ModelPtr iris;
          physics::WorldPtr world;
          event::ConnectionPtr updateConnection;   
          physics::LinkPtr  base_link;

      };

      // Register this plugin with the simulator
      GZ_REGISTER_MODEL_PLUGIN(joint_c)
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s