- DC Motor Research Documentation : Motor Identification and PID Control
- Build the RP2040 Firmware
- Python Script Installation
DC Motor Speed and Position Control with Raspberry Pi Pico RP2040 and embassy-rs π¦. This is the framework to write a firmware code with USB communication and flash storage feature. We use Python to communicate with the firmware API via serial communication. It's possible to scale up the project with another applications. Please refer to the DC Motor Research Documentation for detail research on the DC Motor.
The table below shows the firmware features:
| Features | Details |
|---|---|
| PID Motor Control |
|
| Raw Byte Communication via USB CDC ACM |
|
| Encoder Reading Method |
|
| Flash Storage |
|
| Multicore |
|
| Components | Specification |
|---|---|
| Microcontroller | Raspberry Pi Pico RP2040 |
| Motor | Motor DC JGA25-370 12V Gearbox (1200 RPM) |
| Motor Driver | BTS7960 |
We can see the GPIO pin list on the firmware/main/src/resources/gpio_list.rs
| Pin Name | Motor_0 Pin | Motor_1 Pin |
|---|---|---|
| Motor_PWM_CW_PIN | GP15 |
GP3 |
| Motor_PWM_CCW_PIN | GP14 |
GP2 |
| Encoder_PIN_A | GP6 |
GP4 |
| Encoder_PIN_B | GP7 |
GP5 |
| PWM Slice | PWM_SLICE7 |
PWM_SLICE1 |
We have two main directories: firmware and script as shown on the graph below. To start with this project you can clone this repository and follows the instruction below.
.
βββ assets
βββ firmware
β βββ main
β β βββ src
β β βββ control
β β βββ resources
β β βββ tasks
| | βββ main.rs # Primary RP2040 PID DC Motor Control
β βββ playground # Experimental project (USB, Flash Storage)
βββ script
βββ BasicFunction
βββ Board
βββ Config
βββ DeviceOpFuncs # Device OP file folder to call the firmware API
βββ FWLogger
βββ Tool
βββ run.py # Python Script to Communicate with the Firmware- Create the RP2040 firmware binary (
elforuf2) to be flashed on the RP2040 - The main code of DC motor control is on
firmware/main - You can also try some features on
firmware/playground/before merge them to themain. - To build the project you can go to this link for build firmware
-
- The API to communicate with RP2040 is on
script/DeviceOpFuncs/DCMotor.toml - The structure of the communication is follows this pattern:
Command Format: Send Command --> [COMMAND_HEADER:u8] [OP_CODE:u8] [PARAMETER] Response Command --> [COMMAND_HEADER:u8] [ERROR_CODE:u8] [OP_CODE:u8] [RESPONSE_MESSAGE] Event Format: Received Event --> [EVENT_HEADER:u8] [EVENT_CODE:u8] [MOTOR_ID:u8] Logger Format: Received Log Data --> [LOGGER_HEADER:u8] [sequence:u8] [time_stamp:u32] [DATA_LOG:[i32;5]] DATA_LOG:[i32;5] = [current_pos, current_speed, commanded_pos, commanded_speed, commanded_pwm]-
You can see the
TOMLfile for more detail. -
β οΈ Please make sure to match theTOMLfile with the actual firmware op_code, input, output, and data type.
- The API to communicate with RP2040 is on
-
- Python script to communicate with the RP2040 including the firmware logger by executing the
run.py. - To run the python code you can type:
python -i script/run.py
- Or you can use
uvpython package manager for automatically handle the dependencies with this command:cd script uv run python -i run.py
- Python script to communicate with the RP2040 including the firmware logger by executing the
- On
script/run.pyyou can create custom code to command the RP2040. We have created the example such as:
| Example | Positive Direction | Negative Direction |
|---|---|---|
| Open Loop |
|
|
| Speed Control |
|
|
| Position Control |
|
|






