Programmatic World Control - modulabs/gazebo-tutorial GitHub Wiki
์ด ํ๋ฌ๊ทธ์ธ ์์ ๋ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ์ค๋ ฅ์ ์์ ํ๋ค.
Source: gazebo/examples/plugins/world_edit
์ด์ ํ๋ฌ๊ทธ์ธ ํ์ฝ๋ฆฌ์ผ์์ gazebo_plugin_tutorial์ ์ฌ์ฉํ๋ค
$ mkdir ~/gazebo_plugin_tutorial; cd ~/gazebo_plugin_tutorial
ํ์ผ ~/gazebo_plugin_tutorial/world_edit.world์ ์์ฑํ๋ค
$ gedit world_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"
/// \example examples/plugins/world_edit.cc
/// This example creates a WorldPlugin, initializes the Transport system by
/// creating a new Node, and publishes messages to alter gravity.
namespace gazebo
{
class WorldEdit : public WorldPlugin
{
public: void Load(physics::WorldPtr _parent, sdf::ElementPtr _sdf)
{
// 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 ~/physics topic
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(new transport::Node());
// Initialize the node with the world name
node->Init(_parent->GetName());์๋ก์ด ๋ ธ๋ํฌ์ธํฐ๋ฅผ ์์ฑํ๊ณ world name์ ์ฌ์ฉํ์ฌ ์ด๊ธฐํํ๋ค. world name์ ํน์ world์ ํต์ ํ ์ ์๊ฒ ํ๋ค.
// Create a publisher on the ~/physics topic
transport::PublisherPtr physicsPub =
node->Advertise<msgs::Physics>("~/physics");"~/physics" topic์ ๋ฉ์์ง๋ฅผ ๋ณด๋ด๊ธฐ ์ํด publisher๋ฅผ ์์ฑํ๋ค.
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 message๊ฐ ์์ฑ๋๊ณ step time๊ณผ ์ค๋ ฅ์ด ๋ณ๊ฒฝ๋๋ค. ์ด ๋ฉ์์ง๋ "~/physics" topic๋ก ๊ฒ์๋๋ค(published).
Plugin Overview Tutorial์ ํ ์ฌ์ฉ์๋, ์์ ์ฝ๋๋ฅผ ~/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๊ฐ ์์ฑ๋๋ค
$ 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
$ gazebo world_edit.world
๊ทธ๋ฌ๋ฉด ๋น์ด์๋ world๋ฅผ ๋ณผ ์ ์๋ค.
์ด์ ๋ ๋๋ง ์ฐฝ ์์ ์๋ ์์ ์์ด์ฝ์ ์ฌ์ฉํ์ฌ world์ ๋ฐ์ค๋ฅผ ์ถ๊ฐํ๋ค. ์์๊ฐ ์นด๋ฉ๋ผ๋ก๋ถํฐ ๋ฉ์ด์ ธ ์ฌ๋ผ๊ฐ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.