加入下置 MID360 前向补盲与双雷达融合,修复北侧走廊导航、坐标适配和数据时效处理。 实现接近坡脚、姿态辅助上坡与平台停车任务,增加独立快模式、命令仲裁及失效停车保护,保留测试与分阶段验收记录。 同步实车尺寸及雷达安装参数,调整 Gazebo 红方默认视角,完善根目录 README、仿真使用和实车迁移说明。
188 lines
11 KiB
Python
188 lines
11 KiB
Python
#!/usr/bin/python3
|
|
"""Reference-frame, deskew, schema and fail-closed tests for dual perception."""
|
|
from pathlib import Path
|
|
import math
|
|
import sys
|
|
import unittest
|
|
import numpy as np
|
|
import rclpy
|
|
from livox_ros_driver2.msg import CustomMsg,CustomPoint
|
|
from nav_msgs.msg import Odometry
|
|
from rclpy.qos import ReliabilityPolicy
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'src/br_sim_bringup/scripts'))
|
|
from dual_lidar_fusion import interpolate_poses,project_scan,cloud_message,DualLidarFusion
|
|
from lio_navigation_adapter import rotation
|
|
|
|
LOW=np.array([.325,0.,.044]);LEFT=rotation([0,0,math.sin(math.pi/4),math.cos(math.pi/4)])
|
|
|
|
|
|
def stationary():return [(float(t),np.zeros(3),np.array([0.,0.,0.,1.])) for t in np.arange(0.,.111,.01)]
|
|
|
|
|
|
class GeometryTests(unittest.TestCase):
|
|
def test_vehicle_front_half_is_lower_negative_y(self):
|
|
raw=np.array([[0,-1,0],[0,1,0],[1,0,0],[-1,0,0],[-1,.1,0]])
|
|
world,origins,t,_=project_scan(raw,np.arange(5)*1000000,0,stationary(),LOW,LEFT,True)
|
|
np.testing.assert_allclose(world,np.array([[1,0,0],[0,1,0],[0,-1,0]])+LOW,atol=1e-12)
|
|
np.testing.assert_allclose(origins,np.tile(LOW,(3,1)));np.testing.assert_array_equal(t,[0,2000000,3000000])
|
|
|
|
def test_front_sector_moves_with_vehicle_heading(self):
|
|
q=np.array([0,0,math.sin(math.pi/4),math.cos(math.pi/4)])
|
|
history=[(t,p,q) for t,p,_ in stationary()]
|
|
world,origins,_,_=project_scan([[0,-1,0]],[0],0,history,LOW,LEFT,True)
|
|
np.testing.assert_allclose(world,[[0,1.325,.044]],atol=1e-12)
|
|
np.testing.assert_allclose(origins,[[0,.325,.044]],atol=1e-12)
|
|
|
|
def test_motion_compensates_every_actual_sample_and_origin(self):
|
|
ts=np.arange(0.,.101,.01);history=[];raw=[];expected_origins=[];target=np.array([2.,.2,.08])
|
|
for t in ts:
|
|
q=np.array([0.,0.,math.sin(t*1.5),math.cos(t*1.5)]);R=rotation(q);p=np.array([t*.4,0.,0.])
|
|
history.append((float(t),p,q))
|
|
sample=np.array([0.,.025,.05,.075,.099])
|
|
for t in sample:
|
|
R=rotation([0,0,math.sin(t*1.5),math.cos(t*1.5)]);p=np.array([t*.4,0.,0.])
|
|
raw.append(LEFT.T@(R.T@(target-p)-LOW));expected_origins.append(p+R@LOW)
|
|
world,origins,t,_=project_scan(raw,np.rint(sample*1e9).astype(np.int64),0,history,LOW,LEFT,True)
|
|
np.testing.assert_allclose(world,np.tile(target,(len(sample),1)),atol=1e-10)
|
|
np.testing.assert_allclose(origins,expected_origins,atol=1e-10)
|
|
np.testing.assert_array_equal(t,np.rint(sample*1e9).astype(np.int64))
|
|
self.assertGreater(np.linalg.norm(origins[0]-origins[-1]),.05)
|
|
|
|
def test_pose_coverage_never_extrapolates(self):
|
|
for t in (-.01,.12):
|
|
with self.assertRaises(LookupError):interpolate_poses(stationary(),[t])
|
|
with self.assertRaises(ValueError):interpolate_poses([stationary()[0],stationary()[-1]],[.05])
|
|
|
|
def test_slerp_handles_equivalent_quaternion_signs(self):
|
|
history=stationary();history[1]=(history[1][0],history[1][1],-history[1][2])
|
|
_,q=interpolate_poses(history,[.005]);np.testing.assert_allclose(abs(q[0]),[0,0,0,1])
|
|
|
|
def test_invalid_offsets_rejected(self):
|
|
for offsets in ([-1],[100000000],[5000000,1000000]):
|
|
with self.assertRaises(ValueError):project_scan(np.tile([0,-1.,0],(len(offsets),1)),offsets,0,stationary(),LOW,LEFT,True)
|
|
|
|
def test_world_cloud_preserves_origin_time_identity(self):
|
|
times=np.array([1000000011,1099000011],dtype=np.int64)
|
|
m=cloud_message(np.array([[1,2,3],[4,5,6]]),np.tile(LOW,(2,1)),times,[0,1],1099000011)
|
|
self.assertEqual(m.header.frame_id,'world');self.assertEqual(m.header.stamp.nanosec,99000011)
|
|
fields={f.name:f for f in m.fields}
|
|
def read(name,dtype):return np.ndarray((m.width,),dtype=dtype,buffer=m.data,offset=fields[name].offset,strides=(m.point_step,))
|
|
np.testing.assert_array_equal(read('sensor_id','u1'),[0,1])
|
|
np.testing.assert_array_equal(read('time_sec','<i4'),[1,1]);np.testing.assert_array_equal(read('time_nanosec','<u4'),[11,99000011])
|
|
np.testing.assert_allclose(read('origin_z','<f4'),[.044,.044])
|
|
|
|
|
|
class Recorder:
|
|
def __init__(self):self.messages=[]
|
|
def publish(self,m):self.messages.append(m)
|
|
|
|
|
|
class NodeTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):rclpy.init()
|
|
@classmethod
|
|
def tearDownClass(cls):rclpy.shutdown()
|
|
def setUp(self):
|
|
self.node=DualLidarFusion();self.node.now_s=lambda:.11
|
|
self.node.full=Recorder();self.node.planning=Recorder();self.node.history.extend(stationary())
|
|
def tearDown(self):self.node.destroy_node()
|
|
def scan(self,i,t=0):
|
|
m=CustomMsg();m.header.frame_id='lidar' if i==0 else 'lidar_low';m.lidar_id=i
|
|
m.timebase=t;m.header.stamp.sec=t//1000000000;m.header.stamp.nanosec=t%1000000000
|
|
p=CustomPoint();p.x=2. if i==0 else 0.;p.y=0. if i==0 else -2.;p.offset_time=99000000
|
|
m.points=[p];m.point_num=1;return m
|
|
def test_pose_subscription_preserves_reliable_bursts(self):
|
|
subscription=next(s for s in self.node.subscriptions if s.topic_name=='/sim/navigation/body_odom')
|
|
self.assertEqual(subscription.qos_profile.depth,100)
|
|
self.assertEqual(subscription.qos_profile.reliability,ReliabilityPolicy.RELIABLE)
|
|
|
|
def test_clock_callback_order_does_not_drop_near_future_pose(self):
|
|
m=Odometry();m.header.frame_id='world';m.child_frame_id='lio_base_link'
|
|
m.header.stamp.nanosec=120000000;m.pose.pose.orientation.w=1.
|
|
self.node.on_pose(m)
|
|
self.assertAlmostEqual(self.node.history[-1][0],.12)
|
|
self.assertFalse(self.node.fault)
|
|
|
|
def test_pair_required_and_each_source_retained(self):
|
|
self.node.on_scan(0,self.scan(0));self.node.flush();self.assertFalse(self.node.full.messages)
|
|
self.node.on_scan(1,self.scan(1));self.node.flush()
|
|
self.assertEqual(len(self.node.full.messages),1);self.assertEqual(self.node.full.messages[0].width,2)
|
|
# Repeated timer ticks cannot republish old data to fake freshness.
|
|
self.node.flush();self.assertEqual(len(self.node.full.messages),1)
|
|
def test_exact_scan_end_pose_does_not_wait_for_another_frame(self):
|
|
self.node.history.clear();self.node.now_s=lambda:41.4
|
|
for ns in (41260000000,41300000000,41361000000):
|
|
m=Odometry();m.header.frame_id='world';m.child_frame_id='lio_base_link'
|
|
m.header.stamp.sec=ns//1000000000;m.header.stamp.nanosec=ns%1000000000;m.pose.pose.orientation.w=1.
|
|
self.node.on_pose(m)
|
|
self.node.on_scan(0,self.scan(0,41262000000));self.node.on_scan(1,self.scan(1,41262000000))
|
|
self.node.flush();self.assertEqual(len(self.node.full.messages),1)
|
|
self.assertEqual(self.node.full.messages[0].header.stamp.nanosec,361000000)
|
|
def test_complete_pose_coverage_still_waits_for_source_clock(self):
|
|
self.node.now_s=lambda:.09
|
|
self.node.on_scan(0,self.scan(0));self.node.on_scan(1,self.scan(1));self.node.flush()
|
|
self.assertFalse(self.node.full.messages)
|
|
self.node.now_s=lambda:.11;self.node.flush()
|
|
self.assertEqual(len(self.node.full.messages),1)
|
|
self.assertEqual(self.node.full.messages[0].header.stamp.nanosec,99000000)
|
|
def test_incomplete_pose_history_drops_pair_then_recovers(self):
|
|
self.node.history.clear();self.node.history.extend([stationary()[0],stationary()[-1]])
|
|
self.node.on_scan(0,self.scan(0));self.node.on_scan(1,self.scan(1));self.node.flush()
|
|
self.assertFalse(self.node.fault);self.assertFalse(self.node.full.messages)
|
|
self.node.history.clear();self.node.history.extend((t+.1,p,q) for t,p,q in stationary())
|
|
self.node.now_s=lambda:.22
|
|
self.node.on_scan(0,self.scan(0,100000000));self.node.on_scan(1,self.scan(1,100000000));self.node.flush()
|
|
self.assertEqual(len(self.node.full.messages),1)
|
|
|
|
def test_wrong_frame_stops_publication(self):
|
|
m=self.scan(1);m.header.frame_id='lidar';self.node.on_scan(1,m)
|
|
self.assertTrue(self.node.fault);self.node.on_scan(0,self.scan(0));self.node.flush()
|
|
self.assertFalse(self.node.full.messages)
|
|
def test_timebase_mismatch_stops_publication(self):
|
|
m=self.scan(1);m.header.stamp.nanosec=1;self.node.on_scan(1,m);self.assertTrue(self.node.fault)
|
|
def test_stale_pair_never_published(self):
|
|
self.node.now_s=lambda:1.
|
|
self.node.on_scan(0,self.scan(0));self.node.on_scan(1,self.scan(1));self.node.flush()
|
|
self.assertFalse(self.node.full.messages)
|
|
|
|
def test_ground_uses_raw_geometry_even_with_world_height_and_tilt_error(self):
|
|
# A controlled +3 cm world offset and pitch error make a real ground
|
|
# point exceed the old 15 mm world-Z cut. The raw lower optics still
|
|
# measure the same ground and an 8 cm obstacle, separated by 80 mm.
|
|
q=np.array([0.,math.sin(-.01/2),0.,math.cos(-.01/2)])
|
|
self.node.history.clear();self.node.history.extend((t,np.array([0.,0.,.18]),q) for t,_,_ in stationary())
|
|
upper=self.scan(0);lower=self.scan(1);lower.points=[]
|
|
for height in (0.,.02,.08):
|
|
p=CustomPoint();p.x=0.;p.y=-1.;p.z=float(height-.194);p.offset_time=99000000
|
|
lower.points.append(p)
|
|
lower.point_num=len(lower.points)
|
|
self.node.on_scan(0,upper);self.node.on_scan(1,lower);self.node.flush()
|
|
full=self.node.full.messages[-1];planning=self.node.planning.messages[-1]
|
|
raw=np.stack([np.ndarray((full.width,),dtype='<f4',buffer=full.data,offset=i*4,strides=(full.point_step,)) for i in range(3)],axis=1)
|
|
filtered=np.stack([np.ndarray((planning.width,),dtype='<f4',buffer=planning.data,offset=i*4,strides=(planning.point_step,)) for i in range(3)],axis=1)
|
|
self.assertGreater(raw[1,2],.015)
|
|
self.assertTrue(np.isnan(filtered[1]).all())
|
|
self.assertTrue(np.isfinite(filtered[2:]).all()) # 2 cm and 8 cm tops kept.
|
|
for i in range(full.width):
|
|
start=i*full.point_step+12;end=(i+1)*full.point_step
|
|
self.assertEqual(bytes(full.data[start:end]),bytes(planning.data[start:end]))
|
|
self.assertEqual(full.header,planning.header)
|
|
|
|
def test_negative_world_drift_keeps_low_tops_from_both_lidars(self):
|
|
self.node.history.clear();self.node.history.extend((t,np.array([0.,0.,.11]),q) for t,_,q in stationary())
|
|
for i,optical_height in ((0,.48),(1,.194)):
|
|
m=self.scan(i);m.points=[]
|
|
for height in (0.,.02,.08):
|
|
p=CustomPoint();p.x=2. if i==0 else 0.;p.y=0. if i==0 else -2.
|
|
p.z=float(height-optical_height);p.offset_time=99000000;m.points.append(p)
|
|
m.point_num=len(m.points);self.node.on_scan(i,m)
|
|
self.node.flush();full=self.node.full.messages[-1];planning=self.node.planning.messages[-1]
|
|
def xyz(m):return np.stack([np.ndarray((m.width,),dtype='<f4',buffer=m.data,offset=i*4,strides=(m.point_step,)) for i in range(3)],axis=1)
|
|
raw=xyz(full);filtered=xyz(planning)
|
|
self.assertTrue(np.isnan(filtered[[0,3]]).all())
|
|
self.assertTrue(np.isfinite(filtered[[1,2,4,5]]).all())
|
|
self.assertTrue((raw[[1,4],2]<0.).all()) # 2 cm tops are below world zero.
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|