User-defined functions – MTS Series 793 User Manual

Page 380

Advertising
background image

When using while loops, it is very easy to implement a loop that will run forever. If this happens, the system
will start missing interrupts. This will cause a watchdog timer to fire, causing an interlock. The expression
evaluator will detect this, and stop executing the expression. The signal will be set to invalid, and will remain
that way until Interlock Reset is pressed.

User-Defined Functions

Within the expression of a calculated signal, the user can define new functions. These are only available in
that particular expression.

The syntax is:

function returnType fcnName (arguments)
{

variable declarations;
statements;

}

This is best shown with the following examples:

function int factorial(int value)
{

if (value > 1)

return factorial(value –1) * value;

else

return 1;

}
function real PI()

{return 3.14159;}

function real sum(real A[])
{

int index;
real temp;

temp = 0;
index = 0;
while (index < size(A))

{

temp = temp + A[index];
index = index + 1;

}

return temp;

}

Functions must always have a return type. If the body of the function does not execute a return statement, it
will return a zero.

User-defined functions are called just like other functions.

Some examples:

real myArray[50];
shift(myArray, “My Signal”);
“My Average Signal” = sum(myArray)/size(myArray);

Arrays that are passed to functions are passed by reference, that means that the function can modify the
contents of the array.

Variables declared at the start of the function are created when the function is invoked, and destroyed when
the function exits. This is different than variables declared in the main body of the code. The value of variables
and arrays declared in a function is unpredictable when the function is called. The function body must initialize
them before they are used.

380 MTS Series 793 Control Software

Calculated Signals

Advertising