Tutorials/1.0/plugins/model manipulation plugin


 * Prerequisites:** Overview Plugin Tutorial

> Note: If you're continuing from the previous tutorial, make sure you put in the proper `#include` lines for this tutorial that are listed below.


 * Code:** `/examples/plugins/model_push`

Plugins allow complete access to the physical properties of models and their underlying elements (links, joints, collision objects). The following plugin will apply a linear velocity to its parent model.

Plugin Code:
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 

namespace gazebo {    class ModelPush : public ModelPlugin {   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::ConnectWorldUpdateStart(         boost::bind(&ModelPush::OnUpdate, this)); }

// Called by the world update start event public: void OnUpdate {     // Apply a small linear velocity to the model. this->model->SetLinearVel(math::Vector3(.03, 0, 0)); }

// 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(ModelPush) }


 * 1) Compiling the Plugin ###

Assuming the reader has gone through the Simple World tutorial, all that needs to be done in addition is save the above code as `~/gazebo_plugin_tutorial/model_push.cc` and add the following lines to `~/gazebo_plugin_tutorial/CMakeLists.txt`

add_library(model_push SHARED model_push.cc) target_link_libraries(model_push ${GAZEBO_libraries})

Compiling this code will result in a shared library, ~/gazebo_plugin_tutorial/build/libmodel_push.so, that can be inserted in a Gazebo simulation.


 * 1) Running the Plugin ###

This plugin is used in the world file `examples/plugins/model_push/model_push.world`.

  model://ground_plane

model://sun 0 0 0.5 0 0 0                      1 1 1                       1 1 1               

The hook to attach a plugin to a model is specified at the end of the model element block using:



Make a copy of the above world file and save it locally as `~/gazebo_plugin_tutorial/model_push.world`.

To start simulation, run

gzserver -u ~/gazebo_plugin_tutorial/model_push.world

The `-u` option starts the server in a paused state.

In a separate terminal, start the gui

gzclient

Click on the play button in the gui to unpause the simulation, and you should see the box move.


 * 1) Common Mistakes

1. **Can't find ModelPtr during compilation**

Make sure you have included Phyics.hh:

#include 