7.5 编写插件-系统插件

预备知识

概述/HelloWorld插件教程

概述

来源:gazebo/examples/plugins/system_gui_plugin

https://bitbucket.org/osrf/gazebo/src/gazebo5/examples/plugins/system_gui_plugin

本教程将创建一个源文件,它是gzclient的一个系统插件,用于将图像保存到目录/tmp/gazebo_frames中。

源代码

我们将从源文件开始。创建一个名为system_gui.cc的文件。以下内容:

$ cd~/gazebo_plugin_tutorial

$ geditsystem_gui.cc

将下面的内容复制到system_gui.cc

#include<gazebo/math/Rand.hh>
#include<gazebo/gui/GuiIface.hh>
#include<gazebo/rendering/rendering.hh>
#include<gazebo/gazebo.hh>
 
namespacegazebo
{
  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 beenconstructed.
    public: void Load(int /*_argc*/, char **/*_argv*/)
    {
      this->connections.push_back(
          event::Events::ConnectPreRender(
            boost::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 usercamera
        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 groundplane 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函数都不能阻塞。Load和Init函数在启动时被调用,且在Gazebo被加载之前。

在第一次Update中,我们得到一个指向用户摄像头的指针(在图形界面中使用的摄像头),并支持帧的保存。

1.      获取用户的相机

this->userCam = gui::get_active_camera();

2.      启用保存帧

this->userCam->EnableSaveFrame(true);

3.      设置保存帧的位置

this->userCam->SetSaveFramePathname("/tmp/gazebo_frames");

编译相机插件

假设读者已经阅读了Hello world插件教程,那么需要做的就是将以下代码添加到~/gazebo_plugin_tutorial/CMakeLists.txt。

add_library(system_guiSHARED system_gui.cc)

target_link_libraries(system_gui${GAZEBO_LIBRARIES})

重新构建,您应该得到一个libsystem_gui.so。

$ cd~/gazebo_plugin_tutorial/build

$ cmake ../

$ make

运行插件

首先在后台启动gzserver:

$ gzserver&

用插件运行客户端:

$ gzclient -glibsystem_gui.so

在/tmp/gazebo_frames中,您应该从当前的插件中看到许多保存的图像。

注意:请记住,在退出客户端后,也要终止后台服务器进程。在同一个终端中,将这个过程引入前台:

$ fg

然后按ctrl-c停止进程。或者,只需要杀死gzserver进程:

$ killallgzserver

猜你喜欢

转载自blog.csdn.net/jackkang01/article/details/78787972
7.5