-
Notifications
You must be signed in to change notification settings - Fork 0
/
strip_measurement.py
173 lines (146 loc) · 6.85 KB
/
strip_measurement.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
#!/usr/bin/env python
from __future__ import with_statement
from __future__ import division
from __future__ import absolute_import
import logging
from measurement_window import MeasurementThread, MeasurementWindow
import keithley
import agilent
import sys
from math import fabs
try:
from PyQt5 import QtWidgets as QtW
from PyQt5 import QtCore
except ImportError as e :
from PyQt4 import QtGui as QtW
from PyQt4 import QtCore
import os
import csv
import datetime
from time import sleep
from pyvisa.errors import VisaIOError, InvalidBinaryFormat
from io import open
from collections import OrderedDict
def getDateTimeFilename ( ) :
s = datetime.datetime.now ( ) .isoformat ( )
s = s.replace ( u":", u"_" )
s = s.replace ( u".", u"_" )
return s
class StripMeasurementThread ( MeasurementThread ) :
def __init__ ( self, args ) :
super ( StripMeasurementThread, self ) .__init__ ( args )
def run ( self ) :
args = self.args
fname = getDateTimeFilename ( )
output_csv = os.path.join ( str ( args.output_dir ), fname + u".csv" )
logger = logging.getLogger ( u'probestation.strip_measurement.StripMeasurementThread' )
logger.debug ( u" In strip_measurement.py:" )
try :
if args.devname_ardenv:
if not self._init_envsensor ( ):
errormsg = u"Could not open environment sensor device."
self.error_signal.emit ( errormsg )
logger.error ( errormsg )
self.finished.emit ( os.path.join ( str ( args.output_dir ), fname ) )
input_hv = keithley.KeithleyMeter ( args.devname_hv, args.serialenable )
if input_hv.identify ( ) .startswith ( u"KEITHLEY INSTRUMENTS INC.,MODEL 6517B" ) :
keith_hv = keithley.Keithley6517B ( args.devname_hv, args.serialenable )
elif input_hv.identify ( ) .startswith ( u"KEITHLEY INSTRUMENTS INC.,MODEL 2410" ) :
keith_hv = keithley.Keithley2410 ( args.devname_hv, args.serialenable )
else :
errormsg = u"Could not open devices."
self.error_signal.emit ( errormsg )
logger.error ( errormsg )
self.finished.emit ( os.path.join ( str ( args.output_dir ), fname ) )
return
logger.info ( u" Voltage source device introduced itself as {}" .format ( keith_hv.identify ( ) ) )
agilentE4980A = agilent.AgilentE4980A ( args.devname_agiE4980A, args.serialenable )
logger.info ( u"LCR meter introduced itself as {}" .format ( agilentE4980A.identify ( ) ) )
except VisaIOError :
errormsg = u"Could not open devices."
self.error_signal.emit ( errormsg )
logger.error ( errormsg )
self.finished.emit ( os.path.join ( str ( args.output_dir ), fname ) )
return
try :
logger.info ( u"Starting measurement" )
if args.resistance :
logger.info ( u"Resistance!" )
agilentE4980A.set_frequency ( args.frequency )
agilentE4980A.set_voltage_level ( args.deltavolt )
mode = 'w'
if sys.version_info.major < 3:
mode += 'b'
keith_hv.set_compliance ( args.compcurrent )
with open ( output_csv, mode ) as f :
if not args.resistance :
header = OrderedDict ( [ ( 'keihv_srcvoltage', None ), ( 'agie4980a_capacitance', None ), ( 'keihv_current', None ) ] )
else :
header = OrderedDict ( [ ( 'keihv_srcvoltage', None ), ( 'agie4980a_resistance', None ), ( 'agie4980a_impedance', None ), ( 'keihv_current', None ) ] )
if args.devname_ardenv:
header.update ( { 'envsensor1_temperature': None, 'envsensor1_dewpoint': None, 'envsensor2_temperature': None, 'envsensor2_dewpoint': None } )
writer = csv.DictWriter ( f, header, extrasaction = "ignore" )
writer.writeheader ( )
for keivolt in keith_hv.voltage_series ( args.start, args.end, args.step ) :
sleep ( args.sleep )
if self._exiting :
break
if not args.resistance :
line = agilentE4980A.get_reading ( )
meas = agilent.parse_cgv ( line, "agie4980a" )
meas["keihv_srcvoltage"] = keivolt
if ( not "keihv_srcvoltage" in meas or not "agie4980a_capacitance" in meas or not "agie4980a_conductance" in meas or meas["keihv_srcvoltage"] is None or meas["agie4980a_capacitance"] is None or meas["agie4980a_conductance"] is None ) :
raise IOError ( "Got invalid reading from device" )
compline = keith_hv.get_reading ( )
meas.update ( keith_hv.parse_iv ( compline, u"keihv" ) )
print ( "VSrc = {: 10.4g} V; C = {: 10.4g} F; G = {: 10.4g} S" .format ( meas["keihv_srcvoltage"], meas["agie4980a_capacitance"], meas["agie4980a_conductance"] ) )
else :
line = agilentE4980A.get_resistance ( )
meas = agilent.parse_res ( line, "agie4980a" )
meas["keihv_srcvoltage"] = keivolt
if ( not "keihv_srcvoltage" in meas or not "agie4980a_resistance" in meas or not "agie4980a_impedance" in meas or meas["keihv_srcvoltage"] is None or meas["agie4980a_resistance"] is None or meas["agie4980a_impedance"] is None ) :
raise IOError ( "Got invalid reading from device" )
compline = keith_hv.get_reading ( )
meas.update ( keith_hv.parse_iv ( compline, u"keihv" ) )
print ( "VSrc = {: 10.4g} V; R = {: 10.4g} O" .format( meas["keihv_srcvoltage"], meas["agie4980a_resistance"], meas["agie4980a_impedance"] ) )
if ( abs ( meas[u"keihv_current"] ) >= args.compcurrent ) :
self.error_signal.emit ( u"Compliance current reached" )
print ( u"Compliance current reached" )
#Instant turn off
keith_hv.set_output_state ( False )
self._exiting = True
if args.devname_ardenv:
env = self._measure_environment ( )
meas.update(env)
writer.writerow ( meas )
if not args.resistance :
self.measurement_ready.emit ( ( meas["keihv_srcvoltage"], meas["agie4980a_capacitance"] ) )
else :
self.measurement_ready.emit ( ( meas["keihv_srcvoltage"], meas["agie4980a_resistance"], meas["agie4980a_impedance"] ) )
if self._exiting :
break
except IOError as e :
errormsg = u"Error: {}" .format ( e )
self.error_signal.emit ( errormsg )
logger.error ( errormsg )
except ( VisaIOError, InvalidBinaryFormat, ValueError ) :
errormsg = u"Error during communication with devices."
self.error_signal.emit ( errormsg )
logger.error ( errormsg )
finally :
logger.info ( u"Stopping measurement" )
try :
keith_hv.stop_measurement ( )
except ( VisaIOError, InvalidBinaryFormat, ValueError ) :
logger.error ( u"Error during stopping. Trying to turn off output" )
keith_hv.set_output_state ( False )
self.finished.emit ( os.path.join ( str ( args.output_dir ), fname ) )
class StripMeasurementWindow ( MeasurementWindow ) :
def __init__ ( self, parent, args ) :
thread = StripMeasurementThread ( args )
super ( StripMeasurementWindow, self ) .__init__ ( parent, 2 if args.resistance else 1, args, thread )
if not args.resistance :
self._ylabel = [u"Capacitance in $\\mathrm{F}$", u"Conductance in $\\mathrm{S}$"]
else:
self._ylabel = [u"Resistance in $\\mathrm{Ohm}$", u"Impedance in $\\mathrm{Ohm}$"]
self.setWindowTitle ( u"Strip measurement" )