Control System

Two machine settings computed from sensor readings, with every sum shown.

control-system.js · output · proof · check · try it in the playground


The question

A controller reads sensors and decides how hard to drive its actuators: the parts of a machine that act, such as a valve, a motor or a heater.

This example computes commands for two actuators:

The formulas and constants are illustrative, not taken from a real machine.

What should each command be, and can every intermediate number be checked?


Historical background

James Clerk Maxwell’s 1868 paper On Governors analyzed how a mechanism senses and corrects changes in speed. It is an early mathematical treatment of feedback. This example pairs a feedback command with a feedforward command that uses a measured disturbance. Maxwell, On Governors


What we tell eyemind

First the readings, as facts (the program has a few more that no rule uses):

fact(measurement1('input1', [6, 11]))
fact(measurement2('input2', 'true'))
fact(measurement3('disturbance1', 35766))
fact(measurement4('output2', 24))
fact(observation3('state3', 22))
fact(target2('output2', 29))
// …

Then the rule for actuator 1:

implied_by(
  control1('actuator1', C),
  [
    measurement10('input1', M1),
    measurement2('input2', 'true'),
    measurement3('disturbance1', D1),
    gt(D1, 0),                      // the logarithm needs a positive disturbance
    is(C1, mul(M1, 19.6)),          // proportional part
    is(C2, div(log(D1), log(10))),  // compensation part: log10 of the disturbance
    is(C, sub(C1, C2)),             // simple feedforward control
  ],
)

implied_by(head, body) is a backward rule: “the head holds if every part of the body holds”. Capitalized names such as C and M1 are variables, placeholders filled in as the rule runs.

is(X, Expr) means “calculate”: it sets X to the value of an arithmetic expression built from mul (times), div (divided by), sub (minus), add (plus), log and sqrt. gt(A, B) tests A is greater than B. These are builtins: operations built into eyemind rather than defined by rules. Because a logarithm of zero or less is undefined, the rule first checks with gt that the disturbance is positive.

measurement10 is a helper that turns a pair like [6, 11] into one number: if the first value is smaller, the square root of the difference (here √5); otherwise the first value. It has two rules, one for each case, and the cases exclude each other.

Numbers with a decimal point, like 19.6, are floating-point numbers, which a computer stores to about 16 digits; whole numbers like 29 are exact integers.


Actuator 2, and guarding a division

implied_by(
  control1('actuator2', C),
  [
    observation3('state3', P3),
    measurement4('output2', M4),
    target2('output2', T2),
    is(E, sub(T2, M4)),        // error: target minus output
    is(D, sub(P3, M4)),        // differential error: observed state minus output
    is(C1, mul(5.8, E)),       // proportional part
    nonlinear_part(E, D, C2),  // nonlinear differential part
    is(C, add(C1, C2)),        // PND feedback control
  ],
)

implied_by(
  nonlinear_part(E, D, C2),
  [
    ne(E, 0),
    is(N, div(7.3, E)), // nonlinear factor
    is(C2, mul(N, D)),
  ],
)
implied_by(nonlinear_part(E, _, 0), eq(E, 0))

Actuator 2 adds a proportional part (5.8 times the error) and a nonlinear part. The nonlinear factor divides by the error. On target the error is zero, and dividing by zero would stop the run, so two exclusive rules split the cases: ne(E, 0) (E is not 0) computes the factor, and eq(E, 0) (E equals 0) leaves the nonlinear part out as 0.


What eyemind concludes

// Answers: each instance of a query() that holds.
control1('actuator1', 39.27346198678276)
control1('actuator2', 26.08)

The first line is a comment saying what follows. The program asks query(control1(_, _)), “which commands can be computed?”, and gets one answer per actuator: actuator 1 gets about 39.27, actuator 2 gets 26.08. The numbers are printed in full, as the computer holds them.


The proof, line by line

eyemind --proof writes down how it reached each answer. This is the complete proof:

// Answers: each instance of a query() that holds.
control1('actuator1', 39.27346198678276)
control1('actuator2', 26.08)

// Certificate: the derived facts and every answer of each call the questions make.
called(control1(_0, _1), [control1('actuator1', 39.27346198678276), control1('actuator2', 26.08)])
called(measurement10('input1', C), [measurement10('input1', 2.23606797749979)])
called(nonlinear_part(5, -2, D), [nonlinear_part(5, -2, -2.92)])

It has two parts:

Every rule here is a backward rule, worked out only when the query asks for it, so the proof has no step(...) records: those record how forward rules derived facts. Nor does it write down the readings or the arithmetic: the checker looks up the one and computes the other.

Read the tables as sentences:

  1. Asking for the actuator commands gives exactly two answers: actuator 1 gets 39.27346198678276 and actuator 2 gets 26.08.
  2. The input pair of input1 reduces to exactly 2.23606797749979: rule 15 takes the pair [6, 11], checks 6 < 11 and computes √(11 − 6).
  3. The nonlinear part for error 5 and differential error −2 is exactly −2.92: rule 13 checks that 5 is not 0, computes the factor 7.3 / 5 = 1.46, and multiplies it by −2.

Checked: what the report says

A proof is only worth something if someone else can check it. eyemind -c generates the proof and checks it against the program; --check-proof checks a saved proof. The checker never reasons by itself. It answers the query by applying the program’s rules one level at a time, taking the answer of every inner call from its table:

Each table is checked the same way, by expanding its own call against the program, so every calculation and comparison is recomputed, not taken on faith, and a table with a wrong or a missing value shows up as a disagreement. A table the check does not use fails it too. This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
control1('actuator1', 39.27346198678276)
control1('actuator2', 26.08)

verdict('checked')

The verdict is checked: both commands follow from the readings and formulas, and they are every answer of the original questions. It does not say the formulas are good control engineering.


Try it

eyemind examples/control-system.js            # the commands
eyemind --proof examples/control-system.js    # answers with their proof

Raise the target to fact(target2('output2', 34)): the error becomes 10, and actuator 2’s command becomes 56.54 instead of 26.08; actuator 1 is unchanged. Set the target to 24, the current output: the error is zero, the nonlinear part is left out, and the command is 0.0. Set the disturbance to 0: actuator 1 gets no command, since its compensation is undefined, and actuator 2 is unaffected.


Takeaway

When a machine setting comes from a chain of formulas, a proof whose every intermediate value is recomputed by the checker turns “the controller said so” into something an engineer can audit line by line.