Tutorials/intermediate/animate joint

Create a working directory
 * 1) Setup ##

mkdir ~/gazebo_animate_joints cd ~/gazebo_animate_joints


 * 1) World File ##

The world for this tutorial will consist of two different sized boxes connected with a prismatic joint. The large box is on the bottom, the smaller on the top, and the primistic joint operates along the Z axis (vertically).

Create a world file:

gedit ~/gazebo_animate_joints/animate_joints.world

Copy the SDF code below into it:

  

  



    

   

<box size=".5 .5 .5"/>

<box size=".5 .5 .5"/>

<joint name="my_joint" type="prismatic"> <parent link="base_link"/> <child link="top_link"/>

<plugin name="animate_joints" filename="libanimate_joints.so"/>

<light type="directional" name="sun" cast_shadows="true"> <diffuse rgba="1.0 1.0 1.0 1"/> <specular rgba=".1 .1 .1 1"/> <attenuation range="10" constant="0.8" linear="0.01" quadratic="0.0"/> <direction xyz="0 .2 -1.0"/>

The model plugin below will cause the prismatic joint to move up and down which results in the smaller box moving up and down.
 * 1) Plugin ##

Create a model plugin:

gedit ~/gazebo_animate_joints/animate_joints.cc

Copy the code below into a file called `animate_joints.cc`:


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

namespace gazebo { class AnimateJoints : public ModelPlugin {   public: void Load(physics::ModelPtr _model, sdf::ElementPtr /*_sdf*/) {     std::map<std::string, common::NumericAnimationPtr> anim;

// Create a new animation for the "my_joint" define in the SDF file. // The animation will last for 5.0 seconds, and it will repeat anim["my_joint"].reset(new common::NumericAnimation( "my_animation", 5.0, true));

// Create a key frame for the starting position of the joint common::NumericKeyFrame *key = anim["my_joint"]->CreateKeyFrame(0.0); key->SetValue(0.1);

// Create a key frame half-way through the animation key = anim["my_joint"]->CreateKeyFrame(2.5); key->SetValue(2.0);

// Create the end key frame to be at the same position as the start // for a smooth animation key = anim["my_joint"]->CreateKeyFrame(5.0); key->SetValue(0.1);

// Attach the animation to the model _model->SetJointAnimation(anim); } };

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


 * 1) Build ##

Create the CMake file

gedit ~/gazebo_animate_joints/CMakeLists.txt

Copy the script below into it:

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(animate_joints SHARED animate_joints.cc) target_link_libraries(animate_joints ${GAZEBO_LIBRARIES})

Build the plugin

mkdir ~/gazebo_animate_joints/build cd ~/gazebo_animate_joints/build cmake ../ make


 * 1) Run ##

In a terminal run the world cd ~/gazebo_animate_joints/build export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH gazebo ~/gazebo_animate_joints/animate_joints.world