Debian Codegraph @

pymeasure/instruments/yokogawa/yokogawa7651.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 import logging
25 from time import sleep
26 import re
27
28 import numpy as np
29
30 from pymeasure.instruments import Instrument, SCPIUnknownMixin
31 from pymeasure.instruments.validators import (
32 truncated_discrete_set, strict_discrete_set,
33 truncated_range
34 )
35
36 log = logging.getLogger(__name__)
37 log.addHandler(logging.NullHandler())
38
39
40 class Yokogawa7651(SCPIUnknownMixin, Instrument):
41 """ Represents the Yokogawa 7651 Programmable DC Source
42 and provides a high-level for interacting with the instrument.
43
44 .. code-block:: python
45
46 yoko = Yokogawa7651("GPIB::1")
47
48 yoko.apply_current() # Sets up to source current
49 yoko.source_current_range = 10e-3 # Sets the current range to 10 mA
50 yoko.compliance_voltage = 10 # Sets the compliance voltage to 10 V
51 yoko.source_current = 0 # Sets the source current to 0 mA
52
53 yoko.enable_source() # Enables the current output
54 yoko.ramp_to_current(5e-3) # Ramps the current to 5 mA
55
56 yoko.shutdown() # Ramps the current to 0 mA and disables output
57
58 """
59
60 @staticmethod
61 def _find(v, key):
62 """ Returns a value by parsing a current panel setting output
63 string array, which is returned with a call to "OS;E". This
64 is used for Instrument.control methods, and should not be
65 called directly by the user.
66 """
67 status = ''.join(v.split("\r\n\n")[1:-1])
68 keys = re.findall(r'[^\dE+.-]+', status)
69 values = re.findall(r'[\dE+.-]+', status)
70 if key not in keys:
71 raise ValueError("Invalid key used to search for status of Yokogawa 7561")
72 else:
73 return values[keys.index(key)]
74
75 source_voltage = Instrument.control(
76 "OD;E", "S%g;E",
77 """Control the source voltage in Volts, if that mode is active. (float)"""
78 )
79 source_current = Instrument.control(
80 "OD;E", "S%g;E",
81 """Control the source current in Amps, if that mode is active. (float)"""
82 )
83 source_voltage_range = Instrument.control(
84 "OS;E", "R%d;E",
85 """Control the source voltage range in Volts, which can take values:
86 10 mV, 100 mV, 1 V, 10 V, and 30 V.
87 Voltages are truncated to an appropriate value if needed. """,
88 validator=truncated_discrete_set,
89 values={10e-3: 2, 100e-3: 3, 1: 4, 10: 5, 30: 6},
90 map_values=True,
91 get_process=lambda v: int(Yokogawa7651._find(v, 'R'))
92 )
93 source_current_range = Instrument.control(
94 "OS;E", "R%d;E",
95 """Control the current voltage range in Amps, which can take values:
96 1 mA, 10 mA, and 100 mA.
97 Currents are truncated to an appropriate value if needed. """,
98 validator=truncated_discrete_set,
99 values={1e-3: 4, 10e-3: 5, 100e-3: 6},
100 map_values=True,
101 get_process=lambda v: int(Yokogawa7651._find(v, 'R'))
102 )
103 source_mode = Instrument.control(
104 "OS;E", "F%d;E",
105 """Control the source mode, which can take the values 'current' or 'voltage'.
106 The convenience methods :meth:`~.Yokogawa7651.apply_current` and
107 :meth:`~.Yokogawa7651.apply_voltage` can also be used. """,
108 validator=strict_discrete_set,
109 values={'current': 5, 'voltage': 1},
110 map_values=True,
111 get_process=lambda v: int(Yokogawa7651._find(v, 'F'))
112 )
113 compliance_voltage = Instrument.control(
114 "OS;E", "LV%g;E",
115 """Control the compliance voltage in Volts, which can take values between 1 and 30 V.""",
116 validator=truncated_range,
117 values=[1, 30],
118 get_process=lambda v: int(Yokogawa7651._find(v, 'LV'))
119 )
120 compliance_current = Instrument.control(
121 "OS;E", "LA%g;E",
122 """Control the compliance current in Amps,which can take values from 5 to 120 mA.""",
123 validator=truncated_range,
124 values=[5e-3, 120e-3],
125 get_process=lambda v: float(Yokogawa7651._find(v, 'LA')) * 1e-3, # converts A to mA
126 set_process=lambda v: v * 1e3, # converts mA to A
127 )
128
129 def __init__(self, adapter, name="Yokogawa 7651 Programmable DC Source", **kwargs):
130 super().__init__(
131 adapter, name, **kwargs
132 )
133
134 self.write("H0;E") # Set no header in output data
135
136 @property
137 def id(self):
138 """ Get the identification of the instrument """
139 return self.ask("OS;E").split('\r\n\n')[0]
140
141 @property
142 def source_enabled(self):
143 """ Get a boolean value that is True if the source is enabled,
144 determined by checking if the 5th bit of the OC flag is a binary 1.
145 """
146 oc = int(self.ask("OC;E")[5:])
147 return oc & 0b10000
148
149 def enable_source(self):
150 """ Enables the source of current or voltage depending on the
151 configuration of the instrument. """
152 self.write("O1;E")
153
154 def disable_source(self):
155 """ Disables the source of current or voltage depending on the
156 configuration of the instrument. """
157 self.write("O0;E")
158
159 def apply_current(self, max_current=1e-3, compliance_voltage=1):
160 """ Configures the instrument to apply a source current, which can
161 take optional parameters that defer to the :attr:`~.Yokogawa7651.source_current_range`
162 and :attr:`~.Yokogawa7651.compliance_voltage` properties. """
163 self.source_mode = 'current'
164 self.source_current_range = max_current
165 self.compliance_voltage = compliance_voltage
166
167 def apply_voltage(self, max_voltage=1, compliance_current=10e-3):
168 """ Configures the instrument to apply a source voltage, which can
169 take optional parameters that defer to the :attr:`~.Yokogawa7651.source_voltage_range`
170 and :attr:`~.Yokogawa7651.compliance_current` properties. """
171 self.source_mode = 'voltage'
172 self.source_voltage_range = max_voltage
173 self.compliance_current = compliance_current
174
175 def ramp_to_current(self, current, steps=25, duration=0.5):
176 """ Ramps the current to a value in Amps by traversing a linear spacing
177 of current steps over a duration, defined in seconds.
178
179 :param steps: A number of linear steps to traverse
180 :param duration: A time in seconds over which to ramp
181 """
182 start_current = self.source_current
183 stop_current = current
184 pause = duration / steps
185 if (start_current != stop_current):
186 currents = np.linspace(start_current, stop_current, steps)
187 for current in currents:
188 self.source_current = current
189 sleep(pause)
190
191 def ramp_to_voltage(self, voltage, steps=25, duration=0.5):
192 """ Ramps the voltage to a value in Volts by traversing a linear spacing
193 of voltage steps over a duration, defined in seconds.
194
195 :param steps: A number of linear steps to traverse
196 :param duration: A time in seconds over which to ramp
197 """
198 start_voltage = self.source_voltage
199 stop_voltage = voltage
200 pause = duration / steps
201 if (start_voltage != stop_voltage):
202 voltages = np.linspace(start_voltage, stop_voltage, steps)
203 for voltage in voltages:
204 self.source_voltage = voltage
205 sleep(pause)
206
207 def shutdown(self):
208 """ Shuts down the instrument, and ramps the current or voltage to zero
209 before disabling the source. """
210
211 # Since voltage and current are set the same way, this
212 # ramps either the current or voltage to zero
213 self.ramp_to_current(0.0, steps=25)
214 self.source_current = 0.0
215 self.disable_source()
216 super().shutdown()