Skip to content

Multiaxis kinematics and limits - #4374

Draft
grandixximo wants to merge 15 commits into
LinuxCNC:masterfrom
grandixximo:multiaxis-kins-limits
Draft

Multiaxis kinematics and limits#4374
grandixximo wants to merge 15 commits into
LinuxCNC:masterfrom
grandixximo:multiaxis-kins-limits

Conversation

@grandixximo

@grandixximo grandixximo commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Draft. Where the multiaxis kinematics work is going, open early so it can be argued with. As pieces are agreed I split them into small PRs, rebase this, and carry on, so nothing stalls behind it.

Plan, in order:

  1. Switchkins: one implementation, used by .comp, .c, in tree and out of tree. The registration half merged as switchkins: allow more than three kinematics types #4372; the rest is in this branch.

  2. Small fixes that stand on their own: 5axiskins gets a .tool-offset pin like every other module, maxkins gets one or a doc note saying it has none, and G53 stops ignoring the rotary tool offset on non-wrapped axes.

  3. The kinematics interface learns to answer two more questions: where the tool points, and how joint motion maps to world motion. One entry point each, both weak, both defaulting to today's behaviour, and a tool frame convention written down so every module answers the same way rather than each author choosing one.

  4. Those answers, and forward and inverse alongside them, become pure functions of what the caller passes in: joint values, a parameter block carrying the tool and the kinematics type, and no state kept inside the module. Today the maths reads its geometry from HAL pins and keeps scratch in haldata, so the only way to call it from anywhere else is to write a second copy of it. One implementation, parameters supplied at the call site.

  5. Two stages the offset chain is missing. Both came out of the discussion below, and both want 3 and 4 first.

    A frame composed on top of the active coordinate system, with its own cancel, so the blocks in between are programmed in the tilted plane while G54 itself is untouched. Fanuc spells this G68.2; Siemens puts frames above the kinematic transformation for the same reason. In tree that is a stage in rotate_and_offset_pos() plus the modal state to go with it. Nothing is rewritten, so nothing needs restoring, and on_abort_with_twp_reset.ngc has nothing left to do.

    A move whose target is Cartesian but whose interpolation is in joint space: run the inverse once at the endpoint, then interpolate the joints to reach it. Coordinates and units never change, so the offsets keep applying and there is nothing to switch. That is how the commercial controls cross a singularity without changing coordinate system, and KINEMATICS_INVERSE_FLAGS already carries enough to pin which solution branch the endpoint lands on. It needs a new move type from canon into the trajectory planner, and a way to ask for it in G-code.

    Between them these are the reason not to add a set of offsets per kinematics type. Controls do let you add coordinate systems, Fanuc G54.1 P1 to P48, Siemens G505 to G599, and none of them ties a set to the active kinematics or transformation: selection is explicit in the program, or it belongs to a channel. More coordinate systems is a fair request on its own; binding them to the kinstype is the workaround for these two absences.

  6. Limits taken from the joints instead of from static per-axis INI values. Joint limits are the physical fact. The region they imply in axis space depends on the tool and on where the rotaries are, and is not a box, which is why a box in the INI cannot describe it and config authors have to pick a conservative one and lose travel. [AXIS_L] keeps the job it is good for, a restriction the user imposes in axis space, normally set inside what is reachable; the effective bound is the intersection of the two, and on non-trivial kinematics neither one contains the other, so both are checked. Canon asks per move, which gives TP0 and TP1 the result with no planner change and costs 28 us a move on a closed-form five-axis module. Every commercial five-axis control applies its limits in machine-axis space after the transformation; this is the piece LinuxCNC does not have. Asking also makes the limits per kinematics type, which retires a workaround the docs currently prescribe: switchkins.adoc says the INI limits apply to type 0 only and tells you to setp ini.L.min_limit from M-codes at each switch, with the puma sim shipping M128, M129 and M130 to do it by hand. The realtime seam and TP2 after, and only with an analytic Jacobian: differentiating numerically is eighteen inverse calls, 2 us on a closed-form module and milliseconds on an iterative one.

  7. The joint limit check moves earlier and samples inside segments. It exists already, in inRange(), but only at move endpoints and only once the block has reached motion, so a program that will overtravel says so partway into the cut rather than before it starts. With 4 in place the same check runs at load time, against the tool and the kinematics type each block will actually use. A verify button in a GUI is then a presentation of that, not a subsystem of its own.

    Checking a program that switches type raises a question the tree currently leaves implicit: whether a type's coordinates are geometric. Most are, and for those a scan is one stream of world points with only the inverse changing at the switch. identity is not, since world becomes the joints, so a datum in millimetres lands on a rotary joint in degrees; nothing notices, which is why switchkins.adoc asks the operator to clear the offsets before switching.

    A possible direction rather than part of the plan: a kinematics type could declare its space, cartesian or joint. In practice one type is the joint space and it is the one that wants treating specially, with no offsets applied, limits that are the joint limits directly, and possibly its own syntax later rather than borrowing the axis letters. Everything else stays as it is. I have no mechanism to propose yet and would rather hear objections to the idea first.

  8. Per-mode restrictions. G28, G30, G53, G92, G52 and M6 do not mean the same thing in every kinematics type, and today each one quietly does whatever the module happens to leave it doing. Refusing outright beats guessing, and with 4 in place the control knows enough to decide rather than to be told.

  9. Retire motion.switchkins-type, once the grace period has run. Which release that is has still to be decided.

G12.1 and G13.1 are #4414, split out of this branch and standing on their own.

On tool length specifically: the offset is a nine-axis pose from the interpreter, canon adds it into the coordinate, and each rotary kinematics takes the Z component back through a HAL pin and re-applies it along the real tool direction. That round trip is exact when the config wires the pin, and silently wrong otherwise: a missing net costs the full tool length, and an X or Y offset is applied in the rotating work frame while the tool is bolted to the machine. The mechanism works; it has no owner.

Why 4 is worth the trouble: every other way of answering "will this program overtravel" builds a second model of the machine, in the verification package or in the post, and the two descriptions drift apart. If the kinematics module can be called with parameters the caller chooses, LinuxCNC can check a program against the same code that will run it, with no second model to keep in sync. That is not available to anyone whose kinematics is locked inside the control.

In this branch now

Switchkins becomes one implementation that everything talks to:

  • the dispatch splits from rtapi_app_main(), so a module can take its main from anywhere and call switchkinsInit() itself
  • millturn, xyzab_tdr_kins, xyzacb_trsrn and xyzbca_trsrn drop their private copies of the switch and call it from EXTRA_SETUP(). The generated per-comp .mak takes a <component>-extra-objs list, and switchkins.h is installed
  • switchkinscomp.comp is the out-of-tree template: TOPDIR plus #include, the pattern tpcomp and homecomp already use, so no ABI

User visible

  • The four components gain the coordinates= module parameter, their identity type now coming from kins_util.c.
  • millturn loses its in and out example pins, template scaffolding referenced by no config. fpin and fdemo stay.
  • No pin, G-code or INI name changes.

Testing

The four comp sim configs give identical positions to master through the same MDI sequence, in every kinematics type.

@grandixximo
grandixximo force-pushed the multiaxis-kins-limits branch 2 times, most recently from 9e1c9ed to 773c3e9 Compare August 12, 2026 07:22
Comment thread src/emc/kinematics/switchkins.h Outdated
Comment thread src/emc/motion/motion.h
Comment thread src/emc/nml_intf/emc_nml.hh Outdated
@grandixximo
grandixximo force-pushed the multiaxis-kins-limits branch from 773c3e9 to 34ab07b Compare August 12, 2026 08:34
Comment thread docs/src/man/man9/motion.9.adoc Outdated
Comment thread src/emc/kinematics/switchkins.c Outdated
@BsAtHome

Copy link
Copy Markdown
Contributor

What is the migration path for people having their own out-of-tree kinematics switch component?

@grandixximo

grandixximo commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

What is the migration path for people having their own out-of-tree kinematics switch component?

Their module needs no source change. G12.1 and G13.1 reach a module through the same kinematicsSwitch() the pin does, so a component that is switchable today is driven by G12.1 today without knowing G12.1 exists.

I checked rather than assumed. I built an out-of-tree component that deliberately does not use switchkins.h or switchkins.c: its own switchkins type, its own kinematicsSwitch(), its own kinstype pins, the shape somebody would have written before there was a supported way to reach the in-tree code. Against this branch:

G12.1 P1   kins-type=1, its type 1 kinematics active
G13.1      kins-type=0
G12.1 P7   its own BAD VALUE, motion stays on 0

with no source change, and no deprecation warning, since the pin is never written.

The config keeps working as well. The pin is deprecated, not gone, so an existing M68/M66 config runs as before with one warning per session. Moving off it is a config edit, not code:

M68 E3 Q1 / M66 E0 L0    ->   G12.1 P1
M68 E3 Q0 / M66 E0 L0    ->   G13.1

and the [HAL] net from the analog output goes with them. The sim configs in this PR are worked examples, halui buttons and remapped M-codes included.

One group does have something to do, and it is not the one you asked about: anybody keeping a copy of switchkins.c in their own tree. Their current build is unaffected, but rtapi_app_main() has moved out to switchkins_main.c, so next time they re-sync that copy they either take that file too or call switchkinsInit() themselves. They also no longer need the copy, since switchkins.h is installed now and switchkinscomp.comp shows both routes.

Announcing it is worth raising at a weekly meeting. Nothing is removed in this PR.

Comment thread docs/src/gcode/g-code.adoc Outdated
Comment thread src/emc/kinematics/5axiskins.c Outdated
Comment thread src/hal/components/switchkinscomp.comp
@Sigma1912

Copy link
Copy Markdown
Contributor

Just a thought:
Work offsets in one kinematics are usually not applicable in another one. So when using switchable kinematics I have often found it would be nice to have different sets of G54..G59 offsets.
Maybe a parameter offset set by the 'motion.kins-type' value?

@grandixximo

Copy link
Copy Markdown
Contributor Author

That matches what switchkins.adoc already warns about: coordinate system offsets are described as generally applicable only to the type 0 startup kinematics, with advice to reset them or update them before switching. So the problem is real, and today it rests on the operator remembering.

Can I ask what you switch between, and whether you cut in both types? I am trying to tell two situations apart. If both types leave the axis letters describing the same physical point, then your G54 values are still correct after the switch, and what is missing is having a different one become active. If one of the types is identity, the stored numbers stop describing anything, since X becomes joint 0 and may not even carry the same units, and then a different set does not help either; the values want clearing.

Which of these is it for you: does the zero point of the part sit somewhere different once you switch, or is the part zero the same and you simply want a different one of G54 to G59.3 selected?

On the mechanism, do you mean an association living in the INI, a coordinate system named per kinematics type? There is nothing like that today. Or do you mean it read at runtime? kins-type is a status pin, so it reports rather than commands, but you could net it into a motion analog input and branch on it with M66 in a sub, which would work now. I am unsure whether that belongs in the core or stays the sort of thing an integrator wires up, and your answer would tell me which problem I am actually looking at.

@Sigma1912

Copy link
Copy Markdown
Contributor

Which of these is it for you: does the zero point of the part sit somewhere different once you switch, or is the part zero the same and you simply want a different one of G54 to G59.3 selected?

Here are two examples, the tool may cut in all kinematic modes.

Example1: Serial manipulator arm
https://github.com/LinuxCNC/linuxcnc/tree/master/configs/sim/axis/vismach/melfa-sim
To overcome singularities in industrial robots we need to be able to switch between joint and cartesian mode inside a gcode program. Here the values as well as the units change from one kinematic mode the other.

Example2: Tilted work plane
https://github.com/LinuxCNC/linuxcnc/tree/master/configs/sim/axis/vismach/5axis/table-rotary_spindle-rotary-nutating
The switch to TOOL/TWP mode requires a transformation of the offset values from one coordinate frame to another (rotated AND translated). The new work offset (that describes the same point on the work piece) has thus different values that must be activated after the switch. The original offsets must be restored after switching back.

Currently I have two ways of dealing with this:

  1. Keep the offsets in separate WCS systems (eg use G54 for one and G59 for the other mode). Switching from one to the other is easily handled in the custom M command used to switch the kinematics

  2. Stash the offset values in parameters (so they can be restored when switching back) and then overwrite the old ones.
    This can also be handled in the custom M command but requires much thought to ensure that the correct offset values are restored in all circumstances. Generally I VERY much dislike to alter values in the offset table in the background. There is a lot that can go wrong (eg in case of an abort)

On the mechanism, do you mean an association living in the INI, a coordinate system named per kinematics type? There is nothing like that today. Or do you mean it read at runtime? kins-type is a status pin, so it reports rather than commands, but you could net it into a motion analog input and branch on it with M66 in a sub, which would work now.

Maybe a different set of G54...G59.3 values. One set for each kins-type.
I have also thought about having G54...G58 for kins-type 0, G54.1 ... G58.1 for kins-type 1, G54.2 ... G58.2 for kins-type 2 and so on (for backwards compatibility G59..g59.3 maybe the same for all)

There are also some other things to think about when working in different kinematic modes ( eg G28 , G30, G53, G92/G52, M6) it might be useful to have way of blocking certain Gcodes / Axiswords in a given mode.
I have made a patch that allows blocking certain axis letters at runtime using a magic-comment. (Just as an illustration, not suggesting to use it)
ca9c334

@grandixximo

Copy link
Copy Markdown
Contributor Author

Both examples are clear and they moved my view: I had been assuming identity was mostly a jogging mode, and you cut in all modes. I looked at how the commercial controls handle both cases, and in each of them the closest thing we could do in LinuxCNC turns out not to be an offset set.

For the singularity case, the target stays in Cartesian coordinates and only the interpolation changes: a move that runs the inverse once at the endpoint and then interpolates the joints linearly to reach it. Coordinates and units never change, so the offsets keep applying and there is nothing to switch. In tree that is a new move type from canon into the trajectory planner, and the existing KINEMATICS_INVERSE_FLAGS already carry enough to pin which solution branch it lands on. G-code would need a way to ask for it.

For the tilted plane, an extra stage in the offset chain rather than new values in it: a frame composed on top of the active G54, with its own cancel, so the blocks in between are programmed in the tilted plane while G54 itself is untouched. That is a stage in rotate_and_offset_pos() plus the modal state to go with it. Nothing is rewritten, so nothing needs restoring and on_abort_with_twp_reset.ngc has nothing left to do. Orienting the tool normal to the plane, your G53.1, does need the kinematics to say where the tool points and to solve for the rotaries, which is item 3 above.

On having more of them: that part has plenty of precedent and I would be happy to look at it separately. Fanuc adds G54.1 P1 to P48, or to P300 on the 30i, and Siemens adds G505 to G599. What I cannot find anywhere is a control that ties a set to the active kinematics or transformation. Selection is always explicit in the program, or it belongs to a channel or path. So more coordinate systems yes, but I would rather not bind them to the kinstype.

Which leaves me reading one set per kinstype as paying for the two absences above rather than as the thing you actually want. You have run this in production and I have not, so I would rather hear where that is wrong. Would a composing frame cover your case 2, or would it miss something?

Your other point stands either way. Per-mode restrictions on G28, G30, G53, G92 and M6 look right to me, and refusing outright beats guessing.

@Sigma1912

Copy link
Copy Markdown
Contributor

Nice that you have an overview of what other controllers are doing. Makes more sense adopting something that is established rather than coming up with something quirky that falls on our feet somewhere down the road.
My big deficit (or at least one of them) is that I often have zero experience how things work
on commercial machines so I end up reinventing the wheel and often do it in a slightly odd manner because I don't really know what I'm doing. :)

Which leaves me reading one set per kinstype as paying for the two absences above rather than as the thing you actually want. You have run this in production and I have not, so I would rather hear where that is wrong. Would a composing frame cover your case 2, or would it miss something?

That is how I started out, an offset built into the kinematics. The reason I moved away from that was that in it's current form LinuxCNC is totally unaware of what is going on in a custom kinematics.
So if we can handle things like G53 etc. internally then I'm probably happy with not having different sets of offsets.

Basically what I'd like is to NOT have to tamper directly with the values in the offset table.

@grandixximo

Copy link
Copy Markdown
Contributor Author

That is the same conclusion from the other side, and it names the dependency. A composing frame is only worth having if motion knows the frame is there; otherwise G53 and G28 keep meaning whatever each module happens to do with them, and we are back to the operator remembering. That is items 3 and 4 above, which is why they come before anything in the offset chain.

What "aware" has to mean concretely: the module answers where the tool points and how joint motion maps to world motion, both as plain functions of the joint values and a parameter block, with no state hidden in HAL pins. Once the control can call the kinematics rather than only drive it, G53 resolves in machine axis space, the per-mode restrictions you listed get checked rather than guessed, and a frame composes on top of G54 with nothing rewriting the table.

So no per-kinstype offset sets, nothing tampering with the table in the background, and the tilted plane becomes a frame that cancels. Your two examples are the cases I will hold it to.

G12.1 P- selects one of the kinematics offered by a switchable
kinematics module and G13.1 cancels back to kinematics 0.  Both are
queue synchronisation points, so no motion is ever planned in one
kinematics and executed in another.

Until now the only way to switch from a program was to write
motion.switchkins-type through an analog output and force a sync by
hand, typically M68 E3 Q1 followed by M66 E0 L0, wrapped in a
subroutine or a remapped M-code.  That also costs the #5399 variable
on every switch, because M66 writes it.

G13.1 cancels to kinematics 0 rather than restoring whatever was
selected before, which is how every other cancel in the language
behaves and keeps a block's meaning independent of the path taken
through the program.  To put back a caller's selection, read
#<_kins_type>:

    #<saved> = #<_kins_type>
    G12.1 P2
    ( ... )
    G12.1 P#<saved>

Nothing cancels the selection implicitly.  It survives program end and
abort so that the kinematics keeps matching the position readout, since
switching re-derives world position from the joints and would otherwise
move the readout while the machine stands still.

Motion takes the G-code request and the motion.switchkins-type pin on
their edges, so whichever asked most recently wins and a config can use
either or both.  Writing the pin from motion instead does not work: the
configs source it from an analog output that would put its own value
back on the next servo cycle.  motion.kins-type reports the selection
now in force.

Q was parsed and carried all the way to motion without anything ever
reading it, so it is gone.

EMC_ADJUST_KINS_OFFSET_DATA is registered in the NML format and name
tables and has the update() its declaration promised, without which the
message could not cross the channel.
The interpreter tracked the kinematics it had selected itself, which is
not always the one motion is running.  An abort clears the interpreter
list, so a G12.1 that was queued but not yet sent is dropped while the
interpreter keeps the type it converted.  A config that drives
motion.switchkins-type from HAL changes the kinematics without the
interpreter hearing about it at all.  Either way #<_kins_type> reports
something that is not running, and the save and restore idiom

  #<saved> = #<_kins_type>
  G12.1 P3
  ( ... )
  G12.1 P#<saved>

puts back the wrong kinematics.

Carry the kinematics motion is running up into status and read it back
in Interp::synch(), which already runs after an abort and after every
completed switch.  Task no longer writes the requested value into
status, so the field has a single writer and always reports what motion
is actually running.
handle_kinematicsSwitch() assigned the requested type, published it on
motion.kins-type, stored it in the status, and only then asked the
module to switch.  A module that refuses a type it does not provide
goes on running the one it has, so the readout named a kinematics that
was not in force, and G12.1 P#<_kins_type> put that wrong number back.

Ask first, record after.  A refused switch leaves the type, the pin and
#<_kins_type> on the kinematics still running, and still raises the
motion error.  The refusal reached the operator as nothing at all, only
a line in the realtime log, which was survivable while switching came
from HAL and is not once a G-code block can ask: say which type was
refused and which one is still running.  The failure message names the
type that was asked for rather than the HAL pin, which is not where the
request came from when it came from G-code.

G12.1 P7 on xyzab_tdr_kins, which provides two types, left
motion.kins-type reading 7 while kinstype.is-0 stayed true.  It reads 0.
@Sigma1912

Copy link
Copy Markdown
Contributor

Sounds great!

@grandixximo
grandixximo force-pushed the multiaxis-kins-limits branch from 4a874e3 to ffd07b8 Compare August 19, 2026 09:05
motion.switchkins-type cannot be the general way to choose kinematics.
The interpreter never sees it, so a program is read, its limits
checked and its path looked ahead in whatever kinematics the
interpreter last knew about, which need not be the one that ends up
running it.  Nothing in the pin can fix that; the interpreter has to be
told, which is what G12.1 and G13.1 are for.

Motion says so once per session, the first time the pin is used to
change the type.  A configuration that never switches never sees it,
and the G-code route never triggers it.  The pin is in a grace period:
it keeps working for now, and is meant to go.

Both the man page and the switchkins chapter claimed G12.1 and G13.1
write this pin.  They do not, and cannot: the configs source it from an
analog output that would put its own value back on the next servo
cycle.  They ask motion directly.
The G12.1 plumbing arrived from the out-of-tree patch with names that
describe nothing.  `adjustKinsVar0` is the kinematics type, there is no
Var1, and nothing adjusts an offset.  `kinsType` is not a type at all:
it was a char toggling between 'r' and 's' so the servo cycle could
notice that a new request had arrived.  The field named like a type was
a flag and the field with the opaque name was the type.

So:

  adjustKinsVar0              -> switchkins_type, an int
  kinsType ('r'/'s' toggle)   -> switchkins_seq, a counter
  trajKinsType                -> switchkins_seq in EMC_TRAJ_STAT
  trajKinsTypeModified        -> switchkins_changed in EMC_TRAJ_STAT
  ADJUST_KINS_OFFSET(double)  -> SELECT_KINS_TYPE(int)
  EMC_ADJUST_KINS_OFFSET_DATA -> EMC_TRAJ_SELECT_KINS
  EMCMOT_ADJUST_KINS_OFFSET_DATA -> EMCMOT_SELECT_KINS_TYPE
  emcAdjustKinsOffset()       -> emcSelectKinsType()

switchkins_type rather than kinsType because EMC_TRAJ_STAT already has
kinematics_type, which is the identity/serial/parallel/custom kind and
a different thing entirely.  switchkins_type is what the HAL pin and
switchkins.c already call it.

The three status fields were prefixed traj but lived in EMC_MOTION_STAT.
They are trajectory status, so they move into EMC_TRAJ_STAT and lose the
prefix, which also means EMC_TRAJ_STAT::update() carries them.

A counter instead of a two-state toggle keeps the property the toggle
had, that asking for the type already in force is still seen as a
request, without pretending to be an enum.

No G-code, HAL pin or INI name changes.
The four sim configs whose kinematics components now use the switchkins
core chose their kinematics by writing motion.switchkins-type through an
analog output, the route motion has just deprecated.  Each of them would
have met the user with the deprecation warning the first time they
pressed a kinematics button.

The M428, M429 and M430 remaps, the TWP wrappers behind G53.1, G53.3,
G53.6 and G69, the abort handler and remap.py now use G12.1 and G13.1.
That drops the M66 sync either side of every switch, the test that the
HAL pin exists at all, and the #5399 clobber each M66 costs, since G12.1
and G13.1 synchronise interpreter and motion themselves.  The check that
the switch took reads #<_kins_type> instead of the pin.

millturn keeps the M66 at the end of M428 and M429.  That one is not
there for the switch: M128 and M129 change the axis limits from a Tcl
script, which reaches motion through inihal, so read-ahead has to stop
until the new limits have landed.

The vismach guis for the two trsrn configs were reading the value
requested through the analog output.  They now take motion.kins-type,
which is the kinematics actually in force.

Eight other sim config directories still select kinematics from HAL:
bridgemill, table-rotary-tilting, hexapod-sim, melfa-sim, puma, and the
three copies of scara.  They are untouched here, and still work.
The rest of the sim configs that shipped with switchkins chose their
kinematics by writing motion.switchkins-type through an analog output,
which motion now reports as deprecated: bridgemill, table-rotary-tilting,
hexapod-sim, melfa-sim, puma and the three copies of scara.

Same change as the comp sims got.  The M428, M429 and M430 remaps use
G12.1 and G13.1, which drops the M66 sync either side of every switch,
the test for the hal pin, and the #5399 clobber each M66 costs.  The
check that the switch took reads #<_kins_type>.  The [HAL] net from
motion.analog-out-03 goes with them, and the two halshow watch lists
follow motion.kins-type instead of the pin that used to drive it.

No sim config selects kinematics from HAL now.
The G-code chapter told the reader a config may select the kinematics
"from G-code, from that pin, or from both", and the switchkins chapter
said the same twice, in its introduction and again under G-code
commands.  All three predate motion reporting the pin as deprecated, and
they contradict it.

They now say the pin is deprecated and why, in the same words as the man
page.  The G-code chapter keeps the fact that the pin takes the same
numbering, which is what somebody migrating away from it needs to know.
The paragraph read as though the pin were an equal alternative that
happened to carry a caveat.  State the deprecation first, as a warning.
switchkins.c owned rtapi_app_main(), so a module could only use it by
having no main of its own.  That ruled out halcompile components, which
is why the switchable kinematics in hal/components each carry a private
copy of the dispatch, the kinstype pins and the switch statement.

Move rtapi_app_main(), rtapi_app_exit() and the coordinates= and sparm=
module parameters to switchkins_main.c, and give switchkins.c a single
entry point:

    int switchkinsInit(const int comp_id, kparms* kp, const char* coordinates);

It counts and validates the registered types, creates the pins and
starts on type 0.  The caller owns the hal component, doing hal_init()
before and hal_ready() after, so anything that already has a component
can use switchkins by calling this.

The types switchkinsSetup() supplies now reach the arrays through
switchkinsRegister() like any others, rather than being written
directly through its out parameters.  One registration path means the
checks apply to every type, so a module that both fills an argument and
registers the same type is refused rather than silently overwriting.

The eight existing modules gain switchkins_main.o in their -objs and
are otherwise untouched.
millturn, xyzab_tdr_kins, xyzacb_trsrn and xyzbca_trsrn each carried
their own copy of the switchkins dispatch: a private switchkins_type, a
kinematicsSwitch() with a hand-written case per type, and a setup
routine that had to hal_set_unready() the component again because it
ran from kinematicsType(), long after halcompile had called
hal_ready().  Four copies of the same thing, none of them sharing the
fixes made to switchkins.c.

They could not link switchkins.o before, because switchkins.c supplied
rtapi_app_main() and so does halcompile.  Now that the dispatch is
separate from the 'main' program, a component can link it and call
switchkinsInit() from EXTRA_SETUP(), which halcompile runs after
hal_init() and before hal_ready().

Two build changes make that possible:

 - the generated per-comp .mak takes a <component>-extra-objs list, so
   a .comp can name objects besides its own.
 - switchkins.h is copied to ../include and installed, so
   <switchkins.h> resolves from a generated component source.

Each of the four now registers its kinematics types and calls
switchkinsInit().  Their identity type comes from kins_util.c, which
gets them the coordinates= module parameter they never had, and a bad
motion.switchkins-type is now rejected and leaves the running
kinematics alone instead of stranding the module on a type that does
not exist.

Pin names are unchanged, except that millturn's in/out example pins are
gone: they were template scaffolding copied from userkins.comp, unused
by the sim config, and a kinematics-type setup routine is where
kinematics pins belong now.  millturn keeps its fpin pin and fdemo
function.

The xyzab-tdr, xyzacb-trsrn, xyzbca-trsrn and millturn sim configs give
the same positions through the same MDI sequence as before, to four
decimals, in every kinematics type.
Nothing stopped an out-of-tree kinematics module from using switchkins
except that there was no way to get at the implementation, so anyone
writing one reimplemented kinematicsSwitch() and the kinstype.is-N
pins, or did without switching entirely.

switchkinscomp.comp is the template for doing it properly.  It sets
TOPDIR to a source tree and includes switchkins.c and kins_util.c,
which is how tpcomp.comp and homecomp.comp already reach the
trajectory planning and homing sources.  The module then registers its
kinstypes and calls switchkinsInit() from EXTRA_SETUP(), the same
fifteen lines the in-tree components use.

That gets an out-of-tree module the kinematics switching, the
kinstype.is-N pins, the coordinates= identity mapping and the HAL and
G-code controls, all from the one implementation, and it costs no ABI:
the sources are compiled into the module, so it is built against one
tree and rebuilt when that tree changes.

Like tpcomp, the template is not built in tree because it has no
kinematics until TOPDIR is set, so it is filtered out of COMPS and its
manpage is named explicitly.

Renamed to user_switchkins, pointed at this tree and loaded as
[KINS]KINEMATICS, it homes, switches to its example kinstype and back,
and rejects a kinstype it does not have.
switchkins.h includes kinematics.h, so a module that includes
switchkins.h does not need to include kinematics.h itself.

switchkins.c had picked up the habit along with genhexkins, 5axiskins,
pumakins, scarakins and three21kins, which had it before any of this.
Modules that do not use switchkins.h still include kinematics.h
directly, as they must.
The kinematics modules are users of switchkins, not part of it, so they
take the header the way any other user would.  switchkins.c and
switchkins_main.c keep the quoted form, being the source itself.
A realtime module cannot link a library, so an out-of-tree kinematics
module has to compile the switchkins implementation itself.  Asking it
for the path to a source tree, as the template did, leaves anybody on a
deb install with nothing to point at.

Install switchkins.c and kins_util.c into share/linuxcnc, the way
mesa_modbus.c.tmpl already is, and put that directory on the realtime
include path.  The template then reads

    #include <switchkins.c>
    #include <kins_util.c>

and builds as it stands.
@grandixximo
grandixximo force-pushed the multiaxis-kins-limits branch from ffd07b8 to fea53f7 Compare August 19, 2026 09:51
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