@rulecad/cad 0.7.55 → 0.7.57

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/lib/guides.mjs ADDED
@@ -0,0 +1,354 @@
1
+ // Guides in assemblies (@rulecad/frames' guide.mjs): linear motion on a beam.
2
+ // "guides": [{ "kind": "rail", "rail": "rail-mgn12", "carriage": "MGN12H", "on": "x1.top", "from": 20, "length": 300, "carriages": 1 }]
3
+ // "guides": [{ "kind": "wheels", "wheel": "v-wheel-solid", "wheels": 4, "on": "x1.top", "from": 0, "length": 400 }]
4
+ // "guides": [{ "kind": "slider", "mode": "over", "size": 30, "on": "x1.top.1", "from": 0, "length": 200 }]
5
+ // Before the items are built, each guide is worked out: the rail is a bought item at its place on the face, its
6
+ // screws a fasten into the slot at its hole pitch (so the fasten checks choose their length and count them, and the
7
+ // slot nuts with them), each carriage a bought item on a sliding joint (<id>-1, <id>-2 ..) whose travel is the rail's
8
+ // share less the carriage's length, and two poses put them at either end (<id>-a, <id>-b). Once built, guide.fit says
9
+ // whether the rail fits the face, each carriage's loads are held to its ratings (joint.moment), and the rail's terms go
10
+ // into the error budget of any task the carriage's joint is in. A wheels guide's carriage is a printed plate (the
11
+ // fitting wheel-plate) on its sliding joint over the face "on" names, its wheels bought items held to it, rolling in the
12
+ // V tracks either side over the stretch from..from + length; wheels on a face with no V track are refused with why. A
13
+ // slider is a printed part (the fitting slider) on its sliding joint along the slot "on" names. Units: mm, N, N m, kg.
14
+ import jscad from '@jscad/modeling';
15
+ import { railModel, rail, carriage, carriageMargin, guideFit, railTerms, railJigModel, GUIDE_DEFAULTS, vWheel, wheelMargin, rodsLayout, rodMargin, ROD_DEFAULTS } from '@rulecad/frames';
16
+ import { beamOf, framesContext } from './beams.mjs';
17
+ import { givenFor } from './frames.mjs';
18
+ import { named } from './parts.mjs';
19
+ import { referenceBuilt } from './bought.mjs';
20
+ import { fittingModel } from './fittings.mjs';
21
+
22
+ const KEYS = { rail: ['id', 'kind', 'rail', 'carriage', 'on', 'from', 'length', 'carriages', 'nut', 'note'], wheels: ['id', 'kind', 'wheel', 'wheels', 'pitch', 'load', 'on', 'from', 'length', 'note'], slider: ['id', 'kind', 'mode', 'size', 'load', 'on', 'from', 'length', 'note'],
23
+ rods: ['id', 'kind', 'rod', 'bearing', 'spacing', 'height', 'load', 'on', 'from', 'length', 'endstop', 'nut', 'note'], screw: ['id', 'kind', 'guide', 'screw', 'nut', 'support', 'coupler', 'motor', 'friction', 'note'] };
24
+ const { cylinder } = jscad.primitives, { translate } = jscad.transforms, { subtract, union } = jscad.booleans;
25
+ const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
26
+ const mm = (x) => String(+Number(x).toFixed(2)), r3 = (x) => +x.toFixed(3), n3 = (x, d = 2) => String(Number(Number(x).toFixed(d)));
27
+ const strip = (e) => String(e?.message ?? e).replace(/^(railModel|guideFit|railTerms|railJigModel|wheelsModel|sliderModel|rodsLayout): /, '');
28
+ const G = 9.80665;
29
+ const SETS_OF = (m) => ({ diameter: r3(2 * (m.seat.axle + m.seat.depth)) });
30
+ const SETS_KIND = (L) => `${L.bearing.set}'s ratings as its set gives them: a single bush's moment capacity is worked as a couple along it, an estimate`;
31
+
32
+ /**
33
+ * An assembly's guides worked out before its items are built: each rail an item on its beam's face with its screws as a
34
+ * fasten, each carriage an item on its own sliding joint with its true travel, and two poses. Refused by name: a guide
35
+ * of a kind not built yet, a face that is not a beam's slotted face, and what @rulecad/frames refuses.
36
+ * @param {object} a the assembly as read @param {(x: any) => number} num @param {object} [context] framesContext()'s
37
+ * @returns {{ a: object, guides: object[] }}
38
+ */
39
+ export function expandGuides(a, num, context = framesContext(), material = 'pla') {
40
+ const list = a.guides;
41
+ if (list == null) return { a, guides: [] };
42
+ if (!Array.isArray(list)) throw new Error(`guides is a list: [{ "kind": "rail", "rail": "rail-mgn12", "on": "x1.top", "length": 300 }] (got ${shown(list)})`);
43
+ const items = a.items || [], beams = new Map(items.filter((it) => it.beam).map((it) => [it.id, it])), ids = new Set(items.map((it) => it.id)), given = { sets: context?.sets ?? {} };
44
+ const added = [], fasten = [], joints = [], poses = {}, guides = [], first = (a.fasten || []).length;
45
+ // a carriage on V-wheels or a printed slider: a printed fitting on its own sliding joint along the beam, through the
46
+ // stretch from..from + length less its own length; a plate's wheels bought items held to it at its anchors
47
+ const rolling = (g, { what, id, bid, B, q, faceName }) => {
48
+ const wheels = g.kind === 'wheels', face = wheels ? q.face : B.places.filter((x) => x.face === q.face).length > 1 ? q.name : q.face;
49
+ if (wheels && faceName.includes('.')) throw new Error(`${what}: on is the face the plate lies over, as ${bid}.${q.face}: its wheels run in the slots either side of it`);
50
+ if (!wheels && !q.slot) throw new Error(`${what}: ${bid}'s ${faceName} face is closed: a slider runs in a slot or keys into one`);
51
+ const spec = wheels ? { kind: 'wheel-plate', profile: B.profile.name, face, ...(g.wheel != null ? { wheel: g.wheel } : {}), ...(g.wheels != null ? { wheels: g.wheels } : {}), ...(g.pitch != null ? { pitch: num(g.pitch) } : {}), ...(g.load != null ? { load: num(g.load) } : {}) }
52
+ : { kind: 'slider', profile: B.profile.name, face, ...(g.mode != null ? { mode: g.mode } : {}), ...(g.size != null ? { length: num(g.size) } : {}), ...(g.load != null ? { load: num(g.load) } : {}) };
53
+ const m = fittingModel(spec, context, material, what), from = g.from != null ? num(g.from) : 0, length = num(g.length), own = wheels ? m.length : m.length, travel = r3(length - own);
54
+ if (!(from >= 0 && from + length <= B.length + 1e-9)) throw new Error(`${what}: its stretch runs from ${mm(from)} to ${mm(from + length)} mm along ${bid}, which is ${mm(B.length)} mm long: move "from" or make it shorter`);
55
+ if (!(travel > 0)) throw new Error(`${what}: its ${wheels ? 'carriage' : 'slider'} is ${mm(own)} mm long${wheels ? ' over its wheels' : ''}, so a ${mm(length)} mm stretch leaves it no travel: make length more than ${mm(own)} mm`);
56
+ const cid = `${id}-carriage-1`, jid = `${id}-1`;
57
+ for (const x of [cid, jid]) if (ids.has(x)) throw new Error(`${what}: ${x} is an item already: give the guide another id`);
58
+ ids.add(cid);
59
+ const P = B.profile, n = q.normal, centre = wheels ? [r3((n[0] * P.width) / 2), r3((n[1] * P.height) / 2)] : q.at;
60
+ added.push({ id: cid, fitting: spec, guideOf: id });
61
+ joints.push({ id: jid, type: 'prismatic', parent: { item: bid, at: [centre[0], centre[1], r3(from + own / 2)], axis: [0, 0, 1], ref: [n[0], n[1], 0] }, child: `${cid}.slide`, min: 0, max: travel, label: `${cid} ${wheels ? 'rolls' : 'slides'} along ${bid}`, _guide: { id, kind: g.kind, carriage: cid, model: m, terms: [] } });
62
+ const record = { id, kind: g.kind, beam: bid, face, from, length, model: m, carriages: [cid], joints: [jid], travel, wheels: [], note: g.note ?? null, fasten: [] };
63
+ if (wheels) m.wheels.forEach((w, k) => {
64
+ const wid = `${id}-wheel-${k + 1}`;
65
+ if (ids.has(wid)) throw new Error(`${what}: ${wid} is an item already: give the guide another id`);
66
+ ids.add(wid); record.wheels.push(wid);
67
+ added.push({ id: wid, bought: m.wheel, _guide: { part: 'wheel', model: m, eccentric: w.eccentric }, at: `${cid}.${w.name}`, guideOf: id });
68
+ });
69
+ poses[`${id}-a`] = { [jid]: 0 }; poses[`${id}-b`] = { [jid]: travel };
70
+ guides.push(record);
71
+ };
72
+ // a lead screw drives a rods guide's carriage: found first, so the rods guide's parts are drawn for it
73
+ const fits = [], screws = new Map();
74
+ list.forEach((g, i) => {
75
+ if (g?.kind !== 'screw') return;
76
+ const what = `guide ${g.id ?? i + 1}`;
77
+ for (const k of Object.keys(g)) if (!KEYS.screw.includes(k)) throw new Error(`${what}: a screw guide has no "${k}": it takes ${KEYS.screw.join(', ')}`);
78
+ const target = list.find((x) => x && (x.id ?? `g${list.indexOf(x) + 1}`) === g.guide);
79
+ if (!target) throw new Error(`${what}: guide names the rods guide whose carriage the screw drives, by its id (got ${shown(g.guide)}; guides: ${list.map((x, k) => x?.id ?? `g${k + 1}`).join(', ')})`);
80
+ if (target.kind !== 'rods') throw new Error(`${what}: a screw drives a rods guide's carriage, and ${g.guide} is a ${target.kind} guide; a belt and screws on rails or wheels are not built yet`);
81
+ if (screws.has(g.guide)) throw new Error(`${what}: ${g.guide} is driven by another screw already: one drive to a carriage`);
82
+ if (!g.motor || typeof g.motor !== 'object' || typeof g.motor.model !== 'string') throw new Error(`${what}: motor is the motor item's spec, as { "model": "28byj48", "microsteps": 1 }: its drives and leadscrew come from the guide (got ${shown(g.motor)})`);
83
+ for (const k of ['drives', 'leadscrew', 'belt']) if (g.motor[k] != null) throw new Error(`${what}: motor.${k} comes from the guide: leave it out`);
84
+ screws.set(g.guide, { g, i, id: g.id ?? `g${i + 1}`, what });
85
+ });
86
+ // two rods along a face in printed holders at the stretch's ends, a printed carriage on linear bearings on its own
87
+ // sliding joint; with a screw, a motor plate, the motor, a coupler and the screw, bearings for it in the holders and the
88
+ // lead nut on the carriage; with an endstop, its mount at the address given and a flag on the carriage
89
+ const rodsGuide = (g, { what, id, bid, B, q, faceName }) => {
90
+ if (faceName.includes('.')) throw new Error(`${what}: on is the face the rods run over, as ${bid}.${q.face}: its holders' legs screw into the slots either side`);
91
+ const P = B.profile, gv = givenFor(context, P.name), sc = screws.get(id), sg = sc?.g;
92
+ const from = g.from != null ? num(g.from) : 0, length = num(g.length), opt = (k, x) => (x != null ? { [k]: typeof x === 'string' ? x : num(x) } : {});
93
+ let L;
94
+ try { L = rodsLayout(P.name, { face: q.face, ...opt('rod', g.rod), ...opt('bearing', g.bearing), ...opt('spacing', g.spacing), ...opt('height', g.height), ...(sg ? { screw: sg.screw ?? 't8-screw', ...opt('nut', sg.nut), ...(sg.support !== undefined ? { support: sg.support } : {}), ...opt('coupler', sg.coupler), motor: sg.motor.model } : {}), given: gv }); }
95
+ catch (e) { throw new Error(`${(sg && /screw|nut|support|coupler|motor/.test(String(e.message)) ? sc.what : what)}: ${strip(e)}`); }
96
+ if (!(from >= 0 && from + length <= B.length + 1e-9)) throw new Error(`${what}: its stretch runs from ${mm(from)} to ${mm(from + length)} mm along ${bid}, which is ${mm(B.length)} mm long: move "from" or make it shorter`);
97
+ const base = { profile: P.name, face: q.face, rod: L.rod.set, bearing: L.bearing.set, spacing: L.spacing, height: L.height, ...(g.load != null ? { load: num(g.load) } : {}) };
98
+ const drive = L.screw ? { screw: L.screw.set, leadNut: L.nut.set, ...(L.support ? { support: L.support.set } : { support: null }) } : {};
99
+ const slotNut = g.nut != null ? { nut: String(g.nut) } : {}; // (the slot nuts' kind for whatever screws into the slots either side: holders, motor plate, endstop mount)
100
+ const hold = (end) => fittingModel({ kind: 'rod-holder', ...base, ...drive, ...slotNut, end }, context, material, what);
101
+ const hA = hold('a'), ids_ = (x) => { if (ids.has(x)) throw new Error(`${what}: ${x} is an item already: give the guide another id`); ids.add(x); return x; };
102
+ const ha = ids_(`${id}-holder-a`), hb = ids_(`${id}-holder-b`), cid = ids_(`${id}-carriage-1`), jid = `${id}-1`, gap = ROD_DEFAULTS.gap.value;
103
+ const n = q.normal, yd = [n[1], -n[0]], F = L.frame, at = (y, z) => [r3(q.at[0] + yd[0] * y + n[0] * z), r3(q.at[1] + yd[1] * y + n[1] * z)];
104
+ // the endstop: its mount at the address given, on a face beside; the flag on the carriage where its lever is
105
+ let es = null, flag = null;
106
+ if (g.endstop != null) {
107
+ const e = g.endstop;
108
+ if (!e || typeof e !== 'object' || typeof e.at !== 'string') throw new Error(`${what}: endstop is { "at": "${bid}.${F.sides[0].name}@40", "switch": "endstop-10t85" }: the switch's mount at a side face's address (got ${shown(e)})`);
109
+ for (const k of Object.keys(e)) if (!['at', 'switch'].includes(k)) throw new Error(`${what}: an endstop has no "${k}": it takes at and switch`);
110
+ const [place, dist] = e.at.split('@'), [ebid, ...ef] = place.split('.'), side = F.sides.find((x) => x.name === ef.join('.') || x.side === ef.join('.'));
111
+ if (ebid !== bid || !side || dist == null) throw new Error(`${what}: endstop.at is an address on a face of ${bid} beside the rods, ${F.sides.map((x) => `${bid}.${x.name}@<mm>`).join(' or ')} (got ${shown(e.at)})`);
112
+ const sq = B.places.find((x) => x.name === side.name || (x.face === side.side && x.index === 1)), sy = [sq.normal[1], -sq.normal[0]], toward = Math.sign(sy[0] * n[0] + sy[1] * n[1]) || 1;
113
+ const spec = { kind: 'endstop-mount', profile: P.name, face: side.name, toward, ...(e.switch ? { switch: e.switch } : {}), ...slotNut }, em = fittingModel(spec, context, material, `${what}: endstop`), d = num(dist.replace(/%$/, '')) * (dist.endsWith('%') ? B.length / 100 : 1), dd = dist.startsWith('-') ? B.length + d : d;
114
+ const fd = ROD_DEFAULTS.flag.value;
115
+ flag = { y: r3(side.y + side.out * em.lever.out), z: r3(side.z + em.lever.reach - fd.engage) };
116
+ es = { spec, model: em, at: e.at, distance: r3(dd), side: side.name, mount: ids_(`${id}-endstop-mount`), sw: ids_(`${id}-endstop`), set: em.switch };
117
+ }
118
+ const cm = fittingModel({ kind: 'rod-carriage', ...base, ...(L.screw ? { screw: L.screw.set, leadNut: L.nut.set } : {}), ...(flag ? { flag } : {}) }, context, material, what);
119
+ const travel0 = r3(length - 2 * hA.thickness - 2 * gap - cm.own), c0 = from + hA.thickness + gap + cm.length / 2;
120
+ if (!(travel0 > 0)) throw new Error(`${what}: its holders (${mm(hA.thickness)} mm each) and carriage (${mm(cm.own)} mm) take ${mm(2 * hA.thickness + 2 * gap + cm.own)} mm of the ${mm(length)} mm stretch, which leaves no travel: make length longer`);
121
+ // where the flag meets the lever: the travel's lower end, and the joint's home
122
+ let lo = 0;
123
+ if (es) {
124
+ const lever = es.distance + es.model.lever.x, face0 = c0 + cm.length / 2 - ROD_DEFAULTS.flag.value.thickness, home = r3(lever - face0);
125
+ const ok = es.distance + es.model.outline[0][0] >= from + hA.legs + gap - 1e-6;
126
+ if (home < 0 || home >= travel0 || !ok) {
127
+ const best = r3(face0 - es.model.lever.x), least = r3(from + hA.legs + gap - es.model.outline[0][0]);
128
+ const place = Math.max(best, least);
129
+ throw new Error(`${what}: endstop at ${es.at}: ${!ok ? `its mount reaches back over ${ha}'s leg (to ${mm(from + hA.legs)} mm along ${bid})` : home < 0 ? `the carriage stops at ${ha} ${mm(-home)} mm before its flag reaches the switch's lever` : `the flag only reaches the lever ${mm(home)} mm along a ${mm(travel0)} mm travel`}: at ${bid}.${es.side}@${mm(place)} it trips ${mm(r3(place - best))} mm from the travel's start`);
130
+ }
131
+ lo = home; es.home = home;
132
+ }
133
+ added.push({ id: ha, fitting: { kind: 'rod-holder', ...base, ...drive, ...slotNut, end: 'a' }, at: `${bid}.${q.name}@${r3(from)}`, guideOf: id });
134
+ added.push({ id: hb, fitting: { kind: 'rod-holder', ...base, ...drive, ...slotNut, end: 'b' }, at: `${bid}.${q.name}@${r3(from + length)}`, guideOf: id });
135
+ const rods = [1, 2].map((k) => { const rid = ids_(`${id}-rod-${k}`); added.push({ id: rid, bought: L.rod.set, set: { length: r3(length) }, at: `${ha}.r${k}`, guideOf: id }); return rid; });
136
+ added.push({ id: cid, fitting: { kind: 'rod-carriage', ...base, ...(L.screw ? { screw: L.screw.set, leadNut: L.nut.set } : {}), ...(flag ? { flag } : {}) }, guideOf: id });
137
+ const lms = [1, 2].map((k) => { const lid = ids_(`${id}-bearing-${k}`); added.push({ id: lid, bought: L.bearing.set, at: `${cid}.lm${k}`, guideOf: id }); return lid; });
138
+ const centre = at(F.yc, L.height), jspec = { id: jid, type: 'prismatic', parent: { item: bid, at: [centre[0], centre[1], r3(c0)], axis: [0, 0, 1], ref: [n[0], n[1], 0] }, child: `${cid}.slide`, min: lo, max: travel0, label: `${cid} slides along ${bid} on ${L.rod.set} rods`, _guide: { id, kind: 'rods', carriage: cid, model: cm, terms: [] } };
139
+ joints.push(jspec);
140
+ // the rods pressed into the holders, run through the bearings; the bearings pressed into the carriage
141
+ for (const r of rods) fits.push({ a: r, b: ha, fastens: true, min: -100, note: 'The rod pressed through its holder' }, { a: r, b: hb, fastens: true, min: -100, note: 'The rod pressed through its holder' }, { a: r, b: cid, min: -100, note: 'The rod runs through the carriage\'s bearing' });
142
+ lms.forEach((x, k) => fits.push({ a: x, b: cid, fastens: true, min: -100, note: 'The bearing pressed into its pocket' }, { a: rods[k], b: x, min: -100, note: 'Rod in bearing' }));
143
+ const record = { id, kind: 'rods', beam: bid, face: q.face, from, length, model: cm, layout: L, holder: hA, carriages: [cid], joints: [jid], travel: r3(travel0 - lo), lo, wheels: [], rods, bearings: lms, note: g.note ?? null, fasten: [] };
144
+ // the holders' legs into the slots either side
145
+ // (a saddle's screws, one fasten a leg: each leg's screws into its side's slot)
146
+ const legFastens = (hid, m, said, gid) => [...new Set(m.screws.map((x) => x.side))].forEach((sd) => {
147
+ const list = m.screws.map((x, k) => [x, k]).filter(([x]) => x.side === sd), x = list[0][0];
148
+ fasten.push({ screw: `${x.thread} ${x.head}`, through: [hid], into: `${bid}.${sd}`, at: list.map(([, k]) => `${hid}.b${k + 1}`), nut: x.nutKind, note: `${said} into ${bid}'s ${sd} slot`, guideOf: gid });
149
+ });
150
+ for (const [hid, m] of [[ha, hA], [hb, hold('b')]]) legFastens(hid, m, `${id}: ${hid}'s leg`, id);
151
+ if (sc) {
152
+ // the motor plate outside holder a, the coupler between, the screw from the coupler to holder b's bearing
153
+ const pspec = { kind: 'motor-plate', profile: P.name, face: q.face, motor: L.motor.set, height: L.height, coupler: { length: L.coupler.length, D: L.coupler.D }, ...slotNut };
154
+ const pm = fittingModel(pspec, context, material, sc.what), cl = L.coupler.length, before = r3(pm.anchors.coupler.at[0] + cl + gap / 2);
155
+ const xm = r3(from - before);
156
+ if (xm < 0) throw new Error(`${sc.what}: the motor plate and ${L.coupler.set} take ${mm(before)} mm before the stretch, and it starts ${mm(from)} mm along ${bid}: give the rods guide a "from" of at least ${mm(before)}`);
157
+ const plate = ids_(`${sc.id}-plate`), mo = ids_(`${sc.id}-motor`), cp = ids_(`${sc.id}-coupler`), sw = ids_(`${sc.id}-screw`), nt = ids_(`${sc.id}-nut`);
158
+ const start = xm + pm.anchors.coupler.at[0], end = r3(from + length - hA.thickness + (L.support ? L.support.width : hA.thickness)), slen = r3(end - start - cl / 2);
159
+ const friction = sg.friction != null ? sg.friction : L.screw.friction ?? 0.2;
160
+ added.push({ id: plate, fitting: pspec, at: `${bid}.${q.name}@${xm}`, guideOf: sc.id });
161
+ added.push({ id: mo, motor: { ...sg.motor, drives: jid, leadscrew: { lead: L.screw.lead, diameter: L.screw.d, friction } }, at: `${plate}.motor`, guideOf: sc.id });
162
+ added.push({ id: cp, bought: L.coupler.set, _guide: { part: 'coupler', set: L.coupler.set, length: cl, D: L.coupler.D, bores: [L.motor.shaft.d, L.screw.d] }, at: `${plate}.coupler`, guideOf: sc.id });
163
+ added.push({ id: sw, bought: L.screw.set, _guide: { part: 'screw', set: L.screw.set, d: L.screw.d, from: cl / 2, length: slen, lead: L.screw.lead }, at: `${plate}.coupler`, guideOf: sc.id });
164
+ added.push({ id: nt, bought: L.nut.set, _guide: { part: 'nut', ...L.nut, d: L.screw.d }, at: `${cid}.nut`, guideOf: sc.id });
165
+ const sup = L.support ? ['a', 'b'].map((e) => { const x = ids_(`${sc.id}-support-${e}`); added.push({ id: x, bought: L.support.set, at: `${e === 'a' ? ha : hb}.s1`, guideOf: sc.id }); return x; }) : [];
166
+ fits.push({ a: mo, b: plate, fastens: true, min: -0.1, note: 'The motor\'s face on its plate' }, { a: mo, b: cp, min: -100, note: 'The motor\'s shaft in the coupler' }, { a: sw, b: cp, min: -100, note: 'The screw in the coupler' },
167
+ { a: sw, b: nt, min: -100, note: 'The screw through its nut' }, { a: sw, b: cid, min: -100, note: 'The screw through the carriage' }, { a: nt, b: cid, fastens: true, min: -100, note: 'The nut in its bore, its flange screwed on' },
168
+ { a: sw, b: ha, min: -100, note: 'The screw through holder a' }, { a: sw, b: hb, min: -100, note: 'The screw into holder b' }, ...sup.flatMap((x, k) => [{ a: x, b: k ? hb : ha, fastens: true, min: -100, note: 'The screw\'s bearing pressed into its holder' }, { a: sw, b: x, min: -100, note: 'The screw in its bearing' }]));
169
+ legFastens(plate, pm, `${sc.id}: the motor plate's leg`, sc.id);
170
+ jspec._guide.screw = sc.id;
171
+ guides.push({ id: sc.id, kind: 'screw', guide: id, beam: bid, plate: pm, motor: mo, items: { plate, coupler: cp, screw: sw, nut: nt, support: sup }, length: slen, lead: L.screw.lead, friction: num(friction), frictionKind: sg.friction != null ? 'given' : L.screw.frictionKind, layout: L, from: xm, carriages: [], joints: [jid], wheels: [], note: sg.note ?? null, fasten: [] });
172
+ }
173
+ if (es) {
174
+ added.push({ id: es.mount, fitting: es.spec, at: es.at, guideOf: id });
175
+ added.push({ id: es.sw, bought: es.set, at: `${es.mount}.switch`, guideOf: id });
176
+ fits.push({ a: es.sw, b: es.mount, fastens: true, min: -0.1, note: 'The switch screwed to its mount' }, { a: es.sw, b: cid, min: -1, note: 'The flag on the lever, at home' });
177
+ es.model.screws.forEach((x, k) => fasten.push({ screw: `${x.thread} ${x.head}`, through: [es.mount], into: `${bid}.${es.side}`, at: [`${es.mount}.b${k + 1}`], nut: x.nutKind, note: `${id}: the endstop's mount into ${bid}'s ${es.side} slot`, guideOf: id }));
178
+ jspec.home = { endstop: `${es.sw}.lever`, direction: -1 };
179
+ record.endstop = { mount: es.mount, switch: es.sw, at: es.at, home: es.home, model: es.model };
180
+ }
181
+ poses[`${id}-a`] = { [jid]: lo }; poses[`${id}-b`] = { [jid]: travel0 };
182
+ guides.push(record);
183
+ };
184
+ list.forEach((g, i) => {
185
+ const what = `guide ${g?.id ?? i + 1}`;
186
+ if (!g || typeof g !== 'object' || Array.isArray(g)) throw new Error(`${what} is { "kind": "rail", "rail": "rail-mgn12", "on": "x1.top", "length": 300 } (got ${shown(g)})`);
187
+ if (!KEYS[g.kind]) throw new Error(`${what}: kind is rail (a miniature linear rail), wheels (a plate on V-wheels in a V-slot's tracks), slider (a printed slider in or over a slot), rods (two rods in printed holders, a printed carriage on linear bearings) or screw (a lead screw and its motor driving a rods guide's carriage); a belt is not built yet (got ${shown(g.kind)})`);
188
+ if (g.kind === 'screw') return; // (worked out with the rods guide it drives)
189
+ for (const k of Object.keys(g)) if (!KEYS[g.kind].includes(k)) throw new Error(`${what}: a ${g.kind} guide has no "${k}": it takes ${KEYS[g.kind].join(', ')}`);
190
+ const id = g.id ?? `g${i + 1}`;
191
+ if (!/^[a-z][\w-]*$/.test(id) || ids.has(id)) throw new Error(`${what}: id ${shown(id)} is ${ids.has(id) ? 'an item already' : 'not a name (letters, digits, - or _)'}`);
192
+ ids.add(id);
193
+ const on = String(g.on ?? ''), dot = on.indexOf('.'), bid = on.slice(0, dot), faceName = on.slice(dot + 1);
194
+ if (dot < 0 || on.includes('@')) throw new Error(`${what}: on is a beam's slotted face, as x1.top; where along it is "from" (got ${shown(g.on)})`);
195
+ if (!beams.has(bid)) throw new Error(`${what}: on names a beam item of this assembly, and ${bid} is ${items.some((x) => x.id === bid) ? 'not a beam' : 'not an item'} (beams: ${[...beams.keys()].join(', ') || 'none'})`);
196
+ const B = beamOf(beams.get(bid), num, context).model, q = B.places.find((x) => x.name === faceName || (x.face === faceName && x.index === 1));
197
+ if (!q) throw new Error(`${what}: ${bid} has no face "${faceName}": it has ${[...new Set(B.places.map((x) => x.face))].join(', ')}`);
198
+ if (g.kind === 'rods') { rodsGuide(g, { what, id, bid, B, q, faceName }); return; }
199
+ if (g.kind !== 'rail') { rolling(g, { what, id, bid, B, q, faceName }); return; }
200
+ if (!q.slot) throw new Error(`${what}: ${bid}'s ${faceName} face is closed: a rail's screws go into slot nuts, so it goes on a slotted face`);
201
+ const from = g.from != null ? num(g.from) : 0, length = num(g.length), face = B.places.filter((x) => x.face === q.face).length > 1 ? q.name : q.face;
202
+ let m;
203
+ try { m = railModel(g.rail, { length, ...(g.carriage != null ? { variant: g.carriage } : {}), ...(g.carriages != null ? { carriages: g.carriages } : {}), given }); } catch (e) { throw new Error(`${what}: ${strip(e)}`); }
204
+ if (!(from >= 0 && from + length <= B.length + 1e-9)) throw new Error(`${what}: the rail runs from ${mm(from)} to ${mm(from + length)} mm along ${bid}, which is ${mm(B.length)} mm long: move "from" or cut it shorter`);
205
+ // what crosses its face: a beam joined to it there, by the join's address
206
+ const keep = [];
207
+ for (const j of a.joins ?? []) {
208
+ const [jb, jat] = String(j?.b ?? '').split('@'), [jbid, ...jf] = jb.split('.'), other = String(j?.a ?? '').split('.')[0];
209
+ if (jbid !== bid || jat == null || ![q.name, q.face].includes(jf.join('.')) || !beams.has(other)) continue;
210
+ const d = /%$/.test(jat) ? (parseFloat(jat) / 100) * B.length : parseFloat(jat) < 0 || String(jat).startsWith('-') ? B.length + parseFloat(jat) : parseFloat(jat), o = beamOf(beams.get(other), num, context).model.profile, half = Math.max(o.width, o.height) / 2;
211
+ keep.push({ from: r3(d - half), to: r3(d + half), what: `${other} joined at ${j.b}` });
212
+ }
213
+ const fit = guideFit(m, B.profile, { face, from, beam: B.length, keep, ...(g.nut ? { nut: g.nut } : {}), given: givenFor(context, B.profile.name) });
214
+ const rid = id, record = { id, beam: bid, face: q.name, from, length, model: m, fit, carriages: [], joints: [], keep, note: g.note ?? null };
215
+ added.push({ id: rid, bought: m.set, _guide: { part: 'rail', model: m }, at: `${bid}.${q.name}@${r3(from)}`, guideOf: id });
216
+ // its screws into the slot, at its holes: only where a nut is made for them (else guide.fit says why)
217
+ if (fit.nut) fasten.push({ screw: `${m.rail.thread} cap`, through: [rid], into: `${bid}.${q.name}`, at: m.screws.map((x) => `${rid}.${x.name}`), nut: g.nut ?? fit.nut.nutKind, note: `${id}: the rail into ${bid}'s ${q.name} slot, a screw every ${mm(m.rail.pitch)} mm`, guideOf: id });
218
+ // the carriages, each on its own slide along the beam, from the start of its share of the rail
219
+ const normal = [q.normal[0], q.normal[1], 0], terms = (() => { try { return railTerms(m, B.profile, { beam: B.length, face, given: givenFor(context, B.profile.name) }); } catch { return m.terms; } })();
220
+ m.lanes.forEach((ln, k) => {
221
+ const cid = `${id}-carriage-${k + 1}`, jid = `${id}-${k + 1}`;
222
+ if (ids.has(cid) || ids.has(jid)) throw new Error(`${what}: ${ids.has(cid) ? cid : jid} is an item already: give the guide another id`);
223
+ ids.add(cid);
224
+ added.push({ id: cid, bought: m.set, variant: m.variant, _guide: { part: 'carriage', model: m }, guideOf: id });
225
+ joints.push({ id: jid, type: 'prismatic', parent: { item: bid, at: [q.at[0], q.at[1], r3(from + ln.rest)], axis: [0, 0, 1], ref: normal }, child: `${cid}.slide`, min: 0, max: ln.travel, label: `${cid} slides along ${bid}`, _guide: { id, carriage: cid, model: m, terms } });
226
+ record.carriages.push(cid); record.joints.push(jid);
227
+ });
228
+ poses[`${id}-a`] = Object.fromEntries(record.joints.map((j) => [j, 0])); poses[`${id}-b`] = Object.fromEntries(record.joints.map((j) => [j, m.travel]));
229
+ record.terms = terms;
230
+ try { record.jig = railJigModel(B.profile, { rail: m.set, face, given: givenFor(context, B.profile.name) }); } catch (e) { record.jig = { error: String(e.message).replace(/^railJigModel: /, '') }; }
231
+ guides.push(record);
232
+ });
233
+ const out = { ...a, items: [...items, ...added], ...(fasten.length ? { fasten: [...(a.fasten || []), ...fasten] } : {}), ...(fits.length ? { fits: [...(a.fits || []), ...fits] } : {}), joints: [...(a.joints || []), ...joints], poses: { ...(a.poses || {}), ...poses } };
234
+ for (const r of guides) r.fasten = fasten.map((x, k) => (x.guideOf === r.id ? first + k : -1)).filter((k) => k >= 0);
235
+ return { a: out, guides };
236
+ }
237
+
238
+ /**
239
+ * A guide's rail or carriage item, built as a bought reference: the rail with its counterbored holes and an anchor on
240
+ * each screw's axis at its head's seat (h1, h2 ..); a carriage with its anchors slide (where its joint holds it), top
241
+ * and t1 to t4 (its tapped holes, from its top). @param {object} it @param {string} id
242
+ */
243
+ export function guideBuilt(it, id) {
244
+ const { part, model: m } = it._guide;
245
+ // a screw drive's bought parts, drawn from their sets' figures along their own z: the coupler, the screw, the lead nut
246
+ if (part === 'coupler' || part === 'screw' || part === 'nut') {
247
+ const g = it._guide, rod = (r, z0, z1, seg = 48) => translate([0, 0, (z0 + z1) / 2], cylinder({ radius: r, height: z1 - z0, segments: seg }));
248
+ const body = part === 'coupler' ? subtract(rod(g.D / 2, 0, g.length), rod(g.bores[0] / 2, -1, g.length / 2), rod(g.bores[1] / 2, g.length / 2, g.length + 1))
249
+ : part === 'screw' ? rod(g.d / 2, g.from, g.from + g.length) : subtract(union(rod(g.flange / 2, 0, g.flangeWidth), rod(g.body / 2, g.flangeWidth - g.length, 0.01)), rod(g.d / 2, -g.length - 1, g.flangeWidth + 1));
250
+ const label = part === 'coupler' ? `${g.set} coupler` : part === 'screw' ? `${g.set} x ${mm(g.length)} mm` : `${g.set} lead nut`;
251
+ const parts = named({ body }, { body: { label } }), anchors = { origin: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } };
252
+ const values = part === 'screw' ? { length: g.length, diameter: g.d, lead: g.lead } : part === 'coupler' ? { length: g.length, diameter: g.D } : { flange: g.flange, length: g.length };
253
+ const mass = part === 'screw' ? r3((Math.PI * (g.d / 2) ** 2 * g.length * 7.85e-6)) : null; // (a steel rod's: an estimate of a screw's mass, 7.85 g/cm³)
254
+ return referenceBuilt({ id, key: `guide-${part}`, objName: `${part}:${g.set}${part === 'screw' ? `:${g.length}` : ''}`, name: g.set, parts, anchors, entries: [], values, extra: { bought: { set: g.set, variant: null, part: part === 'screw' ? 'leadscrew' : part, mass, ...(part === 'screw' ? { length: g.length } : {}) } } });
255
+ }
256
+ if (part === 'wheel') {
257
+ const parts = named({ body: vWheel(m, { eccentric: it._guide.eccentric }) }, { body: { label: `${m.wheel} on ${it._guide.eccentric ? m.eccentric : 'a plain spacer'}` } });
258
+ // (the whole wheel is checked for overlaps: its rim runs in the track, which the beam's envelope leaves open, so a rim
259
+ // that strays out of it, or a beam or part across its track's line, is found)
260
+ const anchors = { axle: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } }, values = { diameter: SETS_OF(m).diameter, axle: m.seat.axle, depth: m.seat.depth, radial: m.ratings.radial, axial: m.ratings.axial };
261
+ return referenceBuilt({ id, key: 'guide-wheel', objName: `wheel:${m.wheel}${it._guide.eccentric ? ':eccentric' : ''}`, name: m.wheel, parts, anchors, entries: [], values, extra: { guide: m, bought: { set: m.wheel, variant: null, part: 'wheel', mass: m.kitMass } } });
262
+ }
263
+ if (part === 'rail') {
264
+ const parts = named({ body: rail(m) }, { body: { label: `${m.set} rail x ${mm(m.length)} mm` } });
265
+ const anchors = { a: { at: [0, 0, 0], axis: [-1, 0, 0], ref: [0, 0, 1] }, top: { at: [m.length / 2, 0, m.rail.height], axis: [0, 0, 1], ref: [1, 0, 0] }, ...Object.fromEntries(m.screws.map((x) => [x.name, { at: x.at, axis: x.axis }])) };
266
+ const values = { length: m.length, holes: m.holes.length, pitch: m.rail.pitch, end: m.rail.end, mass: m.rail.mass };
267
+ return referenceBuilt({ id, key: 'guide-rail', objName: `rail:${m.set}`, name: m.set, parts, anchors, entries: [], values, extra: { guide: m, bought: { set: m.set, variant: null, part: 'rail', mass: m.rail.mass, length: m.length } } });
268
+ }
269
+ const c = m.carriage, parts = named({ body: carriage(m) }, { body: { label: `${m.set} carriage ${c.variant ?? ''}`.trim() } });
270
+ const anchors = { slide: { at: [0, 0, 0], axis: [1, 0, 0], ref: [0, 0, 1] }, top: { at: [0, 0, c.height], axis: [0, 0, 1], ref: [1, 0, 0] }, ...Object.fromEntries(c.holes.map(([x, y], k) => [`t${k + 1}`, { at: [x, y, c.height], axis: [0, 0, -1] }])) };
271
+ const values = { length: c.length, width: c.width, height: c.height, travel: m.travel, static_rating: c.ratings.C0, moment_roll: c.ratings.MR, moment_pitch: c.ratings.MP, moment_yaw: c.ratings.MY, ...(c.mass != null ? { mass: c.mass } : {}) };
272
+ return referenceBuilt({ id, key: 'guide-carriage', objName: `carriage:${m.set}:${c.variant}`, name: `${m.set} ${c.variant}`, parts, anchors, entries: [], values, extra: { guide: m, bought: { set: m.set, variant: c.variant, part: 'carriage', mass: c.mass ?? null } } });
273
+ }
274
+
275
+ /**
276
+ * A built assembly's guide findings: guide.holes, guide.end and guide.travel from the rail's model, guide.fit (on the
277
+ * face, its nuts, its ends), guide.budget (what it adds to an error budget) and guide.jig (the printed jig that
278
+ * centres it, and how to print one). @param {object} s a built assembly @param {Function} f the findings' sink
279
+ */
280
+ export function guideFindings(s, f) {
281
+ for (const r of s.guides ?? []) {
282
+ if (r.kind === 'rods') {
283
+ const m = r.model, L = r.layout, refs = { guide: r.id, beam: r.beam, carriages: r.carriages, rods: r.rods, bearings: r.bearings };
284
+ f('info', `${r.id}: two ${L.rod.set} rods ${mm(L.spacing)} mm apart, ${mm(L.height)} mm over ${r.beam}'s ${r.face} face, in printed holders from ${mm(r.from)} to ${mm(r.from + r.length)} mm along it; a printed carriage on two ${L.bearing.set} (${mm(m.own)} mm long with what it carries) slides ${mm(r.travel)} mm${r.lo ? `, from its home ${mm(r.lo)} mm along` : ''}`, null, { code: 'guide.travel', values: { from: r.from, length: r.length, own: m.own, travel: r.travel, home: r.lo, spacing: L.spacing, height: L.height }, refs });
285
+ f('info', `${r.id}: the rods are ${mm(r.length)} mm of ${L.rod.set}, cut to length; each ${L.bearing.set} is rated ${mm(L.bearing.C0)} N static and ${mm(L.bearing.C)} N dynamic (${SETS_KIND(L)})`, null, { code: 'guide.rating', values: { static: L.bearing.C0, dynamic: L.bearing.C }, refs });
286
+ if (r.endstop) f('info', `${r.id}: ${r.endstop.switch} on its printed mount at ${r.endstop.at}; the carriage's flag meets its lever ${mm(r.endstop.home)} mm along the travel, where the joint homes (the lever's place is an estimate: measure your switch's lever and its pretravel)`, null, { code: 'guide.endstop', values: { home: r.endstop.home, lever: r.endstop.model.lever }, refs: { ...refs, switch: r.endstop.switch, mount: r.endstop.mount } });
287
+ continue;
288
+ }
289
+ if (r.kind === 'screw') {
290
+ const L = r.layout, refs = { guide: r.id, drives: r.guide, motor: r.motor, ...r.items };
291
+ f('info', `${r.id}: ${L.motor.set} on a printed plate ${mm(r.from)} mm along ${r.beam}, through ${L.coupler.set} to ${mm(r.length)} mm of ${L.screw.set} (a ${mm(r.lead)} mm lead${L.support ? `, in a ${L.support.set} at each holder` : ''}), the ${L.nut.set} on ${r.guide}'s carriage; the lead and the nut's friction μ ${n3(r.friction, 3)} (${r.frictionKind ?? 'default'}) go to the motor's checks as a lead screw`, null, { code: 'guide.drive', values: { lead: r.lead, length: r.length, friction: r.friction, engaged: r.plate.engaged }, refs });
292
+ const engaged = r.plate.engaged, want = L.motor.shaft.d;
293
+ f(engaged >= want - 1e-9 ? 'pass' : 'warn', `${r.id}: the motor's shaft reaches ${mm(engaged)} mm into ${L.coupler.set} (the plate ${mm(r.plate.thickness)} mm${r.plate.recess ? `, the coupler ${mm(r.plate.recess.depth)} mm into it` : ''}), ${engaged >= want - 1e-9 ? 'at least' : 'less than'} its ${mm(want)} mm diameter${engaged >= want - 1e-9 ? '' : ': a thinner plate at the shaft, or a motor with a longer shaft'}`, engaged >= want - 1e-9 ? null : `${r.id}: shaft short`, { code: 'guide.coupler', values: { engaged, want }, refs });
294
+ continue;
295
+ }
296
+ if (r.kind === 'wheels' || r.kind === 'slider') {
297
+ const m = r.model, refs = { guide: r.id, beam: r.beam, carriages: r.carriages, ...(r.wheels.length ? { wheels: r.wheels } : {}) };
298
+ f('info', `${r.id}: ${r.kind === 'wheels' ? `a plate on ${m.count} ${m.wheel} wheels over ${r.beam}'s ${r.face} face, ${mm(m.length)} mm long over its wheels, rolls` : `a printed slider ${mm(m.length)} mm long in ${r.beam}'s ${r.face} slot slides`} from ${mm(r.from)} to ${mm(r.from + r.length)} mm along ${r.beam}: travel ${mm(r.travel)} mm`, null, { code: 'guide.travel', values: { from: r.from, length: r.length, own: m.length, travel: r.travel }, refs });
299
+ continue;
300
+ }
301
+ const refs = { guide: r.id, beam: r.beam, rail: r.model.set, carriages: r.carriages };
302
+ for (const x of r.model.findings) f(x.level, `${r.id}: ${x.text}`, x.level === 'warn' || x.level === 'fail' ? `${r.id}: ${x.code}` : null, { code: x.code, values: x.values ?? {}, refs });
303
+ for (const x of r.fit.findings) f(x.level, `${r.id}: ${x.text}`, x.level === 'fail' ? `${r.id}: fit` : null, { code: x.code, values: x.values ?? {}, refs: { ...refs, ...(x.refs ?? {}) } });
304
+ const t = r.terms ?? [];
305
+ if (t.length) f('info', `${r.id}: along its travel it adds ${t.map((x) => `${x.term} ${n3(x.value, 3)} mm (${x.kind})`).join(', ')} to the error budget: ${n3(t.reduce((a, x) => a + x.value, 0), 3)} mm at worst. The beam's sag is an estimate (${GUIDE_DEFAULTS.sag.rule.split(':')[0].toLowerCase()})`, null, { code: 'guide.budget', values: { terms: t, worst: t.reduce((a, x) => a + x.value, 0) }, refs });
306
+ if (r.jig?.error) f('info', `${r.id}: no printed jig: ${r.jig.error}`, null, { code: 'guide.jig', refs });
307
+ else if (r.jig) f('info', `${r.id}: a printed jig centres the rail on ${r.beam} while its screws are tightened: ${n3(r.jig.grams)} g, ${n3(r.jig.margin)}× its load. Print one as an item: { "fitting": { "kind": "rail-jig", "profile": "${r.jig.profile.replace(/^profile-/, '')}", "rail": "${r.model.set}" } }`, null, { code: 'guide.jig', values: { grams: r.jig.grams, margin: r.jig.margin }, refs });
308
+ }
309
+ }
310
+
311
+ /**
312
+ * Each carriage's loads against its ratings, at every pose the machine is in (its named poses, the guide's two ends among
313
+ * them, and as drawn): the weight of everything it carries and the masses the assembly's loads hang on it, as a force
314
+ * and a moment about the carriage's middle on the rail's top, in the carriage's frame. joint.moment fails past its
315
+ * ratings (frames' carriageMargin). @param {object} s @param {Function} f
316
+ * @param {{ placeItems: Function, jointValues: Function }} helpers @param {{ masses: object[], beyond: Function, poses: object[] }} known
317
+ */
318
+ export function carriageChecks(s, f, helpers, { masses, beyond, poses }) {
319
+ const motion = s.motion, { placeItems } = helpers, ids = motion.items.map((x) => x[0]), g = motion.gravity || [0, 0, -1], gl = Math.hypot(...g), down = g.map((x) => x / gl);
320
+ const specs = new Map((s.a.joints || []).filter((j) => j._guide).map((j) => [j.id, j]));
321
+ for (const j of motion.joints) {
322
+ const spec = specs.get(j.id); if (!spec || spec._guide.kind === 'slider') continue;
323
+ const m = spec._guide.model, wheels = spec._guide.kind === 'wheels', rods = spec._guide.kind === 'rods', ci = ids.indexOf(spec._guide.carriage), hang = (motion.loads || []).filter((l) => l.mass != null).map((l) => ({ kg: l.mass, item: typeof l.item === 'number' ? l.item : ids.indexOf(l.item), frame: l.frame }));
324
+ const o = rods ? m.anchors.slide.at : wheels ? [0, 0, 0] : [0, 0, m.rail.height]; // (about the carriage's middle: a rail's on its top, a wheel plate's on the face, a rod carriage's on its rods' line)
325
+ let worst = null;
326
+ for (const p of poses) {
327
+ const W = placeItems(motion, p.values), C = W[ci], O = [0, 1, 2].map((k) => C[4 * k] * o[0] + C[4 * k + 1] * o[1] + C[4 * k + 2] * o[2] + C[4 * k + 3]);
328
+ let F = [0, 0, 0], M = [0, 0, 0], kg = 0;
329
+ const add = (mass, X) => { const fo = down.map((x) => x * mass * G), r = [X[0] - O[0], X[1] - O[1], X[2] - O[2]]; F = F.map((x, k) => x + fo[k]); M = [M[0] + (r[1] * fo[2] - r[2] * fo[1]) / 1000, M[1] + (r[2] * fo[0] - r[0] * fo[2]) / 1000, M[2] + (r[0] * fo[1] - r[1] * fo[0]) / 1000]; kg += mass; };
330
+ for (const q of masses) if (q.item === ci || beyond(motion, j, q.item)) { const Wq = W[q.item]; add(q.kg, [0, 1, 2].map((k) => Wq[4 * k] * q.centre[0] + Wq[4 * k + 1] * q.centre[1] + Wq[4 * k + 2] * q.centre[2] + Wq[4 * k + 3])); }
331
+ for (const l of hang) if (l.item === ci || beyond(motion, j, l.item)) { const Wq = W[l.item], at = [l.frame[3], l.frame[7], l.frame[11]]; add(l.kg, [0, 1, 2].map((k) => Wq[4 * k] * at[0] + Wq[4 * k + 1] * at[1] + Wq[4 * k + 2] * at[2] + Wq[4 * k + 3])); }
332
+ const local = (v) => [0, 1, 2].map((k) => C[k] * v[0] + C[4 + k] * v[1] + C[8 + k] * v[2]); // (the carriage's axes, as rows of its frame's transpose)
333
+ const load = { force: local(F), moment: local(M) }, r = rods ? rodMargin(m, load) : wheels ? wheelMargin(m, load) : carriageMargin(m, load);
334
+ if (!worst || r.margin < worst.r.margin) worst = { r, load, p, kg, O };
335
+ }
336
+ if (!worst) continue;
337
+ const { r, load, p, kg, O } = worst, [, Fy, Fz] = load.force, [Mx, My, Mz] = load.moment;
338
+ if (rods) {
339
+ const b = r.bearings.find((x) => x.name === r.worst);
340
+ const text = `${spec._guide.carriage} on ${spec._guide.id}: ${n3(kg * 1000, 0)} g carried, worst ${p.label}: ${n3(Math.hypot(Fy, Fz), 1)} N across the rods, roll ${n3(Mx, 3)}, pitch ${n3(My, 3)} and yaw ${n3(Mz, 3)} N m; its most loaded bearing, ${r.worst}, takes ${n3(b.radial, 1)} N against ${m.bearing}'s ${n3(r.rating, 0)} N static rating: ${n3(r.margin, 1)} times over${r.level === 'fail' ? ', past its rating' : ''} (a moment on a bush as a couple along it: an estimate)`;
341
+ f(r.level, text, r.level === 'pass' ? null : `${j.id}: bearings ${n3(r.margin, 1)}×`, { code: 'joint.moment', at: O, refs: { joint: j.id, carriage: spec._guide.carriage, guide: spec._guide.id, bearing: m.bearing }, values: { force: load.force, moment: load.moment, bearings: r.bearings, worst: r.worst, margin: r.margin, carried: kg, pose: p.label, rating: r.rating } });
342
+ continue;
343
+ }
344
+ if (wheels) {
345
+ const w = r.wheels.find((x) => x.name === r.worst), rt = m.ratings;
346
+ const text = `${spec._guide.carriage} on ${spec._guide.id}: ${n3(kg * 1000, 0)} g carried, worst ${p.label}: ${n3(Math.hypot(Fy, Fz), 1)} N on the plate, roll ${n3(Mx, 3)}, pitch ${n3(My, 3)} and yaw ${n3(Mz, 3)} N m; its most loaded wheel, ${r.worst}, takes ${n3(w.radial, 1)} N across and ${n3(w.axial, 1)} N along its axle against ${n3(rt.radial, 1)} and ${n3(rt.axial, 1)} N: ${n3(r.margin, 2)} times over${r.level === 'fail' ? ', past its rating' : ''} (each wheel's rating from its plastic's contact pressure, and the loads shared out among the wheels: estimates)`;
347
+ f(r.level, text, r.level === 'pass' ? null : `${j.id}: wheels ${n3(r.margin, 1)}×`, { code: 'joint.moment', at: O, refs: { joint: j.id, carriage: spec._guide.carriage, guide: spec._guide.id, wheel: m.wheel }, values: { force: load.force, moment: load.moment, wheels: r.wheels, worst: r.worst, margin: r.margin, carried: kg, pose: p.label, ratings: { radial: rt.radial, axial: rt.axial } } });
348
+ continue;
349
+ }
350
+ const c = m.carriage.ratings;
351
+ const text = `${spec._guide.carriage} (${m.carriage.variant}) on ${spec._guide.id}: ${n3(kg * 1000, 0)} g carried, worst ${p.label}: ${n3(Math.hypot(Fy, Fz), 1)} N across the rail, roll ${n3(Mx, 3)}, pitch ${n3(My, 3)} and yaw ${n3(Mz, 3)} N m, against C0 ${c.C0} N, MR ${c.MR}, MP ${c.MP} and MY ${c.MY} N m: ${n3(r.margin, 2)} times over${r.level === 'fail' ? `, past its ${r.worst} rating` : ''} (each load over its rating, summed: an estimate of how they combine)`;
352
+ f(r.level, text, r.level === 'pass' ? null : `${j.id}: carriage ${n3(r.margin, 1)}×`, { code: 'joint.moment', at: O, refs: { joint: j.id, carriage: spec._guide.carriage, guide: spec._guide.id, rail: m.set }, values: { force: load.force, moment: load.moment, ratios: r.ratios, margin: r.margin, carried: kg, pose: p.label, ratings: c } });
353
+ }
354
+ }