autotest: add flight test for OpticalFlow

This commit is contained in:
Peter Barker 2024-01-04 15:41:11 +11:00 committed by Peter Barker
parent c443d19ab1
commit 18adc77979

View File

@ -2160,8 +2160,10 @@ class AutoTestCopter(vehicle_test_suite.TestSuite):
"EK3_SRC1_VELZ": 0,
})
def OpticalFlow(self):
'''test optical flow works'''
def OpticalFlowLocation(self):
'''test optical flow doesn't supply location'''
self.context_push()
self.assert_sensor_state(mavutil.mavlink.MAV_SYS_STATUS_SENSOR_OPTICAL_FLOW, False, False, False, verbose=True)
@ -2179,6 +2181,65 @@ class AutoTestCopter(vehicle_test_suite.TestSuite):
self.delay_sim_time(5)
self.wait_statustext("Need Position Estimate", timeout=300)
self.context_pop()
self.reboot_sitl()
def OpticalFlow(self):
'''test OpticalFlow in flight'''
self.start_subtest("Make sure no crash if no rangefinder")
self.context_push()
self.set_parameter("SIM_FLOW_ENABLE", 1)
self.set_parameter("FLOW_TYPE", 10)
self.set_analog_rangefinder_parameters()
self.reboot_sitl()
self.change_mode('LOITER')
# ensure OPTICAL_FLOW message is reasonable:
global flow_rate_rads
global rangefinder_distance
global gps_speed
global last_debug_time
flow_rate_rads = 0
rangefinder_distance = 0
gps_speed = 0
last_debug_time = 0
def check_optical_flow(mav, m):
global flow_rate_rads
global rangefinder_distance
global gps_speed
global last_debug_time
m_type = m.get_type()
if m_type == "OPTICAL_FLOW":
flow_rate_rads = math.sqrt(m.flow_comp_m_x**2+m.flow_comp_m_y**2)
elif m_type == "RANGEFINDER":
rangefinder_distance = m.distance
elif m_type == "GPS_RAW_INT":
gps_speed = m.vel/100.0 # cm/s -> m/s
of_speed = flow_rate_rads * rangefinder_distance
if abs(of_speed - gps_speed) > 3:
raise NotAchievedException("gps=%f vs of=%f mismatch" %
(gps_speed, of_speed))
now = self.get_sim_time_cached()
if now - last_debug_time > 5:
last_debug_time = now
self.progress("gps=%f of=%f" % (gps_speed, of_speed))
self.install_message_hook_context(check_optical_flow)
self.fly_generic_mission("CMAC-copter-navtest.txt")
self.context_pop()
self.reboot_sitl()
def OpticalFlowLimits(self):
'''test EKF navigation limiting'''
ex = None
@ -3623,6 +3684,15 @@ class AutoTestCopter(vehicle_test_suite.TestSuite):
self.wait_waypoint(num_wp-1, num_wp-1)
self.wait_disarmed()
def fly_generic_mission(self, filename, strict=True):
num_wp = self.load_generic_mission(filename, strict=strict)
self.set_parameter("AUTO_OPTIONS", 3)
self.change_mode('AUTO')
self.wait_ready_to_arm()
self.arm_vehicle()
self.wait_waypoint(num_wp-1, num_wp-1)
self.wait_disarmed()
def SurfaceTracking(self):
'''Test Surface Tracking'''
ex = None
@ -10067,6 +10137,7 @@ class AutoTestCopter(vehicle_test_suite.TestSuite):
self.ModeCircle,
self.MagFail,
self.OpticalFlow,
self.OpticalFlowLocation,
self.OpticalFlowLimits,
self.OpticalFlowCalibration,
self.MotorFail,