Notes on the documentation

This description is only intended for the use of trained specialists in control and automation engineering who are familiar with the applicable national standards.
It is essential that the following notes and explanations are followed when installing and commissioning these components.

The responsible staff must ensure that the application or use of the products described satisfy all the requirements for safety, including all the relevant laws, regulations, guidelines and standards.

Disclaimer

The documentation has been prepared with care. The products described are, however, constantly under development.
For that reason the documentation is not in every case checked for consistency with performance data, standards or other characteristics.
In the event that it contains technical or editorial errors, we retain the right to make alterations at any time and without warning.
No claims for the modification of products that have already been supplied may be made on the basis of the data, diagrams and descriptions in this documentation.

Trademarks

Beckhoff®, TwinCAT®, EtherCAT®, Safety over EtherCAT®, TwinSAFE® and XFC® are registered trademarks of and licensed by Beckhoff Automation GmbH.
Other designations used in this publication may be trademarks whose use by third parties for their own purposes could violate the rights of the owners.

Patent Pending

The TwinCAT Technology is covered, including but not limited to the following patent applications and patents:
EP0851348, US6167425 with corresponding applications or registrations in various other countries.

Copyright

© Beckhoff Automation GmbH & Co. KG, Germany.
The reproduction, distribution and utilization of this document as well as the communication of its contents to others without express authorization are prohibited.
Offenders will be held liable for the payment of damages. All rights reserved in the event of the grant of a patent, utility model or design.


Overview

The TcMath.lib PLC library contains extended mathematical functions for TwinCAT PLC. The library is supplied together with TwinCAT (from version 2.9 Build 1000).

Functions

FLOOR | The FLOOR function determines a integral value from a floating point number that is a fraction smaller than or equal that number.
FRAC | The FRAC function determines the decimal component of a floating point number.
LMOD | The LMOD function carries out a modulo division and returns the signed residual.
LTRUNC | The LTRUNC function determines the integral component of a floating point number.
MODABS | The MODABS function carries out a modulo division and determines the unsigned modulo value within the modulo range.
MODTURNS | The MODTURNS function carries out a modulo division and determines the signed integral component.
F_GetVersionTcMath | Returns the library version information


FRAC

TwinCAT PLC Library: TcMath

FUNCTION FLOOR

The FLOOR function determines a integral value from a floating point number that is a fraction smaller than or equal that number. The resulting number is of type LREAL and is therefore not limited to the value range of integer variables.

Examples

FLOOR(2.8) = 2

FLOOR(-2.8) = -3

Similar functions: TRUNC, LTRUNC

Note: Unlike FLOOR, the LTRUNC function always determines the integral part of a number. For positive values, this number is smaller than or equal the input parameter, for negative values it is greater than or equal the input parameter.

FUNCTION FLOOR : LREAL

VAR_INPUT
    lr_in : LREAL;
END_VAR

lr_in : Function parameters of type LREAL

Development environment | Target system type | PLC libraries to be linked
TwinCAT v2.9.0 build 1000 onwards | PC or CX (x86) | TcMath.Lib
TwinCAT v2.10.0 Build >= 1301 | CX (ARM)

Overview


FRAC

FUNCTION FRAC

The FRAC function determines the decimal component of a floating point number.

Examples

FRAC(2.8) = 0.8

FRAC(-2.8) = -0.8

FUNCTION FRAC : LREAL

VAR_INPUT
    lr_in : LREAL;
END_VAR

lr_in : Function parameters of type LREAL

Development environment | Target system type | PLC libraries to be linked


FRAC

FUNCTION LMOD

The LMOD function carries out a modulo division and returns the signed residual.

Examples

LMOD( 400.56, 360) = 40.56

LMOD( -400.56, 360) = -40.56

Similar functions: MOD, MODABS

Notes: Unlike MOD, the LMOD function operates with floating point variables and also determines non-integer residuals.

In the context of NC axes, modulo values are usually used unsigned. These can be calculated with the MODABS function.

FUNCTION LMOD : LREAL

VAR_INPUT
    lr_Value : LREAL;
    lr_Arg : LREAL;
END_VAR

lr_Value : Input value

lr_Arg : Modulo range

Development environment | Target system type | PLC libraries to be linked


FRAC

FUNCTION LTRUNC

The LTRUNC function determines the integral component of a floating point number.

Examples

LTRUNC(2.8) = 2

LTRUNC(-2.8) = -2

Similar functions: TRUNC, FLOOR

Note: Unlike TRUNC, the result from LTRUNC is of type LREAL and is therefore not limited to the value range of integer variables.

FUNCTION LTRUNC : LREAL

VAR_INPUT
    lr_in : LREAL;
END_VAR

lr_in : Function parameters of type LREAL

Development environment | Target system type | PLC libraries to be linked


FRAC

FUNCTION MODABS

The MODABS function carries out a modulo division and determines the unsigned modulo value within the modulo range.

Examples

MODABS( 400.56, 360) = 40.56

MODABS( -400.56, 360) = 319,44

Similar functions: MOD, LMOD

Note: The MODABS function can be used to calculate the modulo set position of an NC axis from its absolute set position.

ModuloSetPosition := MODABS( NcToPlc.fPosSoll, 360 );

FUNCTION MODABS : LREAL

VAR_INPUT
    lr_val : LREAL;
    lr_mod : LREAL;
END_VAR

lr_val : Input value

lr_mod : Modulo range

Development environment | Target system type | PLC libraries to be linked


FRAC

FUNCTION MODTURNS

The MODTURNS function carries out a modulo division and determines the signed integral component (modulo periods, modulo rotations).

Examples

MODTURNS ( 800.56, 360) = 2

MODTURNS ( -400.56, 360) = -2

Note: The MODTURNS function can be used to calculate the number of modulo rotations of an NC axis from its absolute set position.

ModuloSetTurns := MODTURNS ( NcToPlc.fPosSoll, 360 );

FUNCTION MODTURNS : LREAL

VAR_INPUT
    lr_Value : LREAL;
    lr_Arg : LREAL;
END_VAR

lr_Value : Input value

lr_Arg : Modulo range

Development environment | Target system type | PLC libraries to be linked


F_GetVersionTcMath

FUNCTION F_GetVersionTcMath

The function returns library version info.

FUNCTION F_GetVersionTcMath : UINT

VAR_INPUT
    nVersionElement : INT;
END_VAR

nVersionElement : Version parameter:

  • 1 : major number;
  • 2 : minor number;
  • 3 : revision number;
Development environment | Target system type | PLC libraries to include