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
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.
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_BLOCKCalc_FBVAR_INPUTrInput1_UserInput:REAL;rInput1_UserConstant_2:REAL;iInput1_Operator:INT;//0=subtract, 1=add, 2= divide, 3= multiply, 4 or other= no calculationiInput1_ScaleFactor:INT;END_VARVAR_OUTPUTrOutput1:REAL;END_VARVARtempinputvar:REAL;tempvar1:REAL;END_VARIFrInput1_UserInput=0AND(iInput1_Operator=2ORiInput1_Operator=3)THENtempinputvar:=1;ELSEtempinputvar:=rInput1_UserInput;END_IFIFiInput1_Operator=0THENtempvar1:=tempinputvar-rInput1_UserConstant_2;ELSIFiInput1_Operator=1THENtempvar1:=tempinputvar+rInput1_UserConstant_2;ELSIFiInput1_Operator=2THENtempvar1:=tempinputvar/rInput1_UserConstant_2;ELSIFiInput1_Operator=3THENtempvar1:=tempinputvar*rInput1_UserConstant_2;ELSEtempvar1:=tempinputvar;END_IFrOutput1:=LIMIT(0, tempvar1*iInput1_ScaleFactor, 99.999);
Last edit: Morberis 2020-11-16
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 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.
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.
Last edit: Morberis 2020-11-16