Skip to content

Common Used System Plugin API

important part in api doc:
- gz::sim
- gz::sim::EntityComponentManager
- gz::sim::components

Understand Component

using X = Component<DataType, class Tag>;

//        or 

using X = Component<DataType, class Tag, Serializer>;

to access component: components::X , X is the component Name
Datatype: data type of that component
Serializer: use for data transfer (not so important)

name

vel

model

access:
- components::Name
- components::LinearVelocity
- components::Model




System Plugin Steps

find entity

1. Find The Entity

We will take help of components to find entity integer value like:

  • has Name component of value xyz
  • has both Entity and Name components
a. plugin is directly attached to entity in world file .sdf
 <model>
    ...

    <plugin>..... </plugin>   
 </model>
code
 void MoveModel::Configure(const Entity &_entity, const std::shared_ptr<const sdf::Element> &_sdf,
                    EntityComponentManager &_ecm,
                    EventManager &/*_eventMgr*/){
        // _entity --> is the entity of model
        entity = _entity;
}
b. search the entity by its name
   // find the entity (int value) which has Name Component with value as "xyz"
   targetEntity = _ecm.EntityByComponents(components::Name("xyz"));
auto entityOpt = _ecm.EntityByName("abc");
entity = entityOpt.value()
c. find multiple entity
//check for such components which has Light, Name components
// each
_ecm.Each<components::Light, components::Name>(
  [&](const Entity &_entity,
      const components::Light *,
      const components::Name *_name) -> bool
  {
    this->lightEntites.push_back(_entity);
    // gzmsg << "Found light: " << _name->Data()
    //       << " (entity " << _entity << ")\n";
    return true;
  });
d. find EntityByComponents uing ```EntityByComponents()``` find such a joint entity which has all of the followings components: parent entity is ***model component*** to which plugin is attached, has a ***name component*** of value ```joint_1``` ***joint compenent*** is attach on it [reference code](https://github.com/gazebosim/gz-sim/blob/gz-sim8/src/Model.cc#L132C1-L133C1)
  this->jointEntity = _ecm.EntityByComponents(
      components::ParentEntity(_entity),
      components::Name(jointName),
      components::Joint());

2. Read/Write on the Component value

command

...Cmd means to command to change that container value like LinearVelocityCmd, WorldPoseCmd, VisualCmd

Read

a. Component
//find Name component value of the entity(any entity_type eg model,link,world etc)
auto Name = _ecm.Component<components::Name>(this->targetEntity);
this->modelName = Name->Data();

Write

a. SetComponentData
const gz::math::Vector3d vel(0.0, 0.0, this->zVelocity);
//set the LinearVelocityCmd component value to be vel
_ecm.SetComponentData<components::LinearVelocityCmd>(this->targetEntity,{vel});
b. mutable component
//set the LinearVelocityCmd component value to be vel
auto velComp = _ecm.Component<components::LinearVelocityCmd>(this->targetEntity);
velComp->Data() = vel;