yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
MSPM0 Smart Robot Car📖
1.Quick Start Guide
  • 1. Assembly Videos
  • 2. Quick Start Videos
  • 3. Battery charging
  • 4. Notes
2.Introduction to the MSPM0 Car
  • 1.About MSPM0
  • 2.Development board interface description
3.Development environment setup and usage
  • 1.CCS Theia development
  • 2.MDK Arm development
  • 3.Uniflash burning
  • 4.External crystal oscillator configuration
  • 5.Project Migration
4.MSPM0 basic course
  • 1. LED light control (GPIO)
  • 2. Buzzer Control (GPIO)
  • 3. Button Control (EXTI)
  • 4. Serial Port (UART)
  • 5. Voltage Detection (ADC)
  • 6. Temperature Detection (ADC)
  • 7. Independent Watchdog (WWDG)
  • 8. Window Watchdog (WWDG)
  • 9. Timer (TIM)
  • 10. Breathing LED (PWM)
  • 11. I2C Communication (I2C)
  • 12. External Flash (SPI)
  • 13. Internal Flash (Flash)
  • 14. DMA
  • 15. FreeRTOS
5.Sensor basic course
  • 1.Servo control
  • 2.Ultrasonic servo PTZ basic use
  • 3.Motor PID control
  • 4.OLED display photosensitive value
  • 5.Infrared avoid sensor
  • 6.1 Infrared line tracking sensor-8 channel infrared tracking sensor module
  • 6.2 Infrared line tracking sensor-4 channel infrared tracking sensor module
  • 7.Infrared remote control
  • 8.Ultrasonic ranging
  • 9.RGB light drive
  • 10.MPU6050 raw data acquisition
  • 11.MPU6050 data fusion
  • 12.K210 vision recognition module
  • 13.K230 vision recognition module
6.Smart car course
  • 1.1 Car line tracking-8 channel infrared tracking sensor module
  • 1.2 Car line tracking-4 channel infrared tracking sensor module
  • 2.1 Drawing a circle-8 channel infrared tracking sensor module
  • 2.2 Drawing a circle-4 channel infrared tracking sensor module
  • 3.Ultrasonic following
  • 4.Ultrasonic avoid
  • 5.Ultrasonic avoid(Servo PTZ)
  • 6.Infrared sensor avoid
  • 7.Light following
  • 8.Gyroscope calibration straight-line walk
  • 9.Infrared remote control
  • 10.Autonomous driving car
7.Vision application course
  • 1.K210 autonomous driving
  • 2.K210 color tracking
  • 3.K210 handwritten digit action
  • 4.K210 color response
  • 5.K210 road sign indication action
  • 6.K210 following machine code
  • 7.K210 QR code command
  • 8.K230 vision line tracking
  • 9.K230 color tracking
  • 10.K230 machine code tracking
  • 11.K230 QR code command
  • 12.K230 autonomous avoid
  • 13.K230 license plate tracking
  • 14.K230 face tracking
  • 15.K230 gaze direction tracking
  • 16.K230 palm tracking
  • 17.K230 target tracking
  • 18.K230 human body tracking
  • 19.K230 OCR character recognition
8.Extended application course (Sold separately)
  • 1.Voice interaction module communication
  • 2.Voice interaction control
  • 3.K230 road sign indication action(Voice)
  • 4.K230 color response(Voice)
  • 5.K230 gesture control(Voice)
  • 6.K230 rock-paper-scissors(Voice)
  • 7.8-channel grayscale color line tracking
  • 8.Bluetooth remote control
  • 9.K210 Scenario-based autonomous driving
  • 10.K230 Scenario-based autonomous driving
Download
  • Code
  • SCH
  • 3D Model File
  • MSPM0 Data Sheet
  • Voice firmware
  • Hardware interface manual
Download ZIP

Welcome to MSPM0 Smart Robot Car repository

3.K210 handwritten digit action

K210 Handwritten Digit Action

K210 Handwritten Digit Action1. Hardware Connection2. Code Analysis3. Main Functions4. K210 Program Burning5. 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

Start SymbolLengthRoutine NumberRoutine GroupData AmountDigit IDSeparatorChecksumEnd Symbol
$XX11BB01XXX,XX#

Control Flow Chart

No
Yes
Start
Vision module recognizes specified digit
Car stationary
MCU parses data and controls car response

bsp_k210_use.c

​x
// 处理K210发来的单字节数据 / Process single byte data from K210
void recv_k210msg(uint8_t recv_msg) 
{
    // 检测包头'$',开始接收新消息 / Detect packet header '$', start receiving new message 
    if (recv_msg == '$')
    {
        new_flag = 1;
    }
    
    // 检测包尾'#',结束接收并校验 / Detect packet tail '#', end reception and verify 
    if(recv_msg == '#' )
    {
        if( buf_len == r_index)  // 数据长度匹配 / Data length matches 
        {
            new_flag = 0;
            tou_flag = 0;
            len_flag = 0;
            
            // 校验和计算:减去接收的校验和字节 / Checksum calculation: subtract received checksum byte 
            buf_crc -= buf_msg[r_index-1];
            buf_crc %= 256;
            
            if(buf_crc == buf_msg[r_index-1])  // 校验正确 / Checksum correct 
            {
                deal_recvmsg();  // 处理接收数据 / Process received data
            }
            else  // 校验失败 / Checksum failed 
            {
                r_index = 0;
                buf_crc = 0;
            }
        }
    }
    
    // 正在接收新消息时的处理逻辑 / Processing logic when receiving new message
    if(new_flag == 1 )
    {
        // 首次接收包头 / First time receiving packet header 
        if(recv_msg == '$' && tou_flag == 0)  
        {
            tou_flag = 1;
        }
        else
        {
            // 存储数据并递增索引 / Store data and increment index
            buf_msg[r_index++] = recv_msg; 
            buf_crc += recv_msg;            
            // 未获取长度时,从首字节读取长度 / When length not obtained, read length from first byte
            if(len_flag == 0)  
            {
                buf_len = buf_msg[0];
                len_flag = 1;
            }
        }
    }
}
•
// 解析接收的完整消息 / Parse received complete message 
void deal_recvmsg(void)
{
    //长度校验 / Length verification 
    if(r_index!=buf_len)
    {
        buf_len = 0;
        return ;
    }
    
    // 提取有效数据(过滤逗号) / Extract valid data (filter commas)
    for(index = 0 ;index<number;index++)
    {
        if(buf_msg[4+index] == 0x2c && i_duo ==0)//遇到逗号且未过滤 / Encounter comma and not filtered
        {
            i_duo = 1;
            continue;
        }
        data[data_i++]=buf_msg[4+index];//存储有效数据 / Store valid data
        i_duo =0;
    }
    
    // 重置接收状态 / Reset receive state 
    buf_crc = 0;
    r_index = 0;
    memset(buf_msg,0,sizeof(buf_msg));
    // 处理解析后的数据 / Process parsed data 
    deal_data(eg_num);    
​

K210 Partial Source Code

xxxxxxxxxx
def send_data(x,y,w,h,msg):
    start = 0x24
    end = 0x23
    length = 5
    class_num = 0x0B #例程编号 / Routine number
    class_group = 0xBB #例程组 / Routine group
    data_num = 0x00 #数据量 / Data amount
    fenge = 0x2c #逗号 / Comma separator
    crc = 0 #校验位 / Checksum
    data = [] #数据组 / Data array
•
    #参数都为0 / All parameters are 0
    if x==0 and y==0 and w==0 and h ==0:
        pass
    else:
        #x(小端模式) / x (little-endian mode)
        low = x & 0xFF #低位 / Low byte
        high = x >> 8& 0xFF #高位 / High byte
        data.append(low)
        data.append(fenge) #增加","
        data.append(high)
        data.append(fenge) #增加","
...
kpu = KPU()
# 加载MNIST手写数字识别模型 / Load MNIST handwritten digit recognition model
kpu.load_kmodel("/sd/KPU/mnist/uint8_mnist_cnn_model.kmodel")
•
•
while True:
    gc.collect()  # 释放内存 / Free memory
    img = sensor.snapshot()  # 捕获图像 / Capture image
    
    # 图像预处理(适配模型输入要求) / Image preprocessing (adapt to model input requirements)
    img_mnist1 = img.to_grayscale(1)
    img_mnist2 = img_mnist1.resize(112, 112)
    img_mnist2.invert()
    img_mnist2.strech_char(1)
    img_mnist2.pix_to_ai()
•
    # 运行模型并获取输出 / Run model and get output
    out = kpu.run_with_output(img_mnist2, getlist=True)
    max_mnist = max(out)  # 最大概率值 / Maximum probability value
    index_mnist = out.index(max_mnist)  # 识别结果(数字索引) / Recognition result (digit index)
    msg_ = str(index_mnist)
    send_data(0,0,0,0,msg_)  # 封装数据(仅发送识别结果) / Package data (only send recognition result)
    
    # 计算置信度(通过sigmoid函数归一化) / Calculate confidence (normalize through sigmoid function)
    score = KPU.sigmoid(max_mnist)
    
    # 当识别为数字8且置信度>0.999时,显示并发送结果 / When recognized as digit 8 and confidence > 0.999, display and send result
    if index_mnist == 8:
        if score > 0.999:
            display_str = "num: %d" % index_mnist
            print(display_str, score)
            img.draw_string(4,3,display_str,color=(0,0,0),scale=2)
            serial.send_bytearray(send_buf)  # 串口发送数据 / Send data via serial port
    # 识别为数字5且置信度>0.999时,仅显示结果 / When recognized as digit 5 and confidence > 0.999, only display result
    elif index_mnist == 5:
        if score > 0.999:
            display_str = "num: %d" % index_mnist
            print(display_str, score)
            img.draw_string(4,3,display_str,color=(0,0,0),scale=2)
    
    lcd.display(img)  # 在LCD上显示图像 / Display image on LCD

3. Main Functions

BSP_Loop

Function Prototypevoid BSP_Loop(void)
Function DescriptionMain loop function: detects key short press events to switch g_key_flag state. When g_key_flag is 0, stops motor control and brakes; when 1, motor moves forward at speed 200, if K210 recognizes target with category 11 and ID '8', after 3 cumulative recognitions, OLED displays recognition result, controls motor to reverse for 2 seconds and resets counter, while displaying currently recognized ID on OLED
Input ParametersNone
Return ValueNone

4. K210 Program Burning

After downloading and opening CanMV IDE, we need to first drag the k210_minist.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

5. Experimental Phenomena

After burning the program, press KEY1 button, the car moves forward continuously. If digit 8 is recognized, the car will reverse for a period of time and then resume forward movement, while the recognized digit will be displayed on the OLED.

image-20251029213340731

K210 color tracking
K210 color response

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文