Skip to content

Commit

Permalink
Merge pull request #427 from potaito/potaito/failure-injection-example
Browse files Browse the repository at this point in the history
Failure injection example script
  • Loading branch information
julianoes authored Jan 4, 2022
2 parents 0ef2093 + 10328b1 commit edd21f4
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/failure_injection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3

import asyncio

from mavsdk import System
from mavsdk.failure import FailureType, FailureUnit

async def run():
drone = System()
await drone.connect(system_address="udp://:14540")

print("Waiting for drone to connect...")
async for state in drone.core.connection_state():
if state.is_connected:
print(f"Drone discovered with UUID: {state.uuid}")
break

print("Waiting for drone to have a global position estimate...")
async for health in drone.telemetry.health():
if health.is_global_position_ok and health.is_home_position_ok:
print("Global position estimate ok")
break

print("-- Enabling failure injection")
await drone.param.set_param_int('SYS_FAILURE_EN', 1)

print("-- Arming")
await drone.action.arm()

print("-- Taking off")
await drone.action.takeoff()

await asyncio.sleep(5)

goto_lat = 0.0
goto_lon = 0.0
goto_alt = 0.0
async for position in drone.telemetry.position():
# Only need position once
if position.latitude_deg and position.longitude_deg:
goto_lat = position.latitude_deg
goto_lon = position.longitude_deg
goto_alt = position.absolute_altitude_m
break

print("-- Flying up")
flying_alt = goto_alt + 20.0 # To fly drone 20m above the ground plane
await drone.action.goto_location(goto_lat, goto_lon, flying_alt, 0)

await asyncio.sleep(5)

print("-- Injecting GPS failure")
await drone.failure.inject(FailureUnit.SENSOR_GPS, FailureType.OFF, instance=0)

print("-- Waiting 20s before exiting script...")
await asyncio.sleep(20)

print("-- Disabling failure injection")
await drone.param.set_param_int('SYS_FAILURE_EN', 0)

if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run())

0 comments on commit edd21f4

Please sign in to comment.