简体   繁体   English

读取多个树莓派输入引脚的脚本

[英]Script to read multiple raspberry pi input pins

I am using Raspberry pi 3 Model B+ and Python 3 for script.我正在使用 Raspberry pi 3 Model B+ 和 Python 3 作为脚本。

I have 16 IR Sensors connected at board pins - Pin # (7, 11, 13, 15, 16, 18, 22, 29, 31, 32, 33, 35, 36, 37, 38, 40)我有 16 个红外传感器连接在板针上 - 针号(7、11、13、15、16、18、22、29、31、32、33、35、36、37、38、40)

Whenever any IR sensors receives an input it should add up 1 in the existing counter.每当任何 IR 传感器接收到输入时,它都应在现有计数器中加 1。 Also even multiple sensors triggers at same time - It should add the correct result.甚至多个传感器同时触发 - 它应该添加正确的结果。

I have written python3 script to read for one pin.我已经编写了 python3 脚本来读取一个引脚。 Whenever pin detects the object it adds up 1 in the counter.每当引脚检测到 object 时,它就会在计数器中加 1。

Also I have multiple raspberry pi boards.我也有多个树莓派板。 I am taking their Serial ID as the unique ID.我将他们的序列号作为唯一 ID。

Can you help me to understand/write for multiple sensors at same time.你能帮我同时理解/编写多个传感器吗?

Code Snippet:代码片段:

import RPi.GPIO as GPIO
import time

sensor_list = [7, 11, 13, 15, 16, 18, 22, 29, 31, 32, 33, 35, 36, 37, 38, 40]

#print("IR Sensor Ready.....")
#print("..STATRED..")
sensor = 16
count_object = 0


def get_serial():
    # Extract serial from cpuinfo file
    cpuserial = "0000000000000000"
    try:
        f = open('/proc/cpuinfo','r')
        for line in f:
            if line[0:6]=='Serial':
                cpuserial = line[10:26]
        f.close()
    except: cpuserial = "ERROR000000000"
    return cpuserial

raspberry_serial = get_serial()

try:
    while sensor:
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(sensor,GPIO.IN)
        if GPIO.input(sensor):
            count_object+=1
            print(f"Total Objects: {count_object} counter on {raspberry_serial} for the sensor id {sensor}")
            while GPIO.input(sensor):
                time.sleep(0.2)
        else:
            pass
except KeyboardInterrupt:
    GPIO.cleanup()

I have suggested a few changes to your core loop as I will describe.正如我将描述的那样,我建议对您的核心循环进行一些更改。 I haven't tested it.我没有测试过。

  • pin setup done once rather than on each loop.引脚设置完成一次,而不是在每个循环上完成。
  • catch and report import errors捕获并报告导入错误
  • on each loop, read each input and increment count for any with a high signal, then sleep在每个循环上,读取每个输入并增加任何高信号的计数,然后休眠

I don't exactly understand what you are trying to measure or your particular timing requirements so I made it read all inputs every 0.2 seconds.我不完全了解您要测量的内容或您的特定时序要求,所以我让它每 0.2 秒读取一次所有输入。 I am continuing in your style of polling for pin changes.我将继续按照您的方式轮询引脚更改。 However depending on how your sensor behaves this might either count a single event multiple times or miss events entirely.但是,根据您的传感器的行为方式,这可能会多次计算单个事件或完全错过事件。 You might want to consider triggering off of transition events if that is appropriate.如果合适,您可能需要考虑触发转换事件。 Also be sure to check whether your sensor requires a pull down.还要确保检查您的传感器是否需要下拉。

I simply printed the counts.我只是打印了计数。 You can format it to include your board id in whatever way you require.您可以将其格式化为以任何您需要的方式包含您的董事会 ID。

import time
try:
    # According to docs the import may raise an exception
    import RPi.GPIO as GPIO
except RuntimeError:
    print("Error importing RPi.GPIO!  This is probably because you need superuser privileges.  You can achieve this by using 'sudo' to run your script")
    raise

sensor_list = [7, 11, 13, 15, 16, 18, 22, 29, 31, 32, 33, 35, 36, 37, 38, 40]

try:
    # Initial setup for all pins only needs to be done once.
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(sensor_list, GPIO.IN)
    # A separate counter initially zero for each input pin.
    counts = { ch: 0 for ch in signal_list }
    while True:
        # flag changes in order to only report results when an event is detected
        detected = False
        for ch in signal_list:
            if GPIO.input(ch):
                detected = True
                counts[ch] += 1
        if detected:
            print(counts)
        # if an event remains high for more than 0.2 sec it might
        # be counted again on the next loop. Likewise if an event
        # comes and goes before the next loop it will be missed.
        time.sleep(0.2)

finally:
    GPIO.cleanup(sensor_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM