Global Strings and Variables with SUbscriber callback

asked 2018-12-14 04:21:35 -0500

Hello All,

I have a node that has a subscriber declared as:

ros::Subscriber sub1= n.subscribe<std_msgs::Float32MultiArray> ("/TOPIC", 1,callback);

and a global string which is declared as:

std::string StringForCallBack;

Inside the call back I have some process that at the end will edit the content of the string "StringForCallBack" example:

void callback(const std_msgs::Float32MultiArray::ConstPtr& variable /*received on the topic)
{
 //Processing
StringForCallBack="Random";
}

The problem is that when I need to use the global string in the main function it uses the initial value of the string and not the edited value from the call back function even though it is a global string

Note: If I use the global string in another call back function is uses the value of the string which was edited in the first call back.

Is there any other way to use variables and strings from the call back function in the main function ?

Your help is highly appreciated

edit retag flag offensive close merge delete