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

Get timestamp of current message starting from zero in rospy

asked 2020-12-18 06:50:04 -0600

jo-jstrm gravatar image

updated 2021-02-05 03:07:54 -0600

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()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-12-19 08:26:12 -0600

tryan gravatar image

Simulation time can start any time. The use_sim_time parameter tells the ROS nodes to expect time to come from the /clock topic rather than the system as explained in the Clock wiki. If you're running a simulation, the simulator may start the clock at 0, but it could just as easily start at some other time. With rosbag, the --clock option publishes the time from the messages in the bag file. You can either manipulate those timestamps in the bag file (offline; the rosbag API may help), or you can deal with it in a node (online). Either way, you'll have to save the stamp from the first message and subtract it from later messages, like you said.

edit flag offensive delete link more

Comments

1

Thank you very much, that clarified the relationship between rosbag play --clock and use_sim_time.

jo-jstrm gravatar image jo-jstrm  ( 2020-12-29 07:37:19 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-12-18 06:50:04 -0600

Seen: 1,410 times

Last updated: Feb 05 '21