forked from druyang/tb6600_rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (123 loc) · 3.1 KB
/
main.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
# Main Python File for Testing Stepper motors with the TB 6600
import RPi.GPIO as GPIO
import time
import curses
QUICKDELAY = 0.0003 #Change this depends on your stepper motor
SLOWDELAY = 0.00045
# Change wiring here for your axis (in GPIO)
XDir = 6
XStepPin = 13
XEnable = 5
YDir = 27
YStepPin = 22
YEnable = 17
ZDir = 19
ZStepPin = 4
ZEnable = 26
SERVOPIN = 12
def motor_initialize(dir_pin, step_pin, en_pin):
GPIO.setup(dir_pin, GPIO.OUT)
GPIO.setup(step_pin, GPIO.OUT)
GPIO.setup(en_pin, GPIO.OUT)
GPIO.output(en_pin, GPIO.HIGH)
GPIO.setup(SERVOPIN, GPIO.OUT)
pass
def pulse_y_cw():
for i in range(1000):
GPIO.output(YDir, 0)
GPIO.output(YStepPin, 1)
time.sleep(delay)
GPIO.output(YStepPin, 0)
time.sleep(delay)
print ("Moving Y CW \n")
pass
def pulse_y_ccw():
for i in range(1000):
GPIO.output(YDir, 1)
GPIO.output(YStepPin, 1)
time.sleep(delay)
GPIO.output(YStepPin, 0)
time.sleep(delay)
print ("Moving Y CCW \n")
pass
def pulse_x_cw():
for i in range(5000):
GPIO.output(XDir, 0)
GPIO.output(XStepPin, 1)
time.sleep(delay)
GPIO.output(XStepPin, 0)
time.sleep(delay)
print ("Moving X CCW \n")
pass
def pulse_x_ccw():
for i in range(5000):
GPIO.output(XDir, 1)
GPIO.output(XStepPin, 1)
time.sleep(delay)
GPIO.output(XStepPin, 0)
time.sleep(delay)
print ("Moving X CCW \n")
pass
def pulse_z_cw():
for i in range(100):
GPIO.output(ZDir, 0)
GPIO.output(ZStepPin, 1)
time.sleep(SLOWDELAY)
GPIO.output(ZStepPin, 0)
time.sleep(SLOWDELAY)
print ("Moving Z CW \n")
pass
def pulse_z_ccw():
for i in range(50):
GPIO.output(ZDir, 1)
GPIO.output(ZStepPin, 1)
time.sleep(SLOWDELAY)
GPIO.output(ZStepPin, 0)
time.sleep(SLOWDELAY)
print ("Moving Z CCW \n")
pass
def servo_left():
global p
p.ChangeDutyCycle(2.5)
def servo_right():
global p
p.ChangeDutyCycle(11.5)
def servo_stop():
global p
p.ChangeDutyCycle(7)
delay = QUICKDELAY
screen = curses.initscr()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
motor_initialize(XDir,XStepPin,XEnable)
motor_initialize(YDir,YStepPin,YEnable)
motor_initialize(ZDir,ZStepPin,ZEnable)
p = GPIO.PWM(SERVOPIN, 50)
p.start(5)
print("Initialized. Use the following keys for control\n")
print("WASD: controlling X and Y motors\n")
print(" QE: raising and lowering Z motor\n")
print(" X: to quit the program\n")
while True:
c = screen.getch()
time.sleep(0.03)
if c == ord('w'):
pulse_x_ccw()
elif c == ord('s'):
pulse_x_cw()
elif c == ord('a'):
pulse_y_ccw()
elif c == ord('d'):
pulse_y_cw()
elif c == ord('q'):
pulse_z_ccw()
elif c == ord('e'):
pulse_z_cw()
elif c == ord('z'):
servo_left()
elif c == ord('c'):
servo_right()
elif c == ord('x'):
servo_stop()
elif c == ord('r'):
break # Exit the while loop