Qu

pid_init

Builtin index · Signal processing

result = pid_init(Kp, Ki, Kd, dt, [integral_clamp=])

Input: Kp, Ki, Kd, dt, integral_clamp · Output: result

What it is

A discrete PID controller's initial state. Kp/Ki/Kd (numbers): the proportional, integral and derivative gains; dt (number, positive): the fixed sample period; integral_clamp (optional number, positive): a symmetric anti-windup bound on the integral accumulator, uncapped when omitted.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = pid_init(Kp, Ki, Kd, dt, [integral_clamp=])
Kp numberKp/Ki/Kd (numbers): the proportional, integral and derivative gains.
Ki numberKp/Ki/Kd (numbers): the proportional, integral and derivative gains.
Kd numberKp/Ki/Kd (numbers): the proportional, integral and derivative gains.
dt numberdt (number, positive): the fixed sample period.
integral_clamp number, optionalintegral_clamp (optional number, positive): a symmetric anti-windup bound on the integral accumulator, uncapped when omitted.
Model —kind "pid"
integral —the running sum, 0
e_prev —the previous error, 0
u —the last output, 0
integral_clamp —the limit, or Nothing
Returns a Model (kind "pid") with fields kp, ki, kd, dt, integral (the running sum, 0), e_prev (the previous error, 0), u (the last output, 0) and integral_clamp (the limit, or Nothing). State is immutable, the same convention kalman_init/lms_init use: thread the value returned by .update(error) through successive calls rather than mutating in place, so the whole control trajectory stays available. The gains are deliberately not range-checked — a negative gain is correct for a sign-inverted plant, and whether a triple is stable is a property of the plant, which pid_init has not seen. Added in v0.3.0.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

s = pid_init(2.0, 1.5, 0.1, 0.05, integral_clamp = 2.0)   # |integral| <= 2.0

from signal-processing

setpoint = 1.0
tau = 1.2
K   = 2.0
dt  = 0.05
s   = pid_init(2.0, 1.5, 0.1, dt)
plant_y = 0.0
for k = 0 to 199
    err = setpoint - plant_y
    s   = s.update(err)                              # new state, `.u` is the output

    plant_y = plant_y + (K*s.u - plant_y)/tau * dt   # the caller applies it

end for
print("final = {plant_y}")        # final = 0.999975  (1.0000)

print("u = {s.u}")                # u = 0.499998  -- K*u = setpoint, as it must be

print("integral = {s.integral}")  # integral = 0.333299  -- u/Ki

output
final = 0.999975
u = 0.499998
integral = 0.333299

from signal-processing

How it works

A discrete PID controller, on the SAME immutable Value::Model state handle as kalman_init/lms_init (kind "pid") and reached through the SAME shared "update" arm — not a parallel mechanism. The reason is the one lms_init gives: a controller's whole diagnostic value is the trajectory of its integral term and its output, and an in-place update destroys exactly that history. pid_init(Kp, Ki, Kd, dt, [integral_clamp=]) — gains, the fixed sample period dt, and an optional anti-windup limit. Fields: kp/ki/kd/dt (the configuration, echoed back so a state is self-describing), integral (the running sum sum(e[i]*dt), 0), e_prev (the previous error, 0), u (the last output, 0) and integral_clamp (the limit, or Nothing when uncapped). e_prev starts at 0, so the first .update(e) sees a derivative of e/dt. That is the textbook definition of the recursion (u[k] = Kp*e[k] + Ki*sum(e[i]*dt) + Kd*(e[k]-e[k-1])/dt with e[-1] = 0) and is deliberate: a step applied at k = 0 genuinely IS a step, and suppressing the first derivative kick would silently change the response the gains were tuned against. dt must be positive — it divides into the derivative term and multiplies into the integral one, so dt = 0 is not a degenerate-but-usable case, it is a division by zero. The gains themselves are NOT range-checked: a negative gain is legitimate for a plant with inverted sign, and whether a given triple is stable is a property of the plant, which pid_init has not seen.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.