-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMotor_control_new.py
More file actions
146 lines (115 loc) · 4.36 KB
/
Motor_control_new.py
File metadata and controls
146 lines (115 loc) · 4.36 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
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
import RPi.GPIO as GPIO
import time
from Bipolar_Stepper_Motor_Class_new import Bipolar_Stepper_Motor
from numpy import abs,sqrt,float
def sign(a): #return the sign of number a
if a > 0:
return 1
elif a < 0:
return -1
else:
return 0
def Single_Motor_Step(stepper, step, speed=100):
# control stepper motor 1 and 2 simultaneously
# stepper1 and stepper2 are objects of Bipolar_Stepper_Motor class
# direction is reflected in the polarity of [step1] or [step2]
dir1 = sign(step) #get dirction from the polarity of argument [step]
step = abs(step)
#T = step / speed #total time
#dt = T / step #time delay every micro_step
stepper.move(dir1,step,1/speed) # don't pass dt here, not working for Z Axis have to figure out why?
return 0
def Motor_Step(stepper1, step1, stepper2, step2, speed):
# control stepper motor 1 and 2 simultaneously
# stepper1 and stepper2 are objects of Bipolar_Stepper_Motor class
# direction is reflected in the polarity of [step1] or [step2]
dir1 = sign(step1) #get dirction from the polarity of argument [step]
dir2 = sign(step2)
step1 = abs(step1)
step2 = abs(step2)
iterator = max(step1, step2)
T = sqrt(step1 ** 2 + step2 ** 2) / speed #total time
dt = T / (step1 + step2) #time delay every micro_step
#Setup the Ratio Values.
M1Ratio = float(step1) / float(iterator)
M2Ratio = float(step2) / float(iterator)
M1Holder = float(0)
M2Holder = float(0)
M1ctr = 0
M2ctr = 0
for i in range(0, iterator):
M1Holder = float(M1Holder) + float(M1Ratio)
M2Holder = float(M2Holder) + float(M2Ratio)
if(M1Holder >= 1):
M1Holder = float(M1Holder) - 1
stepper1.move(dir1,1,dt)
M1ctr = M1ctr + 1
if(M2Holder >= 1):
M2Holder = float(M2Holder) - 1
stepper2.move(dir2,1,dt)
M2ctr = M2ctr + 1
#clean-up any missed or over steps...
if(M1ctr < step1):
stepper1.move(dir1,1,dt)
if(M2ctr < step2):
stepper2.move(dir2,1,dt)
#overstep?
if(M1ctr > step1):
stepper1.move(dir1 * -1,1,dt)
if(M2ctr > step2):
stepper2.move(dir2 * -1,1,dt)
return 0
def Motor_StepThree(stepper1, step1, stepper2, step2,stepper3,step3, speed):
# control stepper motor 1 and 2 simultaneously
# stepper1 and stepper2 are objects of Bipolar_Stepper_Motor class
# direction is reflected in the polarity of [step1] or [step2]
dir1 = sign(step1) #get dirction from the polarity of argument [step]
dir2 = sign(step2)
dir3 = sign(step3)
step1 = abs(step1)
step2 = abs(step2)
step3 = abs(step3)
iterator = max(step1, step2, step3)
T = sqrt(step1 ** 2 + step2 ** 2) / speed #total time
dt = T / (step1 + step2) #time delay every micro_step
#Setup the Ratio Values.
M1Ratio = float(step1) / float(iterator)
M2Ratio = float(step2) / float(iterator)
M3Ratio = float(step3) / float(iterator)
M1Holder = float(0)
M2Holder = float(0)
M3Holder = float(0)
M1ctr = 0
M2ctr = 0
M3ctr = 0
for i in range(0, iterator):
M1Holder = float(M1Holder) + float(M1Ratio)
M2Holder = float(M2Holder) + float(M2Ratio)
M3Holder = float(M3Holder) + float(M3Ratio)
if(M1Holder >= 1):
M1Holder = float(M1Holder) - 1
stepper1.move(dir1,1,dt)
M1ctr = M1ctr + 1
if(M2Holder >= 1):
M2Holder = float(M2Holder) - 1
stepper2.move(dir2,1,dt)
M2ctr = M2ctr + 1
if(M3Holder >= 1):
M3Holder = float(M3Holder) - 1
stepper3.move(dir3,1)
M3ctr = M3ctr + 1
#clean-up any missed or over steps...
if(M1ctr < step1):
stepper1.move(dir1,1,dt)
if(M2ctr < step2):
stepper2.move(dir2,1,dt)
if(M3ctr < step3):
stepper3.move(dir3,1)
#overstep?
if(M1ctr > step1):
stepper1.move(dir1 * -1,1,dt)
if(M2ctr > step2):
stepper2.move(dir2 * -1,1,dt)
if(M3ctr > step3):
stepper3.move(dir3 * -1,1)
return 0