Author Topic: Number of hours - Suggestions anyone?  (Read 14185 times)

secs

  • Newbie
  • Posts: 25
  • I'm a llama!
    • View Profile
Number of hours - Suggestions anyone?
« on: September 16, 2013, 04:47:44 AM »
Hi all. I have a f2424 coupled with a KEP HMI panel. I need to calculate the number of hours between events including if power goes off.

I have been simply increment a value every min. However if the PLC goes off line for any amount of time, this method fails. IWhat I am trying to do is calculate the number of hours between pump cylces. That is the pump starts now, pumps out a sump and turns off. 3 weeks later, the pump cycles again. I need to know the number of hours between these events.

Suggestions anyone?

jrustang

  • Newbie
  • Posts: 16
  • The Shadow!
    • View Profile
Re:Number of hours - Suggestions anyone?
« Reply #1 on: September 16, 2013, 10:02:32 AM »
I'ld Try saving the info to the PLCs EEPROM...

secs

  • Newbie
  • Posts: 25
  • I'm a llama!
    • View Profile
Re:Number of hours - Suggestions anyone?
« Reply #2 on: September 16, 2013, 10:19:47 PM »
You are correct in that saving the date in eprom will work but I am more interested in the calculation? If the last reset is on 25th of feb 2013 and now its 17th of sep 2013 12pm, how many hours has expired inbetween?

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Number of hours - Suggestions anyone?
« Reply #3 on: September 17, 2013, 11:31:21 AM »
To compute the time between 2 dates is usually done by converting each date to it's Julian Day n(JD).  The Julian day is the number of days since 1 January 4713 BC.  Once you have the JDs for each date, then just subtract one from each other to get the number of days between the dates.

Google Julian Date calculations to read up on this approach.

The following is an example of how this is done in FORTRAN.  I think that this can be translated to the TBASIC as used by the PLC fairly easily.

     INTEGER FUNCTION JD (YEAR,MONTH,DAY)
C
C---COMPUTES THE JULIAN DATE (JD) GIVEN A GREGORIAN CALENDAR
C   DATE (YEAR,MONTH,DAY).
C
    INTEGER YEAR,MONTH,DAY,I,J,K
C
    I= YEAR
    J= MONTH
    K= DAY
C
    JD= K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12) /12-3*((I+4900+(J-14)/12)/100)/4

C
    RETURN
    END

I have not translated this code to TBASIC to test it.  If you do attempt to write code for JD calculations, then I would suggest that you break the calculation up in multiple statements and be very careful with the multiplication and division operators.  Remember TBASIC only supports integer math (there are no fractions).  If you divide 1/4 you won't get 0.25 you will get 0.

Gary D.


garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Number of hours - Suggestions anyone?
« Reply #4 on: September 17, 2013, 01:08:10 PM »
OK just for grins I tested it in TBASIC and the results agree with the online calculator at USNO.

http://aa.usno.navy.mil/data/docs/JulianDate.php


' GenJD - custom function to compute Julian Date.  
'   Algoritm agrees with USNO (United States Naval Observatory) online Julian Date Converter
'   at http://aa.usno.navy.mil/data/docs/JulianDate.php
'
' 9/17/2013 is JD 2456553
'
' On entry:
'   I = year 2013
'    J = month 1..12
'    K = day of month
'
' On exit:
'   A holds Julian date
'
A = K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12) /12-3*((I+4900+(J-14)/12)/100)/4
[/color]

Good Luck,

Gary D

secs

  • Newbie
  • Posts: 25
  • I'm a llama!
    • View Profile
Re:Number of hours - Suggestions anyone?
« Reply #5 on: September 17, 2013, 01:56:52 PM »
Many thanks. I write code in Delphi and it does it for me but this is great. I will give it a go and let you know how I go...