Should ros::Rate() run after Creating NodeHandle()?

asked 2021-03-23 20:57:25 -0600

CroCo gravatar image

updated 2021-03-23 20:57:46 -0600

I'm trying to encapsulate my node inside a class so I can later on use mutli-threading but I've noticed this issue which is if ros::Rate is instantiated before instantiating NodeHandle() as follows:

ros::init(...)
m_rate = new ros::Rate(10);
m_nh = new ros::NodeHandle();

I get this error

terminate called after thrwoing an instance of 'ros::TimeNotInitializedException'

But if I do the vise versa, the code runs fine as shown below

ros::init(...)
m_nh = new ros::NodeHandle(); 
m_rate = new ros::Rate(10);

What is internally inside ros::Rate that may cause this problem?

edit retag flag offensive close merge delete

Comments

Duplicated question, see answer here.

danambrosio gravatar image danambrosio  ( 2021-03-23 21:12:37 -0600 )edit

Do you where in the documentation this stated?

CroCo gravatar image CroCo  ( 2021-03-23 21:18:05 -0600 )edit

The ros::Rate constructor initializes a private member variable start_ with Time::now(), see here. The Time::now() function requires that the time has been initialize (stored in the global g_initialized variable). If it has NOT been initialized TimeNotInitializedException gets thrown. If you want to use ros::Time or ros::Rate before a node handle is create you can call ros::Time::init() after initializing your node.

danambrosio gravatar image danambrosio  ( 2021-03-23 21:26:30 -0600 )edit

@danambrosio, thank you.

CroCo gravatar image CroCo  ( 2021-03-23 23:19:16 -0600 )edit