Trajectory planning specifically (kinematics, blending, path smoothing) doesn't have to be in realtime, but there are a few reasons it was done that way:
- Blending used to be done at runtime (before we added lookahead), so there wasn't really a need for complex lookahead / optimization. Trajectory "planning" was mostly just creating the motion structures (line, circle, tapping) and adding them to the queue.
- Motion commands other than lines / arcs can affect future motions (blend mode, spindle sync), so they have to be executed in order. Many motion commands directly act on (realtime) motion state, so it made sense to handle incoming commands in the realtime side.
- When we did add lookahead, it was easier to graft it on to the existing realtime TP (especially for me as a relative novice to LinuxCNC architecture).
I agree that it would definitely be better to have more planning in userspace (more consistent / faster servo updates, easier to do more complex planning). I've been thinking about how to do this in manageable steps
- Treat "immediate" motion commands (pause, resume, config changes, etc.) the same as today (command handler in command.c)
- For actual moves and move-related commands (blending, spindle-sync), create a queue for optimization / lookahead in the user half of the user-motion interface.
- Add a motion command that pops an optimized move out of the user-space queue and sends it (fully-formed) to the motion command handler.
- Move geometry processing out of realtime TP (tpAddLine, tpAddCircle, path blending) to this new layer.
- (optional) move geometry processing out of emccanon if possible (circle / line math, naive CAM detection, etc.)
With a simple queue, some lookahead still has to be done in realtime with this arrangement (the TP has to be able to stop at the end of the last queued motion in case userspace stalls). A more sophisticated queue that allows shared access for both motion and userspace would make it possible to move all of the lookahead calculations out of realtime.