calibrator.py 8.07 KB
Newer Older
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
#!/usr/bin/python3

import sys
import time
import numpy
import traceback
from serialio import SerialIO
import math

class Welford:

    def __init__(self, value):
        self.k = 0
        self.M = 0
        self.S = 0
        self.update(value)
        pass
    
    def update(self, value):
        self.k += 1
        newM = self.M + (value - self.M) / self.k
        newS = self.S + (value - self.M) * (value - newM)
        self.M = newM
        self.S = newS

    def mean(self):
        return self.M
    
    def stddev(self):
        if self.k==1:
            return 0
        return math.sqrt(self.S/(self.k-1))

    def __repr__(self):
        return "%f +- %f" % (self.mean(), self.stddev())
    

class ChinaIO:
    ANALOG_OUT_0 = 0
    ANALOG_OUT_1 = 1
    AIN_STEP = 20/(1<<18) # 20V, 18 bit resolution
    AOUT_STEP = 20/(1<<16) # 20V, 18 bit resolution

    def __init__(self, port):
        self.io = SerialIO(port)
        self.action_msg = ""
        self.status_msg = ""
        pass
    
    def close(self):
        self.io.setchannel(self.ANALOG_OUT_0, 0x8000)
        self.io.setchannel(self.ANALOG_OUT_1, 0x8000)

    def measure(self, analogIn, N=100, message=''):
        time.sleep(0.1)
        stat = {}
        for b in range(N):
            print(message, '[',  end='')
            for i,c in analogIn.items():
                value = (self.io.getchannel(i) - (1<<17)) * self.AIN_STEP
                if not i in stat:
                    stat[i] = Welford(value)
                    pass
                else:
                    stat[i].update(value)
                    pass
                print('%s ' % stat[i], end='')
                pass
            print(']', end='\r')
            pass
        print()
        return [ stat[i] for i in sorted(stat.keys()) ]

    def setCalibration(self, index, value):
        if value > 0:
            self.io.setchannel(31, (value << 8) | index & 0x3f)
            pass
        else:
            self.io.setchannel(31, (-value << 8) | 0x80 | index & 0x7f)
            pass
        pass
    
    def setVoltage(self, channel, value):
        self.io.setchannel(channel, int(value/ self.AOUT_STEP + (1<<15)))
        
    def expect(self, analogIn, ref, expect, N, message):
        for i,r in zip(range(100), ref):
            if r != None:
                self.setVoltage(i, r)
                pass
            pass
        time.sleep(0.1) # Let values settle
        sample = self.measure(analogIn, N=N, message=message)
        for e,v in zip(expect, sample):
            if e != None and (v.mean() < e[0] or e[1] < v.mean()):
                print("      Expected:", expect)
                return False
            pass
        return True
        
    def calibrate(self):
        # Calibration steps:
        #   1. Sanity check returned channels
        #   2. Set offset calibration to default value
        #   3. Read values from grounded inputs
        #   4. Connect AOut0 -> AIn0,AIn2 (+9V)
        #   5. Connect AOut1 -> AIn1,AIn3 (-9V)
        #   6. Zero adjust
        #   7. Determine min and max values
        
        # Sanity check
        analogIn = self.io.analogIn()
        analogOut = self.io.analogOut()
        if len(analogIn) != 4:
115
116
            raise Exception('Did not find 4 analogIn channels (%d)' %
                            (len(analogIn)))
117
        if len(analogOut) != 2:
118
119
            raise Exception('Did not find 2 analogOut channels (%d)'
                            (len(analogOut)))
120
121
122
123
        for i,c in analogIn.items():
            if c.bits != 18:
                raise Exception('analogIn[%d] is not 18 bit (%d)' % (i, c.bits))
            pass
124
125
126
127
        for i,c in analogOut.items():
            if c.bits != 16:
                raise Exception('analogOut[%d] is not 16 bit (%d)' % (i, c.bits))
            pass
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

        # Set offset calibration to default value
        # self.io.setchannel(31, (0x8000 << 8) | 0x00)
        # self.io.setchannel(31, (0x8000 << 8) | 0x00)
        self.setCalibration(0, 0x8000)
        self.setCalibration(1, 0x8000)

        # Read values from grounded inputs
        while not self.expect(analogIn,
                              [ -9, 9 ], 
                              [ (-0.1, 0.1), (-0.1, 0.1),
                                (-0.1, 0.1), (-0.1, 0.1) ],
                              N=500,
                              message='Ground all input pins'):
            pass

        # Connect AOut0 -> AIn0,AIn2 (+9V)
        while not self.expect(analogIn,
                              [ 9, None ], 
                              [ (8.7,9.3), None,
                                (8.7,9.3), None ],
                              N=500,
                              message='Connect: AOut0 -> AIn0, AIn2'):
            pass

        # Connect AOut1 -> AIn1,AIn3 (-9V)
        while not self.expect(analogIn,
                              [ None, -9 ], 
                              [ None, (-9.3,-8.7),
                                None, (-9.3,-8.7) ],
                              N=500,
                              message='Connect: AOut1 -> AIn1, AIn3'):
            pass

        # Zero adjust
        while not self.expect(analogIn,
                              [ 0, 0 ], 
                              [ (-0.1, 0.1), (-0.1, 0.1),
                                (-0.1, 0.1),(-0.1, 0.1) ],
                              N=500,
                              message='Calibrating zero'):
            pass

        #   5. Zero adjust
        v0 = 0x8000
        v1 = 0x8000
        delta = 0x4000
        self.io.setchannel(0, 0x8000)
        self.io.setchannel(1, 0x8000)
        self.setCalibration(0, v0)
        self.setCalibration(1, v1)
        sample = self.measure(analogIn, N=100, message='Sampling baseline')
        def nextv(oldv, delta, sample):
            if sample.mean() > 0:
                return oldv + delta
            else:
                return oldv - delta
            pass
        candidate = [[], []]
        while delta >= 1:
            v0 = nextv(v0, delta, sample[0])
            v1 = nextv(v1, delta, sample[1])
            self.setCalibration(0, v0)
            self.setCalibration(1, v1)
            sample = self.measure(analogIn, N=200,
                                  message='Zero (v0=%x, v1=%x)' % (v0, v1))
            candidate[0].append([v0, sample[0]])
            candidate[1].append([v1, sample[1]])
            delta = delta // 2
            pass
        best = [ sorted(candidate[0], key=lambda x: abs(x[1].mean())),
                 sorted(candidate[1], key=lambda x: abs(x[1].mean())) ]
        print("Done %x %x : %x %x" % (v0, v1, best[0][0][0], best[1][0][0]))
        v0 = best[0][0][0]
        v1 = best[0][0][0]
        self.setCalibration(0, v0)
        self.setCalibration(1, v1)

        # Determine min and max values
        self.setVoltage(0, -9)
        self.setVoltage(1, -9)
        min_sample = self.measure(analogIn, N=200, message='Min (-9V)')
        self.setVoltage(0, 9)
        self.setVoltage(1, 9)
        max_sample = self.measure(analogIn, N=200, message='Max (+9V)')
        print("0: Min: %f, Max: %f" % (min_sample[0].mean() / 9 * 10,
                                       max_sample[0].mean() / 9 * 10))
        print("1: Min: %f, Max: %f" % (min_sample[1].mean() / 9 * 10,
                                       max_sample[1].mean() / 9 * 10))
        def millivolt(v):
            return int(v * 1000)
        self.setCalibration(2, millivolt(min_sample[0].mean() / 9 * 10))
        self.setCalibration(3, millivolt(max_sample[0].mean() / 9 * 10))
        self.setCalibration(4, millivolt(min_sample[1].mean() / 9 * 10))
        self.setCalibration(5, millivolt(max_sample[1].mean() / 9 * 10))
        self.setCalibration(6, 0);
        pass
        
    def write_calibration(self, index, value):
        if value < 0:
            tmp = (int(-value * 1000) << 8) | 0x80 | index
        else:
            tmp = (int(value * 1000) << 8) | index
        print("%d %8x" % (index, tmp))
        self.io.setchannel(31, tmp, 0xffffffff)
        
if __name__ == "__main__":
    io = ChinaIO(sys.argv[1])
    try:
        io.calibrate()
    except:
        traceback.print_exc()
        pass

    io.close()