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
  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 at Prosig
Chris fell in love with technology and innovation in the early days of personal computing (Sinclair ZX80's, Commodore PETs & Apple ]['s) This hobby became a career in software development, product development, team leadership, web development, and marketing. He is now General Manager at Prosig & DJB Instruments, both part of Condition Monitoring Technology Group. In 2019 Chris completed an Executive MBA at the University of Winchester where he focused on innovation and strategy.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.