Recently I've been trying to learn how to control stepper motors with Python. I can run one 28BYJ-48-5V at a time easily using PiStep2 Quad using scripts which are readily available (see the list on bottom), however I've been trying to modify the code to run two of these motors simultaneously. Since I'm still quite new to programming with python, I've not been very successful at figuring out how exactly a script running two or more stepper motors simultaneously would be built properly. So, I'd need some advice here.
This is the code I've modified:
Code: Select all
#!/usr/bin/python # Import required libraries import sys import time import RPi.GPIO as GPIO # Use BCM GPIO references # instead of physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO signals to use # Physical pins 11,15,16,18 # GPIO17,GPIO22,GPIO23,GPIO24 StepPins = [17,22,23,24] StepPins2 = [20,26,16,19] # Set all pins as output for pin in StepPins + StepPins2: print "Setup pins" GPIO.setup(pin,GPIO.OUT) GPIO.output(pin, False) # Define advanced sequence # as shown in manufacturers datasheet Seq = [[1,0,0,1], [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]] StepCount = len(Seq) StepDir = 1 # Set to 1 or 2 for clockwise # Set to -1 or -2 for anti-clockwise # Read wait time from command line if len(sys.argv)>1: WaitTime = int(sys.argv)/float(1000) else: WaitTime = 1/float(1000) # Initialise variables StepCounter = 0 # Start main loop while True: #print (StepCounter,) # print (Seq[StepCounter]) for pin in range(0, 8): xpin = StepPins[pin] if Seq[StepCounter][pin]!=0: #print (" Enable GPIO %i" %(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 # Wait before moving on time.sleep(WaitTime)
I have also tried altering the StepPins part like this:
Code: Select all
StepPins = [[17,22,23,24], [20,26,16,19]] # Set all pins as output for pin in StepPins: print "Setup pins" GPIO.setup(pin,GPIO.OUT) GPIO.output(pin, False)
But every alteration this far results in: Traceback (most recent call last): File *path here* in <module> xpin = StepPins[pin] IndexError: list out of range. So I returned to the first alteration and started wondering how should I add StepPins2 into " xpin = StepPins[pin]" because separating them with , or + will not work.
Here is the resources list I've been using so far:
https://www.raspberrypi-spy.co.uk/2012/ ... in-python/
I've been looking at projects and scripts of other hobbyists too, but as a beginner I don't yet want to confuse myself with too far-fetched examples. Unless modifying the existing codes is an attempt doomed to fail. Could this still be made to work?