Use variables in main from callback
I need to use three variables that I subscribe from another topic and use it in the main loop. I know that I can use boost function but I don't understand how to use in my case:
double theta1f;
double theta2f;
double theta3f;
class Listener
{
public:
void callback(const std_msgs::Float32MultiArray::ConstPtr& msg);
};
void Listener::callback(const std_msgs::Float32MultiArray::ConstPtr& msg)
{
theta1f = msg->data[0];
theta2f = msg->data[1];
theta3f = msg->data[2];
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "node_joint");
ros::NodeHandle nh;
ros::Rate loop_rate(30);
Listener listener;
ros::Subscriber sub = nh.subscribe<std_msgs::float32multiarray>("topic_subscribed", 1, &Listener::callback, &listener);
while (ros::ok())
{
cout theta1f endl;
}
loop_rate.sleep();
ros::spin();
return 0;
}
I need to use theta1f in this main (cout is only an example and i know that is missing the <<). How can I do it using boost function? There are other solutions better than this?
Thank you for your answers.