Using a function in callback ?
I am trying to understand the ROS concepts and the next step would be to try and integrate ROS with an own tool.
For this, I am trying to write a simple function addition
and analyze the effects.
I have a talker which increments the value of a
. Now I have a listener, which listens to this incremented value of a
, everytime callback
function is called and then use a function, lets say a + b
, where b
would be constant lets say 5
. So everytime this a+b
is called in the listener, it should publish it on the console by saying Updated Sum : a (Incremented in every step) + 5 = x (also updated accordingly)
. The console could be published either in the listener or in the talker.
- At first I thought of calling the
additon
function in themain()
of the listener but this doesnt work as ithe variablemsg (getting updated through callback is not in the scope of main
. Then I thought of directly calling
addition(a,b)
in the callback but somehow i cannot update the function asmsg
is a constant pointer and i cannot change its value. Here is the code for the listener :void chatterCallback(const std_msgs::Float32::ConstPtr& msg) { msg->data = msg->data + 5 ; ROS_INFO("Updated Sum: [%f]",msg->data) ; } int main(int argc, char **argv) { ros::init(argc, argv, "listener"); ros::NodeHandle n; ros::Subscriber sub = n.subscribe("sum", 1000, chatterCallback); // ROS_INFO("I heard: [%f]",msg.data) ; // msg.data = addition(msg.data, 5); ros::spin(); return 0; }
Is there a better way to do this ?