#!/usr/bin/python
#!/usr/bin/env python
# Import required libraries
import time
import RPi.GPIO as GPIO
import math
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Define GPIO signals to use
# Pins 18,22,24,26
# GPIO24,GPIO25,GPIO8,GPIO7
MotorPinsA = [24,25,8,7]
MotorPinsB = [22,10,9,11]
switchL = 18
switchR = 23
# Set all pins as output
for pin in MotorPinsA:
#print "Setup A pins"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
for pin in MotorPinsB:
#print "Setup B pins"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
GPIO.setup(switchL, GPIO.IN)
GPIO.setup(switchR, GPIO.IN)
# Define some settings
StepCounter = 0
WaitTime = 0.005
# Define simple sequence
StepCountCCW = 4
SeqCCW = []
SeqCCW = range(0, StepCountCCW)
SeqCCW[0] = [0,0,0,1]
SeqCCW[1] = [0,0,1,0]
SeqCCW[2] = [0,1,0,0]
SeqCCW[3] = [1,0,0,0]
StepCountCW = 4
SeqCW = []
SeqCW = range(0, StepCountCW)
SeqCW[0] = [1,0,0,0]
SeqCW[1] = [0,1,0,0]
SeqCW[2] = [0,0,1,0]
SeqCW[3] = [0,0,0,1]
# Define advanced sequence
# as shown in manufacturers datasheet
StepCountMCW = 8
SeqMCW = []
SeqMCW = range(0, StepCountMCW)
SeqMCW[0] = [1,0,0,0]
SeqMCW[1] = [1,1,0,0]
SeqMCW[2] = [0,1,0,0]
SeqMCW[3] = [0,1,1,0]
SeqMCW[4] = [0,0,1,0]
SeqMCW[5] = [0,0,1,1]
SeqMCW[6] = [0,0,0,1]
SeqMCW[7] = [1,0,0,1]
# Define advanced sequence
# as shown in manufacturers datasheet
StepCountMCCW = 8
SeqMCCW = []
SeqMCCW = range(0, StepCountMCCW)
SeqMCCW[0] = [0,0,0,1]
SeqMCCW[1] = [0,0,1,1]
SeqMCCW[2] = [0,0,1,0]
SeqMCCW[3] = [0,1,1,0]
SeqMCCW[4] = [0,1,0,0]
SeqMCCW[5] = [1,1,0,0]
SeqMCCW[6] = [1,0,0,0]
SeqMCCW[7] = [1,0,0,1]
# Choose a sequence to use
Seq = SeqCW
StepCount = StepCountCW
# Start main loop
def movingStep(Motor, Seq, StepCount, Angal, Desc):
StepCounter = 0;
MSteps = int(math.ceil(Angal * 5.7222));
print(Desc, "Steps : %i" %(MSteps))
for moving in range(0, MSteps):
for pin in range(0, 4):
xpin = Motor[pin]
if Seq[StepCounter][pin]!=0:
#print " Step %i Enable %i" %(StepCounter,xpin)
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
StepCounter += 1
# If we reach the end of the sequence
# start again
if (StepCounter>=StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount
# Wait before moving on
time.sleep(WaitTime)
#------------------
#
# 2060 Step for one cycle
# 515 - 90 Degree
# 1030 - 180 Degree
# 1545 - 270 Degree
#
#-------------------
try:
while 11==11:
movingStep(MotorPinsA, SeqCW, StepCountCW, 90, "Motor A, Clockwise")
time.sleep(0.5);
movingStep(MotorPinsA, SeqCCW, StepCountCCW, 90, "Motor A, Anti-Clockwise")
time.sleep(0.5);
movingStep(MotorPinsB, SeqCW, StepCountCW, 90, "Motor B, Clockwise")
time.sleep(0.5);
movingStep(MotorPinsB, SeqCCW, StepCountCCW, 90, "Motor B, Anti-Clockwise")
time.sleep(0.5);
while 111==11:
movingStep(MotorPinsA, SeqMCW, StepCountMCW, 90 * 2, "Motor A, Clockwise")
time.sleep(0.5);
movingStep(MotorPinsA, SeqMCCW, StepCountMCCW, 90 * 2, "Motor A, Anti-Clockwise")
time.sleep(0.5);
except KeyboardInterrupt:
pass