You are a process engineer trying to design a filter for noisy level signal. You want to…
- User
You are a process engineer trying to design a filter for noisy level signal. You want to create a spike rejection filter in an Allen Bradley PLC. The scan time of the routine is 1hz. On every scan, the filter will need to evaluate the new input compared to the previous scan and if it is outside of a plus or minus window of 3, reject the new value and hold the previous value. Allow this to occur for up to 30 scans before allowing the next value past the filter. This would result in a step change to the new value but that is acceptable.
- Assistant
To design a spike rejection filter in an Allen-Bradley PLC that meets your requirements, you'll need to create a logic that compares the new input with the previous value, accepts or rejects the new value based on a defined window, and incorporates a counter to override the rejection after a certain number of scans.
Below is a step-by-step guide on how to implement this filter:
1. Understanding the Requirements
- Sampling Rate: The PLC routine scans at 1 Hz (once every second).
- Acceptance Criteria:
- If the new input is within ±3 units of the previous value, accept it.
- If not, reject the new value and hold the previous one.
- Override Condition:
- After rejecting values for 30 consecutive scans, accept the new value regardless of the difference.
- Result:
- Step change to the new value after 30 scans of rejection is acceptable.
2. Define Variables and Tags
You will need the following variables:
- Input Variables:
New_Input(REAL or appropriate data type): The latest signal reading.
- Output Variables:
Filtered_Value(REAL): The value after filtering; this is what your system will use.
- Internal Variables:
Previous_Value(REAL): Stores theFiltered_Valuefrom the last scan.Difference(REAL): The difference betweenNew_InputandFiltered_Value.Reject_Counter(DINT): Counts the number of consecutive rejections.First_Scan(BOOL): System variable indicating the first scan of the PLC.Rejection_Flag(BOOL): Indicates if the value is currently being rejected.
3. Implement the Logic
Step 1: Initialization on First Scan
On the first scan of the PLC after startup or program download, initialize the
Filtered_Valueand reset theReject_Counter.Ladder Logic:
- Rung 0:
- Condition:
First_Scanis TRUE. - Actions:
Filtered_Value:=New_InputReject_Counter:= 0Rejection_Flag:= FALSE
- Condition:
Step 2: Calculate the Difference
Calculate the absolute difference between the
New_Inputand theFiltered_Value.- Rung 1:
- Action:
Difference:= ABS(New_Input-Filtered_Value)
- Action:
Step 3: Decision Making
Case 1: Difference Within Acceptable Range
If the difference is within ±3 units, accept the new input.
- Rung 2:
- Condition:
Difference≤ 3 - Actions:
Filtered_Value:=New_InputReject_Counter:= 0Rejection_Flag:= FALSE
- Condition:
Case 2: Difference Outside Acceptable Range
If the difference exceeds ±3 units, decide whether to reject it or accept it based on the
Reject_Counter.-
Rung 3:
- Condition:
Difference> 3 ANDReject_Counter< 30 - Actions:
Reject_Counter:=Reject_Counter+ 1Rejection_Flag:= TRUEFiltered_Valueremains unchanged (holds previous value)
- Condition:
-
Rung 4:
- Condition:
Difference> 3 ANDReject_Counter≥ 30 - Actions:
Filtered_Value:=New_Input(forces acceptance after 30 scans)Reject_Counter:= 0Rejection_Flag:= FALSE
- Condition:
4. Detailed Ladder Logic Example
Below is how the logic would look in ladder logic:
Rung 0: Initialization
| First_Scan |----[XIC]--------------------------------------------| | | |---[MOV]---New_Input---Filtered_Value---| | |---[MOV]---0--------Reject_Counter------| | |---[OTU]---Rejection_Flag--------------| // Unlatch Rejection_Flag |Rung 1: Calculate Difference
| | |---[Compute] Difference = ABS(New_Input - Filtered_Value)---|Rung 2: Accept New Value If Within Range
| Difference <= 3 |----[XIC]----------------------------| | | |---[MOV]---New_Input---Filtered_Value---| | |---[MOV]---0--------Reject_Counter------| | |---[OTU]---Rejection_Flag--------------| |Rung 3: Reject Value and Increment Counter
| Difference > 3 | Reject_Counter < 30 | | |----[XIC]-------------[XIC]----------------------| | | |---[ADD]---Reject_Counter + 1---Reject_Counter---| |---[OTL]---Rejection_Flag------------------------| | // Filtered_Value remains unchanged |Rung 4: Accept New Value After 30 Rejections
| Difference > 3 | Reject_Counter ≥ 30 | | |----[XIC]-------------[XIC]----------------------| | | |---[MOV]---New_Input---Filtered_Value---| | |---[MOV]---0--------Reject_Counter------| | |---[OTU]---Rejection_Flag--------------| |
5. Additional Considerations
Handling Data Types
- Use appropriate data types for your PLC platform:
REALorFLOATfor analog values.DINTorINTfor integer counters.BOOLfor flags.
First Scan Initialization
- The
First_Scanbit is typically a system-defined bit in Allen-Bradley PLCs (e.g.,S:FSin older PLCs orProgram:FirstScanin Studio 5000). - This ensures proper initialization when the PLC starts.
Timer Alternative
- Since your scan rate is 1 Hz, the
Reject_Countereffectively counts seconds. - If your scan rate is variable, consider using a timer that accumulates time up to 30 seconds.
Rejection Flag Usage
- The
Rejection_Flagcan be used to trigger alarms or notifications if needed. - It indicates that the input value is being rejected due to a spike.
Edge Cases
- Returning to Acceptable Range Before 30 Scans:
- If the
New_Inputreturns to within ±3 units before reaching 30 rejections, the filter resets, accepting new inputs normally.
- If the
6. Testing the Logic
Scenario 1: Input Within Range
- Input:
New_Inputchanges within ±3 units. - Expected Result:
Filtered_Valueupdates toNew_Input;Reject_Counterresets to 0.
Scenario 2: Spike Occurs
- Input: Sudden change in
New_Inputbeyond ±3 units. - Expected Result:
- For the first 30 scans:
Filtered_Valueremains unchanged.Reject_Counterincrements each scan.
- On the 31st scan:
Filtered_Valueupdates toNew_Input.Reject_Counterresets to 0.
- For the first 30 scans:
Scenario 3: Spike Stops Before 30 Scans
- Input:
New_Inputreturns to within ±3 units before 30 scans. - Expected Result:
Filtered_Valueupdates toNew_Inputimmediately upon returning within range.Reject_Counterresets to 0.
7. Optimization
Create a Reusable Function Block (Optional)
- If your Allen-Bradley PLC supports Add-On Instructions (AOIs) or Function Blocks, consider encapsulating this logic into a reusable component.
- This promotes code reuse and simplifies maintenance.
Monitoring and Diagnostics
- Implement logic to monitor the
Reject_CounterandRejection_Flagfor diagnostic purposes. - You can log events or display messages when spikes are detected.
8. Conclusion
By implementing the logic as described, you'll have a spike rejection filter that:
- Smoothly filters out temporary spikes in the level signal.
- Prevents abrupt changes due to noise or erroneous readings.
- Allows genuine significant changes to be recognized after a continuous period, ensuring critical changes are not missed.
This filter ensures stability in your process control while still being responsive to necessary changes in the level signal.
Example Overview
Here is a simplified representation of the logic flow:
-
Initialization:
- Set
Filtered_Valueto the firstNew_Input. - Reset
Reject_CounterandRejection_Flag.
- Set
-
Each Scan:
- Calculate
Difference= ABS(New_Input-Filtered_Value). - If
Difference≤ 3:- Accept
New_Inputby updatingFiltered_Value. - Reset
Reject_CounterandRejection_Flag.
- Accept
- Else:
- If
Reject_Counter< 30:- Increment
Reject_Counter. - Maintain
Filtered_Value. - Set
Rejection_Flag.
- Increment
- Else:
- Accept
New_Inputdespite the spike. - Reset
Reject_CounterandRejection_Flag.
- Accept
- If
- Calculate
By following these guidelines, you'll successfully implement the spike rejection filter in your Allen-Bradley PLC to enhance the reliability of your level signal readings.