Skip to content

Commit edd21f4

Browse files
authored
Merge pull request #427 from potaito/potaito/failure-injection-example
Failure injection example script
2 parents 0ef2093 + 10328b1 commit edd21f4

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

examples/failure_injection.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
3+
import asyncio
4+
5+
from mavsdk import System
6+
from mavsdk.failure import FailureType, FailureUnit
7+
8+
async def run():
9+
drone = System()
10+
await drone.connect(system_address="udp://:14540")
11+
12+
print("Waiting for drone to connect...")
13+
async for state in drone.core.connection_state():
14+
if state.is_connected:
15+
print(f"Drone discovered with UUID: {state.uuid}")
16+
break
17+
18+
print("Waiting for drone to have a global position estimate...")
19+
async for health in drone.telemetry.health():
20+
if health.is_global_position_ok and health.is_home_position_ok:
21+
print("Global position estimate ok")
22+
break
23+
24+
print("-- Enabling failure injection")
25+
await drone.param.set_param_int('SYS_FAILURE_EN', 1)
26+
27+
print("-- Arming")
28+
await drone.action.arm()
29+
30+
print("-- Taking off")
31+
await drone.action.takeoff()
32+
33+
await asyncio.sleep(5)
34+
35+
goto_lat = 0.0
36+
goto_lon = 0.0
37+
goto_alt = 0.0
38+
async for position in drone.telemetry.position():
39+
# Only need position once
40+
if position.latitude_deg and position.longitude_deg:
41+
goto_lat = position.latitude_deg
42+
goto_lon = position.longitude_deg
43+
goto_alt = position.absolute_altitude_m
44+
break
45+
46+
print("-- Flying up")
47+
flying_alt = goto_alt + 20.0 # To fly drone 20m above the ground plane
48+
await drone.action.goto_location(goto_lat, goto_lon, flying_alt, 0)
49+
50+
await asyncio.sleep(5)
51+
52+
print("-- Injecting GPS failure")
53+
await drone.failure.inject(FailureUnit.SENSOR_GPS, FailureType.OFF, instance=0)
54+
55+
print("-- Waiting 20s before exiting script...")
56+
await asyncio.sleep(20)
57+
58+
print("-- Disabling failure injection")
59+
await drone.param.set_param_int('SYS_FAILURE_EN', 0)
60+
61+
if __name__ == "__main__":
62+
loop = asyncio.get_event_loop()
63+
loop.run_until_complete(run())

0 commit comments

Comments
 (0)