任务周期时间是一项实用数值,工程师可用它来了解控制器应用在运行期间的表现。 如果任务耗时超过预期,可能影响过程时序、同步、故障排除及系统稳定性。

CODESYS 是一款用于按照 IEC 61131-3 标准编写控制器应用开发环境, 其广泛应用于自动化与控制系统中,适用于任务需在既定时间条件下可靠运行的场景。

在 CODESYS 项目中,监测实际任务周期时间有助于工程师优化性能、识别瓶颈并检测可能影响机器工序的时序行为。 当控制任务、通信任务或运动相关功能需要保持在特定时序窗口内时,这一点尤为重要。

本教程展示了在 CODESYS 中测量实际任务周期时间的两种方法:

  • 使用内置的 TIME() 函数
  • 使用 CmplecTask 库

两种方法都非常有用。 该选择哪一种取决于所需精度以及分析任务行为的深入程度。

任务周期时间描述了控制系统中执行特定任务或操作所需的时间。 在控制器应用中,该值有助于判断代码是否在预期的时间范围内运行。

监控任务周期时间可为多项工程任务提供支持:

  • 检查任务是否保持在其配置的周期时间内
  • 识别导致运行时间增加的代码段
  • 检测意外的时序变化
  • 支持调试过程中的故障排除
  • 改善过程同步
  • 帮助稳定控制器行为

稳定的任务周期时间并不能解决所有控制问题,但它为工程师提供了一个有用的起点。 如果实际周期时间比预期长,或波动较大,则可能需要对应用进行更深入的分析。

在 CODESYS 中,任务定义了程序或功能的执行时机与执行方式。 任务周期时间就是完成该次执行所需的时间。

例如,一个循环任务可以配置为每隔几毫秒运行一次。 如果任务内部的代码执行时间过长,控制器可能无法在预期的时间窗口内完成任务。 这会影响性能、过程同步或运动行为。

由于实际值可能受到多种因素的影响,测量任务周期时间可能颇具挑战:

  • 系统负载
  • 网络延迟
  • 硬件性能
  • 任务配置
  • 软件架构
  • 通信行为
  • 任务内部代码的数量与结构

正因如此,测量方法的选择很重要。 对于基础检查,简单的计时方法可能已经足够。 而当需要微秒级计时或任务特定信息时,则需要更精确的方法。

在 CODESYS 中测量经过时间的一种简单方法是使用内置的 TIME() 函数。

其基本逻辑十分简单:
1. 在代码段运行之前捕获起始时间。
2. 执行需要测量的代码。
3. 在代码段运行之后捕获结束时间。
4. 从结束时间中减去起始时间。
5. 将结果用作测量得到的经过时间

在该示例中,使用两个 FOR 循环来模拟一项耗时任务。 TIME() 函数捕获循环段前后的时间,两者之差即为任务运行时间的估算值。

若需快速了解某段代码的执行时长,这种方法很有帮助。

该示例中,测得的任务周期时间通常在 6 到 8 毫秒之间。 可添加跟踪来随时间监控测量变量。 在例子中,周期偶尔会跳升至10毫秒。

这种波动很关键。 如果一个软运动任务的周期时间配置为 4 毫秒或更短,超出该配置周期时间可能会引发异常。 因此,对于运动或同步要求严格的应用,任务时序尤为重要。

TIME() 功能之所以有用,是因为它很简单。 它所需的代码量少,易于理解,并且可以快速添加到现有项目中。

它适用于以下情况:

  • 您需要进行快速的时间检查
  • 毫秒分辨率已经足够
  • 想要测量特定的代码段
  • 应用不需要高精度时序数据

然而,TIME() 函数也存在局限性。 它提供的值仅具有毫秒级分辨率。 对于需要微秒级分析的应用而言,这可能不够精确。

此方法还需要手动跟踪时间。 您需要定义测量的起点、终点以及经过时间的计算方式。 对于简单的检查,这是可以接受的,但当需要在较大的程序中获取特定任务的运行时间信息时,这种方法可能就不太方便。

对于更精确的任务周期时间测量,CmplecTask 库通常是更佳选择。

The CmpIecTask library is included with CODESYS. This library provides functions for accessing information from the IEC Task Manager.

Once the library is added to a CODESYS project, initialization requires only a small amount of code. After initialization, the program can access a structure with detailed task information, including values such as actual task cycle time and average cycle time.

This approach is more task-specific than manually measuring time before and after a code section. Instead of creating your own timing logic, you use the task information provided through the library.

In the original example, the same simulated working load is used so the measurement methods can be compared. The difference is that the CmpIecTask library handles the task-cycle-time information more directly.

The CmpIecTask library offers several advantages when you need more precise task timing.

First, it can be integrated flexibly into different parts of the program. This makes it easier to access task information without manually placing start and end time measurements around every code section.

Second, the library manages the recording and calculation of task-cycle-time information. This reduces manual effort and lowers the risk of errors in the measurement logic.

Third, the library provides values with microsecond-level resolution. This is more precise than the values obtained with the TIME() function and can be important for applications with tighter timing requirements.

The main drawback is that the library must be added to the CODESYS project. However, for many applications, that is a small trade-off compared with the additional precision and task-specific information it provides.

Both methods can help you understand task timing, but they are not equally suited for every situation.

Use the TIME() function when you need a simple, fast check, and millisecond-level resolution is enough. It is easy to implement and useful for basic timing comparisons.

Use the CmpIecTask library when you need more precise task information, want to avoid manual timing logic or need microsecond-level resolution for performance analysis.

For motion tasks, synchronization tasks or controller applications with tighter timing requirements, the CmpIecTask library is usually the better choice.

在调试或故障排除期间需要评估控制器性能、运动行为或机器同步时,CODESYS 任务时序尤为重要。

在 Festo 软件环境中,CODESYS 可作为更广泛的工程与控制工作流程的一部分。 Festo AX Controls 支持基于 IEC 61131-3 标准编程(包括 CODESYS)的确定性实时控制,并支持 C/C++、C# 和 Python 等高级语言。

Festo Automation Suite 支持调试、编程和诊断。 它可以将拓扑中的 CEPE 等 AX Controls 设备传输至 CODESYS 编程环境,无需额外配置步骤,用户可直接继续编写机器序列。

请将本教程用作检查任务时序的 CODESYS 实用方法。 然后,根据您的下一步工程需求,继续使用相应的 Festo 软件、许可证或学习资源。

  • 任务周期时间反映了在控制系统中执行特定任务或操作所需的时间。
  • 监测任务周期时间有助于工程师优化性能、检测瓶颈并提高系统稳定性。
  • TIME() 函数易于使用且实现快速,但其提供的分辨率为毫秒级。
  • CmplecTask 库提供任务特定信息及微秒级分辨率。
  • 使用 TIME() 函数进行简单检查,使用 CmplecTask 库进行更精确的周期时间分析。
  • 严格的运动控制或同步任务通常需要更仔细地考虑实际任务时序。
  • Festo 软件与支持资源可帮助用户从 CODESYS 故障排除延伸至调试、许可及边缘控制实施。