-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMotor_Control_2D.py
258 lines (192 loc) · 7.57 KB
/
Motor_Control_2D.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 24 14:29:25 2017
@author: Jia Han
3D motor control for processing chamber
"""
import math
from Motor_Control_1D import Motor_Control
import time
import numpy
import logging
from scipy.optimize import minimize
# TODO: fix calculate_velocity to read from cm_per_turn directly
#############################################################################################
#############################################################################################
"""
Class Motor_Control_3D: controls 3D probe drive with 3-motors (x,y,z) using Motor_Control_1D
functions: wait_for_motion_complete, test_limit, probe_to_motor, calculate_velocity, set_movement_velocity
properties: motor_velocity, motor_positions, stop_now, reset_motor, set_zero, enable, disable
"""
class Motor_Control_2D:
def __init__(self, x_ip_addr = None, y_ip_addr = None):
self.x_mc = Motor_Control(verbose=True, server_ip_addr= x_ip_addr, name='x', cm_per_turn = 0.254)
self.y_mc = Motor_Control(verbose=True, server_ip_addr= y_ip_addr, name='y', cm_per_turn = 0.508)
# Velmex model number NN10-0300-E01-21 (short black linear drives)
self.probe_in = 62.948 # LAPD=>58.771 # Distance from chamber wall to chamber center
self.poi = 125.3624 #112.762 # Length of probe outside the chamber from pivot to end
self.ph = 20 # Height from probe shaft to where z-motor actually moves
self.motor_velocity = 4,4
#-------------------------------------------------------------------------------------------
"""
Set and get the target velocity of motor in units of rev/sec
"""
@property
def motor_velocity(self):
return self.x_mc.motor_speed, self.y_mc.motor_speed
@motor_velocity.setter
def motor_velocity(self, v):
xv, yv = v
self.x_mc.motor_speed = xv
self.y_mc.motor_speed = yv
#-------------------------------------------------------------------------------------------
"""
Set motor velocity according to its current position and target position
"""
def set_movement_velocity(self, motor_x, motor_y):
x_m, y_m = self.motor_positions
# distance between current motor position to final motor position
delta_x = abs(motor_x - x_m)
delta_y = abs(motor_y - y_m)
# calculate velocity such that probe moves on a straight line
v_motor_x, v_motor_y = self.calculate_velocity(delta_x, delta_y)
# Set motor velocity accordingly
self.motor_velocity = v_motor_x, v_motor_y
"""
Convert probe velocity vector to motor velocity vector
"""
def calculate_velocity(self, del_x, del_y):
default_speed = 4
# print("delta x : %.3f, delta y : %.3f, delta z : %.3f"%(del_x, del_y, del_z))
del_r = math.sqrt(del_x**2 + del_y**2)
if del_r == 0:
v_x, v_y, v_z = 0, 0, 0
else:
v_x = default_speed * del_x / del_r
v_y = default_speed * del_y / del_r
v_motor_x = v_x * 2 # factor of 2 due to different cm_per_turn
v_motor_y = v_y
v_motor_x = round(v_motor_x,3)
v_motor_y = round(v_motor_y,3)
return v_motor_x, v_motor_y
#--------------------------------------------------------------------------------------------------
"""
Set and get current motor position
"""
@property
def motor_positions(self):
return self.x_mc.motor_position, self.y_mc.motor_position
@motor_positions.setter
def motor_positions(self, mpos):
x_m, y_m = mpos
self.x_mc.motor_position = x_m
self.y_mc.motor_position = y_m
self.wait_for_motion_complete()
#-------------------------------------------------------------------------------------------
"""
Execute after move command send to motor. Wait till motor stops moving.
"""
def wait_for_motion_complete(self):
timeout = time.time() + 300
while True :
try:
x_stat = self.x_mc.motor_status
y_stat = self.y_mc.motor_status
x_not_moving = x_stat.find('M') == -1
y_not_moving = y_stat.find('M') == -1
if x_not_moving and y_not_moving:
time.sleep(0.2)
break
elif time.time() > timeout:
raise TimeoutError("Motor has been moving for over 5min???")
time.sleep(0.2)
except KeyboardInterrupt:
self.x_mc.stop_now
self.y_mc.stop_now
print('\n______Motor stopped and Halted due to Ctrl-C______')
raise KeyboardInterrupt
#--------------------------------------------------------------------------------------------------
@property
def stop_now(self): # Stop motor movement immediatly
self.x_mc.stop_now
self.y_mc.stop_now
@property
def set_zero(self): # Set current position to zero
self.x_mc.set_zero
self.y_mc.set_zero
@property
def reset_motor(self): # Similar to restart all motors
self.x_mc.reset_motor
self.y_mc.reset_motor
@property
def motor_alarm(self):
x_al = self.x_mc.check_alarm
y_al = self.y_mc.check_alarm
return x_al, y_al
#-------------------------------------------------------------------------------------------
"""
Convert probe space dimensions to motor movement in unit of cm
"""
def probe_to_motor_LAPD(self, x, y):
x = self.probe_in - x
D = math.sqrt(x**2 + y**2)
d2 = self.ph/math.sqrt((y/x)**2+1)
Ltc = y/x * d2
motor_x = D - self.probe_in
motor_y = self.ph - d2 - (self.poi + Ltc)*y/x
return motor_x, motor_y
"""
convert encoder feedback from motor to actual probe position using scipy.minimize
"""
def motor_to_probe(self, motor_x, motor_y):
def distance_fun(r1, r2):
a = numpy.array(r1)
b = numpy.array(r2)
return numpy.linalg.norm(a-b)
def fun(x, *args):
mx,my = self.probe_to_motor_LAPD(x[0],x[1])
return distance_fun((mx,my), args)
args = [motor_x, motor_y]
X0 = [motor_x, motor_y]
res = minimize(fun, X0, (args,), options={'maxiter':15000}, method='BFGS')
return round(res.x[0],3), round(res.x[1],3)
#-------------------------------------------------------------------------------------------------
"""
Set probe position by moving three motors through calculation using probe_to_motor
"""
@property
def probe_positions(self):
x_m, y_m = self.motor_positions
return self.motor_to_probe(x_m, y_m)
@probe_positions.setter
def probe_positions(self, pos):
xpos, ypos= pos
# Convert probe distance to motor distance
motor_x, motor_y = self.probe_to_motor_LAPD(xpos, ypos)
#Set movement velocity
self.set_movement_velocity(motor_x, motor_y)
# Move motor
self.motor_positions = motor_x, motor_y
#-------------------------------------------------------------------------------------------------
"""
Note: only X and Y motor are enabled/disabled here because Z motor doesn't come back after been disabled. Need to fix!
"""
@property
def enable(self):
self.x_mc.enable
self.y_mc.enable
if self.x_mc.check_alarm == True:
self.x_mc.clear_alarm
if self.y_mc.check_alarm == True:
self.y_mc.clear_alarm
@property
def disable(self):
self.x_mc.disable
self.y_mc.disable
#===============================================================================================================================================
#<o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o> <o>
#===============================================================================================================================================
if __name__ == '__main__': # standalone testing:
mc = Motor_Control_2D(x_ip_addr = "192.168.0.40", y_ip_addr = "192.168.0.50", z_ip_addr = "192.168.0.60")
mc.probe_positions