Fares DJELILI
Fares DJELILI

Reputation: 3

Publisher/Subscriber on the same node C++ ROS

I aim to create a Subscriber and a Publisher in the same node!

I want to access a part of the message available on a topic of a rosbag.

The message of the thread is as follows:

Type: radar_msgs/RadarDetectionArray

std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
radar_msgs/RadarDetection[] detections
  uint16 detection_id
  geometry_msgs/Point position
    float64 x
    float64 y
    float64 z
  geometry_msgs/Vector3 velocity
    float64 x
    float64 y
    float64 z
  float64 amplitude

I am looking to access position "x" only, and publish it afterward.

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "radar_msgs/RadarDetection.h"
#include "radar_msgs/RadarDetectionArray.h"
#include "geometry_msgs/Point.h"

//radar_msgs::RadarDetectionArray pub_data;

double pub_data;

void srr2Callback(const radar_msgs::RadarDetection msg)
{   
    pub_data = msg.position.x;
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "talker");
    ros::NodeHandle n;

    ros::Publisher chatter_pub = n.advertise<radar_msgs::RadarDetection>("srr2_detections", 1000);
    ros::Subscriber sub = n.subscribe("/rear_srr/rear_right_srr/as_tx/detections", 1000, srr2Callback);
    ros::Rate loop_rate(10);
    
    int count = 0;
    while (ros::ok())
    {
            chatter_pub.publish(pub_data);
            ros::spinOnce();
            loop_rate.sleep();
            ++count;
    }
    return 0;
}

I also share the error I got!

error: request for member ‘__getMD5Sum’ in ‘m’, which is of non-class type ‘const double’
     return m.__getMD5Sum().c_str();

error: request for member ‘__getDataType’ in ‘m’, which is of non-class type ‘const double’
     return m.__getDataType().c_str();

Upvotes: 0

Views: 2874

Answers (1)

ichramm
ichramm

Reputation: 6642

Here:

chatter_pub.publish(pub_data);

You are publishing a double in a topic that expect radar_msgs::RadarDetection.

The error is telling you that it cannot call __getMD5Sum on a double, which is obviously accurate.

If you intend to publish a double you MUST create a publisher specific for that type:

ros::Publisher chatter_pub = n.advertise<std_msgs::Float64>("srr2_detections", 1000);

But even with this change you should not publish the double directly, you must do something like this:

std_msgs::Float64 msg;
msg.data = pub_data;
chatter_pub.publish(msg);

You can also do that with radar_msgs::RadarDetection if you do not want to change the publisher's type:

radar_msgs::RadarDetection msg;
msg.x = pub_data;
chatter_pub.publish(msg);

Upvotes: 2

Related Questions