第一次调用publish()会产生以下消息:
[DEBUG] [1707520305.010403219]: Trying to publish message of type [std_msgs/String/992ce8a1687cec8c8bd883ec73ca41d1] on a publisher with type [std_msgs/String/992ce8a1687cec8c8bd883ec73ca41d1]
这是 ROS 中的已知错误吗?为了它的价值,我在 Ubuntu 20.04 上运行 ros noetic。
我已经验证通过在另一个终端中运行
rostopic echo test_topic
只发布一条消息。
#include <ros/ros.h>
#include <iostream>
#include "std_msgs/String.h"
int main(int argc, char *argv[])
{
ros::init(argc, argv, "test_pub_node");
ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug);
ros::NodeHandle nh;
auto pub = nh.advertise<std_msgs::String>("test_topic", 1);
auto msg = boost::make_shared<std_msgs::String>();
msg->data = "hello";
usleep(1.0e6);
pub.publish(msg);
ros::spinOnce();
usleep(1.0e6);
pub.publish(msg);
ros::spinOnce();
return 0;
}
此问题已在 ROSwiki 上针对旧版本的 ROS 进行了讨论,这不是一个错误。 简单地重写答案,因为 ROSwiki 现在是只读的。 发布者设置和发布数据之间的 1 秒睡眠时间通常不足以让订阅者(rostopic echo)开始侦听。 如果您想确定地收听所有消息,您必须等到看到订阅者(来自同一来源)。