Control SDF Model with C++ Code

In this post, I will show the reader how to control our RP2W from the previous post titled “Model Robots for Gazebo Robotics Simulator: Tutorial” with C++ code with the Gazebo API. Using Gazebo 5.0.1 and Ubuntu 14.04

This tutorial is based largely on the model plugin tutorial from the Gazebo website.

This post assumes that you have followed the previous post and you have an SDF file that models the custom RP2W robot. By the end of this tutorial, you will have a simple C++ code that can applies a constant force to every single joint of the SDF model.

First, ensure that you have the Gazebo development files. Launch terminal (shortcut CTRL+ALT+T) and type:

sudo apt-get install libgazebo5-dev

Next, create a directory (/gazebo_control) and C++ file ( for your plugin:

mkdir ~/gazebo_control
cd ~/gazebo_control

In your file, copy+paste the following:

#include <boost/bind.hpp>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <stdio.h>

namespace gazebo
  class rp2w : 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::ConnectWorldUpdateBegin(
          boost::bind(&rp2w::OnUpdate, this, _1));

    // Called by the world update start event
    public: void OnUpdate(const common::UpdateInfo & /*_info*/)
    	physics::JointPtr chassis_right_wheel=model->GetJoint("chassis_right_wheel");
			physics::JointPtr chassis_left_wheel=model->GetJoint("chassis_left_wheel");
			physics::JointPtr camera_base_camera=model->GetJoint("camera_base_camera");
			physics::JointPtr body_camera_base=model->GetJoint("body_camera_base");
			physics::JointPtr body_front_wheel_y=model->GetJoint("body_front_wheel_y");
    // Pointer to the model
    private: physics::ModelPtr model;

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

  // Register this plugin with the simulator

The Load() function is called once when the plugin is loaded by Gazebo. TheOnUpdate()
function is continuously called until interrupted. You will control the SDF model’s joints inOnUpdate(). First, you need aphysics::JointPtrpointer object to access each joint in the SDF model:
physics::JointPtr /*NAME_OF_JOINT*/ = model->GetJoint("NAME_OF_JOINT")
Then, you will set the force of each joint to 1.00:

For a full list of things you can do with a Gazebo plugin, reference the Gazebo API.

Now, let’s compile the plugin. You will need to create a CMakeLists.txt file:

gedit ~/gazebo_control/CMakeLists.txt

Copy and paste the following into CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
find_package(Boost REQUIRED COMPONENTS system)
include (FindPkgConfig)
pkg_check_modules(GAZEBO gazebo)
add_library(rp2w SHARED
target_link_libraries(rp2w ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES})
list( APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")

Now, create the build directory:

mkdir ~/gazebo_control/build


cmake .

You will now have a ~/gazebo_control/build/ that you will insert into your SDF file in order to run it in Gazebo.

Now, you will need to add your plugin’s path to GAZEBO_PLUGIN_PATH:

export GAZEBO_PLUGIN_PATH=$(GAZEBO_PLUGIN_PATH):~/gazebo_control/build

Lastly, you need to insert a tag into your SDF file in order to link your plugin to your RP2W model. If you followed the previous tutorial, you should have

<plugin name="rp2w" filename="/home/e-motion/gazebo_control/"/>

nested inside the <model> tag.

Start up Gazebo and insert your RP2W model into the world. You should see all of its joints in action.