Introduction for Programmers
Even though the syntax of the Math module has many features similar to a programming language, it is important to keep in mind that the Math module is not a programming language.
It is merely a collection of formulas that is evaluated formula by formula with each iteration of the module. The focus here is on the time relationship between the individual signals. There must never be a situation where a result value is no longer defined at a certain point in time. This is why certain constructs do not work, such as
if (someCondition)
y = 2 * x;
How should y be determined if the condition is not met?
Accordingly, you may miss some programming language functions - probably the most noticeable is that there are no control flow constructs.
A few examples of functions that can be used to achieve a type of conditional execution can be found here:
Conditional execution
Ternary Operator ?
The feature that is probably most similar to classic flow control features is the ternary operator, which works in the Math module in the same way as in common programming languages.
Example
Valve = temperature > 25.0 ? 20.0 : 80.0;
In programming language
Valve = Temperature > 25.0 ? 20.0 : 80.0;
// or
if (temperature > 25.0)
valve = 20.0;
else
Valve = 80.0;
When using the ternary operator, care must be taken not to write the following error-prone constructs:
Result = Measured value != 0.0 ? Maximum / measured value : -1.0;
This can lead to a division by zero.
Instead:
Result = Maximum / nonZero(Measured value);
Select function
To select a specific value from a set of available values, the select()
function can be used.
Example
Select = select(Index, Value0, Value1, Value2, Value3);
In programming language
switch(index)
{
case 0: selection = value0; break;
case 1: selection = value1; break;
case 2: selection = value2; break;
case 3: selection = value3; break;
default: selection = false;
}
Priority encoder
To select a value based on a set of conditions, the prioEnc()
function can be used.
Example
Status = prioEnc(condition0, condition1, condition2, condition3, {none: 1000, map: [10,20,30,40]});
In programming language
if (condition0)
Status = map[0]; // = 10
else if(condition1)
Status = map[1]; // = 20
else if (condition2)
Status = map[2]; // = 30
else if (condition3)
Status = map[3]; // = 40
else
Status = none; // = 1000
There are also further options here via the parameterization of the function, e.g. detecting edges as a condition or holding and continuing the last set condition if no other is set.
Value range limitation
The range()
function can be used to restrict a measured value to a specific value range.
Example
Limited = range(measured value, [-10.0, 10.0]);
In programming language
if (measured value < -10.0)
Limited = -10.0;
else if (measured value > 10.0)
Limited = 10.0;
else
Limited = measured value;
Alternatively, any values can be specified that are to be output if the lower or upper limit is reached.
Example
Limited = range(measured value, [-10.0, 10.0], {lower: -99, upper: 99});
In programming language
if (measured value < -10.0)
Limited = lower; // = -99;
else if (measured value > 10.0)
Limited = upper; // = 99;
else
Limited = measured value;
Store values temporarily
So-called flip-flops can be used to save values between iterations of the Math module.
rsFF
The rsFF()
function can be used to save a Boolean value.
Example
State = rsFF(ConditionReset, ConditionSet);
In programming language
if (ConditionReset)
condition = false;
else if (ConditionSet)
Condition = true;
else
State = last state;
dLatch
The function dLatch()
can be used to save a value of any data type.
Example
flag = dLatch(conditionHold, measured value);
In programming language:
if (conditionHold)
flag = last flag;
else
flag = measured value;