Author Topic: T100MD+ High Speed Counters  (Read 16958 times)

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
T100MD+ High Speed Counters
« on: October 11, 2009, 09:37:02 PM »
I attempted to interface a mechanical rotary encoder to a T100MD+ PLC.  The goal was to use the encoder for a simple input control for PLC based project.  Just one knob and one or two external push button switches.

These encoders are typically used as volume controls on car radios and many other consumer devices.  The encoder that I was using is a EVQ-WTE series encoder manufactured by Panasonic.  The encoder act just like typical 2-bit quadrature encoders with A/B outputs that are out of phase by 90 degrees.   The encoder has 30 mechanical detents for each revolution of the knob but only produces 15 complete output cycles.

The encoder waveforms look like this:

            +----+----+------ Mechanical Detent
            |     |     |
            V     V     V        
      ---+     +----+     +---
  A      |     |     |     |
         +----+     +----+

         ---+     +----+     +---
  B         |     |     |     |
           +----+      +----+
              ^         ^
              |         |
              +--------+------ HSC counts here

  A/B Phase relation for C.W. Rotation



I tried 2 different approaches to interface this encoder to the PLC:

1.  Ladder logic.  This failed because the "debounce" logic used for the PLC INPUT circuits limits the speed that the the encoder can be operated and the ladder logic scan time is a  too slow to catch all of the transitions on the A/B inputs.

If the knob is turned quickly at a speed of about 1 RPM the phase difference of A/B can be as little a 2.5 ms.

I used the following ladder logic.  I used a Sequencer because it is easy to initialize to a value of "0" without having to use a custom function.


 |
 | 1st.Scan                              Seq1:0
 +-----||--------------------------------[StepN]
 |
 |     A                                  CLKA
 +-----||--------------------------------[dDIFU]
 |
 |      A                                 CLKAN
 +-----|/|-------------------------------[dDIFU]
 |
 |    CLKA     B                          Seq1
 +-----||-----|/|---+--------------------[Upctr]
 |                   |
 |    CLKAN   B      |
 +-----||-----||----+
 |
 |    CLKA    B                           Seq1
 +-----||-----||------+-----------------[Cnctr]
 |                    |
 |    CLKAN    B      |
 +-----||-----|/|-----+
 |
 |


2. High Speed Sequencer.  Using HSCDEF 2,2,32000 in a custom function and connected the A and B phase signals to input pins 5 and 6.

The high speed sequencer only counts on the positive transitions of the A phase.   As a result, for every two clicks (mechanical detents) on the encoder the HSCPV[2] will count up/down only one time.  

The good news is that the HSC mechanism is fast enough for my low speed counter application.  Unfortunately the HSC only counts 1/2 of the time!

3. My next experiment is to use both HSC #1 and HSC#2 to connect to the mechanical encoder.  I will connect the encoder A/B inputs directly to HSC #1.  I will connect inverted versions of A/B (ULN2003?) to HSC #1.  I will then use a custom function to calculate the total count T:

    T = hscPV[1] + hscPV[2]

QUESTION  1:

Is there a mechanism to configure the High Speed Counters for the M series of the F series to count on both transitions of the A phase?  Look at my ladder logic for an example of how this is done.


QUESTION 2:

Do you have any other suggestions on how to get a low speed quadrature encoder to work with your PLCs?

QUESTION 3:

Is there some easier mechanism for me to draw waveforms on your YaBB system?  I change the fonts to "Courier" but your BB system has issues with ASCII spaces (0x20) and does not treat them as having the same horizontal width as other printable characters.  This makes it impossible to do nice ASCII artwork.

Thanks,

Gary D.

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:T100MD+ High Speed Counters
« Reply #1 on: October 12, 2009, 07:16:58 PM »
Question 1:  On the M-series PLCs and F-series PLCs the HSC only counts by 1 every full cycle of a phase input. There is not much we can do about the M-series since the firmware are in mask ROM and there is no plan to change the firmware yet. However, if you have the F-series PLC with firmware r70 and above, then it is possible to define each of the 3 HSC to count either x1, x2 or even x4 (which means both transitions of A and B phases are counted, so a single cycle will yield 4 counts).

Question 2:  Since ladder logic is too slow, and current HSC do not meet your requirement, you may try to define the inputs for both phases as interrupt inputs (instead of HSC) and then use interrupt service routine to handle the increment/decrement of the HSC.

Question 3:  YaBB has some limitations that there is not much we can do about. If you have a JPG or GIF graphic file and upload it to some free photo website you can put the link here using the "" tags.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:T100MD+ High Speed Counters
« Reply #2 on: October 12, 2009, 09:41:01 PM »
Thank you very much for the answers to my questions.  

The F-Series may be a "better" choice for the rotary encoder.

QUESTION 4:

Is there a mechanism available to the F-Series to synchronously increment/decrement the 32-bit value associated with the HSC from a custom function?

The reason that I am interested, is that I'd like to translate the value of the 32-bit count value to a series of ladder logic events by way of a custom function that is called periodically.

The following is that sort of code that I'm interested in:

if hscPV[1] > 0
   setIO DEC
   hscPV[1] = hscPV[1]-1
endif
if hscPV[1]< 0
   setIO INC
   hscPV[1] = hscPV[1]+1
endif


The relays DEC and INC will be cleared by ladder logic before the custom function will be called again.  

I suspect that this sort of code may fail because the HSC count value may change during the time that the custom function is executing and it would be possible for each read of the HSC to return a different value.

I will experiment with something more like this

Custom function to initialize HSC #1:

   hscdef 1, 255, 32000
   hscPV[1] = 0
   C = 0      ' tracking value


Custom function 255 has no use for me as the absolute value of the HSC counter is not very useful.  It is the differential value (or the change in value) that I'm trying to learn.


Custom function call periodically to translate counter values to ladder logic events:

   E = hscPV[1] - C     ' Tracking error. Single read access of HSC value

   if E > 0
      setIO DEC
      C = C + 1
   endif
   if E < 0
      setio INC
      C = C - 1
   endif



QUESTION 5:

Is it even possible to read the value of a HSC while the A/B inputs may be changing and get a valid result?

My concern is that you document the HSC counters as behaving as a signed 32-bit integer.  Since I do not know the internal details of your PLC  ASIC, I don't know if TBASIC code:

    C = hscPV[1]

accesses the 32-bit counter with a single 32-bit access or if your ASIC performs 2 16-bit accesses, 4 8-bit accesses...

My concern is that IF the TBASIC code has to "assemble" the 32-bit value from more that a single read access to the HSC than there is a possibility that the value assigned to C will be invalid on some occasions.

A possible issue is when the HSC counter has a value of -1 (0xffffffff) and if the TBASIC accesses the HSC counter as 2 16-bit registers.  If the HSC counter changes value to 0 (it has been incremented) is it possible for the value assigned to C to be some odd mixture of pre and post increment (-1 to 0) and C might get assigned a value of 0x0000ffff (65535) instead of either 0xffffffff(-1) or 0x00000000(0) .

Thanks again.

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:T100MD+ High Speed Counters
« Reply #3 on: October 13, 2009, 08:50:49 PM »
Not sure I understand your Q4. If you want every change of the HSC counter to trigger some custom function which allows you to make decision, you may have to use interrupt inputs and handle the increment/decrement based on the phase relation. In this way every time there is a phase change an interrupt custom function will be called and you can perform your decision making.

Question #5
----------------
The system interrupt service routine that handles the HSC increment/decrement will complete the update of the 32-bit integer after every phase change. There is no possibility that the regular custom function invoked by ladder logic will see an intermediate register value (such as two 16-bit register just before the complete carry over) as you feared.

F-series CPU employs 32-bit CPU and 32-bit integers are accessed in a single memory cycle. M-series CPU is 8/16-bit so it may require a few memory access cycles to complete write to a 32-bit number. However, since the system interrupt service routine does not yield to the ladder logic execution until the 32-bit integer data is fully written it doesn't matter whether the memory access in single or multi-cycles.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:T100MD+ High Speed Counters
« Reply #4 on: October 14, 2009, 01:00:26 AM »
Thank you.  

Let's not worry about my question #4.  I didn't ask the right question.  Sorry.

About question #5.  I think that what you are saying is that during the execution of the following TBASIC code:

    C = hscPV[1]

The M-series PLC has to access the 32-bit hscPV[1] register with two 16-bit reads. AND that there is an interlock to ensure that the HSC does not change value both 16-bit reads have completed.

If there was a possibility of the interrupt to update the HSC might occur between the two 16-bit reads counter then I can use the following sort of trick to get a stable (valid) read:

   C = hscPV[1]              ' read HSC one time
   while C <> hscPV[1]       ' verify that we get the same value 2x
      C = hspPV[1]         '     no, read HSC again
   endwhile

   ' If I get here then we have a good read of the HSC


Thanks again for your help,


Gary D.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:T100MD+ High Speed Counters
« Reply #5 on: October 14, 2009, 10:35:35 PM »
I've taken a stab at using the interrupt input mechanism to interface a low speed quadrature encoder at 2X to the M-series PLC.

This approach is just fast enough.  I connected the the A phase to Input pins 3 and 4 (Interrupts #1 and #2).  I connected the B phase to Input #5 (no special handling).  Variable A will be used to hold the current count. RELAY "TIC" will be used to comunicate to the ladder logic that the A variable has changed.

I initialized the interrupt system with the following TBASIC code:

INTRDEF 1, 2, 1      ' Positive Edge Interrupt handler
INTRDEF 2, 3, 0      ' Negative Edge Interrupt handler

A = 0            ' A will be used to store count

Interrupt #1 will handle the positive edge of phase A and the TBASIC code is as follows:

' PosEdgeA Interrupt handler
'
' Increment/Decrement based on phase B
'
if testIO(B) : A=A+1 : else A=A-1 endif
setIO TIC

Interrupt #2 handles the negative edge of the A phase:

' NegEdgeA
'
' Increment/Decrement based on phase B
'
if testIO(B): A=A-1 : else A=A+1 endif
SetIO TIC


Thanks for all you your support.  

Gary D

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:T100MD+ High Speed Counters
« Reply #6 on: October 15, 2009, 08:50:18 PM »
Thank you for sharing your codes (again!). I am sure some other users would find that useful.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

plc_user

  • Newbie
  • Posts: 21
  • I'm a llama!
    • View Profile
Re:T100MD+ High Speed Counters
« Reply #7 on: October 26, 2009, 06:15:53 PM »
Support wrote earlier in this thread,

Quote
On the M-series PLCs and F-series PLCs the HSC only counts by 1 every full cycle of a phase input. There is not much we can do about the M-series since the firmware are in mask ROM and there is no plan to change the firmware yet. However, if you have the F-series PLC with firmware r70 and above, then it is possible to define each of the 3 HSC to count either x1, x2 or even x4 (which means both transitions of A and B phases are counted, so a single cycle will yield 4 counts).

My question is: Does the F series support x4 in the enhanced mode and if so how do you control these functions?

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:T100MD+ High Speed Counters
« Reply #8 on: October 27, 2009, 01:25:48 PM »
Download the sample file from the following link:

 http://www.tri-plc.com/trilogi/HSC-x4.zip

Note that only F-series PLC with firmware r70 and above supports x2 and x4 HSC counting.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Pilot

  • Newbie
  • Posts: 4
    • View Profile
    • William Arthur
Re:T100MD+ High Speed Counters
« Reply #9 on: November 27, 2009, 08:40:18 AM »
I've been struggling with attempting to use an interupt to start a timer.  I'm a little confused...based on the post below- how input pins 3 and 4 can trigger interupts 1 and 2.  Don't the input pin #s need to coorespond with the interupt #s ?  Thanks.

I've taken a stab at using the interrupt input mechanism to interface a low speed quadrature encoder at 2X to the M-series PLC.

This approach is just fast enough.  I connected the the A phase to Input pins 3 and 4 (Interrupts #1 and #2).  I connected the B phase to Input #5 (no special handling).  Variable A will be used to hold the current count. RELAY "TIC" will be used to comunicate to the ladder logic that the A variable has changed.

I initialized the interrupt system with the following TBASIC code:

INTRDEF 1, 2, 1??????' Positive Edge Interrupt handler
INTRDEF 2, 3, 0??????' Negative Edge Interrupt handler

A = 0????????????' A will be used to store count

Interrupt #1 will handle the positive edge of phase A and the TBASIC code is as follows:

' PosEdgeA Interrupt handler
'
' Increment/Decrement based on phase B
'
if testIO(B) : A=A+1 : else A=A-1 endif
setIO TIC

Interrupt #2 handles the negative edge of the A phase:

' NegEdgeA
'
' Increment/Decrement based on phase B
'
if testIO(B): A=A-1 : else A=A+1 endif
SetIO TIC


Thanks for all you your support.  

Gary D

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:T100MD+ High Speed Counters
« Reply #10 on: November 27, 2009, 02:02:35 PM »
The relationship between the Input pin # and the Interrupts are documented in section 1.2 of the T100MD+ User's Manual.

Input pin #3 is the physical input for interrupt #1
Input pin #4 is the physical input for interrupt #2
Input pin #5 is the physical input for interrupt #3
Input pin #6 is the physical input for interrupt #4

If your goal is to start a timer based on an external edge event, I'd suggest using the normal ladder logic.

In the following example:
  • Start is the INPUT
  • Timer1 is the TIMER
  • StartEvent is a RELAY that is active for a single ladder logic scan on the rising edge of the Start INPUT.
  • Event is a RELAY that is set by the StartEvent  and cleared

when Timer1 times out.
[/list]

    Start                                   StartEvent
-----||---------------------------------------[dDIFU]

  StartEvent     Timer1                        Event
-----||-----+----|/|-------------------+------(RLY)
            |                          |
   Event    |                          |      Timer1
-----||-----+                          +------(TIM)


Good luck,

Gary D

Pilot

  • Newbie
  • Posts: 4
    • View Profile
    • William Arthur
Re:T100MD+ High Speed Counters
« Reply #11 on: December 01, 2009, 05:38:00 AM »
Gary,
Thank you very much for the reply.  :)  I have adjusted my code accordingly but the interrupt still fails to run the custom function.  (guess that I'm still missing something) :(  The reason that I am attempting to use the interrupt instead of a regular latching relay as you had suggested, is variability in the response.  I'm guessing that the scan time of the ladder logic is long enough so that depending on where in the cycle the program is when the input is triggered determines the delay in starting the timer.  My program involves a delay timer then an output - both for time periods specified by the user via the MD-HMI.  The program works fine except that the initial delay and output length are not consistant (enough).  All timers are high speed.  Thanks again.  Your input is very much appreciated. :)
Doug

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:T100MD+ High Speed Counters
« Reply #12 on: December 01, 2009, 08:42:39 AM »
Doug,

If your application cannot tolerate the +/- delay of the ladder logic scans, you should rethink your use of a PLC for this critical bit of timing.  

The only way you can reduce the variability is to do everything relaated to handing the external event and your time delay need in the interurpt handling function.  Start the timer, force the output, poll for timer to compled clear the output and then exit the custom funciton. This has the negative side effect of shutting dow the PLC for the duration of your delay.  The ladder logic scan mechanism will be suspended and the system will be unresponsive to changes on inputs.

You may want to figure out how to do the precise time delays using circuity external to the PLC.   I'd investigate the use of cheap microcontrollers such as Basic Stamp, Rabbit or PIC.  The PLC can handle the user interface and "talk" to the external microcontroller via the RS-485 serial port. The microcontroller could act as a precision, programable time delay relay.
 
Gary D

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:T100MD+ High Speed Counters
« Reply #13 on: December 01, 2009, 01:22:20 PM »
As Gary has pointed out, if you run the following:

  HSCDEF 1, 100,1

You have setup interrupt channel #1 to be triggered by rising edge (OFF to ON) interrupt and CF #100 will run when input #3 turns from OFF to ON. You can verify that this happens by adding a SETLCD command in CF #100 so that it displays something when interrupt occurs at input #3.

If your intention is to start a regular timer in the CF100 (ISR), you can actually do it using the following statement:

    TIMERPV[n] = xxx

where n is the timer number and xxx is the timer count value. This way the timer will start counting down immediately and when timer expires its contact can be used for control purpose or to run another custom function.

If the ladder logic scan time is too long and the timer contact cannot process the output at precise moment that you need then as Gary has suggested you may have to use an external timer to achieve the precise timing for that purpose.

« Last Edit: December 01, 2009, 01:25:30 PM by support »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS