-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcalibrate.coffee
286 lines (232 loc) · 7.26 KB
/
calibrate.coffee
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
# Firmware update
# (C) 2011 Nonolith Labs
# Author: Kevin Mehall <[email protected]>
# Distributed under the terms of the GNU GPLv3
app = (window.app ?= {})
app.init = (server, params) ->
server.connect()
server.disconnected.listen ->
$(document.body).html("Disconnected")
server.devicesChanged.listen (l) ->
app.chooseDevice()
app.chooseDevice()
log = (el, success) ->
c = {}
if success? then c = {color: if success then 'green' else 'red'}
$('<div>').append(el).css(c).appendTo('#log')
window.scrollTo(0, document.body.scrollHeight)
app.chooseDevice = ->
for dev in server.devices
if dev.model == 'com.nonolithlabs.cee'
return onCEE(server.selectDevice(dev))
else if dev.model == 'com.nonolithlabs.bootloader'
return onBootloaderDevice(server.selectDevice(dev))
testing_serial = null
startWithDevice = (dev) ->
unless testing_serial is dev.serial
testing_serial = dev.serial
log($("<h1>").text(dev.serial))
# Bootloader: flash code
onBootloaderDevice = (dev) ->
erase = (cb) ->
dev.erase (m) ->
log("Erased")
if $.isFunction(cb) then cb()
checkCRC = (cb) ->
dev.crcApp (m) ->
valid = (m.crc == firmware.crc)
vs = if valid then 'Valid' else 'INVALID'
console.log(m.crc, firmware.crc)
log("App CRC: #{m.crc} - #{vs}", valid)
if $.isFunction(cb) then cb(valid)
write = (cb) ->
server.device.write firmware.data, (m) ->
success = (m.result == 0)
log("Wrote flash, status #{m.result}", success)
if $.isFunction(cb) and success then cb()
flash_and_check = ->
checkCRC (v) ->
unless v
erase ->
write ->
checkCRC (v) ->
if v then server.device.reset()
else
server.device.reset()
dev.changed.subscribe ->
startWithDevice(dev)
flash_and_check()
# CEE
onCEE = (dev) ->
changedCount = 0
changedCb = null
dev.changed.subscribe ->
if changedCount == 0
startWithDevice(dev)
if dev.fwVersion isnt firmware.fwVersion
return server.send 'enterBootloader'
data.serial = dev.serial
data.hwVersion = dev.hwVersion
data.fwVersion = dev.fwVersion
data.time = new Date()
server.send 'tempCalibration'
log("Found CEE #{dev.hwVersion}, #{dev.fwVersion}")
runNextTest()
else
dev.startCapture()
changedCb()
changedCount += 1
changeRaw = (raw, cb) ->
console.log('changeRaw', raw, cb)
dev.configure({raw})
changedCb = cb
data = {a:{}, b:{}}
window.data = data
zeroOffset = ->
offsetAt = (setval, cb) ->
testStream = (channel, stream, cb) ->
stream.getSample 0.5, (d) ->
target = if stream.id is 'v'
setval/2
else
0
success = Math.abs(d-target) < 100
log("Channel #{channel.id} #{stream.id} offset at #{setval} is #{d} (#{d-target})LSB", success)
data[channel.id].offset ?= {}
data[channel.id].offset[setval] ?= {}
data[channel.id].offset[setval][stream.id] = d
cb()
dev.channels.a.setConstant 1, setval, ->
async.parallel [
(cb) -> testStream(dev.channels.a, dev.channels.a.streams.v, cb)
(cb) -> testStream(dev.channels.a, dev.channels.a.streams.i, cb)
(cb) -> testStream(dev.channels.b, dev.channels.b.streams.v, cb)
(cb) -> testStream(dev.channels.b, dev.channels.b.streams.i, cb)
], cb
async.series [
(cb) -> changeRaw(true, cb)
(cb) -> dev.channels.b.setConstant(3, 0, cb)
(cb) -> setTimeout(cb, 100)
(cb) -> offsetAt(0, cb)
(cb) -> offsetAt(3000, cb)
], ->
server.send 'tempCalibration',
offset_a_v: -Math.round(data['a'].offset[0]['v'])
offset_a_i: -Math.round(data['a'].offset[0]['i'])
offset_b_v: -Math.round(data['b'].offset[0]['v'])
offset_b_i: -Math.round(data['b'].offset[0]['i'])
runNextTest()
measureCSAError = ->
async.series [
(cb) -> changeRaw(false, cb)
(cb) ->
dev.channels.a.streams.i.setGain(32)
dev.channels.b.streams.i.setGain(32)
dev.channels.b.setConstant(3, 0)
dev.channels.a.setPeriodic(1, 'triangle', 5, 2.5, 2.5, cb)
(cb) ->
log("Measuring 9919 error")
streams = []
streamLabels = []
for chId, channel of dev.channels
for sId, stream of channel.streams
streams.push(stream)
streamLabels.push("#{chId}_#{sId}")
l = new server.DataListener(dev, streams)
l.configure(0, 0.4, 2000, false)
l.submit()
dev.startCapture()
l.done.subscribe ->
dev.channels.a.streams.i.setGain(1)
dev.channels.b.streams.i.setGain(1)
data.sweep = {}
unFloat = (a) ->
for i in [0...a.length]
a[i]
for i in [0...streamLabels.length]
data.sweep[streamLabels[i]] = unFloat(l.data[i])
data.sweep.time = unFloat(l.xdata)
log("done", true)
cb()
], runNextTest
calibrateIset = ->
calibrate = (channel, target, cb) ->
dacval = 3000
otherdac = 0
stepsize = 50
above = false
count = 0
step = ->
[daca, dacb] = if channel.id is 'a' then [dacval, otherdac] else [otherdac, dacval]
console.log("setting", Math.round(daca), Math.round(dacb))
dev.controlTransfer 0xC0, 0x15, Math.round(daca), Math.round(dacb), [], 0, ->
channel.streams.i.getSample 0.02, (d) ->
console.log("got", d)
if stepsize <= 1
log("ISET DAC #{channel.id} #{target}ma is #{dacval}", true)
data[channel.id].iset ?= {}
data[channel.id].iset[target] = dacval
return cb()
nabove = d > target
if above != nabove
stepsize /= 2
above = nabove
dacval = Math.round(dacval + if above then stepsize else -stepsize)
count += 1
if dacval < 1300 or count > 100
log("DACVAL too far, #{count}", false)
cb()
else
step()
step()
async.series [
#(cb) -> changeRaw(false, cb)
(cb) -> dev.channels.b.setConstant(1, 0, cb)
(cb) -> dev.channels.a.setConstant(1, 5, cb)
(cb) -> calibrate(dev.channels.a, 200, cb)
(cb) -> calibrate(dev.channels.a, 390, cb)
(cb) -> dev.channels.a.setConstant(1, 0, cb)
(cb) -> dev.channels.b.setConstant(1, 5, cb)
(cb) -> calibrate(dev.channels.b, 200, cb)
(cb) -> calibrate(dev.channels.b, 390, cb)
], runNextTest
writeEEPROM = (cb) ->
server.send 'writeCalibration',
offset_a_v: -Math.round(data['a'].offset[0]['v'])
offset_a_i: -Math.round(data['a'].offset[0]['i'])
offset_b_v: -Math.round(data['b'].offset[0]['v'])
offset_b_i: -Math.round(data['b'].offset[0]['i'])
dac200_a: data['a'].iset[200]
dac200_b: data['b'].iset[200]
dac400_a: data['a'].iset[390]
dac400_b: data['b'].iset[390]
id: server.createCallback ->
log("Wrote EEPROM", true)
cb()
tests = [
zeroOffset
measureCSAError
calibrateIset
]
runNextTest = ->
if tests.length
tests.shift()()
else
testingDone()
log("Testing complete", true)
writeEEPROM(->)
saveData()
testingDone = ->
dev.pauseCapture()
saveData = ->
console.log(data)
$.post 'http://localhost:1337/save', JSON.stringify(data), ->
log("Saved data", true)
app.set_fw = (fw) ->
window.firmware = fw
log("Loaded firmware #{firmware.fwVersion} for #{firmware.device} #{firmware.hwVersion}, CRC = #{firmware.crc}", true)
$(document).ready ->
app.init(server)
$.get('cee.json?'+new Date(), app.set_fw, 'json')
$(document.body).ajaxError (e) ->
log("Server request failed", false)