Table of Contents
Previous Chapter
Chapter 8: calc - Calculation
This record calculates an expression.
--------------------------------------------------------------
Field Type DCT Initial Access Modify Rec Proc PP
Monitor
--------------------------------------------------------------
VAL DOUBLE No 0 Yes Yes Yes No
CALC STRING Yes Null Yes Yes Yes Yes
INPA INLINK Yes 0 No No N/A No
INPB INLINK Yes 0 No No N/A No
INPC INLINK Yes 0 No No N/A No
INPD INLINK Yes 0 No No N/A No
INPE INLINK Yes 0 No No N/A No
INPF INLINK Yes 0 No No N/A No
INPG INLINK Yes 0 No No N/A No
INPH INLINK Yes 0 No No N/A No
INPI INLINK Yes 0 No No N/A No
INPJ INLINK Yes 0 No No N/A No
INPK INLINK Yes 0 No No N/A No
INPL INLINK Yes 0 No No N/A No
A DOUBLE No 0 Yes Yes/No Yes Yes
B DOUBLE No 0 Yes Yes/No Yes Yes
C DOUBLE No 0 Yes Yes/No Yes Yes
D DOUBLE No 0 Yes Yes/No Yes Yes
E DOUBLE No 0 Yes Yes/No Yes Yes
F DOUBLE No 0 Yes Yes/No Yes Yes
G DOUBLE No 0 Yes Yes/No Yes Yes
H DOUBLE No 0 Yes Yes/No Yes Yes
I DOUBLE No 0 Yes Yes/No Yes Yes
J DOUBLE No 0 Yes Yes/No Yes Yes
K DOUBLE No 0 Yes Yes/No Yes Yes
L DOUBLE No 0 Yes Yes/No Yes Yes
LA DOUBLE No 0 Yes No No No
LB DOUBLE No 0 Yes No No No
LC DOUBLE No 0 Yes No No No
LD DOUBLE No 0 Yes No No No
LE DOUBLE No 0 Yes No No No
LF DOUBLE No 0 Yes No No No
LG DOUBLE No 0 Yes No No No
LH DOUBLE No 0 Yes No No No
LI DOUBLE No 0 Yes No No No
LJ DOUBLE No 0 Yes No No No
LK DOUBLE No 0 Yes No No No
LL DOUBLE No 0 Yes No No No
EGU STRING Yes Null Yes Yes No No
PREC SHORT Yes 0 Yes Yes No No
HOPR FLOAT Yes 0 Yes Yes No No
LOPR FLOAT Yes 0 Yes Yes No No
HIHI FLOAT Yes 0 Yes Yes No Yes
LOLO FLOAT Yes 0 Yes Yes No Yes
HIGH FLOAT Yes 0 Yes Yes No Yes
LOW FLOAT Yes 0 Yes Yes No Yes
HHSV GBLCHOICE Yes 0 Yes Yes No Yes
LLSV GBLCHOICE Yes 0 Yes Yes No Yes
HSV GBLCHOICE Yes 0 Yes Yes No Yes
LSV GBLCHOICE Yes 0 Yes Yes No Yes
HYST DOUBLE Yes 0 Yes Yes No No
ADEL DOUBLE Yes 0 Yes Yes No No
MDEL DOUBLE Yes 0 Yes Yes No No
LALM DOUBLE No 0 Yes No No No
ALST DOUBLE No 0 Yes No No No
MLST DOUBLE No 0 Yes No No No
RPCL NOACCESS No 0 No No No
--------------------------------------------------------------
-------------------------------------------------------------------------------------------------
Name Summary Description
-------------------------------------------------------------------------------------------------
VAL Value Field This field is calculated, via the CALC expression, each
time the record is processed.
CALC Infix Expression See below for details
INPA,...,INPL Input Links Each may be a constant, a database link, or a channel
access link. Any link not defined is ignored.
A,...,L Input Values If the corresponding INP field is a constant, this field is
initialized with the constant value but can be changed via
dbPuts.
LA,...,LL Previous Input These fields are used to decide when to trigger monitors
Values on A,...,L.
EGU Engineering Units A 16 character ASCII string describing Engineering units.
This field is used by record support to supply a units
description string when get_units is called.
PREC Display Precision Precision with which to display VAL. This field is used by
record support to supply a value when get_precision
is called.
HOPR High Operating These fields determine the upper and lower display limits
Range for graphics displays and the upper and lower control
limits for control displays. The fields are used by record
support to honor calls to get_graphic_double or
get_control_double.
LOPR Low Operating
Range
HIHI Hihi Alarm Limit These fields specify the alarm limits and severities.
HIGH High Alarm Limit
LOW Low Alarm Limit
LOLO Lolo Alarm Limit
HHSV Severity for a Hihi
Alarm
HSV Severity for a High
Alarm
LSV Severity for a Low
Alarm
LLSV Severity for a Lolo
Alarm
HYST Alarm Deadband These parameters specify hysteresis factors for triggering
monitor callbacks, i.e. monitors specified by calls to
caAddEvent or dbAddEvent. A monitor will not be
triggered until VAL changes by more than the specified
amount.
ADEL Archive Deadband
MDEL Monitor, i.e. value
change, Deadband
LALM Last Alarmed Values when monitors were last triggered. These fields
Value are used to implement the hysteresis factors for monitors.
ALST Archive Last Value
MLST Monitor Last Value
RPCL Expression in
reverse polish
-------------------------------------------------------------------------------------------------
init_record
For each constant input link, the corresponding value field is initialized with the constant value if the input link is CONSTANT or a channel access link is created if the input link is PV_LINK.
A routine postfix is called to convert the infix expression in CALC to reverse polish notation. The result is stored in RPCL.
process
See next section.
special
This is called if CALC is changed. special calls postfix.
get_value
Fills in the values of struct valueDes so that they refer to VAL.
get_units
Retrieves EGU.
get_precision
Retrieves PREC.
get_graphic_double
Sets the upper display and lower display limits for a field. If the field is VAL, HIHI, HIGH, LOW, or LOLO, the limits are set to HOPR and LOPR, else if the field has upper and lower limits defined they will be used, else the upper and lower maximum values for the field type will be used.
get_control_double
Sets the upper control and the lower control limits for a field. If the field is VAL, HIHI, HIGH, LOW, or LOLO, the limits are set to HOPR and LOPR, else if the field has upper and lower limits defined they will be used, else the upper and lower maximum values for the field type will be used.
get_alarm_double
Sets the following values:
upper_alarm_limit = HIHI
upper_warning_limit = HIGH
lower_warning_limit = LOW
lower_alarm_limit = LOLO
Routine process implements the following algorithm:
- Fetch all arguments.
- Call routine calcPerform, which calculates VAL from the postfix version of the expression given in CALC. If calcPerform returns success UDF is set to FALSE.
- Check alarms. This routine checks to see if the new VAL causes the alarm status and severity to change. If so, NSEV, NSTA and LALM are set. It also honors the alarm hysteresis factor (HYST). Thus the value must change by at least HYST before the alarm status and severity changes.
- Check to see if monitors should be invoked.
- Alarm monitors are invoked if the alarm status or severity has changed.
- Archive and value change monitors are invoked if ADEL and MDEL conditions are met.
- Monitors for A-L are checked whenever other monitors are invoked.
- NSEV and NSTA are reset to 0.
- Scan forward link if necessary, set PACT FALSE, and return.
The calculation can express algebraic, relational, and logical expressions. The expression is converted to opcode and stored as reverse polish notation in the calculation record. The database fields are as follows:
- CALC: infix expression as entered
- RPCL: reverse polish expression
The reverse polish calculation is most efficient to evaluate during run-time. The range of expressions supported by the calculation record are separated into operands, algebraic operations, trigonometric, relational operations, logical operations, parenthesis, and the question mark operator.
Operands
- A: Use the value specified by input A
- B: Use the value specified by input B
- C: Use the value specified by input C
- D: Use the value specified by input D
- E: Use the value specified by input E
- F: Use the value specified by input F
- G: Use the value specified by input G
- H: Use the value specified by input H
- I: Use the value specified by input I
- J: Use the value specified by input J
- K: Use the value specified by input K
- L: Use the value specified by input L
- RNDM: Random number (unary), random number between 0-1
Algebraic Operators
- ABS: Absolute value (unary)
- SQR: Square root (unary)
- MIN: Minimum (binary function)
- MAX: Maximum (binary function)
- CEIL: Ceiling (unary)
- FLOOR: Floor (unary)
- LOG: Log base 10 (unary)
- LOGE: Natural log (unary)
- EXP: Exponential function (unary)
- ^ : Exponential (binary)
- ** : Exponential (binary)
- + : Addition (binary)
- - : Subtraction (binary)
- * : Multiplication (binary)
- / : Division (binary)
- % : Modulo (binary)
- NOT: Negate (unary)
- - : Subtraction (unary)
- NINT: Nearest Integer
- LN: Natural Log (synonym for LOGE)
- SQRT: Square Root (synonym for SQR)
- PI: 3.1415926...
- D2R: Conversion from Degrees to Radians (Note: Trig functions assume their arguments are in radians) Degrees = Radians * D2R
Trigonometric Operators
- SIN: Sine
- SINH: Hyperbolic sine
- ASIN: Arc sine
- COS: Cosine
- COSH: Hyperbolic cosine
- ACOS: Arc cosine
- TAN: Tangent
- TANH: Hyperbolic tangent
- ATAN: Arc tangent
Relational Operators
- >= : Greater than or equal to
- > : Greater than
- <= : Less than or equal to
- <: Less than
- # : Not equal to
- = : Equal to
Logical Operators
Bitwise Operators
- | : Bitwise Or
- & : Bitwise And
- OR : Bitwise Or
- AND: Bitwise And
- XOR: Bitwise Exclusive Or
- ~ : One's Complement
- << : Left shift
- >> : Right shift
Parenthesis and Comma
The open and close parenthesis are supported. Nested parenthesis are supported.
The comma is supported when used to separate the arguments of a binary function.
Conditional Expression
The "C" question mark operator is supported. The format is:
(condition)? True result : False result
Algebraic
A + B
Relational
(A + B) < (C + D)
- Result is 1 if (A+B) < (C+D)
- Result is 0 if (A+B) >= (C+D)
Question Mark
(A+B)<(C+D)?E:F
- Result is E if (A+B) < (C+D)
- Result is F if (A+B) >= (C+D)
(A+B)<(C+D)?E
- Result is E if (A+B) < (C+D)
- Result is unchanged if (A+B) >= (C+D)
Logical
A&B
- Causes the following to occur:
- Convert A to integer
- Convert B to integer
- Bit-wise And A and B
- Convert result to floating point
Table of Contents
Next Chapter