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

Subscriber and a Publisher in the same node!

asked 2022-04-23 19:07:31 -0500

faresPE16 gravatar image

updated 2022-04-30 13:17:05 -0500

lucasw gravatar image

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();
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2022-04-24 08:49:22 -0500

Mike Scheutzow gravatar image

updated 2022-04-24 08:51:13 -0500

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().

edit flag offensive delete link more

Question Tools

Stats

Asked: 2022-04-23 19:07:31 -0500

Seen: 89 times

Last updated: Apr 24 '22