devol.dev

How to Tune a PID Controller by Hand

PID tuning is not about finding the “correct” gains. It is about choosing the best tradeoffs for your mechanism.

The equations describe how a controller behaves, but they cannot choose the gains for you. Those depend on the inertia, friction, actuator limits, and load of the real system.

This simulator models a single revolute joint moving to a new position. Adjust the gains and watch how the controller responds.

Joint servo / single axis

Tuning a PID by hand

One joint, one step command, three gains. Drag the setpoint line and pull on the sliders. The lower lane is the torque the controller is actually asking for, which is where most tuning arguments are really settled.

Overshoot
Rise 10-90
Settle 2%
SS error

The top plot shows joint position. The bottom plot shows commanded torque.

Watch both.

A smooth position trace can hide a controller that spends most of its time at its torque limit. If the torque is saturated, increasing the gains often has little effect.

Manual PID tuning

A simple tuning procedure works well for many systems.

  1. Set Ki = 0 and Kd = 0.
  2. Increase Kp until the response becomes fast but begins to oscillate.
  3. Increase Kd until the oscillation is well damped.
  4. Add just enough Ki to remove steady-state error.
  5. Verify the controller no longer saturates and remains stable under load.

The sections below explain why this works.


Proportional gain (Kp)

Start with proportional control alone.

The controller behaves like a virtual spring. The farther the joint is from the target, the harder it pushes back.

Increasing Kp makes the response:

  • Faster
  • Stiffer
  • More prone to overshoot

Ignoring gravity, the closed-loop dynamics are

Jθ¨+(b+Kd)θ˙+Kpθ=KpθrefJ\ddot{\theta} + (b + K_d)\dot{\theta} + K_p\theta = K_p\theta_{ref}

with natural frequency

ωn=KpJ\omega_n=\sqrt{\frac{K_p}{J}}

and damping ratio

ζ=b+Kd2JKp.\zeta=\frac{b+K_d}{2\sqrt{JK_p}}.

As Kp increases, the joint responds more quickly, but the damping ratio falls. Faster responses usually come with more overshoot.

Enable gravity.

The joint now settles below the commanded angle because proportional control must maintain an error to generate holding torque.

The steady-state condition is

Kpe=Gcos(θ).K_p e = G\cos(\theta).

Increasing Kp reduces this error, but never eliminates it.

Takeaway

Increase Kp until the response is fast, then stop before overshoot becomes excessive.


Derivative gain (Kd)

Derivative control resists motion.

It adds damping without increasing stiffness, allowing larger values of Kp without excessive oscillation.

This implementation differentiates the measurement, not the error.

Differentiating the error produces a large spike whenever the setpoint changes. Differentiating the measurement removes this derivative kick while preserving normal damping.

Derivative control does not remove steady-state error.

With gravity enabled, increasing Kd leaves the final position unchanged.

Takeaway

Increase Kd until the response settles quickly with minimal overshoot.


Integral gain (Ki)

Integral control accumulates error over time.

As long as an error remains, the integrator continues increasing the commanded torque until the error disappears.

This is the only PID term capable of eliminating steady-state error.

The closed-loop characteristic equation becomes

Js3+(b+Kd)s2+Kps+Ki=0.Js^3+(b+K_d)s^2+K_ps+K_i=0.

Applying the Routh-Hurwitz criterion gives the stability condition

Ki<(b+Kd)KpJ.K_i < \frac{(b+K_d)K_p}{J}.

Integral gain therefore has an upper limit.

Increasing Kd allows a larger stable value of Ki, which is why adding a small amount of derivative often makes integral control easier to tune.

Takeaway

Add only enough Ki to remove steady-state error.


Torque saturation

Enable the torque limit and increase Kp.

Eventually the torque trace reaches the limit and flattens.

Beyond this point, increasing Kp has little effect because the actuator, not the controller, determines the rise time.

If your controller spends most of its time saturated, tune the mechanism before tuning the gains.


Integral windup

Enable the torque limit.

Add some integral gain.

Now disable anti-windup.

While the actuator is saturated, the integrator continues accumulating error even though the motor cannot produce additional torque.

Once the joint reaches the target, the stored integral command must unwind before the controller can settle. The result is large overshoot.

The anti-windup implementation in this simulator pauses integration whenever the controller is saturated and the integrator would push farther into saturation.

Integral windup is one of the most common causes of poor real-world PID performance.


Reading the P, I, and D contributions

Enable Split P / I / D.

Notice how each term contributes differently.

  • P reacts immediately and then decays.
  • D opposes rapid motion and quickly returns to zero.
  • I grows slowly and eventually supplies the torque required to hold the load.

This division of labor is exactly what the controller is designed to achieve.


Explore further

Try these experiments.

  • Increase Kp until oscillation begins.
  • Add Kd and watch the oscillation disappear.
  • Enable gravity and observe the proportional droop.
  • Add Ki until the droop disappears.
  • Enable torque saturation and observe integral windup.
  • Toggle anti-windup and compare the responses.

Once you can predict what each slider will do before moving it, you have developed intuition that transfers directly to real hardware.

If you are interested in the implementation, the simulator separates the controller, plant, and rendering. The control model has no rendering dependencies and is tested independently, following the same approach used in the inverse kinematics article.