Welcome to MSPM0 Smart Robot Car repository
3.Ultrasonic following
Ultrasonic Following
Ultrasonic Following1. Ultrasonic Module Introduction2. Hardware Connection3. Partial Code Analysis4. Main Functions5. Experimental Phenomenon
1. Ultrasonic Module Introduction
is a module that uses ultrasound for non-contact physical measurement. It can accurately measure physical quantities such as distance, speed, and flow rate by emitting and receiving ultrasonic signals, and converts measurement results into digital signal output. This article will explain the working principle and function of ultrasonic modules.
Main types of ultrasonic modules on the market:
- HC-SR04 Ultrasonic Module
- US-100 Ultrasonic Module
- US-015 Ultrasonic Module
- HY-SRF05 Ultrasonic Ranging Module
- HC-SR04 Ultrasonic Ranging Module
This experiment's ultrasonic module information: HC-SR04
Ranging Principle: Input a high-level signal of more than 10 microseconds to the trigger end of the ultrasonic module to emit ultrasonic waves. After emitting ultrasonic waves and before receiving the returned ultrasonic waves, the receiving end is at high level. Therefore, the program can calculate the distance of the measured object from the high-level pulse duration of the "response" pin. Test Distance = (High Level Time * Sound Speed (340M/S)) / 2;

Note: The above timing diagram shows that only a 10us or more pulse trigger signal needs to be provided. The module internally will emit 8 40kHz cycle levels and detect echoes. Once an echo signal is detected, it outputs a response signal. The pulse width of the response signal is proportional to the measured distance. Therefore, the distance can be calculated from the time interval between transmitting the signal and receiving the response signal.
2. Hardware Connection
| Ultrasonic Module | MSPM0G3507 |
|---|---|
| VCC | VCC |
| Trig | Trig |
| Echo | Echo |
| GND | GND |
.png)
.png)
Control Flowchart
3. Partial Code Analysis
bsp_ultrasonic.c
xxxxxxxxxx// 获取测量距离 / Get measurement distancefloat Hcsr04GetLength(void){ volatile float sum = 0; volatile uint32_t pulse_time_us = 0; for(int i = 0; i < AVG_COUNT; i++) // 测5次取平均 / Measure 5 times and take average { // 发送触发脉冲 / Send trigger pulse SR04_TRIG(0); delay_us(2); SR04_TRIG(1); delay_us(15); // 10-20us触发脉冲 / 10-20us trigger pulse SR04_TRIG(0); while(SR04_ECHO() == 0); // 记录起始时间(100us精度) / Record start time (100us accuracy) Open_Timer(); uint32_t start_count = usHcCount; // 等待下降沿 / Wait for falling edge while(SR04_ECHO() > 0 ); Close_Timer(); // 计算脉冲时间(us) / Calculate pulse time (us) uint32_t end_count = usHcCount; pulse_time_us = (end_count - start_count) * 100; // 转换为us / Convert to us // 计算距离(标准公式:cm = us / 58) / Calculate distance (standard formula: cm = us / 58) sum += (pulse_time_us / 58.0f); delay_ms(5); } distance = sum / AVG_COUNT; // 计算平均值 / Calculate average return distance;}void ultrasonic_track(){ if(int_value <20) { if(int_value <=15) { if(int_value <=10) { wheel_State(MOTION_BACK,100); } else if(int_value<=12) { Motion_Set_Speed(0,0); } else Motion_Set_Speed(50,50); } else Motion_Set_Speed(150,150); } else Motion_Set_Speed(200,200);}4. Main Functions
Hcsr04GetLength
| Function Prototype | float Hcsr04GetLength(void) |
|---|---|
| Function Description | Measures distance through HC-SR04 ultrasonic sensor, takes average of AVG_COUNT consecutive measurements. Sends 10-20us trigger pulse, detects echo pulse width and converts to distance (unit: cm), formula: distance = pulse time (us) / 58 |
| Input Parameters | None |
| Return Value | float type, average measured distance (cm) |
ultrasonic_track
| Function Prototype | void ultrasonic_track() |
|---|---|
| Function Description | Controls car movement based on ultrasonic measured distance (int_value): when distance is less than 20cm, decelerates, stops, or reverses according to different ranges; when distance is greater than or equal to 20cm, moves forward at speed 200 |
| Input Parameters | None |
| Return Value | None |
5. Experimental Phenomenon
After connecting the car properly, connecting the OLED module, and burning the program to MSPM0, place your hand or an obstacle in front of the ultrasonic sensor, the car will maintain a certain distance from the obstacle. When the distance increases, the car moves forward; when the distance decreases, the car moves backward.