Curvo Studio

Servo motor PID position control: encoder feedback, P, I and D terms and tuning

A servo motor is a motor combined with a position sensor and a controller that compares the commanded and measured position and drives the motor to remove the difference. The most common controller is PID: the proportional term reacts to the present error, the integral term removes lasting error, and the derivative term damps the motion to limit overshoot.

What closed-loop control means

In an open-loop system, such as a stepper motor, the controller sends a command and assumes it was carried out. In a closed-loop system, the controller measures the result and corrects it.

A servo loop has four parts:

  • The command (setpoint): where you want the shaft to be, or how fast.
  • The sensor: an encoder, resolver or potentiometer that measures the actual shaft position.
  • The controller: it computes the error, e = setpoint − measured position, and produces a control output.
  • The drive and motor: they turn the controller output into current, and the current into torque.

Whenever a load disturbs the shaft, the error becomes non-zero, the controller pushes back, and the position returns. The price is that a badly tuned loop can oscillate or become unstable, which open-loop systems cannot do.

Encoder feedback and resolution

An incremental encoder produces pulses as the shaft turns, a quadrature pair of channels so the direction is known. An absolute encoder reports the actual angle, even after power-up. The resolution is quoted in counts per revolution, often as a number of bits: an encoder with n bits has 2^n counts per revolution.

A 17-bit encoder has 131,072 counts per revolution, so one count is 360° / 131,072 ≈ 0.0027°. The loop cannot position better than its sensor resolves, and coarse resolution also makes the derivative term noisy, because speed is calculated from position differences.

The P, I and D terms

The PID controller output is

u = Kp × e + Ki × ∫e dt + Kd × de/dt

Each term has a distinct role.

  • Proportional (Kp): the output is proportional to the present error. A higher Kp makes the response faster and the system stiffer against disturbances, but too high a value causes overshoot and oscillation. With P alone there is usually a steady-state error when a constant load torque acts.
  • Integral (Ki): accumulates the error over time, so even a small remaining error eventually pushes the output hard enough to eliminate it. It removes steady-state error but adds phase lag, which increases overshoot, and it can wind up when the actuator saturates, so the integral is normally clamped.
  • Derivative (Kd): reacts to the rate of change of the error. It acts like a damper, reducing overshoot and ringing, but it amplifies sensor noise and quantization, so it is usually filtered.

Effects of increasing each gain on its own, in rough terms: Kp shortens rise time and increases overshoot; Ki removes steady-state error and increases overshoot; Kd reduces overshoot and settling time, but can add noise.

Tuning intuition and a numerical picture

A common manual procedure is:

1. Set Ki and Kd to zero. Increase Kp until the axis reacts quickly but starts to overshoot or oscillate, then back off to about half of that value. 2. Add Kd until the overshoot is acceptable and the step response is well damped. 3. Add a small Ki to remove the remaining steady-state error, and stop as soon as it does without making the response ring. 4. Test with the real load, a real step, and a real disturbance, and check that it stays stable with margin when the load changes.

Think of Kp as a spring, Kd as a damper and Ki as a slow hand that keeps turning until the error is gone. For a pure P loop, the following error at constant speed is velocity divided by the gain. A loop with a position gain Kv = 50 1/s that is tracking 600 rpm (10 revolutions per second, or 3,600°/s) lags the command by 3,600 / 50 = 72°. That is a lot, which is why high-performance drives use high gains and velocity feedforward.

Industrial drives usually nest three loops: an outer position loop (P), a middle velocity loop (PI) and an inner current loop (PI) that sets torque directly. The inner loops are fast and the outer ones are slower, which makes each easier to tune.

Hobby servo and industrial servo

A hobby (RC) servo is a small DC motor, a gearbox, a potentiometer and a control board in one case. The command is a pulse every 20 ms whose width, typically 1 to 2 ms with 1.5 ms at centre, sets the target angle over a limited range, often about 180°. The potentiometer provides feedback, and the board runs a simple, usually proportional, control law. It is cheap and has coarse resolution and limited torque.

An industrial servo is a separate motor, usually a PMSM or BLDC type with an encoder, plus a servo drive with current, velocity and position loops. It is driven by step-direction pulses, an analogue voltage, or a fieldbus. It offers high peak torque, often three times the rated torque for short accelerations, fast response, and tuning tools in the drive. The cost is higher and tuning is required.

How the simulator models a servo motor

Curvo Studio's servo motor simulator does not include a PID controller or a step-response plot. It models the steady-state behaviour of a tuned servo axis with an encoder, and uses a single position loop gain, Kv, to show the effect of loop stiffness.

The drive is assumed to have a current loop that makes current proportional to torque. The motor follows the commanded speed up to the speed ceiling set by the supply voltage. Load raises the current in proportion to torque until the drive reaches its peak, taken as three times the rated torque. Beyond that the axis cannot hold the command and the simulator reports the torque limit, rather than slowing quietly.

The following error is the proportional-loop lag, 6 × rpm / Kv in degrees, which disappears when the axis stops. The start-up response is a first-order approximation whose rise time depends on the loop gain and the load. The encoder resolution is set in bits and the nameplate shows the counts per revolution. The model has no integral action, derivative action, overshoot or oscillation, so those effects are described here but are not simulated.

Try it in the servo motor simulator

In the servo motor simulator, try these experiments.

  • Set the Commanded speed and read the rotor speed and the Following error. Check the following error against 6 × rpm / Kv.
  • Raise the Position loop gain and watch the following error shrink in proportion.
  • Give the Commanded speed a negative sign to reverse the direction.
  • Increase the Mechanical load slider (up to 350%). The Torque and Phase current readouts rise together, because the current tracks torque. Push the load past 300% of rated torque and the simulator reports the torque limit.
  • Lower the Supply voltage below the rated value and see that the speed ceiling falls, while torque stays available.
  • Change the Encoder resolution (8 to 24 bits) and read the counts per revolution hint.

Try it in the simulator

Frequently asked questions

What is PID control in a servo motor?
PID control computes the motor command from the position error with three terms: proportional to the error, proportional to its integral over time, and proportional to its rate of change. Together they make the shaft reach the target quickly, with little overshoot and no lasting error.
What does the P term do in a servo?
The proportional term sets the stiffness. A higher gain gives faster response and a smaller error, but also more overshoot and, if too high, oscillation. With P alone, a constant load usually leaves a steady-state error.
Why add an integral term?
The integral term accumulates any remaining error and increases the output until the error is gone, so it removes steady-state error caused by constant loads or friction. Too much integral action causes overshoot and windup, so it is kept small and clamped.
What does the derivative term do?
The derivative term responds to how fast the error is changing and acts like damping. It reduces overshoot and ringing, but it amplifies sensor noise, so it is usually combined with a filter and kept modest.
What is the difference between a stepper and a servo?
A stepper runs open-loop and holds position by counting steps, but it can lose steps under overload without knowing it. A servo measures the position with an encoder and corrects the error continuously, giving higher speed, torque reserve and certainty of position, at a higher cost.
How do hobby servos differ from industrial servos?
A hobby servo is a small integrated unit with a potentiometer and a simple controller, commanded by 1 to 2 ms pulses. An industrial servo uses a separate motor with a high-resolution encoder and a drive with nested current, velocity and position loops, for higher torque and accuracy.