Publisher/Subscriber 在同一个节点上 C++ ROS
Publisher/Subscriber on the same node C++ ROS
我的目标是在同一个节点中创建一个订阅者和一个发布者!
我想访问有关 rosbag
主题的部分消息。
跟帖留言如下:
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
我只想访问位置“x”,然后再发布。
#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;
}
我也分享我遇到的错误!
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();
此处:
chatter_pub.publish(pub_data);
您正在 double
的主题中发布 radar_msgs::RadarDetection
。
错误告诉你它不能对双精度调用 __getMD5Sum
,这显然是准确的。
如果您打算发布替身,您必须创建一个特定于该类型的发布者:
ros::Publisher chatter_pub = n.advertise<std_msgs::Float64>("srr2_detections", 1000);
但是发生这种变化的事件你不应该直接发布替身,你应该这样做:
std_msgs::Float64 msg;
msg.data = pub_data;
chatter_pub.publish(msg);
如果您不想更改发布者的类型,您也可以使用 radar_msgs::RadarDetection
执行此操作:
radar_msgs::RadarDetection msg;
msg.x = pub_data;
chatter_pub.publish(msg);
我的目标是在同一个节点中创建一个订阅者和一个发布者!
我想访问有关 rosbag
主题的部分消息。
跟帖留言如下:
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
我只想访问位置“x”,然后再发布。
#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;
}
我也分享我遇到的错误!
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();
此处:
chatter_pub.publish(pub_data);
您正在 double
的主题中发布 radar_msgs::RadarDetection
。
错误告诉你它不能对双精度调用 __getMD5Sum
,这显然是准确的。
如果您打算发布替身,您必须创建一个特定于该类型的发布者:
ros::Publisher chatter_pub = n.advertise<std_msgs::Float64>("srr2_detections", 1000);
但是发生这种变化的事件你不应该直接发布替身,你应该这样做:
std_msgs::Float64 msg;
msg.data = pub_data;
chatter_pub.publish(msg);
如果您不想更改发布者的类型,您也可以使用 radar_msgs::RadarDetection
执行此操作:
radar_msgs::RadarDetection msg;
msg.x = pub_data;
chatter_pub.publish(msg);