131 lines
7.1 KiB
Python
131 lines
7.1 KiB
Python
#!/usr/bin/python3
|
|||
|
|
"""Numerical/reference-frame and fail-closed contract tests; no Gazebo required."""
|
||
|
|
from array import array
|
||
|
|
import copy
|
||
|
|
import math
|
||
|
|
from pathlib import Path
|
||
|
|
import sys
|
||
|
|
import unittest
|
||
|
|
|
||
|
|
import numpy as np
|
||
|
|
import rclpy
|
||
|
|
from nav_msgs.msg import Odometry
|
||
|
|
from sensor_msgs.msg import PointCloud2,PointField
|
||
|
|
|
||
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'src/br_sim_bringup/scripts'))
|
||
|
|
from lio_navigation_adapter import FixedAlignment,LioNavigationAdapter,quaternion,rotation,shifted_twist,float_fields
|
||
|
|
|
||
|
|
|
||
|
|
class Recorder:
|
||
|
|
def __init__(self):self.messages=[]
|
||
|
|
def publish(self,m):self.messages.append(copy.deepcopy(m))
|
||
|
|
|
||
|
|
|
||
|
|
class GeometryTests(unittest.TestCase):
|
||
|
|
def test_known_start_maps_imu_and_optical_origins(self):
|
||
|
|
imu=np.array([.011,.02329,.25588]);prior=np.array([-4.5,4.5,.15,math.pi/2])
|
||
|
|
p=np.array([.2,-.3,.02]);q=np.array([0.,0.,math.sin(.35),math.cos(.35)])
|
||
|
|
a=FixedAlignment(prior,imu,p,q)
|
||
|
|
base,bq=a.pose(p,q,-imu);lidar,_=a.pose(p,q,np.array([0.,0.,.3])-imu)
|
||
|
|
np.testing.assert_allclose(base,prior[:3],atol=1e-12)
|
||
|
|
np.testing.assert_allclose(lidar,prior[:3]+[0,0,.3],atol=1e-12)
|
||
|
|
self.assertAlmostEqual(math.atan2(rotation(bq)[1,0],rotation(bq)[0,0]),math.pi/2)
|
||
|
|
old_t=a.t.copy();later,_=a.pose(p+[1,0,0],q,-imu)
|
||
|
|
np.testing.assert_allclose(later-base,a.R@[1,0,0],atol=1e-12)
|
||
|
|
np.testing.assert_array_equal(a.t,old_t)
|
||
|
|
|
||
|
|
def test_rotating_imu_lever_arm_by_finite_difference(self):
|
||
|
|
offset=np.array([.011,.02329,.25588]);yaw=.8;w=.4;dt=1e-6
|
||
|
|
def R(y):return np.array([[math.cos(y),-math.sin(y),0],[math.sin(y),math.cos(y),0],[0,0,1]])
|
||
|
|
imu_world_velocity=(R(yaw+w*dt)@offset-R(yaw-w*dt)@offset)/(2*dt)
|
||
|
|
imu_body_velocity=R(yaw).T@imu_world_velocity
|
||
|
|
base=shifted_twist(imu_body_velocity,[0,0,w],-offset)
|
||
|
|
np.testing.assert_allclose(base,[0,0,0],atol=1e-10)
|
||
|
|
lidar=shifted_twist(imu_body_velocity,[0,0,w],np.array([0,0,.3])-offset)
|
||
|
|
np.testing.assert_allclose(lidar,[0,0,0],atol=1e-10)
|
||
|
|
|
||
|
|
def test_invalid_quaternions_are_rejected(self):
|
||
|
|
for values in ([0,0,0,0],[0,0,0,2],[float('nan'),0,0,1]):
|
||
|
|
with self.assertRaises(ValueError):quaternion(values)
|
||
|
|
|
||
|
|
|
||
|
|
class AdapterTests(unittest.TestCase):
|
||
|
|
@classmethod
|
||
|
|
def setUpClass(cls):rclpy.init()
|
||
|
|
@classmethod
|
||
|
|
def tearDownClass(cls):rclpy.shutdown()
|
||
|
|
def setUp(self):
|
||
|
|
self.node=LioNavigationAdapter();self.time=1.;self.node.now_s=lambda:self.time
|
||
|
|
for key in ('body_pub','sensor_pub','world_pub','planning_pub','ready_pub','status_pub'):setattr(self.node,key,Recorder())
|
||
|
|
def tearDown(self):self.node.destroy_node()
|
||
|
|
def odom(self,t):
|
||
|
|
self.time=t;m=Odometry();m.header.frame_id='camera_init';m.child_frame_id='body'
|
||
|
|
m.header.stamp.sec=int(t);m.header.stamp.nanosec=round((t-int(t))*1e9)
|
||
|
|
m.pose.pose.orientation.w=1.;return m
|
||
|
|
def anchor(self):
|
||
|
|
for t in np.arange(1.,2.11,.05):self.node.on_odom(self.odom(float(t)))
|
||
|
|
self.assertIsNotNone(self.node.alignment)
|
||
|
|
|
||
|
|
def test_stale_and_discontinuous_odometry_never_reanchors(self):
|
||
|
|
self.anchor();t=self.node.odom_stamp;translation=self.node.alignment.t.copy()
|
||
|
|
stale=self.odom(t);self.time=t+.3;count=len(self.node.body_pub.messages)
|
||
|
|
self.node.on_odom(stale);self.node.health()
|
||
|
|
self.assertFalse(self.node.ready_pub.messages[-1].data)
|
||
|
|
self.assertEqual(len(self.node.body_pub.messages),count)
|
||
|
|
self.node.on_odom(self.odom(t+1.))
|
||
|
|
self.assertTrue(self.node.fault)
|
||
|
|
self.assertEqual(len(self.node.body_pub.messages),count)
|
||
|
|
np.testing.assert_array_equal(self.node.alignment.t,translation)
|
||
|
|
|
||
|
|
def test_paired_cloud_filters_original_indices_and_preserves_stamp(self):
|
||
|
|
self.anchor();n=111;data=np.zeros((n,6),dtype='<f4');data[:,3]=1.
|
||
|
|
data[0,3:]=[0,0,-.3] # self return, regardless of latest robot pose
|
||
|
|
data[1,2]=-self.node.alignment.t[2] # world ground
|
||
|
|
m=PointCloud2();m.header.frame_id='camera_init';m.header.stamp=self.odom(self.time).header.stamp
|
||
|
|
m.height=1;m.width=n;m.point_step=24;m.row_step=24*n
|
||
|
|
m.fields=[PointField(name=name,offset=i*4,datatype=PointField.FLOAT32,count=1) for i,name in enumerate(('x','y','z','lidar_x','lidar_y','lidar_z'))]
|
||
|
|
m.data=array('B',data.tobytes());self.node.on_cloud(m);self.node.health()
|
||
|
|
self.assertTrue(self.node.ready_pub.messages[-1].data)
|
||
|
|
full=self.node.world_pub.messages[-1];filtered=self.node.planning_pub.messages[-1]
|
||
|
|
self.assertEqual(filtered.header.stamp,m.header.stamp)
|
||
|
|
xyz=np.stack(float_fields(filtered,('x','y','z')),axis=-1).reshape(n,3)
|
||
|
|
self.assertTrue(np.isnan(xyz[:2]).all());self.assertTrue(np.isfinite(xyz[2:]).all())
|
||
|
|
for a,b in zip(float_fields(full,('lidar_x','lidar_y','lidar_z')),float_fields(filtered,('lidar_x','lidar_y','lidar_z'))):np.testing.assert_array_equal(a,b)
|
||
|
|
|
||
|
|
def test_wrong_source_frame_latches_stop(self):
|
||
|
|
m=self.odom(1.);m.header.frame_id='world';self.node.on_odom(m)
|
||
|
|
self.assertTrue(self.node.fault);self.assertIsNone(self.node.alignment)
|
||
|
|
|
||
|
|
def test_known_slope_uncertainty_preserves_raised_obstacles(self):
|
||
|
|
self.anchor();self.node.planes=[[-.8,2.7,3.,4.,-.597/3.5,0.,1.6119/3.5]]
|
||
|
|
self.node.surface_tolerance=.06
|
||
|
|
xyz=[]
|
||
|
|
for bias in (-.04,0.,.04):
|
||
|
|
for height in (0.,.15):
|
||
|
|
xyz.extend([[1.,3.5,(1.6119-.597)/3.5+bias+height]]*20)
|
||
|
|
data=np.ones((len(xyz),6),dtype='<f4');data[:,:3]=np.asarray(xyz)-self.node.alignment.t
|
||
|
|
m=PointCloud2();m.header.frame_id='camera_init';m.header.stamp=self.odom(self.time).header.stamp
|
||
|
|
m.height=1;m.width=len(data);m.point_step=24;m.row_step=24*len(data)
|
||
|
|
m.fields=[PointField(name=n,offset=i*4,datatype=PointField.FLOAT32,count=1) for i,n in enumerate(('x','y','z','lidar_x','lidar_y','lidar_z'))]
|
||
|
|
m.data=array('B',data.tobytes());original=bytes(m.data);self.node.on_cloud(m)
|
||
|
|
result=np.stack(float_fields(self.node.planning_pub.messages[-1],('x','y','z')),axis=-1).reshape(-1,3)
|
||
|
|
for i in range(3):
|
||
|
|
self.assertTrue(np.isnan(result[i*40:i*40+20]).all())
|
||
|
|
self.assertTrue(np.isfinite(result[i*40+20:(i+1)*40]).all())
|
||
|
|
self.assertEqual(bytes(m.data),original)
|
||
|
|
full=np.stack(float_fields(self.node.world_pub.messages[-1],('x','y','z')),axis=-1).reshape(-1,3)
|
||
|
|
np.testing.assert_allclose(full,xyz,atol=1e-6)
|
||
|
|
|
||
|
|
def test_downstream_planning_loss_revokes_readiness(self):
|
||
|
|
self.anchor();self.node.planning_topic='/sim/navigation/terrain_planning_cloud'
|
||
|
|
self.node.planning_max_age=.2
|
||
|
|
self.node.cloud_stamp=self.time;self.node.downstream_stamp=self.time
|
||
|
|
self.node.health();self.assertTrue(self.node.ready_pub.messages[-1].data)
|
||
|
|
self.time+=.19;self.node.odom_stamp=self.time;self.node.cloud_stamp=self.time
|
||
|
|
self.node.health();self.assertTrue(self.node.ready_pub.messages[-1].data)
|
||
|
|
self.time+=.02;self.node.odom_stamp=self.time;self.node.cloud_stamp=self.time
|
||
|
|
self.node.health();self.assertFalse(self.node.ready_pub.messages[-1].data)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__=='__main__':unittest.main()
|