Help me understand Oscat time function

boompy
2019-08-17
2019-08-28
  • boompy - 2019-08-17

    Hi all

    I'm pretty new to all things Codesys.

    I'm been playing with Oscat libraries and trying to learn from them.

    Now I'm trying to understand what the 'debug' component of the T_PLC_MS function actually does. As I understand it, the PLC should reach an overflow condition every 49 days and the debug component is somehow meant to circumvent this. Is that right?

    Could somebody please explain to me how this works. The Oscat explanation is a bit lost on me.

    If somebody could help me I'd be very grateful.

    Why wouldn't I just use the time() function? What is the benefit of the Oscat function??

    FUNCTION T_PLC_MS : DWORD
    VAR CONSTANT
    debug : BOOL := 0;
    N : INT := 0;
    offset : DWORD := 0;
    END_VAR
    VAR
    tx : TIME;
    END_VAR

    ##############

    tx := TIME();
    T_PLC_MS := TIME_TO_DWORD(Tx);

    IF debug THEN
    T_PLC_MS := (SHL(T_PLC_MS,N) OR SHL(DWORD#1,N)-1) + OFFSET;
    END_IF;

     
  • i-campbell

    i-campbell - 2019-08-28

    The debug section has some bit hackery tools for fast computation which make it hard to read.
    Essentially N increases the speed of the clock, forcing a rollover not every 49 days, but every 49/(2^N) days. Offset also allows you to change the start value of your clock so the first rollover happens in say 1 minute instead of waiting a full 49/(2^N) days.
    You would use this to test how your own code (which uses the T_PLC_MS()) functions at the rollover. For example do you have code that says:

    IF input1 AND NOT AlreadySeenInput1 THEN
    Β  StoredTime := T_PLC_MS();
    Β  AlreadySeenInput1 := TRUE;
    END_IF;
    IF ( T_PLC_MS() > (StoredTime + 30000) ) THEN
    Β  ThirtySecondsLater := TRUE;
    END_IF;
    

    If StoredTime was 10000ms less than the rollover,
    (StoredTime + 30000) would then be only 20000, and ThirtySecondsLater would come on immediately.
    You could test that scenario in simulation in a matter of minutes with the debug code, just don't leave it on the real machine!

    Warm welcome to all things Codesys!

     

Log in to post a comment.