Accepting some difference in EQ operator

martinll
2021-10-19
2021-10-20
  • martinll - 2021-10-19

    Hi!

    IΒ΄m looking for a function/way to get the = function (or similar) to accept numbers that are not exactly alike.

    For example,
    VAR
    Incoming_REAL:Real; //This variable increases every cycle and depends on how log the Incoming_Real has been active and can therefore differ from run to run.

    Add_On:Real:=10.5;
    Gate_Bool:Bool;
    Start_Clapping:Bool;
    END_VAR

    If Gate_Bool=true
    then
    Add_on:=Incoming_REAL + Add_on // This increases the Add_On variable with the number from Incoming_Real.
    End_IF

    If Add_On=Incoming_REAL
    then
    Start_Clapping:=True;

    So i would like the Add_on=Incoming_Real to be a bit accepting and
    having something like

    Start_Clapping:= Add_on=Incoming_Real (+ 0-8 or - 0.8)

    This code is just to explain, it probably holds some errors but hopefully you will understand what iΒ΄m looking for.

    IS there a way to do something like

    Add_on>= 0.8 or <= 0.8 from Incoming_REAL

    Best,

    Martin

     
  • martinll - 2021-10-19

    Right now i have solved with

    VAR
    Incoming_REAL:Real; //This variable increases every cycle and depends on how log the Incoming_Real has been active and can therefore differ from run to run.

    Add_On:Real:=10.5;
    Gate_Bool:Bool;
    Start_Clapping:Bool;
    Upper_limit:REAL :=1;
    Lower_limit:REAL :=2;
    High:REAL;
    LOW:REAL;
    END_VAR

    (with R_trig before both high and low)
    HIGH:=Incoming_REAL + upper_limit + Add_on;
    LOW:=Incoming_REAL + upper_Limit + Add_on;

    and then made a GT on the lower limit and LT on the upper limit. This works but its everything but well written code.

    Any other ideas for a better/easier code?

     
  • tvm - 2021-10-19

    Depending on what environment you're using, you might be able to find a LIM function in one of the libraries (NOTE: not the same as the LIMIT operator). If not, it's easy to make one:

    FUNCTION LIM : BOOL
    VAR_INPUT
        i_Test:     REAL;   //value to test
        i_Upper:    REAL;   //upper limit
        i_Lower:    REAL;   //lower limit
    END_VAR
    LIM:= (i_Test >= i_Lower) AND (i_Test <= i_Upper);  
    
    
    
    //in your program
    Start_Clapping:= LIM(Incoming_Real, Add_On + 0.8, Add_On - 0.8);
    
     

    Last edit: tvm 2021-10-20
  • martinll - 2021-10-20

    Thank you so much tvm, this was exactly what i was looking for!

    I did not find one among my library's so i built one and it works great.

    If someone that is (even) newer to codesys than me and tries so build this there is a small typo in the program above, there are two i_Lower where one of them should be i_Upper but with that change it works great.

    Many thanks!

    Martin

     
    πŸ‘
    1
  • tvm - 2021-10-20

    fixed it

     

Log in to post a comment.