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){
    if(!up){
      this->model->SetWorldPose(ignition::math::Pose3d(0,0,10,0,0,0));
      up=true;
    }
    else{
      this->model->SetWorldPose(ignition::math::Pose3d(0,0,0,0,0,0));
      up=false;
    }
    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
GZ_REGISTER_MODEL_PLUGIN(Reds)
}

and my world file is like this;

<?xml version="1.0"?> 
<sdf version="1.4">
   <world name="default">
      <include>
          <name>"stop_light_1"</name>
          <pose>0 2 0 0 0 0</pose>
          <uri>model://traffic_light_red</uri>
          <plugin name="Reds" filename="libReds.so">
          </plugin>
      </include>
     <include>
        <name>"stop_light_2"</name>
        <pose>0 2 0 0 0 0</pose>
        <uri>model://traffic_light_red</uri>
        <plugin name="Reds" filename="libReds.so">
        </plugin>
    </include>

   </world>
</sdf>

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