@rulecad/gears 0.3.5 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/planetary.mjs CHANGED
@@ -15,7 +15,12 @@ const fmt = (x, d = 3) => String(+x.toFixed(d));
15
15
  */
16
16
  export function planetaryRatio({ sun: S, ring: R }, { held, input, output }) {
17
17
  // ω for each member with the held one at 0, from ω_s − ω_c = −(R/S)(ω_r − ω_c)
18
- const w = held === 'ring' ? { ring: 0, carrier: 1, sun: 1 + R / S } : held === 'carrier' ? { carrier: 0, ring: 1, sun: -R / S } : { sun: 0, carrier: 1, ring: (R + S) / R };
18
+ const w =
19
+ held === 'ring'
20
+ ? { ring: 0, carrier: 1, sun: 1 + R / S }
21
+ : held === 'carrier'
22
+ ? { carrier: 0, ring: 1, sun: -R / S }
23
+ : { sun: 0, carrier: 1, ring: (R + S) / R };
19
24
  return w[input] / w[output];
20
25
  }
21
26
 
@@ -28,52 +33,107 @@ export function planetaryRatio({ sun: S, ring: R }, { held, input, output }) {
28
33
  */
29
34
  export function planetary(spec) {
30
35
  const what = 'planetary';
31
- if (!spec || typeof spec !== 'object') throw new Error(`${what}: give it { module, ratio, planets, held, input, output }`);
32
- const { module: m, ratio } = spec, n = spec.planets ?? 3, held = spec.held ?? 'ring', input = spec.input ?? 'sun';
33
- if (!(typeof m === 'number' && m > 0)) throw new Error(`${what}: module is a size above 0 (got ${JSON.stringify(m)})`);
34
- if (!(typeof ratio === 'number' && Number.isFinite(ratio) && ratio !== 0)) throw new Error(`${what}: ratio is turns of the input for one of the output, a number (got ${JSON.stringify(ratio)})`);
35
- if (!(Number.isInteger(n) && n >= 2 && n <= 8)) throw new Error(`${what}: planets is a whole number from 2 to 8 (got ${JSON.stringify(n)})`);
36
- for (const [k, v] of [['held', held], ['input', input]]) if (!MEMBERS.includes(v)) throw new Error(`${what}: ${k} is one of ${MEMBERS.join(', ')} (got ${JSON.stringify(v)})`);
36
+ if (!spec || typeof spec !== 'object')
37
+ throw new Error(`${what}: give it { module, ratio, planets, held, input, output }`);
38
+ const { module: m, ratio } = spec,
39
+ n = spec.planets ?? 3,
40
+ held = spec.held ?? 'ring',
41
+ input = spec.input ?? 'sun';
42
+ if (!(typeof m === 'number' && m > 0))
43
+ throw new Error(`${what}: module is a size above 0 (got ${JSON.stringify(m)})`);
44
+ if (!(typeof ratio === 'number' && Number.isFinite(ratio) && ratio !== 0))
45
+ throw new Error(
46
+ `${what}: ratio is turns of the input for one of the output, a number (got ${JSON.stringify(ratio)})`,
47
+ );
48
+ if (!(Number.isInteger(n) && n >= 2 && n <= 8))
49
+ throw new Error(`${what}: planets is a whole number from 2 to 8 (got ${JSON.stringify(n)})`);
50
+ for (const [k, v] of [
51
+ ['held', held],
52
+ ['input', input],
53
+ ])
54
+ if (!MEMBERS.includes(v))
55
+ throw new Error(`${what}: ${k} is one of ${MEMBERS.join(', ')} (got ${JSON.stringify(v)})`);
37
56
  const output = spec.output ?? MEMBERS.find((x) => x !== held && x !== input);
38
- if (!MEMBERS.includes(output) || new Set([held, input, output]).size !== 3) throw new Error(`${what}: held, input and output are the sun, the carrier and the ring, one each (got ${held}, ${input}, ${output})`);
39
- const least = spec.least ?? 17, most = spec.most ?? 150, pa = spec.pressureAngle ?? 20;
57
+ if (!MEMBERS.includes(output) || new Set([held, input, output]).size !== 3)
58
+ throw new Error(
59
+ `${what}: held, input and output are the sun, the carrier and the ring, one each (got ${held}, ${input}, ${output})`,
60
+ );
61
+ const least = spec.least ?? 17,
62
+ most = spec.most ?? 150,
63
+ pa = spec.pressureAngle ?? 20;
40
64
  const common = { module: m, pressureAngle: pa, ...(spec.backlash != null ? { backlash: spec.backlash } : {}) };
41
65
  // Every set that puts together, scored by how near its ratio is
42
66
  const sets = [];
43
- for (let S = least; S <= most; S++) for (let P = least; P <= most; P++) {
44
- const R = S + 2 * P;
45
- if ((S + R) % n) continue; // the planets go in evenly spaced
46
- if (R - P < 12) continue; // a ring at least 12 teeth more than its planets, or their tips foul (KHK)
47
- const a = (m * (S + P)) / 2, tip = m * (P + 2);
48
- if (n > 1 && 2 * a * Math.sin(Math.PI / n) < tip + m) continue; // the planets' tips a module apart
49
- const rim = m * (R + 2.5) + 6 * m;
50
- if (spec.size != null && rim > spec.size) continue;
51
- const i = planetaryRatio({ sun: S, ring: R }, { held, input, output });
52
- sets.push({ sun: S, planet: P, ring: R, ratio: i, off: Math.abs(i - ratio) / Math.abs(ratio), rim });
53
- }
54
- if (!sets.length) throw new Error(`${what}: no teeth put together${spec.size != null ? ` within a rim of ${spec.size} mm` : ''} with ${n} planets of ${least} teeth or more: a larger size, fewer planets, or a smaller module`);
67
+ for (let S = least; S <= most; S++)
68
+ for (let P = least; P <= most; P++) {
69
+ const R = S + 2 * P;
70
+ if ((S + R) % n) continue; // the planets go in evenly spaced
71
+ if (R - P < 12) continue; // a ring at least 12 teeth more than its planets, or their tips foul (KHK)
72
+ const a = (m * (S + P)) / 2,
73
+ tip = m * (P + 2);
74
+ if (n > 1 && 2 * a * Math.sin(Math.PI / n) < tip + m) continue; // the planets' tips a module apart
75
+ const rim = m * (R + 2.5) + 6 * m;
76
+ if (spec.size != null && rim > spec.size) continue;
77
+ const i = planetaryRatio({ sun: S, ring: R }, { held, input, output });
78
+ sets.push({ sun: S, planet: P, ring: R, ratio: i, off: Math.abs(i - ratio) / Math.abs(ratio), rim });
79
+ }
80
+ if (!sets.length)
81
+ throw new Error(
82
+ `${what}: no teeth put together${spec.size != null ? ` within a rim of ${spec.size} mm` : ''} with ${n} planets of ${least} teeth or more: a larger size, fewer planets, or a smaller module`,
83
+ );
55
84
  sets.sort((x, y) => x.off - y.off || x.rim - y.rim);
56
85
  const best = layout(sets[0], { n, m, common, held, input, output });
57
- const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
86
+ const out = [],
87
+ f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
58
88
  const off = sets[0].off;
59
- f(off < 0.005 ? 'pass' : off < 0.05 ? 'warn' : 'fail', `${fmt(best.ratio)} : 1 (${input} to ${output}, ${held} held), ${off < 0.005 ? 'as asked' : `${fmt(off * 100, 1)}% from the ${fmt(ratio)} asked: the nearest that puts together`}: sun ${best.sun.teeth}, planets ${best.planet.teeth} × ${n}, ring ${best.ring.teeth}`, 'planetary.ratio', { ratio: best.ratio, asked: ratio });
60
- for (const [pair, ms] of [['sun and planet', best.sunMesh], ['planet and ring', best.ringMesh]]) for (const x of ms.findings) out.push({ ...x, text: `${pair}: ${x.text}` });
61
- return { best, others: sets.slice(1, 6).map((x) => ({ sun: x.sun, planet: x.planet, ring: x.ring, ratio: x.ratio, rim: x.rim })), findings: out };
89
+ f(
90
+ off < 0.005 ? 'pass' : off < 0.05 ? 'warn' : 'fail',
91
+ `${fmt(best.ratio)} : 1 (${input} to ${output}, ${held} held), ${off < 0.005 ? 'as asked' : `${fmt(off * 100, 1)}% from the ${fmt(ratio)} asked: the nearest that puts together`}: sun ${best.sun.teeth}, planets ${best.planet.teeth} × ${n}, ring ${best.ring.teeth}`,
92
+ 'planetary.ratio',
93
+ { ratio: best.ratio, asked: ratio },
94
+ );
95
+ for (const [pair, ms] of [
96
+ ['sun and planet', best.sunMesh],
97
+ ['planet and ring', best.ringMesh],
98
+ ])
99
+ for (const x of ms.findings) out.push({ ...x, text: `${pair}: ${x.text}` });
100
+ return {
101
+ best,
102
+ others: sets.slice(1, 6).map((x) => ({ sun: x.sun, planet: x.planet, ring: x.ring, ratio: x.ratio, rim: x.rim })),
103
+ findings: out,
104
+ };
62
105
  }
63
106
 
64
107
  // The gearbox laid out: the sun at the centre, its first tooth on +x; each planet at its place round the carrier,
65
108
  // turned so a tooth sits in a gap of the sun and of the ring; the ring turned to suit
66
109
  function layout({ sun: S, planet: P, ring: R, ratio }, { n, m, common, held, input, output }) {
67
- const sun = gearModel({ ...common, teeth: S }), planet = gearModel({ ...common, teeth: P }), ring = internalModel({ ...common, teeth: R });
68
- const sunMesh = mesh(sun, planet), ringMesh = mesh(planet, ring), a = sunMesh.centre;
110
+ const sun = gearModel({ ...common, teeth: S }),
111
+ planet = gearModel({ ...common, teeth: P }),
112
+ ring = internalModel({ ...common, teeth: R });
113
+ const sunMesh = mesh(sun, planet),
114
+ ringMesh = mesh(planet, ring),
115
+ a = sunMesh.centre;
69
116
  const planets = Array.from({ length: n }, (_, k) => {
70
117
  const at = (360 * k) / n;
71
118
  return { at: [a * Math.cos(at * DEG), a * Math.sin(at * DEG)], angle: phase(sun, planet, at), place: at };
72
119
  });
73
120
  // the ring, from the first planet: the line from that planet to the ring's centre points back along 180°
74
121
  const ringAngle = (P / R) * planets[0].angle + phase(planet, ring, 180);
75
- return { sun: { teeth: S, model: sun }, planet: { teeth: P, model: planet }, ring: { teeth: R, model: ring, angle: ringAngle },
76
- planets, centre: a, ratio, held, input, output, rim: ring.rim, module: m, sunMesh, ringMesh };
122
+ return {
123
+ sun: { teeth: S, model: sun },
124
+ planet: { teeth: P, model: planet },
125
+ ring: { teeth: R, model: ring, angle: ringAngle },
126
+ planets,
127
+ centre: a,
128
+ ratio,
129
+ held,
130
+ input,
131
+ output,
132
+ rim: ring.rim,
133
+ module: m,
134
+ sunMesh,
135
+ ringMesh,
136
+ };
77
137
  }
78
138
 
79
139
  /**
@@ -82,7 +142,7 @@ function layout({ sun: S, planet: P, ring: R, ratio }, { n, m, common, held, inp
82
142
  * @param {{ sun: number, planet: number, planet2: number, ring: number, ring2: number }} t
83
143
  */
84
144
  export function compoundRatio({ sun: S, planet: P1, planet2: P2, ring: R1, ring2: R2 }) {
85
- return ((S + R1) / S) / (1 - (R1 * P2) / (P1 * R2));
145
+ return (S + R1) / S / (1 - (R1 * P2) / (P1 * R2));
86
146
  }
87
147
 
88
148
  /**
@@ -93,7 +153,8 @@ export function compoundRatio({ sun: S, planet: P1, planet2: P2, ring: R1, ring2
93
153
  * @param {number} [each] the share per mesh (default 0.95, an estimate)
94
154
  */
95
155
  export function compoundEfficiency({ planet: P1, planet2: P2, ring: R1, ring2: R2 }, each = 0.95) {
96
- const rho = (R1 * P2) / (P1 * R2), lost = 1 - each ** 2;
156
+ const rho = (R1 * P2) / (P1 * R2),
157
+ lost = 1 - each ** 2;
97
158
  return each / (1 + (lost * rho) / Math.abs(1 - rho));
98
159
  }
99
160
 
@@ -117,70 +178,192 @@ export function compoundEfficiency({ planet: P1, planet2: P2, ring: R1, ring2: R
117
178
  export function compoundPlanetary(spec) {
118
179
  const what = 'compoundPlanetary';
119
180
  if (!spec || typeof spec !== 'object') throw new Error(`${what}: give it { module, ratio, planets, size }`);
120
- const { module: m, ratio } = spec, n = spec.planets ?? 3;
121
- if (!(typeof m === 'number' && m > 0)) throw new Error(`${what}: module is a size above 0 (got ${JSON.stringify(m)})`);
122
- if (!(typeof ratio === 'number' && Number.isFinite(ratio) && Math.abs(ratio) > 1)) throw new Error(`${what}: ratio is turns of the sun for one of the output, more than 1 either way (got ${JSON.stringify(ratio)})`);
123
- if (!(Number.isInteger(n) && n >= 2 && n <= 6)) throw new Error(`${what}: planets is a whole number from 2 to 6 (got ${JSON.stringify(n)})`);
124
- const least = spec.least ?? 17, most = spec.most ?? 90, pa = spec.pressureAngle ?? 20, steps = spec.step != null ? [spec.step] : [1, -1, 2, -2];
181
+ const { module: m, ratio } = spec,
182
+ n = spec.planets ?? 3;
183
+ if (!(typeof m === 'number' && m > 0))
184
+ throw new Error(`${what}: module is a size above 0 (got ${JSON.stringify(m)})`);
185
+ if (!(typeof ratio === 'number' && Number.isFinite(ratio) && Math.abs(ratio) > 1))
186
+ throw new Error(
187
+ `${what}: ratio is turns of the sun for one of the output, more than 1 either way (got ${JSON.stringify(ratio)})`,
188
+ );
189
+ if (!(Number.isInteger(n) && n >= 2 && n <= 6))
190
+ throw new Error(`${what}: planets is a whole number from 2 to 6 (got ${JSON.stringify(n)})`);
191
+ const least = spec.least ?? 17,
192
+ most = spec.most ?? 90,
193
+ pa = spec.pressureAngle ?? 20,
194
+ steps = spec.step != null ? [spec.step] : [1, -1, 2, -2];
125
195
  const inPlace = printedInPlace(what, spec, m, pa);
126
- const common = { module: m, pressureAngle: pa, ...(inPlace ? { backlash: inPlace.backlash, dedendum: inPlace.dedendum, helix: inPlace.helix, herringbone: true } : spec.backlash != null ? { backlash: spec.backlash } : {}) };
127
- const multiple = (x, k) => Math.ceil(x / k) * k, sets = [];
128
- for (let S = multiple(least, n); S <= most; S += n) for (let P1 = multiple(least, n); P1 <= most; P1 += n) {
129
- const R1 = S + 2 * P1;
130
- if (R1 - P1 < 12) continue;
131
- if (2 * ((m * (S + P1)) / 2) * Math.sin(Math.PI / n) < m * (P1 + 2) + m) continue; // the planets' tips a module apart
132
- for (const k of steps) {
133
- const j = k * n, P2 = P1 + j, R2 = R1 + j;
134
- if (P2 < least) continue;
135
- const set = { sun: S, planet: P1, planet2: P2, ring: R1, ring2: R2 }, i = compoundRatio(set), rim = m * (Math.max(R1, R2) + 2.5) + 6 * m;
136
- if (spec.size != null && rim > spec.size) continue;
137
- sets.push({ ...set, ratio: i, efficiency: compoundEfficiency(set), off: Math.abs(i - ratio) / Math.abs(ratio), rim });
196
+ const common = {
197
+ module: m,
198
+ pressureAngle: pa,
199
+ ...(inPlace
200
+ ? { backlash: inPlace.backlash, dedendum: inPlace.dedendum, helix: inPlace.helix, herringbone: true }
201
+ : spec.backlash != null
202
+ ? { backlash: spec.backlash }
203
+ : {}),
204
+ };
205
+ const multiple = (x, k) => Math.ceil(x / k) * k,
206
+ sets = [];
207
+ for (let S = multiple(least, n); S <= most; S += n)
208
+ for (let P1 = multiple(least, n); P1 <= most; P1 += n) {
209
+ const R1 = S + 2 * P1;
210
+ if (R1 - P1 < 12) continue;
211
+ if (2 * ((m * (S + P1)) / 2) * Math.sin(Math.PI / n) < m * (P1 + 2) + m) continue; // the planets' tips a module apart
212
+ for (const k of steps) {
213
+ const j = k * n,
214
+ P2 = P1 + j,
215
+ R2 = R1 + j;
216
+ if (P2 < least) continue;
217
+ const set = { sun: S, planet: P1, planet2: P2, ring: R1, ring2: R2 },
218
+ i = compoundRatio(set),
219
+ rim = m * (Math.max(R1, R2) + 2.5) + 6 * m;
220
+ if (spec.size != null && rim > spec.size) continue;
221
+ sets.push({
222
+ ...set,
223
+ ratio: i,
224
+ efficiency: compoundEfficiency(set),
225
+ off: Math.abs(i - ratio) / Math.abs(ratio),
226
+ rim,
227
+ });
228
+ }
138
229
  }
139
- }
140
- if (!sets.length) throw new Error(`${what}: no teeth put together${spec.size != null ? ` within a rim of ${spec.size} mm` : ''} with ${n} planets of ${least} teeth or more: a larger size, or a smaller module`);
230
+ if (!sets.length)
231
+ throw new Error(
232
+ `${what}: no teeth put together${spec.size != null ? ` within a rim of ${spec.size} mm` : ''} with ${n} planets of ${least} teeth or more: a larger size, or a smaller module`,
233
+ );
141
234
  sets.sort((x, y) => x.off - y.off || x.rim - y.rim);
142
235
  // (helical teeth: two outer gears in mesh are of opposite hands, a ring and its pinion of the same)
143
236
  const hand = (h) => (inPlace ? { hand: h } : {});
144
- const b = sets[0], sun = gearModel({ ...common, ...hand('right'), teeth: b.sun }), planet = gearModel({ ...common, ...hand('left'), teeth: b.planet }), planet2 = gearModel({ ...common, ...hand('left'), teeth: b.planet2 }), ring = internalModel({ ...common, ...hand('left'), teeth: b.ring }), ring2 = internalModel({ ...common, ...hand('left'), teeth: b.ring2 });
145
- const sunMesh = mesh(sun, planet), ringMesh = mesh(planet, ring), ring2Mesh = mesh(planet2, ring2), a = sunMesh.centre, out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
146
- const planets = Array.from({ length: n }, (_, k) => { const at = (360 * k) / n; return { at: [a * Math.cos(at * DEG), a * Math.sin(at * DEG)], angle: phase(sun, planet, at), place: at }; });
147
- const ringAngle = (b.planet / b.ring) * planets[0].angle + phase(planet, ring, 180), ring2Angle = (b.planet2 / b.ring2) * planets[0].angle + phase(planet2, ring2, 180);
237
+ const b = sets[0],
238
+ sun = gearModel({ ...common, ...hand('right'), teeth: b.sun }),
239
+ planet = gearModel({ ...common, ...hand('left'), teeth: b.planet }),
240
+ planet2 = gearModel({ ...common, ...hand('left'), teeth: b.planet2 }),
241
+ ring = internalModel({ ...common, ...hand('left'), teeth: b.ring }),
242
+ ring2 = internalModel({ ...common, ...hand('left'), teeth: b.ring2 });
243
+ const sunMesh = mesh(sun, planet),
244
+ ringMesh = mesh(planet, ring),
245
+ ring2Mesh = mesh(planet2, ring2),
246
+ a = sunMesh.centre,
247
+ out = [],
248
+ f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
249
+ const planets = Array.from({ length: n }, (_, k) => {
250
+ const at = (360 * k) / n;
251
+ return { at: [a * Math.cos(at * DEG), a * Math.sin(at * DEG)], angle: phase(sun, planet, at), place: at };
252
+ });
253
+ const ringAngle = (b.planet / b.ring) * planets[0].angle + phase(planet, ring, 180),
254
+ ring2Angle = (b.planet2 / b.ring2) * planets[0].angle + phase(planet2, ring2, 180);
148
255
  // Each planet's second step in phase with the output ring. The rings are set from the first planet; another planet
149
256
  // meshes the sun and the held ring as well a whole tooth of its first step on (360 / P1 degrees), and that turns its
150
257
  // second step too, so it takes the first such tooth that also puts its second step in a space of the output ring,
151
258
  // whose angle for a planet at place θ turned ψ is (P2 / R2) ψ + phase(planet2, ring2, θ + 180), to a whole pitch
152
259
  // (360 / R2). Every tooth count a multiple of the planets puts some in phase as they stand (chapter 11's 18/21 and
153
260
  // 54/57 all are) and others not: 57/39/36/135/132's second planet stood 0.11 mm from the output ring at a 0.3 mm gap.
154
- const pitch2 = 360 / b.ring2, missAt = (p, t) => { const d = (b.planet2 / b.ring2) * (p.angle + (360 * t) / b.planet) + phase(planet2, ring2, p.place + 180) - ring2Angle; return ((((d % pitch2) + pitch2 * 1.5) % pitch2) - pitch2 / 2); };
261
+ const pitch2 = 360 / b.ring2,
262
+ missAt = (p, t) => {
263
+ const d =
264
+ (b.planet2 / b.ring2) * (p.angle + (360 * t) / b.planet) + phase(planet2, ring2, p.place + 180) - ring2Angle;
265
+ return (((d % pitch2) + pitch2 * 1.5) % pitch2) - pitch2 / 2;
266
+ };
155
267
  for (const p of planets.slice(1)) {
156
268
  const t = Array.from({ length: b.planet }, (_, i) => i).find((i) => Math.abs(missAt(p, i)) < 1e-6);
157
- if (t == null) f('fail', `the planet at ${fmt(p.place, 1)}° can't mesh with the output ring, whichever tooth of its first step meets the sun`, 'compound.phase', { place: p.place });
269
+ if (t == null)
270
+ f(
271
+ 'fail',
272
+ `the planet at ${fmt(p.place, 1)}° can't mesh with the output ring, whichever tooth of its first step meets the sun`,
273
+ 'compound.phase',
274
+ { place: p.place },
275
+ );
158
276
  else p.angle += (360 * t) / b.planet;
159
277
  }
160
- f(b.off < 0.05 ? 'pass' : b.off < 0.2 ? 'warn' : 'fail', `${fmt(b.ratio, 2)} : 1 (sun to the output ring, the other ring held), ${b.off < 0.01 ? 'as asked' : `${fmt(b.off * 100, 1)}% from the ${fmt(ratio)} asked`}: sun ${b.sun}, stepped planets ${b.planet}/${b.planet2} × ${n}, rings ${b.ring} and ${b.ring2}`, 'compound.ratio', { ratio: b.ratio, asked: ratio });
161
- f('info', `about ${fmt(b.efficiency * 100, 0)}% efficient (an estimate): ${b.efficiency <= 0.5 ? 'under 50%, so it can\'t be driven backwards' : 'it can be driven backwards'}`, 'compound.efficiency', { efficiency: b.efficiency });
162
- if (Math.abs(ring2Mesh.centre - a) > 1e-9) f('fail', `the output ring's centre distance is ${fmt(ring2Mesh.centre)} mm and the planets' ${fmt(a)} mm: the rings' difference must equal the planets'`, 'compound.centre');
163
- for (const [pair, ms] of [['sun and planet', sunMesh], ['planet and held ring', ringMesh], ['planet and output ring', ring2Mesh]]) for (const x of ms.findings) out.push({ ...x, text: `${pair}: ${x.text}` });
164
- const play = compoundPlay({ sun: b.sun, planet: b.planet, planet2: b.planet2, ring: b.ring, ring2: b.ring2, module: m }, { sun: sunMesh.backlash, ring: ringMesh.backlash, ring2: ring2Mesh.backlash });
165
- if (inPlace) f('info', `printed in place: ${fmt(inPlace.gap)} mm clear normal to every flank and under every tip (a backlash of ${fmt(inPlace.backlash)} mm and a dedendum of ${fmt(inPlace.dedendum)} modules), herringbone teeth at ${fmt(inPlace.helix)}° so the rings stay on the planets, and no carrier: the rings hold the planets`, 'compound.inPlace', { gap: inPlace.gap, backlash: inPlace.backlash, dedendum: inPlace.dedendum, helix: inPlace.helix });
166
- f('info', `the output ring turns ${fmt(play.degrees, 2)}° (${fmt(play.mm)} mm at its pitch circle) with the sun and the held ring still: its play, from the three meshes' backlash`, 'compound.play', { degrees: play.degrees, mm: play.mm });
167
- return { best: { sun: { teeth: b.sun, model: sun }, planet: { teeth: b.planet, model: planet }, planet2: { teeth: b.planet2, model: planet2 }, ring: { teeth: b.ring, model: ring, angle: ringAngle }, ring2: { teeth: b.ring2, model: ring2, angle: ring2Angle }, planets, centre: a, ratio: b.ratio, efficiency: b.efficiency, rim: Math.max(ring.rim, ring2.rim), module: m, play, ...(inPlace ? { printInPlace: inPlace } : {}) }, others: sets.slice(1, 6).map(({ off, ...x }) => x), findings: out };
278
+ f(
279
+ b.off < 0.05 ? 'pass' : b.off < 0.2 ? 'warn' : 'fail',
280
+ `${fmt(b.ratio, 2)} : 1 (sun to the output ring, the other ring held), ${b.off < 0.01 ? 'as asked' : `${fmt(b.off * 100, 1)}% from the ${fmt(ratio)} asked`}: sun ${b.sun}, stepped planets ${b.planet}/${b.planet2} × ${n}, rings ${b.ring} and ${b.ring2}`,
281
+ 'compound.ratio',
282
+ { ratio: b.ratio, asked: ratio },
283
+ );
284
+ f(
285
+ 'info',
286
+ `about ${fmt(b.efficiency * 100, 0)}% efficient (an estimate): ${b.efficiency <= 0.5 ? "under 50%, so it can't be driven backwards" : 'it can be driven backwards'}`,
287
+ 'compound.efficiency',
288
+ { efficiency: b.efficiency },
289
+ );
290
+ if (Math.abs(ring2Mesh.centre - a) > 1e-9)
291
+ f(
292
+ 'fail',
293
+ `the output ring's centre distance is ${fmt(ring2Mesh.centre)} mm and the planets' ${fmt(a)} mm: the rings' difference must equal the planets'`,
294
+ 'compound.centre',
295
+ );
296
+ for (const [pair, ms] of [
297
+ ['sun and planet', sunMesh],
298
+ ['planet and held ring', ringMesh],
299
+ ['planet and output ring', ring2Mesh],
300
+ ])
301
+ for (const x of ms.findings) out.push({ ...x, text: `${pair}: ${x.text}` });
302
+ const play = compoundPlay(
303
+ { sun: b.sun, planet: b.planet, planet2: b.planet2, ring: b.ring, ring2: b.ring2, module: m },
304
+ { sun: sunMesh.backlash, ring: ringMesh.backlash, ring2: ring2Mesh.backlash },
305
+ );
306
+ if (inPlace)
307
+ f(
308
+ 'info',
309
+ `printed in place: ${fmt(inPlace.gap)} mm clear normal to every flank and under every tip (a backlash of ${fmt(inPlace.backlash)} mm and a dedendum of ${fmt(inPlace.dedendum)} modules), herringbone teeth at ${fmt(inPlace.helix)}° so the rings stay on the planets, and no carrier: the rings hold the planets`,
310
+ 'compound.inPlace',
311
+ { gap: inPlace.gap, backlash: inPlace.backlash, dedendum: inPlace.dedendum, helix: inPlace.helix },
312
+ );
313
+ f(
314
+ 'info',
315
+ `the output ring turns ${fmt(play.degrees, 2)}° (${fmt(play.mm)} mm at its pitch circle) with the sun and the held ring still: its play, from the three meshes' backlash`,
316
+ 'compound.play',
317
+ { degrees: play.degrees, mm: play.mm },
318
+ );
319
+ return {
320
+ best: {
321
+ sun: { teeth: b.sun, model: sun },
322
+ planet: { teeth: b.planet, model: planet },
323
+ planet2: { teeth: b.planet2, model: planet2 },
324
+ ring: { teeth: b.ring, model: ring, angle: ringAngle },
325
+ ring2: { teeth: b.ring2, model: ring2, angle: ring2Angle },
326
+ planets,
327
+ centre: a,
328
+ ratio: b.ratio,
329
+ efficiency: b.efficiency,
330
+ rim: Math.max(ring.rim, ring2.rim),
331
+ module: m,
332
+ play,
333
+ ...(inPlace ? { printInPlace: inPlace } : {}),
334
+ },
335
+ others: sets.slice(1, 6).map(({ off, ...x }) => x),
336
+ findings: out,
337
+ };
168
338
  }
169
339
 
170
340
  // Printed in place: the gap, checked by name, and the backlash, dedendum and helix it sets
171
341
  function printedInPlace(what, spec, m, pa) {
172
342
  if (spec.printInPlace == null || spec.printInPlace === false) {
173
- for (const k of ['gap', 'helix']) if (spec[k] != null) throw new Error(`${what}: ${k} is for a gearbox printed in place: add printInPlace: true, or leave ${k} out`);
343
+ for (const k of ['gap', 'helix'])
344
+ if (spec[k] != null)
345
+ throw new Error(`${what}: ${k} is for a gearbox printed in place: add printInPlace: true, or leave ${k} out`);
174
346
  return null;
175
347
  }
176
- if (spec.printInPlace !== true) throw new Error(`${what}: printInPlace is true or false (got ${JSON.stringify(spec.printInPlace)})`);
177
- if (spec.backlash != null) throw new Error(`${what}: printed in place, the backlash comes from the gap (2 gap / (cos α cos β_b)): give gap, the print gap each side in mm, not backlash`);
178
- const gap = spec.gap ?? DEFAULTS.printGap.value, helix = spec.helix ?? DEFAULTS.helix.value;
179
- if (!(typeof gap === 'number' && gap > 0 && gap < m / 2)) throw new Error(`${what}: gap is the print gap each side, above 0 and under half the module (${fmt(m / 2)} mm) (got ${JSON.stringify(spec.gap)})`);
180
- if (!(typeof helix === 'number' && helix >= 10 && helix <= 45)) throw new Error(`${what}: helix is the herringbone's angle, from 10 to 45° (got ${JSON.stringify(spec.helix)})`);
348
+ if (spec.printInPlace !== true)
349
+ throw new Error(`${what}: printInPlace is true or false (got ${JSON.stringify(spec.printInPlace)})`);
350
+ if (spec.backlash != null)
351
+ throw new Error(
352
+ `${what}: printed in place, the backlash comes from the gap (2 gap / (cos α cos β_b)): give gap, the print gap each side in mm, not backlash`,
353
+ );
354
+ const gap = spec.gap ?? DEFAULTS.printGap.value,
355
+ helix = spec.helix ?? DEFAULTS.helix.value;
356
+ if (!(typeof gap === 'number' && gap > 0 && gap < m / 2))
357
+ throw new Error(
358
+ `${what}: gap is the print gap each side, above 0 and under half the module (${fmt(m / 2)} mm) (got ${JSON.stringify(spec.gap)})`,
359
+ );
360
+ if (!(typeof helix === 'number' && helix >= 10 && helix <= 45))
361
+ throw new Error(`${what}: helix is the herringbone's angle, from 10 to 45° (got ${JSON.stringify(spec.helix)})`);
181
362
  // The gap normal to a flank: the transverse backlash j_t at the pitch circle, times cos α_t across the involute,
182
363
  // times cos β_b along the helix (ISO/TR 10064-2: j_bn = j_t cos α_t cos β_b), shared by the two flanks of a space
183
- const a = pa * DEG, betaB = Math.atan(Math.tan(helix * DEG) * Math.cos(a)), backlash = (2 * gap) / (Math.cos(a) * Math.cos(betaB));
364
+ const a = pa * DEG,
365
+ betaB = Math.atan(Math.tan(helix * DEG) * Math.cos(a)),
366
+ backlash = (2 * gap) / (Math.cos(a) * Math.cos(betaB));
184
367
  // Under each tip: the dedendum less the addendum, in modules
185
368
  const dedendum = Math.max(DEFAULTS.dedendum.value, DEFAULTS.addendum.value + gap / m);
186
369
  return { gap, helix, backlash, dedendum, carrier: false };
@@ -197,8 +380,15 @@ function printedInPlace(what, spec, m, pa) {
197
380
  * @returns {{ mm: number, degrees: number, radians: number }} mm at the output ring's pitch circle
198
381
  */
199
382
  export function compoundPlay({ planet: P1, planet2: P2, ring2: R2, module: m }, backlash) {
200
- for (const k of ['sun', 'ring', 'ring2']) if (!(typeof backlash?.[k] === 'number' && backlash[k] >= 0)) throw new Error(`compoundPlay: backlash.${k} is the ${k === 'sun' ? 'sun\'s' : k === 'ring' ? 'held ring\'s' : 'output ring\'s'} mesh's, 0 or more mm (got ${JSON.stringify(backlash?.[k])})`);
201
- if (!(typeof m === 'number' && m > 0)) throw new Error(`compoundPlay: module is a size above 0 (got ${JSON.stringify(m)})`);
202
- const k = P2 / P1, mm = (backlash.sun * Math.abs(1 - k)) / 2 + (backlash.ring * (1 + k)) / 2 + backlash.ring2, radians = mm / ((m * R2) / 2);
383
+ for (const k of ['sun', 'ring', 'ring2'])
384
+ if (!(typeof backlash?.[k] === 'number' && backlash[k] >= 0))
385
+ throw new Error(
386
+ `compoundPlay: backlash.${k} is the ${k === 'sun' ? "sun's" : k === 'ring' ? "held ring's" : "output ring's"} mesh's, 0 or more mm (got ${JSON.stringify(backlash?.[k])})`,
387
+ );
388
+ if (!(typeof m === 'number' && m > 0))
389
+ throw new Error(`compoundPlay: module is a size above 0 (got ${JSON.stringify(m)})`);
390
+ const k = P2 / P1,
391
+ mm = (backlash.sun * Math.abs(1 - k)) / 2 + (backlash.ring * (1 + k)) / 2 + backlash.ring2,
392
+ radians = mm / ((m * R2) / 2);
203
393
  return { mm, radians, degrees: radians / DEG };
204
394
  }