ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

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.

asked 2015-05-31 09:03:35 -0600

justiliang gravatar image

I tried googling for an answer but it seems like the answers are targeted for rospy and not roscpp. Also, I am using ros indigo.

The code I am trying to run is pretty simple:

#include "generate_global_map.hpp"
#include <iostream>

int main(int argc, char **argv) {

  ros::init(argc, argv, "test_generate_global_map");

  std::cout << "hello world" << std::endl;

  ros::Rate loop_rate(5); //10hz loop rate

  while (ros::ok()){

  ros::spinOnce(); //ros spin is to ensure that ros threading does not leave subscribes un processed
  loop_rate.sleep();

  }

  return 0;

}

When I try to rosrun this I get this error:

hello world
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)

Anyone know what the issue is?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
5

answered 2015-05-31 10:25:05 -0600

gvdhoorn gravatar image

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()

I hate it when I get these kind of answers, but in this case I think the exception is actually pretty verbose and points you to your issue: you need to create a ros::NodeHandle instance before using any of ros::Rate, ros::ok() or ros::spinOnce(), or manually call ros::start() (but I doubt that is a recommended use of that API).

If you do not want a NodeHandle, you could (as the message tells you) also directly call ros::Time::init().

See also the Writing a Simple Publisher and Subscriber (C++) tutorial.

edit flag offensive delete link more
1

answered 2019-09-08 11:42:57 -0600

marcomasa gravatar image

Additional info for this issue when using Cpp since this is the top result on google:

When you are facing this issue using classes with a ros::NodeHandle and ros::Rate, make sure the order is:

class A
{
ros::NodeHandle nh;
ros::Rate r;
.
.
.
}

Otherwise the ros::Rate object will be created first which ends up with the same exception thrown.

edit flag offensive delete link more
0

answered 2020-11-23 00:20:18 -0600

Rufus gravatar image

updated 2020-11-23 00:22:21 -0600

A more sinister form of this mistake is if you declare a global or static ros::Time / ros::Rate variable. Since global and static variables are created before main(), the advise from the error message will not apply, since wherever you put your ros::NodeHandle or ros::Time::init(), the global variable will still be constructed before it.

In fact, I believe there is no way to initialize a global ros::Time or ros::Rate variable.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-05-31 09:03:35 -0600

Seen: 7,079 times

Last updated: Nov 23 '20