Model plugins - modulabs/gazebo-tutorial GitHub Wiki

Prerequisites

Overview / HelloWorld Plugin Tutorial

Note: ๋งŒ์•ฝ์— ์ด์ „ ํŠœํ† ๋ฆฌ์–ผ์—์„œ ๊ณ„์† ํ•˜๊ณ ์žˆ๋‹ค๋ฉด, ์•„๋ž˜ ๋‚˜์˜ค๋Š” ํŠœํ† ๋ฆฌ์–ผ์— ์ ์ ˆํ•œ #include๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.

Code

Source: gazebo/examples/plugins/model_push

ํ”Œ๋Ÿฌ๊ทธ์ธ์€ ๋ชจ๋ธ์˜ ๋ฌผ๋ฆฌ์  ์†์„ฑ๊ณผ ๊ธฐ๋ณธ ์š”์†Œ (๋งํฌ, ์กฐ์ธํŠธ, ์ถฉ๋Œ ๊ฐ์ฒด)์— ๋Œ€ํ•œ ์™„๋ฒฝํ•œ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•œ๋‹ค. ๋‹ค์Œ ํ”Œ๋Ÿฌ๊ทธ์ธ์€ ์ƒ์œ„ ๋ชจ๋ธ์— ์„ ์†๋„๋ฅผ ์ ์šฉํ•œ๋‹ค.

$ cd ~/gazebo_plugin_tutorial
$ gedit model_push.cc

Plugin Code:

#include <functional>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <ignition/math/Vector3.hh>

namespace gazebo
{
  class ModelPush : public ModelPlugin
  {
    public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
    {
      // Store the pointer to the model
      this->model = _parent;

      // Listen to the update event. This event is broadcast every
      // simulation iteration.
      this->updateConnection = event::Events::ConnectWorldUpdateBegin(
          std::bind(&ModelPush::OnUpdate, this));
    }

    // Called by the world update start event
    public: void OnUpdate()
    {
      // Apply a small linear velocity to the model.
      this->model->SetLinearVel(ignition::math::Vector3d(.3, 0, 0));
    }

    // Pointer to the model
    private: physics::ModelPtr model;

    // Pointer to the update event connection
    private: event::ConnectionPtr updateConnection;
  };

  // Register this plugin with the simulator
  GZ_REGISTER_MODEL_PLUGIN(ModelPush)
}

Compiling the Plugin

Hello WorldPlugin tutorial์„ ํ•ด๋ณด์•˜๋‹ค๋ฉด, ~/gazebo_plugin_tutorial/CMakeLists.txtํŒŒ์ผ์— ์•„๋ž˜ ํ–‰์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ

add_library(model_push SHARED model_push.cc)
target_link_libraries(model_push ${GAZEBO_LIBRARIES})

์ด ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜๋ฉด ๊ฐ€์ œ๋ณด ์‹œ๋ฎฌ๋ ˆ์ด์…˜์— ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ~/gazebo_plugin_tutorial/build/libmodel_push.so ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

$ cd ~/gazebo_plugin_tutorial/build
$ cmake ../
$ make

Running the Plugin

์ด ํ”Œ๋Ÿฌ๊ทธ์ธ์€ world ํŒŒ์ผ examples/plugins/model_push/model_push.world ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

$ cd ~/gazebo_plugin_tutorial
$ gedit model_push.world
<?xml version="1.0"?> 
<sdf version="1.4">
  <world name="default">

    <!-- Ground Plane -->
    <include>
      <uri>model://ground_plane</uri>
    </include>

    <include>
      <uri>model://sun</uri>
    </include>

    <model name="box">
      <pose>0 0 0.5 0 0 0</pose>
      <link name="link">
        <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>

      <plugin name="model_push" filename="libmodel_push.so"/>
    </model>        
  </world>
</sdf>

ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋ชจ๋ธ์— ๋ถ€์ฐฉํ•˜๋Š” ํ›„ํฌ(hook)๋Š” ๋ชจ๋ธ ์š”์†Œ์˜ ๋งˆ์ง€๋ง‰์— ์ง€์ •๋œ๋‹ค:

์•„๋ž˜ ๋‚ด์šฉ์€ ์ด๋ฒˆ ์˜ˆ์ œ์— ํ•ด๋‹น๋˜๋Š” ๋‚ด์šฉ์ด๋ฉฐ, ํ•„์š”์—๋”ฐ๋ผ ์ˆ˜์ •ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•จ

<plugin name="model_push" filename="libmodel_push.so"/>

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ GAZEBO_PLUGIN_PATH์— ์ถ”๊ฐ€ํ•œ๋‹ค:

$ export GAZEBO_PLUGIN_PATH=$HOME/gazebo_plugin_tutorial/build:$GAZEBO_PLUGIN_PATH

์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์‹œ์ž‘ํ•˜๊ณ , ์‹คํ–‰ํ•œ๋‹ค

$ cd ~/gazebo_plugin_tutorial/
$ gzserver -u model_push.world

-u ์˜ต์…˜์€ ์ผ์‹œ ์ •์ง€๋œ ์ƒํƒœ(paused state)๋กœ ์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.

๋ณ„๋„์˜ ํ„ฐ๋ฏธ๋„์—์„œ gui๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค

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