Distributed Clocks & TwinCAT PLC
Validity of the following settings The following information is based on TwinCAT 2.11 build 1539. More recent editions can have a different user interface design; however, usage remains analogously the same. Subfunctions may already have been included in the predecessor versions, but it should be noted that the same TwinCAT version (e.g. 2.11) must be present on both the target system and the programming system in order for the latest functions to be supported. |
From the point of view of the PLC, TwinCAT 2.11 is a continuous development of TwinCAT 2.10. The PLC libraries have been expanded to include more features. The following libraries are of particular interest for use with Distributed Clocks functions:
- TcUtilities.lib
- Functions for the treatment of 64-bit values: UINT64 and INT64
- Functions for reading different time sources
- TcEtherCAT.lib
- Functions for the manipulation of EtherCAT devices
- Functions for reading the DC times
These libraries are included in every TwinCAT installation.
Working with DC times in the controller
From the perspective of the controller the distributed clock time has the following characteristics:
- Unit 1 ns
- Universal zero point 1.1.2000 00:00, i.e. for variable evaluations an offset of 2000 years has to be added
- Scope up to 64 bit (sufficient for 584 years). However, some EtherCAT slaves only support a 32 bit scope, i.e. the register overflows locally after approx. 4.2 seconds and starts again at 0.
The following 3 data types are recommended for handling DC times
- T_DCTIME from TcEtherCAT.lib
This is based on T_ULARGE_INTEGER and is therefore unsigned. It can be used for linking with suitable hardware variables - T_ULARGE_INTEGER from TcUtilities.lib
Unsigned 64-bit data type - T_LARGE_INTEGER from TcUtilities.lib
Signed 64-bit data type, negative numbers are represented in two's complement notation (underflow below 0 --> 0xFFFF FFFF FFFF FFFF etc.)
TcUtilities.lib (section INT64) provides numerous relevant functions. Of particular significance are the cast functions LARGE_TO_ULARGE and vice versa.
This type should be used when working with time differences that may be negative.
If TwinCAT is used for external synchronization, negative times will inevitably occur in the offset values.
64- vs. 32-bit representation Some EtherCAT slaves can only handle 32 bit values for representing the DC time or handle it as a process data. In order to prevent problems caused by overflow (every 4.2 seconds), we strongly recommend using 64-bit times in the controller.
|
This sample project
(Download) contains a function block that cyclically adds the high part to a 32-bit DC time to make 64 bits.
Reading the DC time
As described, there are several time sources on a TwinCAT PC; of these, the Distributed Clock time is crucial for the EtherCAT system. Usually, the first DC-capable slave is the EtherCAT master clock; the controller, with its software form of the DC clock, is synchronized to this master clock. Hence, the current DC time is available in the controller.
In the PLC there are 4 possibilities to access the current DC time, the "now" time, during task runtime.
- Readout of the DcSysTime input variable in the input data of the EtherCAT device (see System Manager description, not recommended)
- TcEtherCAT.lib F_GetActualDcTime
- TcEtherCAT.lib: F_GetCurDcTickTime
- TcEtherCAT.lib: F_GetCurDcTaskTime
Compatibility The functions F_GetActualDcTime and F_GetCurDcTaskTime can only be used with TwinCAT version 2.11 or higher (on programming and target system). |
Sample:
A PLC task is configured in the System Manager with a cycle time of 500 µs and 100 µs base time.
The evaluation of the above-mentioned procurement methods is enabled by Fig. Methods of reading the DC time:
At runtime of the PLC program the three F_Get... functions are now called at any desired point. Then, with TwinCAT 2.11 or higher, they return:
- F_GetActualDcTime: the actual current DC time. In the case of a repeated call within a task, F_GetActualDcTime thus also returns different values.
- F_GetCurDcTickTime: Moment of the last base time. A repeated call only returns different values if there lies at least 1 base time in between.
If base time = cycle time, or is called at the beginning of a task, then this function returns the same result as - F_GetCurDcTaskTime: ‘now’ time of this task, to which the slave shift times reference.
It is recommended to use this function as the basis of DC operations.
The contents of DcSysTime come from the field side master clock - this copying procedure is subject to fluctuations over time and therefore does not advance precisely with cycle time. This also applies to other local DcSystem times transferred as process data from the hardware.