usb_cam installation on Raspberry Pi 4 with Ros Noetic and Ubuntu Mate 20.04 - brennanyama/RobotOperatingSystem GitHub Wiki

usb_cam installation on Raspberry Pi 4 with Ros Noetic and Ubuntu Mate 20.04

usb_cam wiki can be found here.

1. Hardware setup

2. Test and configure USB camera

2.1. Test USB camera

You could follow these instructions, but we will evlaulate the camera using VLC media player instead.

First install VLC media player:

sudo apt install vlc

Once fully installed, 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.2. Find 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 driver filename associated with that device, usually something like /video0. You can determine this by the process of elimination: 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 (or /video0 if 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.3. Find USB camera compatibility 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.4. USB camera serial number

Assigning persistent symbolic names to USB devices defined by their serial number is useful when writing launch files for computers with multiple USB devices attached. If needed, you can read more about this process at this link.

3. Installing usb_cam

usb_cam has apt-get binaries, and is the preferred method of installation.

sudo apt-get install ros-noetic-usb-cam

This installs the usb_cam package to your ROS installation directory in /opt/ros/noetic/share/; this makes it available to all catkin workspaces built on your computer.

3.1. Create a catkin workspace and launch file

Let's create a new catkin workspace for testing the usb_cam node.

source /opt/ros/noetic/setup.bash
mkdir -p ~/Ros/Workspaces/TestUsbCam_ws/src
cd ~/Ros/Workspaces/TestUsbCam_ws
catkin_make

This creates and builds a new catkin workspace called TestUsbCam_ws with no local packages, i.e. you can only use packages that are installed to /opt/ros/noetic/share (this is fine since we are only testing usb_cam)

Next, let's create a launch file. We will start by copying the example launch file included with the usb_cam package to our TestUsbCam_ws catkin workspace.

mkdir -p ~/Ros/Workspaces/TestUsbCam_ws/launch
cp /opt/ros/noetic/share/usb_cam/launch/usb_cam-test.launch ~/Ros/Workspaces/TestUsbCam_ws/launch/usb_cam-test.launch

Edit the launch file, either by navigating to it in your file explorer or using terminal:

nano ~/Ros/Workspaces/TestUsbCam_ws/launch/usb_cam-test.launch

Replace the launch file with the following:

<launch>
  <node name="usb_cam" pkg="usb_cam" type="usb_cam_node" output="screen" >
    <param name="video_device" value="/dev/video0" />
    <param name="image_width" value="1280" />
    <param name="image_height" value="720" />
    <param name="pixel_format" value="yuyv" />
    <param name="framerate" value="30" />
    <param name="autofocus" value="false" />
    <param name="focus" value="0" />    
    <param name="contrast" value="32" />
    <param name="brightness" value="32" />
    <param name="saturation" value="32" />
    <param name="sharpness" value="22" />
    <param name="focus" value="0" />    
    <param name="camera_frame_id" value="usb_cam" />
    <param name="io_method" value="mmap"/>
  </node>
  <node name="image_view" pkg="image_view" type="image_view" respawn="false" output="screen">
    <remap from="image" to="/usb_cam/image_raw"/>
    <param name="autosize" value="true" />
  </node>
</launch>

This will change the resolution to 1280x720 with a framerate of 30 fps. Note that many settings are explicitly set to their defaults for clarity; you can read more about the usb_cam package parameters on its website.

⚠️ **GitHub.com Fallback** ⚠️