Debian Codegraph @

pymeasure/instruments/siglenttechnologies/siglent_spdbase.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 pymeasure.instruments import Instrument, SCPIUnknownMixin
26 from pymeasure.instruments.channel import Channel
27 from pymeasure.instruments.validators import (strict_discrete_range,
28 strict_discrete_set,
29 truncated_range
30 )
31 from enum import IntFlag
32
33 log = logging.getLogger(__name__) # https://docs.python.org/3/howto/logging.html#library-config
34 log.addHandler(logging.NullHandler())
35
36
37 class SystemStatusCode(IntFlag):
38 """System status enums based on ``IntFlag``
39
40 Used in conjunction with :attr:`~.system_status_code`.
41
42 ====== ======
43 Value Enum
44 ====== ======
45 256 WAVEFORM_DISPLAY
46 64 TIMER_ENABLED
47 32 FOUR_WIRE
48 16 OUTPUT_ENABLED
49 1 CONSTANT_CURRENT
50 0 CONSTANT_VOLTAGE
51 ====== ======
52
53 """
54
55 WAVEFORM_DISPLAY = 256 # bit 8 -- waveform display enabled
56 TIMER_ENABLED = 64 # bit 6 -- timer enabled
57 FOUR_WIRE = 32 # bit 5 -- four-wire mode enabled
58 OUTPUT_ENABLED = 16 # bit 4 -- output enabled
59 CONSTANT_CURRENT = 1 # bit 0 -- constant current mode
60 CONSTANT_VOLTAGE = 0 # bit 0 -- constant voltage mode
61
62
63 class SPDChannel(Channel):
64 """ The channel class for Siglent SPDxxxxX instruments.
65 """
66
67 def __init__(self, parent, id,
68 voltage_range: list = [0, 16],
69 current_range: list = [0, 8]):
70 super().__init__(parent, id)
71 self.voltage_range = voltage_range
72 self.current_range = current_range
73
74 voltage = Instrument.measurement(
75 "MEAS:VOLT? CH{ch}",
76 """Measure the channel output voltage.
77
78 :type: float
79 """
80 )
81
82 current = Instrument.measurement(
83 "MEAS:CURR? CH{ch}",
84 """Measure the channel output current.
85
86 :type: float
87 """
88 )
89
90 power = Instrument.measurement(
91 "MEAS:POWE? CH{ch}",
92 """Measure the channel output power.
93
94 :type: float
95 """
96 )
97
98 current_limit = Instrument.control(
99 "CH{ch}:CURR?", "CH{ch}:CURR %g",
100 """Control the output current configuration of the channel.
101
102 :type : float
103 """,
104 validator=truncated_range,
105 values=[0, 8],
106 dynamic=True
107 )
108
109 voltage_setpoint = Instrument.control(
110 "CH{ch}:VOLT?", "CH{ch}:VOLT %g",
111 """Control the output voltage configuration of the channel.
112
113 :type : float
114 """,
115 validator=truncated_range,
116 values=[0, 16],
117 dynamic=True
118 )
119
120 def enable_output(self, enable: bool = True):
121 """Enable the channel output.
122
123 :type: bool
124 ``True``: enables the output
125 ``False``: disables it
126 """
127 self.parent.selected_channel = self.id
128 self.write('OUTP CH{ch},' + ("ON" if enable else "OFF"))
129
130 def enable_timer(self, enable: bool = True):
131 """Enable the channel timer.
132
133 :type: bool
134 ``True``: enables the timer
135 ``False``: disables it
136 """
137 self.write('TIME CH{ch},' + ("ON" if enable else "OFF"))
138
139 def configure_timer(self, step, voltage, current, duration):
140 """Configure the timer step.
141
142 :param step:
143 int: index of the step to save the configuration
144 :param voltage:
145 float: voltage setpoint of the step
146 :param current:
147 float: current limit of the step
148 :param duration:
149 int: duration of the step in seconds
150 """
151 step = strict_discrete_range(step, [1, 5], 1)
152 voltage = truncated_range(voltage, self.voltage_range)
153 current = truncated_range(current, self.current_range)
154 duration = truncated_range(duration, [0, 10000])
155 self.write(f'TIME:SET CH{{ch}},{step:d},{voltage:1.3f},{current:1.3f},{duration:d}')
156
157
158 class SPDBase(SCPIUnknownMixin, Instrument):
159 """ The base class for Siglent SPDxxxxX instruments.
160
161 Uses :class:`SPDChannel` for measurement channels.
162 """
163
164 def __init__(self, adapter, name="Siglent SPDxxxxX instrument Base Class", **kwargs):
165 super().__init__(
166 adapter,
167 name,
168 usb=dict(write_termination='\n',
169 read_termination='\n'),
170 tcpip=dict(write_termination='\n',
171 read_termination='\n'),
172 **kwargs
173 )
174
175 error = Instrument.measurement(
176 "SYST:ERR?",
177 """Get the error code and information of the instrument.
178
179 :type: string
180 """
181 )
182
183 fw_version = Instrument.measurement(
184 "SYST:VERS?",
185 """Get the software version of the instrument.
186
187 :type: string
188 """
189 )
190
191 system_status_code = Instrument.measurement(
192 "SYST:STAT?",
193 """Get the system status register.
194
195 :type: :class:`.SystemStatusCode`
196 """,
197 get_process=lambda v: SystemStatusCode(int(v, base=16)),
198 )
199
200 selected_channel = Instrument.control(
201 "INST?", "INST %s",
202 """Control the selected channel of the instrument.
203
204 :type : int
205 """,
206 validator=strict_discrete_set,
207 values={1: "CH1"}, # This dynamic property should be updated for multi-channel instruments
208 map_values=True,
209 dynamic=True
210 )
211
212 def save_config(self, index):
213 """Save the current config to memory.
214
215 :param index:
216 int: index of the location to save the configuration
217 """
218 index = strict_discrete_range(index, [1, 5], 1)
219 self.write(f"*SAV {index:d}")
220
221 def recall_config(self, index):
222 """Recall a config from memory.
223
224 :param index:
225 int: index of the location from which to recall the configuration
226 """
227 index = strict_discrete_range(index, [1, 5], 1)
228 self.write(f"*RCL {index:d}")
229
230 def enable_local_interface(self, enable: bool = True):
231 """Configure the availability of the local interface.
232
233 :type: bool
234 ``True``: enables the local interface
235 ``False``: disables it.
236 """
237 self.write("*UNLOCK" if enable else "*LOCK")
238
239 def shutdown(self):
240 """ Ensure that the voltage is turned to zero
241 and disable the output. """
242 for ch in self.channels.values():
243 ch.voltage_setpoint = 0
244 ch.enable_output(False)
245 super().shutdown()
246
247
248 class SPDSingleChannelBase(SPDBase):
249 def enable_4W_mode(self, enable: bool = True):
250 """Enable 4-wire mode.
251
252 :type: bool
253 ``True``: enables 4-wire mode
254 ``False``: disables it.
255 """
256 self.write(f'MODE:SET {"4W" if enable else "2W"}')