-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhidget_Calibrator.py
227 lines (191 loc) · 6.97 KB
/
Phidget_Calibrator.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
#! /usr/bin/python
"""Copyright 2011 Phidgets Inc.
This work is licensed under the Creative Commons Attribution 2.5 Canada License.
To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/
"""
__author__="Adam Stelmack"
__version__="2.1.8"
__date__ ="14-Jan-2011 2:29:14 PM"
"""
edited by Jon Renslo for the BDML at Stanford
Calibrates and records calibration constant for selected Phidget bridges
August, 2013
"""
#Basic imports
import sys, os
from time import time as getCurrentTime
from time import sleep
from datetime import *
#Phidget specific imports
from Phidgets.PhidgetException import PhidgetException
from Phidgets.Devices.Bridge import Bridge, BridgeGain
#Event Handler Callback Functions
def BridgeAttached(e):
attached = e.device
print("Bridge %i Attached!" % (attached.getSerialNum()))
def BridgeDetached(e):
detached = e.device
print("Bridge %i Detached!" % (detached.getSerialNum()))
def BridgeError(e):
# TODO add out of range error detection
try:
source = e.device
print("Bridge %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description))
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
def BridgeData(e):
# TODO add out of range error detection
try:
source = e.device
savedData.append(e.value)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
#Convenience functions
def displayDeviceInfo(bridge):
print("|------------|----------------------------------|--------------|------------|")
print("|- Attached -|- Type -|- Serial No. -|- Version -|")
print("|------------|----------------------------------|--------------|------------|")
print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (bridge.isAttached(), bridge.getDeviceName(), bridge.getSerialNum(), bridge.getDeviceVersion()))
print("|------------|----------------------------------|--------------|------------|")
print("Number of bridge inputs: %i" % (bridge.getInputCount()))
print("Data Rate Max: %d" % (bridge.getDataRateMax()))
print("Data Rate Min: %d" % (bridge.getDataRateMin()))
print("Input Value Max: %d" % (bridge.getBridgeMax(0)))
print("Input Value Min: %d" % (bridge.getBridgeMin(0)))
def setEnabledAllChannels(device, state):
for i in range(0,device.getInputCount()):
device.setEnabled(i, state)
def setGainAllChanels(device,gain):
for i in range(0,device.getInputCount()):
device.setGain(i,gain)
def openBridge(serialNum):
#Create a bridge object
try:
bridge = Bridge()
except RuntimeError as e:
print("Runtime Exception: %s" % e.details)
print("Exiting....")
exit(1)
try:
bridge.setOnAttachHandler(BridgeAttached)
bridge.setOnDetachHandler(BridgeDetached)
bridge.setOnErrorhandler(BridgeError)
bridge.setOnBridgeDataHandler(BridgeData)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
print("Calibration Script.\nOpening phidget object....")
try:
bridge.openPhidget(serialNum)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
print("Waiting for attach....")
try:
bridge.waitForAttach(20000)
print("Set data rate to %i ms ..." % (rate))
bridge.setDataRate(rate)
sleep(1)
print("Set Gain to %s..." % str(gainTable[gain]))
setGainAllChanels(bridge,gain)
sleep(1)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
try:
bridge.closePhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
print("Exiting....")
exit(1)
else:
displayDeviceInfo(bridge)
return bridge
def collectMeanVal(bridge,index):
#print("Enabling %i-%i"%(bridge.getSerialNum(),index))
bridge.setEnabled(index,True)
chr = raw_input("Press enter when ready\n")
global savedData
savedData = []
time = 4
print("Taking Data for %i seconds"%time)
sleep(time)
dataPart = savedData[:]
#print("Disabling %i-%i"%(bridge.getSerialNum(),index))
bridge.setEnabled(index,False)
return sum(dataPart)/float(len(dataPart))
def approveVal(val):
print("Is this constant okay? %f (y/n)"%val)
a = raw_input()
return str(a[0])=='y' if a else 0
#Main Program Code
startTime = getCurrentTime()
savedData = []
toTest = [293783, 293743, 293780, 293749, 293138, 293824] # bridges to test, identified by serial number.
gainTable = ['invalid',1,8,16,32,64,128,'unknown']
gain = BridgeGain.PHIDGET_BRIDGE_GAIN_8
rate = 8
#open output file
now = datetime.today().__str__()[:-7]
lNow = now.split()
lNow[1:1] = "_"
dirname = 'data'
if dirname not in os.listdir('.'):
os.makedirs(dirname)
os.chdir(dirname)
filename = 'Phidget_calibration_'+''.join(lNow)+'.csv'
print("Board indicies for reference:")
print("|0 3|\n| |\n|1 usb 2|")
weightInKG = raw_input("Enter the weight of the calibration load in KG\n")
try:
f = open(filename,'w')
except IOError as e:
print("error opening file. exiting...")
exit(1)
#metadata in first row: [dataRate, gain]
f.write(''+str(rate)+',' \
+str(gainTable[gain])+'\n')
for bridgeNum in toTest:
try:
bridge = openBridge(bridgeNum)
for cellIndex in range(4):
print("Calibrating bridge %i, index %i..."%(bridgeNum,cellIndex))
while 1:
print("Collecting zero value. Do not add weight")
zero = collectMeanVal(bridge,cellIndex)
print("Collecting calibrated value. Add weight now")
weighted = collectMeanVal(bridge,cellIndex)
const = float(weightInKG)/(weighted-zero)
if approveVal(const):
break
print("Repeating measurement...")
f.write(''+str(bridgeNum)+','+str(cellIndex)+','+str(const)+'\n')
print("Closing...")
bridge.closePhidget()
f.flush()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
try:
bridge.closePhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
print("Exiting....")
exit(1)
except IOError as e:
print("error writing file. Exiting...")
f.close()
try:
bridge.closePhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
exit(1)
f.close()
print("Output to data/%s"%filename)
exit(0)