| 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 |
from time import sleep |
| 27 |
from enum import IntFlag |
| 28 |
from pymeasure.instruments import Instrument |
| 29 |
from pymeasure.instruments.validators import strict_range, truncated_range, strict_discrete_set |
| 30 |
|
| 31 |
|
| 32 |
log = logging.getLogger(__name__) |
| 33 |
log.addHandler(logging.NullHandler()) |
| 34 |
|
| 35 |
|
| 36 |
class DPSeriesErrors(IntFlag): |
| 37 |
""" IntFlag type to decode error register queries. Error codes are as follows: |
| 38 |
|
| 39 |
0: no error |
| 40 |
1: Receive Overflow Error: serial communications had a receiving error. |
| 41 |
2: Encoder Error 1: encoder needed to correct the motor position. |
| 42 |
4: Encoder Error 2: encoder could not finish motor position correction. |
| 43 |
8: Command Error: a bad command was sent to the controller. |
| 44 |
16: Motor Error: motor speed profiles are set incorrectly. |
| 45 |
32: Range Overflow Error: go to position has an overflow error. |
| 46 |
64: Range Error: invalid number of commands and characters sent to the controller. |
| 47 |
128: Transmit Error: Too many parameters sent back to the pc. |
| 48 |
256: Mode Error: Controller is in a wrong mode. |
| 49 |
512: Zero Parameters Error: Command sent to the controller that expected to see |
| 50 |
parameters follow, but none were given. |
| 51 |
1024: Busy Error: The controller is busy indexing (moving a motor). |
| 52 |
2048: Memory Range Error: Specified address is out of range. |
| 53 |
4096: Memory Command Error: Command pulled from memory is invalid. |
| 54 |
8192: Thumbwheel Read Error: Error reading the thumbwheel, or thumbwheel is not present. |
| 55 |
""" |
| 56 |
NO_ERR = 0 |
| 57 |
RCV_OVERFLOW_ERR = 1 |
| 58 |
ENC_ERR_1 = 2 |
| 59 |
ENC_ERR_2 = 4 |
| 60 |
CMD_ERR = 8 |
| 61 |
MOT_ERR = 16 |
| 62 |
RANGE_OVERFLOW_ERR = 32 |
| 63 |
RANGE_ERR = 64 |
| 64 |
TX_ERR = 128 |
| 65 |
MODE_ERR = 256 |
| 66 |
ZERO_PARAMS_ERR = 512 |
| 67 |
BUSY_ERR = 1024 |
| 68 |
MEM_RANGE_ERR = 2048 |
| 69 |
MEM_CMD_ERR = 4096 |
| 70 |
THBWHEEL_ERR = 8192 |
| 71 |
|
| 72 |
|
| 73 |
class DPSeriesMotorController(Instrument): |
| 74 |
"""Base class to interface with Anaheim Automation DP series stepper motor controllers. |
| 75 |
|
| 76 |
This driver has been tested with the DPY50601 and DPE25601 motor controllers. |
| 77 |
""" |
| 78 |
|
| 79 |
address = Instrument.control( |
| 80 |
"%", "~%i", |
| 81 |
"""Integer property representing the address that the motor controller uses for serial |
| 82 |
communications.""", |
| 83 |
validator=strict_range, |
| 84 |
values=[0, 99], |
| 85 |
cast=int, |
| 86 |
) |
| 87 |
|
| 88 |
basespeed = Instrument.control( |
| 89 |
"VB", "B%i", |
| 90 |
"""Integer property that represents the motor controller's starting/homing speed. This |
| 91 |
property can be set.""", |
| 92 |
validator=truncated_range, |
| 93 |
values=[1, 5000], |
| 94 |
cast=int, |
| 95 |
) |
| 96 |
|
| 97 |
maxspeed = Instrument.control( |
| 98 |
"VM", "M%i", |
| 99 |
"""Integer property that represents the motor controller's maximum (running) speed. |
| 100 |
This property can be set.""", |
| 101 |
validator=truncated_range, |
| 102 |
values=[1, 50000], |
| 103 |
cast=int, |
| 104 |
) |
| 105 |
|
| 106 |
direction = Instrument.control( |
| 107 |
"V+", "%s", |
| 108 |
"""A string property that represents the direction in which the stepper motor will rotate |
| 109 |
upon subsequent step commands. This property can be set. 'CW' corresponds to clockwise |
| 110 |
rotation and 'CCW' corresponds to counter-clockwise rotation.""", |
| 111 |
map_values=True, |
| 112 |
validator=strict_discrete_set, |
| 113 |
values={"CW": "+", "CCW": "-"}, |
| 114 |
get_process=lambda d: "+" if d == 1.0 else "-", |
| 115 |
) |
| 116 |
|
| 117 |
encoder_autocorrect = Instrument.control( |
| 118 |
"VEA", "EA%i", |
| 119 |
"""A boolean property to enable or disable the encoder auto correct function. This property |
| 120 |
can be set.""", |
| 121 |
map_values=True, |
| 122 |
values={True: 1, False: 0}, |
| 123 |
validator=strict_discrete_set, |
| 124 |
cast=int, |
| 125 |
) |
| 126 |
|
| 127 |
encoder_delay = Instrument.control( |
| 128 |
"VED", "ED%i", |
| 129 |
"""An integer property that represents the wait time in ms. after a move is finished before |
| 130 |
the encoder is read for a potential encoder auto-correct action to take place. This |
| 131 |
property can be set.""", |
| 132 |
validator=truncated_range, |
| 133 |
values=[0, 65535], |
| 134 |
cast=int, |
| 135 |
) |
| 136 |
|
| 137 |
encoder_motor_ratio = Instrument.control( |
| 138 |
"VEM", "EM%i", |
| 139 |
"""An integer property that represents the ratio of the number of encoder pulses per motor |
| 140 |
step. This property can be set.""", |
| 141 |
validator=truncated_range, |
| 142 |
values=[1, 255], |
| 143 |
cast=int, |
| 144 |
) |
| 145 |
|
| 146 |
encoder_retries = Instrument.control( |
| 147 |
"VER", "ER%i", |
| 148 |
"""An integer property that represents the number of times the motor controller will try the |
| 149 |
encoder auto correct function before setting an error flag. This property can be set.""", |
| 150 |
validator=truncated_range, |
| 151 |
values=[0, 255], |
| 152 |
cast=int, |
| 153 |
) |
| 154 |
|
| 155 |
encoder_window = Instrument.control( |
| 156 |
"VEW", "EW%i", |
| 157 |
"""An integer property that represents the allowable error in encoder pulses from the |
| 158 |
desired position before the encoder auto-correct function runs. This property can be set. |
| 159 |
""", |
| 160 |
validator=truncated_range, |
| 161 |
values=[0, 255], |
| 162 |
cast=int, |
| 163 |
) |
| 164 |
|
| 165 |
busy = Instrument.measurement( |
| 166 |
"VF", |
| 167 |
"""Query to see if the controller is currently moving a motor.""" |
| 168 |
) |
| 169 |
|
| 170 |
error_reg = Instrument.measurement( |
| 171 |
"!", |
| 172 |
"""Reads the current value of the error codes register.""", |
| 173 |
get_process=lambda err: DPSeriesErrors(int(err)), |
| 174 |
) |
| 175 |
|
| 176 |
def check_errors(self): |
| 177 |
""" Method to read the error codes register and log when an error is detected. |
| 178 |
|
| 179 |
:return error_code: one byte with the error codes register contents |
| 180 |
""" |
| 181 |
current_errors = self.error_reg |
| 182 |
if current_errors != 0: |
| 183 |
logging.error("DP-Series motor controller error detected: %s" % current_errors) |
| 184 |
return current_errors |
| 185 |
|
| 186 |
def __init__(self, adapter, name="Anaheim Automation Stepper Motor Controller", |
| 187 |
address=0, encoder_enabled=False, **kwargs): |
| 188 |
""" |
| 189 |
Initialize communication with the motor controller with the address given by `address`. |
| 190 |
|
| 191 |
In addition to the keyword arguments that can be set for the Instrument base class, this |
| 192 |
class has the following kwargs: |
| 193 |
|
| 194 |
:param address: (int) Address that the motor controller uses for serial communiation. |
| 195 |
:param encoder_enabled: (bool) Flag to indicate if the driver should use an encoder input |
| 196 |
to set its position property. |
| 197 |
""" |
| 198 |
self._address = address |
| 199 |
self._encoder_enabled = encoder_enabled |
| 200 |
kwargs.setdefault('write_termination', '\r') |
| 201 |
kwargs.setdefault('read_termination', '\r') |
| 202 |
kwargs.setdefault('timeout', 2000) |
| 203 |
|
| 204 |
super().__init__( |
| 205 |
adapter, |
| 206 |
name, |
| 207 |
includeSCPI=False, |
| 208 |
asrl={'baud_rate': 38400}, |
| 209 |
**kwargs |
| 210 |
) |
| 211 |
|
| 212 |
@property |
| 213 |
def encoder_enabled(self): |
| 214 |
""" A boolean property to represent whether an external encoder is connected and should be |
| 215 |
used to set the :attr:`step_position` property. |
| 216 |
""" |
| 217 |
return self._encoder_enabled |
| 218 |
|
| 219 |
@encoder_enabled.setter |
| 220 |
def encoder_enabled(self, en): |
| 221 |
self._encoder_enabled = bool(en) |
| 222 |
|
| 223 |
@property |
| 224 |
def step_position(self): |
| 225 |
""" Integer property representing the value of the motor position measured in steps counted |
| 226 |
by the motor controller or, if :attr:`encoder_enabled` is set, the steps counted by an |
| 227 |
externally connected encoder. Note that in the DP series motor controller instrument |
| 228 |
manuals, this property would be referred to as the 'absolute position' while this |
| 229 |
driver implements a conversion between steps and absolute units for the |
| 230 |
:attr:`absolute_position` property. This property can be set. |
| 231 |
""" |
| 232 |
if self._encoder_enabled: |
| 233 |
pos = self.ask("VEP") |
| 234 |
else: |
| 235 |
pos = self.ask("VZ") |
| 236 |
return int(pos) |
| 237 |
|
| 238 |
@step_position.setter |
| 239 |
def step_position(self, pos): |
| 240 |
strict_range(pos, (-8388607, 8388607)) |
| 241 |
self.write("P%i" % pos) |
| 242 |
self.write("G") |
| 243 |
|
| 244 |
@property |
| 245 |
def absolute_position(self): |
| 246 |
""" Float property representing the value of the motor position measured in absolute units. |
| 247 |
Note that in DP series motor controller instrument manuals, 'absolute position' refers to |
| 248 |
the :attr:`step_position` property rather than this property. Also note that use of this |
| 249 |
property relies on :meth:`steps_to_absolute()` and :meth:`absolute_to_steps()` |
| 250 |
being implemented in a subclass. In this way, the user can define the conversion from a |
| 251 |
motor step position into any desired absolute unit. Absolute units could be the position in |
| 252 |
meters of a linear stage or the angular position of a gimbal mount, etc. This property can |
| 253 |
be set. |
| 254 |
""" |
| 255 |
step_pos = self.step_position |
| 256 |
return self.steps_to_absolute(step_pos) |
| 257 |
|
| 258 |
@absolute_position.setter |
| 259 |
def absolute_position(self, abs_pos): |
| 260 |
steps_pos = self.absolute_to_steps(abs_pos) |
| 261 |
self.step_position = steps_pos |
| 262 |
|
| 263 |
def absolute_to_steps(self, pos): |
| 264 |
""" Convert an absolute position to a number of steps to move. This must be implemented in |
| 265 |
subclasses. |
| 266 |
|
| 267 |
:param pos: Absolute position in the units determined by the subclassed |
| 268 |
:meth:`absolute_to_steps()` method. |
| 269 |
""" |
| 270 |
raise NotImplementedError("absolute_to_steps() must be implemented in subclasses!") |
| 271 |
|
| 272 |
def steps_to_absolute(self, steps): |
| 273 |
""" Convert a position measured in steps to an absolute position. |
| 274 |
|
| 275 |
:param steps: Position in steps to be converted to an absolute position. |
| 276 |
""" |
| 277 |
raise NotImplementedError("steps_to_absolute() must be implemented in subclasses!") |
| 278 |
|
| 279 |
def reset_position(self): |
| 280 |
""" |
| 281 |
Reset position as counted by the motor controller and an externally connected encoder to 0. |
| 282 |
""" |
| 283 |
# reset encoder recorded position # |
| 284 |
self.write("ET") |
| 285 |
# reset motor recorded position # |
| 286 |
self.write("Z0") |
| 287 |
|
| 288 |
def stop(self): |
| 289 |
"""Method that stops all motion on the motor controller.""" |
| 290 |
self.write(".") |
| 291 |
|
| 292 |
def move(self, direction): |
| 293 |
""" Move the stepper motor continuously in the given direction until a stop command is sent |
| 294 |
or a limit switch is reached. This method corresponds to the 'slew' command in the DP |
| 295 |
series instrument manuals. |
| 296 |
|
| 297 |
:param direction: value to set on the direction property before moving the motor. |
| 298 |
""" |
| 299 |
self.direction = direction |
| 300 |
self.write("S") |
| 301 |
|
| 302 |
def home(self, home_mode): |
| 303 |
""" Send command to the motor controller to 'home' the motor. |
| 304 |
|
| 305 |
:param home_mode: ``0`` or ``1`` specifying which homing mode to run. |
| 306 |
|
| 307 |
0 will perform a homing operation where the controller moves the motor until a soft |
| 308 |
limit is reached, then will ramp down to base speed and continue motion until a home |
| 309 |
limit is reached. |
| 310 |
|
| 311 |
In mode 1, the controller will move the motor until a limit is reached, then will ramp |
| 312 |
down to base speed, change direction, and run until the limit is released. |
| 313 |
""" |
| 314 |
hm = int(home_mode) |
| 315 |
if hm == 0 or hm == 1: |
| 316 |
self.write("H%i" % hm) |
| 317 |
else: |
| 318 |
raise ValueError("Invalid home mode %i specified!" % hm) |
| 319 |
|
| 320 |
def write(self, command): |
| 321 |
"""Override the instrument base write method to add the motor controller's address to the |
| 322 |
command string. |
| 323 |
|
| 324 |
:param command: command string to be sent to the motor controller. |
| 325 |
""" |
| 326 |
# check if @ was already prepended when using say, the SerialAdapter # |
| 327 |
if "@" in command: |
| 328 |
cmd_str = command |
| 329 |
elif "%" in command or "~" in command: |
| 330 |
cmd_str = "@%s" % command |
| 331 |
else: |
| 332 |
cmd_str = "@%i%s" % (self._address, command) |
| 333 |
super().write(cmd_str) |
| 334 |
|
| 335 |
def wait_for_completion(self, interval=0.5): |
| 336 |
""" Block until the controller is not "busy" (i.e. block until the motor is no longer moving.) |
| 337 |
|
| 338 |
:param interval: (float) seconds between queries to the "busy" flag. |
| 339 |
:return: None |
| 340 |
""" # noqa: E501 |
| 341 |
while self.busy: |
| 342 |
sleep(interval) |