Skip to content

Commit c9ac5ba

Browse files
committed
Added Manual Control Example
1 parent 7732d09 commit c9ac5ba

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

examples/manual_control.py

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

0 commit comments

Comments
 (0)