ROS2与Gazebo11入门教程-仿真世界插件
说明:
- 介绍仿真世界插件
前提条件
已经完成了前面两个教程的学习:
模型插件
插件101
代码
可以在gazebo/examples/plugins/factory资源中找到插件源代码。
在一个运行的仿真中对插入哪些模型以及何时插入这些模型进行控制会很有用。本节教程将展示如何将预定义和自定义的模型插入到Gazebo中。
这里继续使用前面插件教程中的gazebo_plugin_tutorial目录:
$ cd ~/gazebo_plugin_tutorial
- 创建一个新的源代码文件:
$ gedit http://factory.cc
- 将以下代码复制到http://factory.cc文件中:
#include <ignition/math/Pose3.hh>
#include "gazebo/physics/physics.hh"
#include "gazebo/common/common.hh"
#include "gazebo/gazebo.hh"
namespace gazebo
{
class Factory : public WorldPlugin
{
public: void Load(physics::WorldPtr _parent, sdf::ElementPtr /*_sdf*/)
{
// Option 1: Insert model from file via function call.
// The filename must be in the GAZEBO_MODEL_PATH environment variable.
_parent->InsertModelFile("model://box");
// Option 2: Insert model from string via function call.
// Insert a sphere model from string
sdf::SDF sphereSDF;
sphereSDF.SetFromString(
"<sdf version ='1.4'>\
<model name ='sphere'>\
<pose>1 0 0 0 0 0</pose>\
<link name ='link'>\
<pose>0 0 .5 0 0 0</pose>\
<collision name ='collision'>\
<geometry>\
<sphere><radius>0.5</radius></sphere>\
</geometry>\
</collision>\
<visual name ='visual'>\
<geometry>\
<sphere><radius>0.5</radius></sphere>\
</geometry>\
</visual>\
</link>\
</model>\
</sdf>");
// Demonstrate using a custom model name.
sdf::ElementPtr model = sphereSDF.Root()->GetElement("model");
model->GetAttribute("name")->SetFromString("unique_sphere");
_parent->InsertModelSDF(sphereSDF);
// Option 3: Insert model from file via message passing.
{
// Create a new transport node
transport::NodePtr node(new transport::Node());
// Initialize the node with the world name
node->Init(_parent->Name());
// Create a publisher on the ~/factory topic
transport::PublisherPtr factoryPub =
node->Advertise<msgs::Factory>("~/factory");
// Create the message
msgs::Factory msg;
// Model file to load
msg.set_sdf_filename("model://cylinder");
// Pose to initialize the model to
msgs::Set(msg.mutable_pose(),
ignition::math::Pose3d(
ignition::math::Vector3d(1, -2, 0),
ignition::math::Quaterniond(0, 0, 0)));
// Send the message
factoryPub->Publish(msg);
}
}
};
// Register this plugin with the simulator
GZ_REGISTER_WORLD_PLUGIN(Factory)
}
代码说明
- 代码的第一个部分用于创建一个仿真世界插件。
#include <ignition/math/Pose3.hh>
#include "gazebo/physics/physics.hh"
#include "gazebo/common/common.hh"
#include "gazebo/gazebo.hh"
namespace gazebo
{
class Factory : public WorldPlugin
{
public: void Load(physics::WorldPtr _parent, sdf::ElementPtr /*_sdf*/)
在Load函数中,有三种不同的模型插入方法。
第一种方法使用World方法基于GAZEBO_MODEL_PATH环境变量定义的资源路径中的文件来加载模型。
// Option 1: Insert model from file via function call.
// The filename must be in the GAZEBO_MODEL_PATH environment variable.
_parent->InsertModelFile("model://box");
- 第二种方法使用World方法基于字符串数据来加载模型。
// Option 2: Insert model from string via function call.
// Insert a sphere model from string
sdf::SDF sphereSDF;
sphereSDF.SetFromString(
"<sdf version ='1.4'>\
<model name ='sphere'>\
<pose>1 0 0 0 0 0</pose>\
<link name ='link'>\
<pose>0 0 .5 0 0 0</pose>\
<collision name ='collision'>\
<geometry>\
<sphere><radius>0.5</radius></sphere>\
</geometry>\
</collision>\
<visual name ='visual'>\
<geometry>\
<sphere><radius>0.5</radius></sphere>\
</geometry>\
</visual>\
</link>\
</model>\
</sdf>");
// Demonstrate using a custom model name.
sdf::ElementPtr model = sphereSDF.Root()->GetElement("model");
model->GetAttribute("name")->SetFromString("unique_sphere");
_parent->InsertModelSDF(sphereSDF);
- 第三种方法使用消息传递机制来插入模型。对于通过网络连接与Gazebo进行通信的独立应用程序而言,这个方法最有用。
// Option 3: Insert model from file via message passing.
{
// Create a new transport node
transport::NodePtr node(new transport::Node());
// Initialize the node with the world name
node->Init(_parent->Name());
// Create a publisher on the ~/factory topic
transport::PublisherPtr factoryPub =
node->Advertise<msgs::Factory>("~/factory");
// Create the message
msgs::Factory msg;
// Model file to load
msg.set_sdf_filename("model://cylinder");
// Pose to initialize the model to
msgs::Set(msg.mutable_pose(),
ignition::math::Pose3d(
ignition::math::Vector3d(1, -2, 0),
ignition::math::Quaterniond(0, 0, 0)));
// Send the message
factoryPub->Publish(msg);
编译
- 假设读者已经完成了“插件101”教程,那么除了将上述代码另存为〜/gazebo_plugin_tutorial/factory.cc文件之外,还需要将下面两行代码添加到〜/gazebo_plugin_tutorial/CMakeLists.txt文件中:
add_library(factory SHARED http://factory.cc)
target_link_libraries(factory
${GAZEBO_LIBRARIES}
)
- 编译此代码将会生成一个共享库〜/gazebo_plugin_tutorial/build/ libfactory.so,可以将其插入到Gazebo仿真中。
$ mkdir ~/gazebo_plugin_tutorial/build
$ cd ~/gazebo_plugin_tutorial/build
$ cmake ../
$ make
制作形状
- 新建一个用于存放模型的目录,并在该目录中分别为方盒模型和圆柱体模型创建一个子目录,命令如下:
$ mkdir ~/gazebo_plugin_tutorial/models
$ cd ~/gazebo_plugin_tutorial/models
$ mkdir box cylinder
- 创建一个方盒模型,命令为:
$ cd box
$ gedit model.sdf
- 将以下代码复制并粘贴到方盒模型的model.sdf文件中:
<?xml version='1.0'?>
<sdf version ='1.6'>
<model name ='box'>
<pose>1 2 0 0 0 0</pose>
<link name ='link'>
<pose>0 0 .5 0 0 0</pose>
<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>
</model>
</sdf>
- 然后创建一个model.config文件:
$ gedit model.config
- 将下列代码复制到model.config文件中:
<?xml version='1.0'?>
<model>
<name>box</name>
<version>1.0</version>
<sdf >model.sdf</sdf>
<author>
<name>me</name>
<email>somebody@somewhere.com</email>
</author>
<description>
A simple Box.
</description>
</model>
- 进入到圆柱体模型目录中并创建一个新的model.sdf文件:
$ cd ~/gazebo_plugin_tutorial/models/cylinder
$ gedit model.sdf
- 将以下代码复制到model.sdf文件中:
<?xml version='1.0'?>
<sdf version ='1.6'>
<model name ='cylinder'>
<pose>1 2 0 0 0 0</pose>
<link name ='link'>
<pose>0 0 .5 0 0 0</pose>
<collision name ='collision'>
<geometry>
<cylinder><radius>0.5</radius><length>1</length></cylinder>
</geometry>
</collision>
<visual name='visual'>
<geometry>
<cylinder><radius>0.5</radius><length>1</length></cylinder>
</geometry>
</visual>
</link>
</model>
</sdf>
- 创建一个model.config文件:
$ gedit model.config
- 将以下代码复制到该model.config文件中:
<?xml version='1.0'?>
<model>
<name>cylinder</name>
<version>1.0</version>
<sdf>model.sdf</sdf>
<author>
<name>me</name>
<email>somebody@somewhere.com</email>
</author>
<description>
A simple cylinder.
</description>
</model>
运行代码
- 运行以下命令,以确保GAZEBO_MODEL_PATH环境变量引用了您的新模型目录:
$ export GAZEBO_MODEL_PATH=$HOME/gazebo_plugin_tutorial/models:$GAZEBO_MODEL_PATH
- 将您的库路径添加到GAZEBO_PLUGIN_PATH环境变量:
$ export GAZEBO_PLUGIN_PATH=$HOME/gazebo_plugin_tutorial/build:$GAZEBO_PLUGIN_PATH
- 创建一个名为~/gazebo_plugin_tutorial/factory.world的仿真世界SDF文件:
$ cd ~/gazebo_plugin_tutorial
$ gedit factory.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 name="factory" filename="libfactory.so"/>
</world>
</sdf>
- 用该factory.world世界文件运行Gazebo:
$ gazebo ~/gazebo_plugin_tutorial/factory.world
- 这样在Gazebo窗口中就应该会显示一个具有球体、方盒和圆柱体排成一排的仿真环境。
参考:
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号