Author Topic: Method for causing events at counter PV intervals  (Read 13991 times)

congo

  • Newbie
  • Posts: 16
  • I'm a llama!
    • View Profile
Method for causing events at counter PV intervals
« on: April 17, 2018, 02:44:12 PM »
I'm looking for a method to trigger a math equation at intervals of a HSCPV[n] as it accumulates.

For instance, as the counter accumulates 100 counts, solve the math equation, then after another 100 counts solve it again, continue to do this after each 100 counts until the HSCPV[n] is reset from a separate part of the program. The counter does not stop at any point so the equation trigger needs to happen 'on the fly'.

One method I thought might work would be to use an IF statement. IF HSCPV[n]/100=1 THEN (equation here) ENDIF. The 1 could be incremented at each TRUE occurrence of the if statement. But even if the custom function rung clock is set fast at 0.01s it still seems to miss the IF instruction.

I'm sure there's a better way...

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Method for causing events at counter PV intervals
« Reply #1 on: April 17, 2018, 03:36:18 PM »
Congo,

There are a couple of approaches to solve your problem:

Use the HSCDEF statement that initializes the HSCDEF to call a custom function when the HSC (high-speed counter) reaches a specified count.  The CF that is associated with the high-speed counter is called after the HSC reaches the count.  You will need to reload the HSC counter with the next count value.  The interrupt CF will look something like this:

' HSCPC[1] Interrupt function
'

' Reload HSCPV[1] with residual count value
'   the while loop is used to verify that HSCPV[1] was successfully reloaded
'
while (HSCPV[1] >= 100)
   HSCPV[1] = HSCPV[1] - 100      ' Reload counter with residual count value
endwhile

' Your code specific to your task follows:
'


The second approach is to not use the HSCDEF statement and check the HSCPV[1] on each scan of the ladder logic rather than every 0.01 seconds.  The code for this looks a bit different:

' HSCPC[1] Polled on every scan
'
if HSCPV[1] < 100
   return     ' nothing to do...
endif

' Reload HSCPV[1] with residual count value
'   the while loop is used to verify that HSCPV[1] was successfully reloaded
'
while (HSCPV[1] >= 100)
   HSCPV[1] = HSCPV[1] - 100      ' Reload counter with residual count value
endwhile

' Your code specific to your task follows:
'


The reasons that I use the while ... endwhile loop as part of the reload the HSCPV[1] is that there is a potential conflict between your CF write to the HSC and the PLC's hardware/firmware. It is possible for the write to the HSC from the CF to be lost.  The loop ensures that the HSC is correctly updated by the CF by reading the HSC back after the write operation.

The reason that I don't just reload the HSCPV[1] with 0 is that there are delays in both approaches between when the HSCPV gets to your magic number and when the CF can execute.  Do not be surprized if the HSCPV[1] is at 101, 102, ... 199 by the time that the CF executes.  You just need to deal with this possibility.

Gary D*ckinson


« Last Edit: April 17, 2018, 03:43:03 PM by garysdickinson »

congo

  • Newbie
  • Posts: 16
  • I'm a llama!
    • View Profile
Re:Method for causing events at counter PV intervals
« Reply #2 on: April 17, 2018, 06:00:58 PM »
Thanks Gary,

pulses are coming in on this high speed counter at about 50Hz so I'm thinking the delay of when the HSCPV[n] interrupt allows the cusF to execute might be a problem. I can't afford to lose pulses in the time that HSCPV[n] is reloaded also. The total accumulated pulses at the end of any given accumulation must be the same whether I'm reloading the counter for this interrupt purpose or not. This might be a problem in this application, not sure.

I'm also not quite following:

 HSCPV[1] = HSCPV[1] - 100      ' Reload counter with residual count value

Won't this cause HSCPV[1] to decrement constantly? In this case the HSCPV[n] always counts up due to the single direction of the sensor wheel.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Method for causing events at counter PV intervals
« Reply #3 on: April 17, 2018, 11:55:57 PM »
Congo,

Let me explain what I was doing with this snippet of code:

' Reload HSCPV[1] with residual count value
'   the while loop is used to verify that HSCPV[1] was successfully reloaded
'
while (HSCPV[1] >= 100)
   HSCPV[1] = HSCPV[1] - 100      ' Reload counter with residual count value
endwhile

The HSC was programmed to call the CF in response to HSCPV[1] being equal to 100.  I wanted the value of HSCPV[1] to increment from 0 to 100 and when it hit 100 the CF that was called was to reload HSCPV[1] back to a value of 0.  The problem is that the CF is NOT called on the instant that the HSCPV[1] becomes zero, but is called at some time later and synchronized to the ladder logic.  

In my application the value of HSCPV[1] may have been incremented beyond the target value of 100 by the time the CF is called.  If the HSCPV[1] has been incremented to 105, this code would reload HSCPV[1] with the value of 5 so that 95 counts later HSCPV[1] will reach 100.  


   HSCPV[1] = HSCPV[1] - 100      ' Reload counter with residual count value


If your applications increments the HSC at a rate of 50 Hz, then the value of the HSC counter will change every 20 mS.  The HSCPV[1] will only be at 100 for 20 mS.  In order to poll the HSCPV using CF, you will have to poll at 2x rate that the HSCPV can increment. The minimum poll rate would be 100 Hz or once every 10 mS (Nyquist rate...).

If your application absolutely must perform your math equation only when the HSCPV[1] is exactly 100, 200, 300 ... this may be impossible to achieve with the PLC.  Remember that the HSCPV[1] is only at a given value for 20 mS!  And your PLC code must complete each scan in only 10 mS to ensure that you do not miss the critical HSCPV[1] value.

If your application would be OK with HSCPV[1] values of 100, 202, 300, 401, 505 ..., then I see no problem with using the PLCs to get this job completed.

I suggest that you do not use the HSCDEF interrupt mechanism, but simply call a custom function on every scan of the ladder logic. This code just compares the current HSCPV[1] against a 32-bit limit value stored in DM32[].  Each time you do the math just add 100 to the limit...

This is the simplest code that I know of that might solve your problem:

' HSCPV[1] Polled on every scan
'
' LimitValue is a 32 bit variable in DM32[]  Use a #Define to set this up
'
if HSCPV[1] < LimitValue
   return
endif

' Setup next LimitValue
'
LimitValue = LimitValue + 100

' Your code specific to your task follows:
'


This code simply allows HSCPV[1] to increment continuously.  There is no need to reload HSCPV[1] when the limit or target value is reached as this code does not use the HSCDEF to set up an interrupt function.  

The HSC counters are implemented in hardware and are 32-bit counters.  TBASIC works fine with 32-bit values and will treat the HSCPV as 32-bit signed variable (positive and negative values). When the 32-bit counter gets to the biggest positive number (134217727 decimal or &h7FFFFFF) the next count will be the most negative number ( -2147483648 or &H8000000).  The code that I presented should work fine.  However, I have been proven wrong on many occasions.

Best regards,

Gary D*ckinson


« Last Edit: April 18, 2018, 03:55:54 PM by garysdickinson »

congo

  • Newbie
  • Posts: 16
  • I'm a llama!
    • View Profile
Re:Method for causing events at counter PV intervals
« Reply #4 on: April 18, 2018, 02:29:32 PM »
Thanks Gary, once again you are right. I like the limit implementation because it keeps it simple and so far test runs show that it will work well.

Other methods of reloading to the HSCPV[n] work, but risk missing counts and skewing the real counter PV. Thanks for your quick and detailed suggestions.