System Plugin - modulabs/gazebo-tutorial GitHub Wiki

Prerequisites

Overview / HelloWorld Plugin Tutorial

Overview

Source: gazebo/examples/plugins/system_gui_plugin

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” /tmp/gazebo_frames ๋””๋ ‰ํ† ๋ฆฌ์— ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•˜๋„๋ก ์„ค๊ณ„๋œ gzclient์˜ ์‹œ์Šคํ…œ ํ”Œ๋Ÿฌ๊ทธ์ธ ์†Œ์ŠคํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค.

Source code

~/gazebo_plugin_tutorial๋กœ ์ด๋™ํ•˜์—ฌ system_gui.ccํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค:

$ cd ~/gazebo_plugin_tutorial
$ gedit system_gui.cc

์•„๋ž˜ ๋‚ด์šฉ์„ system_gui.cc์— ๋ณต์‚ฌํ•œ๋‹ค

#include <functional>
#include <gazebo/gui/GuiIface.hh>
#include <gazebo/rendering/rendering.hh>
#include <gazebo/gazebo.hh>

namespace gazebo
{
  class SystemGUI : public SystemPlugin
  {
    /////////////////////////////////////////////
    /// \brief Destructor
    public: virtual ~SystemGUI()
    {
      this->connections.clear();
      if (this->userCam)
        this->userCam->EnableSaveFrame(false);
      this->userCam.reset();
    }

    /////////////////////////////////////////////
    /// \brief Called after the plugin has been constructed.
    public: void Load(int /*_argc*/, char ** /*_argv*/)
    {
      this->connections.push_back(
          event::Events::ConnectPreRender(
            std::bind(&SystemGUI::Update, this)));
    }

    /////////////////////////////////////////////
    // \brief Called once after Load
    private: void Init()
    {
    }

    /////////////////////////////////////////////
    /// \brief Called every PreRender event. See the Load function.
    private: void Update()
    {
      if (!this->userCam)
      {
        // Get a pointer to the active user camera
        this->userCam = gui::get_active_camera();

        // Enable saving frames
        this->userCam->EnableSaveFrame(true);

        // Specify the path to save frames into
        this->userCam->SetSaveFramePathname("/tmp/gazebo_frames");
      }

      // Get scene pointer
      rendering::ScenePtr scene = rendering::get_scene();

      // Wait until the scene is initialized.
      if (!scene || !scene->Initialized())
        return;

      // Look for a specific visual by name.
      if (scene->GetVisual("ground_plane"))
        std::cout << "Has ground plane visual\n";
    }

    /// Pointer the user camera.
    private: rendering::UserCameraPtr userCam;

    /// All the event connections.
    private: std::vector<event::ConnectionPtr> connections;
  };

  // Register this plugin with the simulator
  GZ_REGISTER_SYSTEM_PLUGIN(SystemGUI)
}

Load์™€ Initํ•จ์ˆ˜๋Š” ์ ˆ๋Œ€ ์ฃผ์„์ฒ˜๋ฆฌ(block)ํ•˜๋ฉด ์•ˆ๋œ๋‹ค. Load์™€ Init ํ•จ์ˆ˜๋Š” ์‹œ์ž‘ํ•  ๋•Œ ๊ฐ€์ œ๋ณด๊ฐ€ ๋กœ๋“œ๋˜๊ธฐ ์ „์— ํ˜ธ์ถœ๋œ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์—…๋ฐ์ดํŠธ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์นด๋ฉ”๋ผ (๊ทธ๋ž˜ํ”ฝ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์‚ฌ์šฉ๋œ ์นด๋ฉ”๋ผ)์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ํ”„๋ ˆ์ž„ ์ €์žฅ์„ ํ™œ์„ฑํ™” ํ•œ๋‹ค.

  1. ์‚ฌ์šฉ์ž์˜ ์นด๋ฉ”๋ผ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
    this->userCam = gui::get_active_camera();
  1. ํ”„๋ ˆ์ž„ ์ €์žฅ์„ enable ํ•œ๋‹ค
    this->userCam->EnableSaveFrame(true);
  1. ํ”„๋ ˆ์ž„์ด ์ €์žฅ๋  ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค
    this->userCam->SetSaveFramePathname("/tmp/gazebo_frames");

Compiling Camera Plugin

Hello WorldPlugin tutorial์„ ๊ฑฐ์ณค๋‹ค๋ฉด ~/gazebo_plugin_tutorial/CMakeLists.txt์— ์•„๋ž˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

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

๋นŒ๋“œํ•˜์—ฌ libsystem_gui.so๋ฅผ ์ƒ์„ฑํ•œ๋‹ค

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

Running Plugin

๋จผ์ € gzserver๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ(background)๋กœ ์‹คํ–‰ํ•œ๋‹ค:

$ gzserver &

ํ”Œ๋Ÿฌ๊ทธ์ธ๊ณผ ํ•จ๊ป˜ ํด๋ผ์ด์–ธํŠธ(client)๋ฅผ ์‹คํ–‰ํ•œ๋‹ค:

$ gzclient -g libsystem_gui.so

ํ˜„์žฌ์˜ ํ”Œ๋Ÿฌ๊ทธ์ธ์œผ๋กœ ๋ถ€ํ„ฐ ์ €์žฅ๋œ ์ด๋ฏธ์ง€๋Š” /tmp/gazebo_frames์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Note: ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ข…๋ฃŒํ•œ ํ›„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์„œ๋ฒ„๋ฅผ ์ข…๋ฃŒํ•ด์•ผํ•จ์„ ๊ธฐ์–ตํ•ด์•ผ ํ•œ๋‹ค. ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋ฅผ ์‹คํ–‰ํ•œ ํ„ฐ๋ฏธ๋„์—์„œ foreground๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค:

$ fg

๊ทธ๋ฆฌ๊ณ  Ctrl-C๋ฅผ ๋ˆŒ๋Ÿฌ ์ข…๋ฃŒํ•œ๋‹ค. ์•„๋‹ˆ๋ฉด, gzserver process๋ฅผ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค(kill):

$ killall gzserver
โš ๏ธ **GitHub.com Fallback** โš ๏ธ