Problem:
Both logic operations and communication processing in TwinCAT PLC consume CPU resources. To reduce the resource usage of the communication part, this article introduces some basic principles and optimization techniques.Content:
- Typically, only one Task with the same cycle should run on a single CPU core.
- Staggering multiple PLC Tasks with the same cycle can stabilize CPU utilization.
- Running NC cycles offset from PLC cycles can further stabilize CPU utilization.
- Correspondence principles between I/O variables, Tasks, and EtherCAT.
- Principles for determining which Task an I/O variable belongs to.
- Methods for adjusting the Task to which an I/O variable is assigned.
- Typically, only one Task with the same cycle should run on a single CPU core
- Staggering multiple PLC Tasks with the same cycle can stabilize CPU utilization
All Tasks have a start offset of 0, meaning that in the first tick of the TwinCAT real-time kernel, all Tasks execute sequentially.Assume there is a task with a total execution time of 2.1 ms. The default execution sequence is:
Continuation (Professional English Translation):If this large task is split into four smaller tasks—Task 1 through Task 4—with execution times and Start tick values as follows:
The total execution time of the four Tasks remains 2.1 ms. Under otherwise unchanged conditions and ignoring task switching overhead, the execution sequence is:
Task | Execution Time (ms) | Start Tick |
|---|---|---|
Task 1 | 0.5 | 0 |
Task 2 | 0.5 | 3 |
Task 3 | 0.5 | 6 |
Task 4 | 0.6 | 8 |
3. Running NC cycles offset from PLC cycles can further stabilize CPU utilizationIf the default 2 ms NC task and 10 ms PLC task have their Start tick values set to 0 and 1 respectively, NC and PLC tasks will run staggered. As shown in the figure:
3. Running NC cycles offset from PLC cycles can further stabilize CPU utilization
(Continued)The upper half of the figure shows the default setting: both the PLC and NC tasks have Start tick = 0. As a result, every 10 ticks, the NC and PLC tasks must execute simultaneously.The lower half of the figure shows the effect after setting the two tasks’ Start tick values to 0 and 1 respectively—the two tasks no longer execute in the same tick.In actual projects, the default settings are usually sufficient. This is provided merely as an optimization concept—when issues arise, you can try adjusting from this perspective.
4. Correspondence principles between I/O variables, Tasks, and EtherCATBy default, an EtherCAT Frame is triggered by a Task during the Output phase. The Task to which an I/O variable belongs, together with the I/O module it is mapped to, determines which Frame will collect its data. Therefore:
(Continued)The upper half of the figure shows the default setting: both the PLC and NC tasks have Start tick = 0. As a result, every 10 ticks, the NC and PLC tasks must execute simultaneously.The lower half of the figure shows the effect after setting the two tasks’ Start tick values to 0 and 1 respectively—the two tasks no longer execute in the same tick.In actual projects, the default settings are usually sufficient. This is provided merely as an optimization concept—when issues arise, you can try adjusting from this perspective.
4. Correspondence principles between I/O variables, Tasks, and EtherCATBy default, an EtherCAT Frame is triggered by a Task during the Output phase. The Task to which an I/O variable belongs, together with the I/O module it is mapped to, determines which Frame will collect its data. Therefore:
Task cycle = EtherCAT Frame cycle = I/O refresh cycle
A single EtherCAT master can have at most 4 periodic Frames, whereas TwinCAT supports up to 61 Tasks. If the number of Tasks that require I/O mapping is 4 or fewer, each such Task will trigger its own dedicated Frame. This mapping type is called “synchronous mapping” (Synchron), as shown in the figure:If the number of I/O-mapping Tasks exceeds 4, lower-priority Tasks will use “asynchronous mapping” (Asynchron), as shown in the figure
Asynchronous mapping requires extracting the target data from a synchronous mapping Frame instead of immediately triggering a new Frame to refresh its own I/O. This subtle difference only needs to be considered in high-speed applications.To check which Task’s Frame will refresh a specific module, click the Sync Unit Assignment button on the EtherCAT device to open the “Sync Unit Assignment” dialog:
5. Principles for determining which Task an I/O variable belongs toPLC programs cannot directly specify which Task an I/O variable belongs to, yet the Task assignment determines the I/O data refresh rate. TwinCAT automatically assigns I/O variables to Tasks according to default rules. The assignment logic differs between TwinCAT 2 (TC2) and TwinCAT 3 (TC3):- TC2: All I/O variables default to the highest-priority PLC Task (typically the one with the shortest cycle). Manual reassignment is allowed.
Each EtherCAT communication cycle consumes CPU resources. More EtherCAT masters and shorter cycles result in higher CPU load. - TC3: An I/O variable belongs to the highest-priority Task among those that reference the POU containing the variable. Unused variables are assigned to the lowest-priority Task among all referenced Tasks in the PLC project. Manual adjustment is not permitted—this behavior is more rational and usually requires no special tuning.
6. Methods for adjusting the Task to which an I/O variable is assignedIn real-world applications, large systems often use the CU2508 port multiplier to expand EtherCAT master count and enable cable redundancy with DC slaves. For users adding EtherCAT networks later, consider:- Increased CPU utilization due to additional masters (ensure the original CPU has headroom).
- Optimization potential—e.g., moving all variables of an entire EtherCAT network to a slower Task to reduce communication frequency and thus lower CPU load.
Note: Adjust all variables together. If even one variable requires fast refresh, the entire network will refresh at that rate. Mixing fast and slow cycles on the same network generates multiple Frame cycles, increasing CPU overhead.- In TC2: Simply drag and drop the variable to another Task.
- In TC3: Direct movement is not allowed, but you can specify the target Task via the variable attribute TcContextName. For example:
- TC2: All I/O variables default to the highest-priority PLC Task (typically the one with the shortest cycle). Manual reassignment is allowed.
Each EtherCAT communication cycle consumes CPU resources. More EtherCAT masters and shorter cycles result in higher CPU load. - TC3: An I/O variable belongs to the highest-priority Task among those that reference the POU containing the variable. Unused variables are assigned to the lowest-priority Task among all referenced Tasks in the PLC project. Manual adjustment is not permitted—this behavior is more rational and usually requires no special tuning.
6. Methods for adjusting the Task to which an I/O variable is assignedIn real-world applications, large systems often use the CU2508 port multiplier to expand EtherCAT master count and enable cable redundancy with DC slaves. For users adding EtherCAT networks later, consider:
- Increased CPU utilization due to additional masters (ensure the original CPU has headroom).
- Optimization potential—e.g., moving all variables of an entire EtherCAT network to a slower Task to reduce communication frequency and thus lower CPU load.
Note: Adjust all variables together. If even one variable requires fast refresh, the entire network will refresh at that rate. Mixing fast and slow cycles on the same network generates multiple Frame cycles, increasing CPU overhead.
- In TC2: Simply drag and drop the variable to another Task.
- In TC3: Direct movement is not allowed, but you can specify the target Task via the variable attribute TcContextName. For example: