Read on a topic and act when difference occurs?
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: http://pastebin.com/RxyfZ0s0
Asked by 215 on 2015-05-01 10:45:55 UTC
Answers
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.
Asked by alexvs on 2015-05-01 11:58:08 UTC
Comments
well I exactly tried that solution, but ran into a error message saying i could save it into a global variable..
Asked by 215 on 2015-05-01 19:18:55 UTC
could you post some code and the error?
Asked by alexvs on 2015-05-01 19:38:37 UTC
*couldn't save into a global variable
Asked by 215 on 2015-05-02 04:30:57 UTC
Code added
Asked by 215 on 2015-05-02 05:01:50 UTC
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.
Asked by alexvs on 2015-05-02 09:33:22 UTC
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.
Asked by alexvs on 2015-05-02 09:34:20 UTC
Comments