Get timestamp of current message starting from zero in rospy
I seem to have a misunderstanding regarding time in ros, especially sim-time.
Aim
I process a rosbag that contains many pointcloud2s and want to transform the timestamps of the incoming pointcloud to the KITTI format, which starts at zero. This means that I need the time of the current pointcloud relative to the first processed pointcloud.
Problem
I cannot get the callback to start the time at zero, and count from there.
Setup
I have a node that listens for the pointclouds and invokes the callback on each arriving message. I use sim_time="true"
and rosbag play ... --clock
. For the rest, see the code below.
What I tried
I thought that rospy.Time.now()
with sim_time="true"
and rosbag play ... --clock
would start at zero in the first message, but it does not.
The naive approach would be to store the timestamp of the first incoming message and subtract it from every following message. But I thought that there must be a more convenient way in ros.
Code sample
class my_class():
def __init__(self):
file_times = open(os.path.join(os.getcwd(), "times.txt"), "w")
file_times.close
rospy.init_node(name="my_node", anonymous=False)
rospy.Subscriber(name="bag/all_pointclouds", data_class=PointCloud2,
callback=self.my_callback)
def my_callback(self, cloud):
### Write times.txt
times_now = open(os.path.join(self.output_dir, "times_now.txt"), "a")
times_now.write( rospy.Time.now() )
times_now.write(" \n")
if __name__ == "__main__":
listener = my_class()
rospy.spin()