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.
| Side | Signals | Meaning |
|---|---|---|
| Inputs | Enable, Home, JogP, JogN, TargetPos, TargetVel, Move, Abort | Enable, homing, jog ±, target position/speed, start, abort |
| Inputs | OriginDone, InPos (from drive/sensor feedback) | Home complete, in-position (read-only external states) |
| Outputs | Pulse, Dir (pulse/direction outputs), Busy, Done, Err, ErrID | Pulse generation and status reporting |
| In instance | State-machine step, ramp variables, target cache | Invisible 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.
| State | Action | Transitions |
|---|---|---|
| 0 Idle | Outputs clear, awaiting command | Home→1; Move→3 (needs OriginDone); Jog→4 |
| 1 Homing | Pulse toward home, await home signal | Home reached→2; timeout→5 |
| 2 Homed | Clear position count, set OriginDone instance flag | →0 |
| 3 Moving | Pulse along the ramp to target, monitor InPos | In position→0 (Done one beat); Abort→4 decel |
| 4 Jog / decel stop | Pulse while held / ramp down | Released→0 |
| 5 Error | Pulses off, Err + ErrID set | Fault 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".
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).
| Layer | Content |
|---|---|
| Timer interrupt (10 ms) | FB_Axis instance 1 (X), 2 (Y), 3 (Z) called in turn |
| Main scan | Process logic writes TargetPos/Move requests; reads Done/Busy for interlocks |
| Parameter area | Cfg 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.
① 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.
| Symptom | Cause | Fix |
|---|---|---|
| Axes interfere with each other | State in global M/D | Move everything into instance variables |
| Ramp wobbles | FB called from the main scan, unstable period | Call from a fixed-cycle timer interrupt |
| Move works without homing | Gate condition missing | Add "homed before entering Move state" |
| Abort ignored | Abort not wired into the machine | Every state answers Abort |
| Jog keeps moving after release | Jog level without a stop path | Release enters the decel path |
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.
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.
Acceptance bar, one sentence: on a two-axis project this FB works unchanged. If not, project-specific logic leaked inside — refactor until it passes.