0.引言
某项目需要一个小电机转动装置进行测量,该装置(含有树莓派控制系统)本身可以测量角度(姿态),不需要电机的精确角度,但需要对速度要有控制,需要调速达到测量最佳结果。本文通过实测研究步进电机的转速规律,以通过调节参数达到调速目的。
1.步进电机说明
步进电机为28BYJ4,驱动板为ULN2003,4相。
引脚说明:
“IN1”:接树莓派的GPIO17
“IN2”:接树莓派的GPIO22
“IN3”:接树莓派的GPIO23
“IN4”:接树莓派的GPIO24
“-”:接树莓派的GND
“+”:接树莓派的5V
注:数字接口可更换为其他位置。
设计参数:
2.树莓派引脚说明
3.编写代码
stepper.py
#!/usr/bin/python
# Import required libraries
import sys
import time
import RPi.GPIO as GPIO
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO signals to use
# Physical pins 11,15,16,18
# GPIO17,GPIO22,GPIO23,GPIO24
StepPins = [17,22,23,24]
# Set all pins as output
for pin in StepPins:
print "Setup pins"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
# Define advanced sequence
# as shown in manufacturers datasheet
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]]
StepCount = len(Seq)
StepDir = 1 # Set to 1 or 2 for clockwise
# Set to -1 or -2 for anti-clockwise
# Read wait time from command line
if len(sys.argv)>1:
WaitTime = int(sys.argv[1])/float(1000)
else:
WaitTime = 10/float(1000)
# Initialise variables
StepCounter = 0
# Start main loop
while True:
#print StepCounter,
#print Seq[StepCounter]
for pin in range(0, 4):
xpin = StepPins[pin]
if Seq[StepCounter][pin]!=0:
#print " Enable GPIO %i" %(xpin)
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
StepCounter += StepDir
# If we reach the end of the sequence
# start again
if (StepCounter>=StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount+StepDir
# Wait before moving on
time.sleep(WaitTime)
4.转速测试结果分析
(1)测试界面
(2)测试结果
根据操作和结果分析,等待时间需大于等于1ms,最大转速可达1圈4.83秒。
等待时间和转速成正比。
参考资料:
[1] BASEMU. 树莓派使用Python控制步进电机; 2018-11-14 [accessed 2023-03-28].
[2] 孤舟蓑笠不倒翁. 利用树莓派控制步进电机——Python语言; 2015-01-26 [accessed 2023-03-28].
[3] wu_lian_nan. 树莓派入门(三)之步进电机控制; 2019-03-26 [accessed 2023-03-28].
[4] 无1234. 莓派c语言电机,利用树莓派控制步进电机——C语言; 2021-05-21 09 [accessed 2023-03-28].
[5] simple_star_80. Raspberry Pi 引脚说明; 2021-11-01 [accessed 2023-03-28].