-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage.py
136 lines (121 loc) · 5.32 KB
/
stage.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
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 13 12:02:08 2018
@author: lhuismans
"""
import serial
import time
class LudlStage:
"""
This class is for controlling the stage using the MAC5000 from Ludl. So far
this only works using a serial to usb connection and having the converting
piece provided by Teun and Greta in between.
"""
def __init__(self, address):
"""
Initializing the stage, Ludl recommends that the user refrain from use
of the joystick or RS-232 serial communication until completion of
interface initialization routine which is approximately 5 seconds after
power up.
The COM port has to be specified as a string: "COM#".
"""
self.address = address
self.baudrate = 9600
self.endOfLine = '\r'
def getPos(self):
"""
Gets the current position of the stage. A positive reply is of the from:
:A X Y
A negative reply is of the form:
:N -code
See the documentation to find which code corresponds to which error.
Returns the x and y position
"""
command = 'Where X Y' + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO, timeout = 1) as stage:
stage.write(command.encode())
position = stage.readline() #Reads an '\n' terminated line
position = position.decode().split(' ') #Go from bytes to string and split
xPosition = position[1]
yPosition = position[2]
return xPosition, yPosition
def moveAbs(self, x, y):
"""
Moves the motor to an absolute position defined by X and Y.
A positive reply is sent back when the command is received correctly.
The reply does not mean the movement is finished.
"""
command = 'Move X = %d Y = %d' % (x, y) + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO) as stage:
stage.write(command.encode())
def moveVec(self, x, y):
"""
Moves the motor to an absolute position but as opposed to moveAbs it
adjusts the speeds to plot a straight line.
"""
command = "Vmove X = %d Y = %d" % (x, y) + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO) as stage:
stage.write(command.encode())
def moveRel(self, xRel = None, yRel= None):
"""
Moves the motor to a relative position.
Set position to None if this axis is not used.
"""
if xRel == None:
command = "Movrel Y = %d" % yRel + self.endOfLine
elif yRel == None:
command = "Movrel X = %d" % xRel + self.endOfLine
else:
command = "Movrel X = %d Y = %d" % (xRel, yRel) + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO) as stage:
stage.write(command.encode())
def home(self):
"""
Moves to motors toward the end limit switch. This is reached by running
the motor to a large negative position.
If there are no errors a positive reply is sent back.
"""
command = "Home" + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO) as stage:
stage.write(command.encode())
def setZero(self):
"""
Sets the current position as zero position.
"""
command = "Here X = 0 Y = 0" + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO) as stage:
stage.write(command.encode())
def joystick(self, on = True):
"""
Enable or disable the joystick.
"""
switch = "+"
if on == False:
switch = "-"
command = "Joystick X%s Y%s" % (switch, switch) + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO) as stage:
stage.write(command.encode())
def motorsStopped(self):
"""
Checks if the motors are still running.
Reply:
"N" if all motors are stopped. Returns True
"B" if one or more motors are is still running. Returns False
"""
command = "Status" + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO, timeout = 1) as stage:
stage.write(command.encode())
status = stage.readline()
status = status.decode() #Go from bytes to string
if status == "N":
return True
return False
def reset(self):
"""
Resets the whole controller and restart from a power up condition. No
reply is provided.
"""
command = "REMRES" + self.endOfLine
with serial.Serial(self.address, self.baudrate, stopbits = serial.STOPBITS_TWO) as stage:
stage.write(command.encode())
time.sleep(5) #Sleep for 5 seconds to initialize.