Skip to content

Commit f889cf2

Browse files
committed
Create stepper.py
1 parent f9355d0 commit f889cf2

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

stepper.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import RPi.GPIO as GPIO
2+
import time
3+
import sys
4+
5+
GPIO.setmode(GPIO.BCM)
6+
GPIO.setwarnings(False)
7+
8+
enable_pin = 18
9+
coil_A_1_pin = 4
10+
coil_A_2_pin = 17
11+
coil_B_1_pin = 23
12+
coil_B_2_pin = 24
13+
14+
GPIO.setup(enable_pin, GPIO.OUT)
15+
GPIO.setup(coil_A_1_pin, GPIO.OUT)
16+
GPIO.setup(coil_A_2_pin, GPIO.OUT)
17+
GPIO.setup(coil_B_1_pin, GPIO.OUT)
18+
GPIO.setup(coil_B_2_pin, GPIO.OUT)
19+
20+
GPIO.output(enable_pin, 1)
21+
22+
def forward(delay, steps):
23+
for i in range(0, steps):
24+
setStep(1, 0, 1, 0)
25+
time.sleep(delay)
26+
setStep(0, 1, 1, 0)
27+
time.sleep(delay)
28+
setStep(0, 1, 0, 1)
29+
time.sleep(delay)
30+
setStep(1, 0, 0, 1)
31+
time.sleep(delay)
32+
33+
def backwards(delay, steps):
34+
for i in range(0, steps):
35+
setStep(1, 0, 0, 1)
36+
time.sleep(delay)
37+
setStep(0, 1, 0, 1)
38+
time.sleep(delay)
39+
setStep(0, 1, 1, 0)
40+
time.sleep(delay)
41+
setStep(1, 0, 1, 0)
42+
time.sleep(delay)
43+
44+
def setStep(w1, w2, w3, w4):
45+
GPIO.output(coil_A_1_pin, w1)
46+
GPIO.output(coil_A_2_pin, w2)
47+
GPIO.output(coil_B_1_pin, w3)
48+
GPIO.output(coil_B_2_pin, w4)
49+
50+
delay = sys.argv[1]
51+
steps = sys.argv[2]
52+
direction = sys.argv[3]
53+
54+
if direction == 'f':
55+
forward(int(delay) / 1000.0, int(steps))
56+
elif direction == 'b':
57+
backwards(int(delay) / 1000.0, int(steps))

0 commit comments

Comments
 (0)