What is PID?

PID — Proportional Integral Derivative — is the fundamental closed-loop process control algorithm. It has been used in industrial control systems for decades and remains the bedrock from which all motor and process control springs.

For the Linux PC Robot, PID is used to continuously adjust motor power so each wheel turns at exactly the commanded speed. Without PID, the robot would drift, stall, or oscillate wildly in response to friction, load changes, and the imprecise scheduling of a general-purpose OS.

The Core Formula

The power output at each loop iteration is calculated as:

PO = (((E * E_GAIN) + (I * I_GAIN) + (D * D_GAIN)) * S) + BIAS Where: PO = Calculated power output E = Current positional error (target − actual) I = Accumulated integral of error (I += E each cycle) D = Differential of error (D = E − previous E) S = Time scale factor (elapsed / nominal period) BIAS = Output offset to account for motor driver zero-point *_GAIN = Tuning gains for each term

The Three Components

Proportional (P)

The proportional term applies a force directly proportional to the current error. If the motor is far behind its target, it gets more power. If it is close, it gets less. A purely proportional system will generally settle close to the target but may never reach it exactly — there is always a small residual error where the applied force just equals the opposing friction or load.

Integral (I)

The integral term accumulates error over time. Even if the current error is small, a long history of being slightly behind will build up in the integral and push the output higher until the error is truly corrected. It is particularly important for two-wheeled robots: if one wheel falls even slightly behind, the robot turns. The integral term catches and corrects this over time.

Derivative (D)

The derivative term responds to the rate of change of error. If the error is shrinking rapidly, the derivative term reduces the applied force to prevent overshoot. If the error is growing, it amplifies the response to recover faster. It acts as a damper on the system.

Timing on a General-Purpose OS

Linux is not a real-time operating system. The scheduler may delay the PID loop by unpredictable amounts — from microseconds to many milliseconds. A naive implementation that assumes fixed loop timing will malfunction under this jitter.

The LPCR implementation solves this by measuring exact elapsed time using hardware timer registers (available on Pentium and later x86 CPUs). The elapsed time is converted to a scale factor relative to the nominal loop period. This scale is multiplied into the PID output, so a loop that ran for 1.5× its expected interval automatically applies 1.5× the calculated correction — keeping motor distance tracking accurate regardless of OS scheduling jitter.

Read the Full Article

The complete technical article covers closed-loop theory, servo motor selection, the full C++ implementation of PIDControl, Servo, and MotorControl classes, a line-by-line walkthrough of the PIDLoop function, and practical advice on tuning and safety.

→ Read the full PID Servomotor Control article

Source code is available at Mohawksoft.Org.