System Plugin - modulabs/gazebo-tutorial GitHub Wiki
Overview / HelloWorld Plugin Tutorial
Source: gazebo/examples/plugins/system_gui_plugin
์ด ํํ ๋ฆฌ์ผ์์๋ /tmp/gazebo_frames ๋๋ ํ ๋ฆฌ์ ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ๋๋ก ์ค๊ณ๋ gzclient์ ์์คํ
ํ๋ฌ๊ทธ์ธ ์์คํ์ผ์ ์์ฑํ๋ค.
~/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 ํจ์๋ ์์ํ ๋ ๊ฐ์ ๋ณด๊ฐ ๋ก๋๋๊ธฐ ์ ์ ํธ์ถ๋๋ค.
์ฒซ ๋ฒ์งธ ์ ๋ฐ์ดํธ์์ ์ฌ์ฉ์๊ฐ ์นด๋ฉ๋ผ (๊ทธ๋ํฝ ์ธํฐํ์ด์ค์์ ์ฌ์ฉ๋ ์นด๋ฉ๋ผ)์ ๋ํ ํฌ์ธํฐ๋ฅผ ๊ฐ์ ธ์ ํ๋ ์ ์ ์ฅ์ ํ์ฑํ ํ๋ค.
- ์ฌ์ฉ์์ ์นด๋ฉ๋ผ๋ฅผ ๊ฐ์ ธ์จ๋ค
this->userCam = gui::get_active_camera();- ํ๋ ์ ์ ์ฅ์ enable ํ๋ค
this->userCam->EnableSaveFrame(true);- ํ๋ ์์ด ์ ์ฅ๋ ์์น๋ฅผ ์ง์ ํ๋ค
this->userCam->SetSaveFramePathname("/tmp/gazebo_frames");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
๋จผ์ gzserver๋ฅผ ๋ฐฑ๊ทธ๋ผ์ด๋(background)๋ก ์คํํ๋ค:
$ gzserver &
ํ๋ฌ๊ทธ์ธ๊ณผ ํจ๊ป ํด๋ผ์ด์ธํธ(client)๋ฅผ ์คํํ๋ค:
$ gzclient -g libsystem_gui.so
ํ์ฌ์ ํ๋ฌ๊ทธ์ธ์ผ๋ก ๋ถํฐ ์ ์ฅ๋ ์ด๋ฏธ์ง๋ /tmp/gazebo_frames์์ ํ์ธํ ์ ์๋ค.
Note: ํด๋ผ์ด์ธํธ๋ฅผ ์ข ๋ฃํ ํ ๋ฐฑ๊ทธ๋ผ์ด๋ ์๋ฒ๋ฅผ ์ข ๋ฃํด์ผํจ์ ๊ธฐ์ตํด์ผ ํ๋ค. ๋ฐฑ๊ทธ๋ผ์ด๋๋ฅผ ์คํํ ํฐ๋ฏธ๋์์ foreground๋ก ๋ณ๊ฒฝํด์ค๋ค:
$ fg
๊ทธ๋ฆฌ๊ณ
Ctrl-C๋ฅผ ๋๋ฌ ์ข ๋ฃํ๋ค. ์๋๋ฉด, gzserver process๋ฅผ ์ข ๋ฃ์ํจ๋ค(kill):
$ killall gzserver