-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpriorxyP35.py
200 lines (181 loc) · 5.78 KB
/
priorxyP35.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
@copyright, Peter Kner, University of Georgia, 2019
"""
basepath = 'C:\\Documents and Settings\\kner\\My Documents\\python\\'
import os,sys,time
try:
sys.path.index(basepath)
except:
sys.path += [basepath]
#from Priithon.all import F, Y, N, U
import serial
def prior_test():
ser = serial.Serial(
port='COM1',
baudrate=9600,
bytesize=8,
parity=serial.PARITY_NONE,
stopbits=1,
timeout=2,
xonxoff=0,
rtscts=0)
try:
ser.open()
ser.isOpen()
finally:
print("closing port")
ser.close()
return True
class prior(object):
priorcodes = {
1 : 'NO STAGE',
2 : 'NOT IDLE',
3 : 'NO DRIVE',
4 : 'STRING PARSE',
5 : 'COMMAND NOT FOUND',
6 : 'INVALID SHUTTER',
7 : 'NO FOCUS',
8 : 'VALUE OUT OF RANGE',
9 : 'INVALID WHEEL',
10 : 'ARG1 OUT OF RANGE',
11 : 'ARG2 OUT OF RANGE',
12 : 'ARG3 OUT OF RANGE',
13 : 'ARG4 OUT OF RANGE',
14 : 'ARG5 OUT OF RANGE',
15 : 'ARG6 OUT OF RANGE',
16 : 'INCORRECT STATE',
17 : 'WHEEL NOT FITTED',
18 : 'QUEUE FULL',
19 : 'COMPATIBILITY MODE SET',
20 : 'SHUTTER NOT FITTED',
21 : 'INVALID CHECKSUM',
60 : 'ENCODER ERROR',
61 : 'ENCODER RUN OFF'
}
def __init__(self):
self.ser = serial.Serial(port='COM1',baudrate=9600,bytesize=8,parity=serial.PARITY_NONE,stopbits=1,timeout=2,xonxoff=0,rtscts=0)
try:
# self.ser.open()
print(self.ser.isOpen())
except:
print("Prior XY Stage not available!")
raise
# setup some stuff
self.limits = [-80000,80000,-50000,50000] # soft limits to prevent bad things [xmin,xmax,ymin,ymax]
#self.limits=[-180000, -125000, -65000, -10000]
# get current position
a=self.getPosition()
print('Current Position is (%d,%d)' % (self.loc[0],self.loc[1]))
def __del__(self):
self.ser.close()
def getPosition(self):
self.ser.flushInput()
self.ser.write(b'PS\r')
time.sleep(0.1)
t = self.ser.read(self.ser.inWaiting()).decode()
if self.error(t):
raise Exception('Command returned an error!')
self.loc = list(map(float,(t.rstrip('\r')).split(',')))
return self.loc
def MoveRelX(self,d=5):
''' move by 5 microns '''
if self.checksoftlimit(d,0):
print('Move will put you outside soft limits!')
d = 0
comstr = 'GR,%d,0,\r' % d
self.ser.flushInput()
self.ser.write(comstr.encode())
time.sleep(0.1)
t = self.ser.read(self.ser.inWaiting()).decode()
if self.error(t):
raise Exception('Command returned an error!')
return t.strip('\r')
def MoveRelY(self,d=5):
''' move by 5 microns '''
if self.checksoftlimit(0,d):
print('Move will put you outside soft limits!')
d = 0
comstr = 'GR,0,%d,\r' % d
self.ser.flushInput()
self.ser.write(comstr.encode())
time.sleep(0.1)
t = self.ser.read(self.ser.inWaiting()).decode()
if self.error(t):
raise Exception('Command returned an error!')
return t.strip('\r')
def MoveTo(self,x,y):
''' Go to point A(x,y) '''
if self.checksoftlimitAbs(x,y):
raise Exception('Out of bounds!')
else:
cur_pos=self.getPosition()
dx=x-cur_pos[0]
dy=y-cur_pos[1]
comstr = 'GR,%d,%d,\r' % (dx,dy)
self.ser.flushInput()
self.ser.write(comstr.encode())
time.sleep(0.1)
t = self.ser.read(self.ser.inWaiting()).decode()
if self.error(t):
raise Exception('Command returned an error!')
return t.strip('\r')
def MoveAbs(self,dx,dy):
if self.checksoftlimit(dx,dy):
raise Exception('Out of bounds!')
else:
comstr = 'GR,%d,%d,\r' % (dx,dy)
self.ser.flushInput()
self.ser.write(comstr.encode())
time.sleep(0.1)
t = self.ser.read(self.ser.inWaiting()).decode()
if self.error(t):
raise Exception('Command returned an error!')
return t.strip('\r')
def checkhardlimits(self):
# Check hard limits
self.ser.flushInput()
self.ser.write(b'LMT\r')
time.sleep(0.1)
t = self.ser.read(self.ser.inWaiting()).decode()
if self.error(t.decode()):
raise Exception('Command returned an error!')
x = float('0x'+t.strip('\r'))
if x==0:
hardlimit = False
else:
hardlimit = True
hardlimitcode = x
return hardlimit
def checksoftlimit(self,dx,dy):
out = False
x = self.loc[0] + dx
y = self.loc[1] + dy
if (x<self.limits[0]):
out = True
if (x>self.limits[1]):
out = True
if (y<self.limits[2]):
out = True
if (y>self.limits[3]):
out = True
return out
def checksoftlimitAbs(self,x,y):
out = False
if (x<self.limits[0]):
out = True
if (x>self.limits[1]):
out = True
if (y<self.limits[2]):
out = True
if (y>self.limits[3]):
out = True
return out
def error(self,op):
op = (op.rstrip('\r'),)
if op[0]=='E':
err = True
code = int((t.rstrip('\r'))[t.find(',')+1:])
print(priorcodes[code])
else:
err = False
return err