-
Notifications
You must be signed in to change notification settings - Fork 3
/
m2.py
230 lines (190 loc) · 6.49 KB
/
m2.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import time
import pyinterface
import threading
class m2_controller(object):
#reference /src/obs/modules/hal/subref.cpp
#micron is controlled by time
error = []
m_pos = 0.0
CW = 0x10
CCW = 0x11
LIMIT_CW = 1000
LIMIT_CCW = -1000
PULSRATE = 80 #1puls = 0.1micron
MOTOR_SPEED = 200 # * 10pulses/sec
m_limit_up = 1
m_limit_down = 1
def __init__(self):
pass
def open(self, ndev = 2):
self.dio = pyinterface.create_gpg2000(ndev)
self.InitIndexFF()
self.get_pos()
pass
def print_msg(self, msg):
print(msg)
return
def print_error(self, msg):
self.error.append(msg)
self.print_msg('!!!! ERROR !!!! ' + msg)
return
def get_pos(self):
buff = []
buff2 = []
bin = self.dio.ctrl.in_byte("FBIDIO_IN1_8")
bin2 = self.dio.ctrl.in_byte("FBIDIO_IN9_16")
if bin2 & 0x40:
self.m_limit_up = 1
else:
self.m_limit_up = 0
if bin2 & 0x80:
self.m_limit_down = 1
else:
self.m_limit_down = 0
for i in range(8):
if i != 0 and bin == 0:
buff.append(0)
if bin % 2 == 0:
buff.append(0)
else:
buff.append(1)
bin = int(bin/2)
for i in range(8):
if i != 0 and bin2 == 0:
buff2.append(0)
if bin2 % 2 == 0:
buff2.append(0)
else:
buff2.append(1)
bin2 = int(bin2/2)
#calculate each digit
total = (buff[0]*1+buff[1]*2+buff[2]*pow(2.,2.)+buff[3]*pow(2.,3.))/100.0
total = total + (buff[4]*1+buff[5]*2+buff[6]*pow(2.,2.)+buff[7]*pow(2.,3.))/10.0
total2 = buff2[0]*1+buff2[1]*2+buff2[2]*pow(2.,2.)+buff2[3]*pow(2.,3.)
total2 = total2 + (buff2[4]*1)*10
self.m_pos = (total+total2)*pow(-1.,(buff2[5]+1))
return self.m_pos
def read_pos(self):
return self.m_pos
def Strobe(self):
time.sleep(0.01)
self.dio.ctrl.out_byte("FBIDIO_OUT9_16", 0x01)
time.sleep(0.01)
self.dio.ctrl.out_byte("FBIDIO_OUT9_16", 0x00)
time.sleep(0.01)
return
def StrobeHOff(self):
time.sleep(0.01)
self.dio.ctrl.out_byte("FBIDIO_OUT9_16", 0x05)
time.sleep(0.01)
self.dio.ctrl.out_byte("FBIDIO_OUT9_16", 0x04)
time.sleep(0.01)
return
def move(self, dist):
#move subref
puls = int(dist) * self.PULSRATE
ret = self.get_pos()
if dist/1000.+float(ret) <= -4.0 or dist/1000.+float(ret) >= 5.5:
self.print_error("move limit")
return
if self.m_limit_up == 0 and puls < 0:
self.print_error("can't move up direction")
return
if self.m_limit_down == 0 and puls > 0:
self.print_error("can't move down direction")
return
self.MoveIndexFF(puls)
self.get_pos()
return
def InitIndexFF(self):
#initialization?
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x08)
self.StrobeHOff()
#step no.
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0xff)
self.StrobeHOff()
#vs set
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x48)
self.StrobeHOff()
#5(*10=50)
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x00)
self.StrobeHOff()
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x05)
self.StrobeHOff()
#vr set
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x40)
self.StrobeHOff()
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x00)
self.StrobeHOff()
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", self.MOTOR_SPEED)
self.StrobeHOff()
#su-sd set
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x50)
self.StrobeHOff()
#100(/10=10)
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x00)
self.StrobeHOff()
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 100)
self.StrobeHOff()
#position set
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0xc0)
self.StrobeHOff()
#cw
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", self.CW)
self.StrobeHOff()
#0
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x00)
self.StrobeHOff()
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x00)
self.StrobeHOff()
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x00)
self.StrobeHOff()
#start
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x18)
self.StrobeHOff()
return
def MoveIndexFF(self, puls):
if puls >= -65535 and puls <= 65535:
#index mode
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x08)
self.Strobe()
#step no.
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0xff)
self.Strobe()
#position set
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0xc0)
self.Strobe()
#direction
if puls >= 0:
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", self.CW)
self.Strobe()
else:
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", self.CCW)
self.Strobe()
#displacement
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x00)
self.Strobe()
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", (abs(puls) / 256))
self.Strobe()
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", (abs(puls) % 256))
self.Strobe()
#start
self.dio.ctrl.out_byte("FBIDIO_OUT1_8", 0x18)
self.Strobe()
time.sleep((abs(puls) / self.MOTOR_SPEED / 10.) + 1.)
self.print_msg("Motor stopped")
else:
self.print_msg("Puls number is over.")
return False
return True
def m2_client(host, port):
client = pyinterface.server_client_wrapper.control_client_wrapper(m2_controller, host, port)
return client
def m2_monitor_client(host, port):
client = pyinterface.server_client_wrapper.monitor_client_wrapper(m2_controller, host, port)
return client
def start_m2_server(port1 = 9999, port2 = 9998):
m2 = m2_controller()
server = pyinterface.server_client_wrapper.server_wrapper(m2,'', port1, port2)
server.start()
return server