-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
148 lines (114 loc) · 4.58 KB
/
main.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
from threading import Thread
import tkinter as tk
import time
from calculations import Calculate
from crazyHealthTest import HealthTest
import cflib.crtp
from mainframe import MainFrame, LogFrame
from topframe import TopFrame
class Gui:
"""
Main GUI object that handles communication between the visual
layer and the Crazyflie.
It consists of a Top frame, which is the menu and a main frame,
which contains all the visual logs to the Crazyflie.
"""
def __init__(self, root):
self.topframe = TopFrame(root, self)
self.mainframe = MainFrame(root, self)
self.uris = {}
def add_uri(self, uri):
"""
Tries to add a URI to the listbox and make a new LogFrame
object out of it. Raises a warning message is URI already exists
"""
try:
self.mainframe.canvas.delete('warning')
finally:
if uri not in self.uris:
self.topframe.uribox.insert('end', uri)
row, column = Calculate.row_and_col(self.uris)
self.uris[uri] = LogFrame(self.mainframe.canvas, row, column, uri)
self.mainframe.update_canvas_scroll()
else:
self.mainframe.warning_msg('You already have that URI added')
def del_uris(self, uris):
""" Deletes selected uris and repositions the remaining ones """
for uri in uris:
if uri in self.uris:
self.uris[uri].delete()
self.uris.pop(uri)
for i, uri in enumerate(self.uris):
row, column = Calculate.new_placements(i)
self.uris[uri].delete()
self.uris[uri] = LogFrame(self.mainframe.canvas, row, column, uri)
def clear_uris(self):
for uri in self.uris:
self.uris[uri].delete()
self.uris = {}
def run_test(self, test):
""" Runs the chosen test on all Crazyflies, with their own threads """
if self.uris:
for uri in self.uris:
try:
self.uris[uri].reset_motors()
self.uris[uri].reset_battery()
except AttributeError:
pass
finally:
Thread(target=self.start, args=(uri, test)).start()
def start(self, uri, test):
cflib.crtp.init_drivers(enable_debug_driver=False)
cf = HealthTest(uri, self, test)
try:
cf.open_link()
cf.run_test()
finally:
cf.close_link()
def warning_msg(self, msg):
self.mainframe.warning_msg(msg)
# Callbacks from Crazyflie
def connecting(self, uri):
self.uris[uri].update_status_text('Connecting...')
def running_test(self, uri):
self.uris[uri].update_status_text('Running Test...')
def connected(self, uri):
self.uris[uri].clear_status_text()
self.uris[uri].show_motors()
self.uris[uri].show_battery()
def connection_failed(self, uri):
self.uris[uri].update_status_text('Connection Failed', fg='red')
def connection_lost(self, uri):
self.uris[uri].update_status_text('Connection Lost', fg='red')
def disconnected(self, uri):
self.uris[uri].update_status_text('Disconnected', fg='red')
def cb_logs(self, uri, motor_values, battery):
"""
Callback with all the logs from the Crazyflie
This gets sent to the LogFrame object that can represent
the values visually
"""
if not self.uris[uri].test_is_done:
motor_text = Calculate.motor_text(motor_values)
motor_fill = Calculate.motor_fill(motor_values)
b_text, b_fill = Calculate.battery(battery)
self.uris[uri].update_motors(motor_fill, motor_text)
self.uris[uri].update_battery(b_text, b_fill)
def hover_test_done(self, uri, means):
self.uris[uri].test_is_done = True
time.sleep(0.1)
results, colors = Calculate.is_mean_ok(means)
self.uris[uri].update_motor_status(results, colors)
def propeller_test_done(self, motorlog, uri):
self.uris[uri].test_is_done = True
time.sleep(0.1)
results, colors = Calculate.propeller_result(motorlog)
self.uris[uri].update_motor_status(results, colors)
if __name__ == '__main__':
root = tk.Tk()
off_y = int( (root.winfo_screenheight() - 600) / 4 )
off_x = int( (root.winfo_screenwidth() - 800) / 2 )
root.geometry("800x600+{}+{}".format(off_x, off_y))
root.title('Crazyflie Health Test')
gui = Gui(root)
root.mainloop()