Welcome to MSPM0 Smart Robot Car repository
3.Motor PID control
Motor PID Control
Motor PID Control1. Software and Hardware2. Basic Principles2.1 Hardware Schematic2.2 Physical Connection Diagram2.3 Control Principle3. Project Configuration3.1 Description3.2 Pin Configuration4. Main Functions4.1 Main Functions4.2 User Functions5. Experimental Phenomenon
This tutorial demonstrates: using timer to control 2 TT encoded motors and print encoder values via serial port, where KEY buttons can control the motor rotation mode.
1. Software and Hardware
KEIL5
MSPM0G3507 Robot Development Board
TT Encoded Motor * 2
Type-C data cable or DAP-Link
For programming download or simulation to the development board
2. Basic Principles
2.1 Hardware Schematic
.png)

2.2 Physical Connection Diagram
.png)
2.3 Control Principle
Motor speed and direction control: configure timer PWM output mode and change the duty cycle of PWM signal;
Motor encoder reading: configure timer to count motor encoder signals.
- TT Encoded Motor
| Motor Model | TT Encoded Motor |
|---|---|
| Motor Rated Voltage | 6V |
| Encoder Supply Voltage | 3.3-5V |
| Gearbox Reduction Ratio | 1:45 |
| Magnetic Ring Line Count | 13 lines |
| Encoder Type | AB Phase Hall Encoder |
Count Value
Maximum count value reduction ratio encoder line count
4: Represents encoder frequency multiplicationBased on the count value of one rotation, the car's speed can be calculated. This tutorial does not involve speed conversion
- Motor Driver
MSPMOG3507 integrates 2 AT8236 single-channel brush DC motor driver chips.
Input pins IN1 and IN2 control the output state of the H-bridge. The following table shows the logical relationship between input and output:
| IN1 | IN2 | OUT1 | OUT2 | Function |
|---|---|---|---|---|
| 0 | 0 | Z | Z | Coast, Sleep |
| 1 | 0 | H | L | Forward |
| 0 | 1 | L | H | Reverse |
| 1 | 1 | L | L | Brake |
When using PWM control to achieve speed adjustment function, the H-bridge can operate in two different states: fast decay or slow decay.
| IN1 | IN2 | Function |
|---|---|---|
| PWM | 0 | Forward PWM, Fast Decay |
| 1 | PWM | Forward PWM, Slow Decay |
| 0 | PWM | Reverse PWM, Fast Decay |
| PWM | 1 | Reverse PWM, Slow Decay |
PWM Period: T = 50us → f = 20KHz
Encoder Reading
Obtain count value by directly reading timer CNT register value.

The tutorial is set to count on TI1 and TI2, input signals not inverted;

xxxxxxxxxxWhether the counter counts up or down depends on the AB phase signals
3. Project Configuration
3.1 Description
You can refer to the basic tutorial to complete the development environment setup.
The non-omitted project configuration part is the key point that needs to be configured in this tutorial.
3.2 Pin Configuration
- GPIO





- USART

- Timer Configuration: Here demonstrates TIMA1 configuration

- PWM Configuration: Here demonstrates motor PWM configuration

4. Main Functions
For detailed code, you can open the project files we provide and view the source code in the Bsp folder.
4.1 Main Functions
| Function | Corresponding Tutorial |
|---|---|
| Button | Development Board Basic Tutorial: Button Control |
| Serial Port | Development Board Basic Tutorial: Serial Communication |
4.2 User Functions
Function: PID_Location_Calc
| Function Prototype | float PID_Location_Calc**(PID_t *pid, float actual_val) |
|---|---|
| Function Description | Positional PID algorithm |
| Input Parameter 1 | PID_t *pid: structure |
| Input Parameter 2 | actual_val: PID target value |
| Return Value | PID output value |
Function: PID_Calc_One_Motor
| Function Prototype | float PID_Calc_One_Motor(uint8_t motor_id, float now_speed) |
|---|---|
| Function Description | Separately calculate one channel PID |
| Input Parameter 1 | motor_id: motor id. Use 0 and 1 to represent left and right motors respectively |
| Input Parameter 2 | now_speed: set PID target value |
| Return Value | Positional PID calculated output value |
Function: encoder_update
| Function Prototype | void encoder_update(void) |
|---|---|
| Function Description | Encoder data update |
| Input Parameters | None |
| Return Value | None |
Function: Scheduler_Run
| Function Prototype | void Scheduler_Run(void) |
|---|---|
| Function Description | Configure task scheduler for task scheduling |
| Input Parameters | None |
| Return Value |
5. Experimental Phenomenon
After successfully downloading the program, press the RESET button on the development board and open the serial port debugging assistant to observe the phenomenon!
xxxxxxxxxxFor program download, refer to 【3. Development Environment Setup and Usage: 3. Uniflash burning】
Phenomenon:
Press KEY1: can select mode;
Press and hold KEY1 then press KEY2: confirm the selected mode;
mode1: Display encoder values on OLED and send via serial port, then receive encoder values through serial port assistant;
mode2: Motor rotates forward and can display encoder values on OLED and send via serial port, then receive encoder values through serial port assistant;
mode3: Motor rotates in reverse and can display encoder values on OLED and send via serial port, then receive encoder values through serial port assistant;