ros-drivers/usb_cam

Strange Timestamp

Woojin-Crive opened this issue · 1 comments

Screenshot from 2023-08-19 19-01-39

using a simple python script that subscribes '/usb_cam/image_raw' topic, i visualized ros current time and the header/stamp.

Below is the code.

#!/usr/bin/env python3

import rospy
from sensor_msgs.msg import Image
import matplotlib.pyplot as plt
import matplotlib.animation as animation

class CameraTimestampPlotter:
    def __init__(self):
        # Create an empty list to store timestamps
        self.timestamps = []
        self.timestamps_now = []
        self.fig, self.ax = plt.subplots()
        
        # Set up the plot
        self.ax.set_title("Camera Timestamps")
        self.ax.set_xlabel("Message Index")
        self.ax.set_ylabel("Timestamp (seconds)")
        
        # Subscribe to the camera topic
        rospy.init_node('camera_timestamp_plotter', anonymous=True)
        rospy.Subscriber("/usb_cam/image_raw", Image, self.callback)
        
        # Set up animation
        self.ani = animation.FuncAnimation(self.fig, self.update_plot, interval=2000)
        plt.show()

    def callback(self, data):
        # Extract the timestamp (time in seconds) from the header and append to list
        time_stamp = data.header.stamp.to_sec()
        self.timestamps.append(time_stamp)
        self.timestamps_now.append(rospy.Time.now().to_sec())
        rospy.loginfo(f"now: {rospy.Time.now().to_sec()}, Received timestamp: {time_stamp}, diff: {rospy.Time.now().to_sec() - time_stamp}")

    def update_plot(self, i):
        # Clear the previous plot
        self.ax.clear()
        
        # Replot the data
        self.ax.plot(self.timestamps, '-o')
        self.ax.plot(self.timestamps_now, '-o')
        self.ax.set_title("Camera Timestamps")
        self.ax.set_xlabel("Message Index")
        self.ax.set_ylabel("Timestamp (seconds)")

if __name__ == '__main__':
    try:
        CameraTimestampPlotter()
    except rospy.ROSInterruptException:
        pass

the image's timestamp is strange..

how can i fix this problem?

buffer_time_s = buf.timestamp.tv_sec + static_cast<int64_t>(round(buf.timestamp.tv_usec / 1000000.0));

sec 11981, nsec 26378 || stamp 1692444560.26378000
sec 11981, nsec 58404 || stamp 1692444560.58404000
sec 11981, nsec 90406 || stamp 1692444560.90406000
sec 11981, nsec 126368 || stamp 1692444560.126368000
sec 11981, nsec 158402 || stamp 1692444560.158402000
sec 11981, nsec 190400 || stamp 1692444560.190400000
sec 11981, nsec 226403 || stamp 1692444560.226403000
sec 11981, nsec 258402 || stamp 1692444560.258402000
sec 11981, nsec 290395 || stamp 1692444560.290395000
sec 11981, nsec 326383 || stamp 1692444560.326383000
sec 11981, nsec 358368 || stamp 1692444560.358368000
sec 11981, nsec 390362 || stamp 1692444560.390362000
sec 11981, nsec 426367 || stamp 1692444560.426367000
sec 11981, nsec 458399 || stamp 1692444560.458399000
sec 11981, nsec 490401 || stamp 1692444560.490401000
sec 11981, nsec 526393 || stamp 1692444561.526393000
sec 11981, nsec 558394 || stamp 1692444561.558394000
sec 11981, nsec 590404 || stamp 1692444561.590404000
sec 11981, nsec 626392 || stamp 1692444561.626392000
sec 11981, nsec 658394 || stamp 1692444561.658394000
sec 11981, nsec 690393 || stamp 1692444561.690393000
sec 11981, nsec 726388 || stamp 1692444561.726388000
sec 11981, nsec 758392 || stamp 1692444561.758392000
sec 11981, nsec 790380 || stamp 1692444561.790380000
sec 11981, nsec 826391 || stamp 1692444561.826391000
sec 11981, nsec 858393 || stamp 1692444561.858393000
sec 11981, nsec 890395 || stamp 1692444561.890395000
sec 11981, nsec 926388 || stamp 1692444561.926388000
sec 11981, nsec 958386 || stamp 1692444561.958386000
sec 11981, nsec 990383 || stamp 1692444561.990383000

stamp's sec is calculated using rounded usec(as a second) value, but it's putting buf's usec to the stamp's usec.
Is this the problem?

resolvd with #272!