Import variables from a message
Hello,
In my code I would like to get a variable from my message and use this in my main.
Whenever I run this code "x" is said to be 0.00. This should be a different number. In my message I declare my variables as float32.
I feel like I don't use x = msg.x
the proper way. I know that my message is being send the right way. Because in a different code I was able to ROS_INFO_STREAM(x)
. This showed that it receives the right x value.
Could someone please look at my code and hopefully see where I make a mistake?
#include <iostream>
#include <ros/ros.h>
#include <moveit/move_group_interface/move_group.h>
#include <sstream>
#include <ur5_inf3480/Coor.h>
using namespace std;
class MoveRobot {
public:
double x;
double y;
double z;
double roll;
double pitch;
double yaw;
double a;
void PublishCoordinatesCallback(const ur5_inf3480::Coor msg);
};
void MoveRobot::PublishCoordinatesCallback(const ur5_inf3480::Coor msg) {
x = msg.x;
y = msg.y;
z = msg.z;
roll = msg.roll;
pitch = msg.pitch;
yaw = msg.yaw;
a = msg.a;
}
int main(int argc, char **argv) {
ros::init(argc, argv, "coordinates");
ros::NodeHandle nh;
ros::Rate loop_rate(30);
MoveRobot moverobot;
ros::Subscriber sub = nh.subscribe<ur5_inf3480::Coor>("topic_subscribed", 1, &MoveRobot::PublishCoordinatesCallback, &moverobot);
while (ros::ok())
{
ROS_INFO("This is x: %.2f", moverobot.x);
}
loop_rate.sleep();
ros::spin();
return 0;
}
I hope I won't ask a question which has been asked already. I tried to search the forum and the internet first but I can't find a solution. Thank you for your answers!