Skip to content

Commit e05efb6

Browse files
committed
update save timestamp scripts
1 parent 90b1dcd commit e05efb6

File tree

2 files changed

+112
-14
lines changed

2 files changed

+112
-14
lines changed

examples/plot_imu_and_image_ts.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import pandas as pd
2+
import matplotlib.pyplot as plt
3+
4+
def plot_data(gyro_csv, accel_csv, rgb_csv, depth_csv, sensor_type):
5+
# Load the CSV data into DataFrames
6+
gyro_data = pd.read_csv(gyro_csv)
7+
accel_data = pd.read_csv(accel_csv)
8+
rgb_data = pd.read_csv(rgb_csv)
9+
depth_data = pd.read_csv(depth_csv)
10+
11+
# Set up the plot with six subplots: timestamps, timestamp differences, global timestamps for all sensors
12+
fig, ax = plt.subplots(5, 1, figsize=(12, 24), sharex=True)
13+
14+
# Plot timestamps for gyro and accelerometer
15+
ax[0].plot(gyro_data['Index'], gyro_data['Timestamp'], label='Gyro Timestamp', color='blue')
16+
ax[0].plot(accel_data['Index'], accel_data['Timestamp'], label='Accelerometer Timestamp', color='orange')
17+
ax[0].set_title('IMU Timestamps')
18+
ax[0].set_ylabel('Timestamp (us)')
19+
ax[0].legend()
20+
21+
# Plot timestamps for RGB and depth sensors
22+
ax[1].plot(rgb_data['Index'], rgb_data['Timestamp'], label='RGB Timestamp', color='red')
23+
ax[1].plot(depth_data['Index'], depth_data['Timestamp'], label='Depth Timestamp', color='green')
24+
ax[1].set_title('Camera Timestamps')
25+
ax[1].set_ylabel('Timestamp (us)')
26+
ax[1].legend()
27+
28+
# Plot timestamp differences for all sensors if the column exists
29+
if 'Timestamp Difference' in gyro_data.columns:
30+
ax[2].plot(gyro_data['Index'], gyro_data['Timestamp Difference'], label='Gyro Timestamp Difference', color='purple')
31+
ax[2].plot(accel_data['Index'], accel_data['Timestamp Difference'], label='Accelerometer Timestamp Difference', color='magenta')
32+
ax[2].plot(rgb_data['Index'], rgb_data['Timestamp Difference'], label='RGB Timestamp Difference', color='black')
33+
ax[2].plot(depth_data['Index'], depth_data['Timestamp Difference'], label='Depth Timestamp Difference', color='grey')
34+
ax[2].set_title('Timestamp Differences')
35+
ax[2].set_ylabel('Timestamp Difference (us)')
36+
ax[2].legend()
37+
38+
# Plot global timestamps for all sensors if the column exists
39+
if 'Global Timestamp' in gyro_data.columns:
40+
ax[3].plot(gyro_data['Index'], gyro_data['Global Timestamp'], label='Gyro Global Timestamp', color='cyan')
41+
ax[3].plot(accel_data['Index'], accel_data['Global Timestamp'], label='Accelerometer Global Timestamp', color='yellow')
42+
ax[3].plot(rgb_data['Index'], rgb_data['Global Timestamp'], label='RGB Global Timestamp', color='brown')
43+
ax[3].plot(depth_data['Index'], depth_data['Global Timestamp'], label='Depth Global Timestamp', color='orange')
44+
ax[3].set_title('Global Timestamps')
45+
ax[3].set_ylabel('Global Timestamp (us)')
46+
ax[3].legend()
47+
48+
# Plot global timestamp differences for all sensors if the column exists
49+
if 'Global Timestamp Difference' in gyro_data.columns:
50+
ax[4].plot(gyro_data['Index'], gyro_data['Global Timestamp Difference'], label='Gyro Global Timestamp Difference', color='orange')
51+
ax[4].plot(accel_data['Index'], accel_data['Global Timestamp Difference'], label='Accelerometer Global Timestamp Difference', color='brown')
52+
ax[4].plot(rgb_data['Index'], rgb_data['Global Timestamp Difference'], label='RGB Global Timestamp Difference', color='purple')
53+
ax[4].plot(depth_data['Index'], depth_data['Global Timestamp Difference'], label='Depth Global Timestamp Difference', color='magenta')
54+
ax[4].set_title('Global Timestamp Differences')
55+
ax[4].set_ylabel('Global Timestamp Difference (us)')
56+
ax[4].legend()
57+
58+
# Adjust layout and show plot
59+
plt.tight_layout()
60+
plt.show()
61+
62+
def main():
63+
# Path to the gyro, accel, RGB, and depth CSV files
64+
gyro_csv = 'gyro_data.csv'
65+
accel_csv = 'accel_data.csv'
66+
rgb_csv = 'color_data.csv'
67+
depth_csv = 'depth_data.csv'
68+
69+
# Plot all sensor data
70+
plot_data(gyro_csv, accel_csv, rgb_csv, depth_csv, 'Sensor Data')
71+
72+
if __name__ == "__main__":
73+
main()

examples/save_imu.py renamed to examples/save_imu_and_image_ts.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,43 @@
3535
accel_writer = csv.writer(accel_csv_file)
3636
accel_writer.writerow(['Index', 'Timestamp', 'Global Timestamp','Timestamp Difference', 'Global Timestamp Difference'])
3737

38+
color_csv_file = open('color_data.csv', 'w', newline='')
39+
color_writer = csv.writer(color_csv_file)
40+
color_writer.writerow(['Index', 'Timestamp', 'Global Timestamp', 'Timestamp Difference', 'Global Timestamp Difference'])
41+
42+
depth_csv_file = open('depth_data.csv', 'w', newline='')
43+
depth_writer = csv.writer(depth_csv_file)
44+
depth_writer.writerow(['Index', 'Timestamp', 'Global Timestamp', 'Timestamp Difference', 'Global Timestamp Difference'])
45+
last_color_ts = 0
46+
last_depth_ts = 0
47+
last_color_global_ts = 0
48+
last_depth_global_ts = 0
49+
50+
def on_new_frame_set_callback(frame: FrameSet):
51+
global last_color_ts, last_depth_ts, last_color_global_ts, last_depth_global_ts
52+
if frame is None:
53+
return
54+
color_frame = frame.get_color_frame()
55+
depth_frame = frame.get_depth_frame()
56+
if color_frame is not None:
57+
timestamp = color_frame.get_timestamp_us()
58+
global_timestamp = color_frame.get_global_timestamp_us()
59+
index = color_frame.get_index()
60+
timestamp_diff = timestamp - last_color_ts if last_color_ts != 0 else 0
61+
global_timestamp_diff = global_timestamp - last_color_global_ts if last_color_global_ts != 0 else 0
62+
color_writer.writerow([index, timestamp, global_timestamp, timestamp_diff, global_timestamp_diff])
63+
last_color_ts = timestamp
64+
last_color_global_ts = global_timestamp
65+
if depth_frame is not None:
66+
timestamp = depth_frame.get_timestamp_us()
67+
global_timestamp = depth_frame.get_global_timestamp_us()
68+
index = depth_frame.get_index()
69+
timestamp_diff = timestamp - last_depth_ts if last_depth_ts != 0 else 0
70+
global_timestamp_diff = global_timestamp - last_depth_global_ts if last_depth_global_ts != 0 else 0
71+
depth_writer.writerow([index, timestamp, global_timestamp, timestamp_diff, global_timestamp_diff])
72+
last_depth_ts = timestamp
73+
last_depth_global_ts = global_timestamp
74+
3875
def on_gyro_frame_callback(frame: Frame):
3976
if frame is None:
4077
return
@@ -131,24 +168,12 @@ def main():
131168
print(e)
132169
return
133170

134-
pipeline.start(config)
171+
pipeline.start(config, lambda frames: on_new_frame_set_callback(frames))
135172
pipeline.enable_frame_sync()
136173

137-
last_time = time.time()
138174
while True:
139175
try:
140-
frames = pipeline.wait_for_frames(100)
141-
if not frames:
142-
continue
143-
color_frame = frames.get_color_frame()
144-
depth_frame = frames.get_depth_frame()
145-
if not color_frame or not depth_frame:
146-
continue
147-
if time.time() - last_time > 5.0:
148-
print("get color and depth frame")
149-
print("color frame timestamp: ", color_frame.get_timestamp())
150-
print("depth frame timestamp: ", depth_frame.get_timestamp())
151-
last_time = time.time()
176+
time.sleep(1.0)
152177

153178
except KeyboardInterrupt:
154179
# Don't forget to stop the sensor

0 commit comments

Comments
 (0)