Running Stepper Motors Simultaneously

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
LuukS
 
Posts: 9
Joined: Fri Nov 13, 2015 11:53 am

Running Stepper Motors Simultaneously

Post by LuukS »

Hello,

I am running two 10V 0.64A Stepper Motors from an Adafruit Motor HAT. Everything works fine but I cannot seem to figure out how to run two motors at the same time;

I am using the following script, but the motors start after each other. Has anyone figured out how to do this? Thank you!!

Code: Select all

#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor

import time
import atexit
import threading
from threading import Thread

mh = Adafruit_MotorHAT(addr = 0x60)

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)

Stepper1 = mh.getStepper(200, 1) 
Stepper2 = mh.getStepper(200, 2)

Stepper1.setSpeed(6000)
Stepper2.setSpeed(6000)

def Stepperone():
	Stepper1.step(200, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.DOUBLE)


def Steppertwo():
	Stepper2.step(200, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.DOUBLE)

if __name__ == '__main__':
	Thread(target = Stepperone()).start()
	Thread(target = Steppertwo()).start()
	
	
Last edited by LuukS on Sat Nov 14, 2015 8:37 am, edited 1 time in total.

User avatar
AarBee
 
Posts: 13
Joined: Thu May 21, 2015 11:08 pm

Re: Running Stepper Motors Simultaneously

Post by AarBee »

6000rpm is way to fast for the MotorHAT. See discussion viewtopic.php?f=50&t=75319

I suspect that the the extremely high speed you are requesting is causing one thread to lock the other out.

I have also seen that while driving the motors fast, trying to run two motors simultaneously via multi-threading causes the motors to stutter/jitter - adding short wait (time.sleep(0.01)) between start of one thread and another as well as in the isAlive test loop makes that problem go away.

With these modifications, using the template in DualStepper.py works for me. Currently trying to achieve smooth, constant speed motion in a circle.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Running Stepper Motors Simultaneously

Post by adafruit_support_bill »

6000rpm is way to fast for the MotorHAT
Definitely. Between the i2c bus speed and python overhead, it is not possible to achieve those speeds with a 200 step motor and the Hat. Even without the overhead limitations, most of these motors top out at just a few hundred RPM using simple drivers like the HAT. You would need a high-performance constant-current driver to extract more speed from these motors. https://learn.adafruit.com/all-about-st ... he-stepper

User avatar
LuukS
 
Posts: 9
Joined: Fri Nov 13, 2015 11:53 am

Re: Running Stepper Motors Simultaneously

Post by LuukS »

Thank you very much for your replies,

I've modified the DualStepperTest file and hurray, it works! I've modified it more to measure the speed:

When two steppers run at the same time on 960 rpm / baudrate = 400000, DOUBLE step :
0.0003125 sec per step
0.0003125 sec per step
200 dual steps in: 1447505933.69 seconds
200 dual steps in: 1447505933.7 seconds

When they run separately on 960 rpm / baudrate = 400000 DOUBLE step:
0.0003125 sec per step
200 steps in: 0.614535093307 seconds

Is this because the i2c has to send information to two motors instead of one, and therefore divides its bus speed?

As for the speed, what i have found is that it seems like the baudrate controls the maximum speed. The motors were not turning 6000 rpm. In fact, when I use 960 rpm, with a baudrate of 200000 the steppers make 200 steps in 0.61 seconds, which makes approx 98 rpm. From searching around forums and stackoverflow it seemed to me that there are a few factors limiting the rpm, one of which is the python script itself. So it doesn't seem to matter whether I put 100000 rpm or 1500 rpm. But the speed cap will go up once I change the baudrate. (I don't know what the maximum safe baudrate is, so I haven't gone higher then 400000).

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Running Stepper Motors Simultaneously

Post by adafruit_support_bill »

Is this because the i2c has to send information to two motors instead of one, and therefore divides its bus speed?
It is partially bus-speed and partial python execution overhead. But yes: Twice the bus traffic and twice the execution overhead means the max motor speed is lower.
(I don't know what the maximum safe baudrate is, so I haven't gone higher then 400000).
You can keep increasing it until things stop working reliably. People have reported that it is working at speeds up to 500000.

User avatar
LuukS
 
Posts: 9
Joined: Fri Nov 13, 2015 11:53 am

Re: Running Stepper Motors Simultaneously

Post by LuukS »

Great, thank you that is good to know!

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”