forked from wonjsohn/multC_tester
/
MainGui.py
325 lines (276 loc) · 12.7 KB
/
MainGui.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
# -*- coding: utf-8 -*-
"""
Module implementing Control.
"""
from PyQt4.QtGui import QDialog
from PyQt4.QtCore import pyqtSignature, pyqtSlot
from PyQt4.QtCore import QTimer, SIGNAL, SLOT, Qt, QRect
from PyQt4 import QtCore, QtGui
from Utilities import *
from generate_sin import gen as gen_sin
from generate_tri import gen as gen_tri
from generate_spikes import spike_train
from generate_sequence import gen as gen_ramp
from math import floor, pi
import types
from functools import partial
#from par_search import muscle_properties
from Utilities import convertType
class CtrlChannel:
def __init__(self, hostDialog, id, name, type, value = 0.0):
exec interp('self.currVal = #{value}')
self.type = type
self.id = id
self.defaultValue = value
self.doubleSpinBox = QtGui.QDoubleSpinBox(hostDialog)
self.doubleSpinBox.setGeometry(QtCore.QRect(230, id * 35, 105, 30))
self.doubleSpinBox.setProperty("value", value)
self.doubleSpinBox.setObjectName("param_"+name)
self.doubleSpinBox.setSingleStep(0.1)
self.doubleSpinBox.setMaximum(100000.0)
self.label = QtGui.QLabel(hostDialog)
self.label.setObjectName("label_"+name)
self.label.setText(name)
self.label.setGeometry(QtCore.QRect(350, id * 35, 105, 30))
def onNewWireIn(self, whichCh, value = -1):
if value == -1: value = self.ch_all[whichCh].doubleSpinBox.value()
#self.tellFpga(whichCh, value)
self.tellWhichFpga(0, whichCh, value)
print "board 0", whichCh, " is now ", value
self.tellWhichFpga(1, whichCh, value)
print "board 1", whichCh, " is now ", value
from Ui_Controls_Global import Ui_Dialog
class MultiDutTester(QDialog, Ui_Dialog):
"""
GUI class for feeding waveforms or user inputs to OpalKelly boards
"""
def __init__(self, nerfModel, dispView, rawChanList, halfCountRealTime, parent = None):
"""
Constructor
"""
QDialog.__init__(self, parent)
self.setupUi(self)
self.nerfModel = nerfModel
self.dispView = dispView
self.halfCountRealTime = halfCountRealTime
#self.dispView.setWindowTitle('board1')
#self.dispView[1].setWindowTitle('board2')
#self.dispView.show() #display board 1
#self.dispView[1].show() #display board 2
self.data = []
self.isLogData = False
#self.flexor_len = 0.75 #inital muscle length
self.jointAngle = 0.0 # initial joint
self.best_ForceDiff = 1.0 * 0xFFFF #inital muscle length difference (arbitrary)
self.start = False
# Prepare the widgets for each control channel to Fpga
self.ch_all = {}
for (id, name, type, value) in rawChanList:
self.ch_all[name] = CtrlChannel(hostDialog=self, id = id, name=name, type=type, value=value)
# VERY important: dynamically connect SIGNAL to SLOT, with curried arguments
for eachName, eachChan in self.ch_all.iteritems():
fn = partial(onNewWireIn, self, eachName) # Customizing onNewWireIn() into channel-specific
eachChan.doubleSpinBox.valueChanged.connect(fn)
eachChan.doubleSpinBox.editingFinished.connect(fn)
fn(eachChan.defaultValue)
self.on_horizontalSlider_valueChanged(5)
self.startSim()
def startSim(self):
# hold the simulation until start button is pushed
if self.start:
self.timer = QTimer(self)
self.connect(self.timer, SIGNAL("timeout()"), self.onTimer)
self.timer.start(REFRESH_RATE )
# self.on_horizontalSlider_sliderMoved(self, position):
def onTimer(self):
"""
Core function of Controller, which polls data from Model(fpga) and sends them to Viewer.
"""
newData_0 = {}
newData_1 = {}
for name, chan in self.dispView.ch_all.iteritems(): # Sweep thru channels coming out of Fpga
#newData[i] = self.nerfModel.ReadFPGA(DATA_OUT_ADDR[i], CH_TYPE[i])
# if i == 3:
# newData[i] = newData[i] / 100
#print chan.addr, chan.type
newData_0[name] = max(-16777216, min(16777216, self.nerfModel[0].ReadFPGA(chan.addr, chan.type)))
newData_1[name] = max(-16777216, min(16777216, self.nerfModel[1].ReadFPGA(chan.addr, chan.type)))
#print newData[0::6] # printing
# newSpike1 = self.nerfModel.ReadPipe(0xA0, 5000) # read ## bytes
# newSpike2 = self.nerfModel.ReadPipe(0xA1, 5000) # read ## bytes
# newSpike3 = self.nerfModel.ReadPipe(0xA2, 5000) # read ## bytes
# newSpike4 = self.nerfModel.ReadPipe(0xA3, 5000) # read ## bytes
# newSpike5 = self.nerfModel.ReadPipe(0xA4, 5000) # read ## bytes
newSpike1 = ""
newSpike2 = ""
newSpike3 = ""
newSpike4 = ""
newSpike5 = ""
#newSpike = "" # read ## bytes
# display output from FPGA
self.dispView.newDataIO(newData_0.values(), [newSpike1, newSpike2, newSpike3, newSpike4, newSpike5])
#self.dispView.newDataIO(newData_1.values(), [newSpike1, newSpike2, newSpike3, newSpike4, newSpike5])
# self.arm = muscle_properties(self.jointAngle)
#
# self.tellWhichFpga(0, 'bicep_len_pxi', self.arm.flexor_len)
# self.tellWhichFpga(1, 'bicep_len_pxi', self.arm.extensor_len)
#
# if abs(newData_0['f_force_bic'] - newData_1['f_force_bic'] )< self.best_ForceDiff: #
# self.best_ForceDiff = abs(newData_0['f_force_bic'] - newData_1['f_force_bic'] )
#
# print self.jointAngle*180/3.14, self.arm.flexor_len, self.arm.extensor_len, "|force:", newData_0['f_force_bic'], newData_1['f_force_bic'], self.best_ForceDiff , "UPDATE"
#
# if (self.isLogData):
# self.data.append(newData_0)
#
# # sweep the flexor length from xx to xx
# if self.jointAngle >= pi*115/180:
# self.jointAngle = pi*115/180
# else:
# one_degree = pi*1/180
# self.jointAngle = self.jointAngle + 0.2*one_degree
#print self.jointAngle*180/3.14, self.arm.flexor_len, self.arm.extensor_len, newData_0['f_force_bic'], newData_1['f_force_bic']
# def onTimer(self):
# """
# Core of Controller, polling data from Model(fpga) and sending to Viewer.
# """
# newData = []
#
# for name, chan in self.dispView.ch_all.iteritems(): # Sweep thru channels coming out of Fpga
# newData.append(max(-16777216, min(16777216, self.nerfModel.ReadFPGA(chan.addr, chan.type))))
# #print newData[0::6] # printing
# # newSpike1 = self.nerfModel.ReadPipe(0xA0, 5000) # read ## bytes
## newSpike2 = self.nerfModel.ReadPipe(0xA1, 5000) # read ## bytes
## newSpike3 = self.nerfModel.ReadPipe(0xA2, 5000) # read ## bytes
## newSpike4 = self.nerfModel.ReadPipe(0xA3, 5000) # read ## bytes
## newSpike5 = self.nerfModel.ReadPipe(0xA4, 5000) # read ## bytes
# newSpike1 = ""
# newSpike2 = ""
# newSpike3 = ""
# newSpike4 = ""
# newSpike5 = ""
#
# self.dispView.newDataIO(newData, [newSpike1, newSpike2, newSpike3, newSpike4, newSpike5])
# #self.dispView.newDataIO(newData, [])
# if (self.isLogData):
# self.data.append(newData)
def onClkRate(self, value):
""" value = how many times of 1/10 real-time
"""
# F_fpga = C * NUM_NEURON * V * F_emu , (C : cycles_per_neuron = 2, V = 365)
# if F_fpga = 200Mhz, F_emu = 1khz)
# halfcnt = F_fpga / F_neuron / 2 = F_fpga / (C * NUM_NEURON * V * F_emu) / 2
#self.clkRate = value
newHalfCnt = self.halfCountRealTime * 10 / value
print value, newHalfCnt
#print 'halfcnt=%d' %newHalfCnt
# print 'value=%d' %value
# return newHalfCnt # ???
#newValue = function(self, *args, **kw)
self.nerfModel[0].SendPara(bitVal = newHalfCnt, trigEvent = TRIG_CLKRATE)
self.nerfModel[1].SendPara(bitVal = newHalfCnt, trigEvent = TRIG_CLKRATE)
#self.nerfModel.SendPara(bitVal = newHalfCnt, trigEvent = TRIG_CLKRATE)
#
# def tellFpga(self, chanName, newWireIn):
# ctrl = self.ch_all[chanName] # Handle of the Tester channel
# ctrl.currValue = newWireIn
# if (ctrl.type == 'int32'):
# bitVal = convertType(floor(newWireIn), fromType = 'i', toType = 'I')
# elif (ctrl.type == 'float32'):
# bitVal = convertType(newWireIn, fromType = 'f', toType = 'I')
# bitVal2 = convertType(0.0, fromType = 'f', toType = 'I')
# self.nerfModel[0].SendMultiPara(bitVal1 = bitVal, bitVal2=bitVal2, trigEvent = ctrl.id)
def tellWhichFpga(self, xemNum, chanName, newWireIn):
ctrl = self.ch_all[chanName] # Handle of the Tester channel
ctrl.currValue = newWireIn
if (ctrl.type == 'int32'):
bitVal = convertType(floor(newWireIn), fromType = 'i', toType = 'I')
elif (ctrl.type == 'float32'):
bitVal = convertType(newWireIn, fromType = 'f', toType = 'I')
bitVal2 = convertType(0.0, fromType = 'f', toType = 'I') # velocity
self.nerfModel[xemNum].SendMultiPara(bitVal1 = bitVal, bitVal2=bitVal2, trigEvent = ctrl.id)
def plotData(self, data):
from pylab import plot, show, subplot
from scipy.io import savemat, loadmat
import numpy as np
if (data != []):
forplot = np.array(data)
for i in xrange(NUM_CHANNEL):
subplot(NUM_CHANNEL, 1, i+1)
plot(forplot[:, i])
show()
savemat("./matlab_cmn.mat", {"lce": forplot[:, 0], "Ia": forplot[:, 1], \
"II": forplot[:, 2], "force": forplot[:, 3], \
"emg": forplot[:, 4]})
@pyqtSignature("QString")
def on_comboBox_activated(self, p0):
"""
Slot documentation goes here.
"""
choice = p0
if choice == "Spike Train 1Hz":
# pipeInData = spike_train(firing_rate = 1)
pipeInData = gen_sin(F = 1.0, AMP = 100.0, T = 2.0)
elif choice == "Spike Train 10Hz":
# pipeInData = spike_train(firing_rate = 10)
# pipeInData = gen_sin(F = 4.0, AMP = 0.3)
pipeInData = gen_tri(T = 2.0)
elif choice == "Spike Train 20Hz":
# pipeInData = gen_tri()
pipeInData = gen_ramp(T = [0.0, 0.1, 0.2, 0.8, 0.9, 2.0], L = [1.0, 1.0, 1.3, 1.3, 1.0, 1.0], FILT = False)
# pipeInData = gen_ramp(T = [0.0, 0.4, 1.5, 1.55, 1.6, 2.0], L = [0, 0, 15000, 15000, 0, 0], FILT = False)
# pipeInData = gen_ramp(T = [0.0, 0.2, 0.25, 1.75, 1.8, 2.0], L = [1.0, 1.0, 5000.0, 5000.0, 1.0, 1.0], FILT = False) # abrupt rise / fall
# pipeInData = spike_train(firing_rate = 1000)
self.nerfModel[0].SendPipe(pipeInData)
@pyqtSignature("int")
def on_horizontalSlider_sliderMoved(self, position):
"""
Slot documentation goes here.
"""
self.onClkRate(position)
@pyqtSignature("bool")
def on_pushButton_2_clicked(self, checked):
"""
Slot documentation goes here.
"""
self.dispView.close()
# self.dispView[1].close()
self.plotData(self.data)
@pyqtSignature("int")
def on_horizontalSlider_valueChanged(self, value):
"""
Slot documentation goes here.
"""
self.onClkRate(value)
@pyqtSignature("bool")
def on_pushButton_5_clicked(self, checked):
"""
Toggle reset_sim, doesn't stop Fpga clock.
"""
newResetSim = checked
self.nerfModel[0].SendButton(newResetSim, BUTTON_RESET_SIM)
self.nerfModel[1].SendButton(newResetSim, BUTTON_RESET_SIM)
@pyqtSignature("bool")
def on_pushButton_4_clicked(self, checked):
"""
Slot documentation goes here.
"""
newResetGlobal = checked
self.nerfModel[0].SendButton(newResetGlobal, BUTTON_RESET)
self.nerfModel[1].SendButton(newResetGlobal, BUTTON_RESET)
@pyqtSignature("bool")
def on_pushButtonData_clicked(self, checked):
"""
Toggling data logging for Matlab use.
"""
self.isLogData = checked
# this button starts the simulation
@pyqtSignature("bool")
def on_pushButton_clicked(self, checked):
"""
Slot documentation goes here.
"""
self.start = True
self.startSim()
#self.nerfModel[0].SendButton(newStart, BUTTON_RESET)
#self.nerfModel[1].SendButton(newStart, BUTTON_RESET)