Skip to content

kinematics: six fixes found by round-tripping every module - #4546

Merged
BsAtHome merged 6 commits into
LinuxCNC:masterfrom
grandixximo:kins-fixes
Sep 17, 2026
Merged

BsAtHome merged 6 commits into
LinuxCNC:masterfrom
grandixximo:kins-fixes

Conversation

@grandixximo

Copy link
Copy Markdown
Contributor

Six independent fixes, each found by driving a pose through a module's inverse and back through its forward and expecting the same pose back. No interface changes.

  • xyzbc-trt-kins: the inverse turned x-offset, z-offset and tool-offset through the B tilt in the conventional sense whatever conventional-directions says; with the pin false, the default, a pose came back out by twice sin(b) times the offset.
  • scarakins: the elbow flag was set for joint 1 below 90 degrees instead of below zero, so for an elbow between 0 and 90 the inverse returned the other arm.
  • pumakins, three21kins: the branch flags compared a joint angle with the inverse's value without wrapping the difference, so a joint one turn away, which atan2 differences produce freely, was sent down the other branch.
  • userkins, millturn, matrixkins, xyzab_tdr_kins: pins were created on the first kinematicsType() call behind a flag nothing set, so the second call, which motion makes with num_extrajoints, failed on pins already taken. The setup moves to EXTRA_SETUP(), as the trsrn modules do.
  • maxkins: the forward subtracted the U correction in Z where the inverse also subtracted it, and applied the B, U and V corrections after the C rotation where the inverse applies them before. The vismach model settles which is right: the head hangs off the Z slide and does not turn with the table. Up to 40 mm out with the head tilted and the table turned; nothing changes on the max5 sim, which ships B_PIVOT_LENGTH = 0. tests/maxkins round-trips the poses.
  • motion: a kinematics switch whose forward could not solve the joints kept the position but left the module in the new type, so from the next cycle the inverse ran the joints to wherever that position lands in the new kinematics: twenty units per strut on the hexapod, with nothing commanded. The switch is undone and reported, and nothing moves. tests/kins-switch-unsolved covers it.

Part of the plan in #4374.

Comment thread tests/maxkins/test-ui.py Outdated
Comment thread tests/kins-switch-unsolved/test-ui.py
The inverse turns the x and z offsets through the B tilt in the
conventional sense, while the forward turns them in whichever sense the
conventional-directions pin selects.  With the pin false, which is the
default, and any of x-offset, z-offset or tool-offset set, a pose does
not survive the round trip: the position comes back out by twice sin(b)
times the offset.  xyzac has no term of this kind.

Found by tests/kins-jacobian, which multiplies the derivative of the
inverse by differences of the forward and expects the identity.
The forward sets the flag that makes the inverse negate its arc cosine
when joint 1 is below 90 degrees, so for an elbow between 0 and 90 the
inverse returns the other arm and the pose does not survive the round
trip.  The sign of the arc cosine is the sign of the elbow angle, so the
test is against zero.
The forward decides the shoulder, elbow and wrist branches by comparing a
joint angle with the value the inverse's formula gives, within a fuzz,
and does not wrap the difference.  A joint standing a whole turn from
that value, which the differences of two atan2 results produce freely,
fails the comparison and the inverse is sent down the other branch.  The
difference is brought into (-pi, pi] first.
…ns up once, at load

These four build their pins on the first kinematicsType() call behind an is_setup flag nothing sets, so every later call runs the setup again, reassigns haldata and fails to create pins already taken; motion asks twice when num_extrajoints is set.

Move the setup to EXTRA_SETUP(), which halcompile runs once before the component is ready, as the two trsrn modules already do. The pins exist from load, kinematicsType() only answers, and the hal_set_unready()/hal_ready() dance goes with it. userkins is the template for out of tree modules, so its description says where the pins go.
Two faults left kinematicsForward and kinematicsInverse disagreeing whenever the head is tilted, and nothing in tree could show it: max5 is the only config that loads the module and it ships B_PIVOT_LENGTH = 0 with six joints, so the pivot to tip distance is zero and U, V and W never leave zero.

The forward took the U axis apart into xv and zv and subtracted both from the position it reports, while the inverse added xv back and subtracted zv again: X inverted and Z did not, in the same function, the shape of a typo rather than of a convention. The inverse now adds zv back.

The forward also rotated the table and saddle into the workpiece frame and then subtracted the B, U and V corrections from the result, so those landed in the rotated frame; the inverse un-rotates first and adds them back in the machine frame. The vismach model says which is right: max5gui builds the head as tool, spindle, head, brotary, zslide, column, and the workpiece separately as crotary, table, saddle, so the head hangs off the Z slide and does not turn with the C table. The corrections are machine frame, they apply before the rotation, and the inverse had it right. The forward now subtracts them from the joint values and rotates the sum.

A sweep of 40000 random poses, pivot lengths to 300 and tool lengths to 37.5, round trips to 2.7e-13, against 666 mm before. Reported world coordinates move for anyone running maxkins with the head tilted and the table turned; they were wrong by that amount, and the DRO and the machine now agree. Nothing moves at C0, and nothing moves at all on the max5 sim.

tests/maxkins drives the machine through poses with the pivot length, the tool length and each of B, C, U, V and W away from zero and compares the position that comes back with the one commanded: motion runs the inverse to turn the pose into joints and the forward to turn the joint feedback into a position, so a pose that survives the round trip is one the two directions agree on. Both settings of conventional-directions are covered. Against the old code the pose with B 40 and U 15 came back 19.284 mm out in Z, twice u sin(b), and the pose with B 40 and C 30 landed 10.765 mm out in X and 40.174 mm out in Y.
…refused

handle_kinematicsSwitch() switched the module, then ran the new forward kinematics on the joints and, when that failed, reported it and kept the position it knew. The module stayed in the new type, so from the next cycle the inverse ran the joints to wherever that kept position lands in the new kinematics: on the hexapod that is a jump of twenty units on every strut, with no move commanded.

The joints are where they are, and a kinematics whose forward cannot solve them is not one the machine can run in from there. Switch the module back before anything else sees the new type, report which type is still in force, and abort as before. Nothing moves and a later switch is taken on its own merits.

tests/kins-switch-unsolved loads the hexapod with identity first, homes at zero, asks for the hexapod and checks that the type, the joints and the position are all as they were, that the failure was reported, that a move afterwards follows the identity inverse and that a switch to the type in force is accepted.
@BsAtHome
BsAtHome merged commit c30de0c into LinuxCNC:master Sep 17, 2026
17 checks passed
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.

2 participants