ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Read on a topic and act when difference occurs?

asked 2015-05-01 10:45:55 -0500

215 gravatar image

updated 2015-05-02 04:05:12 -0500

I having bit of hard time solving this simple issue.. I have to create a listener which has to act when value changes. The topic i read on is sending messages of the type sensor_msg/Joinstate

when the velocity changes, i have to save the current position, and when the velocity changes again i have to write to a file what the position difference and keep on doing that until doesn't occur anymore.. I know before hand it's only going to happen 100 times..

How would you do that.. ??

Here is the code:

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2015-05-01 11:58:08 -0500

alexvs gravatar image

This is a lot of coding to be answered in one question. Try to be more specific with your question. For how to respond to changes in a topic, see the tutorial on writing a subscriber . If you know how to do that part, all you need to do is write the callback method for the topic that you are subscribing to.

Make a global variable that is the previous joint state. In the callback method use a simple for loop to check if the velocity vector in the current Jointstate (the one that is received by the callback method) has changed from the velocity vector in the previous Jointstate. It looks like the velocity is an array of float64. In C++ this will be interpreted as a std::vector of type double. So you can just traverse both vectors and see if the components have changed to some tolerance. If it has, then traverse the positions the same way and subtract them and save that off. At the end of your callback method, be sure to save the current Jointstate off as the previous Jointstate. If you know how many times its going to happen.. just add a global count variable that counts and run the program until the count is at 100.

edit flag offensive delete link more


well I exactly tried that solution, but ran into a error message saying i could save it into a global variable..

215 gravatar image 215  ( 2015-05-01 19:18:55 -0500 )edit

could you post some code and the error?

alexvs gravatar image alexvs  ( 2015-05-01 19:38:37 -0500 )edit

*couldn't save into a global variable

215 gravatar image 215  ( 2015-05-02 04:30:57 -0500 )edit

Code added

215 gravatar image 215  ( 2015-05-02 05:01:50 -0500 )edit

There are lots of C++ errors in your code. The ROS topic logic looks alright. First of all, you are missing a semicolon on line 15. Second, you are defining your variables after you are using them. Try moving them above the callback.

alexvs gravatar image alexvs  ( 2015-05-02 09:33:22 -0500 )edit

Another issue you will run into after it compiles is that you are only checking the first joint in the array (the zero index). You should make the velocity and position variables std::vectors of doubles instead of a single double and then check every joint to see if it moved using a loop.

alexvs gravatar image alexvs  ( 2015-05-02 09:34:20 -0500 )edit

Question Tools

1 follower


Asked: 2015-05-01 10:45:55 -0500

Seen: 113 times

Last updated: May 02 '15