| name | multi-sensor-timesync |
| description | 多传感器时间同步技能 - 硬件同步、软同步、NTP、ROS2 时间同步 |
| argument-hint | 时间同步 OR hardware sync OR NTP OR timesync |
| user-invocable | true |
多传感器时间同步技能
多传感器时间同步方案
何时使用
当需要以下帮助时使用此技能:
- 硬件同步配置
- 软件同步实现
- NTP/PTP 同步
- ROS2 同步机制
- 时间戳校正
核心实现
硬件同步
hardware_config:
lidar:
model: Velodyne VLP-16
mode: phase_lock
sync_frequency: 10
phase_offset: 0.0
camera:
model:_basalt
trigger_mode: hardware_external
trigger_delay: 0.002
gps_imu:
model: XSens MTi
sync_mode: time_ins
ROS2 软同步
import rclpy
from rclpy.node import Node
from message_filters import Subscriber, ApproximateTimeSynchronizer
from sensor_msgs.msg import Image, PointCloud2, Imu
from geometry_msgs.msg import PoseWithCovarianceStamped
class MultiSensorSyncNode(Node):
def __init__(self):
super().__init__('multi_sensor_sync')
self.image_sub = Subscriber(self, Image, '/camera/image_raw')
self.lidar_sub = Subscriber(self, PointCloud2, '/lidar/points')
self.imu_sub = Subscriber(self, Imu, '/imu/data')
self.gps_sub = Subscriber(self, PoseWithCovarianceStamped, '/gps/pose')
self.sync = ApproximateTimeSynchronizer(
[self.image_sub, self.lidar_sub, self.imu_sub],
queue_size=20,
slop=0.05
)
self.sync.registerCallback(self.sync_callback)
def sync_callback(self, image, lidar, imu):
stamp = image.header.stamp
.process_data(image, lidar, imu)
时间戳校正
import numpy as np
class TimeSynchronizer:
"""软件时间同步"""
def __init__(self, buffer_size=10):
self.buffer_size = buffer_size
self.timestamps = {
'camera': [],
'lidar': [],
'imu': []
}
self.offsets = {
'lidar_camera': 0.0,
'imu_camera': 0.0
}
def add_timestamp(self, sensor, stamp):
"""添加时间戳"""
self.timestamps[sensor].append(stamp)
if len(self.timestamps[sensor]) > self.buffer_size:
self.timestamps[sensor].pop(0)
self.estimate_offset()
def estimate_offset(self):
"""估计传感器间时间偏移"""
for s1, s2 in [('lidar', 'camera'), ('imu', 'camera')]:
if len(self.timestamps[s1]) > 5 (.timestamps[s2]) > :
offset = .compute_offset(
.timestamps[s1],
.timestamps[s2])
.offsets[] = offset
():
diffs = []
t1 times1:
closest = (times2, key= t2: (t2 - t1))
diffs.append(t1 - closest)
np.median(diffs)
():
sensor == :
stamp - .offsets[]
sensor == :
stamp - .offsets[]
stamp
PTP 同步
ethtool -T eth0
sudo ptp4l -i eth0 -m &