K230 Visual Pan-Tilt Tracking

This tutorial is a comprehensive experiment combining multiple peripherals. You can understand each individual peripheral first before conducting this experiment.

1. Software - Hardware

2. Brief Principle

1. Hardware Schematic Diagram

Hardware Schematic Diagram

2. Physical Connection Diagram

K30 Pan-Tilt Wiring (Note: The wiring in the following figure is only for position reference. We provide K230 double-ended PH2.0 4Pin all-black wires at the factory, which have a foolproof design. You don't need to worry about wiring issues.) K30 Pan-Tilt Wiring

Motor Wiring Motor Wiring

3. Control Principle

ModuleFunction
K230 Vision ModuleRecognize faces and send data to the microcontroller through the serial port
Two-dimensional Electric Pan-Tilt + Electronic ControlControl the movement of the K230 module's position

Communication Protocol:

Start SymbolLengthSeparatorRoutine NumberSeparatorxSeparatorySeparatorwSeparatorhEnd Symbol
$XX,06,XXX,XXX,XXX,XXX#

Microcontroller part: Receive the data sent by the K230 through the serial port, and parse and process each byte of the data. After extracting the valid data into an array, perform PID processing on the returned coordinates. Then, the microcontroller drives the servo motors according to the processed data to control the pan-tilt to follow the movement of the face.

3. Main Functions

Function: APP_K230X_PID_Calc

Function Prototypefloat APP_K230X_PID_Calc(float actual_value)
Function DescriptionCalculate the output value of the X-axis using PID
Input ParameterThe difference between the target value and the actual value in the X direction
Return ValueThe output value of the incremental PID calculation for the X-axis

Function: APP_K230Y_PID_Calc

Function Prototypefloat APP_K230Y_PID_Calc(float actual_value)
Function DescriptionCalculate the output value of the Y-axis using PID
Input ParameterThe difference between the target value and the actual value in the Y direction
Return ValueThe output value of the incremental PID calculation for the Y-axis

Function: APP_k230X_Y_Init

Function Prototypevoid APP_k230X_Y_Init(void)
Function DescriptionInitialize the X/Y axes of the K230
Input ParameterNone
Return ValueNone

Function: PwmServo_Set_Angle

Function Prototypevoid PwmServo_Set_Angle(uint8_t index, uint8_t angle)
Function DescriptionSet the angle of the PWM servo motor
Input Parameter 1Servo motor number [0 - (Number of servo motors - 1)] (0: The servo motor in the bottom horizontal axis direction is connected to S1 of the development board; 1: The servo motor in the vertical direction is connected to S2 of the development board)
Input Parameter 2Angle value [0 - 180]
Return ValueNone

4. Partial Code Analysis

5. Experimental Phenomenon

After the program is successfully downloaded, you need to make the face appear within the K230 screen so that the K230 can recognize it. After recognition, press the Key1 button on the expansion board, and the pan-tilt will follow the movement of the face.

How to download the K230 code: Required tools: K230, SD card, CanMV IDE K230 You need to find the face_detection.py file in the project folder, then drag it to CanMV IDE K230, and then choose to save the opened script as main.py to CanMV. image-20250619185937467