Gazebo | Ignition | Community
Ask Your Question

How to Get Velocity of a joint using API ?

asked 2015-05-19 10:07:03 -0500

akshay5059 gravatar image

I want to get the velocity of a particular joint using API. I do not want to use ROS or plugin. Can anyone please help me / I am trying to use gazebo::physics:Joint:GetVelocity() funcion but for that we need Base Pointer for the constructor of gazebo::physics::Joint. Can anyone please tell me what base pointer is and how to get it ?

edit retag flag offensive close merge delete


Are you using c++? I know you write you dont want to use a plugin but i just need to be certain.

Tytteboevsen gravatar imageTytteboevsen ( 2015-05-19 11:20:11 -0500 )edit

Hi Tytteboevsen ! Yes I am using C++ to write the api , then making it executable file using cmake and make commands and I am trying to link it to gazebo directly using gazebo::setupClient(argc, _argv). I read about the GetVelocity function from the following link : . Thank you .

akshay5059 gravatar imageakshay5059 ( 2015-05-20 09:43:57 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-05-20 11:25:52 -0500

Tytteboevsen gravatar image

Hi Akshay5059

Here is a program i wrote some time ago. As i recall it works. let me know if you have any problems with it. I do not know about base pointers i just remember getting this to work:

#include <boost/bind.hpp>
#include <gazebo.hh>
#include <physics/physics.hh>
//#include <physics/World.hh> //added to use getsimtime
#include <common/common.hh>
#include <stdio.h>
#include <unistd.h> // sleep function you added yourself
#include <gazebo/physics/Joint.hh> //These two lines you added to use the function getforcetorque
#include <gazebo/physics/JointWrench.hh> //These two lines you added to use the function getforcetorque

int a=0;
int b;
double angle;
double currenttime;
double PI = 3.141592654;
namespace gazebo
/*  class HelloWorld : public WorldPlugin
    public: HelloWorld() : WorldPlugin() 
          printf("Hello World!\n");

    public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)

  class SetJoints : public ModelPlugin
    public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/) 
      // Store the pointer to the model
      this->model = _parent;

      this->j2_controller = new physics::JointController(model);

      //this->last_update_time_ = this->model->GetWorld()->GetSimTime(); //Trying to get simtime. not needed.

      this->joint1_ = this->model->GetJoint("my_joint1");
      this->joint2_ = this->model->GetJoint("my_joint2");

      //physics::JointPtr joint = this->model->GetJoint("my_joint");
      //physics::JointWrench wrench = joint->GetForceTorque(0);

      // Listen to the update event. This event is broadcast every
      // simulation iteration.
      this->updateConnection = event::Events::ConnectWorldUpdateBegin(
      boost::bind(&SetJoints::OnUpdate, this, _1));

    // Called by the world update start event
    public: void OnUpdate(const common::UpdateInfo & /*_info*/)
      common::Time current_time = this->model->GetWorld()->GetSimTime();//getting simtime
      // Apply a small linear velocity to the model.
      //this->model->SetLinearVel(math::Vector3(.03, 0, 0));
      std::string j2name("my_joint");  

      double current_angle = this->joint1_->GetAngle(0).Radian();
      double current_vel = this->joint1_->GetVelocity(0);
      physics::JointWrench wrench1 = this->joint1_->GetForceTorque(0);
      //double forcetorque = this->joint_->GetForceTorque(0);
      //double current_forcetorque = this->joint_->GetForce(0); //doesn't work yet. try using GetForce

      float test = wrench1.body2Force[2];

      printf("Hello World! %lf\n",test);
//      printf("Hello World!\n");
      //j2_controller->SetJointPosition(this->model->GetJoint("my_joint"), angle);


    // Pointer to the model
    private: physics::ModelPtr model;

    // Pointer to the update event connection
    private: event::ConnectionPtr updateConnection;

    private: physics::JointController * j2_controller;

    //common::Time last_update_time_; Not needed

    //Things for saving joint_ in.
    physics::JointPtr joint1_;
    physics::JointPtr joint2_;
    //physics::JointPtr joint;


  // Register this plugin with the simulator
edit flag offensive delete link more


thank you. this worked for me. i had no idea how to properly implement OnUpdate. wish there was better docs.

tacyon gravatar imagetacyon ( 2018-06-28 01:21:18 -0500 )edit
Login/Signup to Answer

Question Tools



Asked: 2015-05-19 10:07:03 -0500

Seen: 3,161 times

Last updated: May 20 '15