CodeTheRobot

Ubuntu Linux for Robotics · Lesson 72 of 96

ArUco markers

Generating, printing and detecting ArUco markers for docking and alignment.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain aruco markers 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

ArUco markers are square black-and-white codes with an ID that a camera can detect and measure in 3D. Generate them with OpenCV's aruco module, print them at a known size (for example 10 cm) and measure the printed size exactly, because pose accuracy depends on it.

A detector node publishes each marker's pose relative to the camera; transform it with TF2 into base_link and use it for docking or alignment. Good lighting, a flat marker and a calibrated camera matter more than the code. Treat a lost marker as a reason to stop.

Guided lesson

Work through these steps

  1. 1.

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

  2. 2.

    Read the aruco markers 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 ros-lyrical-ros2-aruco
# Install an ArUco detection package if available for your distribution.
python3 -c "import cv2; d=cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_50); cv2.imwrite('marker7.png', cv2.aruco.generateImageMarker(d, 7, 400))"
# Generate marker 7 as a 400 pixel image.
ros2 run ros2_aruco aruco_node --ros-args -p marker_size:=0.10 -p camera_frame:=camera_link
# Detect markers that are 10 cm wide.
ros2 topic echo /aruco_poses --once
# Read the detected marker pose.
ros2 run tf2_ros tf2_echo base_link marker_7
# See the marker in the robot's base frame.

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 ArUco markers. Expected result: marker7.png is created

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: generate marker 7 of the DICT_4X4_50 dictionary at 400 pixels and save it as marker7.png
ls marker7.png

Expected output

marker7.png is created
Stuck? Show the full solution (try it yourself first)
python3 -c "import cv2; d=cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_50); cv2.imwrite('marker7.png', cv2.aruco.generateImageMarker(d, 7, 400))"
ls marker7.png

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

Print marker 7 at 10 cm, measure it with a ruler, detect it, and compare the distance in the detection with a tape measure at 0.5 m and 1 m. Report the error.

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.