CodeTheRobot

ROS 2 Lyrical Luth · Lesson 55 of 86

ArUco markers for docking and alignment

Marker detection, pose estimation and a simple alignment controller.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain aruco markers for docking and alignment in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

An ArUco marker is a square black-and-white pattern with an ID. OpenCV's aruco module detects it in a camera image and, given the camera intrinsics and the marker's real size, estimates its pose in the camera frame. Robots use markers on a charging station or a pallet for precise docking and alignment.

Publish the marker pose as a TF frame (marker_<id>), transform it to base_link, and drive to reduce the lateral error and heading error: angular velocity = k · atan2(y, x). Stop when the distance is below a threshold and treat a lost marker as a reason to stop.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the aruco markers for docking and alignment example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

auto dict = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
// Choose the marker family: 4×4 bits, 50 IDs.
std::vector<int> ids; std::vector<std::vector<cv::Point2f>> corners;
// Outputs: which markers, and where their corners are in the image.
cv::aruco::detectMarkers(frame, dict, corners, ids);
// Find markers in the image.
std::vector<cv::Vec3d> rvecs, tvecs;
// Rotation and translation of each marker relative to the camera.
cv::aruco::estimatePoseSingleMarkers(corners, 0.10f, K, dist, rvecs, tvecs);
// Estimate poses for 10 cm markers using camera intrinsics K and distortion dist.
double w_cmd = 1.0 * std::atan2(tvecs[0][0], tvecs[0][2]);
// Steer toward the marker with gain 1.0 (x = sideways, z = forward).

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Mobile robot

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.

goalMOBILE ROBOT

Demo for ArUco markers for docking and alignment. Expected result: 0.197396

Practice code

Now type it yourself

Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice

#include <cmath>
#include <iostream>

int main() {
  const double marker_x = 0.5, marker_y = 0.1;  // marker in base_link: 0.5 m ahead, 0.1 m to the left
  const double gain = 1.0;
  // TODO: angular velocity command = gain * atan2(marker_y, marker_x)
  std::cout << w << '\n';
  return 0;
}

Expected output

0.197396
Stuck? Show the full solution (try it yourself first)
#include <cmath>
#include <iostream>

int main() {
  const double marker_x = 0.5, marker_y = 0.1;  // marker in base_link: 0.5 m ahead, 0.1 m to the left
  const double gain = 1.0;
  const double w = gain * std::atan2(marker_y, marker_x);
  std::cout << w << '\n';
  return 0;
}

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Place an ArUco marker in your simulated world, detect it with OpenCV, publish its pose as a TF frame, and write a controller that drives to 0.3 m in front of it. Stop safely when the marker is lost.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.