forked from mxcube/HardwareObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobodiffMotor.py
118 lines (94 loc) · 3.6 KB
/
RobodiffMotor.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
from HardwareRepository.BaseHardwareObjects import Device
from HardwareRepository import HardwareRepository
import math
import logging
import time
import gevent
import types
class RobodiffMotor(Device):
(NOTINITIALIZED, UNUSABLE, READY, MOVESTARTED, MOVING, ONLIMIT) = (0,1,2,3,4,5)
def __init__(self, name):
Device.__init__(self, name)
self.__initialized=False
def init(self):
self.motorState = RobodiffMotor.NOTINITIALIZED
self.username = self.motor_name
#gevent.spawn_later(1, self.end_init)
def end_init(self):
if self.__initialized:
return
controller = HardwareRepository.HardwareRepository().getHardwareObject(self.getProperty("controller")) #self.getObjectByRole("controller")
# this is ugly : I added it to make the centring procedure happy
self.specName = self.motor_name
self.motor = getattr(controller, self.motor_name)
self.connect(self.motor, "position", self.positionChanged)
self.connect(self.motor, "state", self.updateState)
self.__initialized=True
def connectNotify(self, signal):
if signal == 'positionChanged':
self.emit('positionChanged', (self.getPosition(), ))
elif signal == 'stateChanged':
self.updateState(emit=True)
elif signal == 'limitsChanged':
self.motorLimitsChanged()
def updateState(self, state=None, emit=False):
self.end_init()
if state is None:
state = self.motor.state()
# convert from grob state to Hardware Object motor state
if state == "MOVING":
state = RobodiffMotor.MOVING
elif state == "READY":
state = RobodiffMotor.READY
elif state == "ONLIMIT":
state = RobodiffMotor.ONLIMIT
else:
state = RobodiffMotor.UNUSABLE
self.setIsReady(state > RobodiffMotor.UNUSABLE)
if self.motorState != state:
self.motorState = state
emit = True
if emit:
self.emit('stateChanged', (self.motorState, ))
def getState(self):
self.end_init()
self.updateState()
return self.motorState
def motorLimitsChanged(self):
self.end_init()
self.emit('limitsChanged', (self.getLimits(), ))
def getLimits(self):
self.end_init()
return self.motor.limits()
def positionChanged(self, absolutePosition):
#print self.name(), absolutePosition
self.emit('positionChanged', (absolutePosition, ))
def getPosition(self):
self.end_init()
return self.motor.position()
def getDialPosition(self):
self.end_init()
return self.getPosition()
def move(self, position):
self.end_init()
self.updateState("MOVING")
self.motor.move(position, wait=False)
def moveRelative(self, relativePosition):
self.move(self.getPosition() + relativePosition)
def syncMoveRelative(self, relative_position, timeout=None):
return self.syncMove(self.getPosition() + relative_position)
def waitEndOfMove(self, timeout=None):
with gevent.Timeout(timeout):
self.motor.wait_move()
def syncMove(self, position, timeout=None):
self.move(position)
self.waitEndOfMove(timeout)
def motorIsMoving(self):
self.end_init()
return self.motorState == RobodiffMotor.MOVING
def getMotorMnemonic(self):
self.end_init()
return self.motor_name
def stop(self):
self.end_init()
self.motor.stop()