Transform [sender=unknown_publisher] For frame []: Frame [] does not exist
注意,[]不是用来SEO的通配符,中括号内实际就是空的。
主要问题出在topic的header上,问题原因乍一想没想到,因为我实际代码更复杂一点,是大消息套小数据,小数据有header,而大消息没header,代码上下文就不赘述了。这里放开注释和不放开注释是两种结果,rviz能显示和不能显示的区别。
C++嘛,都有默认值的,主动写header赋值默认值和不赋值有区别吗?可能C++没区别,rostopic echo也没区别!!!!!
但是RVIZ有区别!!!!
放开注释,消息就能在rviz显示,即便不给topic的header写frame_id。
仔细分析一下报错信息,不知道publisher是谁!!!后边的frame[]是空!
其实就很明显了,是header有问题!把header主动赋进去就好了。
提供一个简化的测试代码,通过调整注释观测rviz显示结果复现同样的报错。
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
int main(int argc, char** argv) {
ros::init(argc, argv, "pointcloud_publisher");
ros::NodeHandle nh;
// Create a point cloud
pcl::PointCloud<pcl::PointXYZ> cloud;
cloud.width = 100;
cloud.height = 1;
cloud.points.resize(cloud.width * cloud.height);
// Fill the point cloud with random points in a small area
for (size_t i = 0; i < cloud.points.size(); ++i) {
cloud.points[i].x = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
cloud.points[i].y = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
cloud.points[i].z = 0.0;
}
// Convert the point cloud to ROS message
sensor_msgs::PointCloud2 output;
pcl::toROSMsg(cloud, output);
// Publish the point cloud
ros::Publisher pub = nh.advertise<sensor_msgs::PointCloud2>("point_cloud", 1);
ros::Rate loop_rate(1);
while (ros::ok()) {
// output.header.stamp = ros::Time::now();
// output.header.frame_id = "test_base_link_frame";
pub.publish(output);
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}