|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This example shows how to use the manual controls plugin. |
| 5 | +
|
| 6 | +Note: Manual inputs are taken from a test set in this example to decrease complexity. Manual inputs |
| 7 | +can be received from devices such as a joystick using third-party python extensions |
| 8 | +
|
| 9 | +Note: Taking off the drone is not necessary before enabling manual inputs. It is acceptable to send |
| 10 | +positive throttle input to leave the ground. Takeoff is used in this example to decrease complexity |
| 11 | +""" |
| 12 | + |
| 13 | +import asyncio |
| 14 | +import random |
| 15 | +from mavsdk import System |
| 16 | + |
| 17 | +# Test set of manual inputs. Format: [roll, pitch, throttle, yaw] |
| 18 | +manual_inputs = [ |
| 19 | + [0, 0, 0.5, 0], # no movement |
| 20 | + [-1, 0, 0.5, 0], # minimum roll |
| 21 | + [1, 0, 0.5, 0], # maximum roll |
| 22 | + [0, -1, 0.5, 0], # minimum pitch |
| 23 | + [0, 1, 0.5, 0], # maximum pitch |
| 24 | + [0, 0, 0.5, -1], # minimum yaw |
| 25 | + [0, 0, 0.5, 1], # maximum yaw |
| 26 | + [0, 0, 1, 0], # max throttle |
| 27 | + [0, 0, 0, 0], # minimum throttle |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +async def manual_controls(): |
| 32 | + """Main function to connect to the drone and input manual controls""" |
| 33 | + # Connect to the Simulation |
| 34 | + drone = System() |
| 35 | + await drone.connect(system_address="udp://:14540") |
| 36 | + |
| 37 | + # This waits till a mavlink based drone is connected |
| 38 | + async for state in drone.core.connection_state(): |
| 39 | + if state.is_connected: |
| 40 | + print(f"-- Connected to drone with UUID: {state.uuid}") |
| 41 | + break |
| 42 | + |
| 43 | + # Checking if Global Position Estimate is ok |
| 44 | + async for global_lock in drone.telemetry.health(): |
| 45 | + if global_lock.is_global_position_ok: |
| 46 | + print("-- Global position state is ok") |
| 47 | + break |
| 48 | + |
| 49 | + # set the manual control input after arming |
| 50 | + await drone.manual_control.set_manual_control_input( |
| 51 | + float(0), float(0), float(0.5), float(0) |
| 52 | + ) |
| 53 | + |
| 54 | + # Arming the drone |
| 55 | + print("-- Arming") |
| 56 | + await drone.action.arm() |
| 57 | + |
| 58 | + # Takeoff the vehicle |
| 59 | + print("-- Taking off") |
| 60 | + await drone.action.takeoff() |
| 61 | + await asyncio.sleep(5) |
| 62 | + |
| 63 | + # set the manual control input after arming |
| 64 | + await drone.manual_control.set_manual_control_input( |
| 65 | + float(0), float(0), float(0.5), float(0) |
| 66 | + ) |
| 67 | + |
| 68 | + # start manual control |
| 69 | + print("-- Starting manual control") |
| 70 | + await drone.manual_control.start_position_control() |
| 71 | + |
| 72 | + while True: |
| 73 | + # grabs a random input from the test list |
| 74 | + # WARNING - your simulation vehicle may crash if its unlucky enough |
| 75 | + input_index = random.randint(0, len(manual_inputs) - 1) |
| 76 | + input_list = manual_inputs[input_index] |
| 77 | + |
| 78 | + # get current state of roll axis (between -1 and 1) |
| 79 | + roll = float(input_list[0]) |
| 80 | + # get current state of pitch axis (between -1 and 1) |
| 81 | + pitch = float(input_list[1]) |
| 82 | + # get current state of throttle axis (between -1 and 1, but between 0 and 1 is expected) |
| 83 | + throttle = float(input_list[2]) |
| 84 | + # get current state of yaw axis (between -1 and 1) |
| 85 | + yaw = float(input_list[3]) |
| 86 | + |
| 87 | + await drone.manual_control.set_manual_control_input(roll, pitch, throttle, yaw) |
| 88 | + |
| 89 | + await asyncio.sleep(0.1) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + |
| 94 | + loop = asyncio.get_event_loop() |
| 95 | + loop.run_until_complete(manual_controls()) |
0 commit comments