Skip to content

Programmatic World Control

Hancheol Choi edited this page Feb 10, 2018 · 4 revisions

이 플러그인 예제는 프로그래밍 방식으로 중력을 수정한다.

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에 박스를 추가한다. 상자가 카메라로부터 멀어져 올라가는 것을 볼 수 있다.

Table of Contents




Clone this wiki locally