K210 Color Tracking

K210 Color Tracking1. Hardware Connection2. Code Analysis3. Main Functions4. K210 Method for Extracting Specified Color LAB Thresholds5. K210 Program Burning6. Experimental Phenomena

1. Hardware Connection

K210 Vision ModuleMSPM0G3507
5V5V
GNDGND
RXTX2
TXRX2

Physical Connection

image-removebg-preview

Schematic Diagram

image-20251029164049976

image-20251029164030395

image-20251029164356332

2. Code Analysis

Control Principle

K210 Protocol

Experiment RoutineStart SymbolLengthRoutine NumberRoutine GroupData AmountxSeparatorySeparatorwSeparatorhSeparatorChecksumEnd Symbol
Color Recognition$XX01BB04XXX,XXX,XXX,XXX,XX#

Control Flow Chart

No
Yes
No
Yes
Start
Vision module detects threshold color
Car stationary
Detected color block in center?
MCU parses data and controls car response

bsp_k210_use.c

k210部分源码

3. Main Functions

Color_Trace

Function Prototypevoid Color_Trace(int x, int y, int w, int h)
Function DescriptionTarget tracking control function based on color recognition: when target area width and height are not 0, applies first-order low-pass filtering to target center X coordinate and area, calculates X direction (left-right) position deviation and Y direction (forward-backward) area deviation, after PID calculation and limiting, controls car movement through Motion_Car_Control; if target area width and height are 0, stops car
Input Parametersx: Target area top-left X coordinate
y: Target area top-left Y coordinate
w: Target area width
h: Target area height
Return ValueNone

BSP_Loop

Function Prototypevoid BSP_Loop(void)
Function DescriptionMain loop function: enables motor control, displays x, y, w, h parameters of k210_msg on OLED, calls Color_Trace function to implement color tracking control based on target area parameters
Input ParametersNone
Return ValueNone

4. K210 Method for Extracting Specified Color LAB Thresholds

After connecting to CanMV IDE via micro USB, drag follow_color_k210.py to CanMV IDE to open it, then need to first click the run button, ensure the color to be recognized appears in the frame buffer, here using red as an example, click menu bar Tools -> Machine Vision -> Threshold Editor, get image from frame buffer

image-20251029204408672

image-20251029204957872

You can manually drag the slider positions to adjust thresholds, white pixels are tracked, finally fill in the LAB thresholds to our color thresholds

image-20251029205301117

image-20251029205546204

5. K210 Program Burning

After downloading and opening CanMV IDE, we need to first drag the follow_color_k210.py file from the k210 source code provided in this course to CanMV IDE to open it, then connect the IDE, here using helloworld.py as example

image-20251029180540342

After successful IDE connection, the phenomenon is as follows

image-20251029181342135

Here we use helloworld.py as an example, open the top menu bar Tools -> Save currently open script as (main.py) to CanMV Cam

image-20251029205906069

image-20251029181630720

Both Yes/No can be selected here. When the following status appears, the write is successful.

image-20251029181701230

6. Experimental Phenomena

After burning the program, the car will follow red color blocks by default. When the color is far from the car, the car moves forward; when the color is close to the car, the car moves backward; when the color moves left/right, the car also follows left/right, keeping the recognized color in the center of the screen. It is recommended not to move the color block too quickly, otherwise it may leave the recognition area.