From 8f956e1d20c1f4b9604b309bce604604dfdd33e4 Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Sun, 1 Nov 2026 14:26:42 +1100 Subject: [PATCH] motion: keep the last pose as the forward's guess after a failed forward With KINEMATICS_BOTH, motion seeds the forward of the feedback, and in joint mode of the command, with the pose of the cycle before; after one failed forward it seeded from world_home instead, every cycle until the forward solved again. For an iterative forward far from home that seed can fail again or converge to another pose with the same joints, and since that pose is a solution, the following cycles stay on it: the position reads wrong until the machine comes back near home. Home is now the guess only until the forward has solved once since all joints were homed; a failed forward puts back the pose it was given, which is the next guess. A kinematics switch now also hands its solved pose to the feedback, whose last pose is in the old kinematics and would otherwise stay the guess. In the hexapod-sim geometry, over 23141 platform poses whose straight path from home the forward follows cycle by cycle, the forward started at home fails at 675 and lands on another pose at 319, all near the edges (X and Y at 15, tilts to 45 degrees, turns to 90); in the middle of the workspace it finds them all. tests/kins-switch-unsolved now starts in the hexapod, moves the platform to one of those, makes the forward fail for a few cycles with genhexkins.limit-iterations 0, and requires the feedback and the joint mode command to come back to the platform; without the change both stay on X-14.69 Y-14.81 Z31.67 A28.6 B18.2 C97.1. --- src/emc/motion/control.c | 106 ++++++++++++++++---------- tests/kins-switch-unsolved/README | 21 +++-- tests/kins-switch-unsolved/test-ui.py | 89 ++++++++++++++++----- tests/kins-switch-unsolved/test.ini | 60 +++++++++------ 4 files changed, 183 insertions(+), 93 deletions(-) diff --git a/src/emc/motion/control.c b/src/emc/motion/control.c index acb56571155..10284142a4f 100644 --- a/src/emc/motion/control.c +++ b/src/emc/motion/control.c @@ -383,7 +383,8 @@ static void handle_kinematicsSwitch(void) { solve them is one the machine cannot run in from here: put the old one back, or the inverse would run the joints to wherever the pose we know lands in the new one */ - if (reanchor_pose(joint_posKinsSwitch, NULL) != 0) { + EmcPose poseKinsSwitch; + if (reanchor_pose(joint_posKinsSwitch, &poseKinsSwitch) != 0) { kinematicsSwitch(switchkins_type); reportError(_("kinematicsForward failed for kinematics type %d," " type %d is still in force"), @@ -391,6 +392,9 @@ static void handle_kinematicsSwitch(void) { SET_MOTION_ERROR_FLAG(1); // abort return; // the kinematics in force and the position are unchanged } + /* the feedback's last pose is in the old kinematics: start the + feedback's forward from the new pose, joints are within ferror */ + emcmotStatus->carte_pos_fb = poseKinsSwitch; switchkins_type = requested_type; hal_set_real(emcmot_hal_data->kins_type, (double)switchkins_type); @@ -709,6 +713,9 @@ static void do_forward_kins(void) double joint_pos[EMCMOT_MAX_JOINTS] = {0,}; int joint_num, result; emcmot_joint_t *joint; + /* the forward has solved since all joints were last homed */ + static bool fb_solved = 0; + EmcPose seed; /* copy joint position feedback to local array */ for (joint_num = 0; joint_num < NO_OF_KINS_JOINTS; joint_num++) { @@ -730,27 +737,34 @@ static void do_forward_kins(void) break; case KINEMATICS_BOTH: - if (get_allhomed()) { - /* is previous value suitable for use as initial guess? */ - if (!emcmotStatus->carte_pos_fb_ok) { - /* no, use home position as initial guess */ - emcmotStatus->carte_pos_fb = emcmotStatus->world_home; - } - /* calculate Cartesean position feedback from joint pos fb */ - result = - kinematicsForward(joint_pos, &emcmotStatus->carte_pos_fb, - &fflags, &iflags); - /* check to make sure kinematics converged */ - if (result < 0) { - /* error during kinematics calculations */ - emcmotStatus->carte_pos_fb_ok = 0; - } else { - /* it worked! */ - emcmotStatus->carte_pos_fb_ok = 1; - } - } else { - emcmotStatus->carte_pos_fb_ok = 0; - } + if (get_allhomed()) { + /* home is the initial guess only until the forward has solved + once: after a failure, home can be far from the joints, and + an iterative forward started there fails again or converges + to another pose with the same joints, and then stays on it */ + if (!emcmotStatus->carte_pos_fb_ok && !fb_solved) { + emcmotStatus->carte_pos_fb = emcmotStatus->world_home; + } + seed = emcmotStatus->carte_pos_fb; + /* calculate Cartesean position feedback from joint pos fb */ + result = + kinematicsForward(joint_pos, &emcmotStatus->carte_pos_fb, + &fflags, &iflags); + /* check to make sure kinematics converged */ + if (result < 0) { + /* error during kinematics calculations; keep the last + pose as the next guess */ + emcmotStatus->carte_pos_fb = seed; + emcmotStatus->carte_pos_fb_ok = 0; + } else { + /* it worked! */ + emcmotStatus->carte_pos_fb_ok = 1; + fb_solved = 1; + } + } else { + emcmotStatus->carte_pos_fb_ok = 0; + fb_solved = 0; + } break; case KINEMATICS_INVERSE_ONLY: @@ -1505,6 +1519,9 @@ static void get_pos_cmds(long period) emcmot_joint_t *joint; double positions[EMCMOT_MAX_JOINTS]; double vel_lim; + /* the forward has solved since all joints were last homed */ + static bool cmd_solved = 0; + EmcPose seed; /* used in teleop mode to compute the max accell requested */ int onlimit = 0; @@ -1623,26 +1640,31 @@ static void get_pos_cmds(long period) break; case KINEMATICS_BOTH: - if (get_allhomed()) { - /* is previous value suitable for use as initial guess? */ - if (!emcmotStatus->carte_pos_cmd_ok) { - /* no, use home position as initial guess */ - emcmotStatus->carte_pos_cmd = emcmotStatus->world_home; - } - /* calculate Cartesean position command from joint coarse pos cmd */ - result = - kinematicsForward(positions, &emcmotStatus->carte_pos_cmd, &fflags, &iflags); - /* check to make sure kinematics converged */ - if (result < 0) { - /* error during kinematics calculations */ - emcmotStatus->carte_pos_cmd_ok = 0; - } else { - /* it worked! */ - emcmotStatus->carte_pos_cmd_ok = 1; - } - } else { - emcmotStatus->carte_pos_cmd_ok = 0; - } + if (get_allhomed()) { + /* home is the initial guess only until the forward has + solved once, as for the feedback */ + if (!emcmotStatus->carte_pos_cmd_ok && !cmd_solved) { + emcmotStatus->carte_pos_cmd = emcmotStatus->world_home; + } + seed = emcmotStatus->carte_pos_cmd; + /* calculate Cartesean position command from joint coarse pos cmd */ + result = + kinematicsForward(positions, &emcmotStatus->carte_pos_cmd, &fflags, &iflags); + /* check to make sure kinematics converged */ + if (result < 0) { + /* error during kinematics calculations; keep the last + pose as the next guess */ + emcmotStatus->carte_pos_cmd = seed; + emcmotStatus->carte_pos_cmd_ok = 0; + } else { + /* it worked! */ + emcmotStatus->carte_pos_cmd_ok = 1; + cmd_solved = 1; + } + } else { + emcmotStatus->carte_pos_cmd_ok = 0; + cmd_solved = 0; + } break; case KINEMATICS_INVERSE_ONLY: diff --git a/tests/kins-switch-unsolved/README b/tests/kins-switch-unsolved/README index c3f263e4f7f..2a28f5b4ed7 100644 --- a/tests/kins-switch-unsolved/README +++ b/tests/kins-switch-unsolved/README @@ -1,9 +1,14 @@ -A kinematics switch whose forward kinematics cannot solve the current -joint position must leave the type in force, the joints and the position -alone, report the failure and abort; motion goes on working in the -kinematics it kept. +The hexapod forward after a failure: the platform is moved far from home, +and genhexkins.limit-iterations is set to 0 for a few cycles so the +forward fails; then the feedback position, and in joint mode the +commanded position, must be where the platform is. An iterative forward +started at home far from the joints can converge to another platform pose +with the same strut lengths, and stays on it from then on. -The hexapod module starts in identity kinematics here, so the joints are -put at a few units each as coordinates. Six struts that short are not a -platform position the hexapod forward can converge to, so G12.1 P1 asks -for exactly that switch. +Then a kinematics switch whose forward kinematics cannot solve the +current joint position must leave the type in force, the joints and the +position alone, report the failure and abort; motion goes on working in +the kinematics it kept. The machine switches to identity and the joints +are put at a few units each as coordinates. Six struts that short are +not a platform position the hexapod forward can converge to, so G12.1 P0 +asks for exactly that switch. diff --git a/tests/kins-switch-unsolved/test-ui.py b/tests/kins-switch-unsolved/test-ui.py index 84130418653..1ac95a80115 100755 --- a/tests/kins-switch-unsolved/test-ui.py +++ b/tests/kins-switch-unsolved/test-ui.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 -# A kinematics switch the new forward cannot solve. Motion must keep the -# position it knows, say so, abort, and take a switch back afterwards. +# The hexapod forward after a failure, and a kinematics switch the new +# forward cannot solve. Motion must find the platform where it is, not on +# another pose with the same struts; and on the switch keep the position +# it knows, say so, abort, and take a switch back afterwards. import hal import linuxcnc import sys @@ -24,6 +26,13 @@ def position(): s.poll() return tuple(round(v, 6) for v in s.position[:6]) +def joints(): + s.poll() + return tuple(round(v, 6) for v in s.joint_position[:6]) + +def off(pose, want): + return max(abs(a - b) for a, b in zip(pose[:6], want)) + def wait_for(what, done, timeout=30): deadline = time.time() + timeout while time.time() < deadline: @@ -63,6 +72,11 @@ def seen(): fresh() return said + drain() +def mdi_mode(): + c.mode(linuxcnc.MODE_MDI) + c.wait_complete(30) + wait_for("MDI mode", lambda: s.task_mode == linuxcnc.MODE_MDI) + def mdi(cmd): c.mdi(cmd) c.wait_complete(30) @@ -70,26 +84,62 @@ def mdi(cmd): and not s.queue and s.inpos) fresh() +def glitch(): + # no iteration allowed: the forward fails, then solves again from + # whatever guess motion gives it + hal.set_p("genhexkins.limit-iterations", "0") + wait_for("the forward to fail", lambda: hal.get_value("genhexkins.fwd-kins-fail")) + hal.set_p("genhexkins.limit-iterations", "120") + wait_for("the forward to solve again", lambda: not hal.get_value("genhexkins.fwd-kins-fail")) + fresh() + c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) c.wait_complete(30) wait_for("machine on", lambda: s.task_state == linuxcnc.STATE_ON) c.home(-1) wait_for("all joints homed", lambda: all(s.homed[:6])) -c.mode(linuxcnc.MODE_MDI) -c.wait_complete(30) -wait_for("MDI mode", lambda: s.task_mode == linuxcnc.MODE_MDI) +mdi_mode() fresh() drain() if kins_type() != 0: - error("starts in kinematics type %d, expected identity (0)" % kins_type()) - -def joints(): - s.poll() - return tuple(round(v, 6) for v in s.joint_position[:6]) + error("starts in kinematics type %d, expected the hexapod (0)" % kins_type()) + +# One failed forward far from home (0 0 20 0 0 0), yet on the way the +# forward follows the platform cycle by cycle; started from home, it +# converges to a platform pose with the same six struts near +# X-14.69 Y-14.81 Z31.67 A28.6 B18.2 C97.1 +target = (-15, -15, 32, 30, 15, 90) +mdi("G0 X-15 Y-15 Z32 A30 B15 C90") +if off(s.actual_position, target) > 1e-6: + error("feedback position %s before the failure, expected %s" % (s.actual_position[:6], target)) +glitch() +if off(s.actual_position, target) > 1e-6: + error("feedback position %s after a failed forward, expected %s" % (s.actual_position[:6], target)) + +# in joint mode motion also runs the forward on the commanded joints +c.mode(linuxcnc.MODE_MANUAL) +c.wait_complete(30) +c.teleop_enable(0) +c.wait_complete(30) +wait_for("joint mode", lambda: s.motion_mode == linuxcnc.TRAJ_MODE_FREE) +glitch() +if off(s.position, target) > 1e-6: + error("commanded position %s in joint mode after a failed forward, expected %s" + % (s.position[:6], target)) + +# to identity, where joints and coordinates are the same thing +mdi_mode() +mdi("G12.1 P1") +if kins_type() != 1: + error("kinematics type %d after G12.1 P1, expected identity (1)" % kins_type()) +if position() != joints(): + error("position %s after the switch to identity, expected the joints %s" + % (position(), joints())) +drain() -# a known position, in identity: joints and coordinates are the same thing +# a known position mdi("G0 X1 Y2 Z3 A4 B5 C6") before = position() if before != (1, 2, 3, 4, 5, 6): @@ -98,12 +148,12 @@ def joints(): # the switch the hexapod forward cannot solve: six struts of a few units # are no platform position. Nothing may change but the report. -mdi("G12.1 P1") +mdi("G12.1 P0") said = wait_said("kinematicsForward failed") if not any("kinematicsForward failed" in m for m in said): error("no report of the failed forward kinematics, got %s" % said) -if kins_type() != 0: - error("kinematics type %d after the failed switch, expected 0 still" % kins_type()) +if kins_type() != 1: + error("kinematics type %d after the failed switch, expected 1 still" % kins_type()) if position() != before: error("position after the failed switch is %s, expected %s unchanged" % (position(), before)) if joints() != before: @@ -114,9 +164,7 @@ def joints(): # motion goes on working in the kinematics it kept, and the module is # back in it too: the joints follow the identity inverse, not the hexapod's -c.mode(linuxcnc.MODE_MDI) -c.wait_complete(30) -wait_for("MDI mode", lambda: s.task_mode == linuxcnc.MODE_MDI) +mdi_mode() mdi("G0 X30 Y30 Z30 A30 B30 C30") if joints() != (30, 30, 30, 30, 30, 30): error("joints after the move are %s, expected all 30" % (joints(),)) @@ -124,10 +172,11 @@ def joints(): if said: error("unexpected messages after the failed switch: %s" % said) -# a switch to the kinematics in force is nothing to do +# G13.1 cancels to the identity the module declares, already in force: +# nothing to do mdi("G13.1") -if kins_type() != 0: - error("kinematics type %d after G13.1, expected identity (0)" % kins_type()) +if kins_type() != 1: + error("kinematics type %d after G13.1, expected identity (1)" % kins_type()) if position() != (30, 30, 30, 30, 30, 30): error("position after G13.1 is %s, expected all 30" % (position(),)) said = drain() diff --git a/tests/kins-switch-unsolved/test.ini b/tests/kins-switch-unsolved/test.ini index c93bc09563f..5f561ec99f6 100644 --- a/tests/kins-switch-unsolved/test.ini +++ b/tests/kins-switch-unsolved/test.ini @@ -10,8 +10,9 @@ RS274NGC_STARTUP_CODE = G17 G21 G40 G49 G54 G64 P0.001 G80 G90 G92.1 G94 G97 G98 PARAMETER_FILE = sim.var [KINS] -# switchkins-type 0 is identity, 1 is the hexapod, 2 is the userk template -KINEMATICS = genhexkins sparm=identityfirst +# the hexapod-sim geometry, the module's defaults; switchkins-type 0 is +# the hexapod, 1 is identity, 2 is the userk template +KINEMATICS = genhexkins JOINTS = 6 [HAL] @@ -19,6 +20,7 @@ HALFILE = sim.hal [TRAJ] COORDINATES = XYZABC +HOME = 0 0 20 0 0 0 LINEAR_UNITS = mm ANGULAR_UNITS = deg DEFAULT_LINEAR_VELOCITY = 200 @@ -77,54 +79,66 @@ MAX_ACCELERATION = 200 [JOINT_0] TYPE = LINEAR -MIN_LIMIT = -500 -MAX_LIMIT = 500 +MIN_LIMIT = 0 +MAX_LIMIT = 100 MAX_VELOCITY = 200 MAX_ACCELERATION = 800 +HOME_OFFSET = 29.7466 +HOME = 29.7466 HOME_SEARCH_VEL = 0 HOME_SEQUENCE = 0 [JOINT_1] TYPE = LINEAR -MIN_LIMIT = -500 -MAX_LIMIT = 500 +MIN_LIMIT = 0 +MAX_LIMIT = 100 MAX_VELOCITY = 200 MAX_ACCELERATION = 800 +HOME_OFFSET = 29.7466 +HOME = 29.7466 HOME_SEARCH_VEL = 0 HOME_SEQUENCE = 0 [JOINT_2] TYPE = LINEAR -MIN_LIMIT = -500 -MAX_LIMIT = 500 +MIN_LIMIT = 0 +MAX_LIMIT = 100 MAX_VELOCITY = 200 MAX_ACCELERATION = 800 +HOME_OFFSET = 29.7466 +HOME = 29.7466 HOME_SEARCH_VEL = 0 HOME_SEQUENCE = 0 [JOINT_3] -TYPE = ANGULAR -MIN_LIMIT = -500 -MAX_LIMIT = 500 -MAX_VELOCITY = 60 -MAX_ACCELERATION = 200 +TYPE = LINEAR +MIN_LIMIT = 0 +MAX_LIMIT = 100 +MAX_VELOCITY = 200 +MAX_ACCELERATION = 800 +HOME_OFFSET = 29.7466 +HOME = 29.7466 HOME_SEARCH_VEL = 0 HOME_SEQUENCE = 0 [JOINT_4] -TYPE = ANGULAR -MIN_LIMIT = -500 -MAX_LIMIT = 500 -MAX_VELOCITY = 60 -MAX_ACCELERATION = 200 +TYPE = LINEAR +MIN_LIMIT = 0 +MAX_LIMIT = 100 +MAX_VELOCITY = 200 +MAX_ACCELERATION = 800 +HOME_OFFSET = 29.7466 +HOME = 29.7466 HOME_SEARCH_VEL = 0 HOME_SEQUENCE = 0 [JOINT_5] -TYPE = ANGULAR -MIN_LIMIT = -500 -MAX_LIMIT = 500 -MAX_VELOCITY = 60 -MAX_ACCELERATION = 200 +TYPE = LINEAR +MIN_LIMIT = 0 +MAX_LIMIT = 100 +MAX_VELOCITY = 200 +MAX_ACCELERATION = 800 +HOME_OFFSET = 29.7466 +HOME = 29.7466 HOME_SEARCH_VEL = 0 HOME_SEQUENCE = 0