Skip to content

Commit

Permalink
autotest: add test for auto-disabling min alt fence breaches on disar…
Browse files Browse the repository at this point in the history
…ming

clean-up fence manipulation functions and add test for auto-enablement on copter
update tests to have some FENCE_ENABLE tests
add avoidance minimum and maximum altitude fence
add fence switch test while flying
add FenceAutoEnableDisableSwitch for auto mode 2
add more scenarios for plane fence auto-enable
validate fence rc switch behaviour
check fence autoenable by taking off in guided mode
more FENCE_AUTOENABLE tests
add FenceEnableDisableAux and FenceMinAltAutoEnableAbort
  • Loading branch information
andyp1per committed May 22, 2024
1 parent c697bf1 commit 691222c
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 14 deletions.
6 changes: 6 additions & 0 deletions Tools/autotest/Generic_Missions/CMAC-fence.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-35.363720 149.163651
-35.358738 149.165070
-35.359295 149.154434
-35.372292 149.157135
-35.368290 149.166809
-35.358738 149.165070
140 changes: 139 additions & 1 deletion Tools/autotest/arducopter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,7 @@ def MaxAltFence(self):
"FENCE_ENABLE": 1,
"AVOID_ENABLE": 0,
"FENCE_TYPE": 1,
"FENCE_ENABLE" : 1,
})

self.change_alt(10)
Expand Down Expand Up @@ -1655,6 +1656,7 @@ def MinAltFence(self):
# enable fence, disable avoidance
self.set_parameters({
"AVOID_ENABLE": 0,
"FENCE_ENABLE" : 1,
"FENCE_TYPE": 8,
"FENCE_ALT_MIN": 20,
})
Expand Down Expand Up @@ -1700,6 +1702,7 @@ def FenceFloorEnabledLanding(self):
self.progress("Test Landing while fence floor enabled")
self.set_parameters({
"AVOID_ENABLE": 0,
"FENCE_ENABLE" : 1,
"FENCE_TYPE": 15,
"FENCE_ALT_MIN": 10,
"FENCE_ALT_MAX": 20,
Expand All @@ -1720,16 +1723,100 @@ def FenceFloorEnabledLanding(self):
self.set_rc(3, 1800)

self.wait_mode('RTL', timeout=120)

self.wait_landed_and_disarmed()
self.assert_fence_enabled()

# Assert fence is not healthy
# Assert fence is not healthy since it was enabled manually
self.assert_sensor_state(fence_bit, healthy=False)

# Disable the fence using mavlink command to ensure cleaned up SITL state
self.do_fence_disable()
self.assert_fence_disabled()

def FenceFloorAutoDisableLanding(self):
"""Ensures we can initiate and complete an RTL while the fence is enabled"""

fence_bit = mavutil.mavlink.MAV_SYS_STATUS_GEOFENCE

self.progress("Test Landing while fence floor enabled")
self.set_parameters({
"AVOID_ENABLE": 0,
"FENCE_TYPE": 11,
"FENCE_ALT_MIN": 10,
"FENCE_ALT_MAX": 20,
"FENCE_AUTOENABLE" : 1,
})

self.change_mode("GUIDED")
self.wait_ready_to_arm()
self.arm_vehicle()
self.takeoff(alt_min=15, mode="GUIDED")

# Check fence is enabled
self.assert_fence_enabled()

# Change to RC controlled mode
self.change_mode('LOITER')

self.set_rc(3, 1800)

self.wait_mode('RTL', timeout=120)
# Assert fence is not healthy now that we are in RTL
self.assert_sensor_state(fence_bit, healthy=False)

self.wait_landed_and_disarmed(0)
# the breach should have cleared since we auto-disable the
# fence on landing
self.assert_fence_disabled()

# Assert fences have gone now that we have landed and disarmed
self.assert_sensor_state(fence_bit, present=True, enabled=False)

def FenceFloorAutoEnableOnArming(self):
"""Ensures we can auto-enable fences on arming and still takeoff and land"""

fence_bit = mavutil.mavlink.MAV_SYS_STATUS_GEOFENCE

self.set_parameters({
"AVOID_ENABLE": 0,
"FENCE_TYPE": 11,
"FENCE_ALT_MIN": 10,
"FENCE_ALT_MAX": 20,
"FENCE_AUTOENABLE" : 3,
})

self.change_mode("GUIDED")
# Check fence is not enabled
self.assert_fence_disabled()

self.wait_ready_to_arm()
self.arm_vehicle()
self.takeoff(alt_min=15, mode="GUIDED")

# Check fence is enabled
self.assert_fence_enabled()

# Change to RC controlled mode
self.change_mode('LOITER')

self.set_rc(3, 1800)

self.wait_mode('RTL', timeout=120)
# Assert fence is not healthy now that we are in RTL
self.assert_sensor_state(fence_bit, healthy=False)

self.wait_landed_and_disarmed(0)
# the breach should have cleared since we auto-disable the
# fence on landing
self.assert_fence_disabled()

# Assert fences have gone now that we have landed and disarmed
self.assert_sensor_state(fence_bit, present=True, enabled=False)

# Disable the fence using mavlink command to ensure cleaned up SITL state
self.assert_fence_disabled()

def GPSGlitchLoiter(self, timeout=30, max_distance=20):
"""fly_gps_glitch_loiter_test. Fly south east in loiter and test
reaction to gps glitch."""
Expand Down Expand Up @@ -7573,6 +7660,54 @@ def AC_Avoidance_Fence(self):
self.set_parameter("FENCE_ENABLE", 1)
self.check_avoidance_corners()

def AvoidanceAltFence(self):
'''Test fence avoidance at minimum and maximum altitude'''
ex = None
try:
self.set_parameters({
"FENCE_ENABLE": 1,
"FENCE_TYPE": 9, # min and max alt fence
"FENCE_ALT_MIN": 10,
"FENCE_ALT_MAX": 30,
})

self.change_mode('LOITER')
self.wait_ekf_happy()

tstart = self.get_sim_time()
self.takeoff(15, mode='LOITER')
self.progress("Increasing throttle, vehicle should stay below 30m")
self.set_rc(3, 1920)

tstart = self.get_sim_time()
while True:
if self.get_sim_time_cached() - tstart > 20:
break
alt = self.get_altitude(relative=True)
self.progress("Altitude %s" % alt)
if alt > 30:
raise NotAchievedException("Breached maximum altitude (%s)" % (str(alt),))

self.progress("Decreasing, vehicle should stay above 10m")
self.set_rc(3, 1080)
tstart = self.get_sim_time()
while True:
if self.get_sim_time_cached() - tstart > 20:
break
alt = self.get_altitude(relative=True)
self.progress("Altitude %s" % alt)
if alt < 10:
raise NotAchievedException("Breached minimum altitude (%s)" % (str(alt),))

except Exception as e:
self.progress("Caught exception: %s" %
self.get_exception_stacktrace(e))
ex = e
self.land_and_disarm()
self.disarm_vehicle(force=True)
if ex is not None:
raise ex

def global_position_int_for_location(self, loc, time_boot, heading=0):
return self.mav.mav.global_position_int_encode(
int(time_boot * 1000), # time_boot_ms
Expand Down Expand Up @@ -10455,6 +10590,7 @@ def tests1c(self):
self.AC_Avoidance_Proximity_AVOID_ALT_MIN,
self.AC_Avoidance_Fence,
self.AC_Avoidance_Beacon,
self.AvoidanceAltFence,
self.BaroWindCorrection,
self.SetpointGlobalPos,
self.ThrowDoubleDrop,
Expand All @@ -10474,6 +10610,8 @@ def tests1d(self):
self.MaxAltFenceAvoid,
self.MinAltFence,
self.FenceFloorEnabledLanding,
self.FenceFloorAutoDisableLanding,
self.FenceFloorAutoEnableOnArming,
self.AutoTuneSwitch,
self.GPSGlitchLoiter,
self.GPSGlitchLoiter2,
Expand Down
Loading

0 comments on commit 691222c

Please sign in to comment.