Control Systems & PID

PID control is the most widely used algorithm in all of engineering. Your home thermostat, your car's cruise control, the motor in a drone, the arm of an industrial robot — all of them use PID (or a variant). It's simple enough to understand in an afternoon and powerful enough to stabilize everything from a quadcopter to a nuclear reactor. Let's build a genuine intuition for it.

The control problem

You have a system — say, a motor driving a robot wheel. You want the wheel to spin at exactly 1.5 m/s. The actual speed might be 1.2 m/s (too slow) or 1.7 m/s (too fast). The error is the difference: desired - actual. Your job is to compute a motor command that drives this error to zero and keeps it there, despite disturbances (different floor friction, inclines, battery voltage changes).

This is the control problem — and PID is the most common solution.

PID — Three Simple Ideas

PID output = P term + I term + D term. Each addresses a different aspect of the error.

P — Proportional: "How far off am I right now?"

The P term multiplies the current error by a gain Kp. If you're way off (large error), apply a big correction. If you're close (small error), apply a small correction. Simple and intuitive. But P-only control often leaves a steady-state error — it can get close to the target but never quite reach it, because once the error is small, the correction is too weak.

I — Integral: "How long have I been off?"

The I term accumulates the error over time and multiplies by Ki. If there's a small persistent error that the P term can't eliminate (say, a steady uphill slope), the I term grows until it provides enough correction. It "integrates out" steady-state errors. Too much I makes the system oscillate — the accumulated correction overshoots the target.

D — Derivative: "How fast is the error changing?"

The D term multiplies the rate of change of error by Kd. It's like predictive damping — if the error is decreasing quickly (you're already converging on the target), the D term reduces the correction to prevent overshoot. Think of it as "applying the brakes" as you approach the target. It improves stability, especially in systems with inertia.

Putting it together

The controller runs in a loop: read the current sensor value, compute error = setpoint - actual, compute output = Kp×error + Ki×∫error dt + Kd×(d error/dt), send output to the actuator, repeat. Loop rates typically range from 100Hz (motor control) to 1000Hz (drone attitude control).

Tuning PID — The Art and Science

The three gains (Kp, Ki, Kd) need to be tuned for each specific system. Wrong gains cause oscillation, instability, or sluggish response.

The Ziegler-Nichols method

A classic tuning recipe: set Ki = 0, Kd = 0. Increase Kp until the system oscillates at a steady amplitude — call this the "ultimate gain" Ku, and the oscillation period Tu. Then set Kp = 0.6×Ku, Ki = 1.2×Ku/Tu, Kd = 0.075×Ku×Tu. This gives a good starting point, usually requiring some manual fine-tuning.

Manual tuning guidelines

Start with Kp: increase until you get close to the target but start seeing oscillation, then back off 20%. Add Kd: increase until oscillation is damped without sluggishness. Add Ki last: increase slowly until steady-state error disappears. Test with step changes and disturbances.

Common problems and fixes

Steady-state error: Increase Ki.
Overshoot: Increase Kd or decrease Kp.
Oscillation: Decrease Kp or increase Kd.
Integrator windup: When the actuator saturates (can't go any faster), the integral keeps accumulating error. Fix with "anti-windup" — clamp the integral term when the actuator is saturated.

PID in Robotics

Motor speed control

Every robot wheel motor runs a PID loop: measure RPM from encoder, compute error vs. desired RPM, output PWM duty cycle to the H-bridge. Runs at 100–500Hz. This is the most common PID application in mobile robotics.

Robot arm joint control

Each joint in a robot arm runs its own PID loop controlling position (angle from encoder) and sometimes velocity and torque. Industrial robot controllers run cascaded PID: an outer position loop feeds setpoints to an inner velocity loop, which feeds setpoints to an innermost current/torque loop. Three PIDs in series for one joint.

Drone attitude control

A quadcopter runs three simultaneous PID loops: roll, pitch, and yaw. Each loop reads IMU data at 1000Hz and outputs motor speed adjustments. Without PID, a drone is uncontrollable — it falls over within milliseconds. PID is what makes a drone hover stably at the push of a button.

Line following

A classic beginner robot project: use an IR sensor array to detect a line. The error is how far left/right of center the line is. PID output adjusts the differential speed between left and right wheels. A pure P controller weaves; adding D smooths it out. A perfect demonstration of PID principles with simple hardware.

Frequently Asked Questions

Is PID ever not enough?

Yes — for highly nonlinear systems, systems with significant time delay, or systems where model knowledge should be exploited. Alternatives: Model Predictive Control (MPC, uses a system model to plan ahead), adaptive control (adjusts gains online), and learning-based control (neural networks replace or augment the PID). But PID is the first thing to try, and it works in 80% of cases.

How do I implement PID in Python?

It's only a few lines: track the previous error and the accumulated integral. Each iteration: error = setpoint - measurement; integral += error * dt; derivative = (error - prev_error) / dt; output = Kp*error + Ki*integral + Kd*derivative; prev_error = error. The simple-pid Python library provides a clean, tested implementation with anti-windup built in.

What is a cascaded PID controller?

A cascade puts multiple PID loops in series. The outer loop's output becomes the setpoint for the inner loop. Example: an outer position loop computes a desired velocity, which becomes the setpoint for an inner velocity loop, which computes a desired torque/current. Cascaded control is more responsive and handles actuator limitations better than a single loop for stiff systems.

Does reinforcement learning replace PID in modern robots?

Sometimes, but mostly they're complementary. RL-trained policies often run at the high level (what behavior to exhibit), while PID still runs the low-level joint control underneath. Very fast, safety-critical inner loops (drone attitude at 1kHz) remain PID-controlled even when the outer navigation layer uses deep RL. RL is better at high-level sequential decisions; PID is better at fast, stable, interpretable low-level regulation.

Frequently Asked Questions

What will I learn here?

This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.

How should I use this page?

Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.

What should I read next?

Use the navigation below to continue to the next lesson or explore related topics.