ROS time. Trying to recover time when recorded to a file

3.1k Views Asked by At

I am trying to get the timestamp for a particular ROS message, record it in a file, read the file and recover the original timestamp. So far unsuccessfully .

Here is my code

import os
import os.path
import rosbag
from sensor_msgs.msg import Image

import rospy

IMG_DIRNAME = '/the/root/to/bags/'

#we get only one published topic time and quit
def main():
    rosbag_file = 'thebag.bag'
    rosbag_file= os.path.join(IMG_DIRNAME,rosbag_file)
    for topic, msg, t in  rosbag.Bag(rosbag_file).read_messages():
        if topic == '/the/topic/image':
            print(".")
            return t


if __name__ == '__main__':
    try:
        time_stamp= main()    #I got the time stamp
        print("original ",time_stamp)
        print(time_stamp)
        print(time_stamp.secs)
        print(time_stamp.nsecs)
        print("recorded ",time_stamp.to_sec())
        print(time_stamp.to_sec())
        print("just to show ",time_stamp.to_nsec())

        ts=time_stamp.to_sec()

        #Here we record it in a file
        with open("afile.txt",'w') as f:
            f.write('{0}\n'.format(ts))
        
        with open("afile.txt",'r') as f:
            new_ts=float(f.read())
        

        time_new= rospy.Time.from_sec(new_ts)
        print("recovered ", time_new)
        print(time_new)
        print(time_new.secs)
        print(time_new.nsecs)

        print("==================================")
        print("other record",time_stamp.to_nsec())
        print(time_stamp.to_nsec())
        tns=time_stamp.to_nsec()
        #Here we record it in a file
        with open("afile2.txt",'w') as f:
            f.write('{0}\n'.format(tns))
        
        with open("afile2.txt",'r') as f:
            ff=f.read().strip("\n")
            print("read as")
            print(ff)
            new_tns=float(ff)

        print(new_tns)
        time_new_ns= rospy.Time(new_tns)
        print("recovered",time_new_ns)
        print(time_new_ns)
        print(time_new_ns.secs)  
        print(time_new_ns.nsecs)      

As a result I got

.
('original ', rospy.Time[1625151029987577614])
1625151029987577614
1625151029
987577614
('recorded ', 1625151029.9875777)
1625151029.99
('just to show ', 1625151029987577614)
('recovered ', rospy.Time[1625151029990000009])
1625151029990000009
1625151029
990000009
==================================
('other record', 1625151029987577614)
1625151029987577614
read as
1625151029987577614
1.62515102999e+18
('recovered', rospy.Time[1625151029987577600000000000])
1625151029987577600000000000
1625151029987577600
0

So you can see that first I got the timestamp (original) and recorded the conversion to seconds (1625151029.99). I recorded it to a file (which has that number recorded) and the recovered timestamp is not the same as the original (the ns part went from 987577614 to 990000009). I guess this was due to putting the to_sec conversion

In order to correct that I do the second part (after the ===). I record the number of nsecs (1625151029987577614) which I read, first as a string correctly, then as a float (shown in scientific notation) But in the last part ("recovered") I don't know how to convert this number correctly to ROS time. The value I got is totally wrong

How can I get the original timestamp here?

EDIT: I tried

print("recalculated")
tt=rospy.Time( int(new_tns //1000000000), int(new_tns%1000000000))
print("recovered",tt)
print(tt)
print(tt.secs)  
print(tt.nsecs) 

but although it got closer to the original , it still does not get it

recalculated
('recovered', rospy.Time[1625151029987577600])
1625151029987577600
1625151029
987577600

plust, couldn't be a simpler and proper way?

2

There are 2 best solutions below

0
On

The problem your having is actually coming from using time_new.nsecs incorrectly. Your expectation is that nsecs will show the total time since epoch in nano seconds. However, this field actually shows nano seconds since seconds (since epoch). So, you're just extracting the nano seconds after what is shown by time_new.secs. If you want to do a full conversation always use to_secs() and to_nsecs().

0
On

Use the following formulae to covert the time_stamp.secs and time_stamp.nsecs to time_stamp_since_epoch

time_stamp_since_epoch = time_stamp.secs + time_stamp.nsecs*1e-9