Publish message received in callback
Hello,
I am trying to do a simple node subscribing to a topic which send data of type TwoInts (a custom message), and publishing the sum of what it received.
TwoInts.msg
# TwoInts.msg
int16 a
int16 b
sum.cpp
#include "ros/ros.h"
#include <beginner_tutorials/TwoInts.h>
#include "std_msgs/Int16.h"
ros::Publisher pub;
void callback(const beginner_tutorials::TwoInts::ConstPtr& msg)
{
ROS_INFO("I heard: [%d, %d]", msg->a, msg->b);
int sum = msg->a + msg->b;
// OR ?
//std_msgs::Int16 sum = msg->a + msg->b;
//pub.publish(sum);
pub.publish(msg->a);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "sum");
ros::NodeHandle n;
pub = n.advertise<std_msgs::Int16>("sum", 1000);
ros::Subscriber sub = n.subscribe("two_ints", 1000, callback);
ros::spin();
return 0;
}
My problem is to publish the message msg in the callback. The attributes a and b are of type int16, but when I try to publish them I got some errors like this one:
error: request for member ‘__getMD5Sum’ in ‘m’, which is of non-class type ‘const short int’
return m.__getMD5Sum().c_str();
I tried many things but I do not see how to solve that...
Thanks for your help.
Mickael