-
Notifications
You must be signed in to change notification settings - Fork 8
/
ARDroneGUI.py
140 lines (130 loc) · 4.79 KB
/
ARDroneGUI.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
# -*- coding:Utf-8 -*-
# ARDrone Lib Package
prog_name = "AR.Drone GUI"
# version:
version = 4
# By Vianney Tran, Romain Fihue, Giulia Guidi, Julien Lagarde
# License: Creative Commons Attribution-ShareAlike 3.0 (CC BY-SA 3.0)
# (http://creativecommons.org/licenses/by-sa/3.0/)
##############
### IMPORT ###
##############
import os, time, threading
from Tkinter import *
###############
### GLOBALS ###
###############
FPS = 50
###################
### DEFINITIONS ###
###################
def nop_func(arg1=None):
"Do nothing"
pass
def kill_fen(root):
"Kill the window that called"
root.fen.destroy()
root.fen.quit()
# Return a function
return nop_func
###############
### CLASSES ###
###############
class ControlWindow():
"Create a simple window to control the drone"
def __init__(self,default_action=nop_func):
"Create a window to control the drone"
self.actions = dict() # Actions associated to keys
self.to_print = list() # Data we have to print each time
self.default_action = default_action
self.text = None # Text that will be changed
self.text_label = None
self.last_key = None
self.running = False
def add_action(self,button_bind,function_call):
"Add an action when a key is pressed"
self.actions[button_bind] = function_call
return True
def add_printable_data(self, description, tree):
"Add something in navdata to print, tree is a tulpe"
# Check if the arguments are good
if type(tree) != type((0,0)): raise TypeError("Tree must be a tulpe")
self.to_print.append((str(description),tree))
return True
def change_text(self, new_text):
"Change the text inside the box"
try: new_text=str(new_text) # Check if we have no error changing text
except: return False
if self.text is None: return False # Window not yet initiated
self.text.set(new_text)
return True
def callback(self, navdata):
"Callback function that can be given to Navdata filter"
new_text = ""
if len(navdata.keys()) < 1: return False # We don't have any navdata
for p in self.to_print:
# Get data
data = navdata
# Get the tree
for key in p[1]:
if data != None:
if str(key) in data.keys():
data = data[str(key)]
else:
data = "Error in given tree (" + str(p[1]) + "): " + str(key)
else:
data = "No data"
break
# Format
new_text = new_text + str(p[0]) + ": " + str(data) + "\n"
# And done
self.change_text(new_text)
def start(self):
"Activate the window (and keep the thread)"
self.fen = Tk()
cadre = Frame(self.fen, width=500, height = 200, bg="grey")
self.text = StringVar() # Text that will be changed
self.text_label = Label(self.fen,textvariable=self.text, fg = "black")
self.text_label.pack()
self.fen.bind("<KeyPress>",self._key_pressed)
self.fen.bind("<KeyRelease>",self._key_released)
cadre.pack()
self.fen.protocol("WM_DELETE_WINDOW", self.stop) # Called when the window is closed
#self.fen.mainloop()
self.running = True
while self.running:
self.fen.update()
time.sleep(1.0/FPS) # Adjust FPS
def stop(self):
"Stop the window"
try: self.fen.destroy()
except: pass
try: self.fen.quit()
except: pass
self.running = False
return True
# Keys handler
def _key_pressed(self, action):
"Function which is called when a key is pressed"
if self.last_key != action.keysym : # Check if it's new key
self.last_key = action.keysym
if action.keysym in self.actions.keys(): # Check if we need to perform action
self.actions[action.keysym]()
def _key_released(self,action):
"Function called when the key is released"
if self.last_key != None:
self.last_key = None
self.default_action()
##################
### __MAIN__ ###
##################
if __name__ == "__main__":
print "> Welcome to " + str(prog_name) + " (r" + str(version) + ")"
print "> By Vianney Tran, Romain Fihue, Giulia Guidi, Julien Lagarde (under CC BY-SA 3.0 license)"
print "> Loading program ..."
gui = None
gui = ControlWindow(lambda arg=1:gui.change_text("No button pressed"))
gui.add_action("Up",lambda arg=gui:gui.change_text("Up Arrow pressed"))
gui.add_action("a",lambda arg=gui:arg.change_text("A button pressed"))
print "Press A or Up Arrow for an example"
gui.start()