Gazebo | Ignition | Community
Ask Your Question
0

onComplete SetAnimation problem...

asked 2013-01-30 12:12:35 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

void SetAnimation (const common::PoseAnimationPtr &_anim, boost::function< void()> _onComplete)

With the second parameter...I'm trying to do "Things to try on your own" section in the wiki of Gazebo, "Controlling a Robot"...

Here is my code:

#include <boost/bind.hpp>
#include "common/CommonTypes.hh"
#include "common/Animation.hh"
#include "common/KeyFrame.hh"
#include "physics/Entity.hh"
#include "physics/World.hh"
#include "gazebo.hh"

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

      gazebo::common::PoseAnimationPtr anim2(
          new gazebo::common::PoseAnimation("test2", 1000.0, true));

      gazebo::common::PoseKeyFrame *key;

      gazebo::common::PoseKeyFrame *key2;

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

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

      key2 = anim2->CreateKeyFrame(0);
      key2->SetTranslation( math::Vector3(0,0,0) );
      key2->SetRotation( math::Quaternion(0,0,0) );

      key2 = anim2->CreateKeyFrame(10.0);
      key2->SetTranslation( math::Vector3(0,3,0) );
      key2->SetRotation( math::Quaternion(0,0,0) );

      _parent->GetEntity("box_model")->SetAnimation(anim);
      _parent->GetEntity("box_model2")->SetAnimation(anim2, anim);
    }
  };
  // Register this plugin with the simulator
  GZ_REGISTER_WORLD_PLUGIN(AnimationTest)
}

Thank you so much!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-01-30 12:39:32 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

The second parameter to SetAnimation is a boost function pointer. The function pointer will be called when the animation is complete. This lets you set and animation, and get a callback on completion.

Here is the api: http://gazebosim.org/api/1.3.1/classgazebo11physics11Entity.html#a0ba2d580de851fabb0ef90f8e53bedcd

What you can do is:

#include <boost/bind.hpp>
#include "common/CommonTypes.hh"
#include "common/Animation.hh"
#include "common/KeyFrame.hh"
#include "physics/Entity.hh"
#include "physics/World.hh"
#include "gazebo.hh"

namespace gazebo
{
  class AnimationTest : public WorldPlugin
  {
    public: void Callback()
    {
      printf("Animation complete\n");
    }

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

      gazebo::common::PoseAnimationPtr anim2(
          new gazebo::common::PoseAnimation("test2", 1000.0, true));

      gazebo::common::PoseKeyFrame *key;

      gazebo::common::PoseKeyFrame *key2;

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

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

      key2 = anim2->CreateKeyFrame(0);
      key2->SetTranslation( math::Vector3(0,0,0) );
      key2->SetRotation( math::Quaternion(0,0,0) );

      key2 = anim2->CreateKeyFrame(10.0);
      key2->SetTranslation( math::Vector3(0,3,0) );
      key2->SetRotation( math::Quaternion(0,0,0) );

      _parent->GetEntity("box_model")->SetAnimation(anim);
      _parent->GetEntity("box_model2")->SetAnimation(anim2,
        boost::bind(&AnimationTest::Callback, this));
    }
  };
  // Register this plugin with the simulator
  GZ_REGISTER_WORLD_PLUGIN(AnimationTest)
}
edit flag offensive delete link more
0

answered 2013-01-31 05:00:42 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Ok, 2 doubts: -How knows the second animation that "anim" (first animation) has finished? -It doesn't work for me, the two animations start at the same time. Should printf show in console or somewhere in Gazebo?

Thank you again.

edit flag offensive delete link more

Comments

"anim2" has no knowledge about "anim". The callback associated with "anim2" will be triggered only when "anim2" finishes. Both animations should start at the same time because the code starts both animation at the same time. The SetAnimation() function is non-blocking.

nkoenig gravatar imagenkoenig ( 2013-01-31 09:51:15 -0500 )edit

Question Tools

Stats

Asked: 2013-01-30 12:12:35 -0500

Seen: 330 times

Last updated: Jan 31 '13