delsey
Posts: 3
Joined: Tue May 12, 2015 2:34 pm

Controlling a stepper motor and counting the steps

Tue May 12, 2015 3:05 pm

Good afternoon,

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()
#------------------------------------------------------------------------
#------------------------------------------------------------------------
Unfortunately the code doesn’t work for me. I get the following error:

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 ;)

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Controlling a stepper motor and counting the steps

Tue May 12, 2015 8:00 pm

Looks like a couple of things:

A bug in the original code, and you may not be supplying any parrameters when running the script.

First the issue with the code:
The existing code is

Code: Select all

#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)
#------------------------------------------------------------------------
#------------------------------------------------------------------------
This doesn't do what the comment claims. There is nothing there to exit with 0 steps. The code just sets steps to 0 when none are supplied on the command line. No default value is set for direction so any attempt to use it will give the error you're seeing.

One possible fix is to change the code to:

Code: Select all

#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:
    direction = 'left' ## new line to set default direction
    steps = 0
 
#print which direction and how many steps 
print("You told me to turn %s %s steps.") % (direction, steps)
#------------------------------------------------------------------------
#------------------------------------------------------------------------
As for the command line, try this: (taken from the comments at the top of the code)

Code: Select all

sudo python countingsteps.py left 1600
If that doesn't help, try contacting the code's author directly.
Arguing with strangers on the internet since 1993.

delsey
Posts: 3
Joined: Tue May 12, 2015 2:34 pm

Re: Controlling a stepper motor and counting the steps

Wed May 13, 2015 4:20 pm

Thanks for the solution. Now it works!

I want to add one micro switch to the circuit for its home position. If the switch is pressed then the 'StepCounter' must be set to zero. This would be his home position.

If the maximum number of steps is for example 200 and the micro switch is pressed at his begin position, then I can rotate it as I wanted.

How would I implement this in the existing code? Any links or information how I can set this up easily is welcome.

delsey
Posts: 3
Joined: Tue May 12, 2015 2:34 pm

Re: Controlling a stepper motor and counting the steps

Mon May 18, 2015 7:35 pm

Good evening,

I want to rotate the stepper motor to the left till he press the micro switch. If the micro switch is pressed then the StepCounter would be equal to zero. That would be his home position. Then I can enter the direction to the right with the number of steps. If the stepper motor have finished the number of steps to the right, it then must return to the left, to his home position. I’m still trying to program that, but any help would be appreciated.

Return to “Python”