jeanbardon
Posts: 6
Joined: Tue Feb 09, 2021 12:43 pm

Command multiple stepper motors simultaneously, in Python

Tue Jul 27, 2021 11:05 am

Hello community,

I am building a small robot with 3 Nema stepper motors, each one controlled by a DRV8825 driver, and a Raspberry Pi 4.
For now, I have been succeeding in writing a function TurnMotor(motor_id, nb_of_turns, speed, direction) that generates a signal to turn one specific motor (specified by motor_id) for a certain number of cycles at a given speed, in a given direction (clockwise or anticlockwise).
The function is very basic, it simply generates a HIGH-LOW signal like this:

Code: Select all

for x in range(step_count):
    GPIO.output(STEP, GPIO.HIGH)
    sleep(delay)
    GPIO.output(STEP, GPIO.LOW)
    sleep(delay)
"delay" is calculated from the required speed. "step_count" is calculated from the required number of motor cycles.
But this is for one motor at a time.

I would like to turn all 3 motors simultaneously at different speeds, be able to stop (or reverse) one while the others continue to run for a specified time, then continue to run it and stop another one, etc. I kind of feel like I would need to have multiple Python threads reading at the same time, but also a bit synchronized, but I am not sure it can exist and if it is the right approach. Could you please help me by pointing towards better approaches to run multiple motors simultaneously?

Sorry for the potentially silly question, I am a beginner in Python, Raspberry and mechatronics !

Thanks in advance

danjperron
Posts: 3840
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Command multiple stepper motors simultaneously, in Python

Tue Jul 27, 2021 12:56 pm

You will have to learn about threading.


I quick google search and I found this
https://wikifactory.com/@jonbull/video- ... a247b63706

lidgaca
Posts: 28
Joined: Thu Jul 30, 2020 8:25 pm
Location: Yorkshire, England

Re: Command multiple stepper motors simultaneously, in Python

Tue Aug 03, 2021 2:33 pm

Another way that may help would be to use a more sophisticated stepper controller such as the L6470 chip from STI.

With that you no longer issue 'STEP and DIR' commands to the driver chip, you tell it to move X steps at Y speed in Z direction (via the SPI bus), and the chip generates the pulse train to do so. Programming and setup of the L6470 is more complex, but you save masses of processing when it comes to movement.

The motorised cam project uses two steppers simultaneously driven from two L6470's (here ... viewtopic.php?f=37&t=297469. It has worked examples of the code to drive it (qt5 creator and c++), and you can buy the L6470 on a card ready to go from ebay for about 10 quid. A bit more than a DRV 8825, but not too bad.

There are probably many other chips that work in this way as well ...


-- Chris

Return to “Automation, sensing and robotics”