Python Raspberry Pi 28-BYJ-48步进电机控制与uln2003
我目前正试图用uln2003驱动器控制28-BYJ-48步进电机。当我运行代码时,电机嗡嗡作响,不动。在所附的代码中,我使用pygame接收来自控制器的输入,并最终转动电机。我能够用一个演示代码来转动马达,但是有了新的代码,马达只会嗡嗡作响,当方向改变时会改变音调。(接收输入不是问题,我已经测试过多次) 以下为代码附件:Python Raspberry Pi 28-BYJ-48步进电机控制与uln2003,python,raspberry-pi3,Python,Raspberry Pi3,我目前正试图用uln2003驱动器控制28-BYJ-48步进电机。当我运行代码时,电机嗡嗡作响,不动。在所附的代码中,我使用pygame接收来自控制器的输入,并最终转动电机。我能够用一个演示代码来转动马达,但是有了新的代码,马达只会嗡嗡作响,当方向改变时会改变音调。(接收输入不是问题,我已经测试过多次) 以下为代码附件: control_pins = [7, 8, 11, 9] for pin in control_pins: GPIO.setup(pin, GPIO.OUT)
control_pins = [7, 8, 11, 9]
for pin in control_pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_cw = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
halfstep_ccw = [
[1,0,0,1],
[0,0,0,1],
[0,0,1,1],
[0,0,1,0],
[0,1,1,0],
[0,1,0,0],
[1,1,0,0],
[1,0,0,0]
]
control = controller()
#Note, that to use the driver and the controller at all times,
#They must be in a loop
quit = False
while quit == False:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
quit = True
#This is getting axis 1 to move forward and backward
move_FB = control.get_value(1)
#This is getting axis 2 to move left and right
move_LR = control.get_value(2)
#This is getting the value of the buttons, with axis i
circle = control.get_button_value(13)
R1 = control.get_button_value(11)
L1 = control.get_button_value(10)
#For buttons, 1 is pressed, 0 is not pressed
#This will quit the program
elif circle == 1:
quit = True
#Move the stepper motor clockwise
elif R1 == 1:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_cw[halfstep][pin])
#Move the stepper motor counter-clockwise
elif L1 == 1:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_ccw[halfstep][pin])
else:
stop()
GPIO.cleanup()
pygame.QUIT
任何帮助都将不胜感激。回答了我自己的问题。
刚刚添加了time.sleep函数,如下所示
elif R1 == 1:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_cw[halfstep][pin])
time.sleep(0.001)
#Move the stepper motor counter-clockwise
elif L1 == 1:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_ccw[halfstep][pin])
time.sleep(0.001)
我使用以下代码:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
coil_A_1_pin = 17 # IN1
coil_A_2_pin = 18 # IN2
coil_B_1_pin = 21 # IN3
coil_B_2_pin = 22 # IN4
# adjust if different
StepCount=8
Seq = [[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)
def setStep(w1, w2, w3, w4):
GPIO.output(coil_A_1_pin, w1)
GPIO.output(coil_A_2_pin, w2)
GPIO.output(coil_B_1_pin, w3)
GPIO.output(coil_B_2_pin, w4)
def forward(delay, steps):
for i in range(steps):
for j in range(StepCount):
setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
time.sleep(delay)
def backwards(delay, steps):
for i in range(steps):
for j in reversed(range(StepCount)):
setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
time.sleep(delay)
if name == '__main__':
while True:
delay = input("Time Delay (ms)?")
steps = input("How many steps forward? ")
forward(int(delay) / 1000.0, int(steps))
steps = input("How many steps backwards? ")
backwards(int(delay) / 1000.0, int(steps))