python - 树莓派 28-BYJ-48 带 uln2003 的步进电机控制
问题描述
我目前正在尝试使用 uln2003 驱动器控制 28-BYJ-48 步进电机。当我运行我的代码时,电机嗡嗡作响并且不动。在附加的代码中,我使用 pygame 从控制器接收输入,并最终转动电机。我可以使用演示代码转动电机,但使用新代码时电机只会嗡嗡作响,并在改变方向时改变螺距。(接收输入不是问题,我测试过多次)
下面附上代码:
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)
推荐阅读
- mongodb - “gcc”:使用 mongo-go-driver 时在 %PATH% 中找不到可执行文件
- arrays - 在 TabGesture Swift 用户界面上
- r - 清除所有绘图,现在 ggplot2 不会绘图
- python - python:如何从两个不同的列表中取出列表中的第一个列表并将它们配对以使它们成为单独的列表
- list - 在 Python 列表中进行就地修改
- javascript - 如何在 TypeScript 中使用 Wix Style React 组件?
- c# - 将 SafeFileHandle 与 FileStream 构造函数一起使用有什么好处吗
- php - 从 crontab (Nagios) 执行 PHP 脚本的 SSH 问题
- c++ - 重新散列时锁定散列映射
- java - 从 Java 中的 URL 获取值