#!/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','