Example 11 - Controlling MIO Module DOUTs
Example 11a - Switch all outputs
Task
The digital outputs of a MIO module should be switched on or off based on a threshold value.
Solution
Math module code
// 0 = no DO active,
// 1 = DO1 active,
// 2 = DO2 active,
// 3 = DO1 and D02 active,
// 4 = DO3 active,
// 5 = DO1 and DO3 active,
// 6 = DO2 and DO3 active,
// 7 = All DOs active,
Threshold = 80.0;
$"Math.Trigger_Thresh" = threshold($'CAN_Value', Threshold, -0.1, {delayOn: 0.5});
mio_set_do = $"Math.Trigger_Thresh" ? 7 : 0;
Fmconsumer configuration
{
"config": {
"fmd": "canbus1",
"messages": [
{
"channels": [
{
"bitLength": 16,
"bitOffset": 0,
"name": "mio_set_do"
}
],
"defaultMessage": "0000000000000000",
"messageKey": 83,
"sendIntervalMs": 1000,
"sendMode": "OnTimer"
}
]
},
"module": "fmconsumer_mio",
"factory": "fmconsumer"
}
Functions used
Explanation
Using the threshold function, we compare the measurement signal "CAN_Value" with the previously defined threshold value.
If this threshold value is exceeded, we write the number 7 to the output channel "mio_set_do". If the threshold value is not exceeded, we write a 0 to the channel.
The "mio_set_do" channel is then sent to the MIO module via CAN bus using the FMConsumer module.
The MIO module interprets the number in the CAN message based on its binary representation. For example, if the first bit of the number is set, output 1 is activated—the same applies to the second and third bits and outputs 2 and 3. This results in the following truth table:
| Decimal number | Output 1 | Output 2 | Output 3 |
|---|---|---|---|
| 0 | Off (0) | Off (0) | Off (0) |
| 1 | On (1) | Off (0) | Off (0) |
| 2 | Off (0) | On (1) | Off (0) |
| 3 | On (1) | On (1) | Off (0) |
| 4 | Off (0) | Off (0) | On (1) |
| 5 | On (1) | Off (0) | On (1) |
| 6 | Off (0) | On (1) | On (1) |
| 7 | On (1) | On (1) | On (1) |
Example 11b - Switching individual outputs
Task
Control the three DOUTs of a MIO module using three limit values.
Solution
Limit1 = 80.0;
Limit2 = 51.5;
Limit3 = 42.42;
Thresh1 = threshold($'CAN_Value1', Limit1, -0.1, {delayOn: 0.5});
Thresh2 = threshold($'CAN_Value2', LimitValue2, -0.1, {delayOn: 0.5});
Thresh3 = threshold($'CAN_Value3', LimitValue3, -0.1, {delayOn: 0.5});
mio_set_do = 1 * Thresh1 + 2 * Thresh2 + 4 * Thresh3;
Functions used
Explanation
Using the truth table described above, we can see that the three outputs are activated with values of 1, 2, and 4, respectively.
If we now want to programmatically activate a combination of the three outputs, we simply need to add these three numbers together.
To determine which numbers we want to add to our result, we multiply each number by its corresponding threshold result.
If the result of the threshold was "False," the number is multiplied by 0 and removed from the addition. However, if the result of the threshold was "True," the number is multiplied by 1 and remains in the addition.