Welcome to MSPM0 Smart Robot Car repository
5.Ultrasonic avoid(Servo PTZ)
Ultrasonic avoid (Servo PTZ)
Ultrasonic avoid (Servo PTZ)1. Software-Hardware2. Brief Principle2.1 Hardware Schematic Diagram2.2 Physical Connection Diagram2.3 Control Principle3. Main Functions4. Experimental Phenomenon
This tutorial is a comprehensive experiment combining multiple peripherals. You can understand individual peripherals before conducting this experiment.
1. Software-Hardware
KEIL5
MSPM0G3507 Robot Development Board
Ultrasonic module, Servo PTZ, TT encoded motor*2: external
Type-C data cable or DAP-Link
For program download or simulation to the development board
2. Brief Principle
2.1 Hardware Schematic Diagram

.png)
2.2 Physical Connection Diagram
- Wiring
.png)
| Servo (S1) | Car | Specific Meaning |
|---|---|---|
| Signal line (yellow) | PA28 (yellow) | Signal line |
| VCC (red) | VCC (red) | Power supply |
| GND (brown) | GND (black) | Ground line |
| Servo (S2) | Car | Specific Meaning |
|---|---|---|
| Signal line (yellow) | PA7 (yellow) | Signal line |
| VCC (red) | VCC (red) | Power supply |
| GND (brown) | GND (black) | Ground line |
Ultrasonic Module:
Note: When using servo PTZ, you need to add jumper cap to connect 5v and choose, see white line.
2.3 Control Principle
Obtain the distance of objects in front through the ultrasonic module on the 9G servo PTZ, servo controls left, right and front distances, and control the car to move forward, backward, turn left, and stop based on the distance.
The ultrasonic module can obtain the distance of obstacles in front. We use this distance to determine the obstacle position relative to the car: When there is an obstacle directly in front of the car, the servo on the 9G servo PTZ will rotate to the left and right sides, finally obtaining the distances on the left and right sides of the car. We control the car to turn left or right based on these distances. The initial angle and the rotation angles to the left and right sides can be modified by changing the code.
- Program Flowchart

| Module | Function |
|---|---|
| Ultrasonic Module | External information collection: obtain obstacle distance |
| Servo | Control ultrasonic module steering |
| Motor | Motion control |
3. Main Functions
This tutorial does not use PID control for car movement
xxxxxxxxxxFunctions introduced before will not be explained again!
Function: get_servo_distance
| Function Prototype | void get_servo_distance() |
|---|---|
| Function Description | Servo PTZ ultrasonic obstacle avoidance program |
| Input Parameters | None |
| Return Value | None |
xxxxxxxxxxFor underlying drivers, refer to Chapter 4 and Chapter 5 tutorialsFor application layer, you can read the source code in the project files yourself
4. Experimental Phenomenon
After successfully downloading the program, press the RESET button on the development board and observe the car's effect!
xxxxxxxxxxFor program download, refer to [3. Development Environment Setup and Usage: 3.Uniflash Programming]
Phenomenon:
With obstacle: The car first stops, then judges left and right distances, and turns left or right based on the distances
Without obstacle: The car moves forward
For specific judgment, refer to the flowchart or code.