Debian Codegraph @

pymeasure/instruments/lakeshore/lakeshore_base.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 import logging
26 import warnings
27 import numpy as np
28 from time import sleep, time
29
30 from pymeasure.instruments import Instrument, Channel
31 from pymeasure.instruments.validators import strict_discrete_set
32
33 log = logging.getLogger(__name__)
34 log.addHandler(logging.NullHandler())
35
36
37 class LakeShoreTemperatureChannel(Channel):
38 """ Temperature input channel on a lakeshore temperature monitor. Reads the temperature in
39 kelvin, celsius, or sensor units. Also provides a method to block the program until a given
40 stable temperature is reached.
41 """
42
43 kelvin = Instrument.measurement(
44 'KRDG? {ch}',
45 """Read the temperature in kelvin from a channel."""
46 )
47 celsius = Instrument.measurement(
48 'CRDG? {ch}',
49 """Read the temperature in celsius from a channel."""
50 )
51 sensor = Instrument.measurement(
52 'SRDG? {ch}',
53 """Read the temperature in sensor units from a channel."""
54 )
55
56 @property
57 def celcius(self):
58 """Access celsius attribute with celcius (sic) property.
59
60 .. deprecated:: 0.14.0
61 Use celsius instead.
62 """
63 warnings.warn("`celcius` is deprecated, use `celsius` instead", FutureWarning)
64 return self.celsius
65
66 def wait_for_temperature(self, target, unit='kelvin', accuracy=0.1,
67 interval=1, timeout=360,
68 should_stop=lambda: False):
69 """ Blocks the program, waiting for the temperature to reach the target
70 within the accuracy (%), checking this each interval time in seconds.
71
72 :param target: Target temperature in kelvin, celsius, or sensor units.
73 :param unit: 'kelvin', 'celsius', or 'sensor' specifying the unit
74 for queried temperature values.
75 :param accuracy: An acceptable percentage deviation between the
76 target and temperature.
77 :param interval: Interval time in seconds between queries.
78 :param timeout: A timeout in seconds after which an exception is raised
79 :param should_stop: A function that returns True if waiting should stop, by
80 default this always returns False
81 """
82 abs_tolerance = target * (accuracy / 100)
83 target_reached = False
84 t = time()
85 while not target_reached:
86 reading = np.array([getattr(self, unit)])
87 target_reached = np.allclose(reading, target, atol=abs_tolerance)
88 sleep(interval)
89 if (time() - t) > timeout:
90 raise Exception((
91 "Timeout occurred after waiting %g seconds for "
92 "the LakeShore 331 temperature to reach %g %s."
93 ) % (timeout, target, unit))
94 if should_stop():
95 return
96
97
98 class LakeShoreHeaterChannel(Channel):
99 """ Heater output channel on a lakeshore temperature controller. Provides properties to query
100 the output power in percent of the max, set the manual output power, heater range, and PID
101 temperature setpoint.
102 """
103
104 output = Instrument.measurement(
105 'HTR? {ch}',
106 """Query the heater output in percent of the max."""
107 )
108 mout = Instrument.control(
109 'MOUT? {ch}',
110 'MOUT {ch},%f',
111 """Manual heater output in percent."""
112 )
113 range = Instrument.control(
114 'RANGE? {ch}',
115 'RANGE {ch},%i',
116 """String property controlling heater range, which can take the
117 values: off, low, medium, and high.""",
118 validator=strict_discrete_set,
119 values={'off': 0, 'low': 1, 'medium': 2, 'high': 3},
120 map_values=True)
121 setpoint = Instrument.control(
122 'SETP? {ch}', 'SETP {ch},%f',
123 """A floating point property that control the setpoint temperature
124 in the preferred units of the control loop sensor."""
125 )