PROJECTS/PRJ_03

Encoder-Based PID Motor Control

Stability, overshoot and response tuning
COMPLETED4 weeks · 2024 Q4
Control TheoryArduinoEmbedded CPIDEncoders
Encoder-Based PID Motor Control
// PRJ_03
// OVERVIEW

Project Overview

A closed-loop motor control system using quadrature encoder feedback and a classic PID controller. Gains were tuned empirically to trade off overshoot, settling time and steady-state error.

Problem Statement

An open-loop PWM drive cannot hold position or velocity under load. A feedback controller is required to reject disturbances and track a reference reliably.

Objectives
  • ›Read a quadrature encoder in interrupts without missing counts
  • ›Implement proportional, integral and derivative terms in embedded C
  • ›Tune Kp, Ki, Kd for a stable response with minimal overshoot
  • ›Log response data for step-input analysis
// ARCHITECTURE

System Architecture

Architecture

Encoder → interrupt-driven counter → PID loop at fixed sample rate → PWM to H-bridge → motor → back to encoder. A UART link streams reference/measured values for offline plotting.

Working Principle

Error = reference − measured. PID output = Kp·e + Ki·∫e dt + Kd·de/dt, saturated and mapped to a PWM duty cycle.

// HARDWARE

Hardware Components

  • ▹DC motor with quadrature encoder
  • ▹H-bridge driver
  • ▹Arduino Uno
  • ▹Bench PSU + USB-serial link
// SOFTWARE

Software Stack

  • ▹Custom PID loop in embedded C
  • ▹Interrupt-driven encoder decoding
  • ▹Serial logger for step-response capture
// JOURNEY

Development Journey

Started with P-only to build intuition, added I to eliminate steady-state error, then D to damp overshoot. Retuned after mechanical load changes.

// DECISION
Fixed sample rate

A fixed loop period keeps the discrete PID equations valid and simplifies gain tuning.

// DECISION
Anti-windup clamp

The integral term is clamped when the PWM output saturates to prevent windup.

// CHALLENGES

Challenges & Solutions

// CHALLENGE
Missed encoder counts at high speed
// SOLUTION

Moved decoding into a hardware-interrupt ISR and used a compact state-machine decoder.

// CHALLENGE
Integral windup on large step inputs
// SOLUTION

Added output-saturation-aware integral clamping.

// RESULTS

Performance Results

// Steady-state error
≈ 0 counts
// Overshoot
< 10%
// Loop rate
1 kHz
// DOWNLOADS

Downloads

GitHub RepositoryProject Report (PDF)· pending
// FUTURE

Improvements & Lessons

Future Improvements
  • +Auto-tuning via relay feedback (Åström–Hägglund)
  • +Cascade position + velocity loops
  • +Move implementation to STM32 with hardware timer encoder mode
Lessons Learned
  • ›PID gains are coupled — tune one term at a time and log responses.
  • ›Anti-windup matters as much as the gains themselves.