此编程基于L293D电机驱动程序。
例如,PI 16,18,22中的引脚是与一侧电动机相关的引脚。
它的PINA和PINB用于打开任一方向的Pine启用Pin22
from time import sleep
import thread
import RPi.GPIO as GPIO
class Motion:
#this class is used to define the different motion of the wheel.
def __init__(self,pinA,pinB,pinE):
GPIO.setmode(GPIO.BOARD)
Motor1A = pinA;
Motor1B = pinB;
Motor1E = pinE;
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
def forward(self):
print "Moving Forward"
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)
GPIO.cleanup()
sleep(2)
def backword(self):
print "Moving Backword"
GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor1E,GPIO.HIGH)
GPIO.cleanup()
sleep(2)
def stop(self):
print "No Movements, Stoped"
GPIO.output(Motor1E,GPIO.LOW)
GPIO.cleanup()
代码的一个问题是,在每种方法之后,您都会调用GPIO.cleanup()
。这还清除了使用GPIO.setmode(GPIO.BOARD)
设置的GPIO引脚编号系统,因此如果您多次调用这些方法,就会引起问题。最好在脚本末尾只致电GPIO.cleanup()
一次。在方法中,例如,您可以在睡眠后低销后低销,以在运行所需时间后停止电动机。
如果您发布了运行代码时看到的实际错误,则有人可能会帮助您进一步调试。