我有两个开关需要独立关闭每个电机,然后关闭电机控制器。所有三个while循环都需要同时运行。 不知道我应该如何让这个工作。我尝试过线程,但没有那个工作。希望这是我想念的东西。对python来说很新。
import time
import RPi.GPIO as GPIO
from multiprocessing import Process
# set mode
GPIO.setmode(GPIO.BOARD)
# setup
# enableA
GPIO.setup(11, GPIO.OUT)
# enableb
GPIO.setup(15, GPIO.OUT)
# enable INT1
GPIO.setup(7, GPIO.OUT)
# enable INT4
GPIO.setup(16, GPIO.OUT)
# enable Switch Down sensor Left
GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# enable Switch Down sensor Right
GPIO.setup(33, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# turn on ena, enb
GPIO.output(11, GPIO.HIGH)
GPIO.output(15, GPIO.HIGH)
# Turn two motor Down
GPIO.output(7, GPIO.HIGH)
GPIO.output(16, GPIO.HIGH)
# turn off left motor
def MLO():
while True:
input_state = GPIO.input(32)
if not input_state:
print('Left off')
GPIO.output(7, GPIO.LOW)
# turn off right motor
def MRO():
while True:
input_Rswitch = GPIO.input(33)
if not input_Rswitch:
print('Right off')
GPIO.output(16, GPIO.LOW)
# turn off motor control
def RCO():
while True:
input_state2 = GPIO.input(33) or GPIO.input(32)
if not input_state2:
print ('system off')
GPIO.output(11, GPIO.LOW)
GPIO.output(15, GPIO.LOW)
if __name__ == '__main__':
p1 = Process(target=MLO())
p2 = Process(target=MRO())
p3 = Process(target=RCO())
p1.start()
p2.start()
p3.start()
GPIO.cleanup()
exit()