ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
The solution to your problem is as follows:
#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);
std_msgs::Int16 sum;
sum.data = msg->a + msg->b;
pub.publish(sum);
}
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;
}
I believe you are confusing Int16 message with the int16 type. The Int16 message looks like this:
int16 data
The Int16 message contains a field called data that is an int16 type.
So, msg->a and msg->b get the int16 type values from the Twoints message. Now, you want to assign the result of sum of the type int16 values back into a Int16 message to publish it. This requires you to assign it to the data field.