How to use Function

mawaloc
2021-08-19
2021-08-19
  • mawaloc - 2021-08-19

    Hello to all,
    I would need some help.

    Instead of using PRG, I've tried to use a function

    I've fews rotary switchs that I'm trying to count when values change.
    So instead of trying to create multiple PRG, the idea was to use a fuction for.

    And unfortunaly it doesn't react.
    someone has any idea.~~~
    code
    ~~~


    FUNCTION Rotary_switch_selection : USINT
    VAR_INPUT
    F_P1pot :USINT;
    END_VAR
    VAR
    F_P1pot_TON1:TON;
    F_P1pot_TON2:TON;
    F_P1pot_RTRIG:R_TRIG;
    F_FunctionCTUD: CTUD;
    F_Ppotbis :USINT;
    F_Functionup:BOOL;
    F_Functiondown:BOOL;
    F_VAL2:BOOL;
    F_PV1:WORD;
    END_VAR

    then in my "main" to call

    PROGRAM Main
    VAR
    P1value:USINT;
    END_VAR

    P1value:=Rotary_switch_selection( F_P1pot:=GVL.P1pot );

    GVL.P1pot => is my Potentiometer value.

    thank you for your help.

     
  • mawaloc - 2021-08-19

    sorry I miss some parts of the code!!

    code
    

    FUNCTION Rotary_switch_selection : USINT
    VAR_INPUT
    F_P1pot :USINT;
    END_VAR
    VAR
    F_P1pot_TON1:TON;
    F_P1pot_TON2:TON;
    F_P1pot_RTRIG:R_TRIG;
    F_FunctionCTUD: CTUD;
    F_Ppotbis :USINT;
    F_Functionup:BOOL;
    F_Functiondown:BOOL;
    F_VAL2:BOOL;
    F_PV1:WORD;
    END_VAR

    code of the function:

    F_P1pot_TON1(IN:= NOT F_P1pot_TON2.Q , pt:=T#0.2S);
    F_P1pot_TON2(IN:= F_P1pot_TON1.Q , pt:=T#0.2S);
    F_P1pot_RTRIG(CLK:=F_P1pot_TON1.Q , Q=> );

    IF F_P1pot_RTRIG.Q THEN
    F_Ppotbis:=F_P1pot;
    END_IF

    IF F_P1pot > F_Ppotbis THEN
    F_Functionup:=TRUE;
    F_Functiondown:=FALSE;
    ELSIF F_P1pot < F_Ppotbis THEN
    F_Functionup:=FALSE;
    F_Functiondown:=TRUE;
    ELSE
    F_Functionup:=FALSE;
    F_Functiondown:=FALSE;
    END_IF

    F_FunctionCTUD(CU:= F_Functionup, CD:= F_Functiondown, RESET :=F_VAL2, load:= , PV:=F_PV1);
    
    Rotary_switch_selection := WORD_TO_BYTE (F_FunctionCTUD.CV);
    
    
    then my main is what I've written above
    
     
  • hermsen

    hermsen - 2021-08-19

    hi,

    As you might have learned with Algebra, functions don't preserve memory and are state independant. They are purely input dependant. This means that the output of a function is always directly related to the inputs and do not contain any memory states.

    example:

    f(x) = sin(t)

    To use memory states you should use a FUNCTION BLOCK instead of a FUNCTION. Then youre code wil work.

     

    Last edit: hermsen 2021-08-19
  • hermsen

    hermsen - 2021-08-19

    tip

    F_P1pot_TON1:TON; -> try to name it according to what the TON represents, not what it is. Here your var represents some p1potWaitTime or so

     

    Last edit: hermsen 2021-08-19
  • mawaloc - 2021-08-19

    Thank You.

    Effectively it's work much better with FB. Sorry for that.
    For naming , I would change it was more for trial purpose and understand how does work. Now I can do it in proper way.

    Thank You again.

    Regards

     

Log in to post a comment.