Ask Your Question
1

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

2 Answers

Sort by ยป oldest newest most voted
4

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
0

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 3,341 times

Last updated: Sep 08