Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Models: what the instrument measures

A query with respuesta: "{tension}" returns what was configured. A measurement is something else: the current a supply delivers depends on the voltage, the load and whether the output is on. That is a model.

modelos:
  corriente_medida:
    tipo: formula
    cuando: { salida: true }
    expr: "min(tension / carga_ohm, limite_corriente) + gauss(0, 1e-5)"
    fallback: 0.0
    formato: fijo:5

And the query that uses it:

  - patron: "MEASure[:SCALar]:CURRent[:DC]"
    query: true
    modelo: corriente_medida

Fields

FieldWhat it is
tipoAlways formula.
exprThe formula.
cuandoConditions on the state. If any is not met, fallback is answered. If there are several, all must hold.
fallbackThe answer when cuando is not met. Defaults to 0.0. Number or text.
formatoHow the number is written. Optional.

What goes in expr

  • State variables, by name: tension, carga_ohm.
  • Numbers: 10, 0.5, 1e-3, 2.5E+2.
  • + - * / with the usual precedence, parentheses and unary minus.
  • Functions:
FunctionResult
gauss(mean, sigma)Gaussian noise. It changes on every read, but the series repeats the same each time you start Crucible, so automated tests are reproducible.
abs(x)Absolute value.
sqrt(x)Square root.
min(a, b), max(a, b)Minimum and maximum.

There are no conditionals inside the formula: cuando only chooses between the formula and a fixed fallback.

The supply’s model, running

Against a freshly started bench:

$ printf 'MEAS:CURR?\n' | nc -q1 127.0.0.1 5025
0.00000
$ printf 'OUTP?\n' | nc -q1 127.0.0.1 5025
0
$ printf 'VOLT 5;:OUTP ON;:MEAS:CURR?\n' | nc -q1 127.0.0.1 5025
0.50001
$ printf 'VOLT 20;:MEAS:CURR?;:OUTP?\n' | nc -q1 127.0.0.1 5025
0.99999;1
$ printf 'MEAS:VOLT?\n' | nc -q1 127.0.0.1 5026
+3.300139E+00

Step by step:

  1. With the output off cuando is not met, and the measurement is the fallback, written in the model’s format: 0.00000.
  2. OUTP? answers 0, not false: it is answered by the salida_como_numero model, with formato: entero, like a real instrument.
  3. At 5 V across 10 Ω, half an ampere plus noise.
  4. At 20 V it would be 2 A, but min clips it to the 1 A limit: the supply goes into constant current.
  5. The multimeter writes scientific notation, like most do.

formato

Your software expects numbers written the way the real instrument writes them:

formatoExample answerTypical use
(not set)1.0, 4.501385029307777—
entero (integer)1, 0, -3States (OUTP?) and counters.
fijo:N (fixed)fijo:4 → 4.5014Supplies that answer with fixed decimals.
cientifico:N (scientific)cientifico:6 → +4.501385E+00Multimeters and most SCPI measurements.

The format also applies to fallback when it is numeric.

Queries with respuesta: "{variable}" return the value as it is (5.0). If your software expects another form, use a model whose expr is the variable, with the right formato.

Errors in formulas

Syntax errors —an unclosed parenthesis, a function that does not exist— show up when validating or starting, with the position:

dispositivo:
  modelo: DMM-ABC
  idn: "ACME,DMM-ABC,SN12345,FW2.01"

protocolo: scpi

estado:
  tension_entrada: 0.0

comandos:
  - patron: "MEASure:VOLTage[:DC]"
    query: true
    modelo: lectura

modelos:
  lectura:
    tipo: formula
    expr: "tension_entrada + gaus(0, 0.0001)"
$ crucible --validar perfiles/formula_rota.yaml

error: perfiles/formula_rota.yaml: perfil inválido: modelo 'lectura': error de evaluación: fórmula 'tension_entrada + gaus(0, 0.0001)': función 'gaus' desconocida; existen: gauss, abs, sqrt, min, max (posición 23)

The message says the function gaus is unknown, lists the ones that exist and gives the position.

A variable that does not exist in the state, however, is only detected when the query is made: no answer arrives and SYST:ERR? returns -200,"Execution error;…la variable '…' no existe en el estado…" (the variable does not exist in the state). Declare in estado every variable your formulas use.