Welcome to MicroROS V2 Robot Car repository
7. AprilTag Machine Code Follow
AprilTag Machine Code Follow (No-PTZ)
1. Experiment Objective
The camera tracks an AprilTag marker, with body rotation tracking + distance following keeping the Tag centered in the frame and maintaining a constant distance. Distance following is based on the 3D pose Z value (actual depth), which is more accurate than pixel-size estimation.
2. Experiment Principle
1. Key Terms
| Term | Description |
|---|---|
| AprilTag | A visual fiducial marker system, similar to a QR code but designed specifically for pose estimation; computes 3D position from the 4 corner points |
| 3D Pose Estimation | Computes 6-DOF (x, y, z, roll, pitch, yaw) from the Tag corner coordinates + known physical size + the PnP algorithm |
| Distance Following (based on pose_z) | Directly uses the Z value of the 3D pose (actual depth in meters), far more accurate than pixel-size estimation |
| tag36h11 | One of the AprilTag families, with 36-bit encoding + 11-bit error detection and high robustness |
| quad_decimate | Image downsampling factor; increasing it speeds up detection but reduces long-distance detection |
| QoS | best_effort (image input + cmd_vel downlink), depth=1 |
2. Technical Architecture
The distinctive feature of AprilTag following is that it uses the 3D pose Z value rather than pixel size for distance estimation, which is more accurate:
/camera/image_raw (BEST_EFFORT)→ april_tag_tracker_node (enable_distance_follow:=true)├── apriltag.Detector.detect() → 4 corner points + 3D pose (pose_z)└── Body control (10 Hz):├── Pixel error → PD angular velocity└── pose_z error → PD linear velocity → /cmd_vel
3. Core Topics Quick Reference
Firmware Downlink
| Topic | Message Type | QoS | Field | Range | Direction |
|---|---|---|---|---|---|
/cmd_vel | geometry_msgs/Twist | best_effort + keep_last(1) | linear.x | -0.30~0.30 m/s | += forward, −= backward |
angular.z | -0.60~0.60 rad/s | += left turn, −= right turn |
3. Experiment Steps
1. Hardware Preparation
| Hardware | Requirement |
|---|---|
| PTZ Version | ❌ |
| No-PTZ Version | ✅ |
| Required Peripherals | ESP32 camera (mounted on bracket), a printed AprilTag marker (tag36h11 family) |
2. Start Agent + Camera + Joint Model (Terminal 1)
xxxxxxxxxxros2 launch microros_v2_bringup car_base.launch.py
3. Start AprilTag Follow (Terminal 2)
xxxxxxxxxxros2 launch microros_v2_no_ptz_visual_control_pkg april_tag_follow.launch.py
5. Operation Instructions
| Key | Function |
|---|---|
| Space | Toggle follow/standby |
| q / ESC | Exit |
Distance Follow Behavior: Tag approaches (pose_z decreases) → move backward; Tag moves away (pose_z increases) → move forward; depth within tolerance (±0.02 m) → stay still.
4. Experiment Result
- Tag approaches → the car moves backward to keep the distance (based on the 3D depth Z value)
- Tag moves away → the car moves forward to follow
- Tag distance within the desired range (0.25 m) → the car stays still
- The terminal outputs the Tag 3D pose (x, y, z, roll, pitch, yaw) in real time
- Tag lost → the car automatically stops once the lost-target count exceeds the threshold
5. Code Explanation
Source code paths:
- Launch:
~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/launch/april_tag_follow.launch.py- Node source:
~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/microros_v2_no_ptz_visual_control_pkg/april_tag_tracker_node.py
1. Core Node Explanation
april_tag_follow.launch.py launches april_tag_tracker_node, with enable_distance_follow defaulting to true. Unlike other follow modes, AprilTag distance following directly uses the 3D pose Z value:
x# Source file: ~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/microros_v2_no_ptz_visual_control_pkg/april_tag_tracker_node.py# Distance follow: based on the 3D depth Z value (not pixel size)if self._dist_follow and tz is not None and tz > 0: err_z = self._target_z_m - tz # target_z unit: meters if abs(err_z) > self._dist_tol_z: lx = self._dist_kp * err_z lx = clamp(lx, -self._max_lx, self._max_lx) if self._reverse_linear: lx = -lx cmd.linear.x = lxKey Advantage: Accurate distance is obtained without calibrating the actual target size. The tag_size parameter (default 0.048 m) must match the physical side length of the printed Tag, otherwise the 3D pose will be inaccurate.
2. Key Parameters
Parameter definition files:
- Launch:
~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/launch/april_tag_follow.launch.py- Node:
~/microros_ws/src/microros_v2_no_ptz_visual_control_pkg/microros_v2_no_ptz_visual_control_pkg/april_tag_tracker_node.py
| Parameter | Type | Default | Description |
|---|---|---|---|
tag_family | str | tag36h11 | AprilTag family |
tag_size | float | 0.048 m | Tag physical side length (affects 3D pose accuracy) |
quad_decimate | float | 2.0 | Image downsampling factor |
tag_select_strategy | str | largest | Multi-Tag selection strategy |
deadband_x_pixel | float | 30.0 px | Horizontal deadband |
angular_kp / angular_kd | float | 1.8 / 0.55 | Angle PD gains |
max_angular_z | float | 0.6 rad/s | Maximum turning speed |
enable_distance_follow | bool | true | Enable distance following |
target_tag_pose_z | float | 0.25 m | Desired following distance (based on 3D pose_z) |
distance_tolerance_pose_z | float | 0.02 m | Distance tolerance deadband |
distance_kp / distance_kd | float | 1.5 / 0.03 | Distance PD gains |
max_linear_x | float | 0.30 m/s | Maximum forward/backward speed |
3. Node Description
| Node | Package | Description |
|---|---|---|
april_tag_tracker_node | microros_v2_no_ptz_visual_control_pkg | AprilTag 6-DoF tracking + following, 3D depth distance following |
6. Common Issues
| Issue | Cause | Solution |
|---|---|---|
| Inaccurate distance | The Tag size parameter tag_size does not match the actual size | Precisely measure and set the Tag physical side length |
| Tag not detected | Tag too small, reflective, or quad_decimate too large | Enlarge the Tag and lower quad_decimate |
| Pose jitter | Lighting changes or the Tag tilted too much | Keep the Tag flat and the lighting uniform |
| Slow following response | Unreasonable distance PD parameters | Adjust distance_kp / target_tag_pose_z |