A First Order Filter for a Script Tag
This example demonstrates how to use Script tags by creating a module to do a first order filter of an analog value. The instructions are written for use with the V12 Training Simulator, but it can be used with any VTScada application that has a tag with an analog value.
A first-order filter is used to smooth out noise from a value that tends not to change rapidly. Over time, it will match the value of whatever tag it is watching. It will lag changes, and transient spikes will have little effect on the value.
(New filter value) = 0.9 * (Old filter value) + 0.1 * (New data source value)
In this example, the parameters PointObj and SciptObj must be present by definition for use in a Script tag. PointObj will be the tag selected for the "In Tag Scope" parameter of the Script tag. (That is, the analog value to be watched.) ScriptObj points is a self reference to the script tag that this module will be used within. This provides the hook between the Script tag and a module.
In this example, the calculation is done every two seconds, and that calculation exists in a script block, therefore running only a single time each two seconds. If you prefer, you could change this to a Watch() on the PointObj tag's value if you wanted code that executed on change rather than on time.
- Copy the following code into a new file.
- Save the file as FilterFor.SRC in the tutorial application's folder.
(Or application of your choice.) - Declare the module in the Plugins section of the AppRoot.SRC file.
[ (PLUGINS) {===== Modules added to other base system modules =====} FilterFor Module "FilterFor.SRC"; ]
- Import File Changes.
- Create a new Script tag in the application.
- When configuring, select an analog tag such as a tank level or a pump flow as the scope tag.
- Type the name you used when declaring your module in AppRoot (FilterFor).
The tag scope will vary from this example according to your choice of application and tag. - Draw your script tag near whatever it is watching. Run the application.
{============================ Filter Module =======================} { This module is used to do a First order Filter of an input point } { Current Value = .1 * new value + .9 * previous value } { The result is stored into the script point's value variable } {==================================================================} ( PointObj { object value of analog to monitor }; ScriptObj { script object }; ) [ PreviousValue { store the last calculated value }; ] Filtering [ { ... every two seconds ... } If AbsTime(1, 2, 0); [ { Ensure there is a valid value the first time through } PreviousValue = PickValid(PreviousValue, PointObj\Value, 0); { Calculate the filtered value } ScriptObj\Value = .1 * PickValid(PointObj\Value, 0) + .9 * PreviousValue; { Save for the next time } PreviousValue = ScriptObj\Value; ] ]