Debian Codegraph @

pymeasure/instruments/lakeshore/lakeshore421.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 from pymeasure.instruments import Instrument
26 from pymeasure.instruments.validators import strict_discrete_set, \
27 truncated_discrete_set
28
29 import numpy as np
30 from time import time, sleep
31
32
33 class LakeShore421(Instrument):
34 """
35 Represents the Lake Shore 421 Gaussmeter and provides a high-level interface for interacting
36 with the instrument.
37
38 .. code-block:: python
39
40 gaussmeter = LakeShore421("COM1")
41 gaussmeter.unit = "T" # Set units to Tesla
42 gaussmeter.auto_range = True # Turn on auto-range
43 gaussmeter.fast_mode = True # Turn on fast-mode
44
45
46 A delay of 50 ms is ensured between subsequent writes, as the instrument cannot correctly
47 handle writes any faster.
48 """
49
50 MULTIPLIERS = {1e3: 'k', 1: '', 1e-3: 'm', 1e-6: 'n'}
51 PROBE_TYPES = {"High Sensitivity": 0,
52 "High Stability": 1,
53 "Ultra-High Sensitivity": 2}
54 RANGES = [30e3, 3e3, 300, 30] # in Gauss
55 RANGE_MULTIPLIER_PROBE = [1, 10, 0.01]
56 RANGE_MULTIPLIER_UNIT = {'G': 1, 'T': 1e-4}
57 UNITS = ['G', 'T']
58 WRITE_DELAY = 0.05
59
60 def __init__(self, adapter, name="Lake Shore 421 Gaussmeter", baud_rate=9600, **kwargs):
61 super().__init__(
62 adapter,
63 name,
64 asrl={'baud_rate': baud_rate, 'data_bits': 7, 'stop_bits': 10, 'parity': 1},
65 read_termination='\r',
66 write_termination='\n',
67 includeSCPI=False,
68 **kwargs
69 )
70 self.last_write_time = time()
71
72 def _raw_to_field(self, field_raw, multiplier_name):
73 if not field_raw == "OL":
74 multiplier = getattr(self, multiplier_name)
75 field = multiplier * field_raw
76 else:
77 field = np.nan
78
79 return field
80
81 def _field_to_raw(self, field, multiplier_name):
82 multiplier = getattr(self, multiplier_name)
83 return field / multiplier
84
85 field_raw = Instrument.measurement(
86 "FIELD?",
87 """ Returns the field in the current units and multiplier
88 """,
89 )
90
91 field_multiplier = Instrument.measurement(
92 "FIELDM?",
93 """ Returns the field multiplier for the returned magnetic field.
94 """,
95 values=MULTIPLIERS,
96 map_values=True,
97 )
98
99 @property
100 def field(self):
101 """ Returns the field in the current units. This property takes into
102 account the field multiplier. Returns np.nan if field is out of range.
103 """
104 return self._raw_to_field(self.field_raw, "field_multiplier")
105
106 unit = Instrument.control(
107 "UNIT?", "UNIT %s",
108 """ A string property that controls the units used by the gaussmeter.
109 Valid values are G (Gauss), T (Tesla). """,
110 validator=strict_discrete_set,
111 values=UNITS,
112 )
113
114 field_range_raw = Instrument.control(
115 "RANGE?", "RANGE %d",
116 """ A integer property that controls the field range of the
117 meter. Valid values are 0 (highest) to 3 (lowest). """,
118 validator=truncated_discrete_set,
119 values=range(4),
120 cast=int,
121 )
122
123 @property
124 def field_range(self):
125 """ A floating point property that controls the field range of the
126 meter in the current unit (G or T). Valid values are 30e3, 3e3, 300,
127 30 (when in Gauss), or 0.003, 0.03, 0.3, and 3 (when in Tesla).
128 """
129 probe_multiplier = self.RANGE_MULTIPLIER_PROBE[self.PROBE_TYPES[self.probe_type]]
130 unit_multiplier = self.RANGE_MULTIPLIER_UNIT[self.unit]
131 range = self.RANGES[self.field_range_raw]
132 return np.round(range * probe_multiplier * unit_multiplier, 3)
133
134 @field_range.setter
135 def field_range(self, range):
136 probe_multiplier = self.RANGE_MULTIPLIER_PROBE[self.PROBE_TYPES[self.probe_type]]
137 unit_multiplier = self.RANGE_MULTIPLIER_UNIT[self.unit]
138 ranges = np.array(self.RANGES) * probe_multiplier * unit_multiplier
139 range = truncated_discrete_set(range, values=ranges)
140 range = np.round(range / (probe_multiplier * unit_multiplier), 3)
141
142 self.field_range_raw = self.RANGES.index(range)
143
144 auto_range = Instrument.control(
145 "AUTO?", "AUTO %d",
146 """ A boolean property that controls the auto-range option of the
147 meter. Valid values are True and False. Note that the auto-range is
148 relatively slow and might not suffice for rapid measurements.
149 """,
150 validator=strict_discrete_set,
151 values={True: 1, False: 0},
152 map_values=True,
153 )
154
155 fast_mode = Instrument.control(
156 "FAST?", "FAST %d",
157 """ A boolean property that controls the fast-mode option of the
158 meter. Valid values are True and False. When enabled, the relative
159 mode, Max Hold mode, alarms, and autorange are disabled. """,
160 validator=strict_discrete_set,
161 values={True: 1, False: 0},
162 map_values=True,
163 )
164
165 field_mode = Instrument.control(
166 "ACDC?", "ACDC %d",
167 """ A string property that controls whether the gaussmeter measures
168 AC or DC magnetic fields. Valid values are "AC" and "DC". """,
169 validator=strict_discrete_set,
170 values={"DC": 0, "AC": 1},
171 map_values=True,
172 )
173
174 def zero_probe(self, wait=True):
175 """ Reset the probe value to 0. It is normally used with a zero gauss
176 chamber, but may also be used with an open probe to cancel the Earth
177 magnetic field. To cancel larger magnetic fields, the relative mode
178 should be used.
179
180 :param bool wait:
181 Wait for 20 seconds after issuing the command to allow the
182 resetting to finish.
183
184 """
185 self.write("ZCAL")
186 if wait:
187 sleep(20)
188
189 probe_type = Instrument.measurement(
190 "TYPE?",
191 """ Returns type of field-probe used with the gaussmeter. Possible
192 values are High Sensitivity, High Stability, or Ultra-High Sensitivity.
193 """,
194 values=PROBE_TYPES,
195 map_values=True,
196 )
197
198 serial_number = Instrument.measurement(
199 "SNUM?",
200 """ Returns the serial number of the probe. """
201 )
202
203 display_filter_enabled = Instrument.control(
204 "FILT?", "FILT %d",
205 """ A boolean property that controls the display filter to make it
206 more readable when the probe is exposed to a noisy field. The filter
207 function makes a linear average of 8 readings and settles in
208 approximately 2 seconds. """,
209 validator=strict_discrete_set,
210 values={True: 1, False: 0},
211 map_values=True,
212 )
213
214 front_panel_locked = Instrument.control(
215 "LOCK?", "LOCK %d",
216 """ A boolean property that locks or unlocks all front panel entries
217 except pressing the Alarm key to silence alarms. """,
218 validator=strict_discrete_set,
219 values={True: 1, False: 0},
220 map_values=True,
221 )
222
223 front_panel_brightness = Instrument.control(
224 "BRIGT?", "BRIGT %d",
225 """ An integer property that controls the brightness of the from panel
226 display. Valid values are 0 (dimmest) to 7 (brightest). """,
227 validator=strict_discrete_set,
228 values=range(8),
229 )
230
231 # MAX HOLD
232 max_hold_enabled = Instrument.control(
233 "MAX?", "MAX %d",
234 """ A boolean property that enables or disables the Max Hold function to
235 store the largest field since the last reset (with max_hold_reset). """,
236 validator=strict_discrete_set,
237 values={True: 1, False: 0},
238 map_values=True,
239 )
240
241 max_hold_field_raw = Instrument.measurement(
242 "MAXR?",
243 """ Returns the largest field since the last reset in the current units
244 and multiplier.
245 """,
246 )
247
248 max_hold_multiplier = Instrument.measurement(
249 "FIELDM?",
250 """ Returns the multiplier for the returned max hold field.
251 """,
252 values=MULTIPLIERS,
253 map_values=True,
254 )
255
256 @property
257 def max_hold_field(self):
258 """ Returns the largest field since the last reset in the current units.
259 This property takes into account the field multiplier. Returns np.nan if
260 field is out of range.
261 """
262 return self._raw_to_field(self.max_hold_field_raw, "max_hold_multiplier")
263
264 def max_hold_reset(self):
265 """ Clears the stored Max Hold value. """
266 self.write("MAXC")
267
268 # RELATIVE MODE
269 relative_mode_enabled = Instrument.control(
270 "REL?", "REL %d",
271 """ A boolean property that enables or disables the relative mode to
272 see small variations with respect to a given setpoint. """,
273 validator=strict_discrete_set,
274 values={True: 1, False: 0},
275 map_values=True,
276 )
277
278 relative_field_raw = Instrument.measurement(
279 "RELR?",
280 """ Returns the relative field in the current units and the current
281 multiplier. """,
282 )
283
284 relative_multiplier = Instrument.measurement(
285 "RELRM?",
286 """ Returns the relative field multiplier for the returned magnetic
287 field. """,
288 values=MULTIPLIERS,
289 map_values=True,
290 )
291
292 @property
293 def relative_field(self):
294 """ Returns the relative field in the current units. This property
295 takes into account the field multiplier. Returns np.nan if field is
296 out of range.
297 """
298 return self._raw_to_field(self.relative_field_raw, "relative_multiplier")
299
300 relative_setpoint_raw = Instrument.control(
301 "RELS?", "RELS %g",
302 """ Property that controls the setpoint for the relative field mode in
303 the current units and multiplier. """,
304 )
305
306 relative_setpoint_multiplier = Instrument.measurement(
307 "RELRM?",
308 """ Returns the multiplier for the setpoint field. """,
309 values=MULTIPLIERS,
310 map_values=True,
311 )
312
313 @property
314 def relative_setpoint(self):
315 """ Property that controls the setpoint for the relative field mode in
316 the current units. This takes into account the field multiplier. """
317 return self._raw_to_field(self.relative_setpoint_raw, "relative_setpoint_multiplier")
318
319 @relative_setpoint.setter
320 def relative_setpoint(self, value):
321 self.relative_setpoint_raw = self._field_to_raw(value, "relative_setpoint_multiplier")
322
323 # ALARM MODE
324 alarm_mode_enabled = Instrument.control(
325 "ALARM?", "ALARM %d",
326 """ A boolean property that enables or disables the alarm mode. """,
327 validator=strict_discrete_set,
328 values={True: 1, False: 0},
329 map_values=True,
330 )
331
332 alarm_audible = Instrument.control(
333 "ALMB?", "ALMB %d",
334 """ A boolean property that enables or disables the audible alarm
335 beeper. """,
336 validator=strict_discrete_set,
337 values={True: 1, False: 0},
338 map_values=True,
339 )
340
341 alarm_in_out = Instrument.control(
342 "ALMB?", "ALMB %d",
343 """ A string property that controls whether an active alarm is caused
344 when the field reading is inside ("Inside") or outside ("Outside") of
345 the high and low setpoint values. """,
346 validator=strict_discrete_set,
347 values={"Inside": 1, "Outside": 0},
348 map_values=True,
349 )
350
351 alarm_active = Instrument.measurement(
352 "ALMS?",
353 """ A boolean property that returns whether the alarm is triggered. """,
354 values={True: 1, False: 0},
355 map_values=True,
356 )
357
358 alarm_sort_enabled = Instrument.control(
359 "ALMSORT?", "ALMSORT %d",
360 """ A boolean property that enables or disables the alarm Sort Pass/Fail
361 function. """,
362 validator=strict_discrete_set,
363 values={True: 1, False: 0},
364 map_values=True,
365 )
366
367 alarm_low_raw = Instrument.measurement(
368 "ALML?", "ALML %g",
369 """ Property that controls the lower setpoint for the alarm mode in the
370 current units and multiplier. """,
371 )
372
373 alarm_low_multiplier = Instrument.measurement(
374 "ALMLM?",
375 """ Returns the multiplier for the lower alarm setpoint field. """,
376 values=MULTIPLIERS,
377 map_values=True,
378 )
379
380 @property
381 def alarm_low(self):
382 """ Property that controls the lower setpoint for the alarm mode in the
383 current units. This takes into account the field multiplier. """
384 return self._raw_to_field(self.alarm_low_raw, "alarm_low_multiplier")
385
386 @alarm_low.setter
387 def alarm_low(self, value):
388 self.alarm_low_raw = self._field_to_raw(value, "alarm_low_multiplier")
389
390 alarm_high_raw = Instrument.measurement(
391 "ALMH?", "ALMH %g",
392 """ Property that controls the upper setpoint for the alarm mode in the
393 current unit and multiplier. """,
394 )
395
396 alarm_high_multiplier = Instrument.measurement(
397 "ALMHM?",
398 """ Returns the multiplier for the upper alarm setpoint field. """,
399 values=MULTIPLIERS,
400 map_values=True,
401 )
402
403 @property
404 def alarm_high(self):
405 """ Property that controls the upper setpoint for the alarm mode in the
406 current units. This takes into account the field multiplier. """
407 return self._raw_to_field(self.alarm_high_raw, "alarm_high_multiplier")
408
409 @alarm_high.setter
410 def alarm_high(self, value):
411 self.alarm_high_raw = self._field_to_raw(value, "alarm_high_multiplier")
412
413 def shutdown(self):
414 """ Closes the serial connection to the system. """
415 self.adapter.connection.close()
416 super().shutdown()
417
418 ###################################################
419 # Redefined methods to ensure time between writes #
420 ###################################################
421
422 def delay_write(self):
423 if self.WRITE_DELAY is None:
424 return
425
426 while time() - self.last_write_time < self.WRITE_DELAY:
427 sleep(self.WRITE_DELAY / 10)
428
429 self.last_write_time = time()
430
431 def write(self, command):
432 self.delay_write()
433 super().write(command)