Programmatic World Control - modulabs/gazebo-tutorial GitHub Wiki

์ด ํ”Œ๋Ÿฌ๊ทธ์ธ ์˜ˆ์ œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ ์ค‘๋ ฅ์„ ์ˆ˜์ •ํ•œ๋‹ค.

Prerequisites:

Setup:

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>

Code

~/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)
}

The Code Explained

      // 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).

Build

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

Run Tutorial

๋จผ์ € GAZEBO_PLUGIN_PATH ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ํด๋”๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค.

export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:~/gazebo_plugin_tutorial/build/

๊ทธ๋ฆฌ๊ณ  ํ„ฐ๋ฏธ๋„ ์ฐฝ์— ์•„๋ž˜ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•œ๋‹ค.

$ cd ~/gazebo_plugin_tutorial
$ gazebo world_edit.world

๊ทธ๋Ÿฌ๋ฉด ๋น„์–ด์žˆ๋Š” world๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ด์ œ ๋ Œ๋”๋ง ์ฐฝ ์œ„์— ์žˆ๋Š” ์ƒ์ž ์•„์ด์ฝ˜์„ ์‚ฌ์šฉํ•˜์—ฌ world์— ๋ฐ•์Šค๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์ƒ์ž๊ฐ€ ์นด๋ฉ”๋ผ๋กœ๋ถ€ํ„ฐ ๋ฉ€์–ด์ ธ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ