ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Tony78's profile - activity

2023-06-08 12:54:59 -0500 received badge  Famous Question (source)
2022-03-05 17:51:34 -0500 received badge  Notable Question (source)
2022-03-05 17:51:34 -0500 received badge  Popular Question (source)
2021-09-01 16:49:54 -0500 asked a question Open/Close Tiago gripper

Open/Close Tiago gripper Hello everyone, I wanted to know if there is a way to open and close the gripper from Tiago in

2020-11-07 02:27:41 -0500 received badge  Student (source)
2020-10-28 12:40:29 -0500 received badge  Famous Question (source)
2020-01-30 10:56:30 -0500 received badge  Notable Question (source)
2020-01-28 08:51:01 -0500 marked best answer 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

2020-01-28 08:51:01 -0500 received badge  Scholar (source)
2020-01-28 08:50:58 -0500 received badge  Supporter (source)
2020-01-11 13:28:18 -0500 received badge  Popular Question (source)
2020-01-06 01:52:35 -0500 asked a question Publish message received in callback

Publish message received in callback Hello, I am trying to do a simple node subscribing to a topic which send data of