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:
"delay" is calculated from the required speed. "step_count" is calculated from the required number of motor cycles.
Code: Select all
for x in range(step_count): GPIO.output(STEP, GPIO.HIGH) sleep(delay) GPIO.output(STEP, GPIO.LOW) sleep(delay)
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