Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

You are a process engineer trying to design a filter for noisy level signal. You want to…

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

  2. 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 the Filtered_Value from the last scan.
      • Difference (REAL): The difference between New_Input and Filtered_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_Value and reset the Reject_Counter.

    Ladder Logic:

    • Rung 0:
      • Condition: First_Scan is TRUE.
      • Actions:
        • Filtered_Value := New_Input
        • Reject_Counter := 0
        • Rejection_Flag := FALSE

    Step 2: Calculate the Difference

    Calculate the absolute difference between the New_Input and the Filtered_Value.

    • Rung 1:
      • Action:
        • Difference := ABS(New_Input - Filtered_Value)

    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_Input
        • Reject_Counter := 0
        • Rejection_Flag := FALSE
    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 AND Reject_Counter < 30
      • Actions:
        • Reject_Counter := Reject_Counter + 1
        • Rejection_Flag := TRUE
        • Filtered_Value remains unchanged (holds previous value)
    • Rung 4:

      • Condition: Difference > 3 AND Reject_Counter ≥ 30
      • Actions:
        • Filtered_Value := New_Input (forces acceptance after 30 scans)
        • Reject_Counter := 0
        • Rejection_Flag := FALSE

    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:
      • REAL or FLOAT for analog values.
      • DINT or INT for integer counters.
      • BOOL for flags.

    First Scan Initialization

    • The First_Scan bit is typically a system-defined bit in Allen-Bradley PLCs (e.g., S:FS in older PLCs or Program:FirstScan in Studio 5000).
    • This ensures proper initialization when the PLC starts.

    Timer Alternative

    • Since your scan rate is 1 Hz, the Reject_Counter effectively counts seconds.
    • If your scan rate is variable, consider using a timer that accumulates time up to 30 seconds.

    Rejection Flag Usage

    • The Rejection_Flag can 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_Input returns to within ±3 units before reaching 30 rejections, the filter resets, accepting new inputs normally.

    6. Testing the Logic

    Scenario 1: Input Within Range

    • Input: New_Input changes within ±3 units.
    • Expected Result: Filtered_Value updates to New_Input; Reject_Counter resets to 0.

    Scenario 2: Spike Occurs

    • Input: Sudden change in New_Input beyond ±3 units.
    • Expected Result:
      • For the first 30 scans:
        • Filtered_Value remains unchanged.
        • Reject_Counter increments each scan.
      • On the 31st scan:
        • Filtered_Value updates to New_Input.
        • Reject_Counter resets to 0.

    Scenario 3: Spike Stops Before 30 Scans

    • Input: New_Input returns to within ±3 units before 30 scans.
    • Expected Result:
      • Filtered_Value updates to New_Input immediately upon returning within range.
      • Reject_Counter resets 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_Counter and Rejection_Flag for 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:

    1. Initialization:

      • Set Filtered_Value to the first New_Input.
      • Reset Reject_Counter and Rejection_Flag.
    2. Each Scan:

      • Calculate Difference = ABS(New_Input - Filtered_Value).
      • If Difference ≤ 3:
        • Accept New_Input by updating Filtered_Value.
        • Reset Reject_Counter and Rejection_Flag.
      • Else:
        • If Reject_Counter < 30:
          • Increment Reject_Counter.
          • Maintain Filtered_Value.
          • Set Rejection_Flag.
        • Else:
          • Accept New_Input despite the spike.
          • Reset Reject_Counter and Rejection_Flag.

    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.