0

我想在运行时减少模型重量。我目前正在开发一个插件,但它似乎不起作用。我正在使用带有 pixhawk 4 sitl 的虹膜模型。我的目的是模拟像消防员一样的液体喷洒无人机。为此,我必须及时减轻无人机的重量。谢谢你。这是我拥有的代码:

#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)
}
4

1 回答 1

0

我终于想通了。我使用 base_link->GetInertial()->SetMass(new_mass); OnUpdate() 函数中的配置如kiner_shah 在评论中所说。但这只会改变视觉价值。为了应用更改,我放置了 base_link -> UpdateMass(); 然后我将插件路径和插件添加到模型 sdf 文件中,如Gazebo 模型插件中所述

这是我的代码:

        #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)
    }
于 2022-02-11T20:18:24.337 回答