Tutorials/1.3/intermediate/pid control joint


 * 1) Tutorial: Use PID Class to Control a Simple Slider Joint in Gazebo#

Create a working directory
 * 1) Setup ##

mkdir ~/gazebo_pid_joints; cd ~/gazebo_pid_joints


 * 1) World File ##

The world for this tutorial will consist of a box connected to the inertial frame ("world") with a prismatic joint, where the primistic joint operates along the Z axis (vertically).

Copy the SDF code below into a file called `pid_joints.world`

  model://ground_plane

model://sun

 0.000000 0.000000 0.500000 0.000000 -0.000000 0.000000                            1.000000 1.000000 1.000000                             1.000000 1.000000 1.000000              world base_link 0.000000 0.000000 1.000000            

The model plugin below will cause the prismatic joint to hold position using the `gazebo::common::PID` controller class.
 * 1) Plugin ##

Copy the code below into a file called `pid_joints.cc`


 * 1) include "gazebo/common/common.hh"
 * 2) include "gazebo/physics/physics.hh"
 * 3) include "gazebo/common/Events.hh"
 * 4) include "gazebo/common/PID.hh"

namespace gazebo { class PIDJoints : public ModelPlugin {   public: void Load(physics::ModelPtr _model, sdf::ElementPtr /*_sdf*/) {     this->model_ = _model; // initialize a PID class this->target_position_ = 1.8; this->pid.Init(100, 0, 1, 0, 0, 100, -100); this->pid.SetCmd(this->target_position_); this->joint_ = this->model_->GetJoint("my_joint"); this->last_update_time_ = this->model_->GetWorld->GetSimTime; this->update_connection_ = event::Events::ConnectWorldUpdateStart(       boost::bind(&PIDJoints::UpdatePID, this)); }   void UpdatePID {     common::Time current_time = this->model_->GetWorld->GetSimTime; double error = this->joint_->GetAngle(0).Radian - target_position_; double dt   = current_time.Double - this->last_update_time_.Double; this->pid.Update(error, dt); this->joint_->SetForce(0, this->pid.GetCmd); this->last_update_time_ = current_time; gzdbg << "error [" << error << "] cmd [" << this->pid.GetCmd << "]\n"; }   common::PID pid; double target_position_; physics::JointPtr joint_; physics::ModelPtr model_; event::ConnectionPtr update_connection_; common::Time last_update_time_; };

// Register this plugin with the simulator GZ_REGISTER_MODEL_PLUGIN(PIDJoints) }


 * 1) Build ##

Create the CMake file by copying the script below into a file called `CMakeLists.txt`

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

include (FindPkgConfig) if (PKG_CONFIG_FOUND) pkg_check_modules(GAZEBO gazebo) endif include_directories(${GAZEBO_INCLUDE_DIRS}) link_directories(${GAZEBO_LIBRARY_DIRS})

add_library(pid_joints SHARED pid_joints.cc) target_link_libraries(pid_joints ${GAZEBO_LIBRARIES})

Build the plugin

mkdir ~/gazebo_pid_joints/build;cd build

cmake ..; make


 * 1) Run ##

In a terminal run gazebo, starting it in a pause state. cd ~/gazebo_pid_joints/build export GAZEBO_PLUGIN_PATH=`pwd`:$GAZEBO_PLUGIN_PATH gazebo -u ../pid_joints.world

When Gazebo loads, start the simulation by clicking the Play button.