Example User Functions

The text from the following example user functions can be copied and pasted into the User Function Editor.

Example 1

Converts inches to millimeters.

Usage


MyPos := inch2mm(_Axis[0].ActPos);
 

Declaration

(*
Converts inches to millimeters. 
*)
FUNCTION inch2mm : REAL
    VAR_INPUT
        Inches : REAL;
    END_VAR
   
    inch2mm := Inches * 25.4;
END_FUNCTION

Example 2

Returns the average of 4 values.

Usage


MyPos := Avg4(_Axis[0].ActPos,  _Axis[1].ActPos, _Axis[2].ActPos, _Axis[3].ActPos);
                

Declaration

(*
Returns the average of the 4 input parameters.
*)
FUNCTION Avg4 : REAL
    VAR_INPUT
        Val1 : REAL;
        Val2 : REAL;
        Val3 : REAL;
        Val4 : REAL;
    END_VAR
        
    Avg4 := (Val1 + Val2 + Val3 + Val4) / 4.0;

END_FUNCTION

Example 3

Converts polar coordinates to cartesian coordinates.

Usage


Polar2Cart(radius, angle,x,y);
                

Declaration

(*
Converts polar coordinates r, theta to cartesian coordinates x, y. Theta is in the interval [0,360) degrees.
*)
FUNCTION Polar2Cart : BOOL
    VAR_INPUT
        r : REAL;          //radius
        theta : REAL;      //angle in degrees [0,360)
    END_VAR
    VAR_OUTPUT
        x : REAL;
        y : REAL;
    END_VAR
    IF theta = 0.0 OR theta = 180.0 THEN
        x := r;
        y := 0.0;
    ELSIF theta = 90.0 OR theta = 270.0 THEN       
        x := 0.0;
        y := r;
    ELSE
        x := r * COS(theta * (M_PI / 180.0));
        y := r * SIN(theta * (M_PI /  180.0));
    END_IF
    Polar2Cart := TRUE;
END_FUNCTION
                

Example 4

Returns the index of the least significant bit that is set in a DWORD.

Usage


MyIndex := LSB(MyDWORD);
                

Declaration

(*
LEAST SIGNIFICANT BIT
returns the index of the least significant bit set in a DWORD.
*)
FUNCTION LSB : DINT
    VAR_INPUT
        DW_IN : DWORD;
    END_VAR
    VAR
        DW : DWORD;
        C : DINT;
    END_VAR
    IF DW_IN <> 0 THEN
        DW := DW_IN AND DINT_TO_DWORD(DWORD_TO_DINT(NOT DW_IN) + 1);
        C := 0;
        IF (DW AND 16#FFFF0000) <> 0 THEN C := C + 16; END_IF
        IF (DW AND 16#FF00FF00) <> 0 THEN C := C + 8;  END_IF
        IF (DW AND 16#F0F0F0F0) <> 0 THEN C := C + 4;  END_IF
        IF (DW AND 16#CCCCCCCC) <> 0 THEN C := C + 2;  END_IF
        IF (DW AND 16#AAAAAAAA) <> 0 THEN C := C + 1;  END_IF
        LSB := C;
    ELSE
        LSB := -1;
    END_IF
END_FUNCTION

 

See Also

User Functions | Expressions


Send comments on this topic.

Copyright © 2024 Delta Computer Systems, Inc. dba Delta Motion