World plugins - modulabs/gazebo-tutorial GitHub Wiki
Source code: gazebo/examples/plugins/factory
์คํ์ค์ธ ์๋ฎฌ๋ ์ด์ ์์ ์ด๋ค ๋ชจ๋ธ์ด ์กด์ฌํ๋์ง ๊ทธ๋ฆฌ๊ณ ์ธ์ ์ฝ์ ๋์ด์ผ ํ๋์ง๋ฅผ ์ ์ดํ๋ ๊ฒ์ ์ ์ฉํ ์ ์๋ค. ์ด๋ฒ ํํ ๋ฆฌ์ผ์์๋ ์ฌ์ ์ ์๋ ๋ชจ๋ธ๊ณผ ์ฌ์ฉ์ ์ ์ ๋ชจ๋ธ์ ๊ฐ์ ๋ณด์ ์ฝ์ ํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ๋ค.
์ด์ ์ ์ค๋ช
ํ gazebo_plugin_tutorial์ ์ฌ์ฉํ๋ค
$ mkdir ~/gazebo_plugin_tutorial
$ cd ~/gazebo_plugin_tutorial
์๋ก์ด ํ์ผ์ ์์ฑํ๋ค:
$ gedit factory.cc
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->GetName());
// 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)
}์ฝ๋์ ์ฒซ๋ถ๋ถ์ world ๋ฌ๊ทธ์ธ์ ์์ฑํ๋ค.
#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 method๋ฅผ ์ฌ์ฉํ์ฌ 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 method๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด ๋ฐ์ดํฐ(string data)๋ก ์ด๋ฃจ์ด์ง ๋ชจ๋ธ ํ์ผ์ ๋ก๋ํ๋ ๊ฒ์ด๋ค.
// 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->GetName());
// 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);Plugin Overview Tutorial ๊ณผ์ ์ ๊ฑฐ์ณค๋ค๋ฉด, ์ ์ฝ๋๋ฅผ ~/gazebo_plugin_tutorial/factory.cc๋ก ์ ์ฅํ๊ณ ~/gazebo_plugin_tutorial/CMakeLists.txt์ ๋ค์ํ์ ์ถ๊ฐํ๋ฉด ๋๋ค.
add_library(factory SHARED factory.cc)
target_link_libraries(factory ${GAZEBO_LIBRARIES})์ด ์ฝ๋๋ฅผ ์ปดํ์ผ ํ๋ฉด, ๊ฐ์ ๋ณด ์๋ฎฌ๋ ์ด์
์ ์ฝ์
ํ ์ ์๋ ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ~/gazebo_plugin_tutorial/build/libfactory.so๊ฐ ์์ฑ๋๋ค
$ 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
box/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>[email protected]</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>[email protected]</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
world SDF ํ์ผ ~/gazebo_plugin_tutorial/factory.world์ ์์ฑํ๋ค
$ cd ~/gazebo_plugin_tutorial
$ gedit factory.world
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>๊ฐ์ ๋ณด ์คํ
$ gazebo ~/gazebo_plugin_tutorial/factory.world
๊ฐ์ ๋ณด ์ฐฝ์๋ ๊ตฌ, ์์, ์ค๋ฆฐ๋๊ฐ ์ฐ์์ผ๋ก ๋ฐฐ์น๋ ํ๊ฒฝ์ด ๋ณด์ฌ์ผ ํ๋ค.