Mathematical Operations With Lua (FW version 1.7)
Mathematical operations can be applied to Lua signals as in any other protocol. This can be done by configuring WCC Lite according to the solution needed. To create an example which tests multiple mathematical operations, an Excel configuration and a Lua script are required. The device sheet should look similar to this:
Each Lua device is created to send result values to Modbus TCP signals with different mathematical functions applied. This way, the same Lua script can be reused and is more optimal since the signal alias for each device can stay the same. There are many other solutions, but this one allows us to observe results more clearly.
In the field "ip" for Modbus TCP master, enter the IP address of the Wi-Fi connection for the computer in use. In the field "host" for IEC104 slave protocol, enter the IP address of the WCC Lite device.
Signals for these devices should be mapped, for example, to this:
Each Lua device has command and result signals. Command received from the IEC104 protocol is sent to the Lua command signal, and then this signal sends back a response for the IEC104 protocol. If the response does not have negative cot attributes, the value is then sent to the Lua result signal, which sends the value to the Modbus TCP result signal. Mathematical operations are applied to IEC104 protocol signals since it is the one sending the commands.
As mentioned before Lua script for each Lua device is going to be unchanged and should look like this:
local saved = get(signals.result) --getting result signal which is equated to new variable 'saved'
local command = get(signals.command) --getting command signal which is equated to new variable 'command'
--get() function returns nill if there is no valid value
if not command then --if command is not nill
if saved then --if signal is not nill
publish(signals.result, saved.value) --this value is published to result signals and saved value
end
return 0
end
local time_diff = time_ms() - tonumber(command.time) --compares command time and real time
local is_command = time_diff < 30000 and time_diff > -30000 --if command time differs from
--real time more than 30s it will not be executed
if string.find(command.attributes, "nt") or string.find(command.attributes,
"iv") or string.find(command.attributes, "ov") then
--searching if signal has negative attributes
if is_command then --if command execution time is not exceeding the limits then
command.attributes = "cot=7,cotn" --equates negative cot values to response signal attributes
publish(signals.command, command) -- and publishes value to command signals and value
if saved then --if there is saved value then
publish(signals.result, saved.value) --restores saved value to result signals
end
return 0
end
else
if is_command then
command.attributes = "cot=7"
publish(signals.command, command) --in this cycle command value is being returned as well as
--cot7 and cot10 values in case given signal is command and has no negative attributes
command.attributes = "cot=10"
publish(signals.command, command) --publishes response to the command
save(signals.result, command.value) --command value is being saved to result signal
end
publish(signals.result, command.value) --in this row command value is being published to result signals
end
Upload Excel configuration to WCC Lite:
After uploading the configuration, no errors should appear, and all signals should be represented on the web. To upload a Lua script, go to Script-Runner, select upload script and then start (for each Lua device):
Open Vinci as an IEC104 master, enter the IP address of WCC Lite and start communication. Then open another Vinci window and connect Modbus TCP master – select Modbus TCP slave in Vinci and enter the same IP address as set in the Excel configuration for the Modbus device. With both communications running, check Protocol connections on the WCC Lite web interface, it should show Connected. From the IEC104 Vinci window, go to the System tab. Select command determined in the Excel configuration (50), IOA (different for each signal) and value. After executing the command, each signal (IEC104 command, Lua command, Lua result and Modbus result) will have the same value, which will now be with math applied. For example, a command with IOA=1 and value 1 is being executed. In Excel configuration for this signal, add a column has a value of 5, which means that this value is going to be added to the value sent and the result will be 6.
There can be multiple mathematical operations for one signal. For example, add, multiply, bit select, etc. If that is the case, math will be applied in typical order (eg, first bit select, then multiply, then add). A more detailed explanation about mathematical operations in Excel configuration can be found here: Optional parameters for signals.
A user can also apply a mathematical condition for the signal value. For example, minimum or maximum value, threshold, suppression time for specific value, etc. Minimum and maximum values can be applied to set the range of the signal; if the value is smaller or larger signal state will show invalid or overflow. Thresholds can be used in many ways. It can be a specific value or a percentage. If the signal value passes the set threshold, it will be represented in the imported signals window. Threshold works by comparing the old value with the new value and then applying the condition of either representing the value or suppressing it, depending on the value change. Suppression value and suppression time are best used together because suppression time determines how long the specific value should be suppressed. There could be multiple values set for suppression. In Excel configuration, those values should be separated by a comma.
Mathematical operations combined with a Lua script are useful for many cases. They can be used for filtering data, converting units, applying specific mathematical logic or other solutions.
Configuration --> Download
Lua script --> Download