Robotics StackExchange | Archived questions

Subscriber and a Publisher in the same node!

Hello guys ! I want to create a subscriber and publisher on the same node to access a part of the message available on rosbag topic ! 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. Here's my code :

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


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<std_msgs::Float64>("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;
}

And here's 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();

Asked by faresPE16 on 2022-04-23 19:07:31 UTC

Comments

Answers

The publisher declaration and publish method must use the same message type.

A std_msgs::Float64 and a double are different c++ types. Your publishing code must create a `std_msgs::Float64 object, set the value, then pass that msg to publish().

Asked by Mike Scheutzow on 2022-04-24 08:49:22 UTC

Comments