CodeTheRobot

Ubuntu Linux for Robotics · Lesson 65 of 96

Camera drivers

USB cameras with v4l2, RealSense, camera_info and calibration.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain camera drivers in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Cameras appear as /dev/video0 (video4linux). List them with v4l2-ctl --list-devices and their formats with v4l2-ctl --list-formats-ext. ROS 2 driver nodes such as usb_cam or v4l2_camera publish sensor_msgs/Image and camera_info; RealSense cameras use realsense2_camera.

Calibrate the camera with camera_calibration to get accurate camera_info (intrinsics and distortion). Choose a resolution and frame rate the USB bus and CPU can sustain; a USB 2 port limits high-resolution streams. Test the raw image with cheese or v4l2-ctl before blaming ROS.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the camera drivers commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

sudo apt install v4l-utils ros-lyrical-v4l2-camera
# Install the tools and a driver node.
v4l2-ctl --list-devices
# List camera devices.
v4l2-ctl -d /dev/video0 --list-formats-ext
# See supported resolutions and frame rates.
ros2 run v4l2_camera v4l2_camera_node --ros-args -p video_device:=/dev/video0 -p image_size:=[640,480]
# Start the ROS 2 driver.
ros2 topic hz /image_raw
# Check the frame rate.
ros2 run rqt_image_view rqt_image_view
# Look at the image.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Sensor view

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

SENSOR VIEW

Demo for Camera drivers. Expected result: A list of video devices

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: list the video devices with v4l2-ctl

Expected output

A list of video devices
Stuck? Show the full solution (try it yourself first)
v4l2-ctl --list-devices

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

List your camera's formats, start the driver at 640×480, measure the rate at two different resolutions, and view the image. Note the CPU use in htop for each.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.