mateus_guilherme
Posts: 20
Joined: Sat Feb 15, 2020 6:36 pm

How to monitor two infrared sensors individually

Sat May 02, 2020 7:09 pm

Hello

I am developing a project that needs to monitor two infrared sensors (vs1838) at the same time. It is a robot that detects a value encoded in NEC (or RC6, RC5, whatever). If the signal is detected on the right sensor, it turns to the left and vice versa.

I know that the LIRC package is a great tool for infrared sensors, but apparently it only accepts a single infrared sensor.

Does anyone know an example in python to monitor 2 sensors at the same time in raspberypi4??

thank you

HiassofT
Posts: 305
Joined: Fri Jun 30, 2017 10:07 pm
Location: Salzburg, Austria
Contact: Website

Re: How to monitor two infrared sensors individually

Sat May 02, 2020 7:18 pm

You can load the gpio-ir overlay multiple times (with different gpio_pin parameters), this will give you two rc, lirc and input event devices.

Just use ir-keytable to configure those (either to identical or different protocols / keymaps) and read the events from the lirc or input devices (adding a udev rule to symlink the devices to known names might be helpful so you know which device is connected to which gpio pin / receiver).

BTW: No need to use the userspace lircd, it's 2020 and the linux kernel ships with nice builtin IR support :)

so long,

Hias

HiassofT
Posts: 305
Joined: Fri Jun 30, 2017 10:07 pm
Location: Salzburg, Austria
Contact: Website

Re: How to monitor two infrared sensors individually

Sat May 02, 2020 7:32 pm

Forgot to add: you can also configure the rc devices with different keymaps and map the same scancode eg to KEY_L on one receiver and KEY_R on the other. Then you'll get 'l' or 'r' via stdin (just like when you press the keys on a keyboard).

A more general problem though might be that both IR receivers may receive the same signal due to reflections etc - that's a different issue though.

so long,

Hias

mateus_guilherme
Posts: 20
Joined: Sat Feb 15, 2020 6:36 pm

Re: How to monitor two infrared sensors individually

Thu May 07, 2020 2:07 pm

Hello

I thank you for your answer, it helped me a lot.

I implemented a small python script. He must check every 100ms if there is a code received in each sensor (left and right). However, I always need to delete the old read values that are in the queue to be processed. Is there a more efficient way for this script?

Code: Select all

#!/usr/bin/python2.7

from evdev import InputDevice
import time

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

print(device1)
print(device2)

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.1)

mateus_guilherme
Posts: 20
Joined: Sat Feb 15, 2020 6:36 pm

Re: How to monitor two infrared sensors individually

Fri May 08, 2020 11:18 am

Hello

I resolved the issue by implementing separate threads to monitor each sensor individually.

Return to “General discussion”