Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 64 additions & 42 deletions src/emc/motion/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,18 @@ 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"),
requested_type, switchkins_type);
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);
Expand Down Expand Up @@ -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;
Comment thread
BsAtHome marked this conversation as resolved.
Resolved

/* copy joint position feedback to local array */
for (joint_num = 0; joint_num < NO_OF_KINS_JOINTS; joint_num++) {
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 13 additions & 8 deletions tests/kins-switch-unsolved/README
Original file line number Diff line number Diff line change
@@ -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.
89 changes: 69 additions & 20 deletions tests/kins-switch-unsolved/test-ui.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -63,33 +72,74 @@ 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)
wait_for("the interpreter to be idle", lambda: s.interp_state == linuxcnc.INTERP_IDLE
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):
Expand All @@ -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:
Expand All @@ -114,20 +164,19 @@ 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(),))
said = drain()
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()
Expand Down
Loading