forked from mxcube/HardwareObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MD2Motor.py
161 lines (130 loc) · 5.89 KB
/
MD2Motor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import logging
import time
from gevent import Timeout
from AbstractMotor import AbstractMotor
from HardwareRepository.BaseHardwareObjects import Device
class MD2TimeoutError(Exception):
pass
class MD2Motor(AbstractMotor, Device):
def __init__(self, name):
AbstractMotor.__init__(self)
Device.__init__(self, name)
self.motor_pos_attr_suffix = "Position"
def init(self):
self.motorState = MD2Motor.NOTINITIALIZED
if self.motor_name is None:
self.motor_name = self.getProperty("motor_name")
self.motor_resolution = self.getProperty("resolution")
if self.motor_resolution is None:
self.motor_resolution = 1E-3
self.position_attr = self.addChannel({"type":"exporter",
"name":"position" },
self.motor_name+self.motor_pos_attr_suffix)
if self.position_attr is not None:
self.position_attr.connectSignal("update", self.motorPositionChanged)
self.state_attr = self.addChannel({"type":"exporter",
"name":"%sState" % self.motor_name},
"State")
self.motors_state_attr = self.addChannel({"type":"exporter",
"name":"MotorStates"}, "MotorStates")
if self.motors_state_attr is not None:
self.motors_state_attr.connectSignal("update", self.updateMotorState)
self._motor_abort = self.addCommand( {"type":"exporter", "name":"abort" },
"abort")
self.get_limits_cmd = self.addCommand({ "type": "exporter",
"name": "get%sLimits" % self.motor_name},
"getMotorLimits")
self.get_dynamic_limits_cmd = self.addCommand({"type": "exporter",
"name": "get%sDynamicLimits" % self.motor_name},
"getMotorDynamicLimits")
self.home_cmd = self.addCommand( {"type":"exporter", "name":"%sHoming" % self.motor_name},
"startHomingMotor")
def connectNotify(self, signal):
if signal == 'positionChanged':
self.emit('positionChanged', (self.getPosition(), ))
elif signal == 'stateChanged':
self.motorStateChanged(self.getState())
elif signal == 'limitsChanged':
self.motorLimitsChanged()
def updateState(self):
self.setIsReady(self.motorState > MD2Motor.UNUSABLE)
def updateMotorState(self, motor_states):
d = dict([x.split("=") for x in motor_states])
new_motor_state = MD2Motor.EXPORTER_TO_MOTOR_STATE[d[self.motor_name]]
if self.motorState == new_motor_state:
return
self.motorState = new_motor_state
self.motorStateChanged(self.motorState)
def motorStateChanged(self, state):
logging.getLogger().debug("%s: in motorStateChanged: motor state changed to %s", self.name(), state)
self.updateState()
self.emit('stateChanged', (self.motorState, ))
def getState(self):
if self.motorState == MD2Motor.NOTINITIALIZED:
try:
self.updateMotorState(self.motors_state_attr.getValue())
except:
return MD2Motor.NOTINITIALIZED
return self.motorState
def motorLimitsChanged(self):
self.emit('limitsChanged', (self.getLimits(), ))
def getDynamicLimits(self):
try:
low_lim,hi_lim = list(map(float, self.get_dynamic_limits_cmd(self.motor_name)))
if low_lim==float(1E999) or hi_lim==float(1E999):
raise ValueError
return low_lim, hi_lim
except:
return (-1E4, 1E4)
def getLimits(self):
try:
low_lim,hi_lim = list(map(float, self.get_limits_cmd(self.motor_name)))
if low_lim==float(1E999) or hi_lim==float(1E999):
raise ValueError
return low_lim, hi_lim
except:
return (-1E4, 1E4)
def motorPositionChanged(self, absolutePosition, private={}):
if abs(absolutePosition - private.get("old_pos", 1E12))<=self.motor_resolution:
return
private["old_pos"]=absolutePosition
self.emit('positionChanged', (absolutePosition, ))
def getPosition(self):
if self.getState() != MD2Motor.NOTINITIALIZED:
return self.position_attr.getValue()
def getDialPosition(self):
return self.getPosition()
def move(self, absolutePosition, timeout=None, wait=False):
if self.getState() != MD2Motor.NOTINITIALIZED:
self.position_attr.setValue(absolutePosition)
self.motorStateChanged(MD2Motor.MOVING)
if wait:
try:
self.waitEndOfMove(timeout)
except:
raise MD2TimeoutError
def moveRelative(self, relativePosition, wait=False, timeout=None):
self.move(self.getPosition() + relativePosition)
if wait:
try:
self.waitEndOfMove(timeout)
except:
raise MD2TimeoutError
def waitEndOfMove(self, timeout=None):
with Timeout(timeout):
time.sleep(0.1)
while self.motorState == MD2Motor.MOVING:
time.sleep(0.1)
def motorIsMoving(self):
return self.isReady() and self.motorState == MD2Motor.MOVING
def getMotorMnemonic(self):
return self.motor_name
def stop(self):
if self.getState() != MD2Motor.NOTINITIALIZED:
self._motor_abort()
def homeMotor(self, timeout=None):
self.home_cmd(self.motor_name)
try:
self.waitEndOfMove(timeout)
except:
raise MD2TimeoutError