Skip to main content

Variables

A variable is a piece of named data that is produced by transforming other data available in a configuration. Variables are defined in a top-level section called variables. Some examples of problems you might solve with variables are:

  • Turning a sensor signal into an execution state
  • Counting parts on a pulsing signal
  • Combining multiple signals into a single state

Here's an example of a variable that counts parts based on an analog input that transitions to a high voltage state for a few seconds each time a part is made:

variables:
part-count:
- source: AIN0
- threshold: 5
- rising-edge
- count
info

In the above example, AIN0 is an identifier available in LabJack data sources.

Using the example above, the variable is defined by the unique name part-count, and the body of the variable is a list of operations. Each line beginning with a dash is an operation. Operations can be thought of as steps of work performed one after the other. Each step takes the result of the previous step as its input, performs some work on it, and then outputs it for the next step to use. The part-count variable can be described as that series of steps.

  1. Wait for changes on the AIN0 pin. When a change is noticed, then pass it to...
  2. A threshold operation, which converts the value to true or false if it is above or below the threshold value. Pass the thresholded value to...
  3. A rising edge detector operation, which watches the value for changes from false to true (low to high). This modifies the input to be true only at the moment the transition occurs, and false at all other times, essentially marking a point in time that an event has occurred. Pass the countable value to...
  4. A count operation, which increments by one for each “event” that was detected in the previous step. There are no steps to pass the counter value to, so assign it as the result of the variable instead.

The first operation in the list represents a special case because there is no previous operation passing a value to it. The first operation is limited to a small set of operations that are able to watch other data for changes. When changes occur, the chain of operations will be run through. The list of these supported “starter” operations are:

The last operation is also special, because there is no following operation to pass its result to. Instead, the result becomes the value of the variable itself, which can be referenced in other parts of an adapter script.

See the Expressions document for more information on how variables and other data sources can reference each other and cause cascading updates.

Shorthand Definition

Variables that only need a single source operation to be defined can make use of a shorthand definition syntax.
This can can help condense scripts that need to define lots of simple transformed data.

The following two examples are equivalent:

variables:
machine-running:
- source: spindlespeed > 10
program-name:
- source: programdata.name
variables:
machine-running: spindlespeed > 10
program-name: programdata.name