Ask Your Question
0

How to use data from a callback function in another function

asked 2020-02-12 10:02:16 -0500

dj95 gravatar image

updated 2020-02-17 15:46:13 -0500

I'm trying to take the x, y, and yaw value from the callback function and use them in a different function. Although when I run the code I have below, all the variables below return a value of 0. From my thought process, I think the global variables should be accessible for the void move() function as well, which doesn't seem to be the case.

Can someone tell what's wrong?

#include "ros/ros.h"
#include "turtlesim/Pose.h"
#include "geometry_msgs/Twist.h"

float x = 0, y = 0, yaw = 0 ;

void callback(const turtlesim::Pose::ConstPtr& msg)
{
    x = msg->x;
    y = msg->y;
    yaw = msg->theta;

}

void move()
{
    while (ros::ok())
    {
        ROS_INFO("x: [%f], y: [%f], yaw: [%f]", x, y, yaw);
    }
}

    int main(int argc, char **argv)
    {
        ros::init(argc, argv, "pose");
        ros::NodeHandle n;
        ros::Subscriber sub = n.subscribe("/turtle1/pose", 1000, callback);
        move();
        ros::spin();
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-02-17 15:58:55 -0500

So going line by line here to help show you the flow of the issue:

ros::init(argc, argv, "pose");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/turtle1/pose", 1000, callback);

All good, initializing ROS, getting a nodehandle, creating a subscriber object and giving it the callback function to call. The spinner would then throw new messages received on that topic of that type to the function.

move() {while (ros::ok()) {...}}

Well, this is going to block infinitely. And while you're going to exit when ROS shuts down, no where in this infinite loop do you actually call a spin or spinOnce for the ROS messages building up to be processed (with a 1000 message queue size).

ros::spin();

Then you spin. This spin will only be called once your move() method exits (ie you end the program) so you're only just then starting to process messages.

What you could do instead is call ros::spinOnce() in your move() function and remove the ros::spin() from the end of your main function. That way you're processing messages in the callback and printing some values to screen.

edit flag offensive delete link more

Comments

Would it be okay if I call ros::spin() before calling the move()? Would that produce the same result?

dj95 gravatar image dj95  ( 2020-02-17 17:02:21 -0500 )edit

No. Spin is blocking and will not return until the session is ended. Think of spin() being a while loop with a spin and a sleep (its more complex than that, but essentially).

stevemacenski gravatar image stevemacenski  ( 2020-02-17 21:04:58 -0500 )edit

Thank you for the explanation.

dj95 gravatar image dj95  ( 2020-02-18 08:13:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2020-02-12 10:02:16 -0500

Seen: 416 times

Last updated: Feb 17 '20