Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

RTO in CODESYS?

2020-08-27
2020-08-31
  • mandeepahujaifm - 2020-08-27

    Hello all, I am sure this is brought up all the time. Looking for an RTO type of FB in CODESYS. Anyone have any luck porting this functionality over from AB? Please advice

     
  • Morberis

    Morberis - 2020-08-27

    I found something for you here

    This is not my code, this is code from the link

    FUNCTION_BLOCK RTO
     VAR_INPUT
            Run : BOOL; 
            Reset : BOOL; 
     END_VAR
     VAR RETAIN
            Running : BOOL;
            StartTime : TIME;
     END_VAR
     VAR
             ResetONS : R_TRIG; 
     END_VAR
     VAR_OUTPUT RETAIN
              ET : TIME;
     END_VAR
    
         IF RUN AND NOT Running THEN
             StartTime := TIME();
             Running := TRUE;
        END_IF
        IF NOT RUN AND NOT Running THEN
             StartTime := TIME();
        END_IF
         ResetONS(clk := Reset); 
        IF ResetONS.Q THEN
             Running := FALSE;
             StartTime := TIME();
        END_IF
        ET := TIME()-StartTime;  
    
     
    • mandeepahujaifm - 2020-08-28

      Ok this is a good start. Doesn't have the "Preset Time" input and doesn't have the Done output bit.

       
      • aliazzz

        aliazzz - 2020-08-29

        would you be so kind and post a definition or a link to a definition of this RTO timer FB? That would most helpfull, thank you in advance. Maybe OSCAT has this type of timer allready implemented, but without your definition It wll be more difficult to help.

         

        Last edit: aliazzz 2020-08-29
        • mandeepahujaifm - 2020-08-31

          Hello aliazzz Please see the attached timing diagram for RTO in AB. I have included my implementation as well in this forum. Also, please let me know if it can be improved etc.

           
  • mandeepahujaifm - 2020-08-31

    After looking all over the Internet and in this fourm I decided to take a stab at it. I am sharing the first implementation of the CODESYS RTO block.

    Download file

    (*  @author - Mandeep Ahuja
        @date - 08/31/2020
        @Rev - 1.0
        @company - ifm efector, inc.
        @Terms - Not to be used without this comment header
    *)
    FUNCTION_BLOCK RTO
    VAR_INPUT
        IN: BOOL;    //INPUT Signal
        RST : BOOL; // RESET Signal
        PT: TIME; //PRESET Time
     END_VAR
     VAR
        StartTime : TIME;
        prevET: TIME;
     END_VAR
     VAR
        INOn: R_TRIG;
    END_VAR
    VAR_OUTPUT  //RETAIN
        ET : TIME; //ELAPSED Time
    END_VAR
    VAR_OUTPUT
        Q: BOOL; // OUTPUT Bit
    END_VAR
    
    INOn(CLK:= IN);
    IF INOn.Q THEN
        StartTime:= TIME(); //On Rising Edge of IN store this moment in Time() as StartTime for the accumulation
    END_IF
    
    IF IN THEN
        ET := TIME()-StartTime + prevET; // Calculate Elapsed Time
    ELSE
        prevET:= ET; // When IN goes low then store the ET and pause accumulating
    END_IF
    
    IF ET >= PT THEN
        ET := PT;
        Q:=TRUE;
    ELSE
        Q:= FALSE;      
    END_IF
    
    IF RST THEN // Reset everything back to Initial state including any Rising/Falling edge triggers
        INOn(CLK:=FALSE); 
        ET:=T#0MS;
        prevET:=T#0MS;
        Q:=FALSE;
    END_IF
    

    Usage

    PROGRAM Main
    VAR PERSISTENT
        RTO_0: RTO;
    END_VAR
    

    Based on the attached timing diagram. That's all the documentation I had.

     
    πŸ‘
    2

    Last edit: mandeepahujaifm 2020-08-31
    • aliazzz

      aliazzz - 2020-08-31

      Hi,

      A tip: you can also submit your code in the snippets corner as then all submitted code is easy to browse/find/search;
      https://forge.codesys.com/tol/iec-snippets/snippets/

      Plus, offcourse kudoes for the effort, it looks very complete! πŸ˜‰

      PS => Your download button does NOT work at time of my posting

       

      Last edit: aliazzz 2020-08-31

Log in to post a comment.