最近想用树莓派控制A4988,网上没找到相应的教程。今天发出来供大家学习。
由于刚开始学python,难免有误,欢迎大家指正
import RPi.GPIO as GPIO
import time
dirPin=17
stepperPin=18
powerPin=27 #控制A4988的供电
outpins=[17,18,27]
inpins=[15]
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(outpins, GPIO.OUT)
GPIO.setup(inpins, GPIO.IN)
def opendoor(steps):
GPIO.output(powerPin,GPIO.HIGH)
GPIO.output(dirPin,GPIO.HIGH)
time.sleep(0.05);
for i in range(0,steps):
GPIO.output(stepperPin,GPIO.HIGH)
time.sleep(0.002)
GPIO.output(stepperPin,GPIO.LOW)
time.sleep(0.002)
time.sleep(1)
destory()
def lockdoor(steps):
GPIO.output(powerPin,GPIO.HIGH)
GPIO.output(dirPin,GPIO.LOW)
time.sleep(0.05);
for i in range(0,steps):
GPIO.output(stepperPin,GPIO.HIGH)
time.sleep(0.002)
GPIO.output(stepperPin,GPIO.LOW)
time.sleep(0.002)
time.sleep(1)
destory()
def destory():
GPIO.output(stepperPin,GPIO.LOW)
GPIO.output(dirPin,GPIO.LOW)
GPIO.output(powerPin,GPIO.LOW)
print(GPIO.input(powerPin))
if __name__ == '__main__':
setup()
try:
opendoor(800)
lockdoor(800)
except KeyboardInterrupt:
destory()