EPICS Component Reference Manual


EPICS Reference: Calculation Record (calc) §

The calculation or "Calc" record is used to perform algebraic, relational, and logical operations on values retrieved from other records. The result of its operations can then be accessed by another record so that it can then be used.

Since 7.0.10 the number of inputs has been increased from 12 to 21.

Parameter Fields §

The record-specific fields are described below, grouped by functionality.

Scan Parameters §

The Calc record has the standard fields for specifying under what circumstances the record will be processed. These fields are described in Scan Fields.

FieldSummaryTypeDCT DefaultReadWriteCA PP
SCANScan MechanismMENU (menuScan)Yes YesYesNo
PHASScan PhaseSHORTYes YesYesNo
EVNTEvent NameSTRING [40]Yes YesYesNo
PRIOScheduling PriorityMENU (menuPriority)Yes YesYesNo
PINIProcess at iocInitMENU (menuPini)Yes YesYesNo

Read Parameters §

The read parameters for the Calc record consist of 21 input links INPA - INPU. The fields can be database links, channel access links, or constants. If they are links, they must specify another record's field or a channel access link. If they are constants, they will be initialized with the value they are configured with and can be changed via dbPuts. They cannot be hardware addresses.

See Address Specification for information on how to specify database links.

FieldSummaryTypeDCT DefaultReadWriteCA PP
INPAInput AINLINKYes YesYesNo
INPUInput UINLINKYes YesYesNo

Expression §

At the core of the Calc record lies the CALC and RPCL fields. The CALC field holds an infix expression to be evaluated whenever the record is processed. The calculated value is placed in the VAL field and can be accessed from there.

The CALC expression gets compiled into a stream of Reverse Polish Notation (RPN) opcodes for a stack-based machine, and stored in the RPCL field. The RPN opcodes are used to calculate VAL at run-time, and are more efficient than evaluating the infix expression. The CALC expression can be replaced at run-time, triggering a special record routine to compile the new expression into Reverse Polish Notation.

The infix expressions that can be used are very similar to the C expression syntax, but with some additions and subtle differences in operator meaning and precedence. The string may contain a series of expressions separated by a semi-colon character ;, any one of which may provide the calculation result. All other expressions included in the string must assign their result to a variable. All alphabetic elements described below are case independent, so upper and lower case letters may be used and mixed in the variable and function names as desired. Spaces may be used anywhere within an expression except between characters that make up a single expression element.

The range of expressions supported by the calculation record are separated into literals, constants, operands, algebraic operators, trigonometric operators, relational operators, logical operators, the assignment operator, parentheses and commas, and the question mark colon or ?: operator.

FieldSummaryTypeDCT DefaultReadWriteCA PP
CALCCalculationSTRING [160]Yes YesYesYes
RPCLReverse Polish CalcNOACCESSNo NoNoNo

Literals §

Constants §

Operands §

The expression uses the values retrieved from the INPx links as operands, though constants can be used as operands too. These values retrieved from the input links are stored in the A-U fields. The values to be used in the expression are simply referenced by the field letter. For instance, the value obtained from INPA link is stored in the field A, and the value obtained from INPB is stored in field B. The field names can be included in the expression which will operate on their respective values, as in A+B. Also, the RNDM nullary function can be included as an operand in the expression in order to generate a random number between 0 and 1.

FieldSummaryTypeDCT DefaultReadWriteCA PP
AValue of Input ADOUBLENo YesYesYes
UValue of Input UDOUBLENo YesYesYes

The keyword VAL returns the current contents of the VAL field (which can be written to by a CA put, so it might not be the result from the last time the expression was evaluated).

Arithmetic Operators §

Except for unary minus these are infix binary operators.

Algebraic Functions §

When functions take more than one argument, a comma separator must appear between them.

Trigonometric Functions §

Hyperbolic Trigonometry Functions §

Numeric Functions §

Boolean/Logical Operators §

These operators use their arguments as a true (non-zero) or false (zero) value.

Bitwise Operators §

Mostly infix binary, the arguments are converted to a 32-bit integer, the operator is applied, and the result converted back into a double.

Relational Operators §

These are all infix binary operators.

Assignment Operator §

Parentheses, Comma, and Semicolon §

The open ( and close parentheses ) are supported to override precedence rules in a sub-expression. Nested parentheses are supported to significant depth.

The comma , is required to separate the arguments of a function.

The semicolon ; is used to value separate expressions. Exactly one value expression must be present, but multiple assignment expressions may be included before and/or after the value expression.

Conditional Operator §

The C language's question mark colon ?: ternary operator is supported. The format is:

condition ? true-expression : false-expression

Expression Examples §

Algebraic §

A + B + 10

Relational §

(A + B) < (C + D)

Question Mark §

(A + B) < (C + D) ? E : F + L + 10

Prior to Base 3.14.9 it was legal to omit the colon : and the second (else) part of the conditional, like this:

(A + B)<(C + D) ? E

Logical §

A & B

Assignment §

sin(a); a:=a+D2R

Operator Display Parameters §

These parameters are used to present meaningful data to the operator. These fields are used to display VAL and other parameters of the calculation record either textually or graphically.

The EGU field contains a string of up to 16 characters which is supplied by the user and which describes the values being operated upon. The string is retrieved whenever the routine get_units is called. The EGU string is solely for an operator's sake and does not have to be used.

The HOPR and LOPR fields only refer to the limits of the VAL, HIHI, HIGH, LOW and LOLO fields. PREC controls the precision of the VAL field.

See Fields Common to All Record Types for more on the record name (NAME) and description (DESC) fields.

FieldSummaryTypeDCT DefaultReadWriteCA PP
EGUEngineering UnitsSTRING [16]Yes YesYesNo
PRECDisplay PrecisionSHORTYes YesYesNo
HOPRHigh Operating RngDOUBLEYes YesYesNo
LOPRLow Operating RangeDOUBLEYes YesYesNo
NAMERecord NameSTRING [61]No YesNoNo
DESCDescriptorSTRING [41]Yes YesYesNo

Alarm Parameters §

The possible alarm conditions for the Calc record are the SCAN, READ, Calculation, and limit alarms. The SCAN and READ alarms are called by the record support routines. The Calculation alarm is called by the record processing routine when the CALC expression is an invalid one, upon which an error message is generated.

The following alarm parameters which are configured by the user, define the limit alarms for the VAL field and the severity corresponding to those conditions.

The HYST field defines an alarm deadband for each limit.

The AFTC field sets the time constant on a low-pass filter that delays the reporting of limit alarms until the signal has been within the alarm range for that number of seconds (the default AFTC value of zero retains the previous behavior).

See Alarm Specification for a complete explanation of record alarms and of the standard fields. Alarm Fields lists other fields related to alarms that are common to all record types.

FieldSummaryTypeDCT DefaultReadWriteCA PP
HIHIHihi Alarm LimitDOUBLEYes YesYesYes
HIGHHigh Alarm LimitDOUBLEYes YesYesYes
LOWLow Alarm LimitDOUBLEYes YesYesYes
LOLOLolo Alarm LimitDOUBLEYes YesYesYes
HHSVHihi SeverityMENU (menuAlarmSevr)Yes YesYesYes
HSVHigh SeverityMENU (menuAlarmSevr)Yes YesYesYes
LSVLow SeverityMENU (menuAlarmSevr)Yes YesYesYes
LLSVLolo SeverityMENU (menuAlarmSevr)Yes YesYesYes
HYSTAlarm DeadbandDOUBLEYes YesYesNo
AFTCAlarm Filter Time ConstantDOUBLEYes YesYesNo

Monitor Parameters §

These paramaeters are used to determine when to send monitors for the value fields. These monitors are sent when the value field exceeds the last monitored field by the appropriate deadband, the ADEL for archiver monitors and the MDEL field for all other types of monitors. If these fields have a value of zero, every time the value changes, monitors are triggered; if they have a value of -1, every time the record is scanned, monitors are triggered. See "Monitor Specification" for a complete explanation of monitors.

FieldSummaryTypeDCT DefaultReadWriteCA PP
ADELArchive DeadbandDOUBLEYes YesYesNo
MDELMonitor DeadbandDOUBLEYes YesYesNo

Run-time Parameters §

These fields are not configurable using a configuration tool and none are modifiable at run-time. They are used to process the record.

The LALM field is used to implement the hysteresis factor for the alarm limits.

The LA-LU fields are used to decide when to trigger monitors for the corresponding fields. For instance, if LA does not equal the value A, monitors for A are triggered. The MLST and ALST fields are used in the same manner for the VAL field.

FieldSummaryTypeDCT DefaultReadWriteCA PP
LALMLast Value AlarmedDOUBLENo YesNoNo
ALSTLast Value ArchivedDOUBLENo YesNoNo
MLSTLast Val MonitoredDOUBLENo YesNoNo
LAPrev Value of ADOUBLENo YesNoNo
LUPrev Value of UDOUBLENo YesNoNo

Record Support §

Record Support Routines §

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 a 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_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 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

Record Processing §

Routine process implements the following algorithm:

  1. Fetch all arguments.

  2. 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.

  3. 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.

  4. Check to see if monitors should be invoked.

    • Alarm monitors are invoked if the alarm status or severity has changed.

    • Archive and values change monitors are invoked if ADEL and MDEL conditions are met.

    • Monitors for A-U are checked whenever other monitors are invoked.

    • NSEV and NSTA are reset to 0.

  5. Scan forward link if necessary, set PACT FALSE, and return.


EPICS Component Reference Manual