Usage of VAR RETAIN PERSISTENT

dawidr
2021-01-29
2021-02-02
  • dawidr

    dawidr - 2021-01-29

    I'm using WAGO PLC PFC200 in my home automation project. I've plenty of POUs, each for one room. Each room implements IRoom interface and uses base POU for common logic like turning off all lights. For lights management, I'm using

    • FbEvaluateShortLongPress from WagoAppBuilding to handle short and long press of buttons on the wall (it could also be a function block from OSCAT library)
    • FbLatchingRelay from WagoAppBuilding as a toggle for PLC digital output
      I want to save the state of FbLatchingRelay in case of e.g.: power drop. I want all lights which were turned off before power drop to be turned on when the power comes back.

    I've solved it by declaring a FbLatchingRelay in the VAR RETAIN PERSISTENT area in my POU. But then after reading here that:

    If you declare a local variable in a function block as RETAIN, CODESYS stores the complete instance of this function block in the Retain range (all data of the function block); however, only the declared RETAIN variable is treated as such.

    I decided to change that in order not to waste RETAIN memory for a bunch of variables which are in POU but are not needed to be stored as RETAIN.

    So right now I have something like that:

    the VAR RETAIN PERSISTENT area is only declared in my main program
    it stores structures for each room (each POU) only with needed data - FbLatchingRelay POU and few other variables
    while initializing the room (POU) I'm passing those structures to my rooms using VAR_IN_OUT
    each room (POU) uses then this data
    PLC_PRG:

    VAR RETAIN PERSISTENT
        BathroomPersistentData: BathroomData;
    END_VAR
    
    Bathroom(PersistData := BathroomPersistentData, xMainLightSwitch := DI1_13, xMirrorLightSwitch := DI2_3, xMirrorLightSwitchActuator => DO2_1, xMainLightSwitchActuator => DO1_11);
    

    Bathroom POU:

    VAR_IN_OUT
        PersistData: BathroomData;
    END_VAR
    

    Is this a good approach? What do you think?
    It complicates project a bit but I'm not wasting RETAIN memory for things which should not be there (whole POUs).

     

    Last edit: dawidr 2021-01-29
  • tvm

    tvm - 2021-02-02

    I used to do it the way you've described. I now do it this way, which I've found easier.

    FUNCTION BLOCK TEST_FB
    VAR PERSISTENT
        Setpoints:   SETPOINT_STRUCT;  //all persistent variables
    END_VAR
    

    Then, within the PersistentVars variable list, go to "Declarations/Add all instance paths". The paths of all your persistent variables will be automatically created.

    https://help.codesys.com/api-content/2/codesys/3.5.12.0/en/_cds_cmd_add_all_instance_paths/

    You asked about the whole function block being added to the retain memory area. I'm not sure what the manual means by this, but I have not found that to be the case. See this post, I never got an answer to this question: https://forge.codesys.com/forge/talk/Engineering/thread/fbd343d7ad/?limit=25#0473

     

    Last edit: tvm 2021-02-02
    • dawidr

      dawidr - 2021-02-02

      I know about "Declarations/Add all instance paths" function but I was thinking that it will store the whole function block in the retain memory area. Just like it was written in the manual (https://help.codesys.com/api-content/2/codesys/3.5.12.0/en/_cds_vartypes_retain_persistent/).

      I will try to do some tests just like you did in your post.

       

Log in to post a comment.