Close

Device consistency rules

A project log for Low cost stereo camera

Building a cheap stereo camera using COTS parts

SolenoidSolenoid 02/11/2017 at 13:420 Comments

The second camera arrived, but now how to distinguish between the two in Linux?

Consistently recognising connected physical hardware is important, in this case as left and right cameras.

When connecting two USB cameras in Linux they automatically appear as:

/dev/video0
/dev/video1
However there is no way to know if video0 is the left or the right camera, and this might even change after a reboot. To fix that Linux uses something called udev rules, there has even been a Hackaday article about how to set up a rule for a device. Basically based on the attributes reported by a device one can set a new name, such as:
/dev/stereo_camera/left
/dev/stereo_camera/right

First one needs to get the distinguishing information from the devices, for that there is a useful command:

udevadm info -a -p $(udevadm info -q path -n /dev/video0)
udevadm info -a -p $(udevadm info -q path -n /dev/video1)

This will print a lot of information about the devices. You should look at the block that starts with something similar to:

looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1':

In there note down the idVendor and idProduct attributes for both cameras, these will distinguish the two devices.

To set up a new rule create a new rules file:
nano /etc/udev/rules.d/81-stereo_camera.rules

Add the following lines, your attributes will differ obviously:

SUBSYSTEM=="usb", ATTRS{idVendor}=="0424", ATTRS{idProduct}=="9514", SYMLINK+="stereo_camera/left"
SUBSYSTEM=="usb", ATTRS{idVendor}=="1e4e", ATTRS{idProduct}=="0110", SYMLINK+="stereo_camera/right"

Finally reload the udev rules with the following commands:

udevadm control --reload-rules
udevadm trigger --attr-match=subsystem=usb

There should be some new devices under /dev/stereo_camera/*, execute the following command to check if the devices have been properly recognised:

ls -la /dev/stereo_camera/*

It will print the paths to the respective devices which will remain consistent over reboots:

/dev/stereo_camera/left -> ../bus/usb/001/006
/dev/stereo_camera/right -> ../bus/usb/001/005

The paths /dev/video0 and /dev/video1 still remain, but unlike /dev/stereo_camera/* it is not guaranteed that they will stay consistent over reboots (video0 for the left camera might become the right camera after reboot).

Discussions