Moving Average (MAVE)
This information applies to the CompactLogix 5370, ControlLogix 5570, CompactGuardLogix 5370, GuardLogix 5570, CompactGuardLogix 5380, CompactLogix 5380, CompactLogix 5480, ControlLogix 5580, and GuardLogix 5580 controllers.
The Moving Average (MAVE) instruction calculates a time average value for the In signal. This instruction optionally supports user-specified weights.
Available Languages
Ladder Diagram
This instruction is not available in ladder diagram logic.
Function Block
Structured Text
MAVE(MAVE_tag,storage,weight);
Operands
Function Block
Operand | Type | Format | Description |
MAVE tag | MOVING_AVERAGE | structure | MAVE structure |
storage | REAL | array | Holds the moving average samples. This array must be at least as large as NumberOfSamples. |
weight | REAL | array | (optional)
Used for weighted averages. This array must be at least as large as NumberOfSamples. Element [0] is used for the newest sample; element [n] is used for the oldest sample. |
MOVING_AVERAGE Structure
Input Parameter | Data Type | Description |
EnableIn | BOOL | Enable input. If cleared, the instruction does not execute and outputs are not updated.
Default is set. |
In | REAL | The analog signal input to the instruction.
Valid = any float
Default = 0.0 |
InFault | BOOL | Bad health indicator for the input. If In is read from an analog input, then InFault is normally controlled by fault status on the analog input. When set, InFault indicates the input signal has an error, the instruction sets the appropriate bit in Status, and the instruction holds Out at its current value. When InFault transitions from set to cleared, the instruction initializes the averaging algorithm and continues executing.
Default is cleared. |
Initialize | BOOL | Initialize input to the instruction. When set, the instruction holds Out = In, except when InFault is set, in which case, the instruction holds Out at its current value. When Initialize transitions from set to cleared, the instruction initializes the averaging algorithm and continues executing.
Default is cleared. |
SampleEnable | BOOL | Enable for taking a sample of In. When set, the instruction enters the value of In into the storage array and calculates a new Out value. When SampleEnable is cleared and Initialize is cleared, the instruction holds Out at its current value.
Default is set. |
NumberOfSamples | DINT | The number of samples to be used in the calculation. If this value is invalid, the instruction sets the appropriate bit in Status and holds Out at its current value. When NumberOfSamples becomes valid again, the instruction initializes the averaging algorithm and continues executing.
Valid = 1 to (minimum size of StorageArray or WeightArray, if used)
Default = 1 |
UseWeights | BOOL | Averaging scheme input to the instruction. When set, the instruction uses the weighted method to calculate the Out. When cleared, the instruction uses the uniform method to calculate Out.
Default is cleared. |
Output Parameter | Data Type | Description |
EnableOut | BOOL | Indicates if instruction is enabled. Cleared to false if Out overflows. |
Out | REAL | The calculated output of the algorithm. |
Status | DINT | Status of the function block. |
InstructFault (Status.0) | BOOL | The instruction detected one of the following execution errors. This is not a minor or major controller error. Check the remaining status bits to determine what occurred. |
InFaulted (Status.1) | BOOL | In health is bad (InFault is set). |
NumberOfSampInv (Status.2) | BOOL | NumberOfSamples invalid or not compatible with array size. |
Structured Text
Operand | Type | Format | Description |
MAVE tag | MOVING_AVERAGE | structure | MAVE structure |
storage | REAL | array | Holds the moving average samples. This array must be at least as large as NumberOfSamples. |
weight | REAL | array | (optional)
Used for weighted averages. This array must be at least as large as NumberOfSamples. Element [0] is used for the newest sample; element [n] is used for the oldest sample. |
MOVING_AVERAGE Structure
Input Parameter | Data Type | Description |
EnableIn | BOOL | Enable input. If cleared, the instruction does not execute and outputs are not updated.
Default is set. |
In | REAL | The analog signal input to the instruction.
Valid = any float
Default = 0.0 |
InFault | BOOL | Bad health indicator for the input. If In is read from an analog input, then InFault is normally controlled by fault status on the analog input. When set, InFault indicates the input signal has an error, the instruction sets the appropriate bit in Status, and the instruction holds Out at its current value. When InFault transitions from set to cleared, the instruction initializes the averaging algorithm and continues executing.
Default is cleared. |
Initialize | BOOL | Initialize input to the instruction. When set, the instruction holds Out = In, except when InFault is set, in which case, the instruction holds Out at its current value. When Initialize transitions from set to cleared, the instruction initializes the averaging algorithm and continues executing.
Default is cleared. |
SampleEnable | BOOL | Enable for taking a sample of In. When set, the instruction enters the value of In into the storage array and calculates a new Out value. When SampleEnable is cleared and Initialize is cleared, the instruction holds Out at its current value.
Default is set. |
NumberOfSamples | DINT | The number of samples to be used in the calculation. If this value is invalid, the instruction sets the appropriate bit in Status and holds Out at its current value. When NumberOfSamples becomes valid again, the instruction initializes the averaging algorithm and continues executing.
Valid = 1 to (minimum size of StorageArray or WeightArray, if used)
Default = 1 |
UseWeights | BOOL | Averaging scheme input to the instruction. When set, the instruction uses the weighted method to calculate the Out. When cleared, the instruction uses the uniform method to calculate Out.
Default is cleared. |
Output Parameter | Data Type | Description |
EnableOut | BOOL | Indicates if instruction is enabled. Cleared to false if Out overflows. |
Out | REAL | The calculated output of the algorithm. |
Status | DINT | Status of the function block. |
InstructFault (Status.0) | BOOL | The instruction detected one of the following execution errors. This is not a minor or major controller error. Check the remaining status bits to determine what occurred. |
InFaulted (Status.1) | BOOL | In health is bad (InFault is set). |
NumberOfSampInv (Status.2) | BOOL | NumberOfSamples invalid or not compatible with array size. |
See Structured Text Syntax for more information on the syntax of expressions within structured text.
Description
The MAVE instruction calculates a weighted or non-weighted moving average of the input signal. The NumberOfSamples specifies the length of the moving average span. At every scan of the block when Sample Enable is set, the instruction moves the value of In into the storage array and discards the oldest value. Each In
n
has a user-configured Weightn
, which is used if UseWeights is set.Condition | Action |
Weighted averaging method
UseWeights is set. | |
Uniform averaging method
UseWeights is cleared. |
The instruction will not place an invalid In value (NAN or ± INF) into the storage array. When In is invalid, the instruction sets Out = In and logs an overflow minor fault, if this reporting is enabled. When In becomes valid, the instruction initializes the averaging algorithm and continues executing.
You can make runtime changes to the NumberOfSamples parameter. If you increase the number, the instruction incrementally averages new data from the current sample size to the new sample size. If you decrease the number, the instruction recalculates the average from the beginning of the sample array to the new NumberOfSamples value.
Initializing the Averaging Algorithm
Certain conditions, such as instruction first scan and instruction first run, require the instruction to initialize the moving average algorithm. When this occurs, the instruction considers the sample StorageArray empty and incrementally averages samples from 1 to the NumberOfSamples value. For example:
Affects Math Status Flags
No
Major/Minor Faults
None specific to this instruction. See Common Attributes for operand-related faults.
Execution
Function Block
Condition/State | Action Taken |
Prescan | EnableIn and EnableOut bits are cleared to false. |
Tag.EnableIn is false | Request re-initialization of Storage array the next time the instruction executes. |
Tag.EnableIn is true | EnableIn and EnableOut bits are set to true. The instruction executes. |
Instruction first run | N/A |
Instruction first scan | Initialize Out to zero. |
Postscan | EnableIn and EnableOut bits are cleared to false. |
Structured Text
Condition/State | Action Taken |
Prescan | See Prescan in the Function Block table. |
Normal Execution | See Tag.EnableIn is true in the Function Block table. |
Postscan | See Postscan in the Function Block table. |
Example
Each scan, the instruction places input_value in array storage. The instruction calculates the average of the values in array storage, optionally using the weight values in array weight, and places the result in Out.
Function Block
Structured Text
MAVE_01.In := input_value;
MAVE(MAVE_01,storage,weight);
output_value := MAVE_01.Out;
Provide Feedback