AprilTag machine code following

AprilTag machine code following1. Program Functionality2. Program Code Reference Path3. Program Startup3.1. Startup Commands3.2 Dynamic Parameter Adjustment4. Core Code4.1. apriltagFollow.py

1. Program Functionality

After the program starts, it automatically detects the AprilTag in the image. When a tag is detected, the servos and gimbal lock onto it, keeping it centered in the image, and the robot enters follow mode. Press the q/Q key to exit the program. The R2 button on the remote controller has a [Pause/Start] function for this function.

📝 The following object example uses a 3x3cm building block and an A36h11 series AprilTag. If you change the following object, you will need to adjust the corresponding parameters to achieve your desired effect!

2. Program Code Reference Path

3. Program Startup

3.1. Startup Commands

For the Raspberry Pi 5 controller, you must first enter the Docker container. This is not necessary for the Orin board.

Enter the Docker container (for steps, see the Docker section [3. Docker Submission and Multi-Terminal Access]).

Enter the terminal.

image-20250812194510782

When the machine code is detected, it will automatically outline the machine code and display its center position and area.

image-20250811193309113

Then, the robot enters follow mode. Slowly move the object, and the robot and servo gimbal will follow.

Alternatively, we can enter the following command to print the target center coordinates and area information:

image-20250811152024043

3.2 Dynamic Parameter Adjustment

Enter in the terminal:

image-20250811194258328

☑️ After modifying the parameters, click a blank area in the GUI to enter the parameter value. Note that this will only take effect for the current boot. To permanently effect the parameter, you need to modify it in the source code.

As shown in the figure above:

🛠 Parameter Explanation:

[mintra_r]: Critical tracking area. When the detected tag area is less than this value, the robot moves forward; when it is greater than this value, the robot moves backward.

[tolerance_x] and [tolerance_r]: X-axis position tolerance. When the deviation between the tag center point's x-coordinate and the screen center (320) is less than this value, alignment is considered complete and no further angle adjustment is performed. Area tolerance: When the deviation between the detected tag area and the critical tracking area (mintra_r) is less than this value, the distance is considered appropriate and no further distance adjustment is performed.

[linear_Kp], [linear_Ki], [linear_Kd]: PID control of linear velocity during robot following.

[angular_Kp], [angular_Ki], [angular_Kd]: Angular velocity PID control during the car's following process.

[Servo_Kp], [Servo_Ki], [Servo_Kd]: Speed ​​PID control during the servo gimbal following process.

4. Core Code

4.1. apriltagFollow.py

This program has the following main functions:

Some core code is as follows: