完善双雷达仿真与自动上坡任务,更新使用文档

加入下置 MID360 前向补盲与双雷达融合,修复北侧走廊导航、坐标适配和数据时效处理。

实现接近坡脚、姿态辅助上坡与平台停车任务,增加独立快模式、命令仲裁及失效停车保护,保留测试与分阶段验收记录。

同步实车尺寸及雷达安装参数,调整 Gazebo 红方默认视角,完善根目录 README、仿真使用和实车迁移说明。
This commit is contained in:
2026-11-12 16:42:07 +08:00
parent cac93cae11
commit 6f1d837b46
95 changed files with 13667 additions and 244 deletions
+216
View File
@@ -0,0 +1,216 @@
#!/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']<self.t-(.35 if self.n.fast else .5):self.n.pose_window.popleft()
self.n.ready=True;self.n.ready_stamp=self.t;self.n.imu_stamp=self.t;self.n.cloud_stamp=self.t;self.n.lower_stamp=self.t;self.n.tick()
def test_initial_flat_never_means_arrival(self):
for _ in range(8):self.tick()
self.assertEqual(self.n.state,'ENTRY');self.assertFalse(self.n.seen_slope)
def test_missing_incline_is_latched_failure(self):
self.tick(x=1.7);self.assertEqual(self.n.state,'FAILED');self.assertEqual(self.n.mode,'STOP')
self.tick();self.assertEqual(self.n.state,'FAILED')
def test_sustained_incline_then_flat_still_needs_full_vehicle_clear(self):
for _ in range(4):self.tick(x=2.1,pitch=-.169)
self.assertEqual(self.n.state,'CLIMB');self.assertTrue(self.n.seen_slope)
self.tick(x=-.3,pitch=-.169);self.assertEqual(self.n.state,'CREST')
for _ in range(4):self.tick(x=-1.)
self.assertEqual(self.n.state,'PLATFORM')
self.tick(x=-1.3);self.assertEqual(self.n.state,'SETTLE')
for _ in range(10):self.tick(x=-1.3,speed=.08)
self.assertEqual(self.n.state,'SETTLE')
for _ in range(100):self.tick(x=-1.3,dt=.02)
self.assertEqual(self.n.state,'SUCCEEDED');self.assertEqual(self.n.mode,'STOP')
def test_lateral_drift_stops_before_leaving_lane(self):
self.tick(x=1.5,y=3.56,pitch=-.169);self.assertEqual(self.n.reason,'estimated_lateral_clearance');self.assertEqual(self.n.mode,'STOP')
def test_new_obstacle_revokes_ramp_drive(self):
self.n.seen_slope=True;self.n.state='CLIMB'
self.n.cloud=np.array([[1.1,3.5125,float(ground_height(1.1))+.2]])
self.tick(x=1.6,pitch=-.169,speed=.3);self.assertEqual(self.n.reason,'obstacle_in_braking_corridor');self.assertEqual(self.n.mode,'STOP')
def test_stale_state_is_not_replaced_by_open_loop_motion(self):
self.tick();self.n.ready=False;self.t+=.02;self.n.tick()
self.assertEqual(self.n.state,'FAILED');self.assertEqual(self.n.mode,'STOP')
self.tick();self.assertEqual(self.n.state,'FAILED');self.assertEqual(self.n.mode,'STOP')
def test_raw_lower_loss_stops_before_fused_cloud_timeout(self):
self.tick();self.t+=.24
self.n.ready_stamp=self.t;self.n.pose['stamp']=self.t;self.n.imu_stamp=self.t;self.n.cloud_stamp=self.t
self.n.tick()
self.assertEqual(self.n.state,'FAILED');self.assertEqual(self.n.mode,'STOP')
self.assertEqual(self.n.health_failure['failed_checks'],['lower_scan'])
self.assertAlmostEqual(self.n.health_failure['sources']['lower_scan']['age_s'],.24)
captured=self.n.health_failure
self.tick();self.assertIs(self.n.health_failure,captured)
def test_fast_late_scan_holds_then_resumes_but_expiry_still_latches(self):
self.n.fast=True;self.n.ramp_speed=1.1;self.n.ramp_acceleration=2.
self.tick();self.t+=.16
for name in ('ready_stamp','imu_stamp','cloud_stamp'):setattr(self.n,name,self.t)
self.n.pose['stamp']=self.t;self.n.tick()
self.assertEqual(self.n.state,'ENTRY');self.assertEqual(self.n.mode,'STOP')
self.assertTrue(self.n.lower_hold);self.assertIsNone(self.n.health_failure)
self.tick();self.assertFalse(self.n.lower_hold);self.assertEqual(self.n.mode,'RAMP')
self.t+=.24
for name in ('ready_stamp','imu_stamp','cloud_stamp'):setattr(self.n,name,self.t)
self.n.pose['stamp']=self.t;self.n.tick()
self.assertEqual(self.n.state,'FAILED')
self.tick();self.assertEqual(self.n.state,'FAILED');self.assertEqual(self.n.mode,'STOP')
def test_single_delayed_lower_frame_does_not_latch_failure(self):
self.tick();self.t+=.199
self.n.ready_stamp=self.t;self.n.pose['stamp']=self.t;self.n.imu_stamp=self.t;self.n.cloud_stamp=self.t
self.n.tick();self.assertEqual(self.n.state,'ENTRY');self.assertIsNone(self.n.health_failure)
def test_future_cloud_waits_for_clock_without_replacing_fresh_input(self):
self.tick();previous=self.n.cloud_stamp
h=Header(frame_id='world');h.stamp.sec=10;h.stamp.nanosec=150000000
self.n.on_cloud(create_cloud_xyz32(h,[[10.,10.,0.]]))
self.n.tick();self.assertEqual(self.n.cloud_stamp,previous);self.assertEqual(self.n.state,'ENTRY')
self.t=10.16;self.n.tick();self.assertAlmostEqual(self.n.cloud_stamp,10.15);self.assertEqual(self.n.state,'ENTRY')
def test_queued_future_cloud_cannot_refresh_expired_data(self):
self.tick();self.t+=.31
h=Header(frame_id='world');h.stamp.sec=11
self.n.on_cloud(create_cloud_xyz32(h,[[10.,10.,0.]]))
self.n.ready_stamp=self.t;self.n.pose['stamp']=self.t;self.n.imu_stamp=self.t;self.n.lower_stamp=self.t
self.n.tick();self.assertEqual(self.n.health_failure['failed_checks'],['fused_cloud'])
def test_lower_watchdog_preserves_actual_source_time_and_clock_order(self):
self.n.state='IDLE';self.n.requested=False
m=CustomMsg();m.header.frame_id='lidar_low';m.header.stamp.sec=10;m.header.stamp.nanosec=100000000
m.timebase=10100000000;m.lidar_id=1;m.point_num=1
point=CustomPoint();point.offset_time=23000000;m.points=[point]
self.n.on_lower(m);self.t=10.12;self.n.tick();self.assertIsNone(self.n.lower_stamp)
self.t=10.13;self.n.tick();self.assertAlmostEqual(self.n.lower_stamp,10.123)
self.n.on_lower(m);self.n.tick();self.assertEqual(self.n.reason,'lower_time_backwards')
def test_wrong_lidar_cannot_refresh_lower_watchdog(self):
m=CustomMsg();m.header.frame_id='lidar';m.lidar_id=0
self.n.on_lower(m);self.assertEqual(self.n.reason,'invalid_lower_scan')
def test_terminal_navigation_result_survives_lease_age(self):
# Terminal status is an event, unlike ACTIVE's short-lived motion grant.
self.n.state='NAVIGATE';self.n.goal_sent=8.;self.n.wait_goal_id=0
status=TrajectoryStatus();status.goal_id=1;status.state=status.SUCCEEDED
status.header.stamp.sec=9;status.goal.x=3.7;status.goal.y=4.8
self.n.status=status
for _ in range(60):self.tick(x=3.7,y=4.8,dt=.02)
self.assertEqual(self.n.nav_index,1)
self.assertEqual(len(self.n.goal_pub.messages),1)
def test_stale_active_grant_cannot_move(self):
self.n.state='NAVIGATE';self.n.goal_sent=8.;self.n.wait_goal_id=0
status=TrajectoryStatus();status.goal_id=1;status.state=status.ACTIVE
status.header.stamp.sec=9;status.goal.x=3.7;status.goal.y=4.8;self.n.status=status
self.tick(x=3.7,y=4.8);self.assertEqual(self.n.mode,'STOP')
def test_fast_pass_through_requires_fresh_grant_and_revokes_it_for_new_goal(self):
self.n.fast=True;self.n.goals=[self.n.goals[0],self.n.goals[-1]]
self.n.state='NAVIGATE';self.n.goal_sent=8.;self.n.wait_goal_id=0
status=TrajectoryStatus();status.goal_id=1;status.state=status.ACTIVE
status.goal.x=3.7;status.goal.y=4.8;status.header.stamp.sec=9
self.n.status=status
self.tick(x=3.05,y=4.8)
self.assertEqual(self.n.nav_index,0) # Stale grant cannot advance.
status.header.stamp.sec=10;status.header.stamp.nanosec=200000000
self.tick(x=3.05,y=4.8)
self.assertEqual(self.n.nav_index,1)
self.assertEqual(self.n.wait_goal_id,1)
self.assertEqual(self.n.mode,'STOP')
self.assertEqual(len(self.n.goal_pub.messages),1)
self.assertAlmostEqual(self.n.goal_pub.messages[0].pose.position.y,3.5125)
def test_actual_start_and_cancel_services(self):
self.n.state='IDLE';self.n.requested=False
start=self.n.create_client(Trigger,'/ramp_mission/start')
cancel=self.n.create_client(Trigger,'/ramp_mission/cancel')
self.assertTrue(start.wait_for_service(timeout_sec=2.))
future=start.call_async(Trigger.Request());rclpy.spin_until_future_complete(self.n,future,timeout_sec=2.)
self.assertTrue(future.result().success);self.assertTrue(self.n.requested)
duplicate=start.call_async(Trigger.Request());rclpy.spin_until_future_complete(self.n,duplicate,timeout_sec=2.)
self.assertFalse(duplicate.result().success)
future=cancel.call_async(Trigger.Request());rclpy.spin_until_future_complete(self.n,future,timeout_sec=2.)
self.assertTrue(future.result().success);self.assertEqual(self.n.state,'FAILED')
if __name__=='__main__':unittest.main()