lang="en-US"UTF-8 Using global variables, variable arithmetic & script fragments - Prosig Support Blog class="post-template-default single single-post postid-2873 single-format-standard custom-background custom-background-image group-blog blogolife-3_0_7 unknown"

Prosig Support Blog

The place to come for support for Prosig's DATS, P8000 & PROTOR products

Prosig Support Blog
2873class="post-2873 post type-post status-publish format-standard hentry category-dats category-tutorial tag-analysis tag-dats tag-exponentiation tag-expression tag-prosig tag-script-fragment tag-script-fragments tag-simple-arithmetic tag-square-root tag-squares tag-trigonometry tag-variables"

Using global variables, variable arithmetic & script fragments

fa fa-file-text-o

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.

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)

Share the knowledge:

Chris Mason

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

Proudly powered byWordPressDesign byBlogoLife