-
Notifications
You must be signed in to change notification settings - Fork 280
/
02_control_with_arrow_keys.py
executable file
·151 lines (76 loc) · 3.03 KB
/
02_control_with_arrow_keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
Simple script for take off and control with arrow keys
"""
import time
from dronekit import connect, VehicleMode, LocationGlobalRelative, Command, LocationGlobal
from pymavlink import mavutil
#- Importing Tkinter: sudo apt-get install python-tk
import Tkinter as tk
#-- Connect to the vehicle
print('Connecting...')
vehicle = connect('udp:127.0.0.1:14551')
#-- Setup the commanded flying speed
gnd_speed = 5 # [m/s]
#-- Define arm and takeoff
def arm_and_takeoff(altitude):
while not vehicle.is_armable:
print("waiting to be armable")
time.sleep(1)
print("Arming motors")
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
while not vehicle.armed: time.sleep(1)
print("Taking Off")
vehicle.simple_takeoff(altitude)
while True:
v_alt = vehicle.location.global_relative_frame.alt
print(">> Altitude = %.1f m"%v_alt)
if v_alt >= altitude - 1.0:
print("Target altitude reached")
break
time.sleep(1)
#-- Define the function for sending mavlink velocity command in body frame
def set_velocity_body(vehicle, vx, vy, vz):
""" Remember: vz is positive downward!!!
http://ardupilot.org/dev/docs/copter-commands-in-guided-mode.html
Bitmask to indicate which dimensions should be ignored by the vehicle
(a value of 0b0000000000000000 or 0b0000001000000000 indicates that
none of the setpoint dimensions should be ignored). Mapping:
bit 1: x, bit 2: y, bit 3: z,
bit 4: vx, bit 5: vy, bit 6: vz,
bit 7: ax, bit 8: ay, bit 9:
"""
msg = vehicle.message_factory.set_position_target_local_ned_encode(
0,
0, 0,
mavutil.mavlink.MAV_FRAME_BODY_NED,
0b0000111111000111, #-- BITMASK -> Consider only the velocities
0, 0, 0, #-- POSITION
vx, vy, vz, #-- VELOCITY
0, 0, 0, #-- ACCELERATIONS
0, 0)
vehicle.send_mavlink(msg)
vehicle.flush()
#-- Key event function
def key(event):
if event.char == event.keysym: #-- standard keys
if event.keysym == 'r':
print("r pressed >> Set the vehicle to RTL")
vehicle.mode = VehicleMode("RTL")
else: #-- non standard keys
if event.keysym == 'Up':
set_velocity_body(vehicle, gnd_speed, 0, 0)
elif event.keysym == 'Down':
set_velocity_body(vehicle,-gnd_speed, 0, 0)
elif event.keysym == 'Left':
set_velocity_body(vehicle, 0, -gnd_speed, 0)
elif event.keysym == 'Right':
set_velocity_body(vehicle, 0, gnd_speed, 0)
#---- MAIN FUNCTION
#- Takeoff
arm_and_takeoff(10)
#- Read the keyboard with tkinter
root = tk.Tk()
print(">> Control the drone with the arrow keys. Press r for RTL mode")
root.bind_all('<Key>', key)
root.mainloop()