首页 > 解决方案 > 用 raspberrypi 和 python 读取两个红外传感器

问题描述

我正在用树莓派和两个红外传感器在 python 中开发一个项目。

红外发射器位于 Arduino 上,每 50 毫秒连续发送一个代码,此速率无法更改。

我需要 raspberrypi python 脚本每 500 毫秒检查两个红外传感器(左和右)。如果左侧传感器在此 500ms 时间间隔内接收到代码,则显示接收到的代码,否则,显示左侧传感器未接收到代码。使用正确的传感器重复该过程。

我为此创建了一个简单的 python 脚本。但是,我总是需要清除缓冲区,以便已读取的旧值不会干扰检查。我正在用“while”一个一个地读取缓冲区值来做到这一点。

EVDEV 是否具有清除缓冲区的刷新功能,还是有更好的方法来执行此操作?

#!/usr/bin/python2.7

from evdev import InputDevice
import time

device1 = InputDevice('/dev/input/event4') 
device2 = InputDevice('/dev/input/event3')

while(True):

        dataSensor1 = device1.read_one()
        dataSensor2 = device2.read_one()

        if(dataSensor1!=None):
                print("Left: ", dataSensor1.value)

                #delete old readings from the Left sensor queue
                while device1.read_one()!=None:
                        pass
        else:
                print("no reading on the left sensor")

        if(dataSensor2!=None):
                print("Right: ", dataSensor2.value)

                #delete old readings from the Right sensor queue
                while device2.read_one()!=None:
                        pass
        else:
                print("no reading on the right sensor")

        time.sleep(0.5)

标签: pythonraspberry-pievdev

解决方案


推荐阅读