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

cv_bridge and timestamps

asked 2021-08-13 11:00:00 -0500

Kansai gravatar image

updated 2021-08-16 19:47:08 -0500

I am trying to add timestamps to a Image message

The Image message has a header member and this has a stamp member

Two-integer timestamp that is expressed as: * stamp.sec: seconds (stamp_secs) since epoch (in Python the variable is called 'secs') * stamp.nsec: nanoseconds since stamp_secs (in Python the variable is called 'nsecs') time-handling sugar is provided by the client library

however the data that I ve received was originally extracted from a rosbag like this

for topic,msg,t in bag.read_messages():
    timestamp= t.to_sec()

This gives a number like 1625151029.99

How can I take this number and put it in the timestamp of the Image message?

EDIT

for topic, msg, t in rosbag.read_messages():
    timestamp= t
    break

print("original")
print(timestamp)
print("recoreded")
ts= timestamp.to_sec()
print(ts)
print("back to timestamp")
print(rospy.Time.from_sec(ts))

#from here we record the timestamp
#Sorry unverified code
with open("afile.txt",'w') as f:
    f.write('{0}\n'.format(ts))
with open("afile.txt",'r') as f:
    new_ts=int(f.read())

print("recovered")
print(rospy.Time.from_sec(new_ts))

In this code we can see that the recovered value is different

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-08-15 08:09:43 -0500

Mike Scheutzow gravatar image

According to this wiki page, 't' is a rospy.Time object, so you should simply assign it directly:

msg.header.stamp = t

I don't know where your quoted text came from, but I think that's a unnecessarily confusing description of a Time object.

edit flag offensive delete link more

Comments

my problem is that we have already do this

timestamp=t.to_sec()

and record this to a file.

Now we are reading this value, so I guess if we convert this back to a rospy.Time object the nsec info is lost, right?

Kansai gravatar image Kansai  ( 2021-08-15 18:11:35 -0500 )edit

No precision will be lost. The rospy.Time object has nanosecond resolution. The from_sec() method takes the value you say you have.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-08-16 07:19:10 -0500 )edit

The to_sec takes the value in sec. Then when writing it to a file it has 2 decimals precision. Then I read it again and use from_sec but the value I get is different than the original. I edited the question with some code

Kansai gravatar image Kansai  ( 2021-08-16 19:40:07 -0500 )edit

Your ts variable is a floating point value. I suggest you google for the proper way to format a float in python.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-08-16 20:10:36 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-08-13 11:00:00 -0500

Seen: 710 times

Last updated: Aug 16 '21