Skip to content

tutla53/dc-motor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

331 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DC Motor PID Control

Home Next >>
DC Motor Research Documentation

Navigation

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.

Features

The table below shows the firmware features:

Features Details
PID Motor Control
  • Speed Control β†’ Step Motion Profile Only
  • Position Control:
    • Step Motion Profile
    • Trapezoid Motion Profile
  • Use Fixed-point Numbers Calculation via fixed
Raw Byte Communication via USB CDC ACM
  • Get and Set Firmware Config
  • Save Firmaware Config on the Flash Memory
  • Controlling Motor based on PID Control Mode
  • Firmware Logger up to 1kHz sampling frequency
Encoder Reading Method
  • RP2040 PIO via embassy_rp::pio_programs::rotary_encoder::PioEncoder
Flash Storage
  • Save firware config on the flash memory to simulate EEPROM via sequential_storage
Multicore
  • CORE0 β†’ USB Communication + Logger + Flash Storage
  • CORE1 β†’ Motor Control (200 Hz sampling rate)

Hardware


Specification

Components Specification
Microcontroller Raspberry Pi Pico RP2040
Motor Motor DC JGA25-370 12V Gearbox (1200 RPM)
Motor Driver BTS7960

GPIO Map

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

Getting Started

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

firmware

  • Create the RP2040 firmware binary (elf or uf2 ) 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 the main.
  • To build the project you can go to this link for build firmware

script

  • Firmware API

    • 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 TOML file for more detail.

    • ⚠️ Please make sure to match the TOML file with the actual firmware op_code, input, output, and data type.

  • Python Script

    • 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 uv python package manager for automatically handle the dependencies with this command:
      cd script
      uv run python -i run.py

Project Example

  • On script/run.py you 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

Home Next >>
DC Motor Research Documentation

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors