Print Entity System Plugin¶
.hh file
#ifndef SYSTEM_PLUGIN_MODEL_HH_
#define SYSTEM_PLUGIN_MODEL_HH_
//! [header]
#include <gz/sim/System.hh> // to inherit system
#include "gz/sim/Model.hh" // for Model component
#include "gz/sim/components/LinearVelocity.hh" // for linear velocity
#include "gz/sim/components/LinearVelocityCmd.hh" // for LinearVelocityCmd component
#include "gz/sim/components/Name.hh" // for Name component
#include <gz/plugin/Register.hh> //for GZ_ADD_PLUGIN_ALIAS()
namespace gz
{
namespace sim
{
namespace systems
{
/// \brief plugin to move a model
/// plugin interface.
class MoveModel :
// This class is a system.
public gz::sim::System,
public gz::sim::ISystemConfigure,
// This class also implements the ISystemPreUpdate interface.
public gz::sim::ISystemPreUpdate
{
public:
MoveModel();
~MoveModel() override;
void Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &_eventMgr) override;
void PreUpdate(const UpdateInfo &_info,
EntityComponentManager &_ecm) override;
private:
std::string modelName;
double zVelocity{0.0};
Entity targetEntity{kNullEntity};
};
}
}
}
//! [header]
#endif
.cc file
#include "PrintEntitySystemPlugin.hh"
using namespace gz;
using namespace sim;
using namespace systems;
PrintEntitySystemPlugin::PrintEntitySystemPlugin(){
std::cout<<"PrintEntitySystemPlugin Plugin Started!!"<<std::endl;
}
PrintEntitySystemPlugin::~PrintEntitySystemPlugin(){
std::cout<<"PrintEntitySystemPlugin Plugin stopped!!"<<std::endl;
}
void PrintEntitySystemPlugin::Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &/*_eventMgr*/)
{
this->targetEntity = _entity;
auto Name = _ecm.Component<components::Name>(this->targetEntity);
this->modelName = Name->Data();
gzmsg << "Target Entity value: " << this->targetEntity << std::endl;
gzmsg << "Target Entity Name: " << this->modelName << std::endl;
}
void PrintEntitySystemPlugin::PreUpdate(const UpdateInfo &_info,
EntityComponentManager &_ecm)
{
// Only run if the simulation is not paused
if (_info.paused)
return;
}
// Register the plugin with Gazebo Sim
GZ_ADD_PLUGIN(gz::sim::systems::PrintEntitySystemPlugin,
gz::sim::System,
gz::sim::ISystemConfigure,
gz::sim::ISystemPreUpdate)
GZ_ADD_PLUGIN_ALIAS(gz::sim::systems::PrintEntitySystemPlugin,
"gz::sim::systems::PrintEntitySystemPlugin")
lets understand code line by line
.hh file Explanation¶
include header files
#ifndef WORLD_PLUGIN_HH
#define WORLD_PLUGIN_HH
//! [header]
#include <gz/sim/System.hh> //to inherit system
#include "gz/sim/components/Name.hh" // for name component
#include <gz/plugin/Register.hh> //for GZ_ADD_PLUGIN_ALIAS()
all plugin are in gz::sim:::systems namespace
namespace gz
{
namespace sim
{
namespace systems
{
/// \brief plugin to move a model
/// plugin interface.
class PrintEntitySystemPlugin :
// This class is a system.
public gz::sim::System,
public gz::sim::ISystemConfigure,
public gz::sim::ISystemPreUpdate
{
public:
PrintEntitySystemPlugin();
~PrintEntitySystemPlugin() override;
void Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &_eventMgr) override;
void PreUpdate(const UpdateInfo &_info,
EntityComponentManager &_ecm) override;
private:
std::string modelName;
Entity targetEntity;
};
}
}
}
PrintEntitySystemPlugin <- inherits
- gz::sim::System : needed for all system plugins
- `gz::sim::ISystemConfigure: needed for accesing sdf,entity
- gz::sim::ISystemPreUpdate: we are just reading data so even PostUpdate can be used but it not doing anything here.
as we know from previous understanding as we inherit ISystemConfigure we need to write defination of Configure() function
& same way ISystemPreUpdate we nee to write defination of PreUpdate(..) function
.cc file Explanation¶
same gz::sim:::systems namespace are added
#include "PrintEntitySystemPlugin.hh"
using namespace gz;
using namespace sim;
using namespace systems;
prints added on constructor & destructor
PrintEntitySystemPlugin::PrintEntitySystemPlugin(){
std::cout<<"PrintEntitySystemPlugin Plugin Started!!"<<std::endl;
}
PrintEntitySystemPlugin::~PrintEntitySystemPlugin(){
std::cout<<"PrintEntitySystemPlugin Plugin stopped!!"<<std::endl;
}
main code is
_entity is the entity it attached to world,model,light etc
void PrintEntitySystemPlugin::Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &/*_eventMgr*/)

_entity is world entity

here it _entity is model entity
means find the value of the Name Component attached to targetEntity


Name->Data() why? ->Data() because its comes from component class
return type of _ecm.Component<..> is component
gz::sim::components::component in gz doc

need register the plugin with all the class it inhertited
// Register the plugin with Gazebo Sim
GZ_ADD_PLUGIN(gz::sim::systems::PrintEntitySystemPlugin,
gz::sim::System,
gz::sim::ISystemConfigure,
gz::sim::ISystemPreUpdate)
alias
GZ_ADD_PLUGIN_ALIAS(gz::sim::systems::PrintEntitySystemPlugin,
"gz::sim::systems::PrintEntitySystemPlugin")
Important: only name=gz::sim::systems::PrintEntitySystemPlugin allowed for plugin you can't put any other name because it how we define on the alis GZ_ADD_PLUGIN_ALIAS(...)
so
<!-- custom plugin attach to world-->
<plugin
filename="PrintEntitySystemPlugin"
name="gz::sim::systems::PrintEntitySystemPlugin">
</plugin>
filename= PrintEntitySystemPlugin
or
filename= libPrintEntitySystemPlugin.so
so no other name is allowed
comes from CMakeList.txt