How to use data from a callback function in another function

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

dj95 gravatar image

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. 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);
edit retag flag offensive close merge delete