Tutorials/1.3/control robot/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 a box connected with a revolute joint to the inertial space. The revolute joint pinning the box to the world has rotational axis along the x-axis.

Create a world file:

gedit ~/gazebo_animate_joints/animate_joints.world

Copy the SDF code below into it:

  model://ground_plane

model://sun

0 0 2.0 0 0 0        0 0 0 0 0 0           0 1.0 0.0 0 0 0           1.0             1.0             0.0             0.0             1.0             0.0             1.0           0 1.0 0.0 0 0 0               0.1 0.1 0.1                 0 0          0 1.0 0.0 0 0 0               0.1 0.1 0.1        world link 1 0 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.hh"
 * 4) include "physics/physics.hh"

namespace gazebo { class AnimateJoints : public ModelPlugin {   public: void Load(physics::ModelPtr _model, sdf::ElementPtr /*_sdf*/) {     std::map 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["box::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["box::my_joint"]->CreateKeyFrame(0.0); key->SetValue(0.0);

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

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

// 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 GAZEBO_PLUGIN_PATH=`pwd`:$GAZEBO_PLUGIN_PATH gazebo ~/gazebo_animate_joints/animate_joints.world

Next: Control Model