@rulecad/gears 0.2.0 → 0.3.1

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/README.md CHANGED
@@ -79,7 +79,8 @@ What every gear takes:
79
79
  1.2 it is rough.
80
80
  - **`backlash`**: the play, with the centre you gave.
81
81
  - **`overlap`** and **`thrust`**: for helical gears, with `width` given, how much the twist adds to the contact ratio, and the push along the axis for each newton driven (none for a herringbone).
82
- - **`findings`**: `mesh.contact`, `mesh.centre` (axes too close jam the teeth; too far knock), `mesh.module` and
82
+ - **`findings`**: `mesh.contact`, `mesh.centre` (axes closer than the gears need eat their backlash, 2 Δa tan α of it, and past
83
+ it jam the teeth; too far apart, they knock), `mesh.module` and
83
84
  `mesh.pressure` (gears that can't mesh), `mesh.interference` (a ring too near its pinion's size), `mesh.helix` (helical
84
85
  gears that cross: outer gears that mesh are of opposite hands; a pinion and its ring or rack, of the same hand).
85
86
 
@@ -102,6 +103,39 @@ planet's place round the carrier and the turn that puts it in phase with the sun
102
103
  gives the ratio exactly, the nearest is returned and `planetary.ratio` says how far off it is (a fail past 5%).
103
104
  `planetaryRatio({ sun, ring }, { held, input, output })` is the ratio of any teeth, any way round.
104
105
 
106
+ ## Worms and bevel gears
107
+
108
+ A **worm** is a screw whose thread, cut along its axis, is the basic rack's tooth: `worm({ module, starts, diameter,
109
+ length, bore })` draws it along z. Its wheel is a helical gear at the worm's lead angle and of its hand,
110
+ `wormWheel(wormSpec, { teeth, width, bore })` (the usual printable wheel: not throated, so they touch at a point,
111
+ which printed teeth spread). `wormMesh(wormSpec, { teeth })` gives the centre distance, the ratio (`starts` teeth of the
112
+ wheel for each turn of the worm), whether it **locks** (the lead angle under the friction angle: the wheel can't turn
113
+ the worm back; `worm.locks`) and how **efficient** it is. A single start of module 1 on a 10 mm worm: a 5.71° lead,
114
+ it locks at μ 0.25, 27% efficient. Three starts on 8 mm: 20.6°, it doesn't.
115
+
116
+ A **bevel gear** carries a turn round a corner. `bevel({ module, teeth, mate, shaftAngle, width, bore })` draws a
117
+ straight bevel by Tredgold's construction: the tooth drawn on the back cone as a spur gear of z / cos δ teeth, wrapped
118
+ onto the back cone and seen from the apex onto the back end, every section nearer the apex that one scaled, so the
119
+ teeth run straight to the apex. The back end is on the bed and the apex up z. `bevelModel` gives the pitch cone angle
120
+ (tan δ = sin Σ / (z₂/z₁ + cos Σ)), the cone distance R, the virtual teeth, and refuses a face width over R / 3;
121
+ `bevelMesh` the ratio and the contact ratio of the back cones' equivalent spur gears. In an assembly, the two cones
122
+ must share their apex.
123
+
124
+ ## The mesh, measured
125
+
126
+ `mesh()` reckons a pair from the formulas. `contact(a, b, { centre, steps })` measures it from the teeth as drawn,
127
+ with their fillets, rounds and backlash and the centre distance given. It turns the driver through one tooth in steps,
128
+ and at each step turns the driven gear each way until its outline meets the driver's:
129
+
130
+ - **backlash**: the turn between the two meetings, as play at the pitch circle (`mesh.backlash`, a warn when it falls
131
+ under 0.02 mm);
132
+ - **transmission error**: how far the driven gear's forward meeting wanders across a tooth (`mesh.error`);
133
+ - **interference**: steps where the outlines already cross where the ratio puts them (`mesh.interference`).
134
+
135
+ `a` and `b` are the parameters the models take; `b.kind` is `'internal'` for a ring or `'rack'` for a rack. For 18
136
+ teeth on 30 at module 1 it measures 0.150 to 0.152 mm of play against the 0.15 mm drawn, and 1 µm of transmission
137
+ error; with the axes 0.2 mm apart, 0.30 mm (2 Δa tan α more). `cad check` measures every mesh in an assembly this way.
138
+
105
139
  ## How it prints
106
140
 
107
141
  `printable(model, printer, width)` reads the gear against the printer (`{ line, layer, nozzle, wall_min }`, as in
@@ -119,6 +153,12 @@ strongest. Every tooth has a round at its root (the commonest place a tooth crac
119
153
  The defaults, each with its rule, are in `DEFAULTS`. The backlash, the chamfer, the tip round and the smallest module
120
154
  are estimates until a printed sampler measures them.
121
155
 
156
+ ## How much gets through
157
+
158
+ `stageEfficiency(kind)` is the share of the power a stage passes on: 0.95 per mesh for printed teeth (an estimate,
159
+ `DEFAULTS.efficiency`), so a planetary's two meshes pass 0.9. A worm's comes from its lead angle and friction
160
+ (`stageEfficiency('worm', { worm: wormModel(…) })`). Give your own figure with `{ efficiency }`.
161
+
122
162
  ## How strong it is
123
163
 
124
164
  `strength(model, { width, material, torque?, safety?, speed? })` reads the teeth by the Lewis equation: the bending
@@ -136,6 +176,14 @@ strength(gearModel({ module: 1, teeth: 18 }), { width: 6, material: { strength:
136
176
  It is a first number. A printed tooth is weaker than a moulded one where its layers and voids are, and stronger than
137
177
  Lewis says where the root fillet and its neighbours share the load. Where it matters, print one and load it.
138
178
 
179
+ Where it matters, `toothStress(params, { width, torque, material: { E, nu, strength } })` analyses one tooth by
180
+ finite elements (`@rulecad/fea`, plane stress): the tooth and a slice of rim, the rim's cut edges held, the load at
181
+ the tip along the line of action. It gives the peak stress at the root of the loaded side, with the fillet as drawn,
182
+ and runs a grid a third coarser to say whether it has settled (`gear.fe.coarse` when it moved over 5%). For 18 teeth
183
+ it reads about 1.33× Lewis, and 1.25× for 40: the fillet's stress concentration, which Lewis leaves out (AGMA's
184
+ geometry factor for a tip-loaded 18-tooth pinion implies about 1.3).
185
+
186
+
139
187
  ## What isn't refused, and what is
140
188
 
141
189
  A model or a solid refuses what isn't a gear, by name, before any maths: `gearModel: teeth is a whole number of 6 or
@@ -166,6 +214,11 @@ a point.
166
214
  | Planetary assembly | R = S + 2P; (S + R) / n whole; 2a sin(π / n) > d_a,planet | KHK, planetary gears |
167
215
  | Tooth bending (Lewis) | σ = K_v F_t / (b m Y); helical: m cos β, z / cos³β teeth | Budynas and Nisbett, Shigley's Mechanical Engineering Design, 10th ed., §14-1 and Table 14-2 |
168
216
  | Velocity factor (Barth, cut profile) | K_v = (3.05 + V) / 3.05, V in m/s | Shigley, eq. 14-4b |
217
+ | Tooth by finite elements | plane stress, 32 cells a module, load F_t / cos α along the line of action at the tip | `@rulecad/fea`; AGMA 908-B89 for the geometry factor it is compared with |
218
+ | Worm lead angle | tan γ = s π m / (π d) | ISO 21771 (worms: ISO/TR 10828) |
219
+ | Worm locking and efficiency | locks when γ < atan μ; η = tan γ (cos α_n − μ tan γ) / (cos α_n tan γ + μ) | Dudley (1984), ch. 3; Shigley, §13-11 |
220
+ | Bevel pitch cone | tan δ₁ = sin Σ / (z₂/z₁ + cos Σ); R = r / sin δ; virtual teeth z / cos δ | Tredgold's approximation; Shigley, §13-12 |
221
+ | Bevel face width | at most R / 3 | AGMA 2005 |
169
222
 
170
223
  ## Words used
171
224
 
package/contact.d.mts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The mesh of a pair as drawn: backlash, transmission error and interference across one pitch of the driver.
3
+ * a: the driver's parameters (as gearModel takes them); b: the driven's (kind 'internal' for a ring, 'rack' for a rack).
4
+ * @param {object} a @param {object & { kind?: 'external' | 'internal' | 'rack' }} b
5
+ * @param {{ centre?: number, steps?: number }} [o] centre: the axes' distance (default: what they need)
6
+ */
7
+ export function contact(a: object, b: object & {
8
+ kind?: "external" | "internal" | "rack";
9
+ }, { centre, steps }?: {
10
+ centre?: number;
11
+ steps?: number;
12
+ }): {
13
+ rows: ({
14
+ angle: number;
15
+ interferes: boolean;
16
+ backlash?: undefined;
17
+ error?: undefined;
18
+ } | {
19
+ angle: number;
20
+ backlash: any;
21
+ error: any;
22
+ interferes?: undefined;
23
+ })[];
24
+ backlash: {
25
+ least: number;
26
+ most: number;
27
+ };
28
+ error: number;
29
+ reckoned: any;
30
+ findings: any[];
31
+ };
package/contact.mjs ADDED
@@ -0,0 +1,79 @@
1
+ // The mesh measured, not reckoned: the drawn teeth turned through a pitch, and at each step the driven gear turned
2
+ // each way until its outline meets the driver's. The turn between the two meetings is the backlash as drawn (with the
3
+ // real fillets, rounds and centre distance); how far the forward meeting is from where the ratio puts it is the
4
+ // transmission error; outlines that cross where the ratio puts them interfere. Plain 2D geometry: the outlines'
5
+ // edges near the pitch point, tested for crossing.
6
+ import { spurProfile, internalProfile, rackProfile, gearModel, internalModel, rackModel, mesh, phase, rackPhase } from './gears.mjs';
7
+
8
+ const DEG = Math.PI / 180;
9
+ const fmt = (x, d = 3) => String(+x.toFixed(d));
10
+
11
+ // Do two sets of edges cross anywhere? Edge against edge, with a box test first
12
+ function cross(A, B) {
13
+ for (const [p, q] of A) {
14
+ const x0 = Math.min(p[0], q[0]), x1 = Math.max(p[0], q[0]), y0 = Math.min(p[1], q[1]), y1 = Math.max(p[1], q[1]);
15
+ for (const [r, s] of B) {
16
+ if (Math.max(r[0], s[0]) < x0 || Math.min(r[0], s[0]) > x1 || Math.max(r[1], s[1]) < y0 || Math.min(r[1], s[1]) > y1) continue;
17
+ const d1 = (s[0] - r[0]) * (p[1] - r[1]) - (s[1] - r[1]) * (p[0] - r[0]), d2 = (s[0] - r[0]) * (q[1] - r[1]) - (s[1] - r[1]) * (q[0] - r[0]);
18
+ const d3 = (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]), d4 = (q[0] - p[0]) * (s[1] - p[1]) - (q[1] - p[1]) * (s[0] - p[0]);
19
+ if (d1 * d2 < 0 && d3 * d4 < 0) return true;
20
+ }
21
+ }
22
+ return false;
23
+ }
24
+ const turn = (pts, a, [cx, cy] = [0, 0], [dx, dy] = [0, 0]) => { const c = Math.cos(a), s = Math.sin(a); return pts.map(([x, y]) => [x * c - y * s + cx + dx, x * s + y * c + cy + dy]); };
25
+ // Only the loop's edges near a point (the far teeth never meet)
26
+ const near = (pts, [px, py], r) => { const by = ([x, y]) => (x - px) ** 2 + (y - py) ** 2 < r * r, out = []; for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; if (by(p) || by(q)) out.push([p, q]); } return out; };
27
+
28
+ /**
29
+ * The mesh of a pair as drawn: backlash, transmission error and interference across one pitch of the driver.
30
+ * a: the driver's parameters (as gearModel takes them); b: the driven's (kind 'internal' for a ring, 'rack' for a rack).
31
+ * @param {object} a @param {object & { kind?: 'external' | 'internal' | 'rack' }} b
32
+ * @param {{ centre?: number, steps?: number }} [o] centre: the axes' distance (default: what they need)
33
+ */
34
+ export function contact(a, b, { centre, steps = 24 } = {}) {
35
+ const kind = b.kind ?? 'external';
36
+ const ga = gearModel(a), gb = kind === 'internal' ? internalModel(b) : kind === 'rack' ? rackModel(b) : gearModel(b);
37
+ const m = mesh(ga, gb, centre === undefined ? {} : { centre }), c = centre ?? m.centre, mod = ga.module;
38
+ const A = spurProfile(a).loops[0];
39
+ const B = kind === 'internal' ? internalProfile(b).loops[1] : kind === 'rack' ? rackProfile(b).loops[0] : spurProfile(b).loops[0];
40
+ // where the driven sits, and how it moves: an angle (degrees) about its centre, or along the rack (mm)
41
+ const per = kind === 'rack' ? (Math.PI * ga.pitch) / 360 : m.ratio, r2 = kind === 'rack' ? null : gb.pitch / 2;
42
+ const base = kind === 'rack' ? rackPhase(ga, 0) : phase(ga, gb, kind === 'internal' ? 180 : 0);
43
+ const O2 = kind === 'internal' ? [-c, 0] : kind === 'rack' ? null : [c, 0];
44
+ const P = kind === 'internal' ? [ga.pitch / 2, 0] : kind === 'rack' ? [0, c - ga.pitch / 2] : [ga.pitch / 2, 0], reach = 3.5 * mod;
45
+ const drivenAt = (deg, nudge) => kind === 'rack'
46
+ ? B.map(([x, y]) => [x + per * deg + base + nudge - Math.PI * mod * Math.round(gb.teeth / 2), y]) // the rack under the pinion, its middle under the pitch point
47
+ : turn(B, (m.ratio * deg + base + nudge) * DEG, O2);
48
+ const driverAt = (deg) => turn(A, deg * DEG, kind === 'rack' ? [0, c] : [0, 0]);
49
+ // the turn (degrees, or mm for a rack) each way from the ratio's place to where the outlines first meet
50
+ const span = kind === 'rack' ? Math.PI * mod / 2 : 180 / gb.teeth;
51
+ const meet = (deg, dir) => {
52
+ const D = near(driverAt(deg), P, reach), crosses = (x) => cross(D, near(drivenAt(deg, dir * x), P, reach));
53
+ if (!crosses(span)) return Infinity;
54
+ let lo = 0, hi = span;
55
+ for (let i = 0; i < 40; i++) { const mid = (lo + hi) / 2; if (crosses(mid)) hi = mid; else lo = mid; }
56
+ return lo;
57
+ };
58
+ const rows = [];
59
+ for (let i = 0; i < steps; i++) {
60
+ const deg = (i * 360) / ga.teeth / steps, D = near(driverAt(deg), P, reach);
61
+ const clash = cross(D, near(drivenAt(deg, 0), P, reach));
62
+ if (clash) { rows.push({ angle: deg, interferes: true }); continue; }
63
+ // the driven pushed on (it lags until its flank meets the driver's: the driver turning +, the driven its way)
64
+ const ahead = meet(deg, Math.sign(per)), behind = meet(deg, -Math.sign(per));
65
+ const toMm = (x) => (kind === 'rack' ? x : x * DEG * r2);
66
+ rows.push({ angle: deg, backlash: toMm(ahead + behind), error: toMm(behind) });
67
+ }
68
+ const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
69
+ const bad = rows.filter((x) => x.interferes);
70
+ const ok = rows.filter((x) => !x.interferes), bl = ok.map((x) => x.backlash), te = ok.map((x) => x.error);
71
+ const least = Math.min(...bl), most = Math.max(...bl), swing = te.length ? Math.max(...te) - Math.min(...te) : 0;
72
+ if (bad.length) f('fail', `the teeth cross where the ratio puts them at ${bad.length} of ${steps} steps (from ${fmt(bad[0].angle, 2)}° of the driver): they interfere, and jam`, 'mesh.interference', { at: bad.map((x) => x.angle) });
73
+ if (ok.length) {
74
+ if (least < 0.02) f('warn', `as drawn, the play falls to ${fmt(least)} mm: printed teeth that come out a little fat will bind`, 'mesh.backlash', { least, most });
75
+ else f('pass', `as drawn, ${fmt(least)}${fmt(most) !== fmt(least) ? ` to ${fmt(most)}` : ''} mm of play at the pitch circle (${fmt(m.backlash)} mm reckoned)`, 'mesh.backlash', { least, most });
76
+ f('info', `transmission error ${fmt(swing * 1000, 0)} µm across a tooth, as drawn (outlines of ${A.length / ga.teeth} points a tooth)`, 'mesh.error', { swing });
77
+ }
78
+ return { rows, backlash: { least, most }, error: swing, reckoned: m.backlash, findings: out };
79
+ }
package/gears.d.mts CHANGED
@@ -170,6 +170,19 @@ export function printable(g: GearModel, printer: {
170
170
  nozzle?: number;
171
171
  wall_min?: number;
172
172
  }, width?: number): any[];
173
+ /**
174
+ * How much of the power a stage passes on: DEFAULTS.efficiency per mesh a tooth force goes through. A planetary's
175
+ * goes through two (sun to planet, planet to ring); a worm's is its own, from its lead angle and friction.
176
+ * @param {'spur' | 'helical' | 'herringbone' | 'rack' | 'internal' | 'bevel' | 'planetary' | 'worm'} kind
177
+ * @param {{ efficiency?: number, worm?: { efficiency: number } }} [o] efficiency: your measured figure per mesh
178
+ * @returns {number} from 0 to 1
179
+ */
180
+ export function stageEfficiency(kind: "spur" | "helical" | "herringbone" | "rack" | "internal" | "bevel" | "planetary" | "worm", o?: {
181
+ efficiency?: number;
182
+ worm?: {
183
+ efficiency: number;
184
+ };
185
+ }): number;
173
186
  /** The Lewis form factor Y for z teeth (20° full depth); Infinity for a rack. @param {number} z */
174
187
  export function lewisFactor(z: number): number;
175
188
  /**
@@ -198,6 +211,118 @@ export function strength(g: GearModel, o?: {
198
211
  torque: number;
199
212
  findings: any[];
200
213
  };
214
+ /**
215
+ * A straight bevel gear's numbers, for a mate of `mate` teeth with the shafts at `shaftAngle` (90° by default): the
216
+ * pitch cone angle δ (tan δ = sin Σ / (z₂/z₁ + cos Σ)), the cone distance R (to the apex along the pitch cone), the
217
+ * virtual teeth z / cos δ of its back cone (Tredgold), and the face width's limit R / 3. Module and diameters at the
218
+ * back (large) end.
219
+ * @param {{ module: number, teeth: number, mate: number, shaftAngle?: number, width?: number, pressureAngle?: number, backlash?: number }} p
220
+ */
221
+ export function bevelModel(p: {
222
+ module: number;
223
+ teeth: number;
224
+ mate: number;
225
+ shaftAngle?: number;
226
+ width?: number;
227
+ pressureAngle?: number;
228
+ backlash?: number;
229
+ }): {
230
+ kind: string;
231
+ mate: any;
232
+ shaftAngle: number;
233
+ pitchAngle: number;
234
+ coneDistance: number;
235
+ virtualTeeth: number;
236
+ width: any;
237
+ height: number;
238
+ smallEnd: number;
239
+ module: number;
240
+ teeth: number;
241
+ /**
242
+ * degrees
243
+ */
244
+ pressureAngle: number;
245
+ /**
246
+ * times the module
247
+ */
248
+ shift: number;
249
+ /**
250
+ * mm, this gear's share is half
251
+ */
252
+ backlash: number;
253
+ /**
254
+ * pitch diameter, module × teeth
255
+ */
256
+ pitch: number;
257
+ /**
258
+ * base circle diameter
259
+ */
260
+ base: number;
261
+ /**
262
+ * tip diameter
263
+ */
264
+ tip: number;
265
+ /**
266
+ * root diameter
267
+ */
268
+ root: number;
269
+ /**
270
+ * tooth thickness on the pitch circle (an arc, mm), less half the backlash
271
+ */
272
+ thickness: number;
273
+ /**
274
+ * π × module
275
+ */
276
+ circularPitch: number;
277
+ /**
278
+ * π × module × cos(pressure angle)
279
+ */
280
+ basePitch: number;
281
+ /**
282
+ * the tooth's width across its tip (mm)
283
+ */
284
+ topLand: number;
285
+ /**
286
+ * the tooth's width where it meets the root (mm)
287
+ */
288
+ rootThickness: number;
289
+ /**
290
+ * the basic rack it was drawn from
291
+ */
292
+ rack: {
293
+ pressureAngle: number;
294
+ addendum: number;
295
+ dedendum: number;
296
+ rootFillet: number;
297
+ tipRound: number;
298
+ };
299
+ };
300
+ /**
301
+ * A straight bevel gear: its back (large) end on the bed at z = 0, its axis up z toward the cone's apex, the spur
302
+ * profile of its back end scaled toward the apex up the face width, so every tooth runs straight to the apex and the
303
+ * pitch surface is the cone. (A printable approximation to the octoidal profile: close for teeth of 12 or more.)
304
+ * @param {{ module: number, teeth: number, mate: number, shaftAngle?: number, width?: number, bore?: number, chamfer?: number }} p
305
+ */
306
+ export function bevel(p: {
307
+ module: number;
308
+ teeth: number;
309
+ mate: number;
310
+ shaftAngle?: number;
311
+ width?: number;
312
+ bore?: number;
313
+ chamfer?: number;
314
+ }): jscad.geometries.geom3.Geom3;
315
+ /**
316
+ * How two bevel gears mesh: turns of b for one of a, the cone angles (they add to the shaft angle), and the contact
317
+ * ratio of their back cones' equivalent spur gears (Tredgold). @param {object} a @param {object} b the gears' parameters
318
+ */
319
+ export function bevelMesh(a: object, b: object): {
320
+ ratio: number;
321
+ pitchAngles: number[];
322
+ coneDistance: number;
323
+ contact: any;
324
+ findings: any[];
325
+ };
201
326
  /**
202
327
  * The defaults, each with the rule it comes from. Coefficients are times the module.
203
328
  * @type {Record<string, { value: number, unit: string, rule: string, kind: string }>}
@@ -208,6 +333,8 @@ export const DEFAULTS: Record<string, {
208
333
  rule: string;
209
334
  kind: string;
210
335
  }>;
336
+ export { contact } from "./contact.mjs";
337
+ export { toothStress } from "./tooth.mjs";
211
338
  /**
212
339
  * width up z; chamfer at the bed face (and the top); bore and flat for a shaft
213
340
  */
@@ -293,3 +420,4 @@ export type GearModel = {
293
420
  };
294
421
  import jscad from '@jscad/modeling';
295
422
  export { planetary, planetaryRatio } from "./planetary.mjs";
423
+ export { wormModel, worm, wormWheel, wormMesh } from "./worm.mjs";
package/gears.mjs CHANGED
@@ -27,6 +27,7 @@ export const DEFAULTS = {
27
27
  tipRound: { value: 0.1, unit: '× module', rule: 'A tip a printer can draw: rounded about a quarter line at module 1, so no corner is left to the slicer', kind: 'estimate' },
28
28
  backlash: { value: 0.15, unit: 'mm', rule: 'Play between mating teeth, split between the two: FDM gear guides give 0.1 to 0.2 mm; measure it with the sampler plate', kind: 'estimate' },
29
29
  chamfer: { value: 0.3, unit: 'mm', rule: 'A chamfer round the bed face, so the first layer\'s squish (elephant\'s foot) doesn\'t widen the teeth there', kind: 'estimate' },
30
+ efficiency: { value: 0.95, unit: 'per mesh', rule: 'Power a printed mesh passes on, dry: metal spur gears pass 98 to 99%, and dry plastic teeth rub more. Measure yours', kind: 'estimate' },
30
31
  module: { value: 0.8, unit: 'mm at a 0.4 mm nozzle', rule: 'The smallest module worth printing: twice the nozzle, so a tooth is three lines at the pitch circle', kind: 'estimate' },
31
32
  };
32
33
  const D = (k) => DEFAULTS[k].value;
@@ -253,6 +254,7 @@ function loop({ teeth: z, thickness: s, r, rb, ra, rf, fillet, round, grow = 0 }
253
254
  flankT: (rho) => -hAt(rho), point: (t, rho) => [rho * Math.cos(t), rho * Math.sin(t)],
254
255
  });
255
256
  const tooth = [...h.pts, [ra, 0], ...h.pts.slice(1).reverse().map(([x, y]) => [x, -y])];
257
+ if (!Number.isInteger(z)) return { tooth, fillet: h.fillet, round: h.round };
256
258
  const out = [];
257
259
  for (let k = 0; k < z; k++) {
258
260
  const c = Math.cos((TAU * k) / z), sn = Math.sin((TAU * k) / z);
@@ -324,7 +326,7 @@ export function rackProfile(p, grow = 0) {
324
326
 
325
327
  // A solid from loops at heights: each slice is the loops at a height, grown (a chamfer: drawn smaller there) and turned
326
328
  // (a helix), all with the same points, so the walls join point to point. Caps are triangulated with their holes.
327
- function loft(profile, width, { chamfer, twist, slide }) {
329
+ function loft(profile, width, { chamfer, twist, slide, scale }) {
328
330
  const [bottom, top] = Array.isArray(chamfer) ? chamfer : [chamfer, 0];
329
331
  if (bottom + top >= width) throw new Error(`the chamfers (${bottom} and ${top} mm) are as tall as the gear (${width} mm)`);
330
332
  // heights: the chamfers' ends, and for a twist enough between that no slice turns more than a degree or so
@@ -335,10 +337,11 @@ function loft(profile, width, { chamfer, twist, slide }) {
335
337
  zs.push(width / 2); // (a herringbone's apex)
336
338
  }
337
339
  if (slide) { const n = Math.ceil(width / 0.5); for (let i = 1; i < n; i++) zs.push((width * i) / n); zs.push(width / 2); }
340
+ if (scale) for (let i = 1; i < 8; i++) zs.push((width * i) / 8); // (straight lines toward the apex: a few slices keep the caps' triangles sound)
338
341
  const heights = [...new Set(zs.map((z) => +z.toFixed(9)))].sort((x, y) => x - y);
339
342
  const slices = heights.map((z) => {
340
343
  const grow = z < bottom ? -(bottom - z) : z > width - top ? -(z - (width - top)) : 0;
341
- const loops = profile(grow), turn = twist ? twist(z) : 0, c = Math.cos(turn), s = Math.sin(turn), dx = slide ? slide(z) : 0;
344
+ const loops = profile(grow, z), turn = twist ? twist(z) : 0, k = scale ? scale(z) : 1, c = Math.cos(turn) * k, s = Math.sin(turn) * k, dx = slide ? slide(z) : 0;
342
345
  const at = (p) => [p[0] * c - p[1] * s + dx, p[0] * s + p[1] * c];
343
346
  const sides = loops.flatMap((pts) => pts.map((p, i) => [at(p), at(pts[(i + 1) % pts.length])]));
344
347
  return slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, z]), slice.fromSides(sides));
@@ -473,9 +476,13 @@ export function mesh(a, b, { centre, width } = {}) {
473
476
  // further apart opens a gap along the line of action of 2 Δa sin αw, which is 2 Δa tan αw round the pitch circle
474
477
  const added = 2 * off * Math.tan(aw);
475
478
  backlash += added;
476
- if (off < -0.01) f('fail', `the axes are ${fmt(-off, 3)} mm closer than these gears need (${fmt(need, 3)} mm${b.kind === 'rack' ? ', centre to the rack\'s pitch line' : ''}): the teeth jam`, 'mesh.centre', { centre, need });
479
+ // closer than they need eats the backlash (2 Δa tan αw of it) and the clearance under the tips (0.25 m); past
480
+ // either, the teeth jam
481
+ const tipRoom = m * (Math.min(a.rack.dedendum, b.rack?.dedendum ?? a.rack.dedendum) - Math.max(a.rack.addendum, b.rack?.addendum ?? a.rack.addendum)) + off;
482
+ if (backlash < 0 || tipRoom < 0) f('fail', `the axes are ${fmt(-off, 3)} mm closer than these gears need (${fmt(need, 3)} mm${b.kind === 'rack' ? ', centre to the rack\'s pitch line' : ''}): ${backlash < 0 ? `more than their ${fmt(a.backlash / 2 + b.backlash / 2, 2)} mm of backlash allows` : 'the tips reach the roots'}, and the teeth jam`, 'mesh.centre', { centre, need });
483
+ else if (off < -0.005 && backlash < 0.05) f('warn', `the axes are ${fmt(-off, 3)} mm closer than these gears need (${fmt(need, 3)} mm): ${fmt(backlash, 3)} mm of play left, tight enough to bind as printed`, 'mesh.centre', { centre, need });
477
484
  else if (backlash > 0.4) f('warn', `the axes are ${fmt(off, 3)} mm further apart than these gears need (${fmt(need, 3)} mm): ${fmt(backlash, 2)} mm of backlash, loose enough to knock`, 'mesh.centre', { centre, need });
478
- else f('pass', `centre distance ${fmt(centre, 3)} mm, as they need${Math.abs(off) > 0.005 ? ` (${off > 0 ? '+' : ''}${fmt(off, 3)})` : ''}: ${fmt(backlash, 2)} mm of backlash`, 'mesh.centre', { centre, need });
485
+ else f('pass', `centre distance ${fmt(centre, 3)} mm, ${Math.abs(off) > 0.005 ? `${fmt(Math.abs(off), 3)} mm ${off > 0 ? 'further apart' : 'closer'} than the ${fmt(need, 3)} they need` : 'as they need'}: ${fmt(backlash, 2)} mm of backlash`, 'mesh.centre', { centre, need });
479
486
  }
480
487
  const thrust = a.herringbone || !ha ? 0 : Math.tan(ha * DEG);
481
488
  return { kind: b.kind, centre: need, ratio, contact, overlap, thrust, pressureAngle: aw / DEG, backlash, text: sense, findings: out };
@@ -550,7 +557,29 @@ export function printable(g, printer, width) {
550
557
  }
551
558
 
552
559
  // Planetary gearboxes: the teeth for a ratio, laid out in phase
560
+ /**
561
+ * How much of the power a stage passes on: DEFAULTS.efficiency per mesh a tooth force goes through. A planetary's
562
+ * goes through two (sun to planet, planet to ring); a worm's is its own, from its lead angle and friction.
563
+ * @param {'spur' | 'helical' | 'herringbone' | 'rack' | 'internal' | 'bevel' | 'planetary' | 'worm'} kind
564
+ * @param {{ efficiency?: number, worm?: { efficiency: number } }} [o] efficiency: your measured figure per mesh
565
+ * @returns {number} from 0 to 1
566
+ */
567
+ export function stageEfficiency(kind, o = {}) {
568
+ const each = o.efficiency ?? D('efficiency');
569
+ if (!(typeof each === 'number' && each > 0 && each <= 1)) throw new Error(`stageEfficiency: efficiency is from 0 to 1 (got ${shown(o.efficiency)})`);
570
+ if (kind === 'worm') { if (!o.worm || !(o.worm.efficiency > 0)) throw new Error('stageEfficiency: a worm needs its model ({ worm: wormModel(…) }), for its lead angle and friction'); return o.worm.efficiency; }
571
+ if (['spur', 'helical', 'herringbone', 'rack', 'internal', 'bevel'].includes(kind)) return each;
572
+ if (kind === 'planetary') return each ** 2;
573
+ throw new Error(`stageEfficiency: no kind of stage ${shown(kind)} (spur, helical, herringbone, rack, internal, bevel, planetary or worm)`);
574
+ }
575
+
553
576
  export { planetary, planetaryRatio } from './planetary.mjs';
577
+ // The mesh measured from the drawn teeth: backlash, transmission error, interference
578
+ export { contact } from './contact.mjs';
579
+ // A tooth's root stress by finite elements
580
+ export { toothStress } from './tooth.mjs';
581
+ // Worms and their wheels
582
+ export { wormModel, worm, wormWheel, wormMesh } from './worm.mjs';
554
583
 
555
584
  // ---------- strength ----------
556
585
 
@@ -600,3 +629,71 @@ export function strength(g, o = {}) {
600
629
  } else if (r) f('info', `the teeth carry ${fmt(result.torque / 1000, 3)} N·m at ${safety}× under ${name}'s ${sigma} MPa (Lewis, Y ${fmt(Y, 3)})`, 'gear.strength', { torque: result.torque });
601
630
  return result;
602
631
  }
632
+
633
+ // ---------- bevel gears ----------
634
+
635
+ /**
636
+ * A straight bevel gear's numbers, for a mate of `mate` teeth with the shafts at `shaftAngle` (90° by default): the
637
+ * pitch cone angle δ (tan δ = sin Σ / (z₂/z₁ + cos Σ)), the cone distance R (to the apex along the pitch cone), the
638
+ * virtual teeth z / cos δ of its back cone (Tredgold), and the face width's limit R / 3. Module and diameters at the
639
+ * back (large) end.
640
+ * @param {{ module: number, teeth: number, mate: number, shaftAngle?: number, width?: number, pressureAngle?: number, backlash?: number }} p
641
+ */
642
+ export function bevelModel(p) {
643
+ const what = 'bevelModel', c = common(what, p), z = whole(what, 'teeth', p?.teeth, 6), mate = whole(what, 'mate', p?.mate, 6);
644
+ const sigma = between(what, 'shaftAngle', p.shaftAngle ?? 90, 30, 150, '°') * DEG, a = c.pressureAngle * DEG;
645
+ const delta = Math.atan2(Math.sin(sigma), mate / z + Math.cos(sigma)), d = c.module * z, R = d / (2 * Math.sin(delta));
646
+ const virtual = z / Math.cos(delta);
647
+ const need = c.addendum - (virtual * Math.sin(a) ** 2) / 2;
648
+ if (c.shift < need - 0.01) throw new Error(`${what}: ${z} teeth on a ${(delta / DEG).toFixed(1)}° cone are undercut (as ${virtual.toFixed(1)} on their back cone): give a profile shift of at least ${(Math.ceil(need * 100) / 100).toFixed(2)}, or more teeth`);
649
+ const width = p.width === undefined ? R / 3 : size(what, 'width', p.width);
650
+ if (width > R / 3 + 1e-9) throw new Error(`${what}: a face width of ${width} mm is over a third of the cone distance (${(R / 3).toFixed(2)} mm): the small end's teeth get too small to carry anything`);
651
+ const g = gearModel({ ...p, teeth: z, helix: 0 });
652
+ return { ...g, kind: 'bevel', mate, shaftAngle: sigma / DEG, pitchAngle: delta / DEG, coneDistance: R, virtualTeeth: virtual, width, height: width * Math.cos(delta), smallEnd: 1 - width / R };
653
+ }
654
+
655
+ /**
656
+ * A straight bevel gear: its back (large) end on the bed at z = 0, its axis up z toward the cone's apex, the spur
657
+ * profile of its back end scaled toward the apex up the face width, so every tooth runs straight to the apex and the
658
+ * pitch surface is the cone. (A printable approximation to the octoidal profile: close for teeth of 12 or more.)
659
+ * @param {{ module: number, teeth: number, mate: number, shaftAngle?: number, width?: number, bore?: number, chamfer?: number }} p
660
+ */
661
+ export function bevel(p) {
662
+ const g = bevelModel(p), h = g.height, H = g.coneDistance * Math.cos(g.pitchAngle * DEG), scale = (z) => 1 - z / H;
663
+ const chamfer = chamfers('bevel', { ...p, chamfer: p.chamfer ?? 0 }, h);
664
+ if (p.bore != null && !(size('bevel', 'bore', p.bore) < g.root * g.smallEnd - 4 * g.module * g.smallEnd)) throw new Error(`bevel: a bore of ${p.bore} mm leaves under two modules of web under the small end's teeth`);
665
+ return loft((grow, z) => [bevelSection(g, grow), ...(p.bore != null ? [reversed(bore(p.bore, p.flat, -grow)).map(([x, y]) => [x / scale(z), y / scale(z)])] : [])], h, { chamfer, scale });
666
+ }
667
+
668
+ // A bevel gear's section across its axis at its back end, by Tredgold's construction: the tooth drawn on the back
669
+ // cone, developed flat as a spur gear of z / cos δ teeth; wrapped back onto the back cone (heights along its slant,
670
+ // angles × 1 / cos δ); then seen from the apex onto the plane of the back end's pitch circle. Every section nearer the
671
+ // apex is this one scaled toward the axis, the teeth running straight to the apex.
672
+ function bevelSection(g, grow = 0) {
673
+ const d = g.pitchAngle * DEG, m = g.module, zv = g.virtualTeeth, a = g.pressureAngle * DEG;
674
+ const rv = (m * zv) / 2, r = g.pitch / 2, H = r / Math.tan(d); // the developed pitch radius; the apex over the pitch circle
675
+ const { tooth } = loop({ teeth: zv, thickness: g.thickness, r: rv, rb: rv * Math.cos(a), ra: rv + m * (g.rack.addendum + g.shift), rf: rv - m * (g.rack.dedendum - g.shift), fillet: g.rack.rootFillet * m, round: g.rack.tipRound * m, grow });
676
+ const mapped = tooth.map(([x, y]) => {
677
+ const rho = Math.hypot(x, y), phi = Math.atan2(y, x) / Math.cos(d); // along the back cone's slant from its apex; round the axis
678
+ const q = rho * Math.cos(d), zq = -r * Math.tan(d) + rho * Math.sin(d); // on the back cone: radius, height
679
+ const k = q * (H / (H - zq)); // seen from the apex, on the plane z = 0
680
+ return [k * Math.cos(phi), k * Math.sin(phi)];
681
+ });
682
+ const out = [];
683
+ for (let i = 0; i < g.teeth; i++) { const c = Math.cos((TAU * i) / g.teeth), s = Math.sin((TAU * i) / g.teeth); for (const [x, y] of mapped) out.push([x * c - y * s, x * s + y * c]); }
684
+ return out;
685
+ }
686
+
687
+ /**
688
+ * How two bevel gears mesh: turns of b for one of a, the cone angles (they add to the shaft angle), and the contact
689
+ * ratio of their back cones' equivalent spur gears (Tredgold). @param {object} a @param {object} b the gears' parameters
690
+ */
691
+ export function bevelMesh(a, b) {
692
+ const ga = bevelModel({ ...a, mate: b.teeth }), gb = bevelModel({ ...b, mate: a.teeth });
693
+ const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
694
+ if (Math.abs(ga.shaftAngle - gb.shaftAngle) > 1e-9) f('fail', `shaft angles differ (${ga.shaftAngle}° and ${gb.shaftAngle}°)`, 'mesh.bevel');
695
+ const virtual = (g) => gearModel({ module: g.module, teeth: Math.max(6, Math.round(g.virtualTeeth)), shift: Math.max(g.shift, 0), pressureAngle: g.pressureAngle });
696
+ const back = mesh(virtual(ga), virtual(gb));
697
+ for (const x of back.findings) out.push({ ...x, text: `on the back cones: ${x.text}` });
698
+ return { ratio: ga.teeth / gb.teeth, pitchAngles: [ga.pitchAngle, gb.pitchAngle], coneDistance: ga.coneDistance, contact: back.contact, findings: out };
699
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/gears",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Gears for printed parts: involute spur gears, racks and ring gears drawn from their rules, with their numbers, how a pair meshes (centre distance, ratio, contact ratio, phase) and how they print.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,9 +16,15 @@
16
16
  "files": [
17
17
  "gears.mjs",
18
18
  "planetary.mjs",
19
+ "contact.mjs",
20
+ "tooth.mjs",
21
+ "worm.mjs",
19
22
  "diagram.mjs",
20
23
  "gears.d.mts",
21
24
  "planetary.d.mts",
25
+ "contact.d.mts",
26
+ "tooth.d.mts",
27
+ "worm.d.mts",
22
28
  "diagram.d.mts",
23
29
  "README.md",
24
30
  "LICENSE"
@@ -51,5 +57,8 @@
51
57
  "peerDependencies": {
52
58
  "@jscad/modeling": "^2.12.0"
53
59
  },
54
- "types": "./gears.d.mts"
60
+ "types": "./gears.d.mts",
61
+ "dependencies": {
62
+ "@rulecad/fea": "^0.2.9"
63
+ }
55
64
  }
package/tooth.d.mts ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * A tooth's root stress by finite elements, at a torque on the gear.
3
+ * @param {object} p the gear's parameters (as gearModel takes them)
4
+ * @param {{ width: number, torque: number, material: { E: number, nu: number, strength?: number, name?: string }, cells?: number }} o
5
+ * cells: across a module (16)
6
+ */
7
+ export function toothStress(p: object, { width, torque, material, cells }?: {
8
+ width: number;
9
+ torque: number;
10
+ material: {
11
+ E: number;
12
+ nu: number;
13
+ strength?: number;
14
+ name?: string;
15
+ };
16
+ cells?: number;
17
+ }): {
18
+ peak: number;
19
+ at: number[];
20
+ lewis: number;
21
+ ratio: number;
22
+ cells: number[];
23
+ change: number;
24
+ findings: any[];
25
+ };
package/tooth.mjs ADDED
@@ -0,0 +1,72 @@
1
+ // One tooth analysed: the tooth and a slice of rim under it as a flat part of finite elements (@rulecad/fea, plane
2
+ // stress), the rim's cut edges held, the load at the tip along the line of action. The peak stress at the root, with
3
+ // the fillet and the printed shape as they are, against Lewis's number, which leaves the fillet's concentration out.
4
+ import { grid, analyse, positions } from '@rulecad/fea';
5
+ import { gearModel, spurProfile, lewisFactor } from './gears.mjs';
6
+
7
+ const fmt = (x, d = 1) => String(+x.toFixed(d));
8
+ const inside = ([x, y], pts) => { let c = false; for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) { const [xi, yi] = pts[i], [xj, yj] = pts[j]; if ((yi > y) !== (yj > y) && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) c = !c; } return c; };
9
+ const toSeg = ([px, py], [ax, ay], [bx, by]) => { const dx = bx - ax, dy = by - ay, t = Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / (dx * dx + dy * dy || 1))); return Math.hypot(px - ax - t * dx, py - ay - t * dy); };
10
+
11
+ /**
12
+ * A tooth's root stress by finite elements, at a torque on the gear.
13
+ * @param {object} p the gear's parameters (as gearModel takes them)
14
+ * @param {{ width: number, torque: number, material: { E: number, nu: number, strength?: number, name?: string }, cells?: number }} o
15
+ * cells: across a module (16)
16
+ */
17
+ export function toothStress(p, { width, torque, material, cells = 32 } = {}) {
18
+ const fine = solveTooth(p, { width, torque, material, cells }), coarse = solveTooth(p, { width, torque, material, cells: Math.round(cells * 0.75) });
19
+ const change = fine.peak / coarse.peak - 1, out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
20
+ const { peak, lewis, h, nx, ny, atRoot } = fine, name = material.name ?? 'the material', n = material.strength ? material.strength / peak : null;
21
+ f(n == null ? 'info' : n >= 2 ? 'pass' : n >= 1 ? 'warn' : 'fail', `a tooth analysed (${nx} × ${ny} cells of ${fmt(h, 3)} mm): ${fmt(peak)} MPa at the root of its loaded side, ${fmt(atRoot, 2)} mm above the root circle, ${fmt(peak / lewis, 2)}× Lewis's ${fmt(lewis)} MPa (the fillet's concentration)${n == null ? '' : n >= 1 ? `: ${fmt(n)}× under ${name}'s ${material.strength} MPa` : `: over ${name}'s ${material.strength} MPa, it breaks`}`, 'gear.fe', { peak, lewis });
22
+ if (Math.abs(change) > 0.05) f('warn', `the peak changed ${fmt(change * 100)}% from cells a third bigger: not settled; more cells`, 'gear.fe.coarse', { change });
23
+ return { peak, at: fine.at, lewis, ratio: peak / lewis, cells: [nx, ny], change, findings: out };
24
+ }
25
+
26
+ function solveTooth(p, { width, torque, material, cells }) {
27
+ const g = gearModel(p), m = g.module, z = g.teeth, a = (g.pressureAngle * Math.PI) / 180;
28
+ if (!(width > 0) || !(torque > 0)) throw new Error(`toothStress: width and torque are sizes above 0 (got ${width} and ${torque})`);
29
+ if (!(material?.E > 0)) throw new Error('toothStress: the material needs a modulus E (MPa) and a Poisson\'s ratio nu');
30
+ // the tooth on +x between the middles of its spaces, and the rim under it two and a half modules deep
31
+ const loop = spurProfile(p).loops[0], half = Math.PI / z, rf = g.root / 2, rr = rf - 2.5 * m;
32
+ const tooth = loop.filter(([x, y]) => Math.abs(Math.atan2(y, x)) <= half + 1e-9 && x > 0).sort((u, v) => Math.atan2(u[1], u[0]) - Math.atan2(v[1], v[0]));
33
+ const arc = Array.from({ length: 13 }, (_, i) => { const t = half - (2 * half * i) / 12; return [rr * Math.cos(t), rr * Math.sin(t)]; });
34
+ const poly = [...tooth, ...arc];
35
+ // a grid over it, each cell as much solid as its 3 × 3 samples say
36
+ const h = m / cells, x0 = Math.min(...poly.map((q) => q[0])), y0 = Math.min(...poly.map((q) => q[1]));
37
+ const nx = Math.ceil((Math.max(...poly.map((q) => q[0])) - x0) / h), ny = Math.ceil((Math.max(...poly.map((q) => q[1])) - y0) / h);
38
+ const G = grid({ nx, ny, h }), density = new Float64Array(G.cells);
39
+ for (let j = 0; j < ny; j++) for (let i = 0; i < nx; i++) {
40
+ let n = 0;
41
+ for (let s = 0; s < 3; s++) for (let t = 0; t < 3; t++) if (inside([x0 + (i + (s + 0.5) / 3) * h, y0 + (j + (t + 0.5) / 3) * h], poly)) n++;
42
+ density[j * nx + i] = n / 9;
43
+ }
44
+ // held: the rim's cut edges (the two radial cuts and the arc under them)
45
+ const at = positions(G).map(([x, y]) => [x + x0, y + y0]), fixed = new Uint8Array(G.dofs);
46
+ const cutA = [[rf * Math.cos(half), rf * Math.sin(half)], [rr * Math.cos(half), rr * Math.sin(half)]], cutB = [[rf * Math.cos(-half), rf * Math.sin(-half)], [rr * Math.cos(-half), rr * Math.sin(-half)]];
47
+ for (let n = 0; n < G.nodes; n++) {
48
+ const q = at[n], r = Math.hypot(...q), near = toSeg(q, ...cutA) < 0.75 * h || toSeg(q, ...cutB) < 0.75 * h || (Math.abs(r - rr) < 0.75 * h && Math.abs(Math.atan2(q[1], q[0])) <= half);
49
+ if (near) fixed[2 * n] = fixed[2 * n + 1] = 1;
50
+ }
51
+ // the load: the mate's push on the lower flank near the tip, along the flank's normal (the line of action), F_t / cos α
52
+ const Ft = torque / (g.pitch / 2), Fn = Ft / Math.cos(a), ra = g.tip / 2;
53
+ const flank = tooth.filter(([x, y]) => y < 0 && Math.hypot(x, y) < ra - 0.2 * m && Math.hypot(x, y) > ra - 0.6 * m);
54
+ const k = Math.floor(flank.length / 2), P = flank[k] ?? tooth[1], Q = flank[Math.min(k + 1, flank.length - 1)] ?? tooth[2];
55
+ const tx = Q[0] - P[0], ty = Q[1] - P[1], tl = Math.hypot(tx, ty) || 1, nIn = [-ty / tl, tx / tl]; // (inward: the tooth is on the left of its outline)
56
+ const load = new Float64Array(G.dofs), touch = [];
57
+ for (let n = 0; n < G.nodes; n++) if (Math.hypot(at[n][0] - P[0], at[n][1] - P[1]) < 0.15 * m) touch.push(n);
58
+ if (!touch.length) throw new Error('toothStress: no node near the load: more cells');
59
+ for (const n of touch) { load[2 * n] = (Fn * nIn[0]) / touch.length; load[2 * n + 1] = (Fn * nIn[1]) / touch.length; }
60
+ const r = analyse({ grid: G, density, material: { E: material.E, nu: material.nu }, thickness: width, fixed, loads: [load], method: 'direct' });
61
+ // the peak on the loaded side (where the root is in tension, as Lewis and the strength mean), away from where the
62
+ // load goes in (a point load's own stress is a property of the model, not the tooth)
63
+ let peak = 0, where = null;
64
+ for (let j = 0; j < ny; j++) for (let i = 0; i < nx; i++) {
65
+ const e = j * nx + i; if (density[e] < 0.5) continue;
66
+ const c = [x0 + (i + 0.5) * h, y0 + (j + 0.5) * h];
67
+ if (c[1] > 0 || Math.hypot(c[0] - P[0], c[1] - P[1]) < 0.6 * m) continue;
68
+ if (r.stress[0][e] > peak) { peak = r.stress[0][e]; where = c; }
69
+ }
70
+ const lewis = Ft / (width * m * lewisFactor(z));
71
+ return { peak, at: where, lewis, h, nx, ny, atRoot: Math.hypot(...where) - rf };
72
+ }
package/worm.d.mts ADDED
@@ -0,0 +1,72 @@
1
+ /**
2
+ * A worm's numbers. module: its axial module (the wheel's transverse module); starts: threads round it (1 to 4);
3
+ * diameter: its pitch diameter (default 10 modules, a stout worm); friction: the coefficient between the flanks
4
+ * (0.25 for printed PLA on PLA, an estimate) for whether it locks and how efficient it is.
5
+ * @param {{ module: number, starts?: number, diameter?: number, pressureAngle?: number, backlash?: number, hand?: 'right' | 'left', friction?: number }} p
6
+ */
7
+ export function wormModel(p: {
8
+ module: number;
9
+ starts?: number;
10
+ diameter?: number;
11
+ pressureAngle?: number;
12
+ backlash?: number;
13
+ hand?: "right" | "left";
14
+ friction?: number;
15
+ }): {
16
+ kind: string;
17
+ module: any;
18
+ starts: number;
19
+ pitch: any;
20
+ tip: any;
21
+ root: number;
22
+ axialPitch: number;
23
+ lead: number;
24
+ hand: "right" | "left";
25
+ pressureAngle: number;
26
+ backlash: number;
27
+ leadAngle: number;
28
+ friction: number;
29
+ frictionAngle: number;
30
+ locks: boolean;
31
+ efficiency: number;
32
+ };
33
+ /**
34
+ * A worm: the thread along z, from z = 0 to its length, its axis on z.
35
+ * @param {{ module: number, starts?: number, diameter?: number, length: number, bore?: number, pressureAngle?: number, backlash?: number, hand?: 'right' | 'left' }} p
36
+ */
37
+ export function worm(p: {
38
+ module: number;
39
+ starts?: number;
40
+ diameter?: number;
41
+ length: number;
42
+ bore?: number;
43
+ pressureAngle?: number;
44
+ backlash?: number;
45
+ hand?: "right" | "left";
46
+ }): jscad.geometries.geom3.Geom3;
47
+ /**
48
+ * The wheel a worm drives: a helical gear at the worm's lead angle, of the worm's hand, its transverse module the
49
+ * worm's axial module. @param {object} wormSpec the worm's parameters @param {{ teeth: number, width: number, bore?: number, shift?: number }} p
50
+ */
51
+ export function wormWheel(wormSpec: object, p: {
52
+ teeth: number;
53
+ width: number;
54
+ bore?: number;
55
+ shift?: number;
56
+ }): any;
57
+ /**
58
+ * How a worm drives its wheel: the centre distance (axes crossed at 90°), turns of the wheel for one of the worm,
59
+ * whether it locks, and how efficient it is. @param {object} wormSpec @param {{ teeth: number, shift?: number }} wheel
60
+ */
61
+ export function wormMesh(wormSpec: object, wheel: {
62
+ teeth: number;
63
+ shift?: number;
64
+ }): {
65
+ centre: number;
66
+ ratio: number;
67
+ locks: boolean;
68
+ efficiency: number;
69
+ leadAngle: number;
70
+ findings: any[];
71
+ };
72
+ import jscad from '@jscad/modeling';
package/worm.mjs ADDED
@@ -0,0 +1,92 @@
1
+ // Worms and their wheels. A worm is a screw whose thread, cut along its axis, is the basic rack's tooth; its wheel is
2
+ // a helical gear at the worm's lead angle, the usual printable wheel (not throated: the two touch at a point, which
3
+ // printed teeth, a little soft, spread). One turn of a worm of s starts moves its wheel s teeth. With a lead angle
4
+ // under the friction angle it locks: the wheel can't turn the worm.
5
+ import jscad from '@jscad/modeling';
6
+ import { rackProfile, helical, gearModel } from './gears.mjs';
7
+
8
+ const { extrudeFromSlices, slice } = jscad.extrusions;
9
+ const { mat4 } = jscad.maths;
10
+ const DEG = Math.PI / 180;
11
+ const fmt = (x, d = 3) => String(+x.toFixed(d));
12
+ const isNum = (x) => typeof x === 'number' && Number.isFinite(x);
13
+ function size(what, k, v) { if (!(isNum(v) && v > 0)) throw new Error(`${what}: ${k} is a size above 0 (got ${JSON.stringify(v)})`); return v; }
14
+
15
+ /**
16
+ * A worm's numbers. module: its axial module (the wheel's transverse module); starts: threads round it (1 to 4);
17
+ * diameter: its pitch diameter (default 10 modules, a stout worm); friction: the coefficient between the flanks
18
+ * (0.25 for printed PLA on PLA, an estimate) for whether it locks and how efficient it is.
19
+ * @param {{ module: number, starts?: number, diameter?: number, pressureAngle?: number, backlash?: number, hand?: 'right' | 'left', friction?: number }} p
20
+ */
21
+ export function wormModel(p) {
22
+ const what = 'wormModel';
23
+ if (!p || typeof p !== 'object') throw new Error(`${what}: give it { module, starts, diameter }`);
24
+ const m = size(what, 'module', p.module), starts = p.starts ?? 1;
25
+ if (!(Number.isInteger(starts) && starts >= 1 && starts <= 4)) throw new Error(`${what}: starts is a whole number from 1 to 4 (got ${JSON.stringify(p.starts)})`);
26
+ const d = p.diameter === undefined ? 10 * m : size(what, 'diameter', p.diameter);
27
+ if (d < 2 * 1.25 * m + 4 * m) throw new Error(`${what}: a pitch diameter of ${d} mm leaves under two modules of core under the threads (at least ${fmt(6.5 * m)} mm)`);
28
+ const mu = p.friction ?? 0.25, axialPitch = Math.PI * m, lead = starts * axialPitch;
29
+ const gamma = Math.atan(lead / (Math.PI * d)), phi = Math.atan(mu), alpha = (p.pressureAngle ?? 20) * DEG;
30
+ // efficiency driving from the worm, through the normal pressure angle: tan γ (cos αn − μ tan γ) / (cos αn tan γ + μ)
31
+ const an = Math.atan(Math.tan(alpha) * Math.cos(gamma));
32
+ const efficiency = (Math.tan(gamma) * (Math.cos(an) - mu * Math.tan(gamma))) / (Math.cos(an) * Math.tan(gamma) + mu);
33
+ return {
34
+ kind: 'worm', module: m, starts, pitch: d, tip: d + 2 * m, root: d - 2.5 * m, axialPitch, lead, hand: p.hand ?? 'right',
35
+ pressureAngle: alpha / DEG, backlash: p.backlash ?? 0.15, leadAngle: gamma / DEG, friction: mu, frictionAngle: phi / DEG,
36
+ locks: gamma < phi, efficiency,
37
+ };
38
+ }
39
+
40
+ /**
41
+ * A worm: the thread along z, from z = 0 to its length, its axis on z.
42
+ * @param {{ module: number, starts?: number, diameter?: number, length: number, bore?: number, pressureAngle?: number, backlash?: number, hand?: 'right' | 'left' }} p
43
+ */
44
+ export function worm(p) {
45
+ const w = wormModel(p), length = size('worm', 'length', p.length), m = w.module, r = w.pitch / 2;
46
+ // the thread's axial section: the basic rack's tooth, its pitch line on the pitch radius (heights above it outward)
47
+ const teeth = Math.max(2, w.starts * 2);
48
+ const [loop] = rackProfile({ module: m, teeth, pressureAngle: w.pressureAngle, backlash: w.backlash, height: m }).loops;
49
+ const p0 = w.axialPitch, top = loop.slice(2).reverse().filter(([x]) => x >= p0 && x <= 2 * p0); // one pitch of the top outline, along x
50
+ const heightAt = (u) => { // the rack's height above its pitch line at u along it (one pitch repeating)
51
+ const x = p0 + (((u % p0) + p0) % p0);
52
+ for (let i = 1; i < top.length; i++) if (top[i][0] >= x) { const [a, b] = [top[i - 1], top[i]]; return a[1] + ((b[1] - a[1]) * (x - a[0])) / (b[0] - a[0] || 1); }
53
+ return top.at(-1)[1];
54
+ };
55
+ // the section across the axis at z = 0: a point at angle θ is on the thread's axial section at u = −lead θ / 2π
56
+ const sense = w.hand === 'left' ? -1 : 1, n = 90 * w.starts;
57
+ const section = Array.from({ length: n }, (_, i) => { const t = (2 * Math.PI * i) / n, rho = r + heightAt((-sense * w.lead * t) / (2 * Math.PI)); return [rho * Math.cos(t), rho * Math.sin(t)]; });
58
+ const hole = p.bore ? Array.from({ length: 48 }, (_, i) => { const t = (-2 * Math.PI * i) / 48; return [(p.bore / 2) * Math.cos(t), (p.bore / 2) * Math.sin(t)]; }) : null;
59
+ if (p.bore && !(p.bore < w.root - 2 * m)) throw new Error(`worm: a bore of ${p.bore} mm leaves under a module of wall under the threads`);
60
+ // up the axis the section turns: a turn for each lead, the thread's own helix
61
+ const steps = Math.max(2, Math.ceil((length / w.lead) * 72)), slices = [];
62
+ for (let k = 0; k <= steps; k++) {
63
+ const z = (length * k) / steps, a = (sense * 2 * Math.PI * z) / w.lead, c = Math.cos(a), s = Math.sin(a), at = ([x, y]) => [x * c - y * s, x * s + y * c];
64
+ const sides = [section, ...(hole ? [hole] : [])].flatMap((pts) => pts.map((q, i) => [at(q), at(pts[(i + 1) % pts.length])]));
65
+ slices.push(slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, z]), slice.fromSides(sides)));
66
+ }
67
+ return extrudeFromSlices({ numberOfSlices: slices.length, repair: false, callback: (t, i) => slices[i] }, slices[0]);
68
+ }
69
+
70
+ /**
71
+ * The wheel a worm drives: a helical gear at the worm's lead angle, of the worm's hand, its transverse module the
72
+ * worm's axial module. @param {object} wormSpec the worm's parameters @param {{ teeth: number, width: number, bore?: number, shift?: number }} p
73
+ */
74
+ export function wormWheel(wormSpec, p) {
75
+ const w = wormModel(wormSpec);
76
+ return helical({ module: w.module, teeth: p.teeth, width: p.width, bore: p.bore, shift: p.shift, pressureAngle: w.pressureAngle, backlash: w.backlash, helix: w.leadAngle, hand: w.hand, chamfer: p.chamfer });
77
+ }
78
+
79
+ /**
80
+ * How a worm drives its wheel: the centre distance (axes crossed at 90°), turns of the wheel for one of the worm,
81
+ * whether it locks, and how efficient it is. @param {object} wormSpec @param {{ teeth: number, shift?: number }} wheel
82
+ */
83
+ export function wormMesh(wormSpec, wheel) {
84
+ const w = wormModel(wormSpec), g = gearModel({ module: w.module, teeth: wheel.teeth, shift: wheel.shift ?? 0, pressureAngle: w.pressureAngle, helix: w.leadAngle, hand: w.hand });
85
+ const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
86
+ const ratio = w.starts / g.teeth;
87
+ f('info', `${g.teeth / w.starts} turns of the worm for one of the wheel; lead angle ${fmt(w.leadAngle, 2)}°`, 'worm.ratio', { ratio });
88
+ if (w.locks) f('pass', `it locks: the lead angle (${fmt(w.leadAngle, 2)}°) is under the friction angle (${fmt(w.frictionAngle, 1)}° at μ ${w.friction}), so the wheel can't turn the worm; ${fmt(w.efficiency * 100, 0)}% efficient driving`, 'worm.locks', { locks: true });
89
+ else f('info', `it doesn't lock: the lead angle (${fmt(w.leadAngle, 2)}°) is over the friction angle (${fmt(w.frictionAngle, 1)}°), so a load on the wheel can turn the worm back; ${fmt(w.efficiency * 100, 0)}% efficient driving`, 'worm.locks', { locks: false });
90
+ if (w.leadAngle > 25) f('warn', `a lead angle of ${fmt(w.leadAngle, 1)}° is steep for a worm: the wheel's teeth lean as much, and print poorly past about 45° at their tips`, 'worm.lead');
91
+ return { centre: (w.pitch + g.pitch) / 2 + (wheel.shift ?? 0) * w.module, ratio, locks: w.locks, efficiency: w.efficiency, leadAngle: w.leadAngle, findings: out };
92
+ }