-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbldc_monitor.py
executable file
·352 lines (287 loc) · 9.7 KB
/
bldc_monitor.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
#!/usr/bin/python
import numpy as np
import serial
import crcmod
import struct
import time
import datetime
import plotly.plotly as py
from optparse import OptionParser
import json
import signal
import sys
import csv
class SerialConnection:
def openConnection(self,port,baudrate):
self.ser = serial.Serial(port,baudrate,timeout=0.05,writeTimeout=0)
time.sleep(1.0)
if self.ser.isOpen():
print "Serial port opened."
## This function is temporary for testing purposes.
def readMessageFake(self):
formatString = 'HHfff'
return np.random.normal(size=struct.calcsize(formatString))
def read(self,length):
preamble = ['\xFF','\xFA'];
input = [0x00,0x00]
input[1] = self.ser.read(1)
while ( True ):
if ( input == preamble ):
break
else:
input[0] = input[1]
input[1] = self.ser.read(1)
while ( self.ser.inWaiting() < length + 2 ):
pass
#Get data
data = self.ser.read(length)
#Get checksum
input = self.ser.read(2)
checksum = struct.unpack('H',input)[0]
#Verify checksum
crc16 = crcmod.mkCrcFun(0x11021,0xFFFF,True)
calcChecksum = crc16(data)
calcChecksum = (~calcChecksum) % 2**16 # convert to uint16_t
if ( checksum != calcChecksum ):
print "Failed checksum."
return
return data
class PlotlyPlotter:
def initPlotly(self):
with open('./config.json') as config_file:
plotly_user_config = json.load(config_file)
username = plotly_user_config['plotly_username']
api_key = plotly_user_config['plotly_api_key']
stream_tokens = plotly_user_config['plotly_streaming_tokens']
stream_server = 'http://stream.plot.ly'
py.sign_in(username, api_key)
numPoints = 450
trace0 = {'x':[],'y':[],'type': 'scatter', 'stream': {'token': stream_tokens[0], 'maxpoints': numPoints}}
trace1 = {'x':[],'y':[],'yaxis':'y2','type': 'scatter', 'stream': {'token': stream_tokens[1], 'maxpoints': numPoints}}
trace2 = {'x':[],'y':[],'yaxis':'y3','type': 'scatter', 'stream': {'token': stream_tokens[2], 'maxpoints': numPoints}}
trace3 = {'x':[],'y':[],'yaxis':'y4','type': 'scatter', 'stream': {'token': stream_tokens[3], 'maxpoints': numPoints}}
xAxisStyle = {'title':'Time'}
domainHeight = 0.22
domainGap = (1.0-4*domainHeight)/3.0
layout1 = { 'title':'Thruster-100 Live Test Data',
'showlegend': False,
'xaxis':xAxisStyle,
'yaxis':{'domain':[0,domainHeight],'title':'Voltage (V)'},
'yaxis2':{'domain':[domainHeight+domainGap,2*domainHeight+domainGap],'title':'Power (W)'},
'yaxis3':{'domain':[2*domainHeight+2*domainGap,3*domainHeight+2*domainGap],'title':'RPM'},
'yaxis4':{'domain':[3*domainHeight+3*domainGap,4*domainHeight+3*domainGap],'title':'Thrust (lb)'}}
r = py.iplot({'data':[trace0, trace1, trace2, trace3],'layout':layout1},filename='Thruster-Stream',fileopt='overwrite')
print r
self.s0 = py.Stream(stream_tokens[0])
self.s1 = py.Stream(stream_tokens[1])
self.s2 = py.Stream(stream_tokens[2])
self.s3 = py.Stream(stream_tokens[3])
self.s0.open()
self.s1.open()
self.s2.open()
self.s3.open()
def closePlotly(self):
self.s0.close()
self.s1.close()
self.s2.close()
self.s3.close()
def streamToPlotly(self,data):
timeStamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
trace0 = {'x': timeStamp, 'y': data[0]}
trace1 = {'x': timeStamp, 'y': data[1]}
trace2 = {'x': timeStamp, 'y': data[2]}
trace3 = {'x': timeStamp, 'y': data[3]}
self.s0.write(trace0)
self.s1.write(trace1)
self.s2.write(trace2)
self.s3.write(trace3)
class CumulativeTimeMeter:
def init(self):
self.startTime = time.time()
self.fileName = 'hourmeter.log'
with open(self.fileName,'r') as f:
try:
oldRecord = json.load(f)
except ValueError, e:
oldRecord = {'cumulativeTime':0,'cumulativeRPSxTime':0,'cumulativeThrustxTime':0}
with open(self.fileName,'w') as f2:
json.dump(oldRecord,f2)
self.oldTime = oldRecord['cumulativeTime']
self.oldRPSTime = oldRecord['cumulativeRPSxTime']
self.oldThrustTime = oldRecord['cumulativeThrustxTime']
self.time = 0
self.rpsTime = 0
self.thrustTime = 0
self.lastTime = time.time()
def getCumulativeTime(self):
return self.time+self.oldTime
def getCumulativeRPSxTime(self):
return self.rpsTime + self.oldRPSTime
def getCumulativeThrustxTime(self):
return self.thrustTime + self.oldThrustTime
def meterTime(self,isMetering,rpm,thrust):
delta = time.time() - self.lastTime
self.lastTime = time.time()
if isMetering:
self.time += delta
self.rpsTime += delta*rpm/60.0
self.thrustTime += delta*thrust
def recordCumulativeTime(self):
record = {'cumulativeTime':self.getCumulativeTime(),'cumulativeRPSxTime':self.getCumulativeRPSxTime(),'cumulativeThrustxTime':self.getCumulativeThrustxTime()}
with open(self.fileName,'w') as f:
json.dump(record,f)
#######################################
meter = CumulativeTimeMeter()
meter.init()
sercon = SerialConnection()
parser = OptionParser()
parser.add_option("-o","--output",dest="filename",help="Output file name.",metavar="FILE")
parser.add_option("-p","--port",dest="portname")
(options,args) = parser.parse_args()
isFile = False
if options.filename:
csvfile = open(options.filename,'w')
csvwriter = csv.writer(csvfile,delimiter=',')
isFile = True
connected = False
try:
sercon.openConnection(options.portname,19200)
connected = True
except:
connected = False
print "Error connecting to serial port. Defaulting to random data."
plotter = PlotlyPlotter()
plotter.initPlotly()
import curses
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)
stdscr.nodelay(1)
stdscr.addstr(0,0,"Thrust Test Stand")
stdscr.addstr(1,0,"============================")
stdscr.addstr(3,0,"Adjust motor:\tUp/down, pgup/pgdown, scroll wheel")
stdscr.addstr(4,0,"Stop motor: \tSPACE")
stdscr.addstr(5,0,"Quit: \t\tq")
if connected:
stdscr.addstr(15,0,"Live serial data.")
else:
stdscr.addstr(15,0,"Simulated data.")
stdscr.refresh()
command = 1500
streamCount = 0
values = []
def sigint_handler(*args):
print "Closing serial port."
sercon.ser.close()
print "Quiting gracefully."
time.sleep(0.25)
sys.exit()
def getMotorFromTerminal():
global command
key = stdscr.getch()
if key == curses.KEY_UP:
command += 5
if command > 1900:
command = 1900
elif key == curses.KEY_DOWN:
command -= 5
if command < 1100:
command = 1100
elif key == curses.KEY_PPAGE:
command += 50
if command > 1900:
command = 1900
elif key == curses.KEY_NPAGE:
command -= 50
if command < 1100:
command = 1100
elif key == ord(' '):
command = 1500
elif key == ord('q'):
command = 1500
if connected:
sercon.ser.write(chr(command))
plotter.closePlotly()
curses.endwin()
sercon.ser.close()
exit()
stdscr.addstr(8,5,"Motor command: "+str(command)+" ")
def readSerial():
global values
if connected:
values = sercon.readMessage()
else:
values = sercon.readMessageFake()
def updateCurses():
stdscr.addstr(10,5,"Thrust:\t%10.2f lb\t%10.0f g"%(values[5],values[5]*453.6))
stdscr.addstr(11,5,"RPM:\t%10.0f rev/min"%(values[0]))
stdscr.addstr(12,5,"Power:\t%10.0f W"%(values[2]*values[4]))
stdscr.addstr(13,5,"Voltage:\t%10.2f V"%(values[4]))
stdscr.addstr(14,5,"PWM: \t%g\t%g us"%(values[6],values[7]))
stdscr.addstr(16,0,"Plotly Stream Data Points Sent: %10.0f"%(streamCount))
def updatePlotly():
global values
if values is not None:
global streamCount
streamCount += 1
plotter.streamToPlotly(values)
def updateHourMeter():
global values
isMetering = False
if values[0] > 60:
isMetering = True
meter.meterTime(isMetering,values[0],values[5])
stdscr.addstr(20,0,"Cumulative Running Time:\t%s"%(str(datetime.timedelta(seconds=meter.getCumulativeTime()))))
stdscr.addstr(21,0,"Cumulative Revolutions:\t\t%10.0f"%(meter.getCumulativeRPSxTime()))
stdscr.addstr(22,0,"Average Thrust:\t\t\t%10.2f"%(meter.getCumulativeThrustxTime()/(meter.getCumulativeTime()+0.001)))
meter.recordCumulativeTime()
if __name__ == '__main__':
signal.signal(signal.SIGINT, sigint_handler)
lastCommandUpdate = time.time()
lastCommand = 0
lastSerialRead = time.time()
lastPlotlyUpdate = time.time()
lastMeterRecord = time.time()
errorCount = 0
formatString = 'hhffffhh'
bldcdata = []
if isFile:
csvwriter.writerow(["rpma","rpmb","currentA","currentB","voltage","thrust","pwmA","pwmB"])
sendCount = 0
while True:
if time.time() - lastSerialRead > 0.05:
lastSerialRead = time.time()
length = struct.calcsize(formatString)
bldcdata = sercon.read(length)
if bldcdata is not None:
values = struct.unpack(formatString,bldcdata)
updateCurses()
if isFile:
csvwriter.writerow(values)
if False and (time.time() - lastPlotlyUpdate > 0.50):
try:
updatePlotly()
except IOError:
errorCount += 1
time.sleep(5)
lastPlotlyUpdate = time.time()
if False and time.time() - lastMeterRecord > 0.20:
lastMeterRecord = time.time()
updateHourMeter()
getMotorFromTerminal()
getMotorFromTerminal()
if (( time.time() - lastCommandUpdate > 0.1 and command is not lastCommand ) or (time.time()-lastCommandUpdate > 0.5)) and connected:
stdscr.addstr(30,0,"Sent: \t\t\t%g"%(sendCount))
sendCount += 1
sercon.ser.write('\xFF')
sercon.ser.write('\xFA')
txData = struct.pack('HH',command,command)
crc16 = crcmod.mkCrcFun(0x11021,0xFFFF,True)
calcChecksum = crc16(txData)
calcChecksum = (~calcChecksum) % 2**16 # convert to uint16_t
for i in range(len(txData)):
sercon.ser.write(txData[i])
sercon.ser.write(struct.pack('H',calcChecksum)[0])
sercon.ser.write(struct.pack('H',calcChecksum)[1])
lastCommand = command
lastCommandUpdate = time.time()