Debian Codegraph @

pymeasure/instruments/danfysik/danfysik8500.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.errors import RangeException
27
28 from time import sleep
29 import numpy as np
30 import re
31
32
33 class Danfysik8500(Instrument):
34 """ Represents the Danfysik 8500 Electromanget Current Supply
35 and provides a high-level interface for interacting with the
36 instrument
37
38 To allow user access to the Prolific Technology PL2303 Serial port adapter
39 in Linux, create the file:
40 :code:`/etc/udev/rules.d/50-danfysik.rules`, with contents:
41
42 .. code-block:: none
43
44 SUBSYSTEMS=="usb",ATTRS{idVendor}=="067b",ATTRS{idProduct}=="2303",MODE="0666",SYMLINK+="danfysik"
45
46 Then reload the udev rules with:
47
48 .. code-block:: bash
49
50 sudo udevadm control --reload-rules
51 sudo udevadm trigger
52
53 The device will be accessible through the port :code:`/dev/danfysik`.
54 """
55
56 id = Instrument.measurement(
57 "PRINT", """Get the idenfitication information. """
58 )
59
60 def __init__(self, adapter, name="Danfysik 8500 Current Supply", **kwargs):
61 super().__init__(
62 adapter,
63 name,
64 includeSCPI=False,
65 write_termination="\r",
66 read_termination="\r",
67 timeout=500,
68 **kwargs
69 )
70 # TODO verify serial connection.
71 self.write("ERRT") # Use text error messages
72 self.write("UNLOCK") # Unlock from remote or local mode
73
74 def read(self):
75 """ Read the device and raise exceptions if errors are reported by the instrument.
76
77 :returns: String ASCII response of the instrument
78 :raises: An :code:`Exception` if the Danfysik raises an error
79 """
80 result = super().read()
81 search = re.search(r"^\?\x07\s(?P<name>.*)$", result, re.MULTILINE)
82 if search:
83 raise Exception("Danfysik raised the error: %s" % (
84 search.groups()[0]))
85 else:
86 return result
87
88 def local(self):
89 """ Sets the instrument in local mode, where the front
90 panel can be used.
91 """
92 self.write("LOC")
93
94 def remote(self):
95 """ Sets the instrument in remote mode, where the the
96 front panel is disabled.
97 """
98 self.write("REM")
99
100 @property
101 def polarity(self):
102 """Control the polarity of the current supply, being either
103 -1 or 1. This property can be set by supplying one of
104 these values.
105 """
106 return 1 if self.ask("PO").strip() == '+' else -1
107
108 @polarity.setter
109 def polarity(self, value):
110 polarity = "+" if value > 0 else "-"
111 self.write("PO %s" % polarity)
112
113 def reset_interlocks(self):
114 """ Resets the instrument interlocks.
115 """
116 self.write("RS")
117
118 def enable(self):
119 """ Enables the flow of current.
120 """
121 self.write("N")
122
123 def disable(self):
124 """ Disables the flow of current.
125 """
126 self.write("F")
127
128 def is_enabled(self):
129 """ Returns True if the current supply is enabled.
130 """
131 return self.status_hex & 0x800000 == 0
132
133 @property
134 def status_hex(self):
135 """Get the status in hexadecimal. This value is parsed in
136 :attr:`~.Danfysik8500.status` into a human-readable list.
137 """
138 status = self.ask("S1H")
139 match = re.search(r'(?P<hex>[A-Z0-9]{6})', status)
140 if match is not None:
141 return int(match.groupdict()['hex'], 16)
142 else:
143 raise Exception("Danfysik status not properly returned. Instead "
144 "got '%s'" % status)
145
146 @property
147 def current(self):
148 """Control the actual current in Amps. This property can be set through
149 :attr:`~.current_ppm`.
150 """
151 return int(self.ask("AD 8")) * 1e-2 * self.polarity
152
153 @current.setter
154 def current(self, amps):
155 if amps > 160 or amps < -160:
156 raise RangeException("Danfysik 8500 is only capable of sourcing "
157 "+/- 160 Amps")
158 self.current_ppm = int((1e6 / 160) * amps)
159
160 @property
161 def current_ppm(self):
162 """Control the current in parts per million..
163 """
164 return int(self.ask("DA 0")[2:])
165
166 @current_ppm.setter
167 def current_ppm(self, ppm):
168 if abs(ppm) < 0 or abs(ppm) > 1e6:
169 raise RangeException("Danfysik 8500 requires parts per million "
170 "to be an appropriate integer")
171 self.write("DA 0,%d" % ppm)
172
173 @property
174 def current_setpoint(self):
175 """Get the setpoint for the current, which can deviate from the actual current
176 (:attr:`~.Danfysik8500.current`) while the supply is in the process of setting the value.
177 """
178 return self.current_ppm * (160 / 1e6)
179
180 @property
181 def slew_rate(self):
182 """Get the slew rate of the current sweep.
183 """
184 return float(self.ask("R3"))
185
186 def wait_for_current(self, has_aborted=lambda: False, delay=0.01):
187 """ Blocks the process until the current has stabilized. A
188 provided function :code:`has_aborted` can be supplied, which
189 is checked after each delay time (in seconds) in addition to the
190 stability check. This allows an abort feature to be integrated.
191
192 :param has_aborted: A function that returns True if the process should stop waiting
193 :param delay: The delay time in seconds between each check for stability
194 """
195 self.wait_for_ready(has_aborted, delay)
196 while not has_aborted() and not self.is_current_stable():
197 sleep(delay)
198
199 def is_current_stable(self):
200 """ Returns True if the current is within 0.02 A of the
201 setpoint value.
202 """
203 return abs(self.current - self.current_setpoint) <= 0.02
204
205 def is_ready(self):
206 """ Returns True if the instrument is in the ready state.
207 """
208 return self.status_hex & 0b10 == 0
209
210 def wait_for_ready(self, has_aborted=lambda: False, delay=0.01):
211 """ Blocks the process until the instrument is ready. A
212 provided function :code:`has_aborted` can be supplied, which
213 is checked after each delay time (in seconds) in addition to the
214 readiness check. This allows an abort feature to be integrated.
215
216 :param has_aborted: A function that returns True if the process should stop waiting
217 :param delay: The delay time in seconds between each check for readiness
218 """
219 while not has_aborted() and not self.is_ready():
220 sleep(delay)
221
222 @property
223 def status(self):
224 """Get a list of human-readable strings that contain
225 the instrument status information, based on :attr:`~.status_hex`.
226 """
227 status = []
228 indicator = self.ask("S1")
229 if indicator[0] == "!":
230 status.append("Main Power OFF")
231 else:
232 status.append("Main Power ON")
233 # Skipping 5, 6 and 7 (from Appendix Manual on command S1)
234 messages = {
235 1: "Polarity Normal",
236 2: "Polarity Reversed",
237 3: "Regulation Transformer is not equal to zero",
238 7: "Spare Interlock",
239 8: "One Transistor Fault",
240 9: "Sum - Interlock",
241 10: "DC Overcurrent (OCP)",
242 11: "DC Overload",
243 12: "Regulation Module Failure",
244 13: "Preregulator Failure",
245 14: "Phase Failure",
246 15: "MPS Waterflow Failure",
247 16: "Earth Leakage Failure",
248 17: "Thermal Breaker/Fuses",
249 18: "MPS Overtemperature",
250 19: "Panic Button/Door Switch",
251 20: "Magnet Waterflow Failure",
252 21: "Magnet Overtemperature",
253 22: "MPS Not Ready"
254 }
255 for index, message in messages.items():
256 if indicator[index] == "!":
257 status.append(message)
258 return status
259
260 def clear_ramp_set(self):
261 """ Clears the ramp set.
262 """
263 self.write("RAMPSET C")
264
265 def set_ramp_delay(self, time):
266 """ Sets the ramp delay time in seconds.
267
268 :param time: The time delay time in seconds
269 """
270 self.write("RAMPSET %f" % time)
271
272 def start_ramp(self):
273 """ Starts the current ramp.
274 """
275 self.write("RAMP R")
276
277 def add_ramp_step(self, current):
278 """ Adds a current step to the ramp set.
279
280 :param current: A current in Amps
281 """
282 self.write("R %.6f" % (current / 160.))
283
284 def stop_ramp(self):
285 """ Stops the current ramp.
286 """
287 self.ask("RAMP S")
288
289 def set_ramp_to_current(self, current, points, delay_time=1):
290 """ Sets up a linear ramp from the initial current to a different
291 current, with a number of points, and delay time.
292
293 :param current: The final current in Amps
294 :param points: The number of linear points to traverse
295 :param delay_time: A delay time in seconds
296 """
297 initial_current = self.current
298 self.clear_ramp_set()
299 self.set_ramp_delay(delay_time)
300 steps = np.linspace(initial_current, current, num=points)
301 cmds = ["R %.6f" % (step / 160.) for step in steps]
302 self.write("\r".join(cmds))
303
304 def ramp_to_current(self, current, points, delay_time=1):
305 """ Executes :meth:`~.set_ramp_to_current` and starts the ramp.
306 """
307 self.set_ramp_to_current(current, points, delay_time)
308 self.start_ramp()
309
310 # self.setSequence(0, [0, 10], [0.01])
311 def set_sequence(self, stack, currents, times, multiplier=999999):
312 """ Sets up an arbitrary ramp profile with a list of currents (Amps)
313 and a list of interval times (seconds) on the specified stack number
314 (0-15)
315 """
316 self.clear_sequence(stack)
317 if min(times) >= 1 and max(times) <= 65535:
318 self.write("SLOW %i" % stack)
319 elif min(times) >= 0.1 and max(times) <= 6553.5:
320 self.write("FAST %i" % stack)
321 times = [0.1 * x for x in times]
322 else:
323 raise RangeException("Timing for Danfysik 8500 ramp sequence is"
324 " out of range")
325 for i in range(len(times)):
326 self.write("WSA %i,%i,%i,%i" % (
327 stack,
328 int(6250 * abs(currents[i])),
329 int(6250 * abs(currents[i + 1])), times[i])
330 )
331 self.write("MULT %i,%i" % (stack, multiplier))
332
333 def clear_sequence(self, stack):
334 """ Clears the sequence by the stack number.
335
336 :param stack: A stack number between 0-15
337 """
338 self.write("CSS %i" % stack)
339
340 def sync_sequence(self, stack, delay=0):
341 """ Arms the ramp sequence to be triggered by a hardware
342 input to pin P33 1&2 (10 to 24 V) or a TS command. If a
343 delay is provided, the sequence will start after the delay.
344
345 :param stack: A stack number between 0-15
346 :param delay: A delay time in seconds
347 """
348 self.write("SYNC %i, %i" % (stack, delay))
349
350 def start_sequence(self, stack):
351 """ Starts a sequence by the stack number.
352
353 :param stack: A stack number between 0-15
354 """
355 self.write("TS %i" % stack)
356
357 def stop_sequence(self):
358 """ Stops the currently running sequence.
359 """
360 self.write("STOP")
361
362 def is_sequence_running(self, stack):
363 """ Returns True if a sequence is running with a given stack number
364
365 :param stack: A stack number between 0-15
366 """
367 return re.search("R%i," % stack, self.ask("S2")) is not None