CodeTheRobot

Ubuntu Linux for Robotics · Lesson 17 of 96

udev rules for sensors and robots

Stable device names, permissions and symlinks for serial adapters, lidars and cameras.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain udev rules for sensors and robots 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

udev creates the device files in /dev when hardware appears. USB serial adapters get names like /dev/ttyUSB0 that can swap when you replug or reboot, and normal users often lack permission to open them. A udev rule matches a device by its vendor ID, product ID (and optionally serial number) and gives it a stable symlink (for example /dev/rplidar) and permissions.

Find the IDs with lsusb and udevadm info. Put rules in /etc/udev/rules.d/99-robot.rules, reload with sudo udevadm control --reload-rules and sudo udevadm trigger, and add your user to the dialout group for serial access (then log out and in). Test a rule with udevadm test before relying on it.

Guided lesson

Work through these steps

  1. 1.

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

  2. 2.

    Read the udev rules for sensors and robots 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

lsusb
# List USB devices with their vendor and product IDs (for example 10c4:ea60).
udevadm info -a -n /dev/ttyUSB0 | grep -E 'idVendor|idProduct|serial'
# Read the attributes you can match on.
SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SYMLINK+="rplidar", MODE="0666"
# A rule: this lidar adapter always appears as /dev/rplidar and is readable by everyone.
sudo udevadm control --reload-rules && sudo udevadm trigger
# Apply the new rules without rebooting.
ls -l /dev/rplidar
# Confirm the stable symlink exists.
sudo usermod -aG dialout $USER
# Allow your user to open serial ports (log out and in afterwards).

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 · The robot's computer

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.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for udev rules for sensors and robots. Expected result: /dev/rplidar -> ttyUSB0

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.

# /etc/udev/rules.d/99-robot.rules
# TODO: add a rule for tty devices with idVendor 10c4 and idProduct ea60 that creates the symlink rplidar
sudo udevadm control --reload-rules && sudo udevadm trigger
ls -l /dev/rplidar

Expected output

/dev/rplidar -> ttyUSB0
Stuck? Show the full solution (try it yourself first)
# /etc/udev/rules.d/99-robot.rules
echo 'SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SYMLINK+="rplidar", MODE="0666"' | sudo tee /etc/udev/rules.d/99-robot.rules
sudo udevadm control --reload-rules && sudo udevadm trigger
ls -l /dev/rplidar

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

Write a rule that gives a USB serial adapter a fixed name, unplug and replug it in a different port and confirm the name is unchanged, then explain why matching on the serial number matters when you have two identical adapters.

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.