Skip to content

Commit 1e5da0d

Browse files
committed
tests: check the shipped frame functions where they run
The frame cases so far run machines written for the test. The frame functions a real machine uses were compiled and nothing more, which leaves the hand-written matrix entries per module unexercised, and those are where a sign or a transposed pair hides. So run them in service: a realtime component loaded after the module under test, reaching it through the same exported entry points motion uses. A failed check fails the load, and a failed load fails the test. The work frame has a tie to the forward kinematics and is checked against it rather than against a matrix copied out of the module: a row of the work frame is how the reported position responds to one machine axis, measured by central difference. The tool frame has no such tie on a machine that carries the work. Its forward reports the rotary joint values, which say how the work is turned and nothing about where the tool points, and the tool length offset moves the reported origin as well as the tip, so it does not isolate the tool axis either. What is checked is that the frame is a rotation, that a spindle the module calls fixed never moves, and that a joint carrying the whole head turns the reported frame about the machine's z and does nothing else. The last one is what catches a frame built for the wrong joint or composed in the wrong order. Every kinematics type a module offers, since the frames are per type, and the geometry checks are asked only of the type where the module models its own machine, which is not always the first. A type that reports no frames is skipped, which the interface allows, so the test also fails a module that reports frames for no type at all. The hal file sets the geometry, which matters more than it sounds: a nutating head defaults to a nutation angle of zero, which leaves the spindle square with the machine and the interesting geometry untested. The helpers in kins_util.c are not exported to a loadable module, and working the answers out in the test is better anyway: nothing the module under test uses is reused to judge it. Checked by mutation, in failed checks: reversing the head composition order in a trsrn module 300, a flipped sign in its work frame 150, one in the xyzac work frame 18, declaring the wrong spindle convention there 25, a flipped sign in the puma flange 72. The tree as it stands fails none.
1 parent 002fedd commit 1e5da0d

5 files changed

Lines changed: 354 additions & 0 deletions

File tree

‎tests/kins-frames/checkresult‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
[ "$(grep -c 'frames agree' "$1")" = 5 ] \
3+
&& ! grep -q "FAIL" "$1"

‎tests/kins-frames/control‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Restrictions: sudo

‎tests/kins-frames/framecheck.c‎

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
/* Check a kinematics module's reported frames where they run in service.
2+
*
3+
* Loaded after the module under test, so kinematicsForward(),
4+
* kinematicsWorkFrame() and kinematicsToolFrame() resolve to it. A
5+
* failed check fails the load, and a failed load fails the test.
6+
*
7+
* The work frame has a tie to the forward kinematics and is checked
8+
* against it: a row of it is how the reported position responds to one
9+
* machine axis, measured here by central difference.
10+
*
11+
* The tool frame has no such tie on a machine that carries the work.
12+
* Its forward reports the rotary joint values, which describe how the
13+
* work is turned, and say nothing about where the tool points. So the
14+
* tool frame is checked for what can be checked: that it is a rotation,
15+
* that a spindle the module calls fixed never moves, and that a joint
16+
* which turns the whole head about the machine's z turns the reported
17+
* frame with it and does nothing else. That last one catches a frame
18+
* built for the wrong joint or composed in the wrong order.
19+
*
20+
* Author: LinuxCNC
21+
* License: GPL Version 2
22+
* System: Linux
23+
*
24+
* Copyright (c) 2026 All rights reserved.
25+
*/
26+
27+
#include <rtapi.h>
28+
#include <rtapi_app.h>
29+
#include <rtapi_math.h>
30+
#include <rtapi_string.h>
31+
#include <hal.h>
32+
#include <emcpos.h>
33+
#include <emcmotcfg.h>
34+
#include <kinematics.h>
35+
36+
MODULE_LICENSE("GPL");
37+
MODULE_DESCRIPTION("kinematics frame checker");
38+
39+
static int joints = 5;
40+
RTAPI_MP_INT(joints, "joint count the module under test was loaded for");
41+
42+
static int carries_tool = 0;
43+
RTAPI_MP_INT(carries_tool, "1 when the machine carries the tool rather than the work");
44+
45+
static int fixed_spindle = 0;
46+
RTAPI_MP_INT(fixed_spindle, "1 when the module reports a spindle square with the machine");
47+
48+
static int ktype = 0;
49+
RTAPI_MP_INT(ktype, "switchkins type where the module models its own machine");
50+
51+
static int spin = -1;
52+
RTAPI_MP_INT(spin, "joint that turns the whole head about the machine's z, -1 for none");
53+
54+
static int r1 = -1, r2 = -1, r3 = -1;
55+
RTAPI_MP_INT(r1, "joint number of the first rotary to sweep");
56+
RTAPI_MP_INT(r2, "joint number of the second rotary, -1 for none");
57+
RTAPI_MP_INT(r3, "joint number of the third rotary, -1 for none");
58+
59+
/* switchkins.h is not an exported header, and a module rejects a type
60+
it does not have, so the loop only needs an upper bound */
61+
#define MAX_TYPES 9
62+
63+
#define TO_RAD (M_PI / 180.0)
64+
#define STEP 1e-6
65+
#define TURN 15.0
66+
#define TOL 1e-6
67+
68+
static int comp_id = -1;
69+
static int failures;
70+
71+
static void expect(int ok, const char *what, const double *j)
72+
{
73+
char pose[128];
74+
int i, n = 0;
75+
76+
if (ok) { return; }
77+
for (i = 0; i < joints && n < (int)sizeof(pose) - 12; i++) {
78+
n += rtapi_snprintf(pose + n, sizeof(pose) - n, "%s%.4g",
79+
i ? "," : "", j[i]);
80+
}
81+
rtapi_print_msg(RTAPI_MSG_ERR, "framecheck: FAIL %s at [%s]\n", what, pose);
82+
failures++;
83+
}
84+
85+
static int close3(const PmCartesian *a, double x, double y, double z)
86+
{
87+
return fabs(a->x - x) < TOL && fabs(a->y - y) < TOL && fabs(a->z - z) < TOL;
88+
}
89+
90+
/* The helpers in kins_util.c are not exported to a loadable module, and
91+
working the answers out here is the better test anyway: nothing the
92+
module under test uses is reused to judge it. */
93+
static double dot(const PmCartesian *a, const PmCartesian *b)
94+
{
95+
return a->x * b->x + a->y * b->y + a->z * b->z;
96+
}
97+
98+
static int is_rotation(const PmRotationMatrix *m)
99+
{
100+
PmCartesian cross;
101+
102+
if (fabs(dot(&m->x, &m->x) - 1) > TOL) { return 0; }
103+
if (fabs(dot(&m->y, &m->y) - 1) > TOL) { return 0; }
104+
if (fabs(dot(&m->z, &m->z) - 1) > TOL) { return 0; }
105+
if (fabs(dot(&m->x, &m->y)) > TOL) { return 0; }
106+
if (fabs(dot(&m->x, &m->z)) > TOL) { return 0; }
107+
if (fabs(dot(&m->y, &m->z)) > TOL) { return 0; }
108+
109+
/* right handed, so the third column is the cross product of the
110+
other two rather than its negative */
111+
cross.x = m->x.y * m->y.z - m->x.z * m->y.y;
112+
cross.y = m->x.z * m->y.x - m->x.x * m->y.z;
113+
cross.z = m->x.x * m->y.y - m->x.y * m->y.x;
114+
return close3(&cross, m->z.x, m->z.y, m->z.z);
115+
}
116+
117+
/* how the reported position responds to a displacement of machine axis
118+
jno: column jno of the forward transform's linear part */
119+
static void response(const double *j, int jno, PmCartesian *out)
120+
{
121+
double t[EMCMOT_MAX_JOINTS];
122+
EmcPose lo, hi;
123+
KINEMATICS_FORWARD_FLAGS ff = 0;
124+
KINEMATICS_INVERSE_FLAGS inf = 0;
125+
126+
memcpy(t, j, sizeof(t));
127+
128+
t[jno] = j[jno] - STEP;
129+
kinematicsForward(t, &lo, &ff, &inf);
130+
t[jno] = j[jno] + STEP;
131+
kinematicsForward(t, &hi, &ff, &inf);
132+
133+
out->x = (hi.tran.x - lo.tran.x) / (2 * STEP);
134+
out->y = (hi.tran.y - lo.tran.y) / (2 * STEP);
135+
out->z = (hi.tran.z - lo.tran.z) / (2 * STEP);
136+
}
137+
138+
/* turn a frame about the machine's z, which is what a joint carrying
139+
the whole head does to everything above it */
140+
static void turn_about_z(double deg, const PmRotationMatrix *m,
141+
PmRotationMatrix *out)
142+
{
143+
const double c = cos(deg * TO_RAD);
144+
const double s = sin(deg * TO_RAD);
145+
146+
out->x.x = c * m->x.x - s * m->x.y;
147+
out->x.y = s * m->x.x + c * m->x.y;
148+
out->x.z = m->x.z;
149+
out->y.x = c * m->y.x - s * m->y.y;
150+
out->y.y = s * m->y.x + c * m->y.y;
151+
out->y.z = m->y.z;
152+
out->z.x = c * m->z.x - s * m->z.y;
153+
out->z.y = s * m->z.x + c * m->z.y;
154+
out->z.z = m->z.z;
155+
}
156+
157+
/* Reporting the frames is optional, and a switchable module usually
158+
supplies them for some of its types and not others, so a type that
159+
declines is skipped rather than failed. */
160+
static int supplies_frames(const double *j)
161+
{
162+
KINEMATICS_FORWARD_FLAGS ff = 0;
163+
PmRotationMatrix m;
164+
165+
if (kinematicsWorkFrame(j, &m, &ff)) { return 0; }
166+
if (kinematicsToolFrame(j, &m, &ff)) { return 0; }
167+
return 1;
168+
}
169+
170+
static void check(const double *j, int own_kinematics)
171+
{
172+
KINEMATICS_FORWARD_FLAGS ff = 0;
173+
PmRotationMatrix work, tool, turned, want;
174+
PmCartesian d;
175+
double t[EMCMOT_MAX_JOINTS];
176+
177+
kinematicsWorkFrame(j, &work, &ff);
178+
kinematicsToolFrame(j, &tool, &ff);
179+
180+
expect(is_rotation(&work), "work frame is a rotation", j);
181+
expect(is_rotation(&tool), "tool frame is a rotation", j);
182+
183+
if (carries_tool) {
184+
/* nothing turns the work, at any pose */
185+
expect(close3(&work.x, 1, 0, 0) && close3(&work.y, 0, 1, 0)
186+
&& close3(&work.z, 0, 0, 1), "work frame is the machine frame", j);
187+
} else {
188+
/* the forward transform maps a machine displacement to a work
189+
one, so a row of the work frame is one of its columns */
190+
response(j, 0, &d);
191+
expect(close3(&d, work.x.x, work.y.x, work.z.x), "work frame against X", j);
192+
response(j, 1, &d);
193+
expect(close3(&d, work.x.y, work.y.y, work.z.y), "work frame against Y", j);
194+
response(j, 2, &d);
195+
expect(close3(&d, work.x.z, work.y.z, work.z.z), "work frame against Z", j);
196+
}
197+
198+
/* the rest describes the module's own machine, so its other
199+
kinematics types, identity and the tool frame's own, are not
200+
asked: they leave everything square with the machine */
201+
if (!own_kinematics) { return; }
202+
203+
if (fixed_spindle) {
204+
expect(close3(&tool.x, 1, 0, 0) && close3(&tool.y, 0, 1, 0)
205+
&& close3(&tool.z, 0, 0, 1), "the spindle stays square", j);
206+
}
207+
208+
if (spin >= 0) {
209+
memcpy(t, j, sizeof(t));
210+
t[spin] = j[spin] + TURN;
211+
kinematicsToolFrame(t, &turned, &ff);
212+
turn_about_z(TURN, &tool, &want);
213+
expect(close3(&turned.x, want.x.x, want.x.y, want.x.z),
214+
"tool x turns with the head", j);
215+
expect(close3(&turned.y, want.y.x, want.y.y, want.y.z),
216+
"tool y turns with the head", j);
217+
expect(close3(&turned.z, want.z.x, want.z.y, want.z.z),
218+
"tool axis turns with the head", j);
219+
}
220+
}
221+
222+
int rtapi_app_main(void)
223+
{
224+
/* rotary values away from the identity, including the quarter and
225+
half turns where a sine changes sign or a cosine vanishes */
226+
static const double angle[] = { 0, 30, -25, 90, 180 };
227+
const int angles = sizeof(angle) / sizeof(angle[0]);
228+
double j[EMCMOT_MAX_JOINTS];
229+
int a, b, c, t;
230+
int checked = 0;
231+
232+
if (joints < 1 || joints > EMCMOT_MAX_JOINTS) {
233+
rtapi_print_msg(RTAPI_MSG_ERR, "framecheck: joints=%d\n", joints);
234+
return -1;
235+
}
236+
237+
comp_id = hal_init("framecheck");
238+
if (comp_id < 0) { return comp_id; }
239+
240+
if (kinematicsType() == 0) {
241+
rtapi_print_msg(RTAPI_MSG_ERR, "framecheck: the module reports no type\n");
242+
hal_exit(comp_id);
243+
return -1;
244+
}
245+
246+
memset(j, 0, sizeof(j));
247+
if (!carries_tool) { j[0] = 10; j[1] = 20; j[2] = 30; }
248+
249+
/* every kinematics the module offers, not just the one it starts
250+
in: the frames a switchable module reports are per type, and the
251+
type that turns the work is rarely the default */
252+
for (t = 0; t < MAX_TYPES; t++) {
253+
if (kinematicsSwitchable() && kinematicsSwitch(t)) { break; }
254+
if (!supplies_frames(j)) { continue; }
255+
checked++;
256+
257+
for (a = 0; a < angles; a++) {
258+
if (r1 >= 0) { j[r1] = angle[a]; }
259+
for (b = 0; b < angles; b++) {
260+
if (r2 >= 0) { j[r2] = angle[b]; }
261+
for (c = 0; c < angles; c++) {
262+
if (r3 >= 0) { j[r3] = angle[c]; }
263+
check(j, t == ktype);
264+
if (r3 < 0) { break; }
265+
}
266+
if (r2 < 0) { break; }
267+
}
268+
if (r1 < 0) { break; }
269+
}
270+
271+
if (!kinematicsSwitchable()) { break; }
272+
}
273+
274+
if (!checked) {
275+
rtapi_print_msg(RTAPI_MSG_ERR,
276+
"framecheck: the module reports frames for no type\n");
277+
hal_exit(comp_id);
278+
return -1;
279+
}
280+
281+
if (failures) {
282+
rtapi_print_msg(RTAPI_MSG_ERR,
283+
"framecheck: %d check(s) failed\n", failures);
284+
hal_exit(comp_id);
285+
return -1;
286+
}
287+
288+
rtapi_print("framecheck: frames agree for %d kinematics type(s)\n", checked);
289+
hal_ready(comp_id);
290+
return 0;
291+
}
292+
293+
void rtapi_app_exit(void) { hal_exit(comp_id); }

‎tests/kins-frames/skip‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
# Builds a realtime component with halcompile, which needs the build
3+
# tools present. Skip when testing installed packages.
4+
[ -z "$SYSTEM_BUILD" ]

‎tests/kins-frames/test.sh‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
set -e
3+
4+
${SUDO} halcompile --install framecheck.c >/dev/null
5+
6+
# One hal file per module: they all define the same entry points, so
7+
# only one can be loaded at a time.
8+
run() {
9+
local hal
10+
hal=$(mktemp --suffix=.hal)
11+
{ printf 'loadrt %s\n' "$1"
12+
printf '%s\n' "$2"
13+
printf 'loadrt framecheck %s\n' "$3"
14+
} > "$hal"
15+
echo "=== $1"
16+
halrun -f "$hal"
17+
rm -f "$hal"
18+
}
19+
20+
run "xyzac-trt-kins coordinates=XYZAC" \
21+
"setp xyzac-trt-kins.y-offset 3
22+
setp xyzac-trt-kins.z-offset 11
23+
setp xyzac-trt-kins.x-rot-point 1
24+
setp xyzac-trt-kins.y-rot-point 2
25+
setp xyzac-trt-kins.z-rot-point 5" \
26+
"joints=5 r1=3 r2=4 fixed_spindle=1"
27+
28+
run "xyzbc-trt-kins coordinates=XYZBC" \
29+
"setp xyzbc-trt-kins.y-offset 3
30+
setp xyzbc-trt-kins.z-offset 11
31+
setp xyzbc-trt-kins.x-rot-point 1
32+
setp xyzbc-trt-kins.y-rot-point 2
33+
setp xyzbc-trt-kins.z-rot-point 5" \
34+
"joints=5 r1=3 r2=4 fixed_spindle=1"
35+
36+
# a nutation angle of zero leaves the head square with the machine and
37+
# the interesting geometry untested, so give both a real one
38+
run "xyzacb_trsrn" \
39+
"setp xyzacb_trsrn_kins.nut-angle 45
40+
setp xyzacb_trsrn_kins.y-pivot 100
41+
setp xyzacb_trsrn_kins.z-pivot 200
42+
setp xyzacb_trsrn_kins.tool-offset-z 50" \
43+
"joints=6 r1=3 r2=4 r3=5 spin=5 ktype=1"
44+
45+
run "xyzbca_trsrn" \
46+
"setp xyzbca_trsrn_kins.nut-angle 45
47+
setp xyzbca_trsrn_kins.x-pivot 100
48+
setp xyzbca_trsrn_kins.z-pivot 200
49+
setp xyzbca_trsrn_kins.tool-offset-z 50" \
50+
"joints=6 r1=3 r2=4 r3=5 spin=5 ktype=1"
51+
52+
run "pumakins" "setp pumakins.A2 300" \
53+
"joints=6 carries_tool=1 r1=0 r2=3 r3=4 spin=0"

0 commit comments

Comments
 (0)