-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStage.py
More file actions
114 lines (87 loc) · 3.06 KB
/
Stage.py
File metadata and controls
114 lines (87 loc) · 3.06 KB
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
from pipython import GCSDevice
from pipython import pitools
import scipy
from scipy import optimize
from scipy.interpolate import interp2d
import numpy as np
import time
class Stage:
X=0
Y=0
Z=0
StagePosFile="Data/stagepos.txt"
__X_Axis=None
__Y_Axis=None
__Z_Axis=None
def __init__(self,LastPos=False,XYZStart=(0,0,3.35)):
Controller = 'E-873'
if(LastPos):
f=open(self.StagePosFile,'r')
XYZStart= np.loadtxt(f)
f.close()
self.__X_Axis = GCSDevice(Controller)
self.__Y_Axis = GCSDevice(Controller)
self.__Z_Axis = GCSDevice(Controller)
self.__Z_Axis.ConnectUSB(serialnum='120002962')
pitools.startup(self.__Z_Axis)
self.__Z_Axis.SVO( self.__Z_Axis.axes, values=True)
time.sleep(0.5)
self.__Z_Axis.FNL(self.__Z_Axis.axes)
time.sleep(2)
self.__X_Axis.ConnectUSB(serialnum='120002968')
pitools.startup(self.__X_Axis)
self.__X_Axis.SVO( self.__X_Axis.axes, values=True)
time.sleep(2)
self.__X_Axis.FNL(self.__X_Axis.axes)
self.__Y_Axis.ConnectUSB(serialnum='120003784')
pitools.startup(self.__Y_Axis)
self.__Y_Axis.SVO( self.__Y_Axis.axes, values=True)
time.sleep(10)
self.MoveToX(XYZStart[0])
self.__Y_Axis.FNL(self.__Y_Axis.axes)
time.sleep(10)
self.MoveToY( XYZStart[1])
self.MoveToZ( XYZStart[2])
self.FocalPlane=None
def MoveToX(self,x):
self.__X_Axis.MOV(self.__X_Axis.axes, x)
self.X=x
self.SavePos()
def MoveToY(self,y):
self.__Y_Axis.MOV(self.__Y_Axis.axes, y)
self.Y=y
self.SavePos()
def MoveToZ(self,z):
self.__Z_Axis.MOV(self.__Z_Axis.axes, z)
self.Z=z
self.SavePos()
def MoveTo(self,x,y,z):
self.MoveToX(x)
self.MoveToY(y)
self.MoveToZ(z)
def GetAxes(self):
return(self.__X_Axis, self.__Y_Axis, self.__Z_Axis)
def SavePos(self):
f=open(self.StagePosFile,'w')
ToSave=np.array([self.X,self.Y,self.Z])
np.savetxt(f,ToSave)
f.close()
def DefineFocalPlane(self,FocalPoints):
xs=FocalPoints[:,0]
ys=FocalPoints[:,1]
zs=FocalPoints[:,2]
# Function of a plane in 2D
def PlaneFunction(x,y, args):
return args[0]*x + args[1]*y + args[2]
# Scalar function to minimize to find best fit plane from focal points
Minimizefunction = lambda args: sum((zs-PlaneFunction(xs,ys, args))**2)
# Minimize and find the best fit plane
res=optimize.minimize(Minimizefunction,[0,0,0])
self.FocalPlane= lambda x,y: PlaneFunction(x,y, res.x)
def FocusAt(self,x,y):
if(self.FocalPlane==None):
print("No focal plane set! Find focal points and try again")
return(False)
self.MoveToX(x)
self.MoveToY(y)
self.MoveToZ(self.FocalPlane(x,y))