fuzzy_pid_init
Builtin index · Signal processing
result = fuzzy_pid_init(error_scale, error_dot_scale, output_scale, dt)
Input: error_scale, error_dot_scale, output_scale, dt · Output: result
What it is
A Mamdani fuzzy PD-type controller's initial state — the classic 2-input (error, error_dot), 1-output textbook fuzzy inference system. All four arguments are positive numbers: error_scale and error_dot_scale are the magnitudes that count as "fully big" (inputs are normalised as clamp(error/error_scale, -1, 1) onto the -1..1 universe the rule base lives on, so these saturate the controller rather than merely detuning it); output_scale multiplies the defuzzified result; dt is the timestep for the internal error_dot estimate.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = fuzzy_pid_init(error_scale, error_dot_scale, output_scale, dt) | error_scale numberAll four arguments are positive numbers: error_scale and error_dot_scale are the magnitudes that count as "fully big" (inputs are normalised as clamp(error/error_scale, -1, 1) onto the -1..1 universe the rule base lives on, so these saturate the controller rather than merely detuning it).error_dot_scale numberAll four arguments are positive numbers: error_scale and error_dot_scale are the magnitudes that count as "fully big" (inputs are normalised as clamp(error/error_scale, -1, 1) onto the -1..1 universe the rule base lives on, so these saturate the controller rather than merely detuning it).output_scale —output_scale multiplies the defuzzified result.dt —dt is the timestep for the internal error_dot estimate. | Model —kind "fuzzy_pid" | Returns a Model (kind "fuzzy_pid") with fields error_scale, error_dot_scale, output_scale, dt, e, e_dot, u and n. Membership functions: 5 triangles per input (NB, NS, ZE, PS, PB) centred at -1, -0.5, 0, 0.5, 1 with half-width 0.5, so adjacent pairs overlap and memberships sum to 1 everywhere. Rule base: the fixed, diagonal-symmetric 5x5 table standard to every fuzzy-PID text; the caller does not supply 25 rules. Its anti-diagonal is all ZE — a large negative error with a large positive error-rate is already being corrected, which is the derivative action. Defuzzification: true centre-of-gravity — each rule's output triangle is clipped at that rule's firing strength (min-implication), the results aggregated by max, and the centroid integrated numerically. Note the consequence, which is a property of COG rather than a defect: a fully saturated input reaches 5/6 = 0.8333... of output_scale, not all of it. Having no integral term, this controller settles with a small proportional offset. 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.
f = fuzzy_pid_init(1, 1, 1, 0.01)
print("{f.update(0, error_dot = 0).u}") # 0 -- centre of the table
print("{f.update(1000, error_dot = 0).u}") # 0.8333 -- saturated: the COG ceiling, not 1
print("{f.update(-1000, error_dot = 1000).u}") # 0 -- anti-diagonal cancels
-1.644518e-17 0.83335 -1.644518e-17
from signal-processing
How it works
A Mamdani fuzzy PD-type controller — the classic textbook 2-input (error, error_dot) 1-output (u) fuzzy inference system, same immutable-state convention as the rest of this family. fuzzy_pid_init(error_scale, error_dot_scale, output_scale, dt). SCALING — the rule base and the membership functions live on a NORMALISED universe -1..1; the three scale factors are what map a real-world plant onto it. error is normalised as clamp(error/error_scale, -1, 1) (likewise error_dot), and the defuzzified result is multiplied by output_scale. So error_scale is "the error magnitude that counts as fully big", not a gain — getting it wrong saturates the controller rather than merely detuning it. MEMBERSHIP FUNCTIONS — 5 triangles per input (NB, NS, ZE, PS, PB) centred at -1, -0.5, 0, 0.5, 1 with half-width 0.5, so adjacent pairs overlap and the memberships sum to 1 everywhere. Same 5 on the output universe. RULE BASE — the fixed, diagonal-symmetric 5x5 table every fuzzy-PID text prints (written out literally in FUZZY_RULES rather than computed from the indices, so it can be read against the book). The caller does not supply 25 rules; a standard rule base is the whole point of the v1. DEFUZZIFICATION — true centre-of-gravity: each rule's output triangle is CLIPPED at that rule's firing strength (Mamdani min-implication), the 25 clipped sets are aggregated by max, and the centroid of that aggregate is taken numerically. Note the consequence, which is a property of COG and not a bug: even a fully-saturated input reaches only 5/6 = 0.8333... of output_scale, because that is where the centroid of the saturated PB triangle sits, not at 1.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.