-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilterWheel.py
More file actions
110 lines (95 loc) · 3.32 KB
/
Copy pathfilterWheel.py
File metadata and controls
110 lines (95 loc) · 3.32 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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 24 21:01:25 2014
@author: Kayvan Forouhesh
@copyright, Kayvan Tehrani and Peter Kner, University of Georgia, 2019
"""
import serial
import time
error_code = {'R':'Recieved','D':'Done','O':'Value Out of range','E':'Wheel Timed out did not reach the location specified'}
class filter_wheel():
def __init__(self,portS='COM5'):
self.timeout=500;
self.ser = serial.Serial()
self.ser.port=portS
self.ser.close()
self.ser.baudrate=9600
self.ser.parity=serial.PARITY_NONE
self.ser.stopbits=1
self.ser.bytesize=8
if(not self.ser.isOpen()):
self.ser.open()
self.res=0
res=self.ser.portstr # check which port was really used
if res==portS:
try:
# self.ser.open()
while not self.ser.isOpen():
pass
print('Filter wheel initialized')
time.sleep(2)
print('Moving to position 2')
err = self.RW('Z',4)
print(error_code[err])
except:
print('error')
else:
print('Port Not Connected')
def RW(self,command,length=2):
'''Enter command without '>'
List of commands:
>I --> Hand shaking, returns <C (long return mode)
>A --> Returns Postion
>Z --> Goes to base point (filter 2)
>Gdn--> Move relative. d is direction (N or P), n is the number of positions.
>Mn --> Move absolute to position n
>R --> Run, position is not updated
>S --> Hard Stop
Returns 'R' when a command is recieved,
'D' when the process is done
'E' when there is an error
'O' Out of range
length is the length of responce requested,
2 for short - e.g. only 'R'
4 for long - e.g. 'R' plus 'D' or 'E'
'''
if length==2:
timeout = 20
else:
timeout = self.timeout
self.ser.flushInput()
command_string = '>%s\n' % command
self.ser.write(command_string.encode())
i=0
while ((self.ser.inWaiting()<length)and(i<timeout)):
i+=1
time.sleep(0.01)
if not i==self.timeout:
time.sleep(0.1)
self.res=self.ser.read(self.ser.inWaiting()).decode()
if length == 2:
n = self.res.find('<')
return self.res[n+1]
elif length == 4:
n = self.res.find('*')
return self.res[n+1]
else:
print('Timed out')
return 'E'
def __del__(self):
self.ser.close()
if(self.ser.isOpen()):
self.ser.close()
print('Port Closed')
def getPosition(self):
val = (self.RW('A',4))
return int(val)
def setPositionq(self,pos):
err = self.RW("M%s" %pos,2)
return error_code[err]
def setPositionf(self,pos):
err = self.RW("M%s" %pos,4)
return error_code[err]
def zero(self):
err = self.RW('Z',4)
return error_code[err]