IMDHMIControls LibraryShop main store

Comparison

Which IEC 61131-3 language fits a cyclic machine controller

PLC programming Published 8 min read

Short answer

For a cyclic machine controller such as a telescope dome, write the control in structured text or ladder on the native IEC 61131-3 cyclic task and add SFC only for the step sequence. The interlock must be ANDed into each output every scan, so a lost permissive clears the drive command within one sweep, typically 10 to 20 ms. Reserve a TwinCAT C++ module for numeric work structured text cannot express, not for the interlock layer.

On this page
  1. Why the cyclic sweep decides the language choice
  2. Comparing the options on the cyclic task
  3. Worked example: gating a dome slew output in structured text
  4. What a C++ module adds, and what it costs
  5. Program layout that keeps the permissive in every pass
  6. The decision for each case
  7. Questions asked

Write the machine control in structured text or ladder logic on the native IEC 61131-3 cyclic task, and keep every interlock in that sweep. That is the decision for a cyclic machine controller such as a telescope dome on TwinCAT 3: the conditions that enable motion — drive ready, motor temperature, wind, end of travel — must be re-tested on every pass, and the IEC languages do that as their normal mode of operation. A C++ module runs in the same runtime but needs an extra runtime licence, module signing and extra integration steps, so it earns its place only for numeric work structured text cannot express. This article compares the options on execution model, licensing, online debugging and how naturally each one keeps a permissive live in every pass.

Why the cyclic sweep decides the language choice #

An IEC 61131-3 program runs as an endless cyclic pass — the sweep — not as a routine with a start and an end. Each pass reads the inputs into the process image, executes the logic, and writes the output image to the terminals. TwinCAT presents variables delivered over the fieldbus as if they were wired I/O, and every value the logic reads stays constant for the whole pass, so the program works on one consistent snapshot. A watchdog checks that the user code finishes inside the configured cycle: a pass that runs late is restarted at its beginning, and repeated overruns can stop the task. Machine-control tasks are commonly configured between 10 and 20 ms.

That execution model is what decides the language. The conditions that enable motion — drive ready, motor temperature, wind, end of travel — can drop out in the middle of a move: wind can shift part of a dome or telescope, a motor can overheat, a mechanism can jam. A step-only state machine tests each condition once on step entry and then waits for a transition, so it leaves the actuator energized after the condition is gone. The layout that fixes this is explicit: on every pass, recompute the combined permissive and gate the output with it, so a lost condition removes the actuator within one pass. The phase-by-phase detail of that read–logic–write pass is covered in what each pass of the PLC scan cycle does.

Comparing the options on the cyclic task #

IEC 61131-3 defines ladder diagram, function block diagram, structured text, sequential function chart and instruction list, with instruction list deprecated in the third edition of the standard. TwinCAT adds CFC, a signal-flow form of function block diagram, and C++ modules that execute inside the runtime. For a broader task-by-task mapping of the languages, see which IEC 61131-3 PLC language to use for each control task.

Option Where it runs Extra runtime licence Online watch and force Fit for cyclic interlock scanning
Structured text Native cyclic task None beyond the base PLC runtime Live watch and modify of variables during operation Best: one expression computes the permissive, one line gates the output
Ladder diagram Native cyclic task None beyond the base PLC runtime Live watch and force of contacts and coils Best: the permissive reads like the wiring diagram
Function block diagram or CFC Native cyclic task None beyond the base PLC runtime Live watch of block inputs and outputs Good: signal flow suits gating logic
Sequential function chart Native task; the IDE supplies the step and transition engine None beyond the base PLC runtime Live watch of the active step Sequence only: keep permissives outside the chart
C++ module TwinCAT task, restricted C++ dialect C++ runtime licence on the target, product line TC1300 Harder than the IEC languages Poor for interlocks; suits numeric modules
C++ pass emulation on a PC A central loop you build and police yourself None on the controller side Tooling you build yourself Last resort, only where no controller is allowed

On the tooling side the choice is neutral: the engineering environment for IEC 61131-3 PLC programming installs without a licence on any number of development computers. Structured text itself reads much like Pascal, C++ or C#, which matters if you arrive from software. And because the IEC languages are standardized, the logic you write carries between vendors' controllers; the shared-logic argument is laid out in why IEC 61131-3 logic carries across controller platforms.

Worked example: gating a dome slew output in structured text #

The case is an azimuth slew on a telescope dome, run from the cyclic PLC task of the controller. A permissive can drop mid-move, so the drive enable must clear within one pass when it does.

The inputs, and where each comes from:

  • Task cycle time: 10 ms — the configured cycle of the PLC task, inside the common machine-control range.
  • bDriveReady — drive status bit from the process image.
  • bMotorOTok — motor thermal contact, TRUE while the motor is below its trip temperature.
  • bWindOk — wind permissive, TRUE while wind is below the slew limit.
  • bCmdSlew — the motion request from the sequence layer.
  • qSlewEnable — the output image bit wired to the drive enable.

The rule in words: combine every condition that must hold during motion into one permissive with an AND, then derive the actuator output as the motion request AND that permissive — both statements in the same pass, with no latch or step flag between them.

PROGRAM MAIN
VAR
    bDriveReady     : BOOL := FALSE; // drive status bit, process image
    bMotorOTok      : BOOL := TRUE;  // thermal contact, TRUE = below trip
    bWindOk         : BOOL := TRUE;  // wind below the slew limit
    bCmdSlew        : BOOL := FALSE; // request from the sequence layer
    bSlewPermissive : BOOL := FALSE;
    qSlewEnable     : BOOL := FALSE; // output image bit to the drive
END_VAR

bSlewPermissive := bDriveReady AND bMotorOTok AND bWindOk;
qSlewEnable     := bCmdSlew AND bSlewPermissive;

The arithmetic is timing. The failing condition is captured at the start of a pass, the AND clears the enable in that same pass, and the output image is written to the terminal at the end of the pass, so the worst case from capture to terminal is 1 pass × 10 ms = 10 ms. Nothing in the logic holds the output on.

The test that proves it: with bCmdSlew TRUE and all three conditions TRUE, qSlewEnable reads TRUE in the online view and the drive enable terminal is on. Force bWindOk FALSE and leave the request TRUE: on the next pass qSlewEnable reads FALSE and the terminal drops, within the configured cycle. A step-only machine fails the same test, because it checked the wind condition once at step entry and holds its enable until a transition, so the same gust leaves the drive enabled for the rest of the move.

Decision: any permissive that can drop mid-move gates the output on every pass, and the clearance bound is one configured cycle; if the machine needs a faster drop-out than that, shorten the task cycle — the language does not set the bound.

What a C++ module adds, and what it costs #

TwinCAT 3 can execute C++ in real time inside the runtime; the product line is TC1300, TwinCAT 3 C++. The dialect is restricted, not standard C++, and integrating the module into the TwinCAT task takes extra steps compared with the IEC languages. The target needs the C++ runtime licence in addition to the base PLC licence, and Beckhoff recommends a TwinCAT 3 licence dongle for platform levels 90-94. Two further requirements are documented: modules are loaded by the runtime and must be signed with a TwinCAT user certificate, and under Beckhoff RT Linux the modules cannot currently be used to their full extent, with the documented minimum install at version 4026.21 (x64-E).

Where the module earns its place: a numerically heavy calculation that structured text cannot express efficiently — a custom filter, geometric tracking, a special control algorithm. Where it does not: inputs, interlocks and outputs stay in the IEC task, so the permissive is recomputed every pass and every variable stays watchable online.

The last option, emulating the pass in C++ on a PC, is workable: one central loop that reads sensors, computes and writes actuators, with no blocking calls and bounded execution time. It is the fallback for a machine that must not have a controller at all, and it hands you the full cost of rebuilding the runtime — the question at the end of this article lists what that means.

Program layout that keeps the permissive in every pass #

  1. Map every field signal to a variable in the I/O configuration: drive status word, motor thermal contact, home and limit switches, wind status, and the drive commands going back.
  2. Condition the inputs at the top of the pass — debounce, scaling — with no blocking calls anywhere in the program.
  3. Compute one combined permissive from the conditioned inputs, every pass.
  4. Let the sequence layer — an SFC network or a CASE statement on a step variable — issue motion requests only.
  5. Derive each actuator output as its request ANDed with the combined permissive, so the output state is always the current permissive, never a remembered one.
  6. Verify in the online task view that execution time sits below the configured cycle and that no overrun counter increments.

Emergency stop and end-of-travel protection also belong in a hardware or safety-rated path; the permissive layer complements that path and never replaces it.

The decision for each case #

  • Cyclic machine whose permissives can drop mid-move — a dome slew, a shutter, a conveyor with interlocks: structured text or ladder on the native cyclic task, output equal to request AND combined permissive, recomputed every pass; SFC only for the sequence, permissives outside the chart. A lost condition clears the output within one pass, at whatever cycle you configured.
  • Simple sequence with no mid-move hazard: SFC alone, with short actions, is the natural fit.
  • Numeric work beyond structured text: a C++ module, product line TC1300, beside the IEC task — after confirming the runtime licence, the platform level, certificate signing and the RT Linux limits. Inputs, interlocks and outputs stay in the IEC task.
  • No controller permitted at all: emulate the pass in C++ on a PC with one central loop and bounded execution — the last resort, built only when the runtime is excluded.

Questions asked

Does the 10 to 20 ms cycle time apply to every controller?

No. The cycle is the task time you configure; 10 to 20 ms is a common range for machine-control tasks, not a fixed standard value. Set the cycle from the fastest reaction the machine needs, then verify in the online task view that measured execution time stays below it. The watchdog polices the configured cycle, and the interlock clearance bound follows whatever cycle you set.

Where does a C++ module stop and the structured text program start?

Let the module own only the calculation — a filter, geometry, an algorithm structured text cannot express efficiently — and exchange results with the IEC program through variables in the cyclic task. Inputs, interlocks and outputs stay in structured text or ladder, so the permissive is re-tested every pass and stays watchable online. The module then cannot strand an actuator when a condition drops.

What do you give up by emulating the PLC pass in C++ on a PC?

The runtime itself. The controller gives you the process image, the cycle watchdog, static memory for function block instances, and live watch and force of every variable during operation; a hand-written loop has none of that until you build and test it. You also inherit the PLC rules — no blocking calls, bounded execution — as your own discipline rather than the watchdog's enforcement.

Does it matter whether the interlock layer is ladder or structured text?

Not for behavior. Both run on the same cyclic task and re-test every condition each pass, so the output clears equally fast either way. Choose by readability and team skill: contact-and-coil logic reads like the machine's wiring diagram, which helps maintenance, while structured text suits computed permissives and comparisons and reads like the Pascal or C# family to a developer.

Keep reading

  • Selection guide

    Which IEC 61131-3 PLC language to use for each control task

    Match each control task to the IEC 61131-3 language whose form shows its dominant data path: ladder for discrete interlocks, structured text for calculations and data handling, function block diagram for connected signal functions, SFC for phased sequences. Instruction List is deprecated, so new code uses the other four, and the controller's supported languages set the final limit.

    PLC programming

  • Selection guide

    S7-300 vs Q series IEC 61131-3 language support

    Both platforms meet IEC 61131-3 once you buy the right toolchain: S7-GRAPH gives the S7-300 sequential function chart with function block calls from steps, and GX Works3 gives the Q series the SFC nesting that IEC Developer V5.0 lacked. With languages equal, integrated motion decides: one QD77MS16 module synchronizes 16 axes over SSCNET III/H at a 0.222 ms cycle, while the S7-300's 4-axis FM 357-2 is discontinued. Choose Q with GX Works3 for new multi-axis machines; stay with Siemens only inside an installed S7-300 base.

    PLC programming

  • Troubleshooting

    When a CODESYS TON works in structured text but not ladder

    The structured text routine counts because its four statements run in sequence inside one scan; the ladder version fails when the reset network clears Timer.Q before the addition samples it, or an unconditional reset restarts the timer every cycle. Put the call, the Q test, the addition and the reset of the same TON instance in one ladder network, left to right in that order, and it counts exactly one B per interval.

    PLC programming