Holding still is harder than it looks
A small arm in a school in Accra is meant to lift its forearm from hanging slightly down to exactly horizontal, then hold it there while a student places a sensor in its gripper. The forearm is driven by a geared DC motor; the more voltage you apply, the more torque it makes. Surely you can work out the voltage that holds the arm level, apply it, and be done?
Try it and three things go wrong. The arm swings past horizontal on the way up, because it arrives with speed. It then settles a little low, because the torque you calculated was for a forearm whose weight you guessed, and the gripper's cable drags a little more than you thought. And when the student puts the sensor in the gripper, the arm sags further, because nothing in your plan knew the sensor was coming. Every fixed command is a prediction, and the world rarely matches the prediction exactly.
The cure is feedback: measure where the arm actually is, compare it with where it should be, and correct the command, over and over, many times a second. This lesson builds that loop for a simulated robot joint, starting with the oldest and still most widely used feedback law, PID. It shows what each of the three terms does, how engineers tune them, the details that separate a classroom formula from firmware that runs on a real robot, and how real robot joints stack three such loops inside each other.
The frequency-domain theory of feedback (transfer functions, Bode plots, gain and phase margins) belongs to Lumenary EE's feedback control and Laplace and stability lessons. Here we stay in the time domain, with a simulated motor and a clock, which is how a robot engineer meets the loop first.
Open loop and closed loop
An open-loop controller computes its command from the goal alone: "to turn the wheel at 2 rad/s, apply 4 V". A closed-loop controller also looks at the result: "the wheel is turning at 1.8 rad/s, which is too slow, so add some voltage". The difference is one wire, from the sensor back to the controller, and it changes everything.
In the diagram, is the reference (what we want), the output (what we get), the command the controller sends to the plant (the motor, gearbox and arm), a disturbance (a load, a slope, a sagging battery), and the error. In the open loop, goes straight to . In the closed loop, moves , which changes , which changes , which pushes back.
Analogy, not a formal construction
Steering a bicycle with your eyes shut is open-loop control: you can aim well at the start, but a gust of wind or a bump will take you off course and you will not know. With your eyes open you correct a little, constantly, without thinking. You are not better at predicting the wind; you are measuring its effect and undoing it.
Here is the difference in numbers. A small gearmotor turns a robot's wheel, and we want 2 rad/s. The open-loop controller applies the 4 V that gives 2 rad/s on the nominal motor. The closed-loop controllers add a correction proportional to the speed error (P) or also to its accumulated history (PI), both measured by an encoder 100 times a second. Then we change the world: a ramp, a tired battery, cold grease in the gearbox.
import numpy as np
# A small gearmotor turning a wheel. Output-shaft model: J*dw/dt = k*s*u - c*w - tau_load
J, k, c = 0.02, 0.25, 0.5 # kg m^2, N m per volt, N m s/rad
T = 0.01 # the controller runs at 100 Hz
def speed_after(controller, s=1.0, c_true=c, tau_load=0.0, seconds=3.0):
"""Simulate; s scales the voltage the driver really delivers (a sagging battery)."""
w, integral = 0.0, 0.0
for _ in range(int(seconds / T)):
u, integral = controller(w, integral)
u = min(max(u, -12.0), 12.0) # the driver cannot exceed its supply
for _ in range(10): # integrate the motor in 1 ms substeps
w += (T / 10) * (k * s * u - c_true * w - tau_load) / J
return w
w_ref = 2.0 # wanted: 2 rad/s
u_ff = w_ref * c / k # the voltage that gives 2 rad/s on the nominal motor: 4 V
open_loop = lambda w, i: (u_ff, i)
p_ctrl = lambda w, i: (u_ff + 10.0 * (w_ref - w), i)
def pi_ctrl(w, i):
e = w_ref - w
i += e * T
return u_ff + 10.0 * e + 40.0 * i, i
cases = [("nominal", {}), ("up a ramp: 0.3 N m load", {"tau_load": 0.3}),
("battery sagged to 85 %", {"s": 0.85}), ("cold grease: c +40 %", {"c_true": 0.7})]
print(f"{'case':26} {'open loop':>10} {'P':>8} {'PI':>8} (rad/s; wanted 2.000)")
for name, kw in cases:
print(f"{name:26} {speed_after(open_loop, **kw):10.3f} "
f"{speed_after(p_ctrl, **kw):8.3f} {speed_after(pi_ctrl, **kw):8.3f}")The open loop is perfect only when the world matches its model, and loses 15 to 30 per cent of the speed otherwise. Proportional feedback cuts each of those errors by a factor of between four and six, and the PI controller removes them completely.
The PID law
Definition— PID controller
A PID controller computes the command from the error as
with three non-negative gains: proportional , integral and derivative . Dropping terms gives the P, PI and PD controllers. An equivalent "standard" form writes , with integral time and derivative time .
Each term looks at the error at a different time. The P term reacts to the present error. The I term remembers the past: it keeps growing as long as any error remains. The D term looks at the trend: if the error is shrinking fast, it eases off the command before the output overshoots.
Definition— step response measures
After a step change in the reference, the overshoot is how far the output goes past the new reference, as a percentage of the step; the settling time is when the output enters, and stays inside, a band around the reference (here rad, or a percentage of the step); the steady-state error is what remains after everything has settled.
Definition— saturation
Real actuators have limits. A motor driver on a 24 V supply cannot apply 60 V however loudly the controller asks. The controller's output is saturated when it is clipped to such a limit; while it is, the loop is effectively open.
P, PI, PD and PID on a robot joint
Now the forearm from the opening. Take as its angle above horizontal and . With the motor's electrical dynamics neglected (its current settles within about a millisecond, far faster than the arm moves), a geared DC motor turns voltage into torque , loses to back-EMF and friction, and the link's weight pulls with torque (the gravity term of the dynamics lesson, for a link of mass whose centre of mass is a distance from the joint):
We use kg·m², N·m/V, N·m·s/rad and a 0.3 kg link with its centre 0.15 m out, so N·m. The supply is 24 V. The controller runs on a microcontroller at a fixed 100 Hz: every 10 ms it samples the encoder, computes, and applies the new voltage at the next tick, so there is one sample of delay, as in many real loops. The arm starts at rest rad below horizontal, , and is asked to go to .
import numpy as np
# One joint of a small arm moving in a vertical plane (theta = 0 is horizontal).
J, k, c = 0.02, 0.25, 0.5 # inertia, N m per volt, damping
TAU_G = 0.3 * 9.81 * 0.15 # gravity torque of the link when horizontal: 0.44 N m
U_MAX, T = 24.0, 0.01 # 24 V supply; the loop runs at 100 Hz
def joint_step(th, w, u):
"""Advance the joint by one control period with voltage u held (ten 1 ms substeps)."""
for _ in range(10):
w += 0.001 * (k * u - c * w - TAU_G * np.cos(th)) / J
th += 0.001 * w
return th, w
def step_response(kp, ki, kd, th0=-0.5, target=0.0, seconds=3.0):
th, w, integral, prev_e, u_next = th0, 0.0, 0.0, target - th0, 0.0
ths, us = [], []
for _ in range(int(seconds / T)):
u = u_next # voltage computed last tick is applied now
e = target - th # sample the encoder
integral += e * T
deriv = (e - prev_e) / T
prev_e = e
u_next = min(max(kp * e + ki * integral + kd * deriv, -U_MAX), U_MAX)
ths.append(th); us.append(u)
th, w = joint_step(th, w, u)
return np.array(ths), np.array(us)
print("step from -0.50 rad to 0 rad (horizontal); settled = within 0.01 rad for good")
print(f"{'controller':24} {'overshoot':>9} {'settled at':>10} {'final error':>11} {'peak volts':>10}")
for name, gains in [("P Kp=15", (15, 0, 0)), ("P Kp=40", (40, 0, 0)),
("PD Kp=40 Kd=0.8", (40, 0, 0.8)), ("PI Kp=15 Ki=30", (15, 30, 0)),
("PID Kp=40 Ki=40 Kd=0.8", (40, 40, 0.8))]:
th, u = step_response(*gains)
outside = np.nonzero(np.abs(th) > 0.01)[0]
settled = "never" if outside[-1] == len(th) - 1 else f"{(outside[-1] + 1) * T:.2f} s"
print(f"{name:24} {max(th.max(), 0) / 0.5 * 100:8.1f}% {settled:>10} "
f"{round(-th[-1], 3) + 0.0:11.3f} {np.abs(u).max():10.1f}")The table tells the story of the three terms.
P alone droops. With V/rad the arm stops rad (almost 7°) below horizontal. To hold the link up, the motor needs V, and a P controller only produces voltage when there is an error, so it must settle where V. Raising to 40 shrinks the droop to rad but makes the arm overshoot: more gain means more push, and the arm arrives with more speed.
D adds damping. The PD controller keeps and adds V·s/rad. As the arm rushes towards the target, the error falls fast, is large and negative, and the D term brakes. The overshoot disappears, but the droop does not: at rest and D contributes nothing.
I removes the droop. The PI controller settles exactly on target. While any error remains, the integral grows and so does the voltage, until the integral term alone supplies the 1.77 V that gravity needs.
PID does all three: fast, little overshoot, no droop, settled in a quarter of a second.
Faster responses also ask for more voltage: the PID run touches 20.5 V of the 24 available, and a larger step would hit the limit, with consequences we meet below.
Tuning by experiment and by rule
The gains above did not fall from the sky. There are three common ways to find them.
By experiment
The manual recipe that most engineers learn first, best done in simulation or on a small, slow, current-limited joint with the arm clear of people and a hand on the stop button:
- Start with and a small . Raise until the response is brisk with a little overshoot.
- Add until the overshoot is damped. Too much makes the motor buzz, because it amplifies sensor noise.
- Add until the steady-state error disappears in an acceptable time. Too much brings back overshoot and slow oscillation.
- Test with large steps, with loads, and at the limits of the motion range, not just the one small step you tuned on.
By rule: Ziegler and Nichols
In 1942 John Ziegler and Nathaniel Nichols, engineers at an instrument maker in Rochester, New York, proposed rules that turn a single experiment into gains. Use P control only and raise until the loop oscillates steadily, neither growing nor dying away. That gain is the ultimate gain , and the oscillation's period is the ultimate period . Then set
Pushing a real robot into sustained oscillation can shake loose bolts and wear gearboxes, so let us do it in simulation, with a small nudge so the motor never saturates.
import numpy as np
J, k, c = 0.02, 0.25, 0.5
TAU_G = 0.3 * 9.81 * 0.15
U_MAX, T = 24.0, 0.01
def joint_step(th, w, u):
for _ in range(10):
w += 0.001 * (k * u - c * w - TAU_G * np.cos(th)) / J
th += 0.001 * w
return th, w
def simulate(kp, ki, kd, th0, target, seconds, holding=True):
u_hold = TAU_G * np.cos(th0) / k if holding else 0.0 # start at rest, held at th0 by
integral = u_hold / ki if ki else 0.0 # the integrator, as after a long wait
th, w, prev_e, u_next = th0, 0.0, target - th0, u_hold
ths = []
for _ in range(int(seconds / T)):
u = u_next
e = target - th
integral += e * T
deriv = (e - prev_e) / T
prev_e = e
u_next = min(max(kp * e + ki * integral + kd * deriv, -U_MAX), U_MAX)
ths.append(th)
th, w = joint_step(th, w, u)
return np.array(ths)
# The experiment: P control only, a small 0.02 rad nudge, and watch whether the swing grows.
def growth(kp):
th = simulate(kp, 0, 0, -0.02, 0.0, 6.0, holding=False)
swing = lambda a, b: np.ptp(th[int(a / T):int(b / T)])
return swing(4.0, 6.0) / swing(2.0, 4.0)
lo, hi = 10.0, 400.0 # growth(lo) < 1 (dies out), growth(hi) > 1 (grows)
for _ in range(40):
mid = 0.5 * (lo + hi)
lo, hi = (mid, hi) if growth(mid) < 1 else (lo, mid)
Ku = 0.5 * (lo + hi)
th = simulate(Ku, 0, 0, -0.02, 0.0, 6.0, holding=False)
x = th[300:] - th[300:].mean()
ups = np.nonzero((x[:-1] < 0) & (x[1:] >= 0))[0] # upward zero crossings
Tu = (ups[-1] - ups[0]) * T / (len(ups) - 1)
print(f"ultimate gain Ku = {Ku:.0f} V/rad, oscillation period Tu = {Tu:.3f} s")
# Ziegler and Nichols' classic PID rule: Kp = 0.6 Ku, Ti = Tu/2, Td = Tu/8
kp, ki, kd = 0.6 * Ku, 0.6 * Ku / (Tu / 2), 0.6 * Ku * Tu / 8
print(f"Ziegler-Nichols gains: Kp = {kp:.0f}, Ki = {ki:.0f}, Kd = {kd:.2f}")
print(f"\n{'a step of 0.05 rad':22} {'overshoot':>9} {'within 5 %':>10}")
for name, g in [("Ziegler-Nichols", (kp, ki, kd)), ("hand-tuned 40/40/0.8", (40, 40, 0.8))]:
th = simulate(*g, -0.05, 0.0, 3.0)
outside = np.nonzero(np.abs(th) > 0.0025)[0]
print(f"{name:22} {max(th.max(), 0) / 0.05 * 100:8.0f}% {(outside[-1] + 1) * T:8.2f} s")The experiment finds V/rad and s, and the rule gives , , . The result is fast, settling sooner than our hand-tuned gains, but it overshoots by 74 per cent. That is not a bug in the simulation. Ziegler and Nichols tuned process plants (temperatures, pressures, flows), aiming for a response in which each swing is about a quarter of the one before. For a robot arm that is far too lively. Treat the rule as a starting point: it tells you the scale of the gains in one experiment, and then you detune, typically lowering a lot and somewhat.
Two refinements are worth knowing. Karl Johan Åström and Tore Hägglund's relay method (1984) lets a relay switch the command between two small values; the loop settles into a small, controlled oscillation whose amplitude and period give and without pushing the loop to the edge of instability. Many "autotune" buttons use it. And modern rules such as Sigurd Skogestad's SIMC rules start from a simple fitted model (a gain, a time constant and a delay) and give gentler, more robust settings.
By model
When you know the model, you can choose the closed-loop behaviour directly. Linearised near horizontal, the joint is with s⁻¹ and rad/s² per volt. PD control turns it into , a mass–spring–damper whose natural frequency is and damping ratio . Our , give rad/s and : a quick response with almost no overshoot, which is what the PD row showed. Turning that idea into a systematic design method for many states at once is the subject of the next lesson; the frequency-domain view of the same choices is in Lumenary EE's feedback control.
From formula to firmware: windup, kick and noise
The PID formula is three lines. The PID loops inside real robots are a few dozen, and every extra line fixes a problem that the formula ignores.
A fixed rate
A digital controller runs on a clock. Every period it reads the sensor, computes, and writes the actuator. In discrete time the integral becomes a running sum and the derivative a difference:
The rate matters: the discrete and are only right for the they assume, and an irregular period (a loop run "as fast as it can" by an operating system that sometimes pauses it) acts like noise injected into the integral and derivative. Real-time loops run from a hardware timer or a real-time scheduler, and good code logs the actual period.
Integral windup
When the output saturates, the plant receives less than the controller asks for, the error stays large for longer, and the integral keeps accumulating. When the output finally reaches the target, the integral holds a large stored value, which drives the output far past the target before it unwinds. This is integral windup, and it is a classic surprise: the loop tuned perfectly on small steps overshoots badly on a big one.
The simplest fix, conditional integration or clamping, stops integrating while the output is saturated and the error would push it further into saturation. Another, back-calculation, feeds the difference between the requested and the actual output back into the integrator, bleeding it off at a chosen rate.
Derivative kick and noise
If the reference jumps, so does the error, and is enormous for one sample: a derivative kick that slams the actuator. Since and is zero except at the jumps, the standard fix is to differentiate the measurement only, using instead of . The response to disturbances is unchanged.
The derivative also amplifies noise. A sensor with noise of standard deviation , differenced over one period, gives derivative noise of about , which grows as the loop gets faster. So the derivative is always low-pass filtered, typically with a time constant of a fraction of (a common choice is with between 5 and 20), but never shorter than a sample or two.
The block below implements all three fixes, each switchable, and tests them on the joint.
import numpy as np
J, k, c = 0.02, 0.25, 0.5
TAU_G = 0.3 * 9.81 * 0.15
U_MAX, T = 24.0, 0.01
def joint_step(th, w, u):
for _ in range(10):
w += 0.001 * (k * u - c * w - TAU_G * np.cos(th)) / J
th += 0.001 * w
return th, w
class PID:
"""A PID loop called once per tick (every T seconds). Options switch on the three fixes."""
def __init__(self, kp, ki, kd, anti_windup=False, d_on_measurement=False, tf=0.0):
self.kp, self.ki, self.kd, self.tf = kp, ki, kd, tf
self.anti_windup, self.d_on_measurement = anti_windup, d_on_measurement
self.integral, self.prev, self.d = 0.0, None, 0.0
def update(self, target, y):
e = target - y
x = -y if self.d_on_measurement else e # differentiate -y or e
if self.prev is not None:
raw = (x - self.prev) / T
self.d += T / (self.tf + T) * (raw - self.d) # first-order filter (none if tf = 0)
self.prev = x
u = self.kp * e + self.ki * self.integral + self.kd * self.d
u_sat = min(max(u, -U_MAX), U_MAX)
if not self.anti_windup or u == u_sat or e * u < 0:
self.integral += e * T # clamping: freeze while pushing the limit
return u_sat
def run(pid, th0, target, t_step=0.5, seconds=4.0, noise=0.0):
"""Hold th0 until t_step, then jump the target. The sensor adds Gaussian noise (rad rms)."""
rng = np.random.default_rng(7)
pid.integral = TAU_G * np.cos(th0) / k / pid.ki # already holding th0 at rest
th, w, u_next, ths, us = th0, 0.0, TAU_G * np.cos(th0) / k, [], []
for i in range(int(seconds / T)):
u = u_next
y = th + noise * rng.standard_normal()
u_next = pid.update(th0 if i * T < t_step else target, y)
ths.append(th); us.append(u)
th, w = joint_step(th, w, u)
return np.array(ths), np.array(us)
gains = (40, 40, 0.8)
print("A big move, -0.5 -> 1.0 rad: the voltage saturates at 24 V for a while")
for name, opts in [("no anti-windup", {}), ("with anti-windup", {"anti_windup": True})]:
th, u = run(PID(*gains, **opts), -0.5, 1.0)
late = np.nonzero(np.abs(th - 1.0) > 0.01)[0][-1] + 1
print(f" {name:17} overshoot {(th.max() - 1.0) / 1.5 * 100:5.1f}% settled {late * T - 0.5:.2f} s after the step"
f" saturated for {np.sum(np.abs(u) >= U_MAX) * T:.2f} s")
print("A small step, 0 -> 0.05 rad, with 1 mrad of sensor noise")
for name, opts in [("D on error", {}), ("D on measurement", {"d_on_measurement": True}),
(" + 30 ms filter", {"d_on_measurement": True, "tf": 0.03})]:
th, u = run(PID(*gains, anti_windup=True, **opts), 0.0, 0.05, noise=0.001)
kick = np.abs(np.diff(u[45:60])).max() # around the step at t = 0.5 s
print(f" {name:17} biggest jump {kick:5.2f} V noise in u after settling {np.std(u[-100:]):.3f} V rms")Read the output line by line. Saturated for only an eighth of a second, the integrator without anti-windup still stores enough to nearly triple the overshoot (16.1 % against 5.9 %) and to double the settling time. With the derivative on the error, the 0.05 rad step produces a one-tick jump of 6.35 V, of which about 4 V is the kick (); differentiating the measurement removes it. And the 30 ms filter halves the voltage noise that the sensor's 1 mrad of noise causes, at the cost of a little phase lag.
On a microcontroller the same loop is written in C and called from a hardware timer interrupt. The sketch below is for illustration and is not run here; real firmware adds fault checks (encoder lost, over-current, over-temperature) that stop the motor safely.
/* Not run here: a 1 kHz position PID for a microcontroller, called by a timer interrupt. */
static float integral = 0.0f, d_filt = 0.0f, y_prev = 0.0f;
void control_tick(void) /* called every T seconds by a timer */
{
const float T = 0.001f, KP = 40.0f, KI = 40.0f, KD = 0.8f, TF = 0.005f, UMAX = 24.0f;
float y = encoder_angle_rad(); /* read the sensor first, at a fixed time */
float e = target_rad - y;
d_filt += T / (TF + T) * (-(y - y_prev) / T - d_filt); /* filtered -dy/dt */
y_prev = y;
float u = KP * e + KI * integral + KD * d_filt;
float u_sat = u > UMAX ? UMAX : (u < -UMAX ? -UMAX : u);
if (u == u_sat || e * u < 0.0f) /* clamping anti-windup */
integral += e * T;
motor_set_volts(u_sat);
}Loops inside loops: how a real joint is controlled
The joint above uses one loop from position error straight to voltage. Real robot joints, from collaborative arms to quadruped legs, almost never do. Instead they cascade three loops, each feeding the reference of the one inside it.
- The current loop is innermost. Motor torque is proportional to current, so a fast current loop turns the motor into a clean torque source: it cancels the effect of the winding's inductance and resistance, of back-EMF and of supply voltage changes. For brushless motors this loop is field-oriented control (FOC), which transforms the three phase currents into a rotating frame where torque-producing current is a single number that a PI loop can regulate. It runs at the PWM rate of the power stage, typically 10 to 40 kHz. Lumenary EE's magnetism and motors and power electronics explain the motor and the switching stage.
- The velocity loop, usually PI, asks for current to reach a commanded speed. It handles friction and load changes before they become position errors.
- The position loop, often just P or PD because the velocity loop already has integral action, asks for speed.
Why bother? Each loop deals with the fast disturbances at its own level, so the outer loops see a simpler, better-behaved plant: to the velocity loop, a motor with a fast current loop looks like a pure inertia driven by torque. Each loop can also be limited separately, so the current limit protects the motor and gearbox no matter what the outer loops ask for. And the loops can be tuned one at a time, from the inside out.
The rule that makes it work: each inner loop must be considerably faster than the loop around it, typically by a factor of 5 to 10 in bandwidth. Then the outer loop sees the inner one as nearly instantaneous. If they are similar in speed, they interact and the cascade can oscillate even though each loop is stable on its own.
A cascade also builds some knowledge of the plant into the controller without writing a model down: the velocity loop works out, by measurement, the torque that a requested speed takes. The next lessons make that knowledge explicit: state feedback uses position and velocity together, and computed-torque control writes the arm's dynamics into the command.
In a robot arm, the drive at each joint runs the current and often the velocity and position loops, and the robot's central controller sends position, velocity or torque set-points to every joint at a fixed rate, commonly 0.5 to 1 kHz, over a real-time fieldbus such as EtherCAT. Open-source drives used in research robots, such as moteus and ODrive, expose all three loops and let the robot's computer send a position, a velocity and a feedforward torque together: the drive turns them into a PD loop with torque feedforward, computed at high rate, while the computer updates the set-points more slowly.
PID in industry and research
PID is a century old and still everywhere. Surveys of process plants have long found that the great majority of their thousands of loops are PI or PID. In robotics:
- Robot joints and drives use the cascade above. Industrial servo drives can identify the load inertia and set their own gains, often adding notch filters so the loops do not excite the arm's structural resonances.
- Drones. The open-source PX4 and ArduPilot autopilots control multicopters with a cascade: position, velocity, attitude, and an inner angular-rate loop running at hundreds of hertz to a few kilohertz, with PID at the rate level.
- Robot software frameworks. In ROS 2, ros2_control provides PID controllers and a joint-trajectory controller that adds PID feedback around a planned trajectory.
What PID cannot do well is also clear. It treats each output separately, so a strongly coupled system (a fast two-link arm, a balancing robot) is hard to tune joint by joint; it has no notion of constraints except by clipping; and it does not use a model, so it reacts to errors instead of anticipating them. The rest of this level addresses exactly these limits: state feedback and LQR for coupled systems, feedforward and computed torque for anticipation, impedance control for contact, and model predictive control for constraints.
A short history
Feedback control is older than electronics: float valves regulated the water level in ancient water clocks. The centrifugal governor that Boulton and Watt fitted to steam engines from 1788 opened the throttle as the engine slowed and closed it as it sped up, proportional control in brass. Governors sometimes "hunted", oscillating instead of settling, and in 1868 James Clerk Maxwell explained why in On Governors, the first mathematical study of a feedback loop's stability.
In 1922 Nicolas Minorsky, working on automatic ship steering for the United States Navy, watched experienced helmsmen and noticed that they responded to the heading error, to how long it had persisted and to how fast it was changing. His analysis of that three-term law is the origin of PID. Pneumatic PID controllers spread through chemical plants in the 1930s, and the same law later moved into electronics, then microprocessors, then the drive of every robot joint.
Common misconceptions
Misconception
"More gain is always better." More gain reduces steady-state error and speeds up the response, up to a point. Beyond it, the loop overshoots, saturates, amplifies sensor noise and finally oscillates; on a real robot, unmodelled delay and flexibility set that limit much lower than a tidy model suggests.
Misconception
"The integral term makes a controller accurate, so make large." The integral removes constant errors, slowly and with a cost: a large adds overshoot, slow oscillation and windup. Much of a constant load can instead be predicted and fed forward (gravity, for example), leaving the integral only the small, unknown remainder.
Misconception
"The D term predicts the future, so always use it." It extrapolates the present slope, which works only if the measurement is clean. Many loops are PI because their sensor is too noisy for a useful derivative, or because the plant (like a motor's current) does not need one. When velocity is measured directly, by a tachometer, a gyroscope or the drive's own estimate, use it instead of differentiating position.
Misconception
"Gains tuned in simulation will work on the robot." A simulation contains only what you put in it. Real joints have friction that sticks and slips, gearbox backlash, flexible links, cable forces, sensor delays and temperature drift. Simulation gives a good starting point; the final tuning happens, carefully, on the machine.
Exercises
1. Feedback and PID control
Warm-upHumanA robot's shoulder joint, under P control, settles below the commanded angle every time it holds its arm out horizontally. Which change removes that steady error, whatever the arm's exact weight?
2. Feedback and PID control
CoreHumanA joint is held by a P controller with V/rad. Its motor and gearbox produce N·m of torque per volt at standstill, and a constant load torque of N·m pulls on it. How far, in milliradians, does the joint settle from its target?
3. Feedback and PID control
CoreHumanIn a simulated relay test, a wheel's velocity loop under P control first oscillates steadily at (in volts per rad/s) with period s. Using the classic Ziegler–Nichols PID rule (, , ), what is the integral gain in the parallel form ?
4. Feedback and PID control
CoreHumanEvery time a robot's planner sends a new joint target, the motor current spikes for one control period and the gearbox clunks. The PID loop computes . Which change removes the spike without slowing the response to disturbances?
5. Feedback and PID control
CoreComputationalA wheel motor (the one from this lesson: , , , a 0.3 N·m load, a 12 V supply) is controlled by SpeedPI(kp, ki, T, u_max), whose update(w_ref, w) is called every s and returns the voltage.
The starter is the textbook PI. Make it production-ready: limit the output to u_max, and add anti-windup by clamping, so that the integral does not grow while the output is saturated and the error would push it further. The tests run the wheel up to 5 rad/s from rest, then block it completely for one second (it has hit a door threshold) and release it: with windup, the wheel shoots off at full speed for many seconds.
6. Feedback and PID control
StretchComputationalMany drives and PLCs implement PID in velocity (incremental) form: instead of computing outright, each call computes the change
and sets , with and .
Complete IncrementalPID.update(e). The tests check that, without limits, it gives exactly the same outputs as the positional PID , and that with limits it leaves saturation immediately when the error changes sign.
7. Feedback and PID control
CoreHumanA robot joint uses cascaded position, velocity and current loops. A new engineer proposes running all three at the same 1 kHz rate with similar bandwidths, "to keep things simple". What is the main problem?
8. Feedback and PID control
Warm-upHumanA PID-controlled joint behaves well on small steps but overshoots badly after a large move during which its motor voltage sat at the supply limit for half a second. What is this effect called (two words)?
Further reading and resources
- Feedback Systems: An Introduction for Scientists and Engineers · Karl Johan Åström and Richard M. Murray · 2021A free textbook; its PID chapter covers windup, derivative filtering and tuning rules in more depth than this lesson.
- On Governors · James Clerk Maxwell · 1868The first mathematical analysis of a feedback controller's stability, prompted by steam-engine governors that hunted.
- Directional stability of automatically steered bodies · Nicolas Minorsky · 1922The ship-steering paper in which the three-term (PID) law was first worked out from watching helmsmen. Journal of the American Society of Naval Engineers.
- Optimum settings for automatic controllers · John G. Ziegler and Nathaniel B. Nichols · 1942The tuning rules still taught today, from engineers at an instrument company. Transactions of the ASME 64.
- Advanced PID Control · Karl Johan Åström and Tore Hägglund · 2006The standard professional reference on PID structures, anti-windup and automatic tuning, including the relay method.
- ros2_controlThe ROS 2 framework for robot controllers, whose joint-trajectory and PID controllers run loops like the ones in this lesson.
- PX4 autopilot documentationAn open-source flight stack whose multicopter controller is a cascade of position, velocity, attitude and rate loops.
- moteus brushless controllerOpen-source firmware and hardware for a robot joint drive, with its current, velocity and position loops in plain C++.