Skip to content

9d planner#3807

Draft
grandixximo wants to merge 14 commits intoLinuxCNC:masterfrom
grandixximo:9d
Draft

9d planner#3807
grandixximo wants to merge 14 commits intoLinuxCNC:masterfrom
grandixximo:9d

Conversation

@grandixximo
Copy link

No description provided.

Luca Toniolo and others added 14 commits February 13, 2026 20:53
COMPLETED
=========

Architecture:
  * Dual-layer: userspace planning, RT execution
  * Lock-free SPSC queue with atomic operations
  * 9D vector abstractions (lines work, arcs TODO)
  * Backward velocity pass optimizer
  * Peak smoothing algorithm
  * Atomic state sharing between layers

Critical Fixes:
  * Optimizer now updates `tc->finalvel` (prevents velocity discontinuities)
  * Force exact stop mode (`TC_TERM_COND_STOP`) - no blending yet
  * RT loop calls `tpRunCycle()` every cycle (fixes 92% done bug)
  * Error handling uses proper `rtapi_print_msg()` instead of `printf()`

Verified Working:
  * Simple linear G-code completes (squares, rectangles)
  * Acceleration stays within INI limits during normal motion
  * No blend spikes (fixed)

KNOWN LIMITATIONS
=================

E-stop: 3x acceleration spike
  - Tormach has identical behavior (checked their code)
  - Industry standard for emergency stops
  - Safety requirement: immediate response
  - Acceptable for Phase 0

No Blending: Exact stop at every corner
  - Expected - Phase 4 feature
  - Prevents acceleration spikes without blend geometry

No Arcs: G2/G3 not implemented
  - Not needed for Phase 0 validation
  - `tpAddCircle_9D()` stub exists

Feed Override: Abrupt changes
  - Predictive handoff needed (Phase 3)
  - Works, just not smooth

FUTURE PHASES
=============

Phase 1: Kinematics in userspace
Phase 2: Ruckig S-curve integration
Phase 3: Predictive handoff, time-based buffering
Phase 4: Bezier blend geometry
Phase 5: Hardening, edge cases
Phase 6: Cleanup

FILES MODIFIED
==============

Core Planning:
  src/emc/motion_planning/motion_planning_9d.cc - Optimizer
  src/emc/motion_planning/motion_planning_9d_userspace.cc - Segment queueing
  src/emc/motion_planning/motion_planning_9d.hh - Interface

RT Layer:
  src/emc/motion/control.c - RT control loop fix
  src/emc/motion/command.c - Mode transitions
  src/emc/tp/tp.c - Apply optimized velocities
  src/emc/tp/tcq.c - Lock-free queue operations

Infrastructure:
  src/emc/motion/atomic_9d.h - SPSC atomics
  src/emc/tp/tc_9d.c/h - 9D vector math
  src/emc/tp/tc_types.h - Shared data structures

TEST
====

G21 G90 F1000
G1 X10 Y0
G1 X10 Y10
G1 X0 Y10
G1 X0 Y0
M2

Expected: Jerky motion (exact stop), completes without errors.
Extract kinematics math into HAL-independent headers and create userspace
kinematics infrastructure for trajectory planning.

Key changes:
- Extract pure math functions from all kinematics modules into *_math.h
  headers (5axiskins, corexykins, genhexkins, genserkins, lineardeltakins,
  maxkins, pentakins, pumakins, rosekins, rotarydeltakins, rotatekins,
  scarakins, scorbotkins, tripodkins, trtfuncs)
- Add kinematics_userspace/ with C interface for userspace kinematics
- Add HAL pin reader for accessing kinematics parameters from userspace
- Add motion_planning modules: Jacobian calculation, joint limits
  enforcement, path sampling, and userspace kinematics integration
- Add kinematics_params.h for shared parameter definitions
- Update trajectory planner (tp.c) to support userspace kinematics path
- Update INI parsing to load userspace kinematics configuration

This enables the 9D planner to compute inverse kinematics and perform
singularity-aware velocity limiting without RT kernel calls.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Phase 2 (Ruckig Integration) was merged from master.

This phase adds:
- Feed override handling with proper velocity/acceleration management
- 9-phase profile optimization for trajectory smoothing
- Downstream exit velocity capping for segment boundaries
- Tangent-aware Jacobian limits for per-axis joint constraints
- Backward pass optimization for profile consistency
- Jerk-aware motion planning refinements
…tation

Replace 1-segment downstream lookahead with multi-segment chain walk
(computeChainExitCap) that cascades reachability limits backward through
up to 16 downstream segments. Prevents exit velocities that create
impossible junctions on short segments or sharp kinks further ahead.

Add feed-limiting binary search in computeBranch: instead of rejecting
branches that exceed junction constraints, find the maximum feed that
produces a feasible exit velocity. Reduces branch rejections during
rapid feed override changes.

Add kink and chain exit cap checks in manageBranches that force branch
computation when the active segment's profile exits above constraints.
The backward fixup pass skips the active segment, so these checks are
the only runtime enforcement point.

Remove SPLIT_MISMATCH detection and velocity clamp from RT handoff
(tp.c) — upstream constraints now prevent the mismatch at the source.

Downgrade diagnostic messages (BRAKE_MAIN, PROFILE_GAP, FEED_LIMIT)
from ERR to DBG level.
@petterreinholdtsen
Copy link
Collaborator

Is there a way to add scripts in tests/ to demonstrate that this new code is working?

@BsAtHome
Copy link
Contributor

src/emc/motion/kinematics_params.h has fixed kinematics enum. The problem is that kinematics modules are loadable from anything the user want to make. Having them enumerated creates a problem for those kinematics not know in the default distro that the user adds himself. The loss of generic user-defined kinematics would be a great loss for us all. Or can you still load new modules with any and all TP?

@grandixximo
Copy link
Author

grandixximo commented Feb 16, 2026

src/emc/motion/kinematics_params.h has fixed kinematics enum. The problem is that kinematics modules are loadable from anything the user want to make. Having them enumerated creates a problem for those kinematics not know in the default distro that the user adds himself. The loss of generic user-defined kinematics would be a great loss for us all. Or can you still load new modules with any and all TP?

Planner 0/1 are unaffected, the TP works in Cartesian space and the RT module's kinematicsInverse/kinematicsForward symbols are resolved at load time as usual. Any user-written kinematics module works fine.

Planner 2 has a problem: it needs a userspace reimplementation of each kinematics module's math (for Jacobian computation, joint-space limit enforcement, path sampling). Currently kinematics_params.h enumerates known modules, and kinematicsUserInit() hard-fails for anything not in the list, aborting trajectory init entirely.

Three options to preserve compatibility with custom kinematics in Planner 2:

Fallback to identity kins in userspace, Treat unknown modules as trivkins for the userspace layer. RT still uses the real module. Joint limit enforcement would be approximate but conservative.

Downgrade to planner 0/1, If userspace kins init fails for an unknown module, automatically fall back to planner 1 (or 0) with a warning. Simplest fix, preserves "any kins works with any TP".

Generic RT-userspace bridge, Add a KINS_TYPE_GENERIC that calls the RT module's forward/inverse via shared memory. Correct but slower, and requires a new communication channel.

Which approach would you prefer?

@grandixximo
Copy link
Author

Is there a way to add scripts in tests/ to demonstrate that this new code is working?

The new code is not really done yet, I'll work on tests after things are stable enough, G64 is still not implemented, rigid tapping is missing, adaptive feed not tested, a few more things...

@grandixximo
Copy link
Author

at the moment still hardening the Feed Override system, it is quite complex still have not squashed all possible ways things could go wrong, but getting closer each day...

@BsAtHome
Copy link
Contributor

Preserving the generic plugable nature of kinematics across trajectory planners is a very nice feature and should be preserved if possible.
What is the structural (core) difference between the realtime kinematics and userspace kinematics? Isn't it possible to make the same base code plugable for both realtime and userspace use with the appropriate wrappers? Say, one source compiles both into a realtime module for TP=[0,1] and a userspace module for TP=2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants