10 #include <gazebo/common/Plugin.hh>
11 #include <gazebo/physics/Model.hh>
12 #include <gazebo/physics/Link.hh>
13 #include <gazebo/physics/Joint.hh>
14 #include <gazebo/common/Events.hh>
15 #include <ignition/math/Pose3.hh>
17 #include <boost/bind.hpp>
19 #include <yarp/os/Bottle.h>
20 #include <yarp/os/BufferedPort.h>
21 #include <yarp/os/LogStream.h>
26 class ModelMover :
public gazebo::ModelPlugin
28 gazebo::physics::ModelPtr model;
29 gazebo::event::ConnectionPtr renderer_connection;
30 yarp::os::BufferedPort<yarp::os::Bottle> port;
31 ignition::math::Vector3d starting_pos;
36 if (
auto* b = port.read(
false))
40 if (model->GetJoint(
"fixed_to_ground"))
42 if (model->RemoveJoint(
"fixed_to_ground"))
44 yInfo() <<
"Removed fixed_to_ground joint";
48 const auto x = starting_pos.X() + b->get(0).asFloat64();
49 const auto y = starting_pos.Y() + b->get(1).asFloat64();
50 const auto z = starting_pos.Z() + b->get(2).asFloat64();
51 ignition::math::Pose3d new_pose(x, y, z, 0.0, 0.0, 0.0);
52 yInfo() <<
"New pose:" << x << y << z;
53 model->SetWorldPose(new_pose);
55 physics::LinkPtr child = model->GetLink(
"red-ball::root_link");
56 physics::LinkPtr parent = model->GetLink(
"world");
59 if (model->CreateJoint(
"fixed_to_ground",
"fixed", parent, child))
61 yInfo() <<
"Added fixed_to_ground joint";
70 void Load(gazebo::physics::ModelPtr model, sdf::ElementPtr)
73 auto model_sdf = model->GetSDF();
74 if( model_sdf->HasElement(
"pose") )
76 starting_pos = model_sdf->Get<ignition::math::Vector3d>(
"pose");
80 starting_pos = ignition::math::Vector3d(0.0, 0.0, 0.0);
83 port.open(
"/" + model->GetName() +
"/mover:i");
84 auto bind = boost::bind(&ModelMover::onWorldFrame,
this);
85 renderer_connection = gazebo::event::Events::ConnectWorldUpdateBegin(bind);
100 GZ_REGISTER_MODEL_PLUGIN(gazebo::ModelMover)