#!/usr/bin/python3 """Safety leases, geometry, phase history and terminal-stop tests (no Gazebo).""" from pathlib import Path import sys,math,unittest import numpy as np import rclpy sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'src/br_sim_bringup/scripts')) from mission_command_mux import select_command from ramp_mission import RampMission,body_command,envelope,obstacle_count,ground_height,speed_target from lio_navigation_adapter import multiply,rotation from scan_planner_msgs.msg import TrajectoryStatus from std_srvs.srv import Trigger from livox_ros_driver2.msg import CustomMsg,CustomPoint from sensor_msgs_py.point_cloud2 import create_cloud_xyz32 from std_msgs.msg import Header def qpose(pitch=0.,yaw=math.pi): return multiply(np.array([0.,0.,math.sin(yaw/2),math.cos(yaw/2)]),np.array([0.,math.sin(pitch/2),0.,math.cos(pitch/2)])) class Recorder: def __init__(self):self.messages=[] def publish(self,m):self.messages.append(m) class MuxTests(unittest.TestCase): def test_no_lease_health_or_expired_lease_is_zero(self): nav=(1.,(.3,0.,0.));lease=dict(stamp=1.,since=.9,mode='NAV') for now,l,health in ((1.,None,True),(1.,lease,False),(1.16,lease,True),(.99,lease,True)): self.assertEqual(select_command(now,l,nav,None,health)[0],(0.,0.,0.)) def test_only_selected_source_can_drive(self): for mode,expected in (('NAV',(.2,.1,0.)),('RAMP',(.3,0.,.1)),('STOP',(0.,0.,0.))): self.assertEqual(select_command(1.,dict(stamp=1.,since=.9,mode=mode),(1.,(.2,.1,0.)),(1.,(.3,0.,.1)),True)[0],expected) def test_commands_before_grant_or_stale_cannot_reappear(self): for t in (.8,.89,1.01): self.assertEqual(select_command(1.,dict(stamp=1.,since=.9,mode='RAMP'),None,(t,(.3,0.,0.)),True)[0],(0.,0.,0.)) def test_nonfinite_or_missing_source_is_zero(self): lease=dict(stamp=1.,since=.9,mode='NAV') self.assertEqual(select_command(1.,lease,None,None,True)[0],(0.,0.,0.)) self.assertEqual(select_command(1.,lease,(1.,(float('nan'),0.,0.)),None,True)[0],(0.,0.,0.)) def test_linear_norm_not_just_individual_axes_is_limited(self): cmd,_=select_command(1.,dict(stamp=1.,since=.9,mode='NAV'),(1.,(.5,.5,1.)),None,True) self.assertAlmostEqual(math.hypot(*cmd[:2]),.5);self.assertEqual(cmd[2],.5) def test_opt_in_limits_and_expiry_at_fast_speed(self): lease=dict(stamp=1.,since=.9,mode='NAV');nav=(1.,(.7,.7,1.)) cmd,_=select_command(1.,lease,nav,None,True,.7,.8) self.assertAlmostEqual(math.hypot(*cmd[:2]),.7);self.assertEqual(cmd[2],.8) self.assertEqual(select_command(1.16,lease,nav,None,True,.7,.8)[0],(0.,0.,0.)) cmd,_=select_command(1.,lease,(1.,(2.,2.,1.)),None,True,2.,1.) self.assertAlmostEqual(math.hypot(*cmd[:2]),2.) self.assertEqual(select_command(1.16,lease,(1.,cmd),None,True,2.,1.)[0],(0.,0.,0.)) class GeometryTests(unittest.TestCase): def test_fast_profile_brakes_before_low_speed_zone_and_still_limits_final_approach(self): self.assertEqual(speed_target(1.,1.2,.2,True),1.2) self.assertLess(speed_target(-.7,1.2,.2,True),1.2) self.assertLessEqual(speed_target(-1.,1.2,.2,True),.2) self.assertLess(speed_target(-1.292,1.2,.2,True),.05) # Full-speed braking corridor must grow when speed rises, rather than # using only a small fixed lookahead after the experimental speed lift. obstacle=np.array([[.4,3.5125,float(ground_height(.4))+.2]]) self.assertEqual(obstacle_count(obstacle,[1.6,3.5125,.5],qpose(-.169),.3,1.2),0) self.assertEqual(obstacle_count(obstacle,[1.6,3.5125,.5],qpose(-.169),1.2,1.2),1) def test_crest_braking_begins_before_crest_zone(self): self.assertEqual(speed_target(1.,.7,.2),.7) self.assertLess(speed_target(.2,.7,.2),.7) self.assertEqual(speed_target(-.25,.7,.2),.2) self.assertEqual(speed_target(-.8,.7,.2),.2) self.assertLess(speed_target(-1.2,.7,.2),.1) def test_pitch_correct_body_forward_and_cross_track(self): for pitch in (0.,-.169): q=qpose(pitch);cmd=body_command(q,.3,.01);world=rotation(q)@np.array([cmd[0],cmd[1],0.]) self.assertLess(world[0],0.);self.assertGreater(world[1],0.);self.assertAlmostEqual(cmd[0],.3,places=10) def test_whole_body_on_platform_requires_more_than_flat_pitch(self): for x,clear in ((-1.,False),(-1.3,True),(-1.5,False)): lo,hi=envelope([x,3.5125,.75],qpose()) self.assertEqual(lo[0]>=-1.76 and hi[0]<=-.84,clear) def test_obstacle_guard_keeps_floor_and_outside_rail_separate(self): cloud=np.array([[1.1,3.5125,float(ground_height(1.1))],[1.1,3.025,.9],[1.1,3.5125,float(ground_height(1.1))+.2]]) self.assertEqual(obstacle_count(cloud,[1.6,3.5125,.5],qpose(-.169),.3),1) class StateTests(unittest.TestCase): @classmethod def setUpClass(cls): # Never let service tests address a live mission on simulation domain 77. import os os.environ['ROS_LOCALHOST_ONLY']='1' rclpy.init(domain_id=189) @classmethod def tearDownClass(cls):rclpy.shutdown() def setUp(self): self.n=RampMission();self.t=10.;self.n.now=lambda:self.t for name in ('lease_pub','direct_pub','report_pub','goal_pub'):setattr(self.n,name,Recorder()) self.n.state='ENTRY';self.n.start_time=10.;self.n.cloud=np.empty((0,3));self.n.gyro=np.zeros(3) def tearDown(self):self.n.destroy_node() def tick(self,x=3.7,y=3.5125,pitch=0.,speed=0.,dt=.1): self.t+=dt;self.n.pose=dict(stamp=self.t,p=np.array([x,y,.15+float(ground_height(x))]),q=qpose(pitch),velocity=np.array([speed,0.,0.]),speed=speed,wz=0.) self.n.pose_window.append(self.n.pose) while self.n.pose_window and self.n.pose_window[0]['stamp']