Skip to content
Merged
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
45 changes: 18 additions & 27 deletions src/emc/kinematics/pumakins.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ static const kins_param_desc puma_params[] = {
};
enum { P_A2, P_A3, P_D3, P_D4, P_D6 };

/* the difference of two angles, brought into (-pi, pi] so that a joint a
whole turn from the formula still matches it */
static double angleDiff(double a, double b)
/* angle, in degrees, moved by whole turns to within a half turn of near */
static double nearestTurn(double angle, double near)
{
double d = a - b;
while (d > PM_PI) { d -= 2*PM_PI; }
while (d <= -PM_PI) { d += 2*PM_PI; }
return d;
return angle - 360.0*round((angle - near)/360.0);
}

/* The flange orientation for a joint set: the ISO 9787 mechanical interface
Expand Down Expand Up @@ -170,16 +166,18 @@ static int puma_forward(const kins_params *p, kins_scratch *s,
*iflags = 0;

/* Set shoulder-up flag if necessary */
if (fabs(angleDiff(joint[0]*PM_PI/180, atan2(hom.tran.y, hom.tran.x) -
atan2(PUMA_D3, -sqrt(sumSq)))) < FLAG_FUZZ)
if (fabs(nearestTurn((atan2(hom.tran.y, hom.tran.x) -
atan2(PUMA_D3, -sqrt(sumSq)))*180.0/PM_PI, joint[0]) - joint[0]) <
FLAG_FUZZ*180.0/PM_PI)
{
*iflags |= PUMA_SHOULDER_RIGHT;
}

/* Set elbow down flag if necessary */
if (fabs(angleDiff(joint[2]*PM_PI/180, atan2(PUMA_A3, PUMA_D4) -
if (fabs(nearestTurn((atan2(PUMA_A3, PUMA_D4) -
atan2(k, -sqrt(PUMA_A3 * PUMA_A3 +
PUMA_D4 * PUMA_D4 - k * k)))) < FLAG_FUZZ)
PUMA_D4 * PUMA_D4 - k * k)))*180.0/PM_PI, joint[2]) - joint[2]) <
FLAG_FUZZ*180.0/PM_PI)
{
*iflags |= PUMA_ELBOW_DOWN;
}
Expand All @@ -195,7 +193,8 @@ static int puma_forward(const kins_params *p, kins_scratch *s,

/* if not singular set wrist flip flag if necessary */
else{
if (! (fabs(angleDiff(joint[3]*PM_PI/180, atan2(t1, t2))) < FLAG_FUZZ))
if (! (fabs(nearestTurn(atan2(t1, t2)*180.0/PM_PI, joint[3]) - joint[3]) <
FLAG_FUZZ*180.0/PM_PI))
{
*iflags |= PUMA_WRIST_FLIP;
}
Expand Down Expand Up @@ -402,21 +401,13 @@ static int puma_inverse(const kins_params *p, kins_scratch *s,
from a difference of two, so a joint crossing +-180 would jump a
whole turn in one call, and th1, th2 and th3 can be a turn off
anywhere. Bring each back within a half turn of the joint passed
in. */
th1 = joint[0]*PM_PI/180 + angleDiff(th1, joint[0]*PM_PI/180);
th2 = joint[1]*PM_PI/180 + angleDiff(th2, joint[1]*PM_PI/180);
th3 = joint[2]*PM_PI/180 + angleDiff(th3, joint[2]*PM_PI/180);
th4 = joint[3]*PM_PI/180 + angleDiff(th4, joint[3]*PM_PI/180);
th5 = joint[4]*PM_PI/180 + angleDiff(th5, joint[4]*PM_PI/180);
th6 = joint[5]*PM_PI/180 + angleDiff(th6, joint[5]*PM_PI/180);

/* copy out */
joint[0] = th1*180/PM_PI;
joint[1] = th2*180/PM_PI;
joint[2] = th3*180/PM_PI;
joint[3] = th4*180/PM_PI;
joint[4] = th5*180/PM_PI;
joint[5] = th6*180/PM_PI;
in, in degrees as it is copied out. */
joint[0] = nearestTurn(th1*180.0/PM_PI, joint[0]);
joint[1] = nearestTurn(th2*180.0/PM_PI, joint[1]);
joint[2] = nearestTurn(th3*180.0/PM_PI, joint[2]);
joint[3] = nearestTurn(th4*180.0/PM_PI, joint[3]);
joint[4] = nearestTurn(th5*180.0/PM_PI, joint[4]);
joint[5] = nearestTurn(th6*180.0/PM_PI, joint[5]);

return 0;
}
Expand Down
9 changes: 9 additions & 0 deletions src/emc/kinematics/scarakins.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ static const kins_param_desc scara_params[] = {
};
enum { P_D1, P_D2, P_D3, P_D4, P_D5, P_D6 };

/* angle, in degrees, moved by whole turns to within a half turn of near */
static double nearestTurn(double angle, double near)
{
return angle - 360.0*round((angle - near)/360.0);
}

/* joint[0], joint[1] and joint[3] are in degrees and joint[2] is in length units */
static int scara_forward(const kins_params *p, kins_scratch *s,
const double * joint,
Expand Down Expand Up @@ -184,6 +190,9 @@ static int scara_inverse(const kins_params *p, kins_scratch *s,
q0 = q0 * (180 / PM_PI);
q1 = q1 * (180 / PM_PI);

/* keep q0 within a half turn of the joint passed in, as pumakins does */
q0 = nearestTurn(q0, joint[0]);

joint[0] = q0;
joint[1] = q1;
joint[2] = D1 + D3 - D5 - z;
Expand Down
37 changes: 18 additions & 19 deletions src/emc/kinematics/three21kins.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,10 @@ static const kins_param_desc three21_params[] = {
};
enum { P_A1, P_A2, P_A3, P_D1, P_D2, P_D3, P_D4, P_D6 };

/* the difference of two angles, brought into (-pi, pi] so that a joint a
whole turn from the formula still matches it */
static double angleDiff(double a, double b)
/* angle, in degrees, moved by whole turns to within a half turn of near */
static double nearestTurn(double angle, double near)
{
double d = a - b;
while (d > PM_PI) { d -= 2*PM_PI; }
while (d <= -PM_PI) { d += 2*PM_PI; }
return d;
return angle - 360.0*round((angle - near)/360.0);
}

static int three21_forward(const kins_params *p, kins_scratch *s,
Expand Down Expand Up @@ -153,8 +149,9 @@ static int three21_forward(const kins_params *p, kins_scratch *s,
*iflags = 0;

/* set shoulder flag */
if (fabs(angleDiff(joint[0]*PM_PI/180, atan2(hom.tran.y, hom.tran.x) -
atan2(d23, -sqrt(sumSq)))) < FLAG_FUZZ)
if (fabs(nearestTurn((atan2(hom.tran.y, hom.tran.x) -
atan2(d23, -sqrt(sumSq)))*180.0/PM_PI, joint[0]) - joint[0]) <
FLAG_FUZZ*180.0/PM_PI)
{
*iflags |= THREE21_SHOULDER_RIGHT;
}
Expand All @@ -164,8 +161,9 @@ static int three21_forward(const kins_params *p, kins_scratch *s,
if (discr < 0.0) {
discr = 0.0;
}
if (fabs(angleDiff(joint[2]*PM_PI/180, atan2(a3, d4) -
atan2(k, -sqrt(discr)))) < FLAG_FUZZ)
if (fabs(nearestTurn((atan2(a3, d4) -
atan2(k, -sqrt(discr)))*180.0/PM_PI, joint[2]) - joint[2]) <
FLAG_FUZZ*180.0/PM_PI)
{
*iflags |= THREE21_ELBOW_DOWN;
}
Expand All @@ -179,7 +177,8 @@ static int three21_forward(const kins_params *p, kins_scratch *s,
}
else
{
if (! (fabs(angleDiff(joint[3]*PM_PI/180, atan2(t1, t2))) < FLAG_FUZZ))
if (! (fabs(nearestTurn(atan2(t1, t2)*180.0/PM_PI, joint[3]) - joint[3]) <
FLAG_FUZZ*180.0/PM_PI))
{
*iflags |= THREE21_WRIST_FLIP;
}
Expand Down Expand Up @@ -370,13 +369,13 @@ static int three21_inverse(const kins_params *p, kins_scratch *s,
th6 = th6 + PM_PI;
}

/* copy out */
joint[0] = th1*180/PM_PI;
joint[1] = th2*180/PM_PI;
joint[2] = th3*180/PM_PI;
joint[3] = th4*180/PM_PI;
joint[4] = th5*180/PM_PI;
joint[5] = th6*180/PM_PI;
/* copy out in degrees, each joint the turn nearest the one passed in */
joint[0] = nearestTurn(th1*180.0/PM_PI, joint[0]);
joint[1] = nearestTurn(th2*180.0/PM_PI, joint[1]);
joint[2] = nearestTurn(th3*180.0/PM_PI, joint[2]);
joint[3] = nearestTurn(th4*180.0/PM_PI, joint[3]);
joint[4] = nearestTurn(th5*180.0/PM_PI, joint[4]);
joint[5] = nearestTurn(th6*180.0/PM_PI, joint[5]);

return 0;
}
Expand Down
1 change: 1 addition & 0 deletions tests/kins-unwrap/pumakins/checkresult
1 change: 1 addition & 0 deletions tests/kins-unwrap/pumakins/skip
1 change: 1 addition & 0 deletions tests/kins-unwrap/pumakins/test.sh
11 changes: 11 additions & 0 deletions tests/kins-unwrap/pumakins/walks
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# The walks pumakins-unwrap took for #4585 and #4708. The wrist pitch,
# joint 5, is left out: at +-180 the wrist is folded back on itself, a
# singularity where joints 4 and 6 are not determined.
# joints 4 and 6 together from 150 and 140
pumakins start=20,-20,100,150,35,140 mask=40
# the waist, joint 1, from 150
pumakins start=150,-20,100,30,35,40 mask=1
# the shoulder, joint 2, from 150
pumakins start=20,150,100,30,35,40 mask=2
# the elbow, joint 3, from 150
pumakins start=20,-20,150,30,35,40 mask=4
1 change: 1 addition & 0 deletions tests/kins-unwrap/scarakins/checkresult
1 change: 1 addition & 0 deletions tests/kins-unwrap/scarakins/skip
1 change: 1 addition & 0 deletions tests/kins-unwrap/scarakins/test.sh
3 changes: 3 additions & 0 deletions tests/kins-unwrap/scarakins/walks
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# joint 0 across 180 with the elbow bent each way
scarakins start=150,30,0,0,0,0 mask=1
scarakins start=150,-30,0,0,0,0 mask=1
7 changes: 7 additions & 0 deletions tests/kins-unwrap/shared-checkresult
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
# every walk in walks ran and held
n=$(grep -cv '^[[:space:]]*\(#\|$\)' "$(dirname "$1")/walks")
[ "$n" -ge 1 ] \
&& [ "$(grep -c '^=== ' "$1")" = "$n" ] \
&& [ "$(grep -c 'joints continuous' "$1")" = "$n" ] \
&& ! grep -q "FAIL" "$1"
File renamed without changes.
20 changes: 20 additions & 0 deletions tests/kins-unwrap/shared-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
# Walk kinematics joints across +-180 the way motion calls them. Each
# line of walks is a module and the walkcheck arguments for one walk.
# One directory per module, so runtests -j runs them side by side.
set -e

[ -r walks ] || { echo "Error: file 'walks' does not exist"; exit 1; }

halcompile --install ../walkcheck.c >/dev/null

# every walk runs, so one failure does not hide the next
status=0
while read -r kins args; do
case "$kins" in ''|'#'*) continue ;; esac
printf 'loadrt %s\nloadrt walkcheck %s\n' "$kins" "$args" > walk.hal
echo "=== $kins $args"
halrun -f walk.hal < /dev/null || status=1
done < walks
Comment thread
BsAtHome marked this conversation as resolved.
Resolved
rm -f walk.hal
exit $status
1 change: 1 addition & 0 deletions tests/kins-unwrap/three21kins/checkresult
1 change: 1 addition & 0 deletions tests/kins-unwrap/three21kins/skip
1 change: 1 addition & 0 deletions tests/kins-unwrap/three21kins/test.sh
5 changes: 5 additions & 0 deletions tests/kins-unwrap/three21kins/walks
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# pumakins' walks: three21kins carries its solver
three21kins start=20,-20,100,150,35,140 mask=40
three21kins start=150,-20,100,30,35,40 mask=1
three21kins start=20,150,100,30,35,40 mask=2
three21kins start=20,-20,150,30,35,40 mask=4
124 changes: 124 additions & 0 deletions tests/kins-unwrap/walkcheck.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* Check that a kinematics module keeps its joints continuous (#4585).
*
* Loaded after the module under test, so kinematicsForward() and
* kinematicsInverse() resolve to it. A failed check fails the load, and
* a failed load fails the test.
*
* An inverse that takes its joints from atan2(), or from a difference of
* two, lands in (-180, 180] or (-360, 360], so a joint crossing a half
* turn comes back a whole turn away in one call. Walk the joints in mask
* 60 degrees up from start, across +-180, and back down, as motion does:
* take the pose of each step from the forward and hand the inverse the
* joints of the step before. The inverse has to return the joints that
* made the pose, not ones a turn away.
*
* Author: LinuxCNC
* License: GPL Version 2
* System: Linux
*
* Copyright (c) 2026 All rights reserved.
*/

#include <rtapi.h>
#include <rtapi_app.h>
#include <rtapi_errno.h>
#include <rtapi_math.h>
#include <rtapi_string.h>
#include <hal.h>
#include <emcpos.h>
#include <kinematics.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("kinematics joint unwrap checker");

static int start[6];
RTAPI_MP_ARRAY_INT(start, 6, "joints to start the walk from");

static int mask = 1;
RTAPI_MP_INT(mask, "joints to walk, bit 0 for joint 0");

/* degrees the inverse may drift from the joints that made the pose; the
round trip is exact, this only covers floating point noise */
#define TOL 1e-6
#define STEP 2.5

static int comp_id = -1;
static int failures;
static int poses;

static void step(double *prev, const double *j)
{
EmcPose world;
double out[6];
KINEMATICS_FORWARD_FLAGS ff = 0;
KINEMATICS_INVERSE_FLAGS inf = 0;
double worst = 0.0;
int i;

poses++;
if (kinematicsForward(j, &world, &ff, &inf)) {
rtapi_print_msg(RTAPI_MSG_ERR, "walkcheck: FAIL forward at %g %g %g %g %g %g\n",
j[0], j[1], j[2], j[3], j[4], j[5]);
failures++;
return;
}
memcpy(out, prev, sizeof(out));
if (kinematicsInverse(&world, out, &inf, &ff)) {
rtapi_print_msg(RTAPI_MSG_ERR, "walkcheck: FAIL inverse at %g %g %g %g %g %g\n",
j[0], j[1], j[2], j[3], j[4], j[5]);
failures++;
return;
}
for (i = 0; i < 6; i++) {
if (fabs(out[i] - j[i]) > worst) { worst = fabs(out[i] - j[i]); }
}
if (worst > TOL) {
rtapi_print_msg(RTAPI_MSG_ERR,
"walkcheck: FAIL at %g %g %g %g %g %g: got %g %g %g %g %g %g,"
" worst joint error %g\n",
j[0], j[1], j[2], j[3], j[4], j[5],
out[0], out[1], out[2], out[3], out[4], out[5], worst);
failures++;
}
memcpy(prev, out, sizeof(out));
}

static void walk_to(double *j, double *prev, int n)
{
int i;

for (i = 0; i < 6; i++) {
if (mask & (1 << i)) { j[i] = start[i] + n * STEP; }
}
step(prev, j);
}

int rtapi_app_main(void)
{
double j[6], prev[6];
int i, n;

if (mask <= 0 || mask >= (1 << 6)) {
rtapi_print_msg(RTAPI_MSG_ERR, "walkcheck: mask %d must pick from joints 0 to 5\n", mask);
return -EINVAL;
}

comp_id = hal_init("walkcheck");
if (comp_id < 0) { return comp_id; }

for (i = 0; i < 6; i++) { j[i] = prev[i] = start[i]; }
for (n = 0; n <= 24; n++) { walk_to(j, prev, n); }
for (n = 24; n >= 0; n--) { walk_to(j, prev, n); }

if (failures) {
rtapi_print_msg(RTAPI_MSG_ERR, "walkcheck: %d of %d pose(s) failed\n",
failures, poses);
hal_exit(comp_id);
return -ERANGE;
}
rtapi_print("walkcheck: joints continuous over %d pose(s)\n", poses);
hal_ready(comp_id);
return 0;
}

void rtapi_app_exit(void) { hal_exit(comp_id); }
2 changes: 0 additions & 2 deletions tests/pumakins-unwrap/checkresult

This file was deleted.

5 changes: 0 additions & 5 deletions tests/pumakins-unwrap/test.sh

This file was deleted.

Loading