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:
' 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