Skip to content

schrodingerslemur/pid-controller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adaptive Motor Control | Sensor-Fusion PID on STM32

Embedded closed-loop motor control system on an STM32 MCU using sensor fusion between a quadrature encoder and an IMU (gyro/accel). The system runs self-tuning PID controller that adapts gains when large disturbances are detected. Telemetry and tuning are exposed over UART and a small Python dashboard for realtime plotting and logging.

Features

  • STM32 (HAL) project structure (CMSIS/HAL-compatible).
  • Quadrature encoder input, PWM motor drive, current sense (ADC), and IMU (I²C) reading.
  • Sensor fusion module (complementary filter + optional simplified Kalman).
  • PID controller with gain scheduling (self-tuning heuristic).
  • UART telemetry protocol (CSV-like) and a Python dashboard tool (telemetry/telemetry.py) that logs and plots data.
  • Makefile for arm-none-eabi-gcc toolchain and flash.sh script template for OpenOCD/ST-Link.
  • Container-friendly: included docker/ files show how to wrap the Python dashboard.

Layout

  • src/ : embedded C sources (main + modules)
  • inc/ : headers
  • firmware/ : Makefile, linker script template
  • telemetry/ : Python telemetry client to read UART and plot/save data
  • docs/ : design notes and tuning guide

Quick start (developer machine)

  1. Install ARM toolchain (e.g. arm-none-eabi-gcc) and OpenOCD or use STM32CubeIDE.
  2. Build:
cd firmware
make
  1. Flash with OpenOCD or via CubeProgrammer. Example (OpenOCD):
./flash.sh
  1. Run telemetry (on host):
python3 telemetry/telemetry.py --port /dev/ttyUSB0 --baud 115200

Files of interest

  • src/main.c – system init, control loop, UART telemetry.
  • src/pid.c / inc/pid.h – PID implementation & adaptive logic.
  • src/sensor_fusion.c / inc/sensor_fusion.h – complementary filter & state estimator.
  • telemetry/telemetry.py – simple real-time plotting and logging (uses pyserial & matplotlib).

Hardware connections (example)

  • Motor driver PWM -> TIMx_CHy (PAx / PBx depending on board)
  • Encoder A/B -> EXTI / TIM encoder interface pins
  • IMU (e.g. MPU6050) -> I2C1 (SCL/SDA)
  • Current sense -> ADC1_INx
  • UART TX/RX -> USB-UART or ST-Link VCP

Tuning notes

Start with conservative PID gains (small P, zero I/D). Increase P until oscillation, add I to eliminate steady-state error, then D to damp. Adaptive routine will increase gains slightly under heavy load conditions detected by discrepancy between encoder velocity and IMU-derived velocity.

About

Embedded Adaptive Motor Control on STM32

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors