Author Topic: People Counter  (Read 915 times)

Lorne Van Dusen

  • Jr. Member
  • Posts: 87
  • I'm a old guy
    • View Profile
People Counter
« on: February 13, 2020, 09:11:05 AM »
I had some spare time and decided to show a simple people counter circuit that emulates an incremental encoder in a ladder circuit. I am hoping that this may help some new comers to help understand the use of ladder logic as well as some Basic.
This program shows how to store a DM32 set value as well as the DM32 count value into EEPROM and to move the stored value back into the original DM32 areas after the power comes back on. This may not be the most ideal way to count people or things but it does give an example of the use of edge triggered bits as well as latching relays and how to use a DM value as a up down counter.

garysdickinson

  • Expert Member
  • Posts: 464
  • Old PLC Coder
    • View Profile
Re: People Counter
« Reply #1 on: February 14, 2020, 09:44:55 PM »
Lorne,

Nice post. It made me have to do some thinking.

I decided to simplify your code a bit and demo how to use PLC COUNTERs to keep track of the running count. It isn't that hard, but it doesn't handle negative numbers.

The high-speed counter mechanism is probably a better approach than ladder logic for most applications. The biggest limitation is the PLC ladder logic scan rate needs to be significantly faster the rate that the A and B inputs change state.

Best Regards,

Gary Dickinosn
« Last Edit: February 15, 2020, 10:31:25 AM by garysdickinson »

garysdickinson

  • Expert Member
  • Posts: 464
  • Old PLC Coder
    • View Profile
Re: People Counter
« Reply #2 on: February 18, 2020, 01:58:20 PM »
Lorne,

I couldn't sleep the other night and kept thinking that a quadrature decoder could be done with even less code. I got it down to 3 rungs of ladder logic and 3 tiny custom functions.

This is a state machine based approach.  There are no edge triggered events. I use a ladder logic COUNTER to manage the state tracking and a table of data in DM[] that is used to determine when to change state.

Lorne found a bug in the state transition table.  I have corrected this mistake and attached a new version of the .pc6 file.

Best regards,

Gary Dickinson
« Last Edit: February 19, 2020, 09:34:51 AM by garysdickinson »

Lorne Van Dusen

  • Jr. Member
  • Posts: 87
  • I'm a old guy
    • View Profile
Re: People Counter
« Reply #3 on: February 19, 2020, 06:54:28 AM »
Gary

 I didn't mean to make you lose any sleep over such a simple thing. I did the program to help beginners trying to understand Ladder logic programming without using the built in High Speed Counter. After all people don't move that quickly past the two sensors.

The whole concept of the program was to count the people as the entered the building and to make it as fail-safe as possible so if as they entered and only blocked the A pulse then backed up or even if the stopped at both A & B blocked it would drop back to the previous count the same is true for when you try to exit if you block the B pulse or even B & A then back up it will reset the count.
 
I totally understand you state machine concept however I cannot figure out how you can determine how many people have actually entered or exited or what the actual count is. You do have 4 separate counts 0 to 3 which seems to work if you always complete the sequence. However if you trigger A c =1 then trigger B c=2 then drop A c=3 then drop B c=0 However if you trigger A c=1 then B c=2  then drop B c=1 then drop A c still equals 1 so you have actually lost a count.

Now whatever you do don't lose any sleep over this and remember there is always more than one way to program any circuit.

Regards
The other Old Guy

Lorne Van Dusen

  • Jr. Member
  • Posts: 87
  • I'm a old guy
    • View Profile
Re: People Counter
« Reply #4 on: March 16, 2020, 02:00:22 PM »
 :) Gary I finally had some spare time to play with your program.
I am impressed with the way you did it in a state machine and how works.
However I am at a loss on how to determine an actual count value.

garysdickinson

  • Expert Member
  • Posts: 464
  • Old PLC Coder
    • View Profile
Re: People Counter
« Reply #5 on: March 16, 2020, 10:40:15 PM »
Lorne,

In trying to explain how the counting mechanism worked, I realized that my algorithm was not as robust as it could be.

So I modified the code and attached it to this post as V3.

Please look at the state machine diagram in an earlier post: http://triplc.com/smf/index.php?action=dlattach;topic=2429.0;attach=235  as reference.


The idea of the new code is to ensure that the state machine went through all 4 states before updating the count of "people".  I used the #define mechanism to carve out two 32 bit variables:
  • TicCnt - count of the number of states from state 0. This value increments when going clockwise else it decrements
  • TotalCnt - running count of "people"

This is the the "new V3" version of the NextSate CF:
Code: [Select]
' NextState - This CF is called on each new state that is to the "right" of
' The previous state. Think of this as going clockwise through the states.
'
' 0-->1-->2-->3-->0-->1-->2-->3-->0 ...
'
'
' On entry: StateCntr has been updated and represents the current state
'
' On exit:
'
' RELAY[2] is updated the patterns for the next legal states
'
' TicCnt will be updated
' TotalCnt may be incremented
'
RELAY[2] = DM[StateTableBase + CtrPV[StateCntr]]
TicCnt = TicCnt + 1 ' we have advanced clockwise one state

' If the current state is 0. Check to see it we should increment
' the TotalCnt value.
'
If CtrPV[StateCntr] = 0

if TicCnt = 4
TotalCnt = TotalCnt + 1 ' We have gone one full turn
endif

' The TicCnt is reset on transitions to state #0
'
' This makes it easy to "know" when to increment the TotalCnt value.
'
' The other issue is that the PLC scan rate must be significantly faster than
' the objects being counted. As there are 4 states the scan ran has to be
' about 8x faster in order to keep up.
'
TicCnt = 0

endif

The PreviousState CF is substantially similar and deals with the counter-clockwise case.

This is a 1x decoder that only counts once each time the state machine goes full circle.  There are 2x and 4x encoders that count 2x or 4x for complete loops through the state machine.

Best regards,

Gary Dickinson
« Last Edit: March 16, 2020, 10:43:33 PM by garysdickinson »