-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontrol_gui.py
executable file
·448 lines (375 loc) · 11.8 KB
/
control_gui.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python3
# Environment setup commands:
# olympe: source ~/code/parrot-groundsdk/./products/olympe/linux/env/shell
import tkinter as tk
from tkinter import *
from PIL import Image
from PIL import ImageTk
import olympe
import subprocess
import time
from olympe.messages.ardrone3.Piloting import TakeOff, Landing, PCMD
from collections import defaultdict
from olympe.messages.ardrone3.PilotingSettingsState import MaxTiltChanged
import olympe.messages.gimbal as gimbal
# from enum import Enum
# Drone flight state variables
is_connected = False
gimbal_attitude = 0
p1 = subprocess;
# Drone constants
DRONE_IP = "192.168.42.1"
SPHINX_IP = "10.202.0.1"
# UI Global variables
HEIGHT = 750
WIDTH = 830
BUTTON_WIDTH = 50
BUTTON_HEIGHT = 50
ROTATE_BUTTON_WIDTH = 70
ROTATE_BUTTON_HEIGHT = 400
# Control variables
control_quit = 0
control_takeoff = 1
# Button helper functions
# Roll drone to the left
def roll_left():
drone(
PCMD(
1,
-10,
0,
0,
0,
10,
)
)
# Roll drone to the right
def roll_right():
drone(
PCMD(
1,
10,
0,
0,
0,
10,
)
)
# Pitch the drone forward (move forward)
def pitch_fwd():
drone(
PCMD(
1,
0,
10,
0,
0,
10,
)
)
# Pitch drone backward (move backward)
def pitch_back():
drone(
PCMD(
1,
0,
-10,
0,
0,
10,
)
)
# Spin drone to the left
def turn_left():
display_message('Turning left...')
drone(
PCMD(
1,
0,
0,
-10,
0,
10,
)
)
# Turn drone to the right
def turn_right():
display_message('Turning right...')
drone(
PCMD(
1,
0,
0,
10,
0,
10,
)
)
def increase_throttle():
drone(
PCMD(
0,
0,
0,
0,
10,
10,
)
)
def decrease_throttle():
drone(
PCMD(
0,
0,
0,
0,
-10,
10,
)
)
# Connect to drone
def connect():
global is_connected
if not is_connected:
display_message('Connecting to the drone...')
drone.connect()
display_message('Connected successfully.')
is_connected = True
connect_button.config(state = "disabled")
start_fpv_button.config(state = "normal")
enable_gimbal_buttons()
# Takeoff routine
def takeoff():
display_message('Taking off...')
assert drone(TakeOff()).wait().success()
display_message('Takeoff successful')
# Set gimbal to attitude so that it looks straight
drone(
gimbal.set_target(
gimbal_id = 0,
control_mode = "position",
yaw_frame_of_reference = "absolute",
yaw = 0.0,
pitch_frame_of_reference = "absolute",
pitch = 0,
roll_frame_of_reference = "absolute",
roll = 0.0
)
).wait()
global gimbal_attitude
gimbal_attitude = 0.0
takeoff_button.config(state = "disabled")
enable_movement_buttons()
# Landing routine
def land():
display_message('Landing...')
assert drone(Landing()).wait().success()
display_message('Landed successfully.')
disable_all_buttons()
enable_gimbal_buttons()
def move_forward():
global gimbal_attitude
# Move straight
if gimbal_attitude <= 10 and gimbal_attitude >= -10:
display_message('Moving straight forward.')
pitch_fwd()
# Increase throttle - move up
elif gimbal_attitude == 100:
display_message('Increasing throttle.')
increase_throttle()
# Decrease throttle - move down
elif gimbal_attitude == -100:
display_message('Decreasing throttle.')
decrease_throttle()
def move_gimbal(attitude):
drone(
gimbal.set_target(
gimbal_id = 0,
control_mode = "position",
yaw_frame_of_reference = "absolute",
yaw = 0.0,
pitch_frame_of_reference = "absolute",
pitch = attitude,
roll_frame_of_reference = "absolute",
roll = 0.0
)
).wait()
def gimbal_up():
global gimbal_attitude
new_attitude = gimbal_attitude + 10
if new_attitude > 100:
new_attitude = 100
display_message('Tilting gimbal up.')
move_gimbal(new_attitude)
gimbal_attitude = new_attitude
if gimbal_attitude != 100:
takeoff_button.config(state = "disabled")
else:
takeoff_button.config(state = "normal")
land_button.config(state = "disabled")
def gimbal_down():
global gimbal_attitude
new_attitude = gimbal_attitude - 10
if new_attitude < -100:
new_attitude = -100
display_message('Tilting gimbal down')
move_gimbal(new_attitude)
gimbal_attitude = new_attitude
if gimbal_attitude != -100:
land_button.config(state = "disabled")
else:
land_button.config(state = "normal")
takeoff_button.config(state = "disabled")
def look_forward():
global gimbal_attitude
move_gimbal(0)
display_message('Gimbal facing straight ahead.')
gimbal_attitude = 0
takeoff_button.config(state = "disabled")
land_button.config(state = "disabled")
def look_up():
global gimbal_attitude
move_gimbal(100)
display_message('Gimbal facing straight up.')
gimbal_attitude = 100
takeoff_button.config(state = "normal")
land_button.config(state = "disabled")
def look_down():
global gimbal_attitude
move_gimbal(-100)
display_message('Gimbal facing straight down.')
gimbal_attitude = -100
takeoff_button.config(state = "disabled")
land_button.config(state = "normal")
def start_fpv():
display_message('Starting first person view video feed...')
p1 = subprocess.Popen(['/home/achilles/code/parrot-groundsdk/out/pdraw-linux/staging/native-wrapper.sh', 'pdraw', '-u','rtsp://10.202.0.1/live'])
def display_message(message):
global message_box
message_box.insert(END, message)
# setting up screen
root = tk.Tk()
root.resizable(False, False)
root.title("Anafi Drone GUI")
root.configure(bg='white')
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.configure(bg='white')
canvas.pack()
controlFrame = tk.Frame(root)
controlFrame.configure(bg='white')
controlFrame.place(relwidth=.95, relheight=.95, relx=0.025, rely=0.025)
# rotate left
l_rotate_button_image = Image.open("images/turn_left.png")
l_rotate_photoImg = ImageTk.PhotoImage(l_rotate_button_image)
l_rotate_button = Button(controlFrame, image=l_rotate_photoImg, command=turn_left)
l_rotate_button.place(relwidth=.2, relheight=.2105, relx=0, rely=0.35)
# rotate right
r_rotate_button_image = Image.open("images/turn_right.png")
r_rotate_photoImg = ImageTk.PhotoImage(r_rotate_button_image)
r_rotate_button = Button(
controlFrame, image=r_rotate_photoImg, command=turn_right)
r_rotate_button.place(relwidth=.2, relheight=.2105, relx=0.35, rely=0.35)
# move forward button
forward_button_image = Image.open("images/move_forward.png")
forward_button_photoImg = ImageTk.PhotoImage(forward_button_image)
forward_button = Button(
controlFrame, image=forward_button_photoImg, command=move_forward)
forward_button.place(relwidth=0.15, relheight=0.2, relx=0.20, rely=0.1)
# connect button
connect_button_image = Image.open("images/connect.png")
connect_button_photoImg = ImageTk.PhotoImage(connect_button_image)
connect_button = Button(
controlFrame, image=connect_button_photoImg, command=connect)
connect_button.place(relwidth=0.2, relheight=0.1, relx=.56, rely=.58)
# Start FPV button
start_fpv_image = Image.open("images/start_fpv.png")
start_fpv_button_photoImg = ImageTk.PhotoImage(start_fpv_image)
start_fpv_button = Button(
controlFrame, image=start_fpv_button_photoImg, command=start_fpv)
start_fpv_button.place(relwidth=0.2, relheight=0.1, relx=0.785, rely=.58)
# takeoff button
takeoff_button_image = Image.open("images/takeoff.png")
takeoff_button_photoImg = ImageTk.PhotoImage(takeoff_button_image)
takeoff_button = Button(
controlFrame, image=takeoff_button_photoImg, command=takeoff)
takeoff_button.place(relwidth=0.22, relheight=0.28, relx=0.55, rely=.7)
# land button
land_button_image = Image.open("images/land.png")
land_button_photoImg = ImageTk.PhotoImage(land_button_image)
land_button = Button(
controlFrame, image=land_button_photoImg, command=land)
land_button.place(relwidth=0.22, relheight=0.28, relx=0.785, rely=.7)
# gimbal up button
gimbal_up_button_image = Image.open("images/gimbal_up.png")
gimbal_up_button_photoImg = ImageTk.PhotoImage(gimbal_up_button_image)
gimbal_up_button = Button(
controlFrame, image=gimbal_up_button_photoImg, command=gimbal_up)
gimbal_up_button.place(relwidth=.207, relheight=.2105, relx=0.56, rely=0.1)
# gimbal down button
gimbal_down_button_image = Image.open("images/gimbal_down.png")
gimbal_down_button_photoImg = ImageTk.PhotoImage(gimbal_down_button_image)
gimbal_down_button = Button(
controlFrame, image=gimbal_down_button_photoImg, command=gimbal_down)
gimbal_down_button.place(relwidth=.207, relheight=.2105, relx=0.56, rely=0.35)
# look up button
look_up_button_image = Image.open("images/look_up.png")
look_up_button_photoImg = ImageTk.PhotoImage(look_up_button_image)
look_up_button = Button(
controlFrame, image=look_up_button_photoImg, command=look_up)
look_up_button.place(relwidth=.207, relheight=.15, relx=0.785, rely=0.1)
# look forward button
look_forward_button_image = Image.open("images/look_forward.png")
look_forward_button_photoImg = ImageTk.PhotoImage(look_forward_button_image)
look_forward_button = Button(
controlFrame, image=look_forward_button_photoImg, command=look_forward)
look_forward_button.place(relwidth=.207, relheight=.15, relx=0.785, rely=0.257)
# look down button
look_down_button_image = Image.open("images/look_down.png")
look_down_button_photoImg = ImageTk.PhotoImage(look_down_button_image)
look_down_button = Button(
controlFrame, image=look_down_button_photoImg, command=look_down)
look_down_button.place(relwidth=.207, relheight=.15, relx=0.785, rely=0.41)
message_box = Listbox(controlFrame)
message_box.place(relwidth= .5, relheight= .35, relx= 0, rely= .6)
buttons = [ l_rotate_button,
r_rotate_button,
forward_button,
takeoff_button,
land_button,
gimbal_up_button,
gimbal_down_button,
look_up_button,
look_forward_button,
look_down_button,
connect_button,
start_fpv_button ]
def disable_all_buttons():
global buttons
for button in buttons:
button.config(state = "disabled")
def enable_all_buttons():
global buttons
for button in buttons:
button.config(state = "normal")
def disable_gimbal_buttons():
look_up_button.config(state = "disabled")
look_down_button.config(state = "disabled")
look_forward_button.config(state = "disabled")
gimbal_up_button.config(state = "disabled")
gimbal_down_button.config(state = "disabled")
def enable_gimbal_buttons():
look_up_button.config(state = "normal")
look_down_button.config(state = "normal")
look_forward_button.config(state = "normal")
gimbal_up_button.config(state = "normal")
gimbal_down_button.config(state = "normal")
def enable_movement_buttons():
l_rotate_button.config(state = "normal")
r_rotate_button.config(state = "normal")
forward_button.config(state = "normal")
# Main Loop Start:
if __name__ == "__main__":
with olympe.Drone(SPHINX_IP) as drone:
disable_all_buttons()
connect_button.config(state = "normal")
root.mainloop()