This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motorbench.py
executable file
·267 lines (220 loc) · 8.01 KB
/
motorbench.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
"""
Motorbench
Motor Sensors Reading from Arduino Uno MEGA
Copyright (C) 2017 Giovanni Grieco <giovanni.grc96@gmail.com>
Poliba Corse <polibacorse@poliba.it>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import csv
import ctypes
import math
import numpy as np
import platform
import re
import serial
import serial.tools.list_ports
import signal
from datetime import datetime
from matplotlib import pyplot as plt
# pyinstaller "Missing module" fix
import matplotlib.backends.backend_tkagg
"""Configuration
Just edit this dict with the correct vid and pid to endure proper
device identification.
"""
ARDUINO = {
'vid': 9025,
'pid': 67
}
"""Utilities for Motorbench
"""
class Helper():
"""Helper to handle indices and logical indices of NaNs.
Thanks to http://stackoverflow.com/a/6520696
Input:
- y, 1d np array with possible NaNs
Output:
- nans, logical indices of NaNs
- index, a function, with signature
indices = index(logical_indices),
to convert logical indices of NaNs to 'equivalent' indices
Example:
>>> # linear interpolation of NaNs
>>> nans, x= _nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
"""
def nan_helper(y):
return np.isnan(y), lambda z: z.nonzero()[0]
"""Helper to return a list without unknown values
"""
def clean_list(lst):
return [x for x in lst if x is not None]
"""Core object
"""
class Motorbench():
def __init__(self):
self._arduino = None
self._find_arduino()
self._serial_io = serial.Serial(
self._arduino.device,
9600,
bytesize=serial.EIGHTBITS)
self._init_plots()
self._revalues = re.compile(b'E([0-9\.]+)F([0-9\.]+)P([0-9\.]+)')
signal.signal(signal.SIGINT, self._save_graph)
self._run_loop()
"""Return a connected Arduino as PySerial ListPortInfo object.
Try to find Arduino using PySerial's Tools module.
Warn user and exit if any Arduino is found.
"""
def _find_arduino(self):
for port in serial.tools.list_ports.comports():
if port.vid == ARDUINO['vid'] and port.pid == ARDUINO['pid']:
self._arduino = port
if not self._arduino:
msg_err = 'Arduino not found.\n' \
'Please connect it and restart the program.'
if platform.system() == 'Windows':
ctypes.windll.user32.MessageBoxW(0, msg_err, 'Oops!', 0x10)
else:
print(msg_err)
exit(1)
"""Return void
Initialise matplotlib plots.
BUG: if someone closes the window, the program should exit, not
redraw itself incorrectly.
"""
def _init_plots(self):
"""
Flow and pressure data should be to the floor int value of
encoder. By empiric test we have a range from -8 to 362 degrees.
ENC_MIN (which is abs value) represents array bias to shift idx
to positive int.
"""
self._ENC_MIN = 8
self._ENC_MAX = 362
vector_dim = self._ENC_MAX + self._ENC_MIN
self._sensors = dict()
ENCODER_PLOT_ID = 311
FLOW_PLOT_ID = 312
PRESSURE_PLOT_ID = 313
plt.ion()
# fig = plt.figure()
#test_toolbar.TestToolbar(fig)
plt.subplot(ENCODER_PLOT_ID)
self._sensors['encoder'] = {
'ID': ENCODER_PLOT_ID,
# [0] is to have a fixed point so matplotlib can plot with
# multiple NaNs
'data': np.array([0] * vector_dim).astype(np.float)
}
self._sensors['encoder']['plot'] = plt.plot(
self._sensors['encoder']['data'])[0]
plt.title('Encoder')
plt.ylabel('degrees')
plt.xlabel('time')
plt.subplot(FLOW_PLOT_ID)
self._sensors['flow'] = {
'ID': FLOW_PLOT_ID,
'data': np.array(
[0] + [np.nan] * vector_dim).astype(np.float),
'plot': plt.plot([], [])[0]
}
plt.title('Flow')
plt.ylabel('kg/h')
plt.xlabel('degrees')
plt.subplot(PRESSURE_PLOT_ID)
self._sensors['pressure'] = {
'ID': PRESSURE_PLOT_ID,
'data': np.array(
[0] + [np.nan] * vector_dim).astype(np.float),
'plot': plt.plot([], [])[0]
}
plt.title('Pressure')
plt.ylabel('kPa')
plt.xlabel('degrees')
plt.tight_layout()
def _save_graph(self, signal, frame):
print('Saving session and exiting...')
session_time = datetime.now().isoformat(sep=' ', timespec='minutes')
self._sensors['flow']['data'].tofile(
session_time + '_flow.csv',
sep=',',
format='%10.5f')
self._sensors['pressure']['data'].tofile(
session_time + '_pressure.csv',
sep=',',
format='%10.5f')
exit(0)
"""Return list of sensor values from Arduino
"""
def _get_frame(self):
read = self._serial_io.readline().rstrip()
filtered = self._revalues.search(read)
return [
float(filtered.group(1)),
float(filtered.group(2)),
float(filtered.group(3))
]
def _update_encoder(self, value, flow, pressure):
plt.subplot(self._sensors['encoder']['ID'])
self._sensors['encoder']['data'] = np.append(
self._sensors['encoder']['data'], value)
self._sensors['encoder']['data'] = np.delete(
self._sensors['encoder']['data'], 0)
self._sensors['encoder']['plot'].set_xdata(
np.arange(len(self._sensors['encoder']['data'])))
self._sensors['encoder']['plot'].set_ydata(
self._sensors['encoder']['data'])
# autozoom
plt.ylim([
min(self._sensors['encoder']['data']) - 1,
max(self._sensors['encoder']['data']) + 1
])
plt.title('Encoder: ' + str(value) + '°')
def _update(self, value, encoder_value, sensor_name, title=['','']):
plt.subplot(self._sensors[sensor_name]['ID'])
self._sensors[sensor_name]['data'][
self._ENC_MIN + math.floor(encoder_value)] = value
"""
Interpolation
"""
nans, x = Helper.nan_helper(self._sensors[sensor_name]['data'])
data_with_nans = np.interp(
x(nans), x(~nans), self._sensors[sensor_name]['data'][~nans])
self._sensors[sensor_name]['plot'].set_xdata(
np.arange(len(data_with_nans)))
self._sensors[sensor_name]['plot'].set_ydata(data_with_nans)
"""
Autozoom feature
"""
plt.xlim([
min(self._sensors['encoder']['data']) - 1,
max(self._sensors['encoder']['data']) + 1
])
# get rid of NaNs when autozooming
nan_filtered = Helper.clean_list(self._sensors[sensor_name]['data'])
plt.ylim([
min(nan_filtered) - 0.5,
max(nan_filtered) + 0.5
])
plt.title(title[0] + str(value) + title[1])
def _run_loop(self):
while True:
encoder, flow, pressure = self._get_frame()
self._update_encoder(encoder, flow, pressure)
self._update(flow, encoder, 'flow', title=['Flow: ', ' kg/h'])
self._update(pressure, encoder, 'pressure',
title=['Pressure: ', ' kPa'])
plt.pause(0.2) # let matplotlib breathe fresh air
if __name__ == '__main__':
Motorbench()