4

I'm confusing that whether the plc's cyclic execution can be seen as a program surrounded by the endless loop. If not, what's the difference between them?

Eric
  • 41
  • 2
  • That is a good way to look at it. It then reveals why you typically do NOT need to do any looping INSIDE the code, e.g. while (input == ON) do_this(); This is common code construct in C, but NOT inside a PLC code-block. Note that there are some valid cases where looping is needed/required (iterating an array to find a matching "whatever", but even THAT can be bad for your scan time if your "array" is rather large). – franji1 Jul 27 '18 at 21:24

2 Answers2

3

Your while(1) analogy is a good explanation of how the PLC's cyclic execution works.

First of all, you typically don't need to make a while(1) loop, since the PLC can be set up to call a specific main task, function or block cyclically. This task should then call all other necessary parts of your program by turn, just like a while loop would.
How the main task is set up depends on your brand of PLC, e.g. Siemens S7 will call the 'OB1' block and PLC's based on Codesys will call the 'MAIN' program block in each scan cycle.
Let's assume you want your program run with a cycle time not exceeding 10ms.

Some PLC's have the scan cycle triggered with constant intervals. This will allow you a constant cycle time of 10ms like your requirement.
If execution of the program always takes less time than the cycle time, all is good, and the PLC will just sit idle for the rest of the scan cycle until next time it is called. The active/idle ratio will be a good indication of how much the PLC is loaded.
If the cycle time is exceeded, it is possible that the PLC will skip a cycle, immediately try to catch up during the next cycle, signal the fault or halt the PLC. Or really any combination of the above, so you probably need to know about how to react to it.

Other PLC's have the scan cycle triggered as soon as the previous cycle ends. This will give you a faster but variable execution. For example the scan cycle can vary between 4-6ms, since the idle time is cut away, which is again below your requirement of 10ms cycle time.
In this case, you should also monitor the cycle time and have a plan for how to react to it being above your requirements. The PLC can probably be set up to signal a fault if it reaches a limit.

Also consider, that the PLC needs to read inputs and write outputs as part of or related to the scan cycle. How this is set up again depends on the PLC brand, but typically it will be done in each scan cycle, that is with the same cycle time.

pboedker
  • 523
  • 1
  • 3
  • 17
1

A PLC execution of one "task" could perhaps look something like the following. You can think that one PLC task loop (while loop) is similar to a thread in standard programming.

while(1)
{
    //Read hardware inputs etc.
    ReadInputs();

    //Run user PLC code
    Programs();

    //Write hardware outputs
    WriteOutputs();

    //Wait until cycle time has been elapsed
    Wait();
}

Of couse, there are also some system own programs anddiagnostics to run, but basically the idea is the following:

  1. Read I/O input values and place them in memory for user programs
  2. Run user program codes
  3. Write output variables from memory to I/O
  4. Wait until cycle time has established. For example, the program could take 1 ms and the cycle time could be 10 ms. In that situation, 9 ms is waited until the same thing will run again.

If the cycle time takes over 10 ms, and exception could be thrown, or not. Depends on platform and settings. The idea of PLC is that the "task" is being run at very same interval every time. So you (should) always know that the last time of execution of the program was 10 ms ago. This helps controlling as you know the time difference.

For some non-automation programmers, it seems to be hard to understand the basic PLC program flow. It's important to understand that you can for example overwrite the output variable many times in the program, and only the last value will be saved to I/O memory.

Quirzo
  • 1,183
  • 8
  • 10