Real-time PID control?

Real-Time PID Control: Sampling, Jitter, Latency and Deterministic Execution
Real-time PID control is not simply PID running on a fast processor.
A real-time control system must:
measure the process at the required time,
complete its control calculation within the required deadline,
update the actuator with predictable latency.
This distinction is fundamental.
A PID calculation that finishes in 100 µs on average can still be unsuitable if it occasionally takes 20 ms when the control loop has a 1 ms deadline.
Real-time control is primarily about deterministic timing, not maximum processor speed. A slightly slower controller with predictable execution can outperform a faster controller whose timing varies unpredictably.
For the basic control concept, see What Is PID?. The individual controller terms are explained in P, I and D Components.
What Does Real-Time Mean in Control Engineering?
A typical digital PID cycle is:
1. Sample the sensor.
2. Update the process variable.
3. Calculate SP - PV error.
4. Execute PI/PID mathematics.
5. Apply output limits and anti-windup.
6. Update the actuator command.
7. Repeat at the next defined sampling instant.
Digital PID is therefore inseparable from time.
A simple discrete integral can be written as:
I[k] = I[k-1] + Ki × e[k] × Ts
while a simple derivative can be written as:
D[k] = Kd × (e[k] - e[k-1]) / Ts
where Ts is the sample period.
If Ts changes unexpectedly, practical integral and derivative behaviour can change as well.
Real-Time Does Not Mean “As Fast as Possible”
A furnace temperature controller rarely needs to execute every 100 µs.
A motor-current controller may require execution thousands of times faster.
Real-time therefore does not mean:
run everything at the highest possible frequency.
It means:
meet the timing requirements of the physical process predictably.
The fastest sampling rate is not automatically the best sampling rate. The loop rate should be selected from the actual dynamics of the sensor, actuator and process.
This is closely related to Time Delay and System Dynamics.
Why Should Sample Time Be Consistent?
Suppose the intended PID period is:
10 ms.
Execution at:
10.0 ms,
10.1 ms,
9.9 ms
may represent a small timing variation.
But execution at:
10 ms,
38 ms,
7 ms,
24 ms
means the control algorithm is no longer running on the same digital time base for which it was tuned.
This can particularly affect integral and derivative action.
For this reason, PID control in a PLC, MCU or real-time computer is commonly executed from a:
periodic task
or:
hardware timer.
What Is Jitter?
Jitter is variation in the actual timing of a periodic task relative to its intended schedule.
If a 5 ms task runs at:
4.99 ms,
5.01 ms,
5.00 ms
the jitter is small.
If the same task runs at:
5 ms,
9 ms,
3 ms,
7 ms
timing determinism has degraded significantly.
Typical sources of jitter include:
higher-priority tasks,
long interrupt service routines,
operating-system scheduling,
blocking I/O,
network processing,
file or database operations.
Average loop time can hide a serious real-time problem. A controller that averages 1 ms but occasionally stalls for 20 ms may be limited by those rare 20 ms events.
What Is a Control Deadline?
A periodic control task must not only start on time.
It should also finish before the next required output update.
Suppose:
sample period = 1 ms
but:
control computation = 1.4 ms.
The algorithm cannot consistently meet a 1 ms deadline.
Repeated deadline misses can cause:
missed samples,
variable output delay,
loop-phase variation,
loss of control performance.
What Is Task Overrun?
A periodic control task overruns when its execution extends into the next scheduled period.
For example:
task period = 10 ms
worst-case execution time = 14 ms
means the task cannot satisfy its requested period under worst-case conditions.
Possible solutions include:
optimizing the control code,
moving non-critical work to slower tasks,
using more capable hardware,
selecting a longer sample period if the physical plant allows it.
Worst-Case Execution Time Matters More Than Average Time
A PID calculation may usually complete in:
150 µs.
But when diagnostics or communication processing occur, it may occasionally require:
850 µs.
On a 1 ms control period, the 150 µs average looks excellent.
But real-time design must consider the:
worst-case execution time - WCET.
The deadline must still be met under the heaviest legitimate execution path.
Sensor Update Rate and PID Rate Are Not the Same Thing
Suppose an analog-input system produces a new PV every:
250 ms.
Running the PID every:
1 ms
does not create new process information.
The controller may simply process the same old PV hundreds of times.
Increasing PID execution rate does not increase the real feedback bandwidth when the sensor or input module is updating much more slowly.
The entire chain must be considered:
sensor response,
signal conditioning,
ADC update,
network transport,
control-task period,
output update.
What Is Measurement Age?
The controller should ideally know how recent its PV information is.
A value displayed as:
12.5 bar
may look current, but the actual measurement may have been taken:
5 ms ago,
20 ms ago,
300 ms ago.
For example:
sensor conversion delay = 8 ms,
network transfer = 5 ms,
task scheduling delay = 7 ms
already produces about 20 ms of measurement age before additional filtering or output delay is considered.
This is part of the real feedback-loop delay.
ADC Conversion and Input Filtering Add Delay
An analog measurement chain can include:
sensor response,
analog signal conditioning,
anti-alias filter,
ADC acquisition,
ADC conversion,
digital filtering.
Every stage can contribute latency.
In slow thermal control this may be insignificant.
In fast motion or electrical current control, the same delay can become a major part of the loop dynamics.
Output Update Delay Also Matters
The PID may finish calculating now, but the actuator command may not be applied immediately.
Examples include:
PWM register updates waiting for the next carrier boundary,
analog-output module refresh time,
fieldbus output cycles,
valve-positioner communication delay.
Output latency contributes to the same closed-loop delay as sensor latency.
ADC and PWM Synchronization
In motor drives and switching power converters, sampling directly on a switching edge can contaminate the measurement with:
high dv/dt,
high di/dt,
ground bounce,
switching interference.
Modern MCU and DSP systems can trigger ADC conversion from a defined point in the PWM cycle.
This is especially important in fast current-control loops.
See also PID Control with Microcontrollers.
How Does Measurement Noise Affect Real-Time PID?
Fast sampling does not only reveal real process dynamics.
It also exposes more high-frequency noise.
Common noise sources include:
electrical interference,
sensor noise,
quantization,
pressure pulsation,
flow turbulence.
Derivative action is particularly sensitive to fast measurement changes.
Practical controllers may therefore use:
analog filtering,
digital low-pass filtering,
filtered derivative,
derivative on measurement.
But every filter also adds delay.
Filtering in a real-time loop is not merely about making the graph look smoother. Stronger filtering can improve noise rejection while simultaneously increasing feedback delay.
What Is Quantization?
An ADC does not have infinite resolution.
A 12-bit ADC ideally provides:
4096 discrete codes.
If a process changes by less than one effective ADC step, the measured value may remain unchanged and then jump suddenly to the next code.
Quantization can become especially visible with:
small control errors,
high derivative gain,
low-resolution measurement,
very low-noise physical systems.
In some cases it can contribute to small limit-cycle behaviour around setpoint.
The Actuator May Be Much Slower Than the Controller
A controller can calculate every:
1 ms.
But a pneumatic valve may take:
2 seconds
to complete a full stroke.
A heater may have a thermal time constant of minutes.
A large pump may require significant acceleration time.
Running the controller much faster does not remove these physical limits.
Control-loop bandwidth must remain realistic relative to sensor, actuator and plant bandwidth.
Actuator Saturation
The controller may mathematically request:
130% output
while the physical actuator can only provide:
100%.
Similar limits include:
maximum valve opening,
maximum heater power,
maximum motor current,
maximum VFD frequency,
maximum servo torque.
When these limits are reached, the loop is saturated.
Integral Windup in Real-Time Control
If error remains while the actuator is saturated, the integral state can continue accumulating.
This creates:
integral windup.
When the physical limitation disappears, stored integral action can create:
large overshoot,
slow recovery,
extended saturation in the opposite direction.
Common anti-windup methods include:
integral clamping,
conditional integration,
back-calculation,
tracking.
When an actuator is already at its physical limit, repeatedly changing PID gains may attack the wrong problem. Check saturation and anti-windup first.
For field diagnosis, see PID Troubleshooting.
Rate Limits Are Also Constraints
An actuator can have both:
position limits
and:
rate limits.
A valve may require five seconds to move from 0% to 100%.
If the PID output jumps instantly from 10% to 90%, the real valve cannot follow that command immediately.
This creates another form of saturation:
rate saturation.
Ignoring actuator rate limits can lead to poor tuning and unnecessary integral accumulation.
Nonlinear Processes Make One Fixed Tuning More Difficult
A tuning set that performs well at one operating point may perform poorly at another.
Examples include:
control-valve installed characteristics,
pH titration curves,
motor field weakening,
fan and pump system curves.
Possible strategies include:
gain scheduling,
adaptive control,
separate operating modes.
Different process dynamics are covered in PID Parameters for Different Process Types.
What Is Gain Scheduling?
Gain scheduling selects different predefined tuning values according to operating condition.
For example:
low-speed operation may use one Kp/Ki set,
high-speed operation may use another.
The scheduling variable could be:
speed,
load,
pressure,
operating region.
Gain scheduling does not automatically mean the controller is adaptive.
The tuning map can be determined beforehand.
Transitions between tuning sets should also avoid sudden output bumps.
Does Adaptive PID Fix Everything?
No.
Adaptive tuning cannot physically repair:
a sticking valve,
a broken sensor,
an undersized motor,
incorrect wiring,
excessive transport delay.
Adaptive control can help when plant dynamics genuinely change with operating condition.
Software intelligence should not be used to hide physical equipment faults.
CPU Loading and Timing Margin
The processor may be running more than PID.
Other tasks can include:
communication,
HMI processing,
motion planning,
data logging,
alarm handling,
diagnostics.
As CPU utilization rises, timing margin for high-priority periodic tasks becomes smaller.
Real-time analysis should therefore consider:
average CPU load,
peak CPU load,
task priorities,
worst-case execution time.
How Should Task Priority Be Assigned?
A fast control loop usually deserves higher execution priority than:
display refresh,
historian logging,
background communication.
But simply giving every important task the highest priority does not solve scheduling.
Priority should reflect:
task period,
deadline,
execution time,
resource dependencies.
What Is Priority Inversion?
A high-priority control task may need a shared resource currently held by a low-priority task.
If other medium-priority tasks then prevent the low-priority task from completing, the high-priority task can remain blocked longer than expected.
This scheduling problem is known as:
priority inversion.
RTOS resource sharing should therefore be designed with control deadlines in mind.
Does Using an RTOS Automatically Make the System Real-Time?
No.
An RTOS can provide:
periodic tasks,
priorities,
timers,
synchronization primitives.
But poor use of:
blocking calls,
mutexes,
task priorities,
oversized tasks
can still create deadline misses.
An RTOS is an implementation tool, not a substitute for real-time system design.
Real-Time PID in a PLC
In a PLC, PID is often placed in a periodic task with a known execution interval.
A slow temperature loop can use a longer update period.
A flow or pressure loop may require a shorter one.
The complete timing chain includes:
analog-input update,
PID task period,
controller calculation,
output-module update.
For implementation details, see PID Control with PLCs.
Real-Time PID in a Microcontroller
In microcontroller PID, the designer can directly control:
hardware timers,
ADC triggering,
interrupt priority,
PWM timing,
numeric representation.
This can provide extremely low deterministic latency for motor-control and power-converter loops.
Real-Time PID with FPGA
FPGA PID control can implement many operations directly as parallel digital hardware.
Potential advantages include:
very high sample rates,
extremely low deterministic latency,
multiple parallel control loops.
The trade-off is greater design complexity compared with many MCU or PLC applications.
Real-Time PID on an Industrial PC
An industrial PC offers strong computing power, networking and data-processing capability.
But a normal desktop operating system is not automatically suitable for strict sub-millisecond deadlines.
Applications with tight timing requirements may use:
a real-time operating system,
a real-time extension,
dedicated motion hardware,
dedicated I/O hardware.
Should SCADA Run the Fast PID Loop?
For most fast industrial loops, no.
SCADA normally provides:
setpoint management,
trends,
alarms,
operator interface,
historian functions.
The fast control loop commonly runs in:
a PLC,
DCS,
drive,
servo controller,
RTU,
MCU.
However, a properly designed real-time industrial PC can also execute control loops.
It is too broad to say that a PC or SCADA can never participate in PID control. The important issue is whether the actual control calculation runs in an execution environment with suitable real-time guarantees.
Networked PID Control
If sensor and actuator data travel across a network, the loop can experience:
communication delay,
variable latency,
packet loss,
retransmission,
network congestion.
General-purpose Ethernet traffic may have variable delay.
Deterministic industrial networks reduce this problem using techniques such as:
scheduling,
time synchronization,
traffic priority.
But communication delay should still be treated as part of the loop.
Why Are Timestamps Useful?
In distributed control, the controller may need to know not only:
what value was measured
but also:
when it was measured.
A pressure value of:
12.5 bar
has different meaning if it represents:
the present instant,
20 ms ago,
300 ms ago.
Timestamps help track measurement age in synchronized distributed systems.
What Should Happen When a Network Sample Is Lost?
The controller should have a defined policy.
Possible responses include:
briefly holding the last valid value,
using an estimate,
switching the loop to Manual,
forcing a safe output,
raising a fault.
The correct strategy depends on process dynamics and safety requirements.
Fast Inner Loops and Slow Outer Loops
Not every control loop in a system should run at the same rate.
A servo architecture may use:
Position loop → Speed loop → Current loop
where:
current loop is fastest,
speed loop is slower,
position loop is slower again.
This directly connects Servo PID Control, Speed Control, Current Control and Position Control.
Timing in Cascade Control
In a cascade system, the inner loop should normally be substantially faster than the outer loop.
For example:
Pressure PI → Flow SP → Flow PI → Valve
The flow loop should react significantly faster than the pressure loop.
If the outer loop is too fast, it can issue new commands before the inner loop has had time to settle.
See PID and Other Control Strategies.
How Fast Should Pressure and Flow Loops Run?
There is no universal value.
The correct update period depends on:
sensor response,
process time constant,
valve or pump dynamics,
transport delay.
Pressure PID and Flow PID are often faster than thermal loops, but they usually do not need the same execution rate as an inverter current controller.
Running a Temperature Loop Too Fast
A thermal process may have a time constant measured in seconds or minutes.
Executing the PID every:
100 µs
does not make the heater or furnace physically respond faster.
It may only increase:
CPU load,
sensitivity to measurement noise.
For PID Temperature Control, loop timing should follow thermal dynamics rather than processor capability.
Different Real-Time Loops Inside a VFD
A modern VFD can contain multiple control functions running at different rates.
For example:
motor-current control,
speed control,
process PID,
communications,
display processing.
The internal motor current loop may be synchronized to PWM and run very quickly.
A process pressure PID can run much more slowly.
The process PID described in PID with VFDs is not the same control loop as the drive's internal motor current or speed regulator.
Can Data Logging Disturb the Control Loop?
Yes, if it is poorly designed.
A high-speed control task should not normally wait for:
file writes,
database communication,
slow SCADA transfers.
A better architecture is:
fast control task → memory buffer → slower logging task
The control task stores only the required values and returns to its deadline-critical work.
Logging Rate and Control Rate Do Not Need to Match
A current controller can run at:
10 kHz
while a SCADA trend stores data at:
10 Hz
or:
1 Hz.
For detailed commissioning, the controller can temporarily record a high-speed internal trace into a circular buffer.
A slow operator trend does not mean the underlying controller is running at the same slow rate.
Why Is a Watchdog Useful?
Control software can stop executing correctly because of:
deadlock,
infinite loop,
memory corruption,
unexpected task blocking.
A watchdog can detect loss of expected program execution and trigger a reset or other fault action.
But output behaviour during restart must also be designed.
A heater, motor or valve should not remain in an unsafe state simply because the processor has restarted.
A watchdog supervises processor execution; it does not replace a complete machine-safety design.
Sensor Timeout and Stale Data
A real-time controller should also verify that new measurement data continues to arrive.
Suppose a sensor normally updates every:
20 ms.
If no new value arrives for:
500 ms
the controller should not silently continue normal operation using the old PV forever.
Possible actions include:
timeout alarm,
Manual mode,
safe output,
process shutdown.
Invalid Measurements
Digital control can also fail because of invalid numeric data.
Possible values include:
NaN,
Infinity,
out-of-range values,
incorrect engineering scaling.
Allowing such values into PID internal states can corrupt integral and output calculations.
Input validation is therefore part of a robust real-time controller.
Manual / Auto Transfer
If a loop is operating manually at 30% output while the stored automatic PID state corresponds to 80%, changing directly to Auto can produce a sudden actuator jump.
Practical controllers use:
tracking,
integrator initialization,
bumpless transfer.
This is part of Stable PID Operation.
How Should a Real-Time PID Loop Be Tuned?
Before tuning Kp, Ki or Kd, verify:
sample period,
measurement timing,
input scaling,
actuator limits,
task execution time.
Only then should controller gains be tuned.
Tuning a PID while the control period itself is changing is equivalent to tuning a moving target. Stabilize timing before judging gain values.
For the gain-setting process, see PID Gain and Tuning.
Can