To get started with an overview of V2 Adapter Scripts, visit this Knowledge Base Article.
Expressions are a type of value supported in several different parts of an IO adapter script. Expressions allow a value to be calculated as a kind of mathematical or logical formula using other identifiers. Identifiers are variables or data sources defined in the adapter script.
- Variables come from the variables section of an adapter script. A variable is a named list of operations applied to other variables or data sources. For more information, see: IO Configuration - Variables.
- Data sources are defined in other sections specific to a particular integration type. Some examples of data sources would be:
- Pins for Labjack U3/T4/T7 integrations
- Registers and coils for Modbus-TCP integrations
- Tags for Ethernet/IP and OPC-UA integrations
- Declared keys for MTConnect and MTConnect Adapter integrations
- Some expressions used within variable definitions also support the special “this” identifier. The this identifier represents the result of the previous step in the variable’s operation list.
Here are a few example expressions, with the variables or data sources highlighted:
- Convert a temperature from C to F
(temp-in * 9 / 5) + 32
- Check if either is running
spindle-1 or spindle-2
- Check if a pin is in a specific range
AIN0 > 1.2 and AIN0 < 3.4
- Check if an execution key is set to STOPPED
exec == ‘STOPPED’
- Cause a counter to reset after counting up to 10
this >= 10
Update Triggers
Variables and data sources are treated the same way in an expression. When any variable or data source referenced within an expression updates, the entire expression is immediately re-computed. This will cause cascading updates to occur where the expression is used.
For example, if an expression is used in the middle of a variable definition, an expression update will cause the entire variable’s operation list to be re-executed with the last data it received. If this causes the variable’s value to change, then any other expression that references the variable will be re-computed as well. Avoid creating circular update loops via expressions.
Expressions can also appear in data item and condition definitions, which are responsible for outputting data to the MachineMetrics cloud. If an expression update changes the value of the data item or condition, the update will immediately be sent out.
Operators and Functions
Expressions support the following operators:
- Arithmetic:
+, -, *, /, % (modulus), ^ (power)
- Boolean:
and, or, not
- Bitwise:
&, |, ~, ^|, <<, >>, >>>
- Relational:
>, <, >=, <=, ==, !=
- Conditional expression:
(statement) ? (yes) : (no)
Expressions support grouping statements with parentheses. Parentheses can be nested arbitrarily deep and are useful for more complex logic or forcing order of operations.
Examples:
(temp-in * 9 / 5) + 32
AIN0 > 1.2 and AIN0 < 3.4
hv-relay-on ? hv-analog : (lv-analog * 10)
Arithmetic Functions
Expressions support many standard arithmetic functions:
abs(x)
: Compute absolute value of a numberceil(x)
: Round value up to next integerfloor(x)
: Round a value down to next integerlog(x, base)
: Calculate the logarithm of a valueround(x, n)
: Round to the nearest integer or n decimal placessqrt(x)
: Calculate the square root of a value
Examples:
floor(AIN0)
round(abs(spindle-speed))
Statistics Functions
Expressions support several statistical functions. These functions can consume an arbitrary number of arguments, including variables, data sources, or constants.
max(a, b, ...)
: Compute the maximum value of the passed valuesmean(a, b, ...)
: Compute the mean value of the passed valuesmedian(a, b, ...)
: Compute the median value of the passed valuesmin(a, b, ...)
: Compute the minimum value of the passed valuesmode(a, b, ...)
: Compute the mode of the passed valuesprod(a, b, ...)
: Compute the product of the passed valuesstd(a, b, ...)
: Compute the standard deviation of the passed valuessum(a, b, ...)
: Compute the sum of the passed valuesvariance(a, b, ...)
: Compute the variance of the passed values
Examples:
max(spindle-1, spindle-2, spindle-3) > 6000
String Functions
Expressions support some functions intended to work with string values.
startsWith(x, term)
: Checks if the string x starts with the passed term. Matches are case-sensitive.endsWith(x, term)
: Checks if the string x ends with the passed term. Matches are case-sensitive.contains(x, term)
: Checks if the string x contains the passed term anywhere. Matches are case-sensitive.
Examples:
startsWith(msg, ‘EX’)
endsWith(msg, ‘EX’)
contains(msg, ‘error’)
Trig Functions
Expressions support several trigonometry functions.
acos(x)
: Calculate the inverse cosine of a valueacot(x)
: Calculate the inverse cotangent of a valueacsc(x)
: Calculate the inverse cosecant of a valueasec(x)
: Calculate the inverse secant of a valueasin(x)
: Calculate the inverse sine of a valueatan(x)
: Calculate the inverse tangent of a valuecos(x)
: Calculate the cosine of a valuecot(x)
: Calculate the cotangent of a valuecsc(x)
: Calculate the cosecant of a valuesec(x)
: Calculate the secant of a valuesin(x)
: Calculate the sine of a valuetan(x)
: Calculate the tangent of a value
Utility Functions
These miscellaneous functions support other useful functionality in expressions.
flag(x, index)
: Read one bit as true or false from a bit flag value. The index is 0-based.- # The following lines are equivalent
flag(register70, 3)
(register70 & (1 << index)) != 0
Have Questions?
Reach out to Support@machinemetrics.com.
Comments
0 comments
Please sign in to leave a comment.