Skip to content
ncoriosPublic

About

1-DOF arm control from first principles. NumPy plant + PID/feedforward controller, four experiments isolating each term, with derivation notes on steady-state error, discretization, anti-windup, and signal processing.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

23 Commits

Folders and files

Repository files navigation

pid-arm

Warmup project building toward locomotion RL research. The goal was to develop ground-level control theory intuition before working in environments where reward shaping and policy behavior depend heavily on understanding what a well-tuned controller looks like and why it fails.

The project implements a 1-DOF revolute arm under gravity in a pure NumPy simulator, then closes the loop with four controllers of increasing sophistication: P → PI → PID → PID + gravity feedforward. Each experiment isolates one concept and produces quantitative results. Supporting notes cover PID theory, discretization, integral windup, and basic signal processing (noise, frequency content, low-pass filters) at the level needed for real hardware.


Plant

plants/arm.py — point-mass revolute arm, Euler integration.

Equations of motion:

I·θ̈ = τ_applied − m·g·l·sin(θ) − b·θ̇
I = m·l²

Parameters used across all experiments:

Parameter Value
Mass m 1.0 kg
Link length l 1.0 m
Gravity g 9.81 m/s²
Joint damping b 0.1 N·m·s/rad
Timestep dt 0.001 s
Setpoint θ_d π/2 rad (90°)
Initial angle 0 rad

Controller

control/control.py — PID class with separate P, I, D methods and an optional feedforward term.

τ = K_p·e + K_i·∫e dt + (−K_d·θ̇) + τ_ff(θ)
e = θ_d − θ
τ_ff(θ) = m·g·l·sin(θ)

The D term is implemented on measurement (−K_d·θ̇) rather than error derivative to avoid derivative kick on setpoint changes. Integral is forward Euler (integrator += e·dt). Feedforward is the exact gravity torque from the arm model.


Experiments

All four experiments use a step input from 0 to π/2 rad. Gains are set to demonstrate each term's effect, not to be globally optimal.

Experiment 1 — P only

numpy_experiments/p_only.py | Kp=10, Ki=0, Kd=0

P control acts as a spring: τ = K_p·e. To hold the arm at 90°, the controller must produce enough torque to balance gravity (m·g·l ≈ 9.81 N·m), which requires a nonzero error. This is structural — reducing the error reduces the torque, which lets gravity pull the arm back down.

Steady-state error analysis. The linear approximation e_ss = mgl/Kp gives 0.981 rad. The exact solution, accounting for the nonlinear sin(θ) gravity term, is found by iterating θ_ss = θ_d − mgl·sin(θ_ss)/Kp, converging to a value that matches the simulation.

P-only step response

Experiment 2 — PI

numpy_experiments/pi.py | Kp=10, Ki=1, Kd=0

The integral term accumulates error over time and increases torque until e → 0, at which point τ_I = m·g·l without any model. This eliminates steady-state error for any constant disturbance. With K_d=0 and low damping, the system oscillates — the integral keeps winding up past the setpoint and the arm overshoots repeatedly.

PI step response

Experiment 3 — PID

numpy_experiments/pid.py | Kp=10, Ki=1, Kd=8

Adding K_d damps the oscillations by opposing angular velocity (−K_d·θ̇). K_d=8 is intentionally high to clearly show the damping effect. The tradeoff is a significantly slower rise time — the system is overdamped and approaches the setpoint sluggishly.

PID step response

Experiment 4 — PID + Gravity Feedforward

numpy_experiments/pid_ff.py | Kp=200, Ki=0.3, Kd=26

τ = τ_PID + m·g·l·sin(θ) — the feedforward term cancels the dominant gravity disturbance analytically, so the PID only handles residual error. This allows much higher gains without the integral needing to wind up to balance gravity. K_p can be pushed to 200 (20× the previous runs) for fast response, K_i is almost vestigial (0.3), and K_d is tuned for critical damping. The result is sub-0.35s settling with <0.1% overshoot.

PID + gravity feedforward step response


Results

Experiment Kp Ki Kd SS Error (rad) SS Error (°) Rise Time (s) Settle Time (s) Overshoot
P only 10 0 0 0.731 41.9° 0.546 never 12.2%
PI 10 1 0 0.063 3.6° 0.536 never 41.8%
PID 10 1 8 0.010 0.54° 20.0 36.2 0%
PID + FF 200 0.3 26 0.000 −0.02° 0.248 0.350 0.1%

Rise time to 90% of setpoint. Settling within 2% band. P-only runs 100s, PI runs 50s; neither truly settles (nonzero/oscillating SS error). PI's "SS error" is the mean residual of its non-decaying oscillation, not a settled value.

The PI run has nonzero steady-state error in the table because the integral oscillates around the setpoint without decaying — it is close but never converges within the run window. PID settles but is overdamped by design. PID+FF demonstrates what happens when the model-based and feedback terms are combined correctly.


Notes

notes/pid-theory.md

Covers:

  • P, I, D terms with physical intuition for the arm (spring/damping/memory analogy)
  • Why P-only has structural steady-state error: e_ss = disturbance / Kp
  • Why I eliminates steady-state error for any constant disturbance without a model
  • Integral windup: actuator saturation causes the integrator to accumulate past the useful range; three mitigation strategies (conditional integration, back-calculation, clamping)
  • D on measurement vs. D on error: derivative kick with setpoint steps, why -K_d·θ̇ is preferred for hardware
  • Discretization: forward Euler for I, finite difference for D, Nyquist-style rule for dt
  • Ziegler-Nichols tuning and a practical alternative
  • Feedforward structure: τ = τ_PID + τ_ff(θ), where FF handles modeled dynamics and PID handles residuals

notes/signal-processing.md

Covers:

  • Continuous vs. discrete signals, sampling
  • How differentiation amplifies noise: a ±0.001 rad jitter at 1 ms timestep becomes ±1 rad/s before K_d is applied
  • Frequency content: real arm motion at 1–20 Hz, noise at 100s–1000s Hz
  • Filter taxonomy: LPF, HPF, BPF, BSF — everything derivable from LPF
  • First-order IIR low-pass filter: y[k] = α·x[k] + (1−α)·y[k−1]
  • Computing α from cutoff frequency: α = ω_c·dt / (1 + ω_c·dt)
  • Lag/noise tradeoff: small α → smooth, high lag; large α → responsive, poor rejection
  • Complete LowPassFilter class with proper initialization to avoid startup transient

Structure

pid-arm/
├── control/
│   └── control.py          # PID class, feedforward function
├── plants/
│   └── arm.py              # 1-DOF arm dynamics
├── numpy_experiments/
│   ├── data_helpers.py     # save_run(), solve_ss()
│   ├── p_only.py
│   ├── pi.py
│   ├── pid.py
│   └── pid_ff.py
├── notes/
│   ├── pid-theory.md
│   └── signal-processing.md
└── outputs/
    ├── arrays/             # .npz files from each run
    └── plots/              # step response plots

Setup

pip install -r requirements.txt   # numpy, matplotlib

# run from the repo root as modules (the scripts use package-relative imports)
python -m numpy_experiments.p_only
python -m numpy_experiments.pi
python -m numpy_experiments.pid
python -m numpy_experiments.pid_ff

Each script runs the simulation, saves the trajectory to outputs/arrays/, and writes a plot to outputs/plots/.

About

1-DOF arm control from first principles. NumPy plant + PID/feedforward controller, four experiments isolating each term, with derivation notes on steady-state error, discretization, anti-windup, and signal processing.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages