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

Calculated Values

jvfred
2020-11-16
2020-11-16
  • jvfred - 2020-11-16

    Hello All,
    I have an application I need help with. I am new to Codesys and need some help creating Calculated Values. I am using Codesys Version 3.5 SP9 Patch 6 in a Eurotherm E+PLC100. Here is what I am trying to do:

    Calc Value 1
    IP1 Scale Factor: 1.0000
    Input 1: User Constant 2 (This is a fixed value)
    Operator: Subtract (Can be Subtract, Add, Divide, Multiply, None)
    IP2 Scale Factor: 1.0000
    Input 2: Calculated Val 5 (Input from another Calculated Value)
    Output: Prcs Var 1 (Output to other parts of the project)
    Resolution: 3 Decimal Places
    Upper Limit: 99.999
    Lower Limit: 0.000
    Default Value: 0.000

     This is an example. All of the values shown are variable. 
     Would like to build a function block for this as I am not that good at structured text, but in the end it doesn't matter.
    
     
  • Morberis

    Morberis - 2020-11-16

    This is a rough idea.

    In this example use the GUI elements to select the subtract, add, divide, multiple, or something you want to equal do calculation. That element will change a variable's int value using structured text, examples below.

    GUI Structured text example
    For subtract
    iInput1_Operator:=0;
    
    For add
    iInput1_Operator:=1;
    
    etc
    

    In this example you would use the display variables to limit the number of digits shown. %3.3f will display 3 digits before the decimal and 3 digits after the decimal. This will also change the input variable to 1 if the input variable is 0 and it's set to either multiply or divide. So that you don't multiply or divide by 0. Just make sure the scale factor variable is never 0.

    What the code does it check to see if in the input is 0 and if you're multiplying or dividing. If it is then it changes the tempinput variable to 1, if not then it passes the input to the tempinput variable. There are implicit check POU's that can do this but I thought for safety in case you didn't know about them, or in case your device doesn't do those checks itself, that I would do something similar in the code.

    Then it does a comparison for the input_operator variable. If it is 0-3 then it does the relevant calculation. If it is outside of that range it passes the temp input variable to temp output variable.

    Tempoutput variable gets multiplied by the scale factor variable and limited to within 0-99.999.

    There are some better ways to do this I'm sure but I thought that this would be pretty readable and modifiable if you wanted to.

    FUNCTION_BLOCK Calc_FB
    VAR_INPUT
        rInput1_UserInput:REAL;
        rInput1_UserConstant_2:REAL;
        iInput1_Operator:INT;//0=subtract, 1=add, 2= divide, 3= multiply, 4 or other= no calculation
        iInput1_ScaleFactor:INT;
    END_VAR
    VAR_OUTPUT
        rOutput1:REAL;
    END_VAR
    VAR
        tempinputvar:REAL;
        tempvar1:REAL;
    END_VAR
    
    IF rInput1_UserInput=0 AND (iInput1_Operator=2 OR iInput1_Operator=3)
        THEN tempinputvar:=1;
        ELSE tempinputvar:=rInput1_UserInput;
    END_IF
    
    
    IF iInput1_Operator=0 THEN tempvar1:=tempinputvar - rInput1_UserConstant_2;
        ELSIF iInput1_Operator=1 THEN tempvar1:=tempinputvar + rInput1_UserConstant_2;
        ELSIF iInput1_Operator=2 THEN tempvar1:=tempinputvar / rInput1_UserConstant_2;
        ELSIF iInput1_Operator=3 THEN tempvar1:=tempinputvar * rInput1_UserConstant_2;
        ELSE tempvar1:=tempinputvar;
    END_IF
    
    rOutput1:=LIMIT(0, tempvar1 * iInput1_ScaleFactor, 99.999);
    
     

    Last edit: Morberis 2020-11-16

Log in to post a comment.