提问人:Rocket Rod 提问时间:4/19/2023 最后编辑:Sanushi SalgadoRocket Rod 更新时间:4/21/2023 访问量:61
Python程序(Raspberry Pi)控制推拉门的打开/关闭
Python program (Raspberry Pi) to control the opening/closing of a sliding door
问:
3 个输出:CW、CCW 和 SPEED 5 个输入:手动打开 (ENT),然后 4 个簧片开关确定位置,打开和关闭时行程结束。
我可以看到 OUTPUT/hardware 和 GPIO.setup 通过 WEBIOPi 正常工作。手动一切正常。
目前,我一直在尝试使用 ENT 输入来触发一个带有 sleep() 延迟的序列,以至少实现一些自动化,然后稍后整理行程开关,但仍然不行。代码更糟,在此之前,这不是等待 ENT/Open 开关,而是运行一些序列。
通过键盘控制输出没有问题,当时看起来很容易,但输入呢?对此有什么想法吗?
#!/usr/bin/python
#DOOR Controller
from __future__ import print_function
import RPi.GPIO as GPIO
from time import sleep
import subprocess
import os
import sys
import datetime
import pdb; #pdb.set_trace()
GPIO.setwarnings(False)
# Relay channel definitions via GPIO numbers
CCW = 2
CW = 3
SPEED = 24
S1_TR = 22
S2_BR = 18
S3_TL = 25
S4_BL = 17
ENT = 23
# Initialise GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(CW,GPIO.OUT)
GPIO.setup(CCW,GPIO.OUT)
GPIO.setup(SPEED,GPIO.OUT)
GPIO.setup(S1_TR,GPIO.IN)
GPIO.setup(S2_BR,GPIO.IN)
GPIO.setup(S3_TL,GPIO.IN)
GPIO.setup(S4_BL,GPIO.IN)
GPIO.setup(ENT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Relays are active-low
GPIO.output(CCW,True)
GPIO.output(CW,True)
GPIO.output(SPEED,False)
#def signal_handler(sig, frame):
# GPIO.cleanup()
# sys.exit(0)
def ENT_CB(ENT):
if GPIO.input(ENT):
GPIO.output(CCW, False)
sleep(2)
GPIO.output(SPEED, True) #fast
sleep(5)
GPIO.output(SPEED, False) #slow
sleep(2)
GPIO.output(CCW, True)
sleep(1)
GPIO.output(CW, False)
sleep(2)
GPIO.output(SPEED, True)
sleep(5)
GPIO.output(SPEED, False)
sleep(2)
GPIO.output(CW, True)
#pdb.set_trace()
if __name__ == '__main__':
GPIO.add_event_detect(ENT, GPIO.FALLING,
callback=ENT_CB, bouncetime=50)
GPIO.add_event_detect(S1_TR, GPIO.RISING)
GPIO.add_event_detect(S3_TL, GPIO.RISING)
GPIO.add_event_detect(S4_BL, GPIO.RISING)
GPIO.add_event_detect(S2_BR, GPIO.RISING)
#signal.signal(signal.SIGINT, signal_handler)
#signal.pause()
#def loop():
#while True:
# if GPIO.event_detected(ENT):
# GPIO.output(CCW, False)
# sleep(2)
# #elif GPIO.event_detected(S3_TL):
# GPIO.output(SPEED, True)
# sleep(5)
# GPIO.output(SPEED, False)
# sleep(2)
# GPIO.output(CCW, True)
# sleep(1)
# GPIO.output(CW, False)
# sleep(2)
# GPIO.output(SPEED, True)
# sleep(5)
# GPIO.output(SPEED, False)
# sleep(2)
# GPIO.output(CW, True)
#
# elif GPIO.event_detected(S4_BL):
# GPIO.output(SPEED, False)
# elif GPIO.event_detected(S2_BR):
# GPIO.output(CCW, True)
# sleep(5)
# GPIO.output(CW, True)
# if GPIO.event_detected(S4_BL):
# GPIO.output(SPEED, True)
# elif GPIO.event_detected(S3_TL):
# GPIO.output(SPEED, False)
# elif GPIO.event_detected(S1_TR):
# GPIO.output(CW, False)
# else:
# print("stop")
# if __name__ == '__main__':
# setup()
# try:
# loop()
# except KeyboardInterrupt:
# cprint("Stopped")
答:
0赞
Rocket Rod
4/19/2023
#1
进一步的“谷歌学校”,我找到了以下解决方案,它有效。 对于一些聪明人来说会很好,但磁簧开关似乎不可靠,好吧,也许测试磁铁不正确。:)
#!/usr/bin/python
#DOOR Controller
from __future__ import print_function
import RPi.GPIO as GPIO
from time import sleep
import subprocess
import os
import sys
import datetime
import pdb; #pdb.set_trace()
GPIO.setwarnings(False)
# Relay channel definitions via GPIO numbers
CCW = 2
CW = 3
SPEED = 24
S1_TR = 22
S2_BR = 18
S3_TL = 25
S4_BL = 17
ENT = 23
# Initialise GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(CW,GPIO.OUT)
GPIO.setup(CCW,GPIO.OUT)
GPIO.setup(SPEED,GPIO.OUT)
GPIO.setup(S1_TR,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(S2_BR,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(S3_TL,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(S4_BL,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(ENT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Relays are active-low
GPIO.output(CCW,True)
GPIO.output(CW,True)
GPIO.output(SPEED,False)
def callback_func(ENT):
while not GPIO.input(ENT):
GPIO.output(CW, GPIO.LOW)
sleep(2)
GPIO.output(SPEED, GPIO.HIGH)
sleep(3)
GPIO.output (SPEED, GPIO.LOW)
sleep(2)
GPIO.output (CW, GPIO.HIGH)
sleep(1)
GPIO.output (CCW, GPIO.LOW)
sleep(2)
GPIO.output(SPEED, GPIO.HIGH)
sleep(3)
GPIO.output (SPEED, GPIO.LOW)
sleep(2)
GPIO.output (CCW, GPIO.HIGH)
GPIO.add_event_detect(ENT, GPIO.FALLING, callback=callback_func, bouncetime=10)
#def callback_func(S4_BL):
# while not GPIO.input(S4_BL):
# GPIO.output(CW, GPIO.LOW)
# sleep(2)
# GPIO.output (CW, GPIO.HIGH)
#GPIO.add_event_detect(S4_BL, GPIO.FALLING, callback=callback_func, bouncetime=50)
def main():
while True:
#print ("DOOR waiting to OPEN/CLOSE")
sleep(5)
if __name__ == "__main__":
main()
上一个:过早输入会导致故障吗?
评论