Convert Header Timestamp to nanosecond Python ROS2 Foxy

asked 2022-09-30 07:47:28 -0600

vanguard478 gravatar image

updated 2022-09-30 07:50:46 -0600

Hi, I have been trying to convert the message timestamp to nanoseconds (epoch time). I checked the ROS2 Foxy C++ API and it has a member function nanoseconds listed for the Time class. Also I checked the code for time.py on the foxy branch of ros2/rclpy and it also has a

@property
def nanoseconds(self):
    return _rclpy.rclpy_time_point_get_nanoseconds(self._time_handle)

But I am getting this error AttributeError: 'Time' object has no attribute 'nanoseconds'

I am currently doing as below and not sure if it is the right method to do it:

cur_time_stamp = int(str(msg.header.stamp.sec) + str(msg.header.stamp.nanosec))

Can someone please help me on how to convert a message timestamp to nanoseconds?

edit retag flag offensive close merge delete

Comments

ravijoshi gravatar image ravijoshi  ( 2022-09-30 08:07:53 -0600 )edit

So I should create a Time Object using something like this(assuming an msg incoming_msg):

from rclpy.time import Time
time2 = Time.from_msg(incoming_msg.header.stamp)
time_in_nanosecs=time2.nanoseconds

So the timestamps ROS messages are builtin_interfaces.msg.Time and are different from the Time object in rclpy.time? Is this difference in the Class type also valid for current ROS2 releases like Galactic or Humble ?

vanguard478 gravatar image vanguard478  ( 2022-09-30 10:52:57 -0600 )edit