Search code examples
logicplc

Directional Logic PLC


How do you determine direction of inputs using ladder diagrams with a PLC? Meaning, how do you save the previous state?

Previous state of inputs. I need to determine direction that photobeams were activated.. forward or reverse. If they are activated in reverse, perform one action. If they are activated forwards, perform a different action. Inputs labeled 1 through 6. Normal direction is 1 through 6.


Solution

  • Here's a simple implementation of a latch in ladder logic:

    |-----[ ]-----+-----------------( )--------|
    |    input    |                output      |
    |             |                            |
    |-----[ ]-----'                            |
         output
    

    and here's one where you can reset the output:

    |-----[ ]-------------+---------( )--------|
    |    input            |        output      |
    |                     |                    |
    |-----[ ]-----[/]-----'                    |
        output   reset
    

    These form the fundamental building blocks for memory in ladder logic. I'm not sure but is this what you're looking for?

    Usually a language implementing ladder logic will have higher level elements that implement memory such as D and T flip-flops. Read the documentation of your ladder logic implementation to see if they're available.

    OK, from your comments it looks like what you want is:

    // Pseudocode:
    // a = sensor 1
    // b = sensor 2
    
    if (a) {
        a_triggered = true;
    }
    
    if (b) {
        if (!a_triggered) {
            REVERSE_DETECTED();
        }
        else {
            a_triggered = false;
        }
    }
    

    This assumes the sensors are close together such that the transition is 10->11->01 such that you can't detect the travel direction while the item is triggering both sensors. Writing this declaratively:

    a_triggered = (a || a_triggered) && !(b_triggered && !b);
    b_triggered = (b || b_triggered) && a_triggered;
    reverse_detected = b && !a_triggered;
    

    Which translates to:

    |-----[ ]---------+-----[/]--------( )--------|
    |      a          |      c     a_triggered    |
    |                 |                           |
    |-----[ ]---------'                           |
    |  a_triggered                                |
    |                                             |
    |-----[ ]---------+-----[ ]--------( )--------|
    |      b          | a_triggered  b_triggered  |
    |                 |                           |
    |-----[ ]---------'                           |
    |  b_triggered                                |
    |                                             |
    |-----[ ]----------[/]-------------( )--------|
    |  b_triggered      b               c         |
    |                                             |
    |-----[ ]----------[/]-------------( )--------|
    |      b      a_triggered   reverse_detected  |
    

    Now you can use the reverse detected signal to do what you want. If your ladder language has latches you can do this cleaner:

    |                             _________       |
    |-----[ ]--------------------|set latch|------|
    |      a                     |         |      |
    |-----[ ]--------------------|clear    |      |
    |      c                     |_________|      |
    |                            a_triggered      |
    |                             _________       |
    |-----[ ]--------------------|set latch|------|
    |      b                     |         |      |
    |-----[/]--------------------|clear    |      |
    |  a_triggered               |_________|      |
    |                            b_triggered      |
    |                                             |
    |-----[ ]----------[/]-------------( )--------|
    |  b_triggered      b               c         |
    |                                             |
    |-----[ ]----------[/]-------------( )--------|
    |      b      a_triggered   reverse_detected  |