Using global variables, variable arithmetic & script fragments

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.

Figure 1: Simple arithmetic worksheet

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.

Figure 2: Variable Mods DPU adding two variables

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.

Figure 3: Modified worksheet using 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.

  • Lines 5,6 & 7 create our two input variables and our output variable. We have used Var1, Var2 & Var3 for consistency, but they do not have to match the names we used in our worksheet global variables.
  • Lines 10 & 11 fectch the global variables Var1 & Var2 and put their values into our local script variables Var1 & Var2.
  • Line 14 performs our calculation and places the result in the script variable Var3.
  • Line 17 takes the value we have calculated in local variable Var3 and puts it into the global variable Var3 so that we can use it elsewhere in the worksheet.
  • As mentioned the rest of the code passes any DATS data through to the output pin.

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

The following two tabs change content below.

Chris Mason

General Manager & Product Development Manager at Prosig
Chris graduated from Portsmouth Polytechnic in 1983. His interest in software began in the late 1970's when he spent his time typing in and debugging programs on a Sinclair ZX80. Chris is General Manager & Product Development Manager at Prosig where he does management things, leads the product development and works on web, blog, social media & marketing projects. Away from computers he can usually be found clad in lycra pretending to be a cyclist.

Latest posts by Chris Mason (see all)

Your email address will not be published. Required fields are marked *