I’m trying to controlling a bipolar stepper motor with a DRV8825 driver and a Raspberry Pi B+ with the latest Raspbian (02-2015) on it. I was able to control the motor with a simple Python code. However I have found a much better Python code on the internet which also counts the steps of the stepper motor:
http://rowboboat.com/bay2/2015/01/02/py ... r-example/
Code: Select all
#Step 0: Preamble
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#Program Title : easy_stepper.py
#Code Written by: Salty Scott
#Current Project: www.rowboboat.com
#This code is a very basic example of using python to control a spark fun
# easy driver. The spark fun easy driver that I am using in this example
# is connected to a 42HS4013A4 stepper motor and my raspberry pi. Pin 23
# is the direction control and pin 24 is the step control. I am using
# these components in the www.rowboboat.com project version 2.0 and I
# hope someone finds this a useful and simple example.
# This program expects two arguments: direction and steps
# Example usage: sudo python easy_stepper.py left 1600
# The above example would turn a 200 step motor one full revolution as by
# default the easy driver 4.4 is in 1/8 microstep mode.
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#Step 1: Import necessary libraries
#------------------------------------------------------------------------
#------------------------------------------------------------------------
import sys
import RPi.GPIO as gpio #https://pypi.python.org/pypi/RPi.GPIO more info
import time
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#Step 2: Read arguements https://www.youtube.com/watch?v=kQFKtI6gn9Y
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#read the direction and number of steps; if steps are 0 exit
try:
direction = sys.argv[1]
steps = int(float(sys.argv[2]))
except:
steps = 0
#print which direction and how many steps
print("You told me to turn %s %s steps.") % (direction, steps)
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#Step 3: Setup the raspberry pi's GPIOs
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#use the broadcom layout for the gpio
gpio.setmode(gpio.BCM)
#GPIO23 = Direction
#GPIO24 = Step
gpio.setup(23, gpio.OUT)
gpio.setup(24, gpio.OUT)
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#Step 4: Set direction of rotation
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#set the output to true for left and false for right
if direction == 'left':
gpio.output(23, True)
elif direction == 'right':
gpio.output(23, False)
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#Step 5: Setup step counter and speed control variables
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#track the numebr of steps taken
StepCounter = 0
#waittime controls speed
WaitTime = 0.000001
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#Step 6: Let the magic happen
#------------------------------------------------------------------------
#------------------------------------------------------------------------
# Start main loop
while StepCounter < steps:
#turning the gpio on and off tells the easy driver to take one step
gpio.output(24, True)
gpio.output(24, False)
StepCounter += 1
#Wait before taking the next step...this controls rotation speed
time.sleep(WaitTime)
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#Step 7: Clear the GPIOs so that some other program might enjoy them
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#relase the GPIO
gpio.cleanup()
#------------------------------------------------------------------------
#------------------------------------------------------------------------File “/home/pi/countingsteps.py”, line 40, in <module>
Print(“Yout told me to turn %s %s steps.”) % (direction, steps)
NameError: name ‘direction’ is not defined.
Also if I comment the sentence out it give me another error:
File “/home/pi/countingsteps.py”, line 62, in <module>
If direction == ‘left’:
NameError: name ‘direction’ is not defined.
Is the direction in the code (direction = sys.argv[1]) local or global? like to hear if anyone sees what is wrong