for loop in PLC causes delay in cycle time

huangapple go评论48阅读模式
英文:

Does for loop in PLC causes delay in cycle time

问题

我已使用Beckhoff PLC(结构化文本语言)一段时间,尽量避免在算法中使用for循环,以确保一切都在实时工作中不会陷入任何循环。时间对我正在进行的算法非常重要。代码中的每行都在1毫秒内完成。如果我在某个地方使用for循环,我认为当进入for循环时,每次迭代将花费1毫秒,因此其余的软件将等到循环完成才会继续。

出于这个原因,我没有编写任何循环,但我的一位同事使用了for循环来实现他的软件。循环从1到100,000开始,每1毫秒,计数器增加100,000,其他计数器也在循环之外每1毫秒增加。

我知道的程序运行方式是,如果PLC在处理1毫秒以下的一切后还有时间,它会处理剩下的部分,这可能是诊断检查或IO检查或其他任务。

那么,如何能够处理100,000次迭代,而我确定周期时间为1毫秒呢?如果我在程序中使用100万个不同的循环会怎样?它是否能够处理这么多循环,即使软件非常庞大?我将尝试并观察,但为了获得更多见解,我想与您讨论。

英文:

I am using Beckhoff PLC(Structured Text language) for a while and trying not to use for loops in all algorithm to make sure everything works in real time and won't get stuck in any loop. Time is so important for algorithms that I am working on. Any line in the code finishes in 1 ms. If I use for loop in somewhere I was thinking that when it comes into for loop, it will take 1ms for every iteration so the rest software will stay untill it finishes the loop.

For this reason I haven't writing any loop but one of my colleagues implemented his softwares with for loop. Loop starts from 1 to 100000 and every 1ms, counter increases 100000 and other counters increasing 1ms which are out of the loop.

The procedure that I know is, If PLC has time after handle everything below 1ms, it cares the rest which might be diagnosis check or IO check or whatever.

So how it can handle 100000 iteration while I identify cycle time is 1ms. What if I use 1 million different loop in program. It can handle for loop also because the program is so small. Would it behave same while software is huge? How I can understand it can finish all loops without causing overflow in cycle time?
I will try and see but for gaining insights I would like to discuss with you.

答案1

得分: 2

以下是已翻译的内容:

没有通用的答案来回答是否可以使处理保持在您期望/需要/首选的扫描周期时间内。

计算能力是有限的。循环的每次迭代都需要非零的计算能力,因此,当然可以有比您的PLC处理能力更多的循环(和/或循环的迭代)。在您的PLC中没有魔法可以使循环“免费”。另一方面,总的来说,循环通常很快(与循环内部执行的处理相比)。

只有在循环浪费时,循环本质上是次优的。替换执行N次迭代的循环在计算上不比不使用循环编写N次相同的代码更糟糕。

循环可能浪费的迹象:

  • 它仅用于等待某事或轮询某些数据
  • 它反复对相同的数据执行相同的操作
  • 它不使用匹配的数据结构(如数组),供您迭代

在PLC中,您需要确保您的循环执行的处理可靠地适合您的周期时间。最糟糕的情况是您关心的:如果您的循环99.9%的时间需要0.01毫秒,但最后的0.1%需要1秒,那是不可行的。

如果循环是最适合算法的方法(从算法效率、清晰度和可维护性的角度来看),它很可能是表达解决方案的最快(或非常接近最快)方法。您只应该担心循环是否编写浪费的循环。

最终,如果您的PLC不足以完成需要完成的所有任务,即使有一个良好编码的解决方案,循环的存在与否都不是问题或解决方案。但是假设您的PLC速度很快。非常快。我在树莓派上运行具有数十万个变量的程序,每个周期执行许多循环,并且周期时间低于0.1毫秒。对于天生“循环”的任何事情,我从不回避循环,但我的循环从不阻塞等待某事,它们从不分配内存或执行会引入执行时间的有意义变化的处理,它们执行“简单”的操作(其中“简单”的理解是计算机现在确实非常快)。

真正的问题不是循环好还是不好,而是您能否确定何时使用它们,以及如何编写良好的循环。问题不在于循环是好还是坏,而在于您是否能够熟练应用它们。

此外,进行测试。查看在周期时间受到影响之前您可以执行多少次空循环的迭代。尝试使用执行您关心的操作的非空循环进行测试。您将迅速了解自己的真正瓶颈,并且可能不再担心循环,而将重点放在循环内部的代码上。

英文:

There is no universal answer to the question of whether processing can remain within your expected/required/preferred scan cycle time.

Computing power is bounded. Each iteration of a loop takes non-zero computing power so, of course, there can be more loops (and/or iterations of loops) to process than what your PLC can handle. There is no magic in your PLC that makes looping "free". On the other hand, looping is, generally speaking, very fast (compared to the processing that is performed inside the loop).

Loops are inherently sub-optimal only if they are wasteful. Replacing a loop that performs N iterations is not computationally worse than coding N times the same code without a loop.

Signs that your loop may be wasteful :

  • It is used only to wait for something or poll some data
  • It repeatedly does the same thing over the same data
  • It uses no matching data structure (such as an array) that you iterate over

In a PLC, you need to make sure that your loops perform processing that reliably fits within your cycle time. The worst case is what you care about : if your loop takes 0.01ms 99.9% of the time, but the last 0.1% requires 1 second, it will not work.

If a loop is what best fits an algorithm (in terms of algorithm efficiency, clarity, maintainability), it is likely to be either the fastest (or very close to the fastest) method to express your solution. You should only be afraid of loops if you code wasteful loops.

In the end, if your PLC is not powerful enough to do everything that needs to be done even with a well-coded solution, the presence or absence of loops is not the problem or the solution. But assume your PLC is fast. Very fast. I run programs with hundreds of thousands of variables, many loops executing every cycle, and am getting cycle times below 0.1ms on a Raspberry Pi. I never avoid loops for anything that is "loopy" by nature, but my loops never block waiting for something, they never allocate memory or perform processing that will introduce meaningful variation in execution time, and they do "simple" things (where "simple" is read with the understanding that computers are truly very fast nowadays).

The real question is not whether loops are good or bad, but whether you are able to identify when to use them, and know how to code them well. It is not about loops being good or bad, but rather you being good or bad with them.

Also, test. See how many iterations of an empty loop you can perform before your cycle time suffers. Try it with non-empty loops that perform things you care about. You will learn about your true bottleneck quickly, and are likely to stop worrying about loops and focus on code inside the loop.

huangapple
  • 本文由 发表于 2023年2月6日 03:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定