-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepper.py
132 lines (108 loc) · 3.32 KB
/
stepper.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
# my first stepper program
# intention: to test the stepper motor
# Kiat Huang <[email protected]>
# Created: 20070427
# License: https://choosealicense.com/licenses/apache-2.0/
#
# Based on
# http://www.raspberrypi-spy.co.uk/2012/07/stepper-motor-control-in-python/
#
# My hardware
# 1. Stepper Motor: 28BJY-48
# 2. Stepper Motor Driver Board: ULN2003
# Bought here: http://www.ebay.fr/itm/292003588929?_trksid=p2057872.m2749.l2649
# Screenshot (in case the item doesn't exist there any more): http://goo.gl/reIvWL
# . Raspberry Pi B+ v1.2 with 40 pins
# Usage
#
# $0 # clockwise, each step is 0.01 seconds
# $0 500 # clockwise, each step is 0.5 seconds
# $0 500 -1 # counter-clockwise, each step is 0.5 seconds
# Features
#
# 1. Makes the motor spin at a certain speed until user interrupts CTRL-C
# 2. On interrupt, resets the GPIOs to LOW
#
# Notes
# 1. Code working with python3.4 on Rasbian
# pi@raspberrypi:~ $ uname -a && cat /etc/debian_version
# Linux raspberrypi 4.4.50+ #970 Mon Feb 20 19:12:50 GMT 2017 armv6l GNU/Linux
# 8.0
# 2. Adapt to your own GPIO pins
# 3. Hardware worked with 3.3V (accidentally) as well as 5V as intended
# 4. Works when the motor spins and the LEDs flash on the board matching Seq
# Import required libraries
import sys
import time
import RPi.GPIO as GPIO
# Define GPIO signals to use
# Physical pins 35, 36, 37, 38
# GPIO19,GPIO16,GPIO26,GPIO20
StepPins = [19,16,26,20]
# Define simple sequence : 4 step seq so 32 x 64 = 2048 steps/rev
Seq1 = [[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]
# Define advanced sequence : 8 step seq so 64 x 64 = 4096 steps/rev
Seq2 = [[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1]]
Seq = Seq1
StepCount = len(Seq)
# Read wait time from the command line
if len(sys.argv)>1:
WaitTime = int(sys.argv[1])/float(1000)
else:
WaitTime = 10/float(1000)
# Read direction, only if wait time is also set
# Set to 1 or 2 for clockwise
# Set to -1 or -2 for anti-clockwise
if len(sys.argv)>2:
StepDir = int(sys.argv[2])
else:
StepDir = 1
# Initialize variables
StepCounter = 0
TotalSteps = 0
# Start main loop
try:
while True:
print (StepCounter, Seq[StepCounter])
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
for pin in StepPins:
print ("Setup Pins")
GPIO.setup(pin, GPIO.OUT)
# GPIO.output(pin, False)
for pin in range(0,4):
xpin = StepPins[pin] # get GPIO
if Seq[StepCounter][pin]!=0:
print ("Enable GPIO ", xpin)
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
StepCounter += StepDir
# If we reach the end of the sequence, start again
if (StepCounter >= StepCount):
StepCounter = 0
if (StepCounter < 0):
StepCounter = StepCount + StepDir
# Count total steps
TotalSteps += 1
# Wait before moving on
time.sleep(WaitTime)
except KeyboardInterrupt:
GPIO.cleanup()
print("Total Steps = ", TotalSteps)
except:
GPIO.cleanup()
if sys.version_info[0] < 3:
print ("Must be using Python 3")
raise