Gazebo如何在运行时更改模型重量



我想在运行时降低模型重量。我目前正在开发一个插件,但它似乎不起作用。我使用的是带有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)
}

我终于想通了。

正如kiner_shah在评论中所说,我在OnUpdate()函数中使用了base_link->GetInertial()->SetMass(new_mass);配置。但这只会改变视觉价值。为了应用更改,我放置了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)
}

最新更新