usb_cam installation on Raspberry Pi 4 with ROS Melodic and Raspberry Pi OS - brennanyama/RobotOperatingSystem GitHub Wiki

usb_cam installation on Raspberry Pi 4 with ROS Melodic and Raspberry Pi OS

Important note: At the time of writing, usb_cam can be built on ROS Melodic running on the Raspbeery Pi OS; however, the image_view package cannot be built. Therefore, we recommend using Ubuntu Mate until ROS support for Debian is improved.

1. Hardware setup

2. Check that your USB camera works

You could follow these instructions, but if you are running Raspberry Pi OS, you should already have VLC media player installed. VLC has the convenient ability to play from a capture device in real time. We will go with this route instead.

First, plug in the USB camera. Open VLC media player:

vlc

Then in the VLC window, navigate to Media>Open Capture Device (also Ctrl+C rather confusingly). A new Open Media window will open. Ensure you are in the Capture Device tab. Under Capture Mode select Video Camera. Under Device Selection>Video Device Name, select /dev/video0. Leave Audio Device Name blank, and Video Standard as undefined. Click Play, and the video stream from the USB camera should open in the original VLC window.

2.1. USB camera driver filename

When you plug in any device to a Linux computer, it will assign an appropriate driver to the device, and create a new driver "file" in the /dev directory. You can view this directory in terminal using:

ls /dev

Generally, to interact with your USB camera, you need to know the specific filename of the driver associated with that device. You can determine this by the process of eliminataion: unplug the device, and run ls /dev, then plug in the device and run ls /dev again, and look for the new device name in the output. Generally speaking, the first USB camera you plug in will instantiate as /video0 (unless your computer already has a built in webcam!). Plugging in more devices without unplugging the original will create more numbered driver files /video1, /video2, etc...

2.2. USB camera parameters

You can list all USB devices connected to your computer in terminal using:

lsusb

If you can't tell which device is your camera based on the name, use the process of elimination (unplug and run lsusb, then plug in and run lsusb again, looking for the change in the output). Look for the Bus and Device number associated with your device. Then to get details about the device in terminal use:

lsusb -s {$BUSNUMBER}:{$DEVICENUMBER} -v

This will tell you all of the possible settings for the camera. Look particularly for the field VideoStreaming Interface Descriptor, as this denotes a video device. There should be multiple listings for a single video device. The important considerations are:

  • wWidth: the supported horizontal resolution
  • wHeight: the supported vertical resolution
  • bDescriptorSubtype: the supported pixel_format of the video device
    • (FORMAT_UNCOMPRESSED): yuyv pixel format
    • (FORMAT_MJEPG): mjpeg pixel format
  • dwFrameInterval: the supported frame interval (which can you to calculate framerate)
    • This number is given as a period in [s E-7] (for some silly reason), e.g. 30 [fps] = 333333 [s E-7]; 60 [fps] = 166666 [s E-7]. You should choose a framerate that matches one of these values (in [fps]).

2.3. USB camera serial number

Read about this here.

3. Installing usb_cam

At the time of writing, the apt-get build for usb_cam did not work, so we will need to build the package and its missing dependencies from source.

Assuming that your catkin workspace directory is located in ~/Ros/Workspaces/ create a new catkin workspace. We will call this Test_usb_cam_ws:

mkdir -p ~/Ros/Workspaces/Test_usb_cam_ws/src

Clone the usb_cam git repository to the src directory of your catkin workspace.

cd ~/Ros/Workspaces/Test_usb_cam_ws/src
git clone https://github.com/ros-drivers/usb_cam

Note that cloning a ROS package to your catkin workspace only includes the package files and not it's dependencies. To install the missing dependencies when building from source, you should always first try to use rosdep. rosdep attempts to install the missing dependencies for a package; however, this process does not always work. In this case, rosdep failed. Normally, this command would be:

rosdep update
rosdep install --from-paths src/ --ignore-src --rosdistro {$ROSDISTRO}

Because rosdep failed, we need to install the missing dependencies manually. These missing packages can be installed to your local catkin workspace or to your ROS installation in /opt/ros/{$ROSDISTRO}/share; installing to your workspace will only affect packages in your catkin workspace and installing to /opt/ros will affect all packages. This process requires some amount of trial and error. This is the process that worked for our particular hardware setup.

cd ~/Ros/Workspaces/Test_usb_cam_ws/src
git clone https://github.com/ros-perception/image_common
git clone https://github.com/ros/common_msgs
sudo apt-get install libyaml-cpp-dev
sudo apt-get install libavcodec-dev
sudo apt-get install libswscale-dev
cd ~/Ros/Workspaces/Test_usb_cam_ws/
catkin_make_isolated

We were able to get this to build; however, this does not include the image_view package, which is not mandatory, but extremely useful for testing purposes. That said, we were unable to get the image_view package working, as too many of the dependencies were incompatible with Debian (on which Raspberry Pi OS).

To test that the package works, we can run the example launch file included in the usb_cam package. Because the image_view package is not installed, you will need to remove the lines in the example launch file that launch the image_view package. Remember that we now need to source the setup.bash file in our local catkin workspace instead of the default one in /opt/ros/...:

source ~/Ros/Workspaces/Test_usb_cam_ws/devel/setup.bash
roslaunch /home/pi/Ros/Workspaces/Test_usb_cam_ws/src/usb_cam/launch/usb_cam-test.launch