Tutorials/intermediate/animate pose

An animation engine built into Gazebo allows you to move models around the environment without relying on the physics engine. This may be desirable for character animation, or any situation where planned movement of a non-dynamic object is desired.

In this tutorial, we will keep it simple and animate a box on a plane.


 * 1) Setup ##

First create a work directory.

cd ~ mkdir gazebo_animate_pose cd gazebo_animate_pose


 * 1) World File ##

This is the world we'll use for this tutorial. Notice that we defined our box model as `static`. This tells Gazebo not to simulate the model in the physics engine.

The world file will not run at this point because we have not yet compiled and installed the plugin specified in the world file.

Copy and past this xml code into a file called `animate_pose.world`

  model://ground_plane

 0 0 0.5 0 0 0                            1 1 1                             1 1 1              true  0 0 10 0 0 0            1.0 1.0 1.0 1             0 0 0 1                 20                 0.01                 0.8                 0.0             0.0 0.2 -8.0             true


 * 1) Animation Plugin ###

The plugin must be attached to a model (as in the above world file). When it is loaded, the plugin will move its parent model 5 meters in the X direction, and apply a 180 degree rotation around the Z axis.

Copy and paste this code into a file called `animate_pose.cc`


 * 1) include "common/CommonTypes.hh"
 * 2) include "common/Animation.hh"
 * 3) include "common/KeyFrame.hh"
 * 4) include "physics/Model.hh"
 * 5) include "gazebo.hh"

namespace gazebo { class AnimatePose : public ModelPlugin {   public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/) {     gazebo::common::PoseAnimationPtr anim(          new gazebo::common::PoseAnimation("test", 1000.0, true));

gazebo::common::PoseKeyFrame *key;

key = anim->CreateKeyFrame(0); key->SetTranslation(math::Vector3(0, 0, 0)); key->SetRotation(math::Quaternion(0, 0, 0));

key = anim->CreateKeyFrame(1000.0); key->SetTranslation(math::Vector3(5, 0, 0)); key->SetRotation(math::Quaternion(0, 0, 1.5707));

_parent->SetAnimation(anim); } };

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

Copy the contents of this CMake script into a file called `CMakeLists.txt`
 * 1) Build ##

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_pose SHARED animate_pose.cc) target_link_libraries(animate_pose ${GAZEBO_libraries})

Create a `build` directory

mkdir build cd build

Compile the plugin

cmake ../ make
 * 1) Run Tutorial ##

Add the current directory to the shared library path and run gazebo with the world we just created. In a terminal

cd ~/gazebo_animate_pose/build export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. gzserver ../animate_pose.world

In another terminal

gzclient

You should see the box moving forward and rotating.