https://classic.gazebosim.org/tutorials?tut=plugins_model&cat=write_plugin
访问模型的物理属性及其元素(links, joints, collision objects…)
$ cd ~/gazebo_plugin_tutorial
$ gedit 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
{
// Pointer to the model
private: physics::ModelPtr model;
// Pointer to the update event connection
private: event::ConnectionPtr updateConnection;
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::ConnectW$ cd ~/gazebo_plugin_tutorial/
$ gzserver -u model_push.worldorldUpdateBegin(
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));
}
};
// Register thie plugin with the simulator
GZ_REGISTER_MODEL_PLUGIN(ModelPush)
}
在CMakeLists.txt中增加以下行,并编译:
add_library(model_push SHARED model_push.cc)
target_link_libraries(model_push ${GAZEBO_LIBRARIES})
在一个model的sdf文件中指定使用此plugin:
<model name="box">
<!-- ... -->
<plugin name="model_push" filename="gazebo_plugin_tutorial/build/libmodel_push.so"/>
</model>
运行server和client,播放可以看到模型移动:
$ cd ~/gazebo_plugin_tutorial/
# -u 以暂停状态启动server
$ gzserver -u model_push.world
# 另一个终端
gzclient
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 cdd@ahucd.cn