-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepletion_monitor.py
209 lines (167 loc) · 8.72 KB
/
depletion_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
import datetime
import logging
import time
import sys
import csv
import matplotlib.pyplot as plt
class depletion_monitor():
def __init__(self, devices, simple, nwellring_current_limit=0.001, minimum_delay=0.1):
self.devices = devices
self.simple = simple
self.minimum_delay = minimum_delay
logging.info('Depletion monitor started!')
logging.info('bias_device is %s' % self.devices['bias'][0].get_name().rstrip())
if not self.simple:
logging.info('nwellring_device is %s' % self.devices['nwellring'][0].get_name().rstrip())
filestr = 'depletion_' + datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d_%H:%M:%S')
logfile = filestr + '.log'
self.datafile = filestr + '.csv'
logging.basicConfig(filename=logfile, filemode='wb', level=logging.INFO, format="%(asctime)s - %(name)s - [%(levelname)-8s] (%(threadName)-10s) %(message)s")
logging.info('Reset devices.')
self.ramp_down()
if not self.simple:
self.devices['nwellring'][0].set_current_limit(nwellring_current_limit)
self.devices['bias'][0].set_current_limit(0.105)
def get_current_reading(self, device):
if device[1] == 'keithley_2410':
return float(device[0].get_current().split(',')[1])
def get_voltage_reading(self, device):
if device[1] == 'keithley_2410':
return float(device[0].get_voltage().split(',')[0])
def ramp_down(self):
logging.info('Ramping down all voltages...')
done = {}
for key in self.devices.keys():
done[key] = False
while True:
for key in self.devices.keys():
value = int(self.get_voltage_reading(self.devices[key]))
if value != 0:
step = -1 if (value > 0) else 1
self.devices[key][0].set_voltage(value + step)
else:
done[key] = True
time.sleep(0.5)
if all([dev for dev in done.itervalues()]):
break
def ramp_up(self, device, max_value):
step = 1 if (max_value > 0) else -1
while True:
value = int(self.get_voltage_reading(device))
if value != max_value:
device[0].set_voltage(value + step)
def deplete(self, bias_voltage, polarity=1):
self.bias_voltage = bias_voltage
self.polarity = polarity
try:
bias_actual_voltage = self.get_voltage_reading(self.devices['bias'])
if not self.simple:
nwell_actual_voltage = self.get_voltage_reading(self.devices['nwellring'])
#TODO Angleichen?
actual_voltage = int(bias_actual_voltage)
if ((polarity == -1) and (actual_voltage > (bias_voltage * polarity))) or ((polarity == 1) and (actual_voltage < bias_voltage)):
for v in range(actual_voltage, polarity * (bias_voltage + 1), polarity):
time.sleep(0.5)
self.devices['bias'][0].set_voltage(v)
if not self.simple:
self.devices['nwellring'][0].set_voltage(v)
else:
for v in range(actual_voltage, polarity * (bias_voltage + 1), polarity * -1):
time.sleep(0.5)
self.devices['bias'][0].set_voltage(v)
if not self.simple:
self.devices['nwellring'][0].set_voltage(v)
except Exception as e:
logging.error('%s: %s' % (sys.exc_info()[0], e))
logging.error('An error occurred! Ramping down Voltage!')
self.ramp_down()
def monitor_current(self, waittime=10, record_nwellcurrent=False):
logging.info('Now monitoring current. Data file is ' + self.datafile)
try:
start = time.time()
with open(self.datafile, 'wb') as outfile:
dat = csv.writer(outfile ,quoting=csv.QUOTE_NONNUMERIC)
if record_nwellcurrent:
dat.writerow(['Absolute time', 'Relative time [s]', 'Nwell current [A]', 'Current [A]'])
else:
dat.writerow(['Absolute time', 'Relative time [s]', 'Current [A]'])
data = []
fig, axarr = plt.subplots(nrows=2, sharex=True)
fig.canvas.set_window_title('Bias Current Monitor')
plt.grid()
plt.xlabel('Time [s]')
plt.ylabel('Current [A]')
plt.ion()
plt.show()
loop = 0
while True:
loop += 1
event = []
event.append(datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'))
event.append(time.time() - start)
if not self.simple:
event.append(self.get_current_reading(self.devices['nwellring']))
event.append(self.get_current_reading(self.devices['bias']))
print event
data.append(event)
with open(self.datafile, 'a') as outfile:
dat = csv.writer(outfile ,quoting=csv.QUOTE_NONNUMERIC)
if record_nwellcurrent:
dat.writerow([event[0], event[1], event[2], event[3]])
else:
if not self.simple:
dat.writerow([event[0], event[1], event[3]])
else:
dat.writerow([event[0], event[1], event[2]])
if loop > 2:
x,y1,y2 = [], [], []
for ev in data:
x.append(ev[1])
y1.append(ev[2])
if not self.simple:
y2.append(ev[3])
plt.cla()
fig.canvas.set_window_title('Bias Current Monitor')
axarr[0].set_title('Diode Current')
axarr[1].set_title('NWell Current')
axarr[0].xaxis.grid(True,'major')
axarr[0].yaxis.grid(True,'major')
axarr[1].xaxis.grid(True,'major')
axarr[1].yaxis.grid(True,'major')
axarr[1].set_xlabel('Time [s]')
axarr[0].set_ylabel('Current [A]')
axarr[1].set_ylabel('Current [A]')
axarr[0].plot(x, y1, 'r-')
if not self.simple:
axarr[1].plot(x, y2, 'b-')
plt.pause(0.1)
time.sleep(waittime)
except Exception as e:
logging.error(sys.exc_info()[0] + ': ' + e)
logging.error('An error occurred! Ramping down Voltage!')
self.ramp_down()
except KeyboardInterrupt:
logging.info('Interrupt detected! Ramping down voltage...')
self.ramp_down()
logging.info('Done. Shutting down.')
if __name__ == '__main__':
import argparse
from basil.dut import Dut
parser = argparse.ArgumentParser(description='LF Diodes Depletion Monitor')
parser.add_argument('-y', '--yaml', help='Sourcemeter YAML file', required=False, default='devices.yaml')
parser.add_argument('-s', '--simple', help='Use only one Sourcemeter to apply bias voltage, leave n-well ring floating', action='store_true', required=False, default=True)
parser.add_argument('-b', '--biasdev', help='Bias device name in YAML file', required=False, default='Sourcemeter1')
parser.add_argument('-n', '--nwelldev', help='NWell ring device name in YAML file', required=False, default='Sourcemeter2')
parser.add_argument('-v', '--voltage', help='Bias voltage', required=True)
parser.add_argument('-r', '--recnwellcurrent', help='Record also the current to the NWellRing?', action='store_true', required=False, default=False)
args = parser.parse_args()
dut = Dut(args.yaml)
dut.init()
if args.simple:
devices = {'bias':[dut[args.biasdev], 'keithley_2410']}
else:
devices = {'bias':[dut[args.biasdev], 'keithley_2410'],
'nwellring':[dut[args.nwelldev], 'keithley_2410']}
mon = depletion_monitor(devices, simple=args.simple)
mon.deplete(bias_voltage=int(args.voltage))
mon.monitor_current(record_nwellcurrent=bool(args.recnwellcurrent))