One of Prosig’s engineers was recently creating a complex worksheet application that required some complicated arithmetic to be performed on values calculated in the worksheet. Worksheet (or global) variables are ideal for this. The Variable Mods DPU also has an expression evaluator built in. Consider the following worksheet that takes a Named Element from two signals and sums them.
In this worksheet we first use the Variables DPU to create 3 variables called Var1, Var2 & Var3. The worksheet uses the Variable Mods (VM) DPU to extract the Named Element $ASSOC_VAR from two different signals and puts them in two variables called Var1 and Var2. In our example these have values of 1.0 and 3.0 respectively. The worksheet then performs a couple of analysis steps. These are for illustrative purposes and are not relevant to our example. Another VM DPU is then used to add the two numbers together. We do this by assigning the expression %Var1% + %Var2% to Var3 as shown in Figure 2 below.
This method works very well so long as we only require the simple arithmetic supported by the VM DPU in DATS V7.0.22. However, our engineer required a much more complex expression using trigonometry and exponentiation. For this we need to get a little more in-depth and use a Script Fragment DPU. Although rarely used, script fragments are very powerful and not as scary as they might sound. In figure 3 you can see a slightly modified version of our worksheet that replaces the VM DPU with a Script Fragment DPU.
In our example we will take our two variables calculate the square root of the sum of their squares. The code used in our script fragment is shown below.
'------------------------------------------------ ' Script Process '------------------------------------------------ Sub Main( ByRef Inputs As SignalSetArray ) Dim fVar1 as single Dim fVar2 as single Dim fVar3 as single ' get data from out global vars into local BASIC vars DacGetGlobalFloat "Var1", fVar1 DacGetGlobalFloat "Var2", fVar2 ' do whatever calculations we want. This is just standard BASIC fVar3 = sqr(fVar1*fVar1 + fVar2*fVar2) ' Put our result back in another global var DacSetGlobalFloat "Var3", fVar3 ' If there is data available on the first input pin, pass it ' straight through to the output pin. Otherwise activate the ' pin anyway, but without any data. If IsDataOnInputPin(0) Then ForwardDataToOutputPin 0, 0 Else ActivateOutputPin 0 End If End Sub
The final section of code is the deafult added at creation by DATS and simply passes the data on our input pin to our output pin. We can leave that code alone. The rest of the code is fairly straightforward.
And that’s it.
Script fragments can perform much more complicated tasks including accessing data and signals, but that is beyond the scope of this simple example.
The good news to result from this example is that it made us think and as a result the expression editor has been improved and in V7.0.23 of DATS it will support a wide range of trigonometry, logarithms, exponentiation and other useful functions.[divider top=”0″]
Download the worksheets used in this example
Example 1: Worksheet using Variable Mods DPU
Example 2: Worksheet using Script Fragment DPU