-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdacwrapper.py
212 lines (166 loc) · 8.37 KB
/
qdacwrapper.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
import qdac
import numpy as np
import pandas as pd
from IPython.display import display
from gui import GUI
class qdacChannel:
def __init__(self, qdac, number, gui, name, _qdacwrapper):
"""Class to represent individual channel of QDAC instrument. Created and
managed by qdacWrapper.
Args:
qdac: Actual QDAC object. This is defined in qdac.py which is
provided by QDevil directly.
number: Integer indicating which channel this corresponds to (1-48).
gui: Tkinter class defined in gui.py. This handles a table that is
displayed in a new window indicating the current status of each
channel (name and voltage).
name: Name of the channel. Used both in gui display and in
specifiying which channel to use in Measure class
(in measure.py).
_qdacwrapper:qdacWrapper object. This is required for each channel to
communicate back to the wrapper when voltages or names are changed.
Might not be necessary with better implementation of Measure class
(in measure.py) which communicates directly to the individual
channels.
"""
self.qdacInst = qdac
self.number = number
self.guiDisplay = gui
self._name = name
self._qdacWrapper = _qdacwrapper
self.voltage = 0
loc = [number, 1]
self.guiDisplay.update_name(loc, name)
return
def __repr__(self):
return 'QDAC Channel %s' % (self.number,)
def ramp(self, voltage):
"""Ramps this channel to the given voltage (V)
Args:
voltage: Voltage (in Volts) to ramp to
"""
#Uncomment below when actually connected to qdac
#self.qdacInst.setDCVoltage(channel = number, volts = voltage)
self.voltage = voltage
self._qdacWrapper.voltage_dict[self.number] = voltage
#Location of gui is (channel #, 2) -- 2 is for column of voltages
self.display_voltage([self.number, 2], voltage)
def display_voltage(self, loc, value):
"""Sends a value to be displayed by associated Tkinter gui.
Don't actually need loc input since it is static for a given channel,
but keeping for now depending on how this changes.
Args:
loc: location as a size 2 iterable that points to the associated
entry for this channel's voltage reading in the Tkinter gui
(see gui.py)
value: Value to display. Should usually just be the voltage output
"""
self.guiDisplay.submit_to_tkinter(loc, np.round(value,6))
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._qdacWrapper.channel_mapping[name] = self._qdacWrapper.channel_mapping.pop(self.name)
self._name = name
loc = [self.number, 1]
self.guiDisplay.update_name(loc, name)
class qdacWrapper:
"""Wrapper class for QDevil QDAC. Manages each individual channel through a
dictionary. Important aspects are the _multiChannel property and snapshot
method. Most of the other methods are now redundant by the way the Measure
class works and I should remove."""
location = '/dev/ttyUSB0'
qdacInst= 1
name = 'qdac'
_multiChannel = True
def __init__(self):
self.guiDisplay = GUI()
self.guiDisplay.start()
self.channel_mapping = {'qdac%s' % (n,):qdacChannel(qdac = self.qdacInst, number = n, gui = self.guiDisplay, name= 'qdac%s' % (n,), _qdacwrapper = self) for n in range(1,49)}
self.voltage_dict = {n: 0 for n in range(1,49)}
return
def _nameGate(self, name, channel):
if type(name) != str:
raise Exception("Please use a string for channel name")
if type(channel) != int or not (1 <= channel <= 48):
raise Exception("Please use an integer 1-48 for channel number")
chan_name = self._getName(channel)
print("Overriding %s = Channel %s to %s = Channel %s" % (chan_name, channel, name, channel))
self.channel_mapping[name] = self.channel_mapping.pop(chan_name) #Deletes old entry for name that corresponded to given channel
self.channel_mapping[name]._name = name
#Send updated name to GUI
number = self.channel_mapping[name].number
#Location is (number, 1) -- 1 is for the column of names
loc = [number, 1]
self.guiDisplay.update_name(loc, name)
return
def _ramp(self, channels, voltages):
"""Ramp selected channels to the given voltages. Input can be the Name or number of the channel and can also be given as an array"""
#Name can be the name given to the channel or the actual channel number itself
if type(channels) in {np.ndarray, list, tuple}:
if len(channels) != len(list(voltages)):
raise Exception("Different number of channels provided than voltages")
channels_list = self._convertChannels(channels) #Turns all channels input into array of integers that can be passed to QDAC
#with qdac.qdac(self.location) as q:
for i in range(len(channels_list)):
qdacInst.setDCVoltage(channel=channels_list[i], volts = voltages[i])
#self.voltage_list[channels_list[i]-1] = voltages[i]
self.voltage_dict[channels_list[i]] = voltages[i]
return
def _getChannel(self, name):
if self._nameExist(name):
return self.channel_mapping[name]
else:
raise Exception("No channel with the name %s exists!" % (name,))
def _getName(self, channel):
for name in self.channel_mapping:
if self.channel_mapping[name].number == channel:
return name
def _nameExist(self, name):
if type(name) != str:
raise Exception("Input %s is not a string!" % (name,))
return (name in self.channel_mapping.keys())
def _channelExist(self,channel):
if type(channel) != int:
raise Exception("Input %s is not an integer!" % (channel,))
return channel in self.channel_mapping.values()
def _convertChannels(self, channels):
"""Convert list of channels (given by name or number) into list of numbers"""
if len(channels) ==1:
channels = channels[0]
input_type = type(channels)
if input_type in {np.ndarray, list, tuple}:
return np.append(self._parseChannel(channels[0]), self._convertChannels(channels[1:]))
else:
return self._parseChannel(channels)
def _parseChannel(self, channel):
"""Parses input such that it returns the channel number given either name or number."""
input_type = type(channel)
if input_type == str:
return np.array([self._getChannel(channel)])
elif input_type == int and 1 <= channel <= 48:
return np.array([channel])
else:
raise Exception("Inputs should an existing Channel name or integer between 1 and 48")
def snapshot(self):
#Convert voltage table to pandas data frame
#channel_table = {'Channel Number': [], 'Channel Name': [], 'Voltage': []}
channel_table = {'Channel Name': [], 'Voltage': []}
channel_index = []
for i in range(1,49):
#channel_table['Channel Number'].append(i)
channel_index.append('Channel %s' % (i,))
channel_table['Voltage'].append(self.voltage_dict[i])
try:
channel_table['Channel Name'].append(self._getName(i))
except:
channel_table['Channel Name'].append('')
channel_df = pd.DataFrame(data=channel_table, index = channel_index)
#channel_df = channel_df[['Channel Number', 'Channel Name', 'Voltage']]
#Rearrange columns to ensure that Channel name comes before Voltage
channel_df = channel_df[['Channel Name', 'Voltage']]
return channel_df
def print_readable_snapshot(self):
print(self.name + ':\n')
display(self.snapshot())