msheald
Posts: 15
Joined: Sun Dec 31, 2017 7:45 pm

Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Sun Dec 31, 2017 8:02 pm

Hello! I'm new to Python and Raspberry Pi.

As I try to learn Python, I'm trying to get my two 5-volt, 32-step unipolar 5-wire steppers to work with the Adafruit MotorHAT.

When I run the StepperTest.py program, the motors turn, although the speed seems slow.

I will want to steppers to turn continuously rather than reversing direction as in the test program.

The main program appears to be Adafruit_MotorHAT_Motors.py. I tried to activate it from the directory with sudo python Adafruit_MotorHAT_Motors.py the same way I did StepperTest.py from the examples directory. But nothing happened.

I've got a lot to learn - DOS and Fortran were my languages years ago.

While I learn, I was hoping the folks could help me with my current project.

Do I run the program from the directory with sudo python Adafruit_MotorHAT_Motors.py? (Also, can this be run from the home directory or do I have to be in the directory that has the program?)
What edits to the program I make to the program to let the motors run till I stop the program (control c, correct?) and adjust the speed?

FYI, the project I have in mind is an automatic roof closure system for a small observatory for my LX200 8 inch telescope running unattended so that I can perform variable star work. The enclosure with be about 3 feet by 3 feet by 3 feet. I'll use an IR sensor to compare the sky temperature with the local temperature and close the roof when clouds come in. The steppers will power the roof.

Thank you and best regards.

Mike

pcmanbob
Posts: 2551
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Sun Dec 31, 2017 10:58 pm

if you post the StepperTest.py program then we may be able to explain how to drive your motors in one direction.

please use code tags when posting python code

Add [code] at the top



and [/code] at the bottom.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

msheald
Posts: 15
Joined: Sun Dec 31, 2017 7:45 pm

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Tue Jan 02, 2018 12:06 am

Hello! Thank you for your note. I'll be running two steppers for my project - one on each side of the hinge axis in order to close the roof. As I mentioned above, when I run the program, nothing seemed to happen. The Pi returned quickly to the command line. I did not hear the stepper do anything for that brief moment.

This is the dual stepper motor program for Adafruit MotorHAT from github.

Code: Select all


#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor
import time
import atexit
import threading
import random

# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT()

# create empty threads (these will hold the stepper 1 and 2 threads)
st1 = threading.Thread()
st2 = threading.Thread()


# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
    mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)

atexit.register(turnOffMotors)

myStepper1 = mh.getStepper(200, 1)      # 200 steps/rev, motor port #1
myStepper2 = mh.getStepper(200, 2)      # 200 steps/rev, motor port #1
myStepper1.setSpeed(60)          # 30 RPM
myStepper2.setSpeed(60)          # 30 RPM


stepstyles = [Adafruit_MotorHAT.SINGLE, Adafruit_MotorHAT.DOUBLE, Adafruit_MotorHAT.INTERLEAVE, Adafruit_MotorHAT.MICROSTEP]

def stepper_worker(stepper, numsteps, direction, style):
    #print("Steppin!")
    stepper.step(numsteps, direction, style)
    #print("Done")

while (True):
    if not st1.isAlive():
        randomdir = random.randint(0, 1)
        print("Stepper 1"),
        if (randomdir == 0):
            dir = Adafruit_MotorHAT.FORWARD
            print("forward"),
        else:
            dir = Adafruit_MotorHAT.BACKWARD
            print("backward"),
        randomsteps = random.randint(10,50)
        print("%d steps" % randomsteps)
        st1 = threading.Thread(target=stepper_worker, args=(myStepper1, randomsteps, dir, stepstyles[random.randint(0,3)],))
        st1.start()

    if not st2.isAlive():
        print("Stepper 2"),
        randomdir = random.randint(0, 1)
        if (randomdir == 0):
            dir = Adafruit_MotorHAT.FORWARD
            print("forward"),
        else:
            dir = Adafruit_MotorHAT.BACKWARD
            print("backward"),

        randomsteps = random.randint(10,50)
        print("%d steps" % randomsteps)

        st2 = threading.Thread(target=stepper_worker, args=(myStepper2, randomsteps, dir, stepstyles[random.randint(0,3)],))
        st2.start()
    
time.sleep(0.1) # Small delay to stop from constantly polling threads (see: https://forums.adafruit.com/viewtopic.php?f=50&t=104354&p=562733#p562733)



Thank you for your help and consideration. Best regards.

Mike

rhubarbdog
Posts: 18
Joined: Mon Dec 11, 2017 9:58 pm

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Tue Jan 02, 2018 11:18 am

How much python have you done.
The test program seems quite clear and i'm not a python expert. Have you not considered buying a beginners python book before starting with this program and hardware build

pcmanbob
Posts: 2551
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Tue Jan 02, 2018 11:43 am

looking at the program you posted it looks to me as if its for driving a stepper motor that has 200 steps per revolution but you say yours is a 32 step motor ? that might explain the slow speed.

also this example code seems to drive the motors in a random direction so you could have the 2 motors running in different directions and at different speeds.

I would suggest the first thing to do is look at the specifications for your motors and decide what number of steps are required per revolution and what style of step is required also as the motor hat seems to have 4 options.

then you can rewrite the code removing the while loop and possibly even the treading as you will be driving both motor together I assume, just specifying the motor to drive its direction step-style and number of steps, which you will need to do for both motors.

your comment about nothing seemed to happen. The Pi returned quickly to the command line. suggest the program did not run if there was no output to the screen as there are several print statements in the program.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

msheald
Posts: 15
Joined: Sun Dec 31, 2017 7:45 pm

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Tue Jan 02, 2018 9:49 pm

Thank you!

I've read a couple of basic python books. Looking at a python program after those books is like trying to interpret a DOS program after reading a basic DOS book that goes over some basic architecture and a dozen codes. I've got a ways to go! Best regards.

Mike

msheald
Posts: 15
Joined: Sun Dec 31, 2017 7:45 pm

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Wed Jan 03, 2018 10:58 pm

Hello! It looks like I made a mistake in wiring. I tested resistance of the leads but made a mistake in hooking up to the MotorHAT.

The Steppers have 32 steps, but with a gear reduction of 1/16. In the program, would I put 32 steps or 513 steps?

What would be the best way to modify the program to achieve a uniform forward speed and direction? The Adafruit's page for the stepper refers the MotorShield product rather than MotorHAT, and I am unclear how to modify the MotorHAT program that drives the two steppers at once. Thank you and best regards.

Mike

pcmanbob
Posts: 2551
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Wed Jan 03, 2018 11:41 pm

Well if the motors have 32 steps per revolution then that's what you need to enter, but if they have a reduction gearbox of 1/16 then it will take 16 revolutions of the motor to turn the output shaft of the gear box 1 turn, which is why you motor seems so slow.

as I don't have the hat or stepper motor this is just a guess using what's in the posted code so dont blame me if it does not work.

Code: Select all

#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor
import time
import atexit
import threading
import random

# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT()

# create empty threads (these will hold the stepper 1 and 2 threads)
st1 = threading.Thread()
st2 = threading.Thread()


# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
    mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)

atexit.register(turnOffMotors)

myStepper1 = mh.getStepper(32, 1)      # 32 steps/rev, motor port #1
myStepper2 = mh.getStepper(32, 2)      # 32 steps/rev, motor port #1
myStepper1.setSpeed(60)          # 30 RPM
myStepper2.setSpeed(60)          # 30 RPM


stepstyles = [Adafruit_MotorHAT.SINGLE, Adafruit_MotorHAT.DOUBLE, Adafruit_MotorHAT.INTERLEAVE, Adafruit_MotorHAT.MICROSTEP]

def stepper_worker(stepper, numsteps, direction, style):
    #print("Steppin!")
    stepper.step(numsteps, direction, style)
    #print("Done")

while (True):
    if not st1.isAlive():
        
        print("Stepper 1"),
        
            dir = Adafruit_MotorHAT.FORWARD
            print("forward"),
        
        randomsteps = 100
        print("%d steps" % randomsteps)
        st1 = threading.Thread(target=stepper_worker, args=(myStepper1, randomsteps, dir, stepstyles[0],))
        st1.start()

    if not st2.isAlive():
        print("Stepper 2"),
        
            dir = Adafruit_MotorHAT.FORWARD
            print("forward"),
        
        randomsteps = 100

        print("%d steps" % randomsteps)

        st2 = threading.Thread(target=stepper_worker, args=(myStepper2, randomsteps, dir, stepstyles[0],))
        st2.start()
    
time.sleep(0.1) # Small delay to stop from constantly polling threads (see: https://forums.adafruit.com/viewtopic.php?f=50&t=104354&p=562733#p562733)

Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

msheald
Posts: 15
Joined: Sun Dec 31, 2017 7:45 pm

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Thu Jan 04, 2018 11:35 am

Thank you! I really appreciate your help with a Python beginner like me.

I'll try the code today and let you know. Best regards.

Mike

msheald
Posts: 15
Joined: Sun Dec 31, 2017 7:45 pm

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Thu Jan 04, 2018 10:38 pm

Hello! The program works. Thank you.

I had to adjust a few indents, and I used 513 steps vs. 32, but the stepper went forward continuously.

It turns at 3 RPM. It seems to turn the same speed if I set RPM to 30 or 60.

I am using a 12 volt 5 amp power supply.

Is there a way to increase the speed or might this be the inherent speed of the Stepper? Thank you and best regards.

Mike

pcmanbob
Posts: 2551
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Fri Jan 05, 2018 10:16 am

Glad it works .

Indent problem caused by being edited in different editors common problem.

A motor with a reduction gear box will be inherently slower because of the gear box, you could try changing the set speed value, but I don't know what value will give higher speed so just experiment with different values, you could also try changing the step style in the lines.

Code: Select all

st1 = threading.Thread(target=stepper_worker, args=(myStepper1, randomsteps, dir, stepstyles[0],))
st2 = threading.Thread(target=stepper_worker, args=(myStepper2, randomsteps, dir, stepstyles[0],))
valid entries are 0,1,2,3

Just try changing one thing at a time until you find the best combination to give maximum speed .
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help


msheald
Posts: 15
Joined: Sun Dec 31, 2017 7:45 pm

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Wed Jan 10, 2018 2:29 pm

Hello! It does not look like these Steppers can turn fast enough with the MotorHAT. I've purchased a 60 rpm DC motor that looks like it will do the trick. Thank you again.

Mike

pcmanbob
Posts: 2551
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Newbie, Adafruit MotorHAT, and Stepper Control Program Modification

Wed Jan 10, 2018 3:38 pm

What you probably needed was a stepper motor without a reduction gearbox as I explained the gearbox was slowing the output shaft by a factor of 16, if the steep motor in question was not very fast in the first place, this would have only made things worse.

hope you get it working with your new motor, if you need any more help just ask and good luck with your project.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

Return to “Beginners”

Who is online

Users browsing this forum: jsander, moham_96 and 60 guests