https://classic.gazebosim.org/tutorials?tut=system_plugin&cat=write_plugin
这是一个gzclient的系统插件,将图像保存在/tmp/gazebo_frames
1. 源码
#include <functional>
#include <gazebo/gui/GuiIface.hh>
#include <gazebo/rendering/rendering.hh>
#include <gazebo/gazebo.hh>
namespace gazebo
{
class SystemGUI : public SystemPlugin
{
// Pointer the user camera.
private: rendering::UserCameraPtr userCam;
// All the event connections
private: std::vector<event::ConnectionPtr> connections;
// Destructor
public: virtual ~SystemGUI()
{
this->connections.clear();
if (this->userCam) this->userCam->EnableSaveFrame(false);
this->userCam.reset();
}
// 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)));
}
// Called once after Load
private: void Init() {}
// 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";
}
};
// Rigister this plugin with the simulator
GZ_REGISTER_SYSTEM_PLUGIN(SystemGUI)
}
2. 运行
编译并运行。略
gzserver
# 另一个终端
gzclient -g libsystem_gui.so
# 完事了要结束server进程
fg
# 或 killall gzserver
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 cdd@ahucd.cn