test sync time

This commit is contained in:
flandre 2025-06-09 14:50:04 +08:00
parent 4a24879206
commit 721708cb3a

97
test/sync_time.py Normal file
View File

@ -0,0 +1,97 @@
import json
from pathlib import Path
import itertools
from flandre.utils.RfFrame import b2t
rec_folder = Path(
'/run/media/lambda/b86dccdc-f134-464b-a310-6575ee9ae85c/record/')
robot_folder = rec_folder / 'robot'
device_folder = rec_folder / 'device'
camera_folder = rec_folder / 'camera'
device_ts_map = {int(file.stem): file for file in device_folder.glob('*')}
camera_ts_map = {int(file.stem): file for file in camera_folder.glob('*')}
js = itertools.chain(*[json.loads(file.read_text())
for file in robot_folder.glob('*')])
robot_ts_map = {v['host_ts']: v for v in js}
def func(a: int, b: list[int]):
"""
Finds the largest number in list `b` that is less than or equal to `a`.
Args:
a: An integer.
b: A list of integers.
Returns:
The largest number in `b` <= `a`, or None if no such number exists.
"""
# Create a new list containing only the numbers from b that are <= a
candidates = [num for num in b if num <= a]
# If the candidates list is not empty, return the largest number from it
if candidates:
return max(candidates)
else:
# If no number in b was less than or equal to a, return None
return None
# print(device_ts_map.__len__())
# print(robot_ts_map.__len__())
ass = sorted(list(device_ts_map.keys()))[50:-100]
a0 = ass[0]
a1 = ass[-1]
print((a1-a0)*10**-9)
r0 = robot_ts_map[func(a0, list(robot_ts_map.keys()))]['device_ts']
r1 = robot_ts_map[func(a1, list(robot_ts_map.keys()))]['device_ts']
seq, encoder, host_ts, device_ts0, buffer = b2t(device_ts_map[a0].read_bytes())
print(device_ts0)
seq, encoder, host_ts, device_ts1, buffer = b2t(device_ts_map[a1].read_bytes())
print(device_ts1)
print(a1-a0, (device_ts1-device_ts0)*10**3, int((r1-r0)*10**9))
print((a1-a0-(device_ts1-device_ts0)*10**3)*10**-9)
print((a1-a0-int((r1-r0)*10**9))*10**-9)
# for i, a in enumerate(ass):
# try:
# # a - func(a, list(robot_ts_map.keys()))
# print(i, a-func(a, list(robot_ts_map.keys())))
# except Exception:
# print(i,a)
# pass
# a = 174945200000000000
# print(func(a, list(robot_ts_map.keys())))
# print(ass[1766-1:1766+2])
# seq, encoder, host_ts, device_ts, buffer = b2t(device_ts_map[1749444627751252600].read_bytes())
# print(seq)
# seq, encoder, host_ts, device_ts, buffer = b2t(device_ts_map[1749442951214858600].read_bytes())
# print(seq)
# seq, encoder, host_ts, device_ts, buffer = b2t(device_ts_map[1749444583738329700].read_bytes())
# print(seq)
# 1766 1749442951214858600
# 2271 1749442950915344400
# 2272 1749442950944313600
# 2273 1749442950961272400
# 2274 1749442950993404700
# 2275 1749442951056585700
# 2276 1749442951061834900
# 2277 1749442951119567900
# 2278 1749442951139249100
# 2279 1749442951144567800
# 2280 1749442951149875300
# 2281 1749442951169837900
# 2282 1749442951197964400