这个插件示例通过编程方式修改了重力。
预备知识
- 模型操作
- 插件教程
设置
来源:gazebo/examples/plugins/world_edit
https://bitbucket.org/osrf/gazebo/src/gazebo7/examples/plugins/world_edit
使用以前插件教程中的gazebo_plugin_tutorial
$ mkdir~/gazebo_plugin_tutorial; cd ~/gazebo_plugin_tutorial
创建一个文件名为~/gazebo_plugin_tutorial/world_edit.world
$ geditworld_edit.world
将以下内容添加到其中:
<?xml version ='1.0'?> <sdf version ='1.4'> <world name='default'> <include> <uri>model://ground_plane</uri> </include> <include> <uri>model://sun</uri> </include> <plugin filename="libworld_edit.so" name="world_edit"/> </world> </sdf>
代码
创建一个文件名为~/gazebo_plugin_tutorial/world_edit.cc
$ gedit world_edit.cc
添加以下内容:
#include<sdf/sdf.hh> #include<ignition/math/Pose3.hh> #include"gazebo/gazebo.hh" #include"gazebo/common/Plugin.hh" #include"gazebo/msgs/msgs.hh" #include"gazebo/physics/physics.hh" #include"gazebo/transport/transport.hh" /// \exampleexamples/plugins/world_edit.cc /// Thisexample creates a WorldPlugin, initializes the Transport system by /// creating anew Node, and publishes messages to alter gravity. namespacegazebo { class WorldEdit : public WorldPlugin { public: void Load(physics::WorldPtr_parent, sdf::ElementPtr _sdf) { // Create a new transport node transport::NodePtr node(newtransport::Node()); // Initialize the node with the worldname node->Init(_parent->GetName()); // Create a publisher on the ~/physicstopic transport::PublisherPtr physicsPub = node->Advertise<msgs::Physics>("~/physics"); msgs::Physics physicsMsg; physicsMsg.set_type(msgs::Physics::ODE); // Set the step time physicsMsg.set_max_step_size(0.01); // Change gravity msgs::Set(physicsMsg.mutable_gravity(), ignition::math::Vector3d(0.01, 0,0.1)); physicsPub->Publish(physicsMsg); } }; // Register this plugin with the simulator GZ_REGISTER_WORLD_PLUGIN(WorldEdit) }
代码解释
// Create a new transport node transport::NodePtr node(newtransport::Node()); // Initialize the node with theworld name node->Init(_parent->GetName());
我们创建一个新的节点指针,并将它初始化为使用世界名。世界名称允许节点与一个特定的世界通信。
// Create a publisher on the~/physics topic transport::PublisherPtr physicsPub= node->Advertise<msgs::Physics>("~/physics");
创建发布者是为了在“~/physics”主题上发送物理消息。
msgs::Physics physicsMsg; physicsMsg.set_type(msgs::Physics::ODE); // Set the step time physicsMsg.set_max_step_size(0.01); // Change gravity msgs::Set(physicsMsg.mutable_gravity(), ignition::math::Vector3d(0.01, 0, 0.1)); physicsPub->Publish(physicsMsg);
物理信息被创建,步骤时间和重力被改变。这条信息随后被发布到“~/physics”主题。
创建
假设读者已经完成了插件概述教程,那么除了以上代码之外,还需要将以上代码保存为~/gazebo_plugin_tutorial/world_edit.cc。将以下代码添加到~/gazebo_plugin_tutorial/CMakeLists.txt
add_library(world_edit SHARED world_edit.cc)
target_link_libraries(world_edit ${GAZEBO_LIBRARIES})
编译这段代码将导致一个共享库,~/gazebo_plugin_tutorial/build/libworld_edit.so。因此,这可以插入到Gazebo仿真中。
$ mkdir~/gazebo_plugin_tutorial/build
$ cd~/gazebo_plugin_tutorial/build
$ cmake ../
$ make
运行教程
首先,您需要将该文件夹添加到GAZEBO_PLUGIN_PATH环境变量:
export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:~/gazebo_plugin_tutorial/build/
然后在一个终端
$ cd~/gazebo_plugin_tutorial
$ gazeboworld_edit.world
你应该看到一个空的世界。
现在,使用位于渲染窗口上方的box图标向世界添加一个方框。这个箱子应该从照相机里浮起来。