官网教程:http://gazebosim.org/tutorials?tut=plugins_model&cat=write_plugin
系列二主要关注点在于modelPlugin
在模型中插入plugin是能够控制模型的运行,模型的性质的体现。给一些元素在特性,比如速度等。
官方的例程:
model_push.cc文件
#include <functional>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <ignition/math/Vector3.hh>
namespace gazebo
{
class ModelPush : public ModelPlugin
{
public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
{
// Store the pointer to the model
this->model = _parent;
// Listen to the update event. This event is broadcast every
// simulation iteration.
this->updateConnection = event::Events::ConnectWorldUpdateBegin(
std::bind(&ModelPush::OnUpdate, this));
}
// Called by the world update start event
public: void OnUpdate()
{
// Apply a small linear velocity to the model.
this->model->SetLinearVel(ignition::math::Vector3d(.3, 0, 0));
}
// Pointer to the model
private: physics::ModelPtr model;
// Pointer to the update event connection
private: event::ConnectionPtr updateConnection;
};
// Register this plugin with the simulator
GZ_REGISTER_MODEL_PLUGIN(ModelPush)
}
上面的代码核心在
this->updateConnection = event::Events::ConnectWorldUpdateBegin( std::bind(&ModelPush::OnUpdate, this));
public: void OnUpdate()
{
// Apply a small linear velocity to the model.
this->model->SetLinearVel(ignition::math::Vector3d(.3, 0, 0));
}
将OnUpdate()函数关联起来,一旦模型更新,就会调用这个函数。
CMake文件编译共享库核心在下面两句话,分别是生成共享文件和链接关联库。
add_library(model_push SHARED model_push.cc)
target_link_libraries(model_push ${GAZEBO_LIBRARIES})
plugin的使用:
<?xml version="1.0"?>
<sdf version="1.4">
<world name="default">
<!-- Ground Plane -->
<include>
<uri>model://ground_plane</uri>
</include>
<include>
<uri>model://sun</uri>
</include>
<model name="box">
<pose>0 0 0.5 0 0 0</pose>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</visual>
</link>
<plugin name="model_push" filename="libmodel_push.so"/>
</model>
</world>
</sdf>
上面额代码关键在于在这个标签中插入。运行:
gzserver -u model_push.world
gzclient
总结:通过modernplugin,我们能够根据自己的需求修改模型中元素的性质、运动,使仿真更接近实际情况。
上面的重点涉及:
(1)事件与回调的运用
(2)plugin的插入位置
(3)C++ 文件的编译
评论(0)
您还未登录,请登录后发表或查看评论