MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do you encapsulate a stepper positioning routine in a function block (FB)?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do you encapsulate a stepper positioning routine in a function block (FB)?

Writing three axes' positioning logic three times triples the code and triples the fixes. Wrapping "one axis's positioning control" in an FB and creating three instances gives identical interfaces and isolated state — the textbook FB use case. This article delivers a deployable interface design, internal state machine and commissioning points to build from.

1. Interface design: decide the outside first

SideSignalsMeaning
InputsEnable, Home, JogP, JogN, TargetPos, TargetVel, Move, AbortEnable, homing, jog ±, target position/speed, start, abort
InputsOriginDone, InPos (from drive/sensor feedback)Home complete, in-position (read-only external states)
OutputsPulse, Dir (pulse/direction outputs), Busy, Done, Err, ErrIDPulse generation and status reporting
In instanceState-machine step, ramp variables, target cacheInvisible outside, instance-private

Design points: ① make all "axis personality" (pulses per revolution, max speed, soft limits) a configuration input (a Cfg structure) loaded from the parameter area at initialisation — one FB serves axes with different mechanics; ② ErrID distinguishes error types (Move before homing, target beyond soft limit, abort requested) — the field reads ErrID straight to the cause.

2. The internal state machine: five stations

StateActionTransitions
0 IdleOutputs clear, awaiting commandHome→1; Move→3 (needs OriginDone); Jog→4
1 HomingPulse toward home, await home signalHome reached→2; timeout→5
2 HomedClear position count, set OriginDone instance flag→0
3 MovingPulse along the ramp to target, monitor InPosIn position→0 (Done one beat); Abort→4 decel
4 Jog / decel stopPulse while held / ramp downReleased→0
5 ErrorPulses off, Err + ErrID setFault reset (edge)→0

Three rules: ① the state machine uses only instance variables — multi-instance zero interference; ② every state answers Abort; ③ "no Move before homing" gates entry to state 3, with ErrID recording "not homed".

3. Pulse generation and ramping: the instance's core algorithm

Pulse output binds the platform's pulse instruction/axis resource to the instance (one channel per axis). The ramp is implemented inside the instance: each control cycle (FB called from a 5~10 ms timer interrupt) speed steps toward target by acceleration, position integrates speed into pulses — all ramp variables instance-private, three axes' ramps independent. A fixed control cycle is essential: call the FB from a timer interrupt (every 10 ms), not the main scan (a wandering scan bends the ramp).

new speed = old speed + acceleration × period (clamped to target); pulse increment per cycle = new speed × period

4. Organising the three instance calls

LayerContent
Timer interrupt (10 ms)FB_Axis instance 1 (X), 2 (Y), 3 (Z) called in turn
Main scanProcess logic writes TargetPos/Move requests; reads Done/Busy for interlocks
Parameter areaCfg structure loaded from D area (one block per axis)

Main-scan and interrupt exchange via request-acknowledge handshake: the main sets Move, the FB executes in the interrupt and sets Done — the main touches only interface signals, never instance internals.

5. Commissioning points

① One axis first: tune only the X instance, unloaded and slow, verifying interface and state flow; ② watch the step number: monitor the instance's state variable — where it sticks is immediately visible; ③ ramps from slow: raise acceleration step by step — overshoot slows the ramp, sluggish crawl speeds it; ④ three-axis integration watches the interlocks: "X done before Z moves" lives in the main scan (reading each instance's Done), never inside the FB — the FB stays a single-axis affair; multi-axis coordination is upstairs business.

6. Common errors

SymptomCauseFix
Axes interfere with each otherState in global M/DMove everything into instance variables
Ramp wobblesFB called from the main scan, unstable periodCall from a fixed-cycle timer interrupt
Move works without homingGate condition missingAdd "homed before entering Move state"
Abort ignoredAbort not wired into the machineEvery state answers Abort
Jog keeps moving after releaseJog level without a stop pathRelease enters the decel path

7. Notes

The value of this wrapper is reuse and isolation: the next project's three axes take the FB, load configurations, and the interface is unchanged. Inside the FB hold two lines — all state instance-private, all timing fixed-cycle — and axis control, being "stateful + real-time", holds steady. However complex the line above it, to the FB it is only interface signals.

8. References

FB instance mechanism follows IEC 61131-3; pulse output instructions/axis resources follow the PLC's programming manual; timer-interrupt calling and cycle design follow the interrupt chapters; positioning safety (soft limits, abort, no motion before homing) follows GB 5226.1 / IEC 60204-1, with hard limits and E-stop remaining independent hardware loops.

9. Cautions

⚠️ Three traps to avoid:
① State variables lazily global — three instances trample each other's state machines;
② FB called from the main scan — unstable cycle, wobbly speed curves;
③ Multi-axis coordination interlocks written inside the FB — the FB loses its single-axis generality and the next project cannot reuse it.

Acceptance bar, one sentence: on a two-axis project this FB works unchanged. If not, project-specific logic leaked inside — refactor until it passes.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL