forked from mxcube/HardwareObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobodiffMotorWPositions.py
106 lines (71 loc) · 3.44 KB
/
RobodiffMotorWPositions.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
from RobodiffMotor import RobodiffMotor
import math
import logging
import time
import gevent
import types
class RobodiffMotorWPositions(RobodiffMotor):
def __init__(self, name):
RobodiffMotor.__init__(self, name)
def init(self):
RobodiffMotor.init(self)
self.predefinedPositions = {}
self.predefinedPositionsNamesList = []
self.delta = self.getProperty('delta') or 0
try:
positions = self['positions']
except:
logging.getLogger("HWR").error('%s does not define positions.', str(self.name()))
else:
for definedPosition in positions:
positionUsername = definedPosition.getProperty('username')
try:
offset = float(definedPosition.getProperty('offset'))
except:
logging.getLogger("HWR").warning('%s, ignoring position %s: invalid offset.', str(self.name()), positionUsername)
else:
self.predefinedPositions[positionUsername] = offset
self.sortPredefinedPositionsList()
def getPositionsData(self):
return self["positions"]
def connectNotify(self, signal):
RobodiffMotor.connectNotify(self, signal)
if signal == 'predefinedPositionChanged':
positionName = self.getCurrentPositionName()
try:
pos = self.predefinedPositions[positionName]
except KeyError:
self.emit(signal, ('', None))
else:
self.emit(signal, (positionName, pos))
def sortPredefinedPositionsList(self):
self.predefinedPositionsNamesList = list(self.predefinedPositions.keys())
self.predefinedPositionsNamesList.sort(lambda x, y: int(round(self.predefinedPositions[x] - self.predefinedPositions[y])))
def updateState(self, state=None, emit=False):
RobodiffMotor.updateState(self, state)
if self.motorState==RobodiffMotor.READY:
pos = self.getPosition()
for positionName in self.predefinedPositions:
if self.predefinedPositions[positionName] >= pos-self.delta and self.predefinedPositions[positionName] <= pos+self.delta:
self.emit('predefinedPositionChanged', (positionName, pos, ))
return
self.emit('predefinedPositionChanged', ('', None, ))
def getPredefinedPositionsList(self):
return self.predefinedPositionsNamesList
def moveToPosition(self, positionName):
try:
self.move(self.predefinedPositions[positionName])
except:
logging.getLogger("HWR").exception('Cannot move motor %s: invalid position name.', str(self.userName()))
def getCurrentPositionName(self):
if not self.motorIsMoving(): #self.isReady() and self.getState() == self.READY:
for positionName in self.predefinedPositions:
if self.predefinedPositions[positionName] >= self.getPosition()-self.delta and self.predefinedPositions[positionName] <= self.getPosition()+self.delta:
return positionName
return ''
def setNewPredefinedPosition(self, positionName, positionOffset):
try:
self.predefinedPositions[str(positionName)] = float(positionOffset)
self.sortPredefinedPositionsList()
except:
logging.getLogger("HWR").exception('Cannot set new predefined position')