How can i apply same model plugin to different models?

asked 2022-04-24 04:06:53 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hi, im new in Gazebo. I've tried to write a model plugin that changes pose of attached model. My plugin;

#include <functional>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <ignition/math/Vector3.hh>
#include <ros/ros.h>

double old_time =0;
bool up =false;
namespace gazebo
class Reds : public ModelPlugin
/// \brief Pointer to the world
public: physics::WorldPtr world;

public: void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
  // Store the pointer to the model
  this->model = _parent;

  // Listen to the update event. This event is broadcast every
  // simulation iteration.
  this->updateConnection = event::Events::ConnectWorldUpdateBegin(
      std::bind(&Reds::OnUpdate, this));

  ROS_WARN("Loaded Reds Plugin with parent...%s", this->model->GetName().c_str());

// Called by the world update start event
public: void OnUpdate()
  if(ros::Time::now().toSec() - old_time>=4){
    old_time = ros::Time::now().toSec();

// Pointer to the model
private: physics::ModelPtr model;

// Pointer to the update event connection
private: event::ConnectionPtr updateConnection;

// Register this plugin with the simulator

and my world file is like this;

<?xml version="1.0"?> 
<sdf version="1.4">
   <world name="default">
          <pose>0 2 0 0 0 0</pose>
          <plugin name="Reds" filename="">
        <pose>0 2 0 0 0 0</pose>
        <plugin name="Reds" filename="">


When i launch the world i can see gazebo register plugins, image description but it only effect first one. Ive tried to write another plugin wich is same as Reds plugin and add this new plugin to it. It didn't work. How can i apply same model plugin to different models?

edit retag flag offensive close merge delete