← Back to Lab Hub RP2040 MicroPython Flight Lab

Quadcopter Motor Physics & PID Stabilizer Simulator

MicroPython PWM Control, Essential Force Vectors, and PID Feedback Loop

🏹 Force Vector Physics: Total Thrust ($\vec{T}$) acts perpendicular to the frame. Gravity ($\vec{F}_g$) acts straight down. The Net Vector ($\vec{F}_{\text{net}} = \vec{T} + \vec{F}_g$) drives physical movement!
🛡️ The Crucial Role of PID Auto-Stabilization: Human vs. Closed-Loop Control
A quadcopter is inherently unstable. Without feedback control, tiny motor inaccuracies or wind gusts create uncommanded torque moments ($\tau = r \times F$). Because human nervous reaction times (~200ms) are far too slow, the RP2040 MicroPython PID controller acts as a digital inner ear executing 500 times per second ($500\text{Hz}$) to recalculate motor PWM adjustments ($\Delta \text{PWM}$) before human error causes a crash!
P (Proportional): Restores angle error D (Derivative): Damps rotation speed Open-Loop (PID OFF): Unstable rotational drift
Total Propeller Thrust ($\vec{T}$)
Gravity Force ($\vec{F}_g = m \cdot g$)
Net Resultant Motion ($\vec{F}_{\text{net}}$)
Front Pair Thrust ($F_{\text{front}}$)
Rear Pair Thrust ($F_{\text{rear}}$)
Main Throttle Base PWM (All Motors): 32768 u16 (50% Hover)
Target Pitch Angle Command: 0.0° (Level)
Front Motors PWM (M1, M4): 32768 u16
Rear Motors PWM (M2, M3): 32768 u16
Total Thrust Force ($\vec{T}$): 2.45 N
Net Pitch Torque (Moment): 0.00 Nm
Current Pitch Angle: 0.0°
🐍 MicroPython Flight Controller Inspector
from machine import Pin, PWM
import utime

# RP2040 500Hz PWM Motor Driver Outputs
m_front = PWM(Pin(18), freq=500) # Front (M1, M4)
m_rear = PWM(Pin(19), freq=500) # Rear (M2, M3)

# PID Auto-Stabilization Loop
def update_pitch_control(base_pwm, target_deg, current_deg):
  error = target_deg - current_deg # Err: 0.0°
  pid_adj = 350.0 * error - 80.0 * gyro_rate
  # Apply Motor Mixing Matrix
  pwm_f = int(base_pwm - pid_adj)
  pwm_r = int(base_pwm + pid_adj)
  # Write duty_u16 to RP2040 registers
  m_front.duty_u16(32768)
  m_rear.duty_u16(32768)
📝 Concept Check Quiz
Why is an active PID feedback loop required for quadcopter stability rather than setting constant motor speeds?