报错输出
terminate called after throwing an instance of 'ros::TimeNotInitializedException'
what(): Cannot use ros::Time::now() before the first NodeHandle has been created or ros::start() has been called. If this is a standalone app or test that just uses ros::Time and does not communicate over ROS, you may also call ros::Time::init()
Aborted (core dumped)
原因及解决方法
参考链接:ROS 社区
除了在 ros::Time::now() 之前需要创建 ROS 节点,在 ros::Rate , ros::ok() 和 ros::spinOnce() 之前都需要先创建 ROS 节点。 由于我是使用 C++ 类来实现消息的订阅和发布,所以需要使用初始化列表,在调用 ros::Rate 的构造函数之前,调用 ros::NodeHandle 的构造函数。
#include <ros/ros.h>
class Uav
{
public:
Uav();
ros::NodeHandle nh;
ros::Rate rate;
};
Uav::Uav():nh(), rate(20.0)
{
}
|