@rulecad/cad 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +24 -10
- package/CHANGELOG.md +404 -0
- package/EXAMPLES.md +40 -38
- package/README.md +193 -67
- package/TUTORIAL.md +2 -2
- package/bin/cad.mjs +2250 -512
- package/lib/animate.mjs +173 -49
- package/lib/assembly.mjs +1847 -434
- package/lib/beams.mjs +256 -52
- package/lib/belts.mjs +66 -15
- package/lib/board-work.mjs +12 -0
- package/lib/bought.mjs +309 -53
- package/lib/buildpage.mjs +179 -29
- package/lib/carried.mjs +201 -0
- package/lib/clone.mjs +194 -42
- package/lib/design.mjs +649 -121
- package/lib/drive.mjs +187 -63
- package/lib/edit.mjs +899 -191
- package/lib/errors.mjs +16 -3
- package/lib/explain.mjs +62 -13
- package/lib/fasten.mjs +2702 -507
- package/lib/fittings.mjs +715 -121
- package/lib/fixings.mjs +21 -9
- package/lib/framebuild.mjs +790 -175
- package/lib/framecheck.mjs +1901 -368
- package/lib/framegen.mjs +100 -22
- package/lib/frameheat.mjs +225 -42
- package/lib/frameprint.mjs +513 -108
- package/lib/frames.mjs +81 -17
- package/lib/framesize.mjs +792 -120
- package/lib/frametip.mjs +235 -0
- package/lib/gait.mjs +135 -23
- package/lib/grip.mjs +62 -13
- package/lib/guides.mjs +1403 -230
- package/lib/host.mjs +123 -21
- package/lib/inplace.mjs +171 -36
- package/lib/jigs.mjs +360 -78
- package/lib/joins.mjs +697 -118
- package/lib/jsonedit.mjs +88 -26
- package/lib/kept.mjs +84 -0
- package/lib/kit.mjs +438 -68
- package/lib/layout.mjs +2 -1
- package/lib/links.mjs +149 -42
- package/lib/list.mjs +326 -64
- package/lib/live-guard.mjs +68 -0
- package/lib/live-shape.mjs +96 -24
- package/lib/live.mjs +189 -59
- package/lib/loads.mjs +579 -130
- package/lib/machine.mjs +372 -74
- package/lib/magnets.mjs +162 -44
- package/lib/measured.mjs +417 -94
- package/lib/mesh.mjs +526 -119
- package/lib/meshcmd.mjs +215 -60
- package/lib/motors.mjs +1021 -213
- package/lib/motorsvg.mjs +114 -32
- package/lib/names.mjs +3 -1
- package/lib/optimise.mjs +978 -283
- package/lib/order.mjs +519 -112
- package/lib/panels.mjs +482 -94
- package/lib/parts.mjs +78 -23
- package/lib/pins.mjs +163 -44
- package/lib/plates.mjs +223 -35
- package/lib/player.mjs +37 -9
- package/lib/power.mjs +64 -13
- package/lib/preview.mjs +83 -16
- package/lib/profilemeasure.mjs +572 -129
- package/lib/rules.mjs +15 -5
- package/lib/sandbox-hooks.mjs +77 -19
- package/lib/sandbox-run.mjs +117 -18
- package/lib/sandbox.mjs +509 -60
- package/lib/schema.mjs +42 -11
- package/lib/seats.mjs +227 -49
- package/lib/services.mjs +261 -52
- package/lib/sheet.mjs +851 -178
- package/lib/slotnuts.mjs +213 -34
- package/lib/targets/gist.mjs +4 -1
- package/lib/targets.mjs +22 -4
- package/lib/task.mjs +376 -79
- package/lib/versions.mjs +111 -28
- package/lib/wires.mjs +228 -45
- package/lib/workspace.mjs +197 -12
- package/package.json +19 -19
- package/schemas/assembly.schema.json +2 -2
package/lib/task.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// "task": { "tool": "pen.tip", "sheet": { "size": "a5", "at": [60, -74, 0] }, "speed": 50, "accuracy": 0.3 }
|
|
5
5
|
import fs from 'node:fs';
|
|
6
6
|
import { chainFromMotion, reach, sheetFrame, sheetPoint, jacobian, fk } from '@rulecad/robots';
|
|
7
|
-
import {
|
|
7
|
+
import { peekRuleSet } from './rules.mjs';
|
|
8
8
|
import { paths } from './workspace.mjs';
|
|
9
9
|
import { trainFrom, workingSpeed } from './motors.mjs';
|
|
10
10
|
import { placeItems } from '@rulecad/model-link';
|
|
@@ -15,21 +15,64 @@ const CORNERS = ['lower left', 'lower right', 'upper right', 'upper left'];
|
|
|
15
15
|
|
|
16
16
|
/** The task as written, checked by name when the assembly is built. */
|
|
17
17
|
export function taskOf(a, motion, num) {
|
|
18
|
-
const t = a.task;
|
|
18
|
+
const t = a.task;
|
|
19
|
+
if (!t) return null;
|
|
19
20
|
const what = 'task';
|
|
20
|
-
if (!t.tool || !String(t.tool).includes('.'))
|
|
21
|
+
if (!t.tool || !String(t.tool).includes('.'))
|
|
22
|
+
throw new Error(`${what}: tool is an anchor, <item>.<anchor> (got ${JSON.stringify(t.tool)})`);
|
|
21
23
|
const given = ['sheet', 'region', 'volume'].filter((k) => t[k] != null);
|
|
22
|
-
if (given.length !== 1)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
if (given.length !== 1)
|
|
25
|
+
throw new Error(`${what}: give one of sheet, region or volume (got ${given.join(', ') || 'none'})`);
|
|
26
|
+
const [id, name] = String(t.tool).split('.'),
|
|
27
|
+
mark = motion.marks.find((m) => m.id === id && m.name === name);
|
|
28
|
+
if (!mark)
|
|
29
|
+
throw new Error(
|
|
30
|
+
`${what}: no anchor "${t.tool}" (anchors: ${
|
|
31
|
+
motion.marks
|
|
32
|
+
.filter((m) => m.id === id)
|
|
33
|
+
.map((m) => m.name)
|
|
34
|
+
.join(', ') || `no item ${id}`
|
|
35
|
+
})`,
|
|
36
|
+
);
|
|
37
|
+
const vec = (v) => v.map(num),
|
|
38
|
+
value = (k) => {
|
|
39
|
+
if (t[k] == null) return null;
|
|
40
|
+
const x = num(t[k]);
|
|
41
|
+
if (!(x > 0)) throw new Error(`${what}: ${k} is above 0 (got ${x})`);
|
|
42
|
+
return x;
|
|
43
|
+
};
|
|
44
|
+
const out = {
|
|
45
|
+
tool: t.tool,
|
|
46
|
+
item: id,
|
|
47
|
+
point: [mark.frame[3], mark.frame[7], mark.frame[11]],
|
|
48
|
+
speed: value('speed'),
|
|
49
|
+
accuracy: value('accuracy'),
|
|
50
|
+
elbow: t.elbow ?? null,
|
|
51
|
+
deflection: t.deflection != null ? num(t.deflection) : 0,
|
|
52
|
+
payload: t.payload != null ? num(t.payload) : null,
|
|
53
|
+
accel: t.accel != null ? num(t.accel) : null,
|
|
54
|
+
blockTime: value('blockTime'),
|
|
55
|
+
};
|
|
56
|
+
if (t.elbow != null && !['left', 'right'].includes(t.elbow))
|
|
57
|
+
throw new Error(`${what}: elbow is left or right (got ${JSON.stringify(t.elbow)})`);
|
|
58
|
+
if (t.sheet)
|
|
59
|
+
out.sheet = {
|
|
60
|
+
...t.sheet,
|
|
61
|
+
at: vec(t.sheet.at),
|
|
62
|
+
...(t.sheet.normal ? { normal: vec(t.sheet.normal) } : {}),
|
|
63
|
+
...(t.sheet.u ? { u: vec(t.sheet.u) } : {}),
|
|
64
|
+
};
|
|
65
|
+
const ranges = (o) =>
|
|
66
|
+
Object.fromEntries(
|
|
67
|
+
Object.entries(o).map(([k, x]) => [k, Array.isArray(x) ? x.map(num) : typeof x === 'object' ? x : num(x)]),
|
|
68
|
+
);
|
|
30
69
|
if (t.region) out.region = ranges(t.region);
|
|
31
70
|
if (t.volume) out.volume = ranges(t.volume);
|
|
32
|
-
try {
|
|
71
|
+
try {
|
|
72
|
+
if (out.sheet) sheetFrame(out.sheet);
|
|
73
|
+
} catch (e) {
|
|
74
|
+
throw new Error(`${what}: ${e.message}`);
|
|
75
|
+
}
|
|
33
76
|
return out;
|
|
34
77
|
}
|
|
35
78
|
|
|
@@ -38,46 +81,115 @@ export const chainOf = (s) => chainFromMotion(s.motion, { tip: s.task.item, tool
|
|
|
38
81
|
// One sampling of the task, kept so the three checks and the picture read the same samples
|
|
39
82
|
export function sample(s) {
|
|
40
83
|
if (s.taskSamples) return s.taskSamples;
|
|
41
|
-
const t = s.task,
|
|
84
|
+
const t = s.task,
|
|
85
|
+
c = chainOf(s),
|
|
86
|
+
spec = t.sheet ? { sheet: t.sheet } : t.region ? { region: t.region } : { volume: t.volume };
|
|
42
87
|
let step = 10;
|
|
43
|
-
if (t.sheet) {
|
|
88
|
+
if (t.sheet) {
|
|
89
|
+
const f = sheetFrame(t.sheet);
|
|
90
|
+
step = Math.min(f.width, f.height) / 12;
|
|
91
|
+
}
|
|
44
92
|
const go = (elbow) => reach(c, { ...spec, step, ...(elbow ? { elbow } : {}) });
|
|
45
93
|
// Hold the elbow it's given, or the one that reaches the most
|
|
46
|
-
let r,
|
|
47
|
-
|
|
94
|
+
let r,
|
|
95
|
+
elbow = t.elbow;
|
|
96
|
+
if (c.dof === 2 && !elbow) {
|
|
97
|
+
const a = go('right'),
|
|
98
|
+
b = go('left');
|
|
99
|
+
r =
|
|
100
|
+
b.summary.reachable > a.summary.reachable ||
|
|
101
|
+
(b.summary.reachable === a.summary.reachable && b.summary.margin > a.summary.margin)
|
|
102
|
+
? ((elbow = 'left'), b)
|
|
103
|
+
: ((elbow = 'right'), a);
|
|
104
|
+
} else r = go(elbow);
|
|
48
105
|
return (s.taskSamples = { c, r, elbow, step });
|
|
49
106
|
}
|
|
50
107
|
|
|
51
108
|
// A point on the sheet, said in millimetres from its centre
|
|
52
|
-
const onSheet = (t, p) => {
|
|
109
|
+
const onSheet = (t, p) => {
|
|
110
|
+
if (!t.sheet) return `(${p.map((x) => n3(x, 1)).join(', ')})`;
|
|
111
|
+
const f = sheetFrame(t.sheet),
|
|
112
|
+
d = p.map((x, k) => x - f.at[k]);
|
|
113
|
+
return `(${n3(dot(d, f.u), 1)}, ${n3(dot(d, f.v), 1)}) from its centre`;
|
|
114
|
+
};
|
|
53
115
|
|
|
54
116
|
function reachCheck(s, f, picture) {
|
|
55
|
-
const t = s.task,
|
|
56
|
-
|
|
57
|
-
|
|
117
|
+
const t = s.task,
|
|
118
|
+
{ r, elbow } = sample(s),
|
|
119
|
+
code = 'task.reach',
|
|
120
|
+
{ summary } = r,
|
|
121
|
+
thing = t.sheet ? 'sheet' : t.region ? 'region' : 'volume';
|
|
122
|
+
const miss = summary.corners
|
|
123
|
+
.map((c, i) => ({ c, name: CORNERS[i] ?? `corner ${i + 1}` }))
|
|
124
|
+
.filter((x) => !x.c.reachable)
|
|
125
|
+
.sort((a, b) => b.c.miss - a.c.miss);
|
|
126
|
+
const values = {
|
|
127
|
+
margin: summary.margin,
|
|
128
|
+
reachable: summary.reachable,
|
|
129
|
+
total: summary.total,
|
|
130
|
+
elbow,
|
|
131
|
+
samples: r.samples.map((x) => ({
|
|
132
|
+
point: x.point.map((v) => +v.toFixed(3)),
|
|
133
|
+
reachable: x.reachable,
|
|
134
|
+
q: x.q.map((v) => +v.toFixed(4)),
|
|
135
|
+
margin: +x.margin.toFixed(3),
|
|
136
|
+
manipulability: +x.manipulability.toFixed(1),
|
|
137
|
+
})),
|
|
138
|
+
picture,
|
|
139
|
+
};
|
|
58
140
|
if (miss.length) {
|
|
59
141
|
const w = miss[0];
|
|
60
|
-
return f(
|
|
142
|
+
return f(
|
|
143
|
+
'fail',
|
|
144
|
+
`the ${w.name} corner of the ${thing} can't be reached: ${t.tool} misses it by ${n3(w.c.miss)} mm (${summary.reachable} of ${summary.total} points on it are in reach)`,
|
|
145
|
+
`${thing}: ${w.name} out of reach`,
|
|
146
|
+
{ code, at: w.c.point, refs: { corner: w.name, tool: t.tool }, values: { ...values, miss: w.c.miss } },
|
|
147
|
+
);
|
|
61
148
|
}
|
|
62
149
|
if (summary.reachable < summary.total) {
|
|
63
150
|
const w = summary.worst;
|
|
64
|
-
return f(
|
|
151
|
+
return f(
|
|
152
|
+
'fail',
|
|
153
|
+
`${summary.total - summary.reachable} of ${summary.total} points on the ${thing} can't be reached, ${n3(w.miss)} mm short at worst, at ${onSheet(t, w.point)}: limits or the hole near the base`,
|
|
154
|
+
`${thing}: not all in reach`,
|
|
155
|
+
{ code, at: w.point, refs: { tool: t.tool }, values: { ...values, miss: w.miss } },
|
|
156
|
+
);
|
|
65
157
|
}
|
|
66
|
-
const w = summary.corners.reduce((a, b) => (b.margin < a.margin ? b : a)),
|
|
67
|
-
|
|
158
|
+
const w = summary.corners.reduce((a, b) => (b.margin < a.margin ? b : a)),
|
|
159
|
+
near = Math.min(summary.margin, w.margin);
|
|
160
|
+
f(
|
|
161
|
+
'pass',
|
|
162
|
+
`${t.tool} reaches the whole ${thing} with ${elbow ? `the elbow to the ${elbow}` : 'its joints'}: ${n3(near)} mm to spare at the worst point`,
|
|
163
|
+
null,
|
|
164
|
+
{ code, at: summary.worst.point, refs: { tool: t.tool }, values },
|
|
165
|
+
);
|
|
68
166
|
}
|
|
69
167
|
|
|
70
168
|
// How fast a sampled point can go: in each direction across the sheet, the speed at which the first joint hits its limit
|
|
71
169
|
function fastest(c, q, frame, limits) {
|
|
72
170
|
if (!frame) return fastestAnyWay(c, q, limits);
|
|
73
|
-
const J = jacobian(c, q),
|
|
74
|
-
|
|
171
|
+
const J = jacobian(c, q),
|
|
172
|
+
rows = frame
|
|
173
|
+
? [frame.u, frame.v].map((d) => c.names.map((_, i) => d[0] * J[0][i] + d[1] * J[1][i] + d[2] * J[2][i]))
|
|
174
|
+
: [J[0], J[1]];
|
|
175
|
+
const N = c.names.length,
|
|
176
|
+
a = dot2(rows[0], rows[0]),
|
|
177
|
+
b = dot2(rows[0], rows[1]),
|
|
178
|
+
d = dot2(rows[1], rows[1]),
|
|
179
|
+
det = a * d - b * b;
|
|
75
180
|
if (Math.abs(det) < 1e-9) return 0;
|
|
76
181
|
let worst = Infinity;
|
|
77
182
|
for (let k = 0; k < 24; k++) {
|
|
78
|
-
const th = (k * Math.PI) / 12,
|
|
183
|
+
const th = (k * Math.PI) / 12,
|
|
184
|
+
vx = Math.cos(th),
|
|
185
|
+
vy = Math.sin(th),
|
|
186
|
+
l0 = (d * vx - b * vy) / det,
|
|
187
|
+
l1 = (a * vy - b * vx) / det;
|
|
79
188
|
let scale = Infinity;
|
|
80
|
-
for (let i = 0; i < N; i++) {
|
|
189
|
+
for (let i = 0; i < N; i++) {
|
|
190
|
+
const qd = Math.abs(rows[0][i] * l0 + rows[1][i] * l1);
|
|
191
|
+
if (qd > 1e-12) scale = Math.min(scale, limits[i] / qd);
|
|
192
|
+
}
|
|
81
193
|
worst = Math.min(worst, scale);
|
|
82
194
|
}
|
|
83
195
|
return worst;
|
|
@@ -85,11 +197,28 @@ function fastest(c, q, frame, limits) {
|
|
|
85
197
|
const dot2 = (a, b) => a.reduce((t, x, i) => t + x * b[i], 0);
|
|
86
198
|
// In a volume or along a single axis: every way the joints can move together, the slowest the tool goes before a joint runs out
|
|
87
199
|
function fastestAnyWay(c, q, limits) {
|
|
88
|
-
const J = jacobian(c, q),
|
|
200
|
+
const J = jacobian(c, q),
|
|
201
|
+
N = c.names.length,
|
|
202
|
+
turns =
|
|
203
|
+
N === 1
|
|
204
|
+
? [[1]]
|
|
205
|
+
: N === 2
|
|
206
|
+
? Array.from({ length: 24 }, (_, k) => [Math.cos((k * Math.PI) / 12), Math.sin((k * Math.PI) / 12)])
|
|
207
|
+
: Array.from({ length: 200 }, (_, k) => {
|
|
208
|
+
const z = 1 - (2 * (k + 0.5)) / 200,
|
|
209
|
+
r = Math.sqrt(1 - z * z),
|
|
210
|
+
a = k * 2.399963;
|
|
211
|
+
return [r * Math.cos(a), r * Math.sin(a), z, ...new Array(N - 3).fill(0)];
|
|
212
|
+
});
|
|
89
213
|
let worst = Infinity;
|
|
90
214
|
for (const d of turns) {
|
|
91
|
-
const v = J.map((row) => dot2(row, d)),
|
|
92
|
-
|
|
215
|
+
const v = J.map((row) => dot2(row, d)),
|
|
216
|
+
tool = Math.hypot(...v);
|
|
217
|
+
if (tool < 1e-9) continue;
|
|
218
|
+
let scale = Infinity;
|
|
219
|
+
d.forEach((x, i) => {
|
|
220
|
+
if (Math.abs(x) > 1e-12) scale = Math.min(scale, limits[i] / Math.abs(x));
|
|
221
|
+
});
|
|
93
222
|
worst = Math.min(worst, tool * scale);
|
|
94
223
|
}
|
|
95
224
|
return worst === Infinity ? 0 : worst;
|
|
@@ -99,37 +228,88 @@ function fastestAnyWay(c, q, limits) {
|
|
|
99
228
|
function jointSpeeds(s, c) {
|
|
100
229
|
const motion = s.motion;
|
|
101
230
|
return c.names.map((id) => {
|
|
102
|
-
const t = trainFrom(motion, id),
|
|
231
|
+
const t = trainFrom(motion, id),
|
|
232
|
+
d = motion.drives.find((x) => x.joint === t.root);
|
|
103
233
|
if (!d) return null;
|
|
104
|
-
const rpm = workingSpeed(d.model, d.spec, d.supply),
|
|
234
|
+
const rpm = workingSpeed(d.model, d.spec, d.supply),
|
|
235
|
+
output = (rpm / t.ratio) * 6; // deg/s at the joint
|
|
105
236
|
return t.slide != null ? output * t.slide : output;
|
|
106
237
|
});
|
|
107
238
|
}
|
|
108
239
|
|
|
109
240
|
function speedCheck(s, f) {
|
|
110
|
-
const t = s.task,
|
|
111
|
-
|
|
241
|
+
const t = s.task,
|
|
242
|
+
code = 'task.speed';
|
|
243
|
+
if (t.speed == null) return;
|
|
244
|
+
const { c, r } = sample(s),
|
|
245
|
+
limits = jointSpeeds(s, c),
|
|
246
|
+
frame = t.sheet ? sheetFrame(t.sheet) : null;
|
|
112
247
|
const free = c.names.filter((_, i) => limits[i] == null);
|
|
113
|
-
if (free.length)
|
|
248
|
+
if (free.length)
|
|
249
|
+
return f(
|
|
250
|
+
'warn',
|
|
251
|
+
`nothing says how fast ${free.join(' and ')} can turn, so ${t.speed} mm/s isn't checked: give ${free.length > 1 ? 'them' : 'it'} a motor`,
|
|
252
|
+
`speed: ${free.join(', ')} undriven`,
|
|
253
|
+
{ code, refs: { joints: free } },
|
|
254
|
+
);
|
|
114
255
|
let worst = null;
|
|
115
|
-
for (const x of r.samples)
|
|
256
|
+
for (const x of r.samples)
|
|
257
|
+
if (x.reachable) {
|
|
258
|
+
const v = fastest(c, x.q, frame, limits);
|
|
259
|
+
if (!worst || v < worst.v) worst = { v, x };
|
|
260
|
+
}
|
|
116
261
|
if (!worst) return;
|
|
117
|
-
const values = { slowest: worst.v, wanted: t.speed, margin: worst.v / t.speed },
|
|
118
|
-
|
|
262
|
+
const values = { slowest: worst.v, wanted: t.speed, margin: worst.v / t.speed },
|
|
263
|
+
at = worst.x.point,
|
|
264
|
+
say = `at ${n3(worst.v, 1)} mm/s at the slowest point, ${onSheet(t, at)}, against the ${t.speed} mm/s it's asked for`;
|
|
265
|
+
if (worst.v < t.speed)
|
|
266
|
+
f(
|
|
267
|
+
'fail',
|
|
268
|
+
`${t.tool} moves ${say}: a faster motor speed, less ratio or a shorter arm`,
|
|
269
|
+
`speed ${n3(worst.v, 0)} mm/s`,
|
|
270
|
+
{ code, at, values },
|
|
271
|
+
);
|
|
119
272
|
else f('pass', `${t.tool} moves ${say}`, null, { code, at, values });
|
|
120
273
|
}
|
|
121
274
|
|
|
122
275
|
// Each joint's error terms: step resolution from its motor, what the assembly says of its play, its twist and so on, and a
|
|
123
276
|
// guide's rail's (its parallelism, its carriage's play, the beam's straightness and sag)
|
|
124
277
|
export function terms(s, c) {
|
|
125
|
-
const motion = s.motion,
|
|
278
|
+
const motion = s.motion,
|
|
279
|
+
specs = new Map((s.a.joints || []).map((j) => [j.id, j]));
|
|
126
280
|
return c.names.map((id) => {
|
|
127
|
-
const out = [],
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
281
|
+
const out = [],
|
|
282
|
+
t = trainFrom(motion, id),
|
|
283
|
+
d = motion.drives.find((x) => x.joint === t.root),
|
|
284
|
+
spec = specs.get(id);
|
|
285
|
+
if (d?.model.stepAngle) {
|
|
286
|
+
const micro = d.spec.microsteps ?? 16;
|
|
287
|
+
out.push({
|
|
288
|
+
term: 'step',
|
|
289
|
+
value: t.slide != null ? (d.model.stepAngle / micro) * t.slide : d.model.stepAngle / micro / t.ratio,
|
|
290
|
+
kind: 'spec',
|
|
291
|
+
note: `${n3(d.model.stepAngle, 3)}° at 1/${micro} step, ${n3(t.ratio, 1)}:1`,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
const belt = motion.joints.find((x) => x.id === id)?.slide?.stretch;
|
|
295
|
+
if (belt)
|
|
296
|
+
out.push({
|
|
297
|
+
term: 'belt stretch',
|
|
298
|
+
value: (belt.load * belt.length) / (belt.k * belt.width),
|
|
299
|
+
kind: 'estimate',
|
|
300
|
+
note: `${belt.load} N over ${belt.length} mm`,
|
|
301
|
+
});
|
|
302
|
+
for (const [term, expr] of Object.entries(spec?.errors ?? {}))
|
|
303
|
+
out.push({ term, value: s.num(expr), kind: kindOf(expr), expr });
|
|
131
304
|
for (const x of spec?._guide?.terms ?? []) out.push({ term: x.term, value: x.value, kind: x.kind, note: x.note }); // (a guide's rail: mm along its travel, from its rule sets and the beam's)
|
|
132
|
-
if (spec?._guide?.id) {
|
|
305
|
+
if (spec?._guide?.id) {
|
|
306
|
+
const fr = frameTerms(s, spec._guide);
|
|
307
|
+
return {
|
|
308
|
+
joint: id,
|
|
309
|
+
terms: [...out.filter((x) => !fr.drop.includes(x.term)), ...fr.add],
|
|
310
|
+
geared: t.stages.length > 0,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
133
313
|
return { joint: id, terms: out, geared: t.stages.length > 0 };
|
|
134
314
|
});
|
|
135
315
|
}
|
|
@@ -142,23 +322,53 @@ export function terms(s, c) {
|
|
|
142
322
|
* does not reach them. @returns {{ add: object[], drop: string[] }} terms to add, and the guide's own to drop
|
|
143
323
|
*/
|
|
144
324
|
export function frameTerms(s, g) {
|
|
145
|
-
const none = { add: [], drop: [] },
|
|
325
|
+
const none = { add: [], drop: [] },
|
|
326
|
+
rec = (s.guides ?? []).find((x) => x.id === g.id);
|
|
146
327
|
if (!rec?.beam || ['rods', 'screw'].includes(rec.kind)) return none;
|
|
147
|
-
const o = s.objects.find((x) => x.id === rec.beam && x.beam),
|
|
328
|
+
const o = s.objects.find((x) => x.id === rec.beam && x.beam),
|
|
329
|
+
p = o?.beam?.profile,
|
|
330
|
+
L = rec.length;
|
|
148
331
|
if (!p || !(L > 0)) return none;
|
|
149
|
-
const add = [],
|
|
332
|
+
const add = [],
|
|
333
|
+
drop = [],
|
|
334
|
+
have = new Set((g.terms ?? []).map((x) => x.term)),
|
|
335
|
+
solved = s.frameGuideSag?.get(g.id);
|
|
150
336
|
if (solved) {
|
|
151
337
|
drop.push('beam sag');
|
|
152
|
-
add.push({
|
|
338
|
+
add.push({
|
|
339
|
+
term: 'beam sag',
|
|
340
|
+
value: solved.sag,
|
|
341
|
+
kind: solved.restsOn === 'beams' ? 'derived' : solved.restsOn,
|
|
342
|
+
note: `${rec.beam} under ${g.id}, solved with the frame${solved.pose ? `, ${solved.pose}` : ''}: ${n3(solved.rigid, 3)} mm with its joins rigid`,
|
|
343
|
+
});
|
|
153
344
|
}
|
|
154
345
|
const st = p.supply?.straightness;
|
|
155
|
-
if (st != null && !have.has('beam straightness'))
|
|
156
|
-
|
|
346
|
+
if (st != null && !have.has('beam straightness'))
|
|
347
|
+
add.push({
|
|
348
|
+
term: 'beam straightness',
|
|
349
|
+
value: +((st * L) / 1000).toFixed(4),
|
|
350
|
+
kind: p.figures?.straightness_per_metre?.kind ?? 'spec',
|
|
351
|
+
note: `${p.name}, ${n3(st)} mm a metre over the guide's ${n3(L, 1)} mm`,
|
|
352
|
+
});
|
|
353
|
+
const tw = p.supply?.twist,
|
|
354
|
+
W = s.task?.point && s.motion ? worldOf(s, rec.beam) : null,
|
|
355
|
+
T = W ? worldOf(s, s.task.item) : null;
|
|
157
356
|
if (tw != null && W && T) {
|
|
158
|
-
const q = s.task.point,
|
|
159
|
-
|
|
357
|
+
const q = s.task.point,
|
|
358
|
+
P = [0, 1, 2].map((r) => T[4 * r] * q[0] + T[4 * r + 1] * q[1] + T[4 * r + 2] * q[2] + T[4 * r + 3]); // (the tool, in the world, as drawn)
|
|
359
|
+
const O = [W[3], W[7], W[11]],
|
|
360
|
+
d = [W[2], W[6], W[10]],
|
|
361
|
+
v = P.map((x, k) => x - O[k]),
|
|
362
|
+
along = dot(v, d),
|
|
363
|
+
lever = Math.hypot(...v.map((x, k) => x - along * d[k]));
|
|
160
364
|
const deg = (tw * L) / 1000;
|
|
161
|
-
if (lever > 1e-6)
|
|
365
|
+
if (lever > 1e-6)
|
|
366
|
+
add.push({
|
|
367
|
+
term: 'beam twist',
|
|
368
|
+
value: +(((deg * Math.PI) / 180) * lever).toFixed(4),
|
|
369
|
+
kind: p.figures?.twist_per_metre?.kind ?? 'spec',
|
|
370
|
+
note: `${p.name}, ${n3(tw)}° a metre over the guide's ${n3(L, 1)} mm, ${n3(deg, 3)}°, at the tool ${n3(lever, 1)} mm from ${rec.beam}'s axis`,
|
|
371
|
+
});
|
|
162
372
|
}
|
|
163
373
|
return { add, drop };
|
|
164
374
|
}
|
|
@@ -166,41 +376,81 @@ export function frameTerms(s, g) {
|
|
|
166
376
|
function worldOf(s, id) {
|
|
167
377
|
const i = s.motion.items.findIndex((x) => x[0] === id);
|
|
168
378
|
if (i < 0) return null;
|
|
169
|
-
try {
|
|
379
|
+
try {
|
|
380
|
+
return placeItems(s.motion, s.jointValues)[i];
|
|
381
|
+
} catch {
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
170
384
|
}
|
|
171
385
|
function kindOf(expr) {
|
|
172
|
-
const kinds = [...String(expr).matchAll(/([a-z][\w-]*)\.([a-z_]\w*)/gi)].map(([, set, rule]) => {
|
|
386
|
+
const kinds = [...String(expr).matchAll(/([a-z][\w-]*)\.([a-z_]\w*)/gi)].map(([, set, rule]) => {
|
|
387
|
+
try {
|
|
388
|
+
return peekRuleSet(set).rules[rule]?.kind ?? 'estimate';
|
|
389
|
+
} catch {
|
|
390
|
+
return 'estimate';
|
|
391
|
+
}
|
|
392
|
+
});
|
|
173
393
|
return kinds.length ? (kinds.includes('estimate') ? 'estimate' : kinds[0]) : 'estimate';
|
|
174
394
|
}
|
|
175
395
|
|
|
176
396
|
// The error at one sample: each angle times what it swings at the tool. Worst case, root-sum-square, and each term's share
|
|
177
397
|
function errorAt(c, q, list, extra) {
|
|
178
|
-
const J = jacobian(c, q),
|
|
179
|
-
|
|
398
|
+
const J = jacobian(c, q),
|
|
399
|
+
lever = c.names.map((_, i) => Math.hypot(J[0][i], J[1][i], J[2][i])),
|
|
400
|
+
parts = [];
|
|
401
|
+
list.forEach((j, i) => {
|
|
402
|
+
for (const x of j.terms)
|
|
403
|
+
parts.push({ joint: j.joint, term: x.term, kind: x.kind, note: x.note, angle: x.value, mm: x.value * lever[i] });
|
|
404
|
+
});
|
|
180
405
|
if (extra) parts.push({ joint: null, term: 'link deflection', kind: 'estimate', mm: extra });
|
|
181
|
-
return {
|
|
406
|
+
return {
|
|
407
|
+
worst: parts.reduce((t, p) => t + p.mm, 0),
|
|
408
|
+
rss: Math.sqrt(parts.reduce((t, p) => t + p.mm * p.mm, 0)),
|
|
409
|
+
parts,
|
|
410
|
+
};
|
|
182
411
|
}
|
|
183
412
|
|
|
184
413
|
// The worst-case error at one joint configuration
|
|
185
414
|
export const jacobianError = (c, q, list, extra) => errorAt(c, q, list, extra).worst;
|
|
186
415
|
|
|
187
416
|
export function accuracyOf(s) {
|
|
188
|
-
const { c, r } = sample(s),
|
|
417
|
+
const { c, r } = sample(s),
|
|
418
|
+
list = terms(s, c),
|
|
419
|
+
at = r.samples.filter((x) => x.reachable).map((x) => ({ x, e: errorAt(c, x.q, list, s.task.deflection) }));
|
|
189
420
|
return { list, at, worst: at.reduce((a, b) => (b.e.worst > a.e.worst ? b : a), at[0]) };
|
|
190
421
|
}
|
|
191
422
|
|
|
192
423
|
function accuracyCheck(s, f, picture) {
|
|
193
|
-
const t = s.task,
|
|
194
|
-
|
|
195
|
-
|
|
424
|
+
const t = s.task,
|
|
425
|
+
code = 'task.accuracy';
|
|
426
|
+
if (t.accuracy == null) return null;
|
|
427
|
+
const { list, at, worst } = accuracyOf(s);
|
|
428
|
+
if (!worst) return null;
|
|
429
|
+
const big = [...worst.e.parts].sort((a, b) => b.mm - a.mm)[0],
|
|
430
|
+
soft = worst.e.parts.filter((p) => p.kind === 'estimate');
|
|
196
431
|
const slides = new Set(s.motion.joints.filter((j) => j.type === 'prismatic').map((j) => j.id)); // (a sliding joint's terms are mm already)
|
|
197
|
-
const name = (p) =>
|
|
198
|
-
|
|
432
|
+
const name = (p) =>
|
|
433
|
+
`${p.joint ? `${p.joint}'s ` : ''}${p.term}${p.angle != null && !slides.has(p.joint) ? ` (${n3(p.angle, 3)}°)` : ''}`;
|
|
434
|
+
const unmeasured = list
|
|
435
|
+
.filter((j) => j.geared && !j.terms.some((x) => /play|backlash/.test(x.term)))
|
|
436
|
+
.map((j) => j.joint);
|
|
199
437
|
const predict = { name: code, figure: `${t.tool} error at its worst point`, value: worst.e.worst, unit: 'mm' };
|
|
200
|
-
const margin = worst.e.worst / t.accuracy,
|
|
201
|
-
|
|
438
|
+
const margin = worst.e.worst / t.accuracy,
|
|
439
|
+
level = margin > 1 ? 'fail' : margin > 0.8 ? 'warn' : 'pass';
|
|
440
|
+
const caveat = soft.length
|
|
441
|
+
? ` Rests on estimates (${soft.map(name).join(', ')}): settle them with cad measured.`
|
|
442
|
+
: '',
|
|
443
|
+
missing = unmeasured.length
|
|
444
|
+
? ` ${unmeasured.join(' and ')} ${unmeasured.length > 1 ? 'have' : 'has'} gears and no play given: it counts as none until measured (bench test 14).`
|
|
445
|
+
: '';
|
|
202
446
|
const text = `${t.tool} can be out by ${n3(worst.e.worst, 3)} mm at worst, ${n3(worst.e.rss, 3)} mm root-sum-square, at ${onSheet(t, worst.x.point)}, against ${t.accuracy} mm. Biggest: ${name(big)}, ${n3(big.mm, 3)} mm.${caveat}${missing}`;
|
|
203
|
-
f(level, text, level === 'pass' ? null : `pen error ${n3(worst.e.worst, 2)} mm`, {
|
|
447
|
+
f(level, text, level === 'pass' ? null : `pen error ${n3(worst.e.worst, 2)} mm`, {
|
|
448
|
+
code,
|
|
449
|
+
at: worst.x.point,
|
|
450
|
+
predict,
|
|
451
|
+
refs: { biggest: big.term, joint: big.joint },
|
|
452
|
+
values: { worst: worst.e.worst, rss: worst.e.rss, wanted: t.accuracy, margin, terms: worst.e.parts, picture },
|
|
453
|
+
});
|
|
204
454
|
return at;
|
|
205
455
|
}
|
|
206
456
|
|
|
@@ -208,20 +458,61 @@ function accuracyCheck(s, f, picture) {
|
|
|
208
458
|
const colour = (x) => `rgb(${Math.round(240 - 180 * x)},${Math.round(240 - 100 * x)},${Math.round(250 - 40 * x)})`;
|
|
209
459
|
function pictures(s, errors) {
|
|
210
460
|
if (!s.task.sheet) return null;
|
|
211
|
-
const { c, r } = sample(s),
|
|
212
|
-
|
|
213
|
-
|
|
461
|
+
const { c, r } = sample(s),
|
|
462
|
+
f = sheetFrame(s.task.sheet),
|
|
463
|
+
toSheet = (p) => {
|
|
464
|
+
const d = p.map((x, k) => x - f.at[k]);
|
|
465
|
+
return [dot(d, f.u), -dot(d, f.v)];
|
|
466
|
+
};
|
|
467
|
+
const pts = [...r.samples.map((x) => toSheet(x.point)), ...fk(c, r.summary.worst.q).frames.map((x) => toSheet(x.at))],
|
|
468
|
+
lo = [0, 1].map((k) => Math.min(...pts.map((p) => p[k]))),
|
|
469
|
+
hi = [0, 1].map((k) => Math.max(...pts.map((p) => p[k])));
|
|
470
|
+
const m = 15,
|
|
471
|
+
W = hi[0] - lo[0] + 2 * m,
|
|
472
|
+
H = hi[1] - lo[1] + 2 * m,
|
|
473
|
+
step = sample(s).step;
|
|
214
474
|
const head = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${lo[0] - m} ${lo[1] - m} ${W} ${H}" width="${Math.round(W * 3)}" height="${Math.round(H * 3)}" font-family="sans-serif" font-size="5">`;
|
|
215
475
|
const sheet = `<rect x="${-f.width / 2}" y="${-f.height / 2}" width="${f.width}" height="${f.height}" fill="none" stroke="#333" stroke-width="0.6"/>`;
|
|
216
|
-
const arm = (q) => {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
476
|
+
const arm = (q) => {
|
|
477
|
+
const F = fk(c, q),
|
|
478
|
+
P = [...F.frames.map((x) => x.at), F.position].map(toSheet);
|
|
479
|
+
return (
|
|
480
|
+
`<polyline points="${P.map((p) => p.join(',')).join(' ')}" fill="none" stroke="#c33" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>` +
|
|
481
|
+
P.map((p) => `<circle cx="${p[0]}" cy="${p[1]}" r="2" fill="#c33"/>`).join('')
|
|
482
|
+
);
|
|
483
|
+
};
|
|
484
|
+
const cells = (val) =>
|
|
485
|
+
r.samples
|
|
486
|
+
.map((x) => {
|
|
487
|
+
const [px, py] = toSheet(x.point),
|
|
488
|
+
v = val(x);
|
|
489
|
+
return `<rect x="${px - step / 2}" y="${py - step / 2}" width="${step}" height="${step}" fill="${v == null ? '#f8c0c0' : colour(v)}"/>`;
|
|
490
|
+
})
|
|
491
|
+
.join('');
|
|
492
|
+
const dir = paths.preview(s.project, '');
|
|
493
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
494
|
+
const write = (name, body, title) => {
|
|
495
|
+
const file = paths.preview(s.project, `${s.name}-${name}.svg`);
|
|
496
|
+
fs.writeFileSync(file, `${head}<text x="${lo[0] - m + 2}" y="${lo[1] - m + 7}">${title}</text>${body}</svg>\n`);
|
|
497
|
+
return file;
|
|
498
|
+
};
|
|
220
499
|
const top = Math.max(...r.samples.map((x) => x.manipulability), 1e-9);
|
|
221
|
-
const out = {
|
|
500
|
+
const out = {
|
|
501
|
+
reach: write(
|
|
502
|
+
'reach',
|
|
503
|
+
cells((x) => (x.reachable ? x.manipulability / top : null)) + sheet + arm(r.summary.worst.q),
|
|
504
|
+
'Reach: darker has more dexterity, red is out of reach. The arm at the least margin',
|
|
505
|
+
),
|
|
506
|
+
};
|
|
222
507
|
if (errors?.length) {
|
|
223
|
-
const most = Math.max(...errors.map((e) => e.e.worst), 1e-9),
|
|
224
|
-
|
|
508
|
+
const most = Math.max(...errors.map((e) => e.e.worst), 1e-9),
|
|
509
|
+
by = new Map(errors.map((e) => [e.x, e.e.worst / most])),
|
|
510
|
+
worst = errors.reduce((a, b) => (b.e.worst > a.e.worst ? b : a));
|
|
511
|
+
out.error = write(
|
|
512
|
+
'error',
|
|
513
|
+
cells((x) => (by.has(x) ? by.get(x) : null)) + sheet + arm(worst.x.q),
|
|
514
|
+
`Predicted error: darkest is ${n3(most, 3)} mm. The arm at its worst point`,
|
|
515
|
+
);
|
|
225
516
|
}
|
|
226
517
|
return out;
|
|
227
518
|
}
|
|
@@ -231,7 +522,13 @@ export function taskChecks(s, f) {
|
|
|
231
522
|
if (!s.task) return;
|
|
232
523
|
const relative = (x) => x && x.replace(`${paths.preview(s.project, '')}/`, '');
|
|
233
524
|
let drawn = null;
|
|
234
|
-
try {
|
|
525
|
+
try {
|
|
526
|
+
const { c } = sample(s);
|
|
527
|
+
if (!c.dof) return;
|
|
528
|
+
drawn = true;
|
|
529
|
+
} catch (e) {
|
|
530
|
+
return f('fail', `task: ${e.message}`, 'task: no chain', { code: 'task.chain' });
|
|
531
|
+
}
|
|
235
532
|
const pic = pictures(s, s.task.accuracy != null ? accuracyOf(s).at : null);
|
|
236
533
|
reachCheck(s, f, relative(pic?.reach));
|
|
237
534
|
speedCheck(s, f);
|