-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv_measurement.py
147 lines (123 loc) · 5.59 KB
/
cv_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
#!/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
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 CvMeasurementThread ( MeasurementThread ) :
def __init__ ( self, args ) :
super ( CvMeasurementThread, 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.cv_measurement.CvMeasurementThread' )
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" )
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 :
header = OrderedDict ( [ ( 'keihv_srcvoltage', None ), ( 'agie4980a_capacitance', None ), ('agie4980a_conductance', 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, fieldnames = header, extrasaction = u"ignore" )
writer.writeheader ( )
for keivolt in keith_hv.voltage_series ( args.start, args.end, args.step ) :
sleep ( args.sleep )
if self._exiting :
break
line = agilentE4980A.get_reading ( )
meas = agilent.parse_cgv ( line, u"agie4980a" )
meas[u"keihv_srcvoltage"] = keivolt
if ( not u"keihv_srcvoltage" in meas or not u"agie4980a_capacitance" in meas or not u"agie4980a_conductance" in meas or meas[u"keihv_srcvoltage"] is None or meas[u"agie4980a_capacitance"] is None or meas[u"agie4980a_conductance"] is None ) :
raise IOError ( u"Got invalid reading from device" )
compline = keith_hv.get_reading ( )
meas.update ( keith_hv.parse_iv ( compline, u"keihv" ) )
if args.devname_ardenv:
env = self._measure_environment ( )
meas.update(env)
print ( u"VSrc = {: 10.4g} V; C = {: 10.4g} F; G = {: 10.4g} S; I = {: 10.4g} A" .format ( meas[u"keihv_srcvoltage"], meas[u"agie4980a_capacitance"], meas[u"agie4980a_conductance"], meas[u"keihv_current"] ) )
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
writer.writerow ( meas )
self.measurement_ready.emit ( ( meas[u"keihv_srcvoltage"], 1 / meas[u"agie4980a_capacitance"] ** 2 ) )
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 CvMeasurementWindow ( MeasurementWindow ) :
def __init__ ( self, parent, args ) :
thread = CvMeasurementThread ( args )
super ( CvMeasurementWindow, self ) .__init__ ( parent, 1, args, thread )
self._ylabel = [u"Capacitance${}^{-2}$ in $\\mathrm{F}^{-2}$", u"Conductance in $\\mathrm{S}$"]
self.setWindowTitle ( u"CV measurement" )