Calculate Time Between Two Events Using RTC

2014-01-07
2024-08-14
  • MrkTrussell - 2014-01-07

    Hey Guys!

    Currently using Version 2.3.9.34 with an ABB AC500 PLC (PM591).

    I am trying to get the timing between two events. I won't go into high detail, let's just say, for the sake of discussion, when either of the two events occur, they set a corresponding boolean variable = TRUE.

    So, if Machine Starts, bStart = TRUE.
    And, if Machine Stops, bStop = TRUE.

    I'm over simplifying to make this easy to answer.

    I just need the elapsed system time between the two events. Here is the code I am currently using:

    (* SYSTEM TIME *)
    TIME_SYS   :=SYS_TIME(TRUE);
    TIME_END   :=TIME_SYS;
    (* RTC IN SECONDS *)
    RTC_TIME   :=SysRtcGetTime(TRUE);
    TIME_END2   :=RTC_TIME;
    (* EVENT TIMER *)
    RTRIG(CLK:= ESC_FAULT, Q=>);      (* START TIMER    *)
    FTRIG(CLK:= ESC_RUN, Q=> );      (* STOP TIMER    *)
    IF RTRIG.Q OR FTRIG.Q = TRUE THEN
       COUNT             :=    COUNT + 1;
       EVENT_TIME         :=   TIME_END - TIME_START;
       TIME_START         :=   TIME_SYS;
       EVENT_TIME2         :=   TIME_END2 - TIME_START2;
       TIME_START2         :=   RTC_TIME;
    END_IF;
    

    The two times are redundant. One is DWORD and the other is DT format.

    This seems to work in my current configuration, but if I want to test a scenario where the event is defined by a value reaching or exceeding a set point, it doesn't seem to function properly.

    Does anyone have a function block they use for timing system events?

    Any help is surely appreciated. Thanks!

     
  • shooter - 2014-01-07

    yes have a look at w www.oscat.de w it is english library for codesys with lots of functions etc.
    keep all times in DT format easier to work .

     
  • MrkTrussell - 2014-01-07

    I simplified the code:

    FUNCTION_BLOCK TIMER_SYS
    (* INPUT VARIABLES    *)
    VAR_INPUT
       bSTART: BOOL;
       bSTOP: BOOL;
    END_VAR
    (* OUTPUT VARIABLES   *)
    VAR_OUTPUT
       dwLAP_TIME:    DWORD;
    END_VAR
    (* LOCAL VARIABLES   *)
    VAR
       dwTIME_SYS:       DWORD;
       dwTIME_END:       DWORD;
       dwTIME_START:    DWORD;
       rtSTART:       R_TRIG;
       rtSTOP:       R_TRIG;
    END_VAR
    dwTIME_SYS   := SYS_TIME(TRUE);
    dwTIME_END   := dwTIME_SYS;
    rtSTART(CLK:= bSTART, Q=>);            (* START TIMER    *)
    rtSTOP(CLK:= bSTOP, Q=> );               (* STOP TIMER    *)
    IF rtSTART.Q OR rtSTOP.Q = TRUE THEN
       dwLAP_TIME      :=   dwTIME_END - dwTIME_START;
       dwTIME_START      :=   dwTIME_SYS;
    END_IF;
    

    Now, I have this function block to use to find the time between two events:

    Time is output to dwLAP_TIME in milliseconds.

    Feel free to use this!

    IMG: Bild

     
  • bamir - 2024-08-14

    Hi
    Just wanted to know which library is used to get sys_time

     
  • dkugler - 2024-08-14

    a few seconds later with google search...

    https://stackoverflow.com/a/61986234
    SYS_TIME is definitely an ABB AC500 specific function, included in library SysInt_AC500_V10.lib.

     

Log in to post a comment.