0
$\begingroup$

Rosanswers logo

Drear ROS users,
I have a model of a robot arm (KUKA youBot) running in Gazebo. In my cpp program, which generates messages for the Joints of the model, I also need to somehow read and store into a variable the current positions of the Joints.

As far as I know, the corresponding messages are published under the topic named "joint_states". So, when I type in a separate Terminal Window "rostopic echo joint_states", I get all the names of the joints together with corresponding positions, velocities, etc.,constantly published with some high freq. (+ from the youbot Documentation (Locomotec) it is known, that "the arm periodically publish JointState messages for position and velocities of the arm joints").

So, how can I subscribe for this topic "joint_states", read out and store the positions of the arm joints?

Hope for your help!

2 dornhege: Please, if possible, explain in some more detail, what do you mean by "just use sensor_msgs::JointState"? Please give a short example on how to, e.g., add 0.03 to all current positions of the Joints, which have currently been read out by "just using sensor_msgs::JointState".. You will help me so much!.. - ASMIK2011ROS (13 mins ago)


Originally posted by ASMIK2011ROS on ROS Answers with karma: 62 on 2011-06-05

Post score: -1

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

You subscribe to joint_states like you subscribe for any other topic: [Tutorial](http://www.ros.org/wiki/ROS/Tutorials/WritingPublisherSubscriber(c%2B%2B\) )

Example for accessing joint state:

for(unsigned int i = 0; i < msg.position.size(); i++) { msg.position[i] += 0.03; }

You can lookup the message definitions in the ROS wiki.


Originally posted by dornhege with karma: 31395 on 2011-06-05

This answer was NOT ACCEPTED on the original site

Post score: 4


Original comments

Comment by ASMIK2011ROS on 2011-06-05:
.. Please, if possible, explain in some more detail, what do you mean by "just use sensor_msgs::JointState"? Please give a short example on how to, e.g., add 0.03 to all current positions of the Joints, which have currently been read out by "just using sensor_msgs::JointState".. You will help me so much!..

Comment by dornhege on 2011-06-05:
You don't really need to put something in variable - just use the sensor_msgs::JointState.

Comment by dornhege on 2011-06-05:
yes, the signature is correct. ROS_INFO will print out, so you see results on the screen (it also sends it to /rosout for logging).

Comment by ASMIK2011ROS on 2011-06-05:
And this tutorial also doesn't answer the question "how to save the Joint positions into a variable". How to correctly set the type of such a variable? Do I definitely need to use the callback function for this? (As well as previous question, this one is also asked not without reading all the possible tutorials. So, I will be grateful for some EXAMPLE OF YOURS.)

Comment by ASMIK2011ROS on 2011-06-05:
void callback(const sensor_msgs::JointState::ConstPtr& msg)
{ ROS_INFO("Received: ", msg->position); cout<<Subscriber works!" };

Comment by ASMIK2011ROS on 2011-06-05:
Unfortunately this tutorial is not clear for me. E.g. with ROS_INFO we can publish the positions - where should I see them? And I should write the callback function, when using not std_msgs, but sensor_msgs, as follows, right?:

$\endgroup$
0
$\begingroup$

Rosanswers logo

OK, I've solved the problem. So, once again ('cause seemingly the original description was not clear enough or too long to read(?) ;), my problem was: I HAD TO BE ABLE TO READ OUT AND STORE THE POSITIONS FROM THE MODEL IN GAZEBO "FROM TIME TO TIME, WHEN NEEDED" INSIDE A "GLOBAL" PROGRAM, WHICH HAS PLENTY OF OTHER DIFFERENT TASKS (So, creating a separate node for subscribing for messages of joint_state topic was of course not the solution I needed). HERE'S THE CODE I USED inside my "global program":

using namespace std;
float joint_pos_global;
void callback(const sensor_msgs::JointState & msg)
{
joint_pos_global = msg.position[3];
}
int main(int argc, char** argv) {
//--------//
ros::init(argc, argv, "listener");
ros::NodeHandle nh;
ros::Subscriber sub; sub = nh.subscribe ("joint_states", 10, callback);
//--------//

smth....smth...smth...smth...

//--Here I suddenly need to know the current position of joint Numb. 3--//
ros::spinOnce();
cout << "Position of joint 3 is" << joint_pos_global << "\n";
//--------------------------------------------------------------------------------------------------//

                           }

Originally posted by ASMIK2011ROS with karma: 62 on 2011-06-10

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by JonW on 2011-06-10:
That may not work as you expect - I believe that Messages are queued up for delivery by ROS until the spin occurs at which point all callbacks occur. When the queue overflows messages are dropped, so the joint states you are seeing may be the ones from immediately after your last spin.

Comment by Abdul Mannan on 2016-11-25:
Hi, I am facing the same problem. Could you tell me the header files you are including in the beginning. I am getting errors by simply including

#include "ros/ros.h"
#include "std_msgs/String.h"

Thank you so much.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.