-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrazyHealthTest.py
More file actions
200 lines (146 loc) · 5.86 KB
/
crazyHealthTest.py
File metadata and controls
200 lines (146 loc) · 5.86 KB
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
import logging
import time
from threading import Event
from cflib.crazyflie.log import LogConfig
from cflib.crazyflie import Crazyflie
from cflib.positioning.motion_commander import MotionCommander
logging.basicConfig(level=logging.ERROR)
class HealthTest:
"""
Represents the Crazyflie and handles its connection to the radio.
The callbacks are then passed to the main gui object which handles
the communication between the visual graphics.
"""
def __init__(self, uri, main_gui, test):
self.cf = Crazyflie(rw_cache='./cache')
self.uri = uri
self.main_gui = main_gui
self.link_is_open = False
self.is_hover_test_running = False
self.motor_means = [0, 0, 0, 0]
self.motor_mean_counter = 0
self._connect = Event()
self._logconfig = Event()
self.test = test
self.variance = {}
def open_link(self):
"""
Adds callbacks then tries to connect to the Crazyflie.
If no response, send an error msg to main_gui (GUI) and
remove callbacks, else, add log configurations.
"""
if self.link_is_open:
self.main_gui.warning_msg('Link is already open')
else:
self.add_callbacks()
self.main_gui.connecting(self.uri)
self.cf.open_link(self.uri)
self._connect.wait()
if not self.link_is_open:
self.remove_callbacks()
self.main_gui.warning_msg("Couldn't open link")
else:
self.add_logconfigs()
self._logconfig.wait()
return self
def run_test(self):
if self.test == 'propeller':
self.propeller_test()
else:
self.hover_test()
def hover_test(self):
"""
Takes off and hovers at x m for y seconds.
Default is 0.5m and 5 seconds.
A mean motor thrust for each motor is taken and
passed to the main gui for calculations.
"""
height = 0.5
duration = 5
self.main_gui.running_test(self.uri)
MotionCommander.VELOCITY = 0.8
with MotionCommander(self.cf, height) as mc:
self.is_hover_test_running = True
mc.stop()
time.sleep(duration)
self.hover_test_done()
def propeller_test(self):
self.main_gui.running_test(self.uri)
self.cf.param.set_value('health.startPropTest', '0')
time.sleep(0.2)
self.initial_counter = self.motor_pass_counter
self.cf.param.set_value('health.startPropTest', "1")
time.sleep(5)
while self.initial_counter == self.motor_pass_counter:
time.sleep(0.1)
self.propeller_test_done()
def hover_test_done(self):
try:
means = [(total / self.motor_mean_counter) for total in self.motor_means]
finally:
self.main_gui.hover_test_done(self.uri, means)
def propeller_test_done(self):
""" Sends the results to the main gui object """
self.main_gui.propeller_test_done(self.motorlog, self.uri)
def add_callbacks(self):
self.cf.connected.add_callback(self.connected)
self.cf.disconnected.add_callback(self.disconnected)
self.cf.connection_failed.add_callback(self.connection_failed)
self.cf.connection_lost.add_callback(self.connection_lost)
def remove_callbacks(self):
try:
self.cf.connected.remove_callback(self.connected)
self.cf.connection_lost.remove_callback(self.connection_lost)
self.cf.connection_failed.remove_callback(self.connection_failed)
self.cf.disconnected.remove_callback(self.disconnected)
except ValueError:
pass
def close_link(self):
self.cf.close_link()
self.remove_callbacks()
self.link_is_open = False
def add_logconfigs(self):
"""
The TOC's are motor thrust, battery level and results from propeller test
"""
self._log = LogConfig(self.uri, period_in_ms=50)
self._log.add_variable("pwm.m1_pwm", "uint32_t")
self._log.add_variable("pwm.m2_pwm", "uint32_t")
self._log.add_variable("pwm.m3_pwm", "uint32_t")
self._log.add_variable("pwm.m4_pwm", "uint32_t")
self._log.add_variable("pm.vbatMV", "uint16_t")
self._log.add_variable('health.motorTestCount', 'uint16_t')
self._log.add_variable('health.motorPass', 'uint8_t')
self._log.data_received_cb.add_callback(self.log_callback)
self.cf.log.add_config(self._log)
self._log.start()
self._logconfig.set()
def log_callback(self, timestamp, data, logconf):
m1, m2 = data['pwm.m1_pwm'], data['pwm.m2_pwm']
m3, m4 = data['pwm.m3_pwm'], data['pwm.m4_pwm']
motor_values = [m1, m2, m3, m4]
battery = data['pm.vbatMV']
self.motorlog = data['health.motorPass']
self.motor_pass_counter = data['health.motorTestCount']
if self.is_hover_test_running:
for i, data in enumerate(motor_values):
self.motor_means[i] += data
self.motor_mean_counter += 1
self.main_gui.cb_logs(self.uri, motor_values, battery)
# Connection callbacks from Crazyflie
def connected(self, callback):
self.link_is_open = True
self._connect.set()
self.main_gui.connected(self.uri)
def disconnected(self, *args):
self.main_gui.disconnected(self.uri)
def connection_failed(self, *args):
self.main_gui.connection_failed(self.uri)
def connection_lost(self, *args):
self.main_gui.connection_lost(self.uri)
def __enter__(self):
""" In case of use with wrapper """
self.open_link()
def __exit__(self, *args):
""" In case of use with wrapper """
self.close_link()