tags:

views:

87

answers:

2

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.

+1  A: 

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  |
slebetman
Gotcha. I have three basic states for the first two inputs. How would I determine then which input was hit first if going in reverse? For instance, IN 1 ON, IN 2 OFF. Then IN 1 OFF, IN 2 ON, but then IN 1 ON, IN 2 OFF. What capabilities do I have to determine direction in this case?
0A0D
I still don't understand. Given the sequence you gave, assuming (1,2), what do you expect 10->01->10 to output? With real-world switches, does 10->00->01->00->10 do the same thing? And what do you mean by three states? You've only given two states: 10 and 01.
slebetman
If the person enters the beams correctly, but then backs out, the gate should close automatically as long as the safety is not engaged.. I need to track states so I know what was the last sequence
0A0D
Are there two beams? Is that what you mean?
slebetman
0A0D
@slebetman: Just wanted to say thank you again. I got this PLC project dropped on my lap and I have never programmed a PLC in my life. I have a CS degree and have been a software engineer for 7 years but never had to deal with PLCs before.. thanks again!
0A0D
@slebetman +1 for ASCII art ladder logic! Nicely done.
Andy
+1  A: 

Using the DirectLogic programmable controllers, there is a differential input that would make this very easy. I would be willing to bet that most PLC's have similar instructions.

However, if you are using DirectLogic PLC's, their RLL-Plus stage programming would be a much clearer implementation of "stateful" programming within the ladder logic realm.

The positive differential would be used to execute output logic when the input goes from low to high. It is only true for one cycle, so it is possible you would need to engage a latch or utilize a "set". This depends on the photocells being overlapping:

|
|----] _| [------] [----------------------( set )---|
|      1          2         |       reverse detected
|                           |
|----] _| [------] [--------|
|      2          3         |  
|                           |
|----] _| [------] [--------|
|      3          4         |  
|                           |
|----] _| [------] [--------|
|      4          5         |
|                           |
|----] _| [------] [--------|
|      5          6         
|
|

In this case, if 2 is on, and 1 goes high, you set or latch the reverse detection bit. Or'ing the input logic with each possible reversal ( 2 goes high while 3 is on) would keep this down to a single rung if you care about such things.

I wasn't clear on whether or not there is overlap in the photocells. If not overlapping, I might do this more like slebetman's answer:

|
|----]|_ [-------------------------------( set )---|
|      2                                2 exited
|                           
|----]|_ [-------------------------------( set )---|
|      3                             |  3 exited
|                                    |
|                                    |---( rst )---|
|                                       2 exited
|
|
|----]|_ [-------------------------------( set )---|
|      4                             |  4 exited
|                                    |
|                                    |---( rst )---|
|                                       3 exited
|
|
|----]|_ [-------------------------------( set )---|
|      5                             |  5 exited
|                                    |
|                                    |---( rst )---|
|                                       4 exited
|
|
|----]|_ [-------------------------------( set )---|
|      6                             |  6 exited
|                                    |
|                                    |---( rst )---|
|                                       5 exited
|
|
|----] _| [------] [----------------------( set )---|
|      1      2 exited      |       reverse detected
|                           |
|----] _| [------] [--------|
|      2      3 exited      |  
|                           |
|----] _| [------] [--------|
|      3      4 exited      |  
|                           |
|----] _| [------] [--------|
|      4      5 exited      |  
|                           |
|----] _| [------] [--------|
|      5      6 exited      
|                           

From the Manual:

The And Positive Differential instruction logically ANDs a normally open contact in series with another contact in a rung. The status of the contact will be open until the associated image register point makes an Off- to-On transition, closing it for one CPU scan. Thereafter, it remains open until another Off-to-On transition.

arriflex