Skip to content

Commit

Permalink
autotest: add a test for trimming AHRS using RC inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Jan 3, 2025
1 parent b77961e commit cbe6cb0
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Tools/autotest/arducopter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12332,6 +12332,89 @@ def RTLYaw(self):
self.wait_heading(original_heading)
self.wait_disarmed()

def AHRSAutoTrim(self):
'''calibrate AHRS trim using RC input'''
self.progress("Making earth frame same as body frame") # because I'm lazy
self.takeoff(5, mode='GUIDED')
self.guided_achieve_heading(0)
self.do_land()

self.set_parameters({
'RC9_OPTION': 182,
})

param_last_check_time = 0
for mode in ['STABILIZE', 'ALT_HOLD']:
self.set_parameters({
'AHRS_TRIM_X': 0.1,
'AHRS_TRIM_Y': -0.1,
})
self.takeoff(mode=mode)
self.set_rc(9, 2000)
tstart = self.get_sim_time()
while True:
now = self.get_sim_time_cached()
if now - tstart > 30:
raise ValueError(f"Failed to reduce trims in {mode}!")
lpn = self.assert_receive_message('LOCAL_POSITION_NED')
delta = 40
roll_input = 1500
if lpn.vx > 0:
roll_input -= delta
elif lpn.vx < 0:
roll_input += delta

pitch_input = 1500
if lpn.vy > 0:
pitch_input += delta
elif lpn.vy < 0:
pitch_input -= delta
self.set_rc_from_map({
1: roll_input,
2: pitch_input,
}, quiet=True)

# check parameters once/second:
if now - param_last_check_time > 1:
param_last_check_time = now
trim_x = self.get_parameter('AHRS_TRIM_X', verbose=False)
trim_y = self.get_parameter('AHRS_TRIM_Y', verbose=False)
self.progress(f"trim_x={trim_x} trim_y={trim_y}")
if abs(trim_x) < 0.02 and abs(trim_y) < 0.02:
self.progress("Good AHRS trims")
if abs(lpn.vx) > 1 or abs(lpn.vy) > 1:
raise NotAchievedException("Velocity after trimming?!")
break
self.context_collect('STATUSTEXT')
self.set_rc(9, 1000)
self.wait_statustext('Trim saved', check_context=True)
self.context_stop_collecting('STATUSTEXT')
self.do_land()
self.set_rc_default()

self.progress("Landing should cancel the autotrim")
self.takeoff(10, mode='STABILIZE')
self.context_collect('STATUSTEXT')
self.set_rc(9, 2000)
self.wait_statustext('AutoTrim running', check_context=True)
self.do_land()
self.wait_statustext('AutoTrim cancelled', check_context=True)
self.set_rc(9, 1000)

self.progress("Changing mode to LOITER")
self.takeoff(10, mode='STABILIZE')
self.context_collect('STATUSTEXT')
self.set_rc(9, 2000)
self.wait_statustext('AutoTrim running', check_context=True)
self.change_mode('LOITER')
self.wait_statustext('AutoTrim cancelled', check_context=True)
self.do_land()
self.set_rc(9, 1000)

def do_land(self):
self.change_mode('LAND')
self.wait_disarmed()

def tests2b(self): # this block currently around 9.5mins here
'''return list of all tests'''
ret = ([
Expand Down Expand Up @@ -12442,6 +12525,7 @@ def tests2b(self): # this block currently around 9.5mins here
self.CommonOrigin,
self.TestTetherStuck,
self.ScriptingFlipMode,
self.AHRSAutoTrim,
])
return ret

Expand Down

0 comments on commit cbe6cb0

Please sign in to comment.