Documentation
    Preparing search index...

    Class PIDController

    A textbook PID controller with the §111 constructor, clamped output, and the anti-windup and derivative conventions documented on this module.

    const controller = new PIDController({ kp: 2, ki: 0.5, kd: 0.1, outputLimits: [-10, 10] });

    // Once per fixed step (§10), with the injected simulation delta:
    const torque = controller.update(targetSpeed, measuredSpeed, time.fixedDeltaTime);

    The three gains are plain mutable fields so a control-system demonstration can retune them live (§111's stated use). They are validated at construction only; assigning a non-finite gain afterwards is the caller's responsibility. PIDController.outputLimits and PIDController.derivativeOn are fixed for the controller's lifetime — both change what the accumulated integral means, so changing them would need a reset to be meaningful, and building a second controller is clearer.

    Index
    derivativeOn: PIDDerivativeSource

    Signal the derivative term acts on.

    kd: number

    Derivative gain, in seconds. Mutable for live tuning.

    ki: number

    Integral gain, per second. Mutable for live tuning.

    kp: number

    Proportional gain. Mutable for live tuning; see the class note.

    outputLimits: readonly [number, number]

    [minimum, maximum] output bounds (§111 outputLimits). Frozen copy.

    • get integral(): number

      ∫e dt so far, in error·seconds. Zero after PIDController.reset.

      Returns number

    • get lastError(): number

      setpoint − measurement of the most recent update; 0 after a reset.

      Returns number

    • get lastOutput(): number

      The value the most recent PIDController.update returned; 0 after a reset.

      Returns number

    • get primed(): boolean

      Whether a previous sample exists, i.e. whether the next step has a derivative.

      Returns boolean

    • Clears the integrator, the derivative history, and the reported last output/error, returning the controller to its just-constructed state. The gains, limits, and derivative convention are untouched.

      Reset when the loop is re-enabled, when the plant is teleported, or when a replay rewinds (§33) — anything that makes the accumulated history describe a plant that no longer exists.

      Returns void

    • Advances the controller by deltaSeconds and returns the clamped output.

      Only deltaSeconds is validated: the setpoint and the measurement come from the plant on the hot path and are taken as given, so a NaN measurement propagates into the integrator rather than throwing. Validate sensor data before it reaches a controller.

      Parameters

      • setpoint: number

        Desired value of the controlled quantity.

      • measurement: number

        Current value of the controlled quantity.

      • deltaSeconds: number

        Step in seconds (§7a); finite and > 0.

      Returns number

      RangeError if deltaSeconds is not a finite positive number.