Welcome to STM32-BoardCar repository
10.K230 Color Line Patrol
k230 color line patrol
k230 color line patrol1. Software-Hardware2. Brief principle1. Hardware schematic2. Physical connection diagram3. Control principle3. Main functions4. Partial code analysis5. Experimental phenomenon
This tutorial is a comprehensive experiment combining multiple peripherals. You can first understand a single peripheral and then conduct this experiment.
1. Software-Hardware
STM32F103CubeIDE
STM32 robot development board
k230 module: external
Type-C data cable or ST-Link
Download or simulate the development board
2. Brief principle
1. Hardware schematic

2. Physical connection diagram
Motor wiring

K230 wiring (Note: The wiring in the figure below is for position reference only. We are equipped with K230 dual-head PH2.0 4Pin all-black wire, with fool-proof design, no need to worry about wiring problems)

3. Control principle
Program flow chart
Control principle:
PID control
PID control is a commonly used closed-loop control algorithm that minimizes the error between the actual output and the expected output of the system by continuously adjusting the output of the controller.
| PID parameters | Function | Purpose |
|---|---|---|
| P (Proportional) | Determines the response speed of the PID controller output | Accelerates the system response speed |
| I (Integral) | Determines the response degree of the PID controller to the accumulated error | Eliminates steady-state error |
| D (Differential) | Determines the response degree of the PID controller to the error change rate | Improves the steady-state performance of the system |
| Module | Function |
|---|---|
| k230 visual module | Learn the color of the track and then send the data to the microcontroller through the serial port |
Communication protocol:
When k230 recognizes the color track, it will send a data frame through the serial port in the following format
| Experimental routine | Start character | Length | Delimiter | Routine number | Delimiter | x | Delimiter | y | Delimiter | w | Delimiter | h | End character |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Color recognition | $ | XX | , | 01 | , | XXX | , | XXX | , | XXX | , | XXX | # |
MCU part:
Receive the data sent by k230 through the serial port and process each byte of the data. Extract valid data from the data frame content and store it in the array. Then perform PID processing on the array data, and the MCU uses the processed data to drive the motor to move along the track.
3. Main functions
Function: APP_IRR_PID_Init
| Function prototype | void APP_IRR_PID_Init(void) |
|---|---|
| Function description | Initialize k230, infrared PID parameters |
| Input parameters | None |
| Return value | None |
Function: APP_K230_PID_Calc
| Function prototype | void APP_K230_PID_Calc(void) |
|---|---|
| Function description | Control motor movement after PID calculation |
| Input parameters | None |
| Return value | None |
Function: wheel_Ctrl
| Function prototype | void wheel_Ctrl(int16_t V_x, int16_t V_y, int16_t V_z) |
|---|---|
| Function description | Control motor movement after PID calculation |
| Input parameter 1 | Speed in the x-axis direction (straight) |
| Input parameter 2 | Speed in the y-axis direction (translation) |
| Input parameter 3 | Speed in the z-axis direction (rotation) |
| Return value | None |
For the underlying driver, you can refer to [Chapter 3: Advanced Timer Tutorial] and [Chapter 4: Motor Control Tutorial]For the application layer, you can read the source code in the project file by yourself
4. Partial code analysis
yb_protocol.c
xvoid Pto_Data_Parse_K230(uint8_t *data_buf, uint8_t num){uint8_t pto_head = data_buf[0];uint8_t pto_tail = data_buf[num-1];if (!(pto_head == PTO_HEAD && pto_tail == PTO_TAIL)) {return;}
uint8_t data_index = 1;uint8_t field_index[PTO_BUF_LEN_MAX1] = {0};int i = 0;
// Parse comma separatorfor (i = 1; i < num-1; i++) {if (data_buf[i] == ',') {data_buf[i] = 0;field_index[data_index] = i;data_index++;}}
// Handle pure string without comma Road sign recognitionif (data_index == 1) {uint8_t len = num - 2;memcpy(lubiao, (char*)data_buf+1, len);lubiao[len] = '\0'; // Make sure the string ends with '\0'}else { // Process multi-field datafor (i = 0; i < data_index; i++) {uint8_t start = field_index[i] + 1;uint8_t len;
if (i == data_index - 1) {// Last field, length is calculated before the end marklen = (num - 1) - start;} else {// Middle field, length is the next delimiter position minus the current start positionlen = field_index[i + 1] - start;}
if (i == 6 && data_index == 7) {memcpy(msg, (char*)data_buf + start, len);msg[len] = '\0'; // Add string terminator}else if (i == 2 && data_index == 3) {memcpy(game, (char*)data_buf + start, len);game[len] = '\0'; // Add string terminator}else {values1[i] = Pto_Char_To_Int((char*)data_buf + start);}}
// Protocol length verificationuint8_t pto_len = values1[0];if (pto_len != num) {printf("pto_len error:%d , data_len:%d\n", pto_len, num);return;}pto_id = values1[1];}int x = values1[2];int y = values1[3];int w = values1[4];int h = values1[5];// int degress = values[7];/* Open when debugging// printf("dmcode:x:%d, y:%d, w:%d, h:%d,\n", x, y, w, h);// printf("msg:%s\n", game);// // printf("game:%d\n", pto_id// Debug output// printf("msg:%s\n", msg);*/}5. Experimental phenomenon
After successfully downloading the program, we need to experiment in an environment with relatively stable light. (Because the color will be affected by the light, if the tracked color is not collected, you need to press the K230 reset button to re-collect)
Put the track color under the k230 lens to learn. After learning, press the k1 button on the expansion board, and the car will patrol the track.