Estoy tratando de controlar dos (posiblemente más) motores paso a paso con la Raspberry Pi 3B. El principal problema que encuentro con la ejecución de dos motores paso a paso es que cuando ejecuto ambos motores, ambos parecen girar a la rotación y velocidad exactas, pero cuando solo hago funcionar uno de ellos, el motor parece girar más lentamente y Suena mas irregular. Los servomotores utilizados aquí son el motor paso a paso Nema 17HS4401 y yo uso l298n para el controlador del motor. Aquí está el código que hice para hacer funcionar los motores:
import RPi.GPIO as GPIO
import time
import threading
class Stepper:
def __init__(self, steps_per_rotation, A1, A2, B1, B2, E):
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
self.steps_per_rotation = steps_per_rotation
self.coil_A_1_pin = A1
self.coil_A_2_pin = A2
self.coil_B_1_pin = B1
self.coil_B_2_pin = B2
self.enable_pin = E
GPIO.setup(self.coil_A_1_pin, GPIO.OUT)
GPIO.setup(self.coil_A_2_pin, GPIO.OUT)
GPIO.setup(self.coil_B_1_pin, GPIO.OUT)
GPIO.setup(self.coil_B_2_pin, GPIO.OUT)
GPIO.setup(self.enable_pin, GPIO.OUT)
def set_step(self, w1, w2, w3, w4):
GPIO.output(self.coil_A_1_pin, w1)
GPIO.output(self.coil_A_2_pin, w2)
GPIO.output(self.coil_B_1_pin, w3)
GPIO.output(self.coil_B_2_pin, w4)
def enable(self):
GPIO.output(self.enable_pin, 1)
def disable(self):
GPIO.output(self.enable_pin, 0)
def forward_rotation(self, delay, rotation):
steps = rotation * self.steps_per_rotation
delay = delay/1000
self.enable()
for i in range(0, steps):
self.set_step(1, 0, 1, 0)
time.sleep(delay)
self.set_step(0, 1, 1, 0)
time.sleep(delay)
self.set_step(0, 1, 0, 1)
time.sleep(delay)
self.set_step(1, 0, 0, 1)
time.sleep(delay)
self.disable()
def backward_rotation(self, delay, rotation):
steps = rotation * self.steps_per_rotation
delay = delay/1000
self.enable()
for i in range(0, steps):
self.set_step(1, 0, 0, 1)
time.sleep(delay)
self.set_step(0, 1, 0, 1)
time.sleep(delay)
self.set_step(0, 1, 1, 0)
time.sleep(delay)
self.set_step(1, 0, 1, 0)
time.sleep(delay)
self.disable()
def forward_time(self, delay, second):
delay = delay/1000
self.enable()
timeout = time.time() + second
while time.time() < timeout:
self.set_step(1, 0, 1, 0)
time.sleep(delay)
self.set_step(0, 1, 1, 0)
time.sleep(delay)
self.set_step(0, 1, 0, 1)
time.sleep(delay)
self.set_step(1, 0, 0, 1)
time.sleep(delay)
self.disable()
def backward_time(self, delay, second):
delay = delay/1000
self.enable()
timeout = time.time() + second
while time.time() < timeout:
self.set_step(1, 0, 0, 1)
time.sleep(delay)
self.set_step(0, 1, 0, 1)
time.sleep(delay)
self.set_step(0, 1, 1, 0)
time.sleep(delay)
self.set_step(1, 0, 1, 0)
time.sleep(delay)
self.disable()
def forwards(self, delay, steps = 1):
delay = delay/1000
self.enable()
for i in range(0, steps):
self.set_step(1, 0, 1, 0)
time.sleep(delay)
self.set_step(0, 1, 1, 0)
time.sleep(delay)
self.set_step(0, 1, 0, 1)
time.sleep(delay)
self.set_step(1, 0, 0, 1)
time.sleep(delay)
self.disable()
def backwards(self, delay, steps = 1):
delay = delay/1000
self.enable()
for i in range(0, steps):
self.set_step(1, 0, 0, 1)
time.sleep(delay)
self.set_step(0, 1, 0, 1)
time.sleep(delay)
self.set_step(0, 1, 1, 0)
time.sleep(delay)
self.set_step(1, 0, 1, 0)
time.sleep(delay)
self.disable()
luego ejecuto el código como una clase en el siguiente código:
from Stepper_Motor import Stepper
import time
from threading import Thread
from queue import Queue
Acoil_A_1_pin = 2
Acoil_A_2_pin = 3
Acoil_B_1_pin = 4
Acoil_B_2_pin = 14
Aenable_pin = 15
Bcoil_A_1_pin = 18
Bcoil_A_2_pin = 17
Bcoil_B_1_pin = 27
Bcoil_B_2_pin = 22
Benable_pin = 23
steps_per_rotation = 50
stepper_A = Stepper(steps_per_rotation, Acoil_A_1_pin, Acoil_A_2_pin, Acoil_B_1_pin, Acoil_B_2_pin, Aenable_pin)
stepper_B = stepper = Stepper(steps_per_rotation, Bcoil_A_1_pin, Bcoil_A_2_pin, Bcoil_B_1_pin, Bcoil_B_2_pin, Benable_pin)
#q = Queue()
motor_on_A = False
motor_on_B = False
def spin_A():
while True:
if motor_on_A:
stepper_A.forwards(2)
def spin_B():
while True:
if motor_on_B:
stepper_B.forwards(2)
def control():
global motor_on_A
global motor_on_B
while True:
user_input = input()
if user_input == "start_A":
#q.put("start")
motor_on_A = True
elif user_input == "start_B":
#q.put("start")
motor_on_B = True
elif user_input == "start":
#q.put("start")
motor_on_A = True
motor_on_B = True
elif user_input == "stop_A":
#q.put("stop")
motor_on_A = False
elif user_input == "stop_B":
#q.put("stop")
motor_on_B = False
elif user_input == "stop":
#q.put("stop")
motor_on_B = False
motor_on_A = False
sA = Thread(target = spin_A)
c = Thread(target = control)
sB = Thread(target = spin_B)
sA.start()
sB.start()
c.start()
Soy nuevo en programación y electrónica, por lo que agradeceré cualquier ayuda,