adapnex::core::PID

A Proportional‐Integral‐Derivative (PID) controller in the Standard (Ideal/ISA) form.

Synopsis

Declared in <adapnex/core/pid.h>

template<
    typename ProcessType = double,
    typename ControlType = ProcessType,
    typename KpType = decltype(ControlType{} / ProcessType{})>
class PID;

Description

A PID controller continuously calculates an error value as the difference between a desired setpoint and a measured process variable. It applies a correction based on proportional, integral, and derivative terms:

  • P (Proportional) accounts for present values of the error.

  • I (Integral) accounts for past values of the error.

  • D (Derivative) accounts for possible future trends of the error based on its rate of change.

PID Logic Diagram

Parameter Type Direction Description

ACTUAL

ProcessType

Input

The current measured process variable feedback.

SETPOINT

ProcessType

Input

The target setpoint value.

KP

KpType

Input

Proportional gain. Must be finite and non‐zero.

TN

Duration

Input

Integral reset time. Setting TN to 0s disables the integral action.

TV

Duration

Input

Derivative rate time. Setting TV to 0s disables the derivative action.

MIN_OUT

ControlType

Input

Minimum allowed output limit.

MAX_OUT

ControlType

Input

Maximum allowed output limit.

RESET

bool

Input

Re‐initializes the controller, clearing internal error history and resetting integrator state.

OUT

ControlType

Output

The computed control variable output.

Common Use Case

Closed‐loop process control such as temperature regulation in an oven, pressure control in a vessel, or speed regulation on a drive.

Example: Temperature Control

class TemperatureController : public Task {
public:
    float current_temp = 0.0f;
    float target_temp = 75.0f;
    float heater_power = 0.0f;

private:
    PID<float> pid{2.5f, 20s, 2s, 0.0f, 100.0f};

    void Update() override {
        pid(current_temp, target_temp);
        heater_power = pid.OUT;
    }
};

If limits are provided (MIN_OUT < MAX_OUT), the controller uses Integral Back‐Calculation (Anti‐Windup). The integral part will be automatically adapted as if the history of the input values had naturally produced the limited output value. To disable limitation and anti‐windup, set MIN_OUT >= MAX_OUT.

The integral part can be disabled by setting TN to 0s. The derivative part can be disabled by setting TV to 0s.

Non‐finite inputs are handled gracefully. If the controller cannot be meaningfully calculated on a cycle (KP is zero or not finite, or ACTUAL or SETPOINT is NaN), it holds the previous OUT and leaves its internal state untouched, resuming cleanly once the inputs are valid again. An infinite ACTUAL or SETPOINT is a supported value that saturates OUT to the corresponding limit. A non‐finite limit (Inf or NaN) leaves that bound unbounded.

Member Functions

Name

Description

PID [constructor]

Constructors

operator()

Function call operators

Data Members

Name

Description

ACTUAL

The current measured process variable (feedback). A NaN value (e.g. a momentary sensor dropout) holds the previous OUT for that cycle and resumes once it clears. An infinite value drives OUT to saturation.

KP

Proportional gain. Acts as the conversion multiplier from ProcessType to ControlType. Must be finite and non‐zero. A zero or non‐finite KP holds the previous OUT for that cycle and the internal state is not updated.

MAX_OUT

Maximum allowed output limit. If MIN_OUT >= MAX_OUT, limitation and anti‐windup are disabled. A non‐finite value (+infinity or NaN) leaves the upper bound unbounded while keeping any valid lower limit active.

MIN_OUT

Minimum allowed output limit. If MIN_OUT >= MAX_OUT, limitation and anti‐windup are disabled. A non‐finite value (‐infinity or NaN) leaves the lower bound unbounded while keeping any valid upper limit active.

OUT

The computed control variable output.

RESET

When true, re‐initializes the controller on this cycle: the internal error history (integral and previous error) is cleared and timing is re‐armed. Level‐sensitive, like the other inputs.

SETPOINT

The target setpoint value. An infinite value is supported and drives OUT to saturation. A NaN value holds the previous OUT for that cycle.

TN

Integral time / Reset time (Tn). Determines how fast the integral action duplicates the proportional action. Setting TN to 0 disables the integral part.

TV

Derivative time / Rate time (Tv). Determines how far the derivative action looks ahead. Setting TV to 0 disables the derivative part.

Created with MrDocs