Feedback and PID control
Build a feedback loop and choose proportional, integral, and derivative actions from observed error behavior.
- Trace reference, measurement, error, controller output, plant, and disturbance through a closed loop.
- Calculate a discrete PID update and explain the distinct role and risk of each term.
Related study — optional context
Understand
The working idea
Feedback compares what you want with what you measure and uses the difference to act. A toy car that adjusts steering from camera position can reject a side push; a fixed steering command cannot. Define reference r, measured output y_m, and error e = r - y_m. The controller produces u, the plant responds, and the sensor closes the loop. The sign matters: an action intended to reduce positive error must not increase it.
A continuous PID law is u(t) = K_p e(t) + K_i ∫e(t)dt + K_d de/dt. Proportional action reacts to present error. Integral action accumulates persistent error and can remove offset. Derivative action responds to change and can add damping, but it amplifies measurement noise and is often applied to measured output rather than a suddenly changing reference. In sampled code, one simple form uses I_k = I_{k-1} + e_kΔt and D_k = (e_k - e_{k-1})/Δt.
Actuators saturate. A PWM command cannot exceed its allowed range, and a motor driver cannot supply infinite voltage or current. If integration continues during saturation, the stored integral “winds up,” causing prolonged overshoot after the error changes sign. Clamp or condition integration, and record saturation explicitly. A specific build decision for a geared desk robot is to set output limits from driver and motor constraints before increasing K_i.
A common misconception is that PID terms are three magic improvements that should all be increased. More K_p can speed response but also magnify noise or excite oscillation; more K_i can correct bias but erode stability; K_d can become a noise command. Another failure is testing with the mechanism connected before confirming feedback direction. First command a small bounded output, observe sensor sign, and verify that the computed correction opposes displacement. PID does not compensate for an encoder that intermittently disconnects or a mechanically jammed axis.
Follow the reasoning
Worked example
A sampled PI controller has K_p = 0.8 output/degree, K_i = 0.5 output/(degree·s), Δt = 0.10 s, prior integral I = 2.0 degree·s, reference 30°, and measurement 26°. Find the unconstrained output.
- 1
Error is e = 30° - 26° = 4°.
- 2
Update integral: I = 2.0 + 4 × 0.10 = 2.4 degree·s.
- 3
Proportional contribution is 0.8 × 4 = 3.2 output units; integral contribution is 0.5 × 2.4 = 1.2.
- 4
Total unconstrained output is u = 3.2 + 1.2 = 4.4 output units.
The PI update commands 4.4 output units before saturation or direction mapping.
Sanity checkPositive reference error gives positive corrective output under the declared sign convention; reversing sensor polarity would make that dangerous.
Try it yourself
Independent check
Move to the bench
Implement and diagnose a bounded position loop on a safe low-energy mechanism.
- Low-voltage servo-like axis or motor with position sensor
- Current-limited supply
- Microcontroller
- Serial plotter
- Physical travel stops
Experiment
- With motor power disabled, move the axis by hand and confirm sensor direction and travel limits.
- Enable a small proportional-only output limit, apply a modest setpoint step, and record error, command, and saturation state.
- Add limited integral action only if a repeatable offset remains; compare settling and overshoot.
- Deliberate fault: invert the sensor sign in a software test while output remains tightly limited; identify growing error and disable immediately, then restore negative feedback.
Observe
- Expected: proportional control reduces error, and careful integral action reduces steady offset but may increase overshoot.
- Measure: actual reference, sensor value, command, sample time, and saturation; never substitute expected stability for a recorded trace.
Specify output limits and anti-windup behavior for a camera pan axis that may hit a cable-imposed travel limit.
- Use a low-energy axis, current limiting, clear stops, and an immediate power disconnect; never test first on an appliance or road vehicle.
Keep evidence
Bench note
Record what you expected, what you actually measured or observed, and one difference worth investigating. This is self-reported evidence, not a certification.