Debian Codegraph @

pymeasure/instruments/newport/esp300.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 time import sleep
26
27 from pymeasure.instruments import Instrument, SCPIUnknownMixin
28 from pymeasure.instruments.validators import strict_discrete_set
29
30
31 class AxisError(Exception):
32 """ Raised when a particular axis causes an error for
33 the Newport ESP300. """
34
35 MESSAGES = {
36 '00': 'MOTOR TYPE NOT DEFINED',
37 '01': 'PARAMETER OUT OF RANGE',
38 '02': 'AMPLIFIER FAULT DETECTED',
39 '03': 'FOLLOWING ERROR THRESHOLD EXCEEDED',
40 '04': 'POSITIVE HARDWARE LIMIT DETECTED',
41 '05': 'NEGATIVE HARDWARE LIMIT DETECTED',
42 '06': 'POSITIVE SOFTWARE LIMIT DETECTED',
43 '07': 'NEGATIVE SOFTWARE LIMIT DETECTED',
44 '08': 'MOTOR / STAGE NOT CONNECTED',
45 '09': 'FEEDBACK SIGNAL FAULT DETECTED',
46 '10': 'MAXIMUM VELOCITY EXCEEDED',
47 '11': 'MAXIMUM ACCELERATION EXCEEDED',
48 '12': 'Reserved for future use',
49 '13': 'MOTOR NOT ENABLED',
50 '14': 'Reserved for future use',
51 '15': 'MAXIMUM JERK EXCEEDED',
52 '16': 'MAXIMUM DAC OFFSET EXCEEDED',
53 '17': 'ESP CRITICAL SETTINGS ARE PROTECTED',
54 '18': 'ESP STAGE DEVICE ERROR',
55 '19': 'ESP STAGE DATA INVALID',
56 '20': 'HOMING ABORTED',
57 '21': 'MOTOR CURRENT NOT DEFINED',
58 '22': 'UNIDRIVE COMMUNICATIONS ERROR',
59 '23': 'UNIDRIVE NOT DETECTED',
60 '24': 'SPEED OUT OF RANGE',
61 '25': 'INVALID TRAJECTORY MASTER AXIS',
62 '26': 'PARAMETER CHARGE NOT ALLOWED',
63 '27': 'INVALID TRAJECTORY MODE FOR HOMING',
64 '28': 'INVALID ENCODER STEP RATIO',
65 '29': 'DIGITAL I/O INTERLOCK DETECTED',
66 '30': 'COMMAND NOT ALLOWED DURING HOMING',
67 '31': 'COMMAND NOT ALLOWED DUE TO GROUP',
68 '32': 'INVALID TRAJECTORY MODE FOR MOVING'
69 }
70
71 def __init__(self, code):
72 self.axis = str(code)[0]
73 self.error = str(code)[1:]
74 self.message = self.MESSAGES[self.error]
75
76 def __str__(self):
77 return "Newport ESP300 axis {} reported the error: {}".format(
78 self.axis, self.message)
79
80
81 class GeneralError(Exception):
82 """ Raised when the Newport ESP300 has a general error.
83 """
84
85 MESSAGES = {
86 '1': 'PCI COMMUNICATION TIME-OUT',
87 '4': 'EMERGENCY SOP ACTIVATED',
88 '6': 'COMMAND DOES NOT EXIST',
89 '7': 'PARAMETER OUT OF RANGE',
90 '8': 'CABLE INTERLOCK ERROR',
91 '9': 'AXIS NUMBER OUT OF RANGE',
92 '13': 'GROUP NUMBER MISSING',
93 '14': 'GROUP NUMBER OUT OF RANGE',
94 '15': 'GROUP NUMBER NOT ASSIGNED',
95 '17': 'GROUP AXIS OUT OF RANGE',
96 '18': 'GROUP AXIS ALREADY ASSIGNED',
97 '19': 'GROUP AXIS DUPLICATED',
98 '16': 'GROUP NUMBER ALREADY ASSIGNED',
99 '20': 'DATA ACQUISITION IS BUSY',
100 '21': 'DATA ACQUISITION SETUP ERROR',
101 '23': 'SERVO CYCLE TICK FAILURE',
102 '25': 'DOWNLOAD IN PROGRESS',
103 '26': 'STORED PROGRAM NOT STARTED',
104 '27': 'COMMAND NOT ALLOWED',
105 '29': 'GROUP PARAMETER MISSING',
106 '30': 'GROUP PARAMETER OUT OF RANGE',
107 '31': 'GROUP MAXIMUM VELOCITY EXCEEDED',
108 '32': 'GROUP MAXIMUM ACCELERATION EXCEEDED',
109 '22': 'DATA ACQUISITION NOT ENABLED',
110 '28': 'STORED PROGRAM FLASH AREA FULL',
111 '33': 'GROUP MAXIMUM DECELERATION EXCEEDED',
112 '35': 'PROGRAM NOT FOUND',
113 '37': 'AXIS NUMBER MISSING',
114 '38': 'COMMAND PARAMETER MISSING',
115 '34': 'GROUP MOVE NOT ALLOWED DURING MOTION',
116 '39': 'PROGRAM LABEL NOT FOUND',
117 '40': 'LAST COMMAND CANNOT BE REPEATED',
118 '41': 'MAX NUMBER OF LABELS PER PROGRAM EXCEEDED'
119 }
120
121 def __init__(self, code):
122 self.error = str(code)
123 self.message = self.MESSAGES[self.error]
124
125 def __str__(self):
126 return "Newport ESP300 reported the error: %s" % (
127 self.message)
128
129
130 class Axis:
131 """ Represents an axis of the Newport ESP300 Motor Controller,
132 which can have independent parameters from the other axes.
133 """
134
135 position = Instrument.control(
136 "TP", "PA%g",
137 """ A floating point property that controls the position
138 of the axis. The units are defined based on the actuator.
139 Use the :meth:`~.wait_for_stop` method to ensure the position
140 is stable.
141 """
142 )
143 enabled = Instrument.measurement(
144 "MO?",
145 """ Returns a boolean value that is True if the motion for
146 this axis is enabled.
147 """,
148 cast=bool
149 )
150 left_limit = Instrument.control(
151 "SL?", "SL%g",
152 """ A floating point property that controls the left software
153 limit of the axis. """
154 )
155 right_limit = Instrument.control(
156 "SR?", "SR%g",
157 """ A floating point property that controls the right software
158 limit of the axis. """
159 )
160 units = Instrument.control(
161 "SN?", "SN%d",
162 """ A string property that controls the displacement units of the
163 axis, which can take values of: encoder count, motor step, millimeter,
164 micrometer, inches, milli-inches, micro-inches, degree, gradient, radian,
165 milliradian, and microradian.
166 """,
167 validator=strict_discrete_set,
168 values={
169 'encoder count': 0, 'motor step': 1, 'millimeter': 2,
170 'micrometer': 3, 'inches': 4, 'milli-inches': 5,
171 'micro-inches': 6, 'degree': 7, 'gradient': 8,
172 'radian': 9, 'milliradian': 10, 'microradian': 11
173 },
174 map_values=True
175 )
176 motion_done = Instrument.measurement(
177 "MD?",
178 """ Returns a boolean that is True if the motion is finished.
179 """,
180 cast=bool
181 )
182
183 def __init__(self, axis, controller):
184 self.axis = str(axis)
185 self.controller = controller
186
187 def ask(self, command):
188 command = self.axis + command
189 return self.controller.ask(command)
190
191 def write(self, command):
192 command = self.axis + command
193 self.controller.write(command)
194
195 def values(self, command, **kwargs):
196 command = self.axis + command
197 return self.controller.values(command, **kwargs)
198
199 def enable(self):
200 """ Enables motion for the axis. """
201 self.write("MO")
202
203 def disable(self):
204 """ Disables motion for the axis. """
205 self.write("MF")
206
207 def home(self, type=1):
208 """ Drives the axis to the home position, which may be the negative
209 hardware limit for some actuators (e.g. LTA-HS).
210 type can take integer values from 0 to 6.
211 """
212 home_type = strict_discrete_set(type, [0, 1, 2, 3, 4, 5, 6])
213 self.write("OR%d" % home_type)
214
215 def define_position(self, position):
216 """ Overwrites the value of the current position with the given
217 value. """
218 self.write("DH%g" % position)
219
220 def zero(self):
221 """ Resets the axis position to be zero at the current poisiton.
222 """
223 self.write("DH")
224
225 def wait_for_stop(self, delay=0, interval=0.05):
226 """ Blocks the program until the motion is completed. A further
227 delay can be specified in seconds.
228 """
229 self.write("WS%d" % (delay * 1e3))
230 while not self.motion_done:
231 sleep(interval)
232
233
234 class ESP300(SCPIUnknownMixin, Instrument):
235 """ Represents the Newport ESP 300 Motion Controller
236 and provides a high-level for interacting with the instrument.
237
238 By default this instrument is constructed with x, y, and phi
239 attributes that represent axes 1, 2, and 3. Custom implementations
240 can overwrite this depending on the available axes. Axes are controlled
241 through an :class:`Axis <pymeasure.instruments.newport.esp300.Axis>`
242 class.
243 """
244
245 error = Instrument.measurement(
246 "TE?",
247 """ Get an error code from the motion controller.
248 """,
249 cast=int
250 )
251
252 def __init__(self, adapter, name="Newport ESP 300 Motion Controller", **kwargs):
253 super().__init__(
254 adapter,
255 name,
256 **kwargs
257 )
258 # Defines default axes, which can be overwritten
259 self.x = Axis(1, self)
260 self.y = Axis(2, self)
261 self.phi = Axis(3, self)
262
263 def clear_errors(self):
264 """ Clears the error messages by checking until a 0 code is
265 received. """
266 while self.error != 0:
267 continue
268
269 @property
270 def errors(self):
271 """ Get a list of error Exceptions that can be later raised, or
272 used to diagnose the situation.
273 """
274 errors = []
275
276 code = self.error
277 while code != 0:
278 if code > 100:
279 errors.append(AxisError(code))
280 else:
281 errors.append(GeneralError(code))
282 code = self.error
283 return errors
284
285 @property
286 def axes(self):
287 """ Get a list of the :class:`Axis <pymeasure.instruments.newport.esp300.Axis>`
288 objects that are present. """
289 axes = []
290 directory = dir(self)
291 for name in directory:
292 if name == 'axes':
293 continue # Skip this property
294 try:
295 item = getattr(self, name)
296 if isinstance(item, Axis):
297 axes.append(item)
298 except TypeError:
299 continue
300 except Exception as e:
301 raise e
302 return axes
303
304 def enable(self):
305 """ Enables all of the axes associated with this controller.
306 """
307 for axis in self.axes:
308 axis.enable()
309
310 def disable(self):
311 """ Disables all of the axes associated with this controller.
312 """
313 for axis in self.axes:
314 axis.disable()
315
316 def shutdown(self):
317 """ Shuts down the controller by disabling all of the axes.
318 """
319 self.disable()
320 super().shutdown()