Revision history [back]

How to use data from a callback function in another function

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);
move();
ros::spin();
}


How to use data from a callback function in another function

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);
move();
ros::spin();
}


How to use data from a callback function in another function

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. 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();
}