@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.
Files changed (83) hide show
  1. package/AGENTS.md +24 -10
  2. package/CHANGELOG.md +404 -0
  3. package/EXAMPLES.md +40 -38
  4. package/README.md +193 -67
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2250 -512
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1847 -434
  9. package/lib/beams.mjs +256 -52
  10. package/lib/belts.mjs +66 -15
  11. package/lib/board-work.mjs +12 -0
  12. package/lib/bought.mjs +309 -53
  13. package/lib/buildpage.mjs +179 -29
  14. package/lib/carried.mjs +201 -0
  15. package/lib/clone.mjs +194 -42
  16. package/lib/design.mjs +649 -121
  17. package/lib/drive.mjs +187 -63
  18. package/lib/edit.mjs +899 -191
  19. package/lib/errors.mjs +16 -3
  20. package/lib/explain.mjs +62 -13
  21. package/lib/fasten.mjs +2702 -507
  22. package/lib/fittings.mjs +715 -121
  23. package/lib/fixings.mjs +21 -9
  24. package/lib/framebuild.mjs +790 -175
  25. package/lib/framecheck.mjs +1901 -368
  26. package/lib/framegen.mjs +100 -22
  27. package/lib/frameheat.mjs +225 -42
  28. package/lib/frameprint.mjs +513 -108
  29. package/lib/frames.mjs +81 -17
  30. package/lib/framesize.mjs +792 -120
  31. package/lib/frametip.mjs +235 -0
  32. package/lib/gait.mjs +135 -23
  33. package/lib/grip.mjs +62 -13
  34. package/lib/guides.mjs +1403 -230
  35. package/lib/host.mjs +123 -21
  36. package/lib/inplace.mjs +171 -36
  37. package/lib/jigs.mjs +360 -78
  38. package/lib/joins.mjs +697 -118
  39. package/lib/jsonedit.mjs +88 -26
  40. package/lib/kept.mjs +84 -0
  41. package/lib/kit.mjs +438 -68
  42. package/lib/layout.mjs +2 -1
  43. package/lib/links.mjs +149 -42
  44. package/lib/list.mjs +326 -64
  45. package/lib/live-guard.mjs +68 -0
  46. package/lib/live-shape.mjs +96 -24
  47. package/lib/live.mjs +189 -59
  48. package/lib/loads.mjs +579 -130
  49. package/lib/machine.mjs +372 -74
  50. package/lib/magnets.mjs +162 -44
  51. package/lib/measured.mjs +417 -94
  52. package/lib/mesh.mjs +526 -119
  53. package/lib/meshcmd.mjs +215 -60
  54. package/lib/motors.mjs +1021 -213
  55. package/lib/motorsvg.mjs +114 -32
  56. package/lib/names.mjs +3 -1
  57. package/lib/optimise.mjs +978 -283
  58. package/lib/order.mjs +519 -112
  59. package/lib/panels.mjs +482 -94
  60. package/lib/parts.mjs +78 -23
  61. package/lib/pins.mjs +163 -44
  62. package/lib/plates.mjs +223 -35
  63. package/lib/player.mjs +37 -9
  64. package/lib/power.mjs +64 -13
  65. package/lib/preview.mjs +83 -16
  66. package/lib/profilemeasure.mjs +572 -129
  67. package/lib/rules.mjs +15 -5
  68. package/lib/sandbox-hooks.mjs +77 -19
  69. package/lib/sandbox-run.mjs +117 -18
  70. package/lib/sandbox.mjs +509 -60
  71. package/lib/schema.mjs +42 -11
  72. package/lib/seats.mjs +227 -49
  73. package/lib/services.mjs +261 -52
  74. package/lib/sheet.mjs +851 -178
  75. package/lib/slotnuts.mjs +213 -34
  76. package/lib/targets/gist.mjs +4 -1
  77. package/lib/targets.mjs +22 -4
  78. package/lib/task.mjs +376 -79
  79. package/lib/versions.mjs +111 -28
  80. package/lib/wires.mjs +228 -45
  81. package/lib/workspace.mjs +197 -12
  82. package/package.json +19 -19
  83. package/schemas/assembly.schema.json +2 -2
package/lib/guides.mjs CHANGED
@@ -12,24 +12,73 @@
12
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
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
14
  import jscad from '@jscad/modeling';
15
- import { railModel, rail, carriage, carriageMargin, guideFit, railTerms, railJigModel, GUIDE_DEFAULTS, vWheel, wheelMargin, rodsLayout, rodMargin, ROD_DEFAULTS, beltLayout, beltRub, BELT_DEFAULTS } from '@rulecad/frames';
15
+ import {
16
+ railModel,
17
+ rail,
18
+ carriage,
19
+ carriageMargin,
20
+ guideFit,
21
+ railTerms,
22
+ railJigModel,
23
+ GUIDE_DEFAULTS,
24
+ vWheel,
25
+ wheelMargin,
26
+ rodsLayout,
27
+ rodMargin,
28
+ ROD_DEFAULTS,
29
+ beltLayout,
30
+ beltRub,
31
+ beltSpans,
32
+ BELT_DEFAULTS,
33
+ } from '@rulecad/frames';
16
34
  import { beamOf, framesContext } from './beams.mjs';
17
35
  import { givenFor } from './frames.mjs';
18
36
  import { named } from './parts.mjs';
19
37
  import { referenceBuilt } from './bought.mjs';
20
38
  import { fittingModel } from './fittings.mjs';
21
39
 
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
- belt: ['id', 'kind', 'guide', 'motor', 'pulley', 'idler', 'belt', 'tension', 'accel', 'cut', 'across', 'idlerAt', 'height', 'nut', 'note'] };
25
- const { cylinder, cuboid, polygon } = jscad.primitives, { translate, rotateZ } = jscad.transforms, { subtract, union } = jscad.booleans, { extrudeRotate } = jscad.extrusions;
40
+ const KEYS = {
41
+ rail: ['id', 'kind', 'rail', 'carriage', 'on', 'from', 'length', 'carriages', 'nut', 'note'],
42
+ wheels: ['id', 'kind', 'wheel', 'wheels', 'pitch', 'load', 'on', 'from', 'length', 'note'],
43
+ slider: ['id', 'kind', 'mode', 'size', 'load', 'on', 'from', 'length', 'note'],
44
+ rods: ['id', 'kind', 'rod', 'bearing', 'spacing', 'height', 'load', 'on', 'from', 'length', 'endstop', 'nut', 'note'],
45
+ screw: ['id', 'kind', 'guide', 'screw', 'nut', 'support', 'coupler', 'motor', 'friction', 'note'],
46
+ belt: [
47
+ 'id',
48
+ 'kind',
49
+ 'guide',
50
+ 'motor',
51
+ 'pulley',
52
+ 'idler',
53
+ 'belt',
54
+ 'tension',
55
+ 'accel',
56
+ 'cut',
57
+ 'across',
58
+ 'idlerAt',
59
+ 'height',
60
+ 'nut',
61
+ 'note',
62
+ ],
63
+ };
64
+ const { cylinder, cuboid, polygon } = jscad.primitives,
65
+ { translate, rotateZ } = jscad.transforms,
66
+ { subtract, union } = jscad.booleans,
67
+ { extrudeRotate } = jscad.extrusions;
26
68
  const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
27
69
  const up2 = (x, step) => Math.ceil(x / step - 1e-9) * step;
28
- const mm = (x) => String(+Number(x).toFixed(2)), r3 = (x) => +x.toFixed(3), n3 = (x, d = 2) => String(Number(Number(x).toFixed(d)));
29
- const strip = (e) => String(e?.message ?? e).replace(/^(railModel|guideFit|railTerms|railJigModel|wheelsModel|sliderModel|rodsLayout|beltLayout|beltPlateModel|beltClampModel|beltIdlerModel): /, '');
70
+ const mm = (x) => String(+Number(x).toFixed(2)),
71
+ r3 = (x) => +x.toFixed(3),
72
+ n3 = (x, d = 2) => String(Number(Number(x).toFixed(d)));
73
+ const strip = (e) =>
74
+ String(e?.message ?? e).replace(
75
+ /^(railModel|guideFit|railTerms|railJigModel|wheelsModel|sliderModel|rodsLayout|beltLayout|beltPlateModel|beltClampModel|beltIdlerModel): /,
76
+ '',
77
+ );
30
78
  const G = 9.80665;
31
79
  const SETS_OF = (m) => ({ diameter: r3(2 * (m.seat.axle + m.seat.depth)) });
32
- 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`;
80
+ const SETS_KIND = (L) =>
81
+ `${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`;
33
82
 
34
83
  /**
35
84
  * 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
@@ -41,253 +90,940 @@ const SETS_KIND = (L) => `${L.bearing.set}'s ratings as its set gives them: a si
41
90
  export function expandGuides(a, num, context = framesContext(), material = 'pla') {
42
91
  const list = a.guides;
43
92
  if (list == null) return { a, guides: [] };
44
- if (!Array.isArray(list)) throw new Error(`guides is a list: [{ "kind": "rail", "rail": "rail-mgn12", "on": "x1.top", "length": 300 }] (got ${shown(list)})`);
45
- 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 ?? {} };
46
- const added = [], fasten = [], joints = [], poses = {}, guides = [], first = (a.fasten || []).length;
93
+ if (!Array.isArray(list))
94
+ throw new Error(
95
+ `guides is a list: [{ "kind": "rail", "rail": "rail-mgn12", "on": "x1.top", "length": 300 }] (got ${shown(list)})`,
96
+ );
97
+ const items = a.items || [],
98
+ beams = new Map(items.filter((it) => it.beam).map((it) => [it.id, it])),
99
+ ids = new Set(items.map((it) => it.id)),
100
+ given = { sets: context?.sets ?? {} };
101
+ const added = [],
102
+ fasten = [],
103
+ joints = [],
104
+ poses = {},
105
+ guides = [],
106
+ first = (a.fasten || []).length;
47
107
  // a carriage on V-wheels or a printed slider: a printed fitting on its own sliding joint along the beam, through the
48
108
  // stretch from..from + length less its own length; a plate's wheels bought items held to it at its anchors
49
109
  const rolling = (g, { what, id, bid, B, q, faceName }) => {
50
- const wheels = g.kind === 'wheels', face = wheels ? q.face : B.places.filter((x) => x.face === q.face).length > 1 ? q.name : q.face;
51
- 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`);
52
- if (!wheels && !q.slot) throw new Error(`${what}: ${bid}'s ${faceName} face is closed: a slider runs in a slot or keys into one`);
53
- 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) } : {}) }
54
- : { 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) } : {}) };
55
- 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);
56
- 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`);
57
- 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`);
58
- const cid = `${id}-carriage-1`, jid = `${id}-1`;
59
- for (const x of [cid, jid]) if (ids.has(x)) throw new Error(`${what}: ${x} is an item already: give the guide another id`);
110
+ const wheels = g.kind === 'wheels',
111
+ face = wheels ? q.face : B.places.filter((x) => x.face === q.face).length > 1 ? q.name : q.face;
112
+ if (wheels && faceName.includes('.'))
113
+ throw new Error(
114
+ `${what}: on is the face the plate lies over, as ${bid}.${q.face}: its wheels run in the slots either side of it`,
115
+ );
116
+ if (!wheels && !q.slot)
117
+ throw new Error(`${what}: ${bid}'s ${faceName} face is closed: a slider runs in a slot or keys into one`);
118
+ const spec = wheels
119
+ ? {
120
+ kind: 'wheel-plate',
121
+ profile: B.profile.name,
122
+ face,
123
+ ...(g.wheel != null ? { wheel: g.wheel } : {}),
124
+ ...(g.wheels != null ? { wheels: g.wheels } : {}),
125
+ ...(g.pitch != null ? { pitch: num(g.pitch) } : {}),
126
+ ...(g.load != null ? { load: num(g.load) } : {}),
127
+ }
128
+ : {
129
+ kind: 'slider',
130
+ profile: B.profile.name,
131
+ face,
132
+ ...(g.mode != null ? { mode: g.mode } : {}),
133
+ ...(g.size != null ? { length: num(g.size) } : {}),
134
+ ...(g.load != null ? { load: num(g.load) } : {}),
135
+ };
136
+ const m = fittingModel(spec, context, material, what),
137
+ from = g.from != null ? num(g.from) : 0,
138
+ length = num(g.length),
139
+ own = wheels ? m.length : m.length,
140
+ travel = r3(length - own);
141
+ if (!(from >= 0 && from + length <= B.length + 1e-9))
142
+ throw new Error(
143
+ `${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`,
144
+ );
145
+ if (!(travel > 0))
146
+ throw new Error(
147
+ `${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`,
148
+ );
149
+ const cid = `${id}-carriage-1`,
150
+ jid = `${id}-1`;
151
+ for (const x of [cid, jid])
152
+ if (ids.has(x)) throw new Error(`${what}: ${x} is an item already: give the guide another id`);
60
153
  ids.add(cid);
61
- const P = B.profile, n = q.normal, centre = wheels ? [r3((n[0] * P.width) / 2), r3((n[1] * P.height) / 2)] : q.at;
154
+ const P = B.profile,
155
+ n = q.normal,
156
+ centre = wheels ? [r3((n[0] * P.width) / 2), r3((n[1] * P.height) / 2)] : q.at;
62
157
  added.push({ id: cid, fitting: spec, guideOf: id });
63
- 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: [] } });
64
- const record = { id, kind: g.kind, beam: bid, face, from, length, model: m, carriages: [cid], joints: [jid], travel, wheels: [], note: g.note ?? null, fasten: [] };
65
- if (wheels) m.wheels.forEach((w, k) => {
66
- const wid = `${id}-wheel-${k + 1}`;
67
- if (ids.has(wid)) throw new Error(`${what}: ${wid} is an item already: give the guide another id`);
68
- ids.add(wid); record.wheels.push(wid);
69
- added.push({ id: wid, bought: m.wheel, _guide: { part: 'wheel', model: m, eccentric: w.eccentric }, at: `${cid}.${w.name}`, guideOf: id });
158
+ joints.push({
159
+ id: jid,
160
+ type: 'prismatic',
161
+ parent: { item: bid, at: [centre[0], centre[1], r3(from + own / 2)], axis: [0, 0, 1], ref: [n[0], n[1], 0] },
162
+ child: `${cid}.slide`,
163
+ min: 0,
164
+ max: travel,
165
+ label: `${cid} ${wheels ? 'rolls' : 'slides'} along ${bid}`,
166
+ _guide: { id, kind: g.kind, carriage: cid, model: m, terms: [] },
70
167
  });
71
- poses[`${id}-a`] = { [jid]: 0 }; poses[`${id}-b`] = { [jid]: travel };
168
+ const record = {
169
+ id,
170
+ kind: g.kind,
171
+ beam: bid,
172
+ face,
173
+ from,
174
+ length,
175
+ model: m,
176
+ carriages: [cid],
177
+ joints: [jid],
178
+ travel,
179
+ wheels: [],
180
+ note: g.note ?? null,
181
+ fasten: [],
182
+ };
183
+ if (wheels)
184
+ m.wheels.forEach((w, k) => {
185
+ const wid = `${id}-wheel-${k + 1}`;
186
+ if (ids.has(wid)) throw new Error(`${what}: ${wid} is an item already: give the guide another id`);
187
+ ids.add(wid);
188
+ record.wheels.push(wid);
189
+ added.push({
190
+ id: wid,
191
+ bought: m.wheel,
192
+ _guide: { part: 'wheel', model: m, eccentric: w.eccentric },
193
+ at: `${cid}.${w.name}`,
194
+ guideOf: id,
195
+ });
196
+ });
197
+ poses[`${id}-a`] = { [jid]: 0 };
198
+ poses[`${id}-b`] = { [jid]: travel };
72
199
  guides.push(record);
73
200
  };
74
201
  // a lead screw drives a rods guide's carriage: found first, so the rods guide's parts are drawn for it
75
- const fits = [], screws = new Map();
202
+ const fits = [],
203
+ screws = new Map();
76
204
  list.forEach((g, i) => {
77
205
  if (g?.kind !== 'screw') return;
78
206
  const what = `guide ${g.id ?? i + 1}`;
79
- 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(', ')}`);
207
+ for (const k of Object.keys(g))
208
+ if (!KEYS.screw.includes(k))
209
+ throw new Error(`${what}: a screw guide has no "${k}": it takes ${KEYS.screw.join(', ')}`);
80
210
  const target = list.find((x) => x && (x.id ?? `g${list.indexOf(x) + 1}`) === g.guide);
81
- 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(', ')})`);
82
- 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 rail guide's carriage is driven by a belt, and screws on rails or wheels are not built yet`);
83
- if (screws.has(g.guide)) throw new Error(`${what}: ${g.guide} is driven by another screw already: one drive to a carriage`);
84
- 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)})`);
85
- 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`);
211
+ if (!target)
212
+ throw new Error(
213
+ `${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(', ')})`,
214
+ );
215
+ if (target.kind !== 'rods')
216
+ throw new Error(
217
+ `${what}: a screw drives a rods guide's carriage, and ${g.guide} is a ${target.kind} guide; a rail guide's carriage is driven by a belt, and screws on rails or wheels are not built yet`,
218
+ );
219
+ if (screws.has(g.guide))
220
+ throw new Error(`${what}: ${g.guide} is driven by another screw already: one drive to a carriage`);
221
+ if (!g.motor || typeof g.motor !== 'object' || typeof g.motor.model !== 'string')
222
+ throw new Error(
223
+ `${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)})`,
224
+ );
225
+ for (const k of ['drives', 'leadscrew', 'belt'])
226
+ if (g.motor[k] != null) throw new Error(`${what}: motor.${k} comes from the guide: leave it out`);
86
227
  screws.set(g.guide, { g, i, id: g.id ?? `g${i + 1}`, what });
87
228
  });
88
229
  // two rods along a face in printed holders at the stretch's ends, a printed carriage on linear bearings on its own
89
230
  // sliding joint; with a screw, a motor plate, the motor, a coupler and the screw, bearings for it in the holders and the
90
231
  // lead nut on the carriage; with an endstop, its mount at the address given and a flag on the carriage
91
232
  const rodsGuide = (g, { what, id, bid, B, q, faceName }) => {
92
- 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`);
93
- const P = B.profile, gv = givenFor(context, P.name), sc = screws.get(id), sg = sc?.g;
94
- 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) } : {});
233
+ if (faceName.includes('.'))
234
+ throw new Error(
235
+ `${what}: on is the face the rods run over, as ${bid}.${q.face}: its holders' legs screw into the slots either side`,
236
+ );
237
+ const P = B.profile,
238
+ gv = givenFor(context, P.name),
239
+ sc = screws.get(id),
240
+ sg = sc?.g;
241
+ const from = g.from != null ? num(g.from) : 0,
242
+ length = num(g.length),
243
+ opt = (k, x) => (x != null ? { [k]: typeof x === 'string' ? x : num(x) } : {});
95
244
  let L;
96
- 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 }); }
97
- catch (e) { throw new Error(`${(sg && /screw|nut|support|coupler|motor/.test(String(e.message)) ? sc.what : what)}: ${strip(e)}`); }
98
- 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`);
99
- 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) } : {}) };
100
- const drive = L.screw ? { screw: L.screw.set, leadNut: L.nut.set, ...(L.support ? { support: L.support.set } : { support: null }) } : {};
245
+ try {
246
+ L = rodsLayout(P.name, {
247
+ face: q.face,
248
+ ...opt('rod', g.rod),
249
+ ...opt('bearing', g.bearing),
250
+ ...opt('spacing', g.spacing),
251
+ ...opt('height', g.height),
252
+ ...(sg
253
+ ? {
254
+ screw: sg.screw ?? 't8-screw',
255
+ ...opt('nut', sg.nut),
256
+ ...(sg.support !== undefined ? { support: sg.support } : {}),
257
+ ...opt('coupler', sg.coupler),
258
+ motor: sg.motor.model,
259
+ }
260
+ : {}),
261
+ given: gv,
262
+ });
263
+ } catch (e) {
264
+ throw new Error(
265
+ `${sg && /screw|nut|support|coupler|motor/.test(String(e.message)) ? sc.what : what}: ${strip(e)}`,
266
+ );
267
+ }
268
+ if (!(from >= 0 && from + length <= B.length + 1e-9))
269
+ throw new Error(
270
+ `${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`,
271
+ );
272
+ const base = {
273
+ profile: P.name,
274
+ face: q.face,
275
+ rod: L.rod.set,
276
+ bearing: L.bearing.set,
277
+ spacing: L.spacing,
278
+ height: L.height,
279
+ ...(g.load != null ? { load: num(g.load) } : {}),
280
+ };
281
+ const drive = L.screw
282
+ ? { screw: L.screw.set, leadNut: L.nut.set, ...(L.support ? { support: L.support.set } : { support: null }) }
283
+ : {};
101
284
  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)
102
- const hold = (end) => fittingModel({ kind: 'rod-holder', ...base, ...drive, ...slotNut, end }, context, material, what);
103
- 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; };
104
- const ha = ids_(`${id}-holder-a`), hb = ids_(`${id}-holder-b`), cid = ids_(`${id}-carriage-1`), jid = `${id}-1`, gap = ROD_DEFAULTS.gap.value;
105
- 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)];
285
+ const hold = (end) =>
286
+ fittingModel({ kind: 'rod-holder', ...base, ...drive, ...slotNut, end }, context, material, what);
287
+ const hA = hold('a'),
288
+ ids_ = (x) => {
289
+ if (ids.has(x)) throw new Error(`${what}: ${x} is an item already: give the guide another id`);
290
+ ids.add(x);
291
+ return x;
292
+ };
293
+ const ha = ids_(`${id}-holder-a`),
294
+ hb = ids_(`${id}-holder-b`),
295
+ cid = ids_(`${id}-carriage-1`),
296
+ jid = `${id}-1`,
297
+ gap = ROD_DEFAULTS.gap.value;
298
+ const n = q.normal,
299
+ yd = [n[1], -n[0]],
300
+ F = L.frame,
301
+ at = (y, z) => [r3(q.at[0] + yd[0] * y + n[0] * z), r3(q.at[1] + yd[1] * y + n[1] * z)];
106
302
  // the endstop: its mount at the address given, on a face beside; the flag on the carriage where its lever is
107
- let es = null, flag = null;
303
+ let es = null,
304
+ flag = null;
108
305
  if (g.endstop != null) {
109
306
  const e = g.endstop;
110
- 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)})`);
111
- 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`);
112
- const [place, dist] = e.at.split('@'), [ebid, ...ef] = place.split('.'), side = F.sides.find((x) => x.name === ef.join('.') || x.side === ef.join('.'));
113
- 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)})`);
114
- 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;
115
- 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;
307
+ if (!e || typeof e !== 'object' || typeof e.at !== 'string')
308
+ throw new Error(
309
+ `${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)})`,
310
+ );
311
+ for (const k of Object.keys(e))
312
+ if (!['at', 'switch'].includes(k)) throw new Error(`${what}: an endstop has no "${k}": it takes at and switch`);
313
+ const [place, dist] = e.at.split('@'),
314
+ [ebid, ...ef] = place.split('.'),
315
+ side = F.sides.find((x) => x.name === ef.join('.') || x.side === ef.join('.'));
316
+ if (ebid !== bid || !side || dist == null)
317
+ throw new Error(
318
+ `${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)})`,
319
+ );
320
+ const sq = B.places.find((x) => x.name === side.name || (x.face === side.side && x.index === 1)),
321
+ sy = [sq.normal[1], -sq.normal[0]],
322
+ toward = Math.sign(sy[0] * n[0] + sy[1] * n[1]) || 1;
323
+ const spec = {
324
+ kind: 'endstop-mount',
325
+ profile: P.name,
326
+ face: side.name,
327
+ toward,
328
+ ...(e.switch ? { switch: e.switch } : {}),
329
+ ...slotNut,
330
+ },
331
+ em = fittingModel(spec, context, material, `${what}: endstop`),
332
+ d = num(dist.replace(/%$/, '')) * (dist.endsWith('%') ? B.length / 100 : 1),
333
+ dd = dist.startsWith('-') ? B.length + d : d;
116
334
  const fd = ROD_DEFAULTS.flag.value;
117
335
  flag = { y: r3(side.y + side.out * em.lever.out), z: r3(side.z + em.lever.reach - fd.engage) };
118
- 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 };
336
+ es = {
337
+ spec,
338
+ model: em,
339
+ at: e.at,
340
+ distance: r3(dd),
341
+ side: side.name,
342
+ mount: ids_(`${id}-endstop-mount`),
343
+ sw: ids_(`${id}-endstop`),
344
+ set: em.switch,
345
+ };
119
346
  }
120
- const cm = fittingModel({ kind: 'rod-carriage', ...base, ...(L.screw ? { screw: L.screw.set, leadNut: L.nut.set } : {}), ...(flag ? { flag } : {}) }, context, material, what);
121
- const travel0 = r3(length - 2 * hA.thickness - 2 * gap - cm.own), c0 = from + hA.thickness + gap + cm.length / 2;
122
- 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`);
347
+ const cm = fittingModel(
348
+ {
349
+ kind: 'rod-carriage',
350
+ ...base,
351
+ ...(L.screw ? { screw: L.screw.set, leadNut: L.nut.set } : {}),
352
+ ...(flag ? { flag } : {}),
353
+ },
354
+ context,
355
+ material,
356
+ what,
357
+ );
358
+ const travel0 = r3(length - 2 * hA.thickness - 2 * gap - cm.own),
359
+ c0 = from + hA.thickness + gap + cm.length / 2;
360
+ if (!(travel0 > 0))
361
+ throw new Error(
362
+ `${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`,
363
+ );
123
364
  // where the flag meets the lever: the travel's lower end, and the joint's home
124
365
  let lo = 0;
125
366
  if (es) {
126
- const lever = es.distance + es.model.lever.x, face0 = c0 + cm.length / 2 - ROD_DEFAULTS.flag.value.thickness, home = r3(lever - face0);
367
+ const lever = es.distance + es.model.lever.x,
368
+ face0 = c0 + cm.length / 2 - ROD_DEFAULTS.flag.value.thickness,
369
+ home = r3(lever - face0);
127
370
  const ok = es.distance + es.model.outline[0][0] >= from + hA.legs + gap - 1e-6;
128
371
  if (home < 0 || home >= travel0 || !ok) {
129
- const best = r3(face0 - es.model.lever.x), least = r3(from + hA.legs + gap - es.model.outline[0][0]);
372
+ const best = r3(face0 - es.model.lever.x),
373
+ least = r3(from + hA.legs + gap - es.model.outline[0][0]);
130
374
  const place = Math.max(best, least);
131
- 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`);
375
+ throw new Error(
376
+ `${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`,
377
+ );
132
378
  }
133
- lo = home; es.home = home;
379
+ lo = home;
380
+ es.home = home;
134
381
  }
135
- added.push({ id: ha, fitting: { kind: 'rod-holder', ...base, ...drive, ...slotNut, end: 'a' }, at: `${bid}.${q.name}@${r3(from)}`, guideOf: id });
136
- added.push({ id: hb, fitting: { kind: 'rod-holder', ...base, ...drive, ...slotNut, end: 'b' }, at: `${bid}.${q.name}@${r3(from + length)}`, guideOf: id });
137
- 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; });
138
- added.push({ id: cid, fitting: { kind: 'rod-carriage', ...base, ...(L.screw ? { screw: L.screw.set, leadNut: L.nut.set } : {}), ...(flag ? { flag } : {}) }, guideOf: id });
139
- 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; });
140
- 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: [] } };
382
+ added.push({
383
+ id: ha,
384
+ fitting: { kind: 'rod-holder', ...base, ...drive, ...slotNut, end: 'a' },
385
+ at: `${bid}.${q.name}@${r3(from)}`,
386
+ guideOf: id,
387
+ });
388
+ added.push({
389
+ id: hb,
390
+ fitting: { kind: 'rod-holder', ...base, ...drive, ...slotNut, end: 'b' },
391
+ at: `${bid}.${q.name}@${r3(from + length)}`,
392
+ guideOf: id,
393
+ });
394
+ const rods = [1, 2].map((k) => {
395
+ const rid = ids_(`${id}-rod-${k}`);
396
+ added.push({ id: rid, bought: L.rod.set, set: { length: r3(length) }, at: `${ha}.r${k}`, guideOf: id });
397
+ return rid;
398
+ });
399
+ added.push({
400
+ id: cid,
401
+ fitting: {
402
+ kind: 'rod-carriage',
403
+ ...base,
404
+ ...(L.screw ? { screw: L.screw.set, leadNut: L.nut.set } : {}),
405
+ ...(flag ? { flag } : {}),
406
+ },
407
+ guideOf: id,
408
+ });
409
+ const lms = [1, 2].map((k) => {
410
+ const lid = ids_(`${id}-bearing-${k}`);
411
+ added.push({ id: lid, bought: L.bearing.set, at: `${cid}.lm${k}`, guideOf: id });
412
+ return lid;
413
+ });
414
+ const centre = at(F.yc, L.height),
415
+ jspec = {
416
+ id: jid,
417
+ type: 'prismatic',
418
+ parent: { item: bid, at: [centre[0], centre[1], r3(c0)], axis: [0, 0, 1], ref: [n[0], n[1], 0] },
419
+ child: `${cid}.slide`,
420
+ min: lo,
421
+ max: travel0,
422
+ label: `${cid} slides along ${bid} on ${L.rod.set} rods`,
423
+ _guide: { id, kind: 'rods', carriage: cid, model: cm, terms: [] },
424
+ };
141
425
  joints.push(jspec);
142
426
  // the rods pressed into the holders, run through the bearings; the bearings pressed into the carriage
143
- 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' });
144
- 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' }));
145
- 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: [] };
427
+ for (const r of rods)
428
+ fits.push(
429
+ { a: r, b: ha, fastens: true, min: -100, note: 'The rod pressed through its holder' },
430
+ { a: r, b: hb, fastens: true, min: -100, note: 'The rod pressed through its holder' },
431
+ { a: r, b: cid, min: -100, note: "The rod runs through the carriage's bearing" },
432
+ );
433
+ lms.forEach((x, k) =>
434
+ fits.push(
435
+ { a: x, b: cid, fastens: true, min: -100, note: 'The bearing pressed into its pocket' },
436
+ { a: rods[k], b: x, min: -100, note: 'Rod in bearing' },
437
+ ),
438
+ );
439
+ const record = {
440
+ id,
441
+ kind: 'rods',
442
+ beam: bid,
443
+ face: q.face,
444
+ from,
445
+ length,
446
+ model: cm,
447
+ layout: L,
448
+ holder: hA,
449
+ carriages: [cid],
450
+ joints: [jid],
451
+ travel: r3(travel0 - lo),
452
+ lo,
453
+ wheels: [],
454
+ rods,
455
+ bearings: lms,
456
+ note: g.note ?? null,
457
+ fasten: [],
458
+ };
146
459
  // the holders' legs into the slots either side
147
460
  // (a saddle's screws, one fasten a leg: each leg's screws into its side's slot)
148
- const legFastens = (hid, m, said, gid) => [...new Set(m.screws.map((x) => x.side))].forEach((sd) => {
149
- const list = m.screws.map((x, k) => [x, k]).filter(([x]) => x.side === sd), x = list[0][0];
150
- 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 });
151
- });
152
- for (const [hid, m] of [[ha, hA], [hb, hold('b')]]) legFastens(hid, m, `${id}: ${hid}'s leg`, id);
461
+ const legFastens = (hid, m, said, gid) =>
462
+ [...new Set(m.screws.map((x) => x.side))].forEach((sd) => {
463
+ const list = m.screws.map((x, k) => [x, k]).filter(([x]) => x.side === sd),
464
+ x = list[0][0];
465
+ fasten.push({
466
+ screw: `${x.thread} ${x.head}`,
467
+ through: [hid],
468
+ into: `${bid}.${sd}`,
469
+ at: list.map(([, k]) => `${hid}.b${k + 1}`),
470
+ nut: x.nutKind,
471
+ note: `${said} into ${bid}'s ${sd} slot`,
472
+ guideOf: gid,
473
+ });
474
+ });
475
+ for (const [hid, m] of [
476
+ [ha, hA],
477
+ [hb, hold('b')],
478
+ ])
479
+ legFastens(hid, m, `${id}: ${hid}'s leg`, id);
153
480
  if (sc) {
154
481
  // the motor plate outside holder a, the coupler between, the screw from the coupler to holder b's bearing
155
- 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 };
156
- const pm = fittingModel(pspec, context, material, sc.what), cl = L.coupler.length, before = r3(pm.anchors.coupler.at[0] + cl + gap / 2);
482
+ const pspec = {
483
+ kind: 'motor-plate',
484
+ profile: P.name,
485
+ face: q.face,
486
+ motor: L.motor.set,
487
+ height: L.height,
488
+ coupler: { length: L.coupler.length, D: L.coupler.D },
489
+ ...slotNut,
490
+ };
491
+ const pm = fittingModel(pspec, context, material, sc.what),
492
+ cl = L.coupler.length,
493
+ before = r3(pm.anchors.coupler.at[0] + cl + gap / 2);
157
494
  const xm = r3(from - before);
158
- 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)}`);
159
- 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`);
160
- 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);
161
- const friction = sg.friction != null ? sg.friction : L.screw.friction ?? 0.2;
495
+ if (xm < 0)
496
+ throw new Error(
497
+ `${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)}`,
498
+ );
499
+ const plate = ids_(`${sc.id}-plate`),
500
+ mo = ids_(`${sc.id}-motor`),
501
+ cp = ids_(`${sc.id}-coupler`),
502
+ sw = ids_(`${sc.id}-screw`),
503
+ nt = ids_(`${sc.id}-nut`);
504
+ const start = xm + pm.anchors.coupler.at[0],
505
+ end = r3(from + length - hA.thickness + (L.support ? L.support.width : hA.thickness)),
506
+ slen = r3(end - start - cl / 2);
507
+ const friction = sg.friction != null ? sg.friction : (L.screw.friction ?? 0.2);
162
508
  added.push({ id: plate, fitting: pspec, at: `${bid}.${q.name}@${xm}`, guideOf: sc.id });
163
- 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 });
164
- 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 });
165
- 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 });
166
- added.push({ id: nt, bought: L.nut.set, _guide: { part: 'nut', ...L.nut, d: L.screw.d }, at: `${cid}.nut`, guideOf: sc.id });
167
- 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; }) : [];
168
- 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' },
169
- { 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' },
170
- { 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' }]));
509
+ added.push({
510
+ id: mo,
511
+ motor: { ...sg.motor, drives: jid, leadscrew: { lead: L.screw.lead, diameter: L.screw.d, friction } },
512
+ at: `${plate}.motor`,
513
+ guideOf: sc.id,
514
+ });
515
+ added.push({
516
+ id: cp,
517
+ bought: L.coupler.set,
518
+ _guide: {
519
+ part: 'coupler',
520
+ set: L.coupler.set,
521
+ length: cl,
522
+ D: L.coupler.D,
523
+ bores: [L.motor.shaft.d, L.screw.d],
524
+ },
525
+ at: `${plate}.coupler`,
526
+ guideOf: sc.id,
527
+ });
528
+ added.push({
529
+ id: sw,
530
+ bought: L.screw.set,
531
+ _guide: { part: 'screw', set: L.screw.set, d: L.screw.d, from: cl / 2, length: slen, lead: L.screw.lead },
532
+ at: `${plate}.coupler`,
533
+ guideOf: sc.id,
534
+ });
535
+ added.push({
536
+ id: nt,
537
+ bought: L.nut.set,
538
+ _guide: { part: 'nut', ...L.nut, d: L.screw.d },
539
+ at: `${cid}.nut`,
540
+ guideOf: sc.id,
541
+ });
542
+ const sup = L.support
543
+ ? ['a', 'b'].map((e) => {
544
+ const x = ids_(`${sc.id}-support-${e}`);
545
+ added.push({ id: x, bought: L.support.set, at: `${e === 'a' ? ha : hb}.s1`, guideOf: sc.id });
546
+ return x;
547
+ })
548
+ : [];
549
+ fits.push(
550
+ { a: mo, b: plate, fastens: true, min: -0.1, note: "The motor's face on its plate" },
551
+ { a: mo, b: cp, min: -100, note: "The motor's shaft in the coupler" },
552
+ { a: sw, b: cp, min: -100, note: 'The screw in the coupler' },
553
+ { a: sw, b: nt, min: -100, note: 'The screw through its nut' },
554
+ { a: sw, b: cid, min: -100, note: 'The screw through the carriage' },
555
+ { a: nt, b: cid, fastens: true, min: -100, note: 'The nut in its bore, its flange screwed on' },
556
+ { a: sw, b: ha, min: -100, note: 'The screw through holder a' },
557
+ { a: sw, b: hb, min: -100, note: 'The screw into holder b' },
558
+ ...sup.flatMap((x, k) => [
559
+ { a: x, b: k ? hb : ha, fastens: true, min: -100, note: "The screw's bearing pressed into its holder" },
560
+ { a: sw, b: x, min: -100, note: 'The screw in its bearing' },
561
+ ]),
562
+ );
171
563
  legFastens(plate, pm, `${sc.id}: the motor plate's leg`, sc.id);
172
564
  jspec._guide.screw = sc.id;
173
- 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: [] });
565
+ guides.push({
566
+ id: sc.id,
567
+ kind: 'screw',
568
+ guide: id,
569
+ beam: bid,
570
+ plate: pm,
571
+ motor: mo,
572
+ items: { plate, coupler: cp, screw: sw, nut: nt, support: sup },
573
+ length: slen,
574
+ lead: L.screw.lead,
575
+ friction: num(friction),
576
+ frictionKind: sg.friction != null ? 'given' : L.screw.frictionKind,
577
+ layout: L,
578
+ from: xm,
579
+ carriages: [],
580
+ joints: [jid],
581
+ wheels: [],
582
+ note: sg.note ?? null,
583
+ fasten: [],
584
+ });
174
585
  }
175
586
  if (es) {
176
587
  added.push({ id: es.mount, fitting: es.spec, at: es.at, guideOf: id });
177
588
  added.push({ id: es.sw, bought: es.set, at: `${es.mount}.switch`, guideOf: id });
178
- 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' });
179
- 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 }));
589
+ fits.push(
590
+ { a: es.sw, b: es.mount, fastens: true, min: -0.1, note: 'The switch screwed to its mount' },
591
+ { a: es.sw, b: cid, min: -1, note: 'The flag on the lever, at home' },
592
+ );
593
+ es.model.screws.forEach((x, k) =>
594
+ fasten.push({
595
+ screw: `${x.thread} ${x.head}`,
596
+ through: [es.mount],
597
+ into: `${bid}.${es.side}`,
598
+ at: [`${es.mount}.b${k + 1}`],
599
+ nut: x.nutKind,
600
+ note: `${id}: the endstop's mount into ${bid}'s ${es.side} slot`,
601
+ guideOf: id,
602
+ }),
603
+ );
180
604
  jspec.home = { endstop: `${es.sw}.lever`, direction: -1 };
181
605
  record.endstop = { mount: es.mount, switch: es.sw, at: es.at, home: es.home, model: es.model };
182
606
  }
183
- poses[`${id}-a`] = { [jid]: lo }; poses[`${id}-b`] = { [jid]: travel0 };
607
+ poses[`${id}-a`] = { [jid]: lo };
608
+ poses[`${id}-b`] = { [jid]: travel0 };
184
609
  guides.push(record);
185
610
  };
186
611
  list.forEach((g, i) => {
187
612
  const what = `guide ${g?.id ?? i + 1}`;
188
- 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)})`);
189
- 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) screw (a lead screw and its motor driving a rods guide's carriage) or belt (a timing belt and its motor driving a rail guide's carriage) (got ${shown(g.kind)})`);
613
+ if (!g || typeof g !== 'object' || Array.isArray(g))
614
+ throw new Error(
615
+ `${what} is { "kind": "rail", "rail": "rail-mgn12", "on": "x1.top", "length": 300 } (got ${shown(g)})`,
616
+ );
617
+ if (!KEYS[g.kind])
618
+ throw new Error(
619
+ `${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) screw (a lead screw and its motor driving a rods guide's carriage) or belt (a timing belt and its motor driving a rail guide's carriage) (got ${shown(g.kind)})`,
620
+ );
190
621
  if (g.kind === 'screw' || g.kind === 'belt') return; // (a screw worked out with the rods guide it drives, a belt once the rail it drives is)
191
- 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(', ')}`);
622
+ for (const k of Object.keys(g))
623
+ if (!KEYS[g.kind].includes(k))
624
+ throw new Error(`${what}: a ${g.kind} guide has no "${k}": it takes ${KEYS[g.kind].join(', ')}`);
192
625
  const id = g.id ?? `g${i + 1}`;
193
- 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 _)'}`);
626
+ if (!/^[a-z][\w-]*$/.test(id) || ids.has(id))
627
+ throw new Error(
628
+ `${what}: id ${shown(id)} is ${ids.has(id) ? 'an item already' : 'not a name (letters, digits, - or _)'}`,
629
+ );
194
630
  ids.add(id);
195
- const on = String(g.on ?? ''), dot = on.indexOf('.'), bid = on.slice(0, dot), faceName = on.slice(dot + 1);
196
- 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)})`);
197
- 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'})`);
198
- const B = beamOf(beams.get(bid), num, context).model, q = B.places.find((x) => x.name === faceName || (x.face === faceName && x.index === 1));
199
- if (!q) throw new Error(`${what}: ${bid} has no face "${faceName}": it has ${[...new Set(B.places.map((x) => x.face))].join(', ')}`);
200
- if (g.kind === 'rods') { rodsGuide(g, { what, id, bid, B, q, faceName }); return; }
201
- if (g.kind !== 'rail') { rolling(g, { what, id, bid, B, q, faceName }); return; }
202
- 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`);
203
- 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;
631
+ const on = String(g.on ?? ''),
632
+ dot = on.indexOf('.'),
633
+ bid = on.slice(0, dot),
634
+ faceName = on.slice(dot + 1);
635
+ if (dot < 0 || on.includes('@'))
636
+ throw new Error(`${what}: on is a beam's slotted face, as x1.top; where along it is "from" (got ${shown(g.on)})`);
637
+ if (!beams.has(bid))
638
+ throw new Error(
639
+ `${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'})`,
640
+ );
641
+ const B = beamOf(beams.get(bid), num, context).model,
642
+ q = B.places.find((x) => x.name === faceName || (x.face === faceName && x.index === 1));
643
+ if (!q)
644
+ throw new Error(
645
+ `${what}: ${bid} has no face "${faceName}": it has ${[...new Set(B.places.map((x) => x.face))].join(', ')}`,
646
+ );
647
+ if (g.kind === 'rods') {
648
+ rodsGuide(g, { what, id, bid, B, q, faceName });
649
+ return;
650
+ }
651
+ if (g.kind !== 'rail') {
652
+ rolling(g, { what, id, bid, B, q, faceName });
653
+ return;
654
+ }
655
+ if (!q.slot)
656
+ throw new Error(
657
+ `${what}: ${bid}'s ${faceName} face is closed: a rail's screws go into slot nuts, so it goes on a slotted face`,
658
+ );
659
+ const from = g.from != null ? num(g.from) : 0,
660
+ length = num(g.length),
661
+ face = B.places.filter((x) => x.face === q.face).length > 1 ? q.name : q.face;
204
662
  let m;
205
- 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)}`); }
206
- 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`);
663
+ try {
664
+ m = railModel(g.rail, {
665
+ length,
666
+ ...(g.carriage != null ? { variant: g.carriage } : {}),
667
+ ...(g.carriages != null ? { carriages: g.carriages } : {}),
668
+ given,
669
+ });
670
+ } catch (e) {
671
+ throw new Error(`${what}: ${strip(e)}`);
672
+ }
673
+ if (!(from >= 0 && from + length <= B.length + 1e-9))
674
+ throw new Error(
675
+ `${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`,
676
+ );
207
677
  // what crosses its face: a beam joined to it there, by the join's address
208
678
  const keep = [];
209
679
  for (const j of a.joins ?? []) {
210
- const [jb, jat] = String(j?.b ?? '').split('@'), [jbid, ...jf] = jb.split('.'), other = String(j?.a ?? '').split('.')[0];
680
+ const [jb, jat] = String(j?.b ?? '').split('@'),
681
+ [jbid, ...jf] = jb.split('.'),
682
+ other = String(j?.a ?? '').split('.')[0];
211
683
  if (jbid !== bid || jat == null || ![q.name, q.face].includes(jf.join('.')) || !beams.has(other)) continue;
212
- 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;
684
+ const d = /%$/.test(jat)
685
+ ? (parseFloat(jat) / 100) * B.length
686
+ : parseFloat(jat) < 0 || String(jat).startsWith('-')
687
+ ? B.length + parseFloat(jat)
688
+ : parseFloat(jat),
689
+ o = beamOf(beams.get(other), num, context).model.profile,
690
+ half = Math.max(o.width, o.height) / 2;
213
691
  keep.push({ from: r3(d - half), to: r3(d + half), what: `${other} joined at ${j.b}` });
214
692
  }
215
- const fit = guideFit(m, B.profile, { face, from, beam: B.length, keep, ...(g.nut ? { nut: g.nut } : {}), given: givenFor(context, B.profile.name) });
216
- const rid = id, record = { id, kind: 'rail', beam: bid, face: q.name, from, length, model: m, fit, carriages: [], joints: [], keep, note: g.note ?? null };
217
- added.push({ id: rid, bought: m.set, _guide: { part: 'rail', model: m }, at: `${bid}.${q.name}@${r3(from)}`, guideOf: id });
693
+ const fit = guideFit(m, B.profile, {
694
+ face,
695
+ from,
696
+ beam: B.length,
697
+ keep,
698
+ ...(g.nut ? { nut: g.nut } : {}),
699
+ given: givenFor(context, B.profile.name),
700
+ });
701
+ const rid = id,
702
+ record = {
703
+ id,
704
+ kind: 'rail',
705
+ beam: bid,
706
+ face: q.name,
707
+ from,
708
+ length,
709
+ model: m,
710
+ fit,
711
+ carriages: [],
712
+ joints: [],
713
+ keep,
714
+ note: g.note ?? null,
715
+ };
716
+ added.push({
717
+ id: rid,
718
+ bought: m.set,
719
+ _guide: { part: 'rail', model: m },
720
+ at: `${bid}.${q.name}@${r3(from)}`,
721
+ guideOf: id,
722
+ });
218
723
  // its screws into the slot, at its holes: only where a nut is made for them (else guide.fit says why); or, with
219
724
  // "nut": "printed", into hex nuts in a printed carrier strip in the slot under the rail, a nut under each hole
220
725
  if (fit.carrier) {
221
- const nid = `${id}-nuts`, c = fit.carrier;
726
+ const nid = `${id}-nuts`,
727
+ c = fit.carrier;
222
728
  if (ids.has(nid)) throw new Error(`${what}: ${nid} is an item already: give the guide another id`);
223
729
  ids.add(nid);
224
- added.push({ id: nid, fitting: { kind: 'nut-carrier', profile: B.profile.name, face, thread: m.rail.thread, count: c.count, ...(c.count > 1 ? { pitch: c.pitch } : {}) }, at: `${bid}.${q.name}@${r3(from + m.holes[0] - c.at[0])}`, guideOf: id });
225
- fasten.push({ screw: `${m.rail.thread} cap`, through: [rid], into: nid, at: m.screws.map((x) => `${rid}.${x.name}`), note: `${id}: the rail into the hex nuts in ${nid}, a screw every ${mm(m.rail.pitch)} mm`, guideOf: id });
226
- fits.push({ a: nid, b: bid, fastens: true, min: -100, note: `${nid} in ${bid}'s ${q.name} slot, held there by the rail's screws` });
730
+ added.push({
731
+ id: nid,
732
+ fitting: {
733
+ kind: 'nut-carrier',
734
+ profile: B.profile.name,
735
+ face,
736
+ thread: m.rail.thread,
737
+ count: c.count,
738
+ ...(c.count > 1 ? { pitch: c.pitch } : {}),
739
+ },
740
+ at: `${bid}.${q.name}@${r3(from + m.holes[0] - c.at[0])}`,
741
+ guideOf: id,
742
+ });
743
+ fasten.push({
744
+ screw: `${m.rail.thread} cap`,
745
+ through: [rid],
746
+ into: nid,
747
+ at: m.screws.map((x) => `${rid}.${x.name}`),
748
+ note: `${id}: the rail into the hex nuts in ${nid}, a screw every ${mm(m.rail.pitch)} mm`,
749
+ guideOf: id,
750
+ });
751
+ fits.push({
752
+ a: nid,
753
+ b: bid,
754
+ fastens: true,
755
+ min: -100,
756
+ note: `${nid} in ${bid}'s ${q.name} slot, held there by the rail's screws`,
757
+ });
227
758
  }
228
- 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 });
759
+ if (fit.nut)
760
+ fasten.push({
761
+ screw: `${m.rail.thread} cap`,
762
+ through: [rid],
763
+ into: `${bid}.${q.name}`,
764
+ at: m.screws.map((x) => `${rid}.${x.name}`),
765
+ nut: g.nut ?? fit.nut.nutKind,
766
+ note: `${id}: the rail into ${bid}'s ${q.name} slot, a screw every ${mm(m.rail.pitch)} mm`,
767
+ guideOf: id,
768
+ });
229
769
  // the carriages, each on its own slide along the beam, from the start of its share of the rail
230
- 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; } })();
770
+ const normal = [q.normal[0], q.normal[1], 0],
771
+ terms = (() => {
772
+ try {
773
+ return railTerms(m, B.profile, { beam: B.length, face, given: givenFor(context, B.profile.name) });
774
+ } catch {
775
+ return m.terms;
776
+ }
777
+ })();
231
778
  m.lanes.forEach((ln, k) => {
232
- const cid = `${id}-carriage-${k + 1}`, jid = `${id}-${k + 1}`;
233
- 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`);
779
+ const cid = `${id}-carriage-${k + 1}`,
780
+ jid = `${id}-${k + 1}`;
781
+ if (ids.has(cid) || ids.has(jid))
782
+ throw new Error(`${what}: ${ids.has(cid) ? cid : jid} is an item already: give the guide another id`);
234
783
  ids.add(cid);
235
784
  added.push({ id: cid, bought: m.set, variant: m.variant, _guide: { part: 'carriage', model: m }, guideOf: id });
236
- 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 } });
237
- record.carriages.push(cid); record.joints.push(jid);
785
+ joints.push({
786
+ id: jid,
787
+ type: 'prismatic',
788
+ parent: { item: bid, at: [q.at[0], q.at[1], r3(from + ln.rest)], axis: [0, 0, 1], ref: normal },
789
+ child: `${cid}.slide`,
790
+ min: 0,
791
+ max: ln.travel,
792
+ label: `${cid} slides along ${bid}`,
793
+ _guide: { id, carriage: cid, model: m, terms },
794
+ });
795
+ record.carriages.push(cid);
796
+ record.joints.push(jid);
238
797
  });
239
- poses[`${id}-a`] = Object.fromEntries(record.joints.map((j) => [j, 0])); poses[`${id}-b`] = Object.fromEntries(record.joints.map((j) => [j, m.travel]));
798
+ poses[`${id}-a`] = Object.fromEntries(record.joints.map((j) => [j, 0]));
799
+ poses[`${id}-b`] = Object.fromEntries(record.joints.map((j) => [j, m.travel]));
240
800
  record.terms = terms;
241
- 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: /, '') }; }
801
+ try {
802
+ record.jig = railJigModel(B.profile, { rail: m.set, face, given: givenFor(context, B.profile.name) });
803
+ } catch (e) {
804
+ record.jig = { error: String(e.message).replace(/^railJigModel: /, '') };
805
+ }
242
806
  guides.push(record);
243
807
  });
244
808
  // a belt drives a rail guide's one carriage: worked out once the rail is, from its carriage's place and travel
245
809
  const belts = new Set();
246
810
  list.forEach((g, i) => {
247
811
  if (g?.kind !== 'belt') return;
248
- const id = g.id ?? `g${i + 1}`, what = `guide ${g.id ?? i + 1}`;
249
- for (const k of Object.keys(g)) if (!KEYS.belt.includes(k)) throw new Error(`${what}: a belt guide has no "${k}": it takes ${KEYS.belt.join(', ')}`);
250
- 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 _)'}`);
251
- const named_ = list.map((x, k) => x?.id ?? `g${k + 1}`), target = guides.find((x) => x.id === g.guide);
252
- if (!target) throw new Error(`${what}: guide names the rail guide whose carriage the belt drives, by its id (got ${shown(g.guide)}; guides: ${named_.join(', ')})`);
253
- if (target.kind !== 'rail') throw new Error(`${what}: a belt drives a rail guide's carriage, and ${g.guide} is a ${target.kind} guide; a rods guide is driven by a screw`);
254
- if (target.carriages.length !== 1) throw new Error(`${what}: a belt drives one carriage, and ${g.guide} has ${target.carriages.length}: give it "carriages": 1`);
255
- if (belts.has(g.guide)) throw new Error(`${what}: ${g.guide} is driven by another belt already: one drive to a carriage`);
812
+ const id = g.id ?? `g${i + 1}`,
813
+ what = `guide ${g.id ?? i + 1}`;
814
+ for (const k of Object.keys(g))
815
+ if (!KEYS.belt.includes(k))
816
+ throw new Error(`${what}: a belt guide has no "${k}": it takes ${KEYS.belt.join(', ')}`);
817
+ if (!/^[a-z][\w-]*$/.test(id) || ids.has(id))
818
+ throw new Error(
819
+ `${what}: id ${shown(id)} is ${ids.has(id) ? 'an item already' : 'not a name (letters, digits, - or _)'}`,
820
+ );
821
+ const named_ = list.map((x, k) => x?.id ?? `g${k + 1}`),
822
+ target = guides.find((x) => x.id === g.guide);
823
+ if (!target)
824
+ throw new Error(
825
+ `${what}: guide names the rail guide whose carriage the belt drives, by its id (got ${shown(g.guide)}; guides: ${named_.join(', ')})`,
826
+ );
827
+ if (target.kind !== 'rail')
828
+ throw new Error(
829
+ `${what}: a belt drives a rail guide's carriage, and ${g.guide} is a ${target.kind} guide; a rods guide is driven by a screw`,
830
+ );
831
+ if (target.carriages.length !== 1)
832
+ throw new Error(
833
+ `${what}: a belt drives one carriage, and ${g.guide} has ${target.carriages.length}: give it "carriages": 1`,
834
+ );
835
+ if (belts.has(g.guide))
836
+ throw new Error(`${what}: ${g.guide} is driven by another belt already: one drive to a carriage`);
256
837
  belts.add(g.guide);
257
- 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": "nema17", "microsteps": 16 }: its drives and its belt come from the guide (got ${shown(g.motor)})`);
258
- 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`);
838
+ if (!g.motor || typeof g.motor !== 'object' || typeof g.motor.model !== 'string')
839
+ throw new Error(
840
+ `${what}: motor is the motor item's spec, as { "model": "nema17", "microsteps": 16 }: its drives and its belt come from the guide (got ${shown(g.motor)})`,
841
+ );
842
+ for (const k of ['drives', 'leadscrew', 'belt'])
843
+ if (g.motor[k] != null) throw new Error(`${what}: motor.${k} comes from the guide: leave it out`);
259
844
  ids.add(id);
260
- const bid = target.beam, B = beamOf(beams.get(bid), num, context).model, q = B.places.find((x) => x.name === target.face), face = B.places.filter((x) => x.face === q.face).length > 1 ? q.name : q.face;
261
- const m = target.model, c = m.carriage, ln = m.lanes[0], opt = (k) => (g[k] != null ? { [k]: num(g[k]) } : {}), set = (k) => (g[k] != null ? { [k]: String(g[k]) } : {});
845
+ const bid = target.beam,
846
+ B = beamOf(beams.get(bid), num, context).model,
847
+ q = B.places.find((x) => x.name === target.face),
848
+ face = B.places.filter((x) => x.face === q.face).length > 1 ? q.name : q.face;
849
+ const m = target.model,
850
+ c = m.carriage,
851
+ ln = m.lanes[0],
852
+ opt = (k) => (g[k] != null ? { [k]: num(g[k]) } : {}),
853
+ set = (k) => (g[k] != null ? { [k]: String(g[k]) } : {});
262
854
  let L;
263
855
  try {
264
- L = beltLayout(B.profile.name, { face, beam: B.length, rail: { from: target.from, length: target.length, width: m.rail.width, height: m.rail.height }, carriage: { top: c.height, width: c.width, length: c.lengthMax ?? c.length, clearance: c.clearance, holes: c.holes, thread: c.thread, depth: c.depth, rest: ln.rest, travel: ln.travel },
265
- motor: g.motor.model, ...set('pulley'), ...set('idler'), ...set('belt'), ...set('nut'), ...opt('tension'), ...opt('across'), ...opt('idlerAt'), ...opt('cut'), ...opt('height'), material, given: givenFor(context, B.profile.name) });
266
- } catch (e) { throw new Error(`${what}: ${strip(e)}`); }
267
- const own = (x) => { if (ids.has(x)) throw new Error(`${what}: ${x} is an item already: give the guide another id`); ids.add(x); return x; };
268
- const plate = own(`${id}-plate`), mo = own(`${id}-motor`), pu = own(`${id}-pulley`), cl = own(`${id}-clamp`), blk = own(`${id}-idler-block`), il = own(`${id}-idler`), bt = own(`${id}-belt`);
269
- const cid = target.carriages[0], jid = target.joints[0], S = L.sets, at0 = `${bid}.${q.name}@0`;
270
- added.push({ id: plate, fitting: { kind: 'belt-plate', profile: B.profile.name, ...L.specs.plate }, at: at0, guideOf: id });
271
- added.push({ id: mo, motor: { ...g.motor, drives: jid, belt: { teeth: S.pulley.teeth, pitch: S.belt.pitch, stiffness: S.belt.kw, width: S.belt.width, length: L.span, load: 0 } }, at: `${plate}.motor`, guideOf: id });
272
- added.push({ id: pu, bought: S.pulley.set, _guide: { part: 'pulley', ...S.pulley }, at: `${plate}.pulley`, guideOf: id });
856
+ L = beltLayout(B.profile.name, {
857
+ face,
858
+ beam: B.length,
859
+ rail: { from: target.from, length: target.length, width: m.rail.width, height: m.rail.height },
860
+ carriage: {
861
+ top: c.height,
862
+ width: c.width,
863
+ length: c.lengthMax ?? c.length,
864
+ clearance: c.clearance,
865
+ holes: c.holes,
866
+ thread: c.thread,
867
+ depth: c.depth,
868
+ rest: ln.rest,
869
+ travel: ln.travel,
870
+ },
871
+ motor: g.motor.model,
872
+ ...set('pulley'),
873
+ ...set('idler'),
874
+ ...set('belt'),
875
+ ...set('nut'),
876
+ ...opt('tension'),
877
+ ...opt('across'),
878
+ ...opt('idlerAt'),
879
+ ...opt('cut'),
880
+ ...opt('height'),
881
+ material,
882
+ given: givenFor(context, B.profile.name),
883
+ });
884
+ } catch (e) {
885
+ throw new Error(`${what}: ${strip(e)}`);
886
+ }
887
+ const own = (x) => {
888
+ if (ids.has(x)) throw new Error(`${what}: ${x} is an item already: give the guide another id`);
889
+ ids.add(x);
890
+ return x;
891
+ };
892
+ const plate = own(`${id}-plate`),
893
+ mo = own(`${id}-motor`),
894
+ pu = own(`${id}-pulley`),
895
+ cl = own(`${id}-clamp`),
896
+ blk = own(`${id}-idler-block`),
897
+ il = own(`${id}-idler`),
898
+ bt = own(`${id}-belt`);
899
+ const cid = target.carriages[0],
900
+ jid = target.joints[0],
901
+ S = L.sets,
902
+ at0 = `${bid}.${q.name}@0`;
903
+ added.push({
904
+ id: plate,
905
+ fitting: { kind: 'belt-plate', profile: B.profile.name, ...L.specs.plate },
906
+ at: at0,
907
+ guideOf: id,
908
+ });
909
+ added.push({
910
+ id: mo,
911
+ motor: {
912
+ ...g.motor,
913
+ drives: jid,
914
+ belt: {
915
+ teeth: S.pulley.teeth,
916
+ pitch: S.belt.pitch,
917
+ stiffness: S.belt.kw,
918
+ width: S.belt.width,
919
+ length: L.span,
920
+ load: 0,
921
+ },
922
+ },
923
+ at: `${plate}.motor`,
924
+ guideOf: id,
925
+ });
926
+ added.push({
927
+ id: pu,
928
+ bought: S.pulley.set,
929
+ _guide: { part: 'pulley', ...S.pulley },
930
+ at: `${plate}.pulley`,
931
+ guideOf: id,
932
+ });
273
933
  added.push({ id: cl, fitting: { kind: 'belt-clamp', ...L.specs.clamp }, at: `${cid}.top`, guideOf: id });
274
- added.push({ id: blk, fitting: { kind: 'belt-idler', profile: B.profile.name, ...L.specs.idler }, at: `${bid}.${q.name}@${L.idler.block}`, guideOf: id });
934
+ added.push({
935
+ id: blk,
936
+ fitting: { kind: 'belt-idler', profile: B.profile.name, ...L.specs.idler },
937
+ at: `${bid}.${q.name}@${L.idler.block}`,
938
+ guideOf: id,
939
+ });
275
940
  added.push({ id: il, bought: S.idler.set, _guide: { part: 'idler', ...S.idler }, at: `${blk}.axle`, guideOf: id });
276
941
  // the belt itself: drawn where the carriage is at the start of its travel, left out of the overlap check (its runs
277
942
  // move with the carriage, so belt.rub holds them to the parts they pass through the whole travel)
278
- const drawn = L.drawn(target.from + ln.rest), tb = S.belt.thickness;
279
- added.push({ id: bt, bought: S.belt.set, _guide: { part: 'belt', set: S.belt.set, length: L.buy, cut: L.cut, height: L.height, width: S.belt.width, thickness: tb, runs: drawn.runs, wraps: drawn.wraps }, at: at0, guideOf: id });
280
- const pm = L.plate, bm = L.block, cm = L.clamp, screwsOf = (x, mdl) => mdl.screws.map((_, k) => `${x}.b${k + 1}`);
281
- fasten.push({ screw: `${pm.thread} ${pm.head}`, through: [plate], into: `${bid}.${q.name}`, at: screwsOf(plate, pm), nut: g.nut ?? pm.screws[0].nutKind, note: `${id}: the motor's plate into ${bid}'s ${q.name} slot`, guideOf: id });
282
- fasten.push({ screw: `${cm.thread} cap`, through: [cl], into: cid, at: screwsOf(cl, cm), note: `${id}: the belt's clamp into ${cid}'s tapped holes`, guideOf: id });
283
- fasten.push({ screw: `${bm.thread} cap`, through: [blk], into: `${bid}.${q.name}`, at: screwsOf(blk, bm), nut: g.nut ?? bm.screws[0].nutKind, note: `${id}: the idler's block into ${bid}'s ${q.name} slot, slid along it to tension the belt`, guideOf: id });
284
- fits.push({ a: mo, b: plate, fastens: true, min: -0.1, note: 'The motor\'s face under its plate' }, { a: mo, b: pu, min: -100, note: 'The motor\'s shaft in the pulley' }, { a: il, b: blk, fastens: true, min: -0.1, note: 'The idler on its axle, on the block\'s ring' });
943
+ const drawn = L.drawn(target.from + ln.rest),
944
+ tb = S.belt.thickness;
945
+ added.push({
946
+ id: bt,
947
+ bought: S.belt.set,
948
+ _guide: {
949
+ part: 'belt',
950
+ set: S.belt.set,
951
+ length: L.buy,
952
+ cut: L.cut,
953
+ height: L.height,
954
+ width: S.belt.width,
955
+ thickness: tb,
956
+ runs: drawn.runs,
957
+ wraps: drawn.wraps,
958
+ },
959
+ at: at0,
960
+ guideOf: id,
961
+ });
962
+ const pm = L.plate,
963
+ bm = L.block,
964
+ cm = L.clamp,
965
+ screwsOf = (x, mdl) => mdl.screws.map((_, k) => `${x}.b${k + 1}`);
966
+ fasten.push({
967
+ screw: `${pm.thread} ${pm.head}`,
968
+ through: [plate],
969
+ into: `${bid}.${q.name}`,
970
+ at: screwsOf(plate, pm),
971
+ nut: g.nut ?? pm.screws[0].nutKind,
972
+ note: `${id}: the motor's plate into ${bid}'s ${q.name} slot`,
973
+ guideOf: id,
974
+ });
975
+ fasten.push({
976
+ screw: `${cm.thread} cap`,
977
+ through: [cl],
978
+ into: cid,
979
+ at: screwsOf(cl, cm),
980
+ note: `${id}: the belt's clamp into ${cid}'s tapped holes`,
981
+ guideOf: id,
982
+ });
983
+ fasten.push({
984
+ screw: `${bm.thread} cap`,
985
+ through: [blk],
986
+ into: `${bid}.${q.name}`,
987
+ at: screwsOf(blk, bm),
988
+ nut: g.nut ?? bm.screws[0].nutKind,
989
+ note: `${id}: the idler's block into ${bid}'s ${q.name} slot, slid along it to tension the belt`,
990
+ guideOf: id,
991
+ });
992
+ fits.push(
993
+ { a: mo, b: plate, fastens: true, min: -0.1, note: "The motor's face under its plate" },
994
+ { a: mo, b: pu, min: -100, note: "The motor's shaft in the pulley" },
995
+ { a: il, b: blk, fastens: true, min: -0.1, note: "The idler on its axle, on the block's ring" },
996
+ );
285
997
  const spec = joints.find((x) => x.id === jid);
286
998
  if (spec) spec._guide.belt = id;
287
- guides.push({ id, kind: 'belt', guide: g.guide, on: bid, face: q.name, layout: L, rub: beltRub(L), motor: mo, carriage: cid, items: { plate, motor: mo, pulley: pu, clamp: cl, block: blk, idler: il, belt: bt },
288
- accel: g.accel != null ? num(g.accel) : null, tensionGiven: g.tension != null, carriages: [], joints: [jid], wheels: [], note: g.note ?? null, fasten: [] });
999
+ guides.push({
1000
+ id,
1001
+ kind: 'belt',
1002
+ guide: g.guide,
1003
+ on: bid,
1004
+ face: q.name,
1005
+ layout: L,
1006
+ rub: beltRub(L),
1007
+ motor: mo,
1008
+ carriage: cid,
1009
+ items: { plate, motor: mo, pulley: pu, clamp: cl, block: blk, idler: il, belt: bt },
1010
+ accel: g.accel != null ? num(g.accel) : null,
1011
+ tensionGiven: g.tension != null,
1012
+ carriages: [],
1013
+ joints: [jid],
1014
+ wheels: [],
1015
+ note: g.note ?? null,
1016
+ fasten: [],
1017
+ });
289
1018
  });
290
- 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 } };
1019
+ const out = {
1020
+ ...a,
1021
+ items: [...items, ...added],
1022
+ ...(fasten.length ? { fasten: [...(a.fasten || []), ...fasten] } : {}),
1023
+ ...(fits.length ? { fits: [...(a.fits || []), ...fits] } : {}),
1024
+ joints: [...(a.joints || []), ...joints],
1025
+ poses: { ...(a.poses || {}), ...poses },
1026
+ };
291
1027
  for (const r of guides) r.fasten = fasten.map((x, k) => (x.guideOf === r.id ? first + k : -1)).filter((k) => k >= 0);
292
1028
  return { a: out, guides };
293
1029
  }
@@ -302,51 +1038,221 @@ export function guideBuilt(it, id) {
302
1038
  // a belt drive's bought parts: the pulley and the idler, drawn round their own z from their sets' figures; the belt
303
1039
  // along its path as drawn, in its frame on the face (out of the overlap check: belt.rub holds it through the travel)
304
1040
  if (part === 'pulley' || part === 'idler') {
305
- const g = it._guide, body = subtract(translate([0, 0, g.body / 2], cylinder({ radius: g.od / 2, height: g.body, segments: 48 })), translate([0, 0, g.body / 2], cylinder({ radius: g.bore / 2, height: g.body + 2, segments: 24 })));
306
- const parts = named({ body }, { body: { label: `${g.set}` } }), anchors = { origin: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } };
307
- return referenceBuilt({ id, key: `guide-${part}`, objName: `${part}:${g.set}`, name: g.set, parts, anchors, entries: [], values: { pitch_diameter: g.pd, outer_diameter: g.od, body_width: g.body, bore: g.bore, ...(g.teeth ? { teeth: g.teeth } : {}) }, extra: { bought: { set: g.set, variant: null, part, mass: null } } });
1041
+ const g = it._guide,
1042
+ body = subtract(
1043
+ translate([0, 0, g.body / 2], cylinder({ radius: g.od / 2, height: g.body, segments: 48 })),
1044
+ translate([0, 0, g.body / 2], cylinder({ radius: g.bore / 2, height: g.body + 2, segments: 24 })),
1045
+ );
1046
+ const parts = named({ body }, { body: { label: `${g.set}` } }),
1047
+ anchors = { origin: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } };
1048
+ return referenceBuilt({
1049
+ id,
1050
+ key: `guide-${part}`,
1051
+ objName: `${part}:${g.set}`,
1052
+ name: g.set,
1053
+ parts,
1054
+ anchors,
1055
+ entries: [],
1056
+ values: {
1057
+ pitch_diameter: g.pd,
1058
+ outer_diameter: g.od,
1059
+ body_width: g.body,
1060
+ bore: g.bore,
1061
+ ...(g.teeth ? { teeth: g.teeth } : {}),
1062
+ },
1063
+ extra: { bought: { set: g.set, variant: null, part, mass: null } },
1064
+ });
308
1065
  }
309
1066
  if (part === 'belt') {
310
- const g = it._guide, z0 = g.height - g.width / 2, t = g.thickness, pieces = [];
1067
+ const g = it._guide,
1068
+ z0 = g.height - g.width / 2,
1069
+ t = g.thickness,
1070
+ pieces = [];
311
1071
  for (const r of g.runs) {
312
- const dx = r.to[0] - r.from[0], dy = r.to[1] - r.from[1], len = Math.hypot(dx, dy);
1072
+ const dx = r.to[0] - r.from[0],
1073
+ dy = r.to[1] - r.from[1],
1074
+ len = Math.hypot(dx, dy);
313
1075
  if (len < 0.01) continue;
314
- pieces.push(translate([(r.from[0] + r.to[0]) / 2, (r.from[1] + r.to[1]) / 2, g.height], rotateZ(Math.atan2(dy, dx), cuboid({ size: [len, t, g.width] }))));
1076
+ pieces.push(
1077
+ translate(
1078
+ [(r.from[0] + r.to[0]) / 2, (r.from[1] + r.to[1]) / 2, g.height],
1079
+ rotateZ(Math.atan2(dy, dx), cuboid({ size: [len, t, g.width] })),
1080
+ ),
1081
+ );
315
1082
  }
316
- for (const w of g.wraps) pieces.push(translate([w.at[0], w.at[1], 0], extrudeRotate({ segments: 48, startAngle: w.from, angle: Math.min(w.angle, 2 * Math.PI - 1e-3) }, polygon({ points: [[w.r - t / 2, z0], [w.r + t / 2, z0], [w.r + t / 2, z0 + g.width], [w.r - t / 2, z0 + g.width]] }))));
317
- const parts = named({ body: union(...pieces) }, { body: { label: `${g.set} x ${mm(g.cut)} mm, cut from ${mm(g.length)} mm` } });
1083
+ for (const w of g.wraps)
1084
+ pieces.push(
1085
+ translate(
1086
+ [w.at[0], w.at[1], 0],
1087
+ extrudeRotate(
1088
+ { segments: 48, startAngle: w.from, angle: Math.min(w.angle, 2 * Math.PI - 1e-3) },
1089
+ polygon({
1090
+ points: [
1091
+ [w.r - t / 2, z0],
1092
+ [w.r + t / 2, z0],
1093
+ [w.r + t / 2, z0 + g.width],
1094
+ [w.r - t / 2, z0 + g.width],
1095
+ ],
1096
+ }),
1097
+ ),
1098
+ ),
1099
+ );
1100
+ const parts = named(
1101
+ { body: union(...pieces) },
1102
+ { body: { label: `${g.set} x ${mm(g.cut)} mm, cut from ${mm(g.length)} mm` } },
1103
+ );
318
1104
  parts[0].collide = jscad.geometries.geom3.create();
319
- return referenceBuilt({ id, key: 'guide-belt', objName: `belt:${g.set}:${g.cut}`, name: g.set, parts, anchors: { origin: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } }, entries: [], values: { length: g.length, cut: g.cut, width: g.width }, extra: { bought: { set: g.set, variant: null, part: 'belt', mass: null, length: g.length } } });
1105
+ return referenceBuilt({
1106
+ id,
1107
+ key: 'guide-belt',
1108
+ objName: `belt:${g.set}:${g.cut}`,
1109
+ name: g.set,
1110
+ parts,
1111
+ anchors: { origin: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } },
1112
+ entries: [],
1113
+ values: { length: g.length, cut: g.cut, width: g.width },
1114
+ extra: { bought: { set: g.set, variant: null, part: 'belt', mass: null, length: g.length } },
1115
+ });
320
1116
  }
321
1117
  // a screw drive's bought parts, drawn from their sets' figures along their own z: the coupler, the screw, the lead nut
322
1118
  if (part === 'coupler' || part === 'screw' || part === 'nut') {
323
- const g = it._guide, rod = (r, z0, z1, seg = 48) => translate([0, 0, (z0 + z1) / 2], cylinder({ radius: r, height: z1 - z0, segments: seg }));
324
- 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))
325
- : 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));
326
- const label = part === 'coupler' ? `${g.set} coupler` : part === 'screw' ? `${g.set} x ${mm(g.length)} mm` : `${g.set} lead nut`;
327
- const parts = named({ body }, { body: { label } }), anchors = { origin: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } };
328
- 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 };
329
- 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³)
330
- 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 } : {}) } } });
1119
+ const g = it._guide,
1120
+ rod = (r, z0, z1, seg = 48) =>
1121
+ translate([0, 0, (z0 + z1) / 2], cylinder({ radius: r, height: z1 - z0, segments: seg }));
1122
+ const body =
1123
+ part === 'coupler'
1124
+ ? subtract(
1125
+ rod(g.D / 2, 0, g.length),
1126
+ rod(g.bores[0] / 2, -1, g.length / 2),
1127
+ rod(g.bores[1] / 2, g.length / 2, g.length + 1),
1128
+ )
1129
+ : part === 'screw'
1130
+ ? rod(g.d / 2, g.from, g.from + g.length)
1131
+ : subtract(
1132
+ union(rod(g.flange / 2, 0, g.flangeWidth), rod(g.body / 2, g.flangeWidth - g.length, 0.01)),
1133
+ rod(g.d / 2, -g.length - 1, g.flangeWidth + 1),
1134
+ );
1135
+ const label =
1136
+ part === 'coupler'
1137
+ ? `${g.set} coupler`
1138
+ : part === 'screw'
1139
+ ? `${g.set} x ${mm(g.length)} mm`
1140
+ : `${g.set} lead nut`;
1141
+ const parts = named({ body }, { body: { label } }),
1142
+ anchors = { origin: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } };
1143
+ const values =
1144
+ part === 'screw'
1145
+ ? { length: g.length, diameter: g.d, lead: g.lead }
1146
+ : part === 'coupler'
1147
+ ? { length: g.length, diameter: g.D }
1148
+ : { flange: g.flange, length: g.length };
1149
+ 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³)
1150
+ return referenceBuilt({
1151
+ id,
1152
+ key: `guide-${part}`,
1153
+ objName: `${part}:${g.set}${part === 'screw' ? `:${g.length}` : ''}`,
1154
+ name: g.set,
1155
+ parts,
1156
+ anchors,
1157
+ entries: [],
1158
+ values,
1159
+ extra: {
1160
+ bought: {
1161
+ set: g.set,
1162
+ variant: null,
1163
+ part: part === 'screw' ? 'leadscrew' : part,
1164
+ mass,
1165
+ ...(part === 'screw' ? { length: g.length } : {}),
1166
+ },
1167
+ },
1168
+ });
331
1169
  }
332
1170
  if (part === 'wheel') {
333
- const parts = named({ body: vWheel(m, { eccentric: it._guide.eccentric }) }, { body: { label: `${m.wheel} on ${it._guide.eccentric ? m.eccentric : 'a plain spacer'}` } });
1171
+ const parts = named(
1172
+ { body: vWheel(m, { eccentric: it._guide.eccentric }) },
1173
+ { body: { label: `${m.wheel} on ${it._guide.eccentric ? m.eccentric : 'a plain spacer'}` } },
1174
+ );
334
1175
  // (the whole wheel is checked for overlaps: its rim runs in the track, which the beam's envelope leaves open, so a rim
335
1176
  // that strays out of it, or a beam or part across its track's line, is found)
336
- 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 };
337
- 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 } } });
1177
+ const anchors = { axle: { at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } },
1178
+ values = {
1179
+ diameter: SETS_OF(m).diameter,
1180
+ axle: m.seat.axle,
1181
+ depth: m.seat.depth,
1182
+ radial: m.ratings.radial,
1183
+ axial: m.ratings.axial,
1184
+ };
1185
+ return referenceBuilt({
1186
+ id,
1187
+ key: 'guide-wheel',
1188
+ objName: `wheel:${m.wheel}${it._guide.eccentric ? ':eccentric' : ''}`,
1189
+ name: m.wheel,
1190
+ parts,
1191
+ anchors,
1192
+ entries: [],
1193
+ values,
1194
+ extra: { guide: m, bought: { set: m.wheel, variant: null, part: 'wheel', mass: m.kitMass } },
1195
+ });
338
1196
  }
339
1197
  if (part === 'rail') {
340
1198
  const parts = named({ body: rail(m) }, { body: { label: `${m.set} rail x ${mm(m.length)} mm` } });
341
- 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 }])) };
1199
+ const anchors = {
1200
+ a: { at: [0, 0, 0], axis: [-1, 0, 0], ref: [0, 0, 1] },
1201
+ top: { at: [m.length / 2, 0, m.rail.height], axis: [0, 0, 1], ref: [1, 0, 0] },
1202
+ ...Object.fromEntries(m.screws.map((x) => [x.name, { at: x.at, axis: x.axis }])),
1203
+ };
342
1204
  const values = { length: m.length, holes: m.holes.length, pitch: m.rail.pitch, end: m.rail.end, mass: m.rail.mass };
343
- 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 } } });
1205
+ return referenceBuilt({
1206
+ id,
1207
+ key: 'guide-rail',
1208
+ objName: `rail:${m.set}`,
1209
+ name: m.set,
1210
+ parts,
1211
+ anchors,
1212
+ entries: [],
1213
+ values,
1214
+ extra: { guide: m, bought: { set: m.set, variant: null, part: 'rail', mass: m.rail.mass, length: m.length } },
1215
+ });
344
1216
  }
345
- const c = m.carriage, parts = named({ body: carriage(m) }, { body: { label: `${m.set} carriage ${c.variant ?? ''}`.trim() } });
346
- 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] }])) };
347
- 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 } : {}) };
1217
+ const c = m.carriage,
1218
+ parts = named({ body: carriage(m) }, { body: { label: `${m.set} carriage ${c.variant ?? ''}`.trim() } });
1219
+ const anchors = {
1220
+ slide: { at: [0, 0, 0], axis: [1, 0, 0], ref: [0, 0, 1] },
1221
+ top: { at: [0, 0, c.height], axis: [0, 0, 1], ref: [1, 0, 0] },
1222
+ ...Object.fromEntries(c.holes.map(([x, y], k) => [`t${k + 1}`, { at: [x, y, c.height], axis: [0, 0, -1] }])),
1223
+ };
1224
+ const values = {
1225
+ length: c.length,
1226
+ width: c.width,
1227
+ height: c.height,
1228
+ travel: m.travel,
1229
+ static_rating: c.ratings.C0,
1230
+ moment_roll: c.ratings.MR,
1231
+ moment_pitch: c.ratings.MP,
1232
+ moment_yaw: c.ratings.MY,
1233
+ ...(c.mass != null ? { mass: c.mass } : {}),
1234
+ };
348
1235
  // (its tapped holes are its mount: a fasten into the carriage ends in its own thread, held to the depth its maker gives)
349
- 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, mount: { thread: c.thread, holes: c.holes, reach: c.depth } } } });
1236
+ return referenceBuilt({
1237
+ id,
1238
+ key: 'guide-carriage',
1239
+ objName: `carriage:${m.set}:${c.variant}`,
1240
+ name: `${m.set} ${c.variant}`,
1241
+ parts,
1242
+ anchors,
1243
+ entries: [],
1244
+ values,
1245
+ extra: {
1246
+ guide: m,
1247
+ bought: {
1248
+ set: m.set,
1249
+ variant: c.variant,
1250
+ part: 'carriage',
1251
+ mass: c.mass ?? null,
1252
+ mount: { thread: c.thread, holes: c.holes, reach: c.depth },
1253
+ },
1254
+ },
1255
+ });
350
1256
  }
351
1257
 
352
1258
  /**
@@ -357,32 +1263,116 @@ export function guideBuilt(it, id) {
357
1263
  export function guideFindings(s, f) {
358
1264
  for (const r of s.guides ?? []) {
359
1265
  if (r.kind === 'rods') {
360
- const m = r.model, L = r.layout, refs = { guide: r.id, beam: r.beam, carriages: r.carriages, rods: r.rods, bearings: r.bearings };
361
- 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 });
362
- 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 });
363
- 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 } });
1266
+ const m = r.model,
1267
+ L = r.layout,
1268
+ refs = { guide: r.id, beam: r.beam, carriages: r.carriages, rods: r.rods, bearings: r.bearings };
1269
+ f(
1270
+ 'info',
1271
+ `${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` : ''}`,
1272
+ null,
1273
+ {
1274
+ code: 'guide.travel',
1275
+ values: {
1276
+ from: r.from,
1277
+ length: r.length,
1278
+ own: m.own,
1279
+ travel: r.travel,
1280
+ home: r.lo,
1281
+ spacing: L.spacing,
1282
+ height: L.height,
1283
+ },
1284
+ refs,
1285
+ },
1286
+ );
1287
+ f(
1288
+ 'info',
1289
+ `${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)})`,
1290
+ null,
1291
+ { code: 'guide.rating', values: { static: L.bearing.C0, dynamic: L.bearing.C }, refs },
1292
+ );
1293
+ if (r.endstop)
1294
+ f(
1295
+ 'info',
1296
+ `${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)`,
1297
+ null,
1298
+ {
1299
+ code: 'guide.endstop',
1300
+ values: { home: r.endstop.home, lever: r.endstop.model.lever },
1301
+ refs: { ...refs, switch: r.endstop.switch, mount: r.endstop.mount },
1302
+ },
1303
+ );
1304
+ continue;
1305
+ }
1306
+ if (r.kind === 'belt') {
1307
+ beltFindings(r, f);
364
1308
  continue;
365
1309
  }
366
- if (r.kind === 'belt') { beltFindings(r, f); continue; }
367
1310
  if (r.kind === 'screw') {
368
- const L = r.layout, refs = { guide: r.id, drives: r.guide, motor: r.motor, ...r.items };
369
- 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 });
370
- const engaged = r.plate.engaged, want = L.motor.shaft.d;
371
- 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 });
1311
+ const L = r.layout,
1312
+ refs = { guide: r.id, drives: r.guide, motor: r.motor, ...r.items };
1313
+ f(
1314
+ 'info',
1315
+ `${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`,
1316
+ null,
1317
+ {
1318
+ code: 'guide.drive',
1319
+ values: { lead: r.lead, length: r.length, friction: r.friction, engaged: r.plate.engaged },
1320
+ refs,
1321
+ },
1322
+ );
1323
+ const engaged = r.plate.engaged,
1324
+ want = L.motor.shaft.d;
1325
+ f(
1326
+ engaged >= want - 1e-9 ? 'pass' : 'warn',
1327
+ `${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'}`,
1328
+ engaged >= want - 1e-9 ? null : `${r.id}: shaft short`,
1329
+ { code: 'guide.coupler', values: { engaged, want }, refs },
1330
+ );
372
1331
  continue;
373
1332
  }
374
1333
  if (r.kind === 'wheels' || r.kind === 'slider') {
375
- const m = r.model, refs = { guide: r.id, beam: r.beam, carriages: r.carriages, ...(r.wheels.length ? { wheels: r.wheels } : {}) };
376
- 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 });
1334
+ const m = r.model,
1335
+ refs = { guide: r.id, beam: r.beam, carriages: r.carriages, ...(r.wheels.length ? { wheels: r.wheels } : {}) };
1336
+ f(
1337
+ 'info',
1338
+ `${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`,
1339
+ null,
1340
+ { code: 'guide.travel', values: { from: r.from, length: r.length, own: m.length, travel: r.travel }, refs },
1341
+ );
377
1342
  continue;
378
1343
  }
379
1344
  const refs = { guide: r.id, beam: r.beam, rail: r.model.set, carriages: r.carriages };
380
- 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 });
381
- 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 ?? {}) } });
1345
+ for (const x of r.model.findings)
1346
+ f(x.level, `${r.id}: ${x.text}`, x.level === 'warn' || x.level === 'fail' ? `${r.id}: ${x.code}` : null, {
1347
+ code: x.code,
1348
+ values: x.values ?? {},
1349
+ refs,
1350
+ });
1351
+ for (const x of r.fit.findings)
1352
+ f(x.level, `${r.id}: ${x.text}`, x.level === 'fail' ? `${r.id}: fit` : null, {
1353
+ code: x.code,
1354
+ values: x.values ?? {},
1355
+ refs: { ...refs, ...(x.refs ?? {}) },
1356
+ });
382
1357
  const t = r.terms ?? [];
383
- 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 });
1358
+ if (t.length)
1359
+ f(
1360
+ 'info',
1361
+ `${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(
1362
+ t.reduce((a, x) => a + x.value, 0),
1363
+ 3,
1364
+ )} mm at worst. The beam's sag is an estimate (${GUIDE_DEFAULTS.sag.rule.split(':')[0].toLowerCase()})`,
1365
+ null,
1366
+ { code: 'guide.budget', values: { terms: t, worst: t.reduce((a, x) => a + x.value, 0) }, refs },
1367
+ );
384
1368
  if (r.jig?.error) f('info', `${r.id}: no printed jig: ${r.jig.error}`, null, { code: 'guide.jig', refs });
385
- 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 });
1369
+ else if (r.jig)
1370
+ f(
1371
+ 'info',
1372
+ `${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}" } }`,
1373
+ null,
1374
+ { code: 'guide.jig', values: { grams: r.jig.grams, margin: r.jig.margin }, refs },
1375
+ );
386
1376
  }
387
1377
  }
388
1378
 
@@ -394,61 +1384,244 @@ export function guideFindings(s, f) {
394
1384
  * @param {{ placeItems: Function, jointValues: Function }} helpers @param {{ masses: object[], beyond: Function, poses: object[] }} known
395
1385
  */
396
1386
  export function carriageChecks(s, f, helpers, { masses, beyond, poses }) {
397
- 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);
1387
+ const motion = s.motion,
1388
+ { placeItems } = helpers,
1389
+ ids = motion.items.map((x) => x[0]),
1390
+ g = motion.gravity || [0, 0, -1],
1391
+ gl = Math.hypot(...g),
1392
+ down = g.map((x) => x / gl);
398
1393
  const specs = new Map((s.a.joints || []).filter((j) => j._guide).map((j) => [j.id, j]));
399
1394
  for (const j of motion.joints) {
400
- const spec = specs.get(j.id); if (!spec || spec._guide.kind === 'slider') continue;
401
- 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 }));
1395
+ const spec = specs.get(j.id);
1396
+ if (!spec || spec._guide.kind === 'slider') continue;
1397
+ const m = spec._guide.model,
1398
+ wheels = spec._guide.kind === 'wheels',
1399
+ rods = spec._guide.kind === 'rods',
1400
+ ci = ids.indexOf(spec._guide.carriage),
1401
+ hang = (motion.loads || [])
1402
+ .filter((l) => l.mass != null)
1403
+ .map((l) => ({ kg: l.mass, item: typeof l.item === 'number' ? l.item : ids.indexOf(l.item), frame: l.frame }));
402
1404
  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)
403
1405
  let worst = null;
404
1406
  for (const p of poses) {
405
- 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]);
406
- let F = [0, 0, 0], M = [0, 0, 0], kg = 0;
407
- 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; };
408
- 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])); }
409
- 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])); }
1407
+ const W = placeItems(motion, p.values),
1408
+ C = W[ci],
1409
+ 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]);
1410
+ let F = [0, 0, 0],
1411
+ M = [0, 0, 0],
1412
+ kg = 0;
1413
+ const add = (mass, X) => {
1414
+ const fo = down.map((x) => x * mass * G),
1415
+ r = [X[0] - O[0], X[1] - O[1], X[2] - O[2]];
1416
+ F = F.map((x, k) => x + fo[k]);
1417
+ M = [
1418
+ M[0] + (r[1] * fo[2] - r[2] * fo[1]) / 1000,
1419
+ M[1] + (r[2] * fo[0] - r[0] * fo[2]) / 1000,
1420
+ M[2] + (r[0] * fo[1] - r[1] * fo[0]) / 1000,
1421
+ ];
1422
+ kg += mass;
1423
+ };
1424
+ for (const q of masses)
1425
+ if (q.item === ci || beyond(motion, j, q.item)) {
1426
+ const Wq = W[q.item];
1427
+ add(
1428
+ q.kg,
1429
+ [0, 1, 2].map(
1430
+ (k) =>
1431
+ Wq[4 * k] * q.centre[0] + Wq[4 * k + 1] * q.centre[1] + Wq[4 * k + 2] * q.centre[2] + Wq[4 * k + 3],
1432
+ ),
1433
+ );
1434
+ }
1435
+ for (const l of hang)
1436
+ if (l.item === ci || beyond(motion, j, l.item)) {
1437
+ const Wq = W[l.item],
1438
+ at = [l.frame[3], l.frame[7], l.frame[11]];
1439
+ add(
1440
+ l.kg,
1441
+ [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]),
1442
+ );
1443
+ }
410
1444
  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)
411
- const load = { force: local(F), moment: local(M) }, r = rods ? rodMargin(m, load) : wheels ? wheelMargin(m, load) : carriageMargin(m, load);
1445
+ const load = { force: local(F), moment: local(M) },
1446
+ r = rods ? rodMargin(m, load) : wheels ? wheelMargin(m, load) : carriageMargin(m, load);
412
1447
  if (!worst || r.margin < worst.r.margin) worst = { r, load, p, kg, O };
413
1448
  }
414
1449
  if (!worst) continue;
415
- const { r, load, p, kg, O } = worst, [, Fy, Fz] = load.force, [Mx, My, Mz] = load.moment;
1450
+ const { r, load, p, kg, O } = worst,
1451
+ [, Fy, Fz] = load.force,
1452
+ [Mx, My, Mz] = load.moment;
416
1453
  if (rods) {
417
1454
  const b = r.bearings.find((x) => x.name === r.worst);
418
1455
  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)`;
419
- 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 } });
1456
+ f(r.level, text, r.level === 'pass' ? null : `${j.id}: bearings ${n3(r.margin, 1)}×`, {
1457
+ code: 'joint.moment',
1458
+ at: O,
1459
+ refs: { joint: j.id, carriage: spec._guide.carriage, guide: spec._guide.id, bearing: m.bearing },
1460
+ values: {
1461
+ force: load.force,
1462
+ moment: load.moment,
1463
+ bearings: r.bearings,
1464
+ worst: r.worst,
1465
+ margin: r.margin,
1466
+ carried: kg,
1467
+ pose: p.label,
1468
+ rating: r.rating,
1469
+ },
1470
+ });
420
1471
  continue;
421
1472
  }
422
1473
  if (wheels) {
423
- const w = r.wheels.find((x) => x.name === r.worst), rt = m.ratings;
1474
+ const w = r.wheels.find((x) => x.name === r.worst),
1475
+ rt = m.ratings;
424
1476
  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)`;
425
- 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 } } });
1477
+ f(r.level, text, r.level === 'pass' ? null : `${j.id}: wheels ${n3(r.margin, 1)}×`, {
1478
+ code: 'joint.moment',
1479
+ at: O,
1480
+ refs: { joint: j.id, carriage: spec._guide.carriage, guide: spec._guide.id, wheel: m.wheel },
1481
+ values: {
1482
+ force: load.force,
1483
+ moment: load.moment,
1484
+ wheels: r.wheels,
1485
+ worst: r.worst,
1486
+ margin: r.margin,
1487
+ carried: kg,
1488
+ pose: p.label,
1489
+ ratings: { radial: rt.radial, axial: rt.axial },
1490
+ },
1491
+ });
426
1492
  continue;
427
1493
  }
428
1494
  const c = m.carriage.ratings;
429
1495
  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)`;
430
- 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 } });
1496
+ f(r.level, text, r.level === 'pass' ? null : `${j.id}: carriage ${n3(r.margin, 1)}×`, {
1497
+ code: 'joint.moment',
1498
+ at: O,
1499
+ refs: { joint: j.id, carriage: spec._guide.carriage, guide: spec._guide.id, rail: m.set },
1500
+ values: {
1501
+ force: load.force,
1502
+ moment: load.moment,
1503
+ ratios: r.ratios,
1504
+ margin: r.margin,
1505
+ carried: kg,
1506
+ pose: p.label,
1507
+ ratings: c,
1508
+ },
1509
+ });
431
1510
  }
432
1511
  }
433
1512
 
434
1513
  // A belt drive's findings: what it is and its ratio, its length to cut and to buy, its tension, whether its runs rub
435
1514
  // what they pass through the travel, whether it lies square, and what is left of the tensioner's travel
436
1515
  function beltFindings(r, f) {
437
- const L = r.layout, S = L.sets, B = S.belt, refs = { guide: r.id, drives: r.guide, carriage: r.carriage, ...r.items }, T = L.tension;
438
- f('info', `${r.id}: ${S.pulley.set} on ${r.motor} under its printed plate past ${r.on}'s end a, ${B.set} round ${S.idler.set} on its printed block at ${mm(L.idler.block)} mm, both ends in the clamp on ${r.carriage}: ${S.pulley.teeth} teeth × ${mm(B.pitch)} mm is ${mm(L.mmPerTurn)} mm of travel a turn, the belt ${mm(L.height)} mm over the face; the motor's checks read it as a belt`, null, { code: 'guide.drive', values: { mmPerTurn: L.mmPerTurn, teeth: S.pulley.teeth, pitch: B.pitch, height: L.height }, refs });
439
- f('info', `${r.id}: the belt's path is ${mm(L.length)} mm by its pitch line (the working runs, ${mm(L.pulley.r * 2)} and ${mm(L.idler.r * 2)} mm pitch circles, the return run and the ends in the clamp); cut it ${mm(L.cut)} mm, ${mm(L.cut / B.pitch)} teeth, which stretches ${mm(L.stretch)} mm at ${mm(T)} N; buy ${mm(L.buy / 1000)} m (${BELT_DEFAULTS.sold.rule.split(':')[0].toLowerCase()}: an estimate)`, null, { code: 'belt.length', values: { path: L.length, cut: L.cut, buy: L.buy, stretch: L.stretch, teeth: L.cut / B.pitch }, refs });
440
- // (the belt carries the push and, off level, the weight's part along the travel: the two together at their worst)
441
- const push = r.force ?? 0, W = r.weight ?? 0, F = push + W, slack = T - F / 2, tight = T + F / 2, lo = B.least ?? 0, hi = B.rated ?? Infinity, ok = slack >= lo - 1e-9 && tight <= hi + 1e-9;
442
- f(ok ? 'pass' : 'fail', `${r.id}: each span at ${mm(T)} N (${r.tensionGiven ? 'given' : 'BELT_DEFAULTS.tension, an estimate'}); pushing ${r.carried != null ? `${n3(r.carried * 1000, 0)} g at ${n3(r.accelUsed, 0)} mm/s²` : 'the carriage'} takes ${n3(push, 2)} N${W ? `, and its weight along the travel ${n3(W, 2)} N more` : ''}, so the spans run from ${n3(slack, 2)} to ${n3(tight, 2)} N, ${ok ? 'within' : 'outside'} ${B.set}'s ${mm(lo)} N least static and ${mm(hi)} N rated tension${ok ? '' : slack < lo ? ': more tension' : `: less tension or less ${W ? 'weight or ' : ''}acceleration`}`, ok ? null : `${r.id}: belt tension`, { code: 'belt.tension', values: { tension: T, force: F, push, weight: W, slack, tight, least: lo, rated: hi }, refs });
1516
+ const L = r.layout,
1517
+ S = L.sets,
1518
+ B = S.belt,
1519
+ refs = { guide: r.id, drives: r.guide, carriage: r.carriage, ...r.items },
1520
+ T = L.tension;
1521
+ f(
1522
+ 'info',
1523
+ `${r.id}: ${S.pulley.set} on ${r.motor} under its printed plate past ${r.on}'s end a, ${B.set} round ${S.idler.set} on its printed block at ${mm(L.idler.block)} mm, both ends in the clamp on ${r.carriage}: ${S.pulley.teeth} teeth × ${mm(B.pitch)} mm is ${mm(L.mmPerTurn)} mm of travel a turn, the belt ${mm(L.height)} mm over the face; the motor's checks read it as a belt`,
1524
+ null,
1525
+ {
1526
+ code: 'guide.drive',
1527
+ values: { mmPerTurn: L.mmPerTurn, teeth: S.pulley.teeth, pitch: B.pitch, height: L.height },
1528
+ refs,
1529
+ },
1530
+ );
1531
+ f(
1532
+ 'info',
1533
+ `${r.id}: the belt's path is ${mm(L.length)} mm by its pitch line (the working runs, ${mm(L.pulley.r * 2)} and ${mm(L.idler.r * 2)} mm pitch circles, the return run and the ends in the clamp); cut it ${mm(L.cut)} mm, ${mm(L.cut / B.pitch)} teeth, which stretches ${mm(L.stretch)} mm at ${mm(T)} N; buy ${mm(L.buy / 1000)} m (${BELT_DEFAULTS.sold.rule.split(':')[0].toLowerCase()}: an estimate)`,
1534
+ null,
1535
+ {
1536
+ code: 'belt.length',
1537
+ values: { path: L.length, cut: L.cut, buy: L.buy, stretch: L.stretch, teeth: L.cut / B.pitch },
1538
+ refs,
1539
+ },
1540
+ );
1541
+ // (the belt carries the push and, off level, the weight's part along the travel: the two together at their worst.
1542
+ // The motor holds the pulley, so the spans share it by their lengths, the short one taking the most: beltSpans walks
1543
+ // the travel for where the spans differ most, which is at one of its ends)
1544
+ const push = r.force ?? 0,
1545
+ W = r.weight ?? 0,
1546
+ F = push + W,
1547
+ sp = beltSpans(L, { force: F }),
1548
+ { slack, tight, least: lo, rated: hi, ok } = sp,
1549
+ side = sp.span === 'a' ? "the pulley's side a" : "the idler's side b";
1550
+ f(
1551
+ ok ? 'pass' : 'fail',
1552
+ `${r.id}: each span at ${mm(T)} N (${r.tensionGiven ? 'given' : 'BELT_DEFAULTS.tension, an estimate'}); pushing ${r.carried != null ? `${n3(r.carried * 1000, 0)} g at ${n3(r.accelUsed, 0)} mm/s²` : 'the carriage'} takes ${n3(push, 2)} N${W ? `, and its weight along the travel ${n3(W, 2)} N more` : ''}. The motor holds the pulley, so the two spans share that by their lengths, the shorter taking the more: at its worst, with the carriage at ${mm(sp.at)} mm along ${r.on}, ${side} is ${mm(Math.min(sp.a, sp.b))} mm of belt against ${mm(Math.max(sp.a, sp.b))} mm and takes ${n3(sp.share * 100, 0)}% of it, so the spans run from ${n3(slack, 2)} to ${n3(tight, 2)} N, ${ok ? 'within' : 'outside'} ${B.set}'s ${mm(lo)} N least static and ${mm(hi)} N rated tension${ok ? '' : slack < lo ? `: more tension, or less ${W ? 'weight or ' : ''}acceleration` : `: less tension or less ${W ? 'weight or ' : ''}acceleration`}`,
1553
+ ok ? null : `${r.id}: belt tension`,
1554
+ {
1555
+ code: 'belt.tension',
1556
+ values: {
1557
+ tension: T,
1558
+ force: F,
1559
+ push,
1560
+ weight: W,
1561
+ slack,
1562
+ tight,
1563
+ at: sp.at,
1564
+ span: sp.span,
1565
+ a: sp.a,
1566
+ b: sp.b,
1567
+ share: sp.share,
1568
+ least: lo,
1569
+ rated: hi,
1570
+ },
1571
+ refs,
1572
+ },
1573
+ );
443
1574
  // (what the carriage carries, by its box: a part on its free holes rubs the return run unless it stays under the belt's
444
1575
  // band or the belt is raised over it, height; the room under the band is said either way)
445
- const carried = r.carriedParts ?? [], under = r3(L.height - B.width / 2 - L.carriage.top), raise = (top) => Math.ceil((top + B.width / 2 + 1) * 2) / 2;
446
- for (const x of r.rub.rubs) f('fail', x.carried ? `${r.id}: ${x.run} rubs ${x.part}, which ${r.carriage} carries, ${mm(x.depth)} mm into its box with the carriage at ${mm(x.at)} mm along ${r.on}: the belt's band is ${mm(under)} mm over the carriage's top. Raise the belt over it with "height": ${mm(raise(x.top))} (1 mm clear of its ${mm(x.top)} mm top), or keep the part under the band or clear of the run across the carriage`
447
- : `${r.id}: ${x.run} rubs ${x.part}, ${mm(x.depth)} mm into it with the carriage at ${mm(x.at)} mm along ${r.on}: move the idler or the pulley back onto the belt's square line, or the part out of its way`, `${r.id}: belt rubs ${x.part}`, { code: 'belt.rub', values: { depth: x.depth, carriage: x.at, under, ...(x.carried ? { top: x.top, height: raise(x.top) } : {}) }, refs: { ...refs, run: x.run, part: x.part } });
448
- if (!r.rub.rubs.length) f('pass', `${r.id}: through the carriage's travel the belt's runs clear the beam, the rail, the carriage${carried.length ? `, the clamp and what the carriage carries (${carried.join(', ')}, by their boxes)` : ' and the clamp'}, by ${mm(r.rub.least.clearance)} mm at least (${r.rub.least.run} and ${r.rub.least.part}, the carriage at ${mm(r.rub.least.at)} mm; the belt ${mm(B.thickness)} mm thick, an estimate). The return run passes ${mm(under)} mm over the carriage's top: a part screwed to its free holes stays under that, or the belt goes higher ("height")`, null, { code: 'belt.rub', values: { clearance: r.rub.least.clearance, carriage: r.rub.least.at, under }, refs: { ...refs, ...(carried.length ? { carried } : {}) } });
449
- const lim = BELT_DEFAULTS.line.value, square = L.line <= lim + 1e-9;
450
- f(square ? 'pass' : 'fail', `${r.id}: the idler's top is ${mm(L.across)} mm off the pulley's line along the travel, ${n3(L.line, 3)}° off square, ${square ? 'within' : 'past'} the ${lim}° the belt is held to (BELT_DEFAULTS.line, an estimate)${square ? '' : ': move the idler back onto the line'}`, square ? null : `${r.id}: belt off square`, { code: 'belt.line', values: { angle: L.line, across: L.across, limit: lim }, refs });
451
- const d = L.idler.offset, room = d >= 0 ? L.idler.room.b : L.idler.room.a, way = d >= 0 ? 'toward the beam\'s end b' : 'toward the rail', fits = Math.abs(d) <= room + 1e-9;
452
- f(fits ? 'pass' : 'fail', `${r.id}: cut ${mm(L.cut)} mm, the belt sets its tension with the idler's block ${mm(Math.abs(d))} mm ${way} from where it is drawn, along the slot, which leaves it ${mm(room)} mm that way${fits ? `: ${mm(room - Math.abs(d))} mm of the tensioner's travel left` : `: the tensioner's travel is used up; cut the belt ${mm(up2(2 * (Math.abs(d) - room), B.pitch))} mm ${d >= 0 ? 'shorter' : 'longer'}, or move the block`}`, fits ? null : `${r.id}: tensioner used up`, { code: 'belt.tensioner', values: { offset: d, room: L.idler.room, left: r3(room - Math.abs(d)) }, refs });
453
- f('info', `${r.id}: the belt holds the carriage on two spans, the pulley's side a and the idler's side b; at its least stiff they act as ${mm(L.span)} mm of belt (a·b/(a+b), the most of it through the travel); at ${n3(B.k, 0)} N a unit of strain (${B.set}'s stiffness, ${B.kKind ?? 'its kind not given'}), ${n3(F, 2)} N${W ? ` (the push and the weight along the travel)` : ''} stretches it ${n3((F * L.span) / B.k, 4)} mm: the belt stretch term of the error budget where a task is given`, null, { code: 'belt.stretch', values: { span: L.span, stiffness: B.k, force: F, stretch: (F * L.span) / B.k }, refs });
1576
+ const carried = r.carriedParts ?? [],
1577
+ under = r3(L.height - B.width / 2 - L.carriage.top),
1578
+ raise = (top) => Math.ceil((top + B.width / 2 + 1) * 2) / 2;
1579
+ for (const x of r.rub.rubs)
1580
+ f(
1581
+ 'fail',
1582
+ x.carried
1583
+ ? `${r.id}: ${x.run} rubs ${x.part}, which ${r.carriage} carries, ${mm(x.depth)} mm into its box with the carriage at ${mm(x.at)} mm along ${r.on}: the belt's band is ${mm(under)} mm over the carriage's top. Raise the belt over it with "height": ${mm(raise(x.top))} (1 mm clear of its ${mm(x.top)} mm top), or keep the part under the band or clear of the run across the carriage`
1584
+ : `${r.id}: ${x.run} rubs ${x.part}, ${mm(x.depth)} mm into it with the carriage at ${mm(x.at)} mm along ${r.on}: move the idler or the pulley back onto the belt's square line, or the part out of its way`,
1585
+ `${r.id}: belt rubs ${x.part}`,
1586
+ {
1587
+ code: 'belt.rub',
1588
+ values: { depth: x.depth, carriage: x.at, under, ...(x.carried ? { top: x.top, height: raise(x.top) } : {}) },
1589
+ refs: { ...refs, run: x.run, part: x.part },
1590
+ },
1591
+ );
1592
+ if (!r.rub.rubs.length)
1593
+ f(
1594
+ 'pass',
1595
+ `${r.id}: through the carriage's travel the belt's runs clear the beam, the rail, the carriage${carried.length ? `, the clamp and what the carriage carries (${carried.join(', ')}, by their boxes)` : ' and the clamp'}, by ${mm(r.rub.least.clearance)} mm at least (${r.rub.least.run} and ${r.rub.least.part}, the carriage at ${mm(r.rub.least.at)} mm; the belt ${mm(B.thickness)} mm thick, an estimate). The return run passes ${mm(under)} mm over the carriage's top: a part screwed to its free holes stays under that, or the belt goes higher ("height")`,
1596
+ null,
1597
+ {
1598
+ code: 'belt.rub',
1599
+ values: { clearance: r.rub.least.clearance, carriage: r.rub.least.at, under },
1600
+ refs: { ...refs, ...(carried.length ? { carried } : {}) },
1601
+ },
1602
+ );
1603
+ const lim = BELT_DEFAULTS.line.value,
1604
+ square = L.line <= lim + 1e-9;
1605
+ f(
1606
+ square ? 'pass' : 'fail',
1607
+ `${r.id}: the idler's top is ${mm(L.across)} mm off the pulley's line along the travel, ${n3(L.line, 3)}° off square, ${square ? 'within' : 'past'} the ${lim}° the belt is held to (BELT_DEFAULTS.line, an estimate)${square ? '' : ': move the idler back onto the line'}`,
1608
+ square ? null : `${r.id}: belt off square`,
1609
+ { code: 'belt.line', values: { angle: L.line, across: L.across, limit: lim }, refs },
1610
+ );
1611
+ const d = L.idler.offset,
1612
+ room = d >= 0 ? L.idler.room.b : L.idler.room.a,
1613
+ way = d >= 0 ? "toward the beam's end b" : 'toward the rail',
1614
+ fits = Math.abs(d) <= room + 1e-9;
1615
+ f(
1616
+ fits ? 'pass' : 'fail',
1617
+ `${r.id}: cut ${mm(L.cut)} mm, the belt sets its tension with the idler's block ${mm(Math.abs(d))} mm ${way} from where it is drawn, along the slot, which leaves it ${mm(room)} mm that way${fits ? `: ${mm(room - Math.abs(d))} mm of the tensioner's travel left` : `: the tensioner's travel is used up; cut the belt ${mm(up2(2 * (Math.abs(d) - room), B.pitch))} mm ${d >= 0 ? 'shorter' : 'longer'}, or move the block`}`,
1618
+ fits ? null : `${r.id}: tensioner used up`,
1619
+ { code: 'belt.tensioner', values: { offset: d, room: L.idler.room, left: r3(room - Math.abs(d)) }, refs },
1620
+ );
1621
+ f(
1622
+ 'info',
1623
+ `${r.id}: the belt holds the carriage on two spans, the pulley's side a and the idler's side b; at its least stiff they act as ${mm(L.span)} mm of belt (a·b/(a+b), the most of it through the travel); at ${n3(B.k, 0)} N a unit of strain (${B.set}'s stiffness, ${B.kKind ?? 'its kind not given'}), ${n3(F, 2)} N${W ? ` (the push and the weight along the travel)` : ''} stretches it ${n3((F * L.span) / B.k, 4)} mm: the belt stretch term of the error budget where a task is given`,
1624
+ null,
1625
+ { code: 'belt.stretch', values: { span: L.span, stiffness: B.k, force: F, stretch: (F * L.span) / B.k }, refs },
1626
+ );
454
1627
  }