@rulecad/gears 0.3.4 → 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/CHANGELOG.md CHANGED
@@ -4,6 +4,78 @@ What changed in each release, for someone upgrading a workspace. A **printed siz
4
4
  print (a gear's tip diameter, a tooth's thickness). Each release says whether any printed size moved, and why.
5
5
  Releases before this file was started are described in their pull requests on GitHub.
6
6
 
7
+ ## 0.4.0: unused names removed from the mesh drawing; mesh and contact judge a pair at the centre distance given, with its tips and its ring; needs Node 22.13 or later, the version the kit is tested on; a tooth's root stress is read at its surface
8
+
9
+ ### Changed
10
+
11
+ - Values the code worked out and never used are gone. No printed size moves, and nothing a design or a check does
12
+ changes.
13
+
14
+ - @rulecad/gears now states Node 22.13 or later in its `engines`, the same lowest Node as every rulecad package, and the
15
+ tests run on exactly Node 22.13.0 as well as the newest Node 24. Node 20 reached the end of its support on 30 April
16
+ 2026, and the kit's packages stated five different lowest versions, some of them too low: on Node 20.6 to 20.9 `cad`
17
+ stopped at start-up with "Unexpected token 'with'", and on Node 22.0 to 22.12 every `cad` command that runs a design
18
+ stops, because the sandbox designs run in cannot start there. npm gave no warning at install for either.
19
+
20
+ Upgrading: on a Node older than 22.13, npm now warns at install that this package needs Node 22.13; install Node 22.13
21
+ or later (24 recommended). Nothing else changes, and no printed size moves.
22
+
23
+ ### Already published
24
+
25
+ - The gear-mesh changes listed here (`mesh()` and `contact()` judging a pair at the centre given, and
26
+ `mesh.tipClearance`) are already in @rulecad/gears 0.3.5 as it went out on npm on 2026-11-29, though that version's
27
+ CHANGELOG did not say so (a line there now does): its publish run took the commit merged just after the release
28
+ (#573) in place of the release itself. A workspace already on 0.3.5 has them; this release only records them.
29
+
30
+ ### Fixed
31
+
32
+ - `mesh(a, b, { centre })` works out the contact ratio, and the working pressure angle it returns, at the centre
33
+ given, not at the one the gears need: 20 teeth on 40 at module 1 set 31 mm apart now fail `mesh.contact` at 0.74
34
+ (they passed at 1.63). Pairs set apart so far that the teeth never meet fail and say so, helical pairs too (a long
35
+ twist no longer makes up for teeth that don't reach), and so do axes so close that the base circles overlap, or at
36
+ no distance at all. A pair off its centre that passed before can now warn or fail.
37
+ - `mesh.centre` read a pinion in a ring the wrong way round: moved out from the ring's centre its teeth go deeper,
38
+ so it is tighter, not looser. It now says so, and a pinion pushed too far out fails.
39
+ - `contact()` fails a pair that never touches as drawn (`mesh.contact`, "the teeth don't touch at ... steps"), instead
40
+ of passing it with "Infinity mm of play"; its `backlash` is then `{ least: null, most: null }`. No finding prints
41
+ Infinity or NaN.
42
+ - `contact()` tests the outlines wherever the teeth can meet (inside both tip circles), not only within 3.5 modules of
43
+ the pitch point, so a pinion fouling its ring further round fails (20 teeth in a ring of 23, 24 or 26).
44
+ - A pinion in a ring is judged by KHK's trochoid interference condition, which honours shift and the centre, in place
45
+ of the rule that a ring needs 10 teeth more than its pinion: 20 in 28 and 52 in 60 now warn (they clear by the
46
+ backlash) instead of failing, and a shifted 12 in 24, which passed, now fails.
47
+
48
+ - `toothStress` read the root's peak stress at its cells' centres, half a cell inside the fillet. It now reads
49
+ `@rulecad/fea`'s surface stress, where the root's peak is: a module 2, 20-tooth PLA pinion 6 mm wide at 1 N m reads
50
+ 17.8 MPa where it read 16.6 (7% higher), and the reading moves under 1% from cells a third bigger, where it moved
51
+ 9% (`gear.fe.coarse` warned).
52
+
53
+ Upgrading: tooth stresses by finite elements (`gear.fe`) rise by a few percent, so a tooth near the 2× margin can now
54
+ warn. No printed size moves.
55
+
56
+ ### Added
57
+
58
+ - `mesh.tipClearance`: the room under the tips where the axes are. Shifted pairs lose it (18 on 18 both shifted a
59
+ whole module dig 0.165 mm into the roots); it fails below none, warns under a tenth of a module, and gives the
60
+ shortened `addendum` for each gear (ISO 21771's (x₁ + x₂ − y) module shortening) in `values.addendum`, reckoned at
61
+ the centre the gears need; tips that dig in because the axes are set too close are told to move the axes instead.
62
+ `mesh()` returns the room as `clearance`.
63
+
64
+ Upgrading: no printed size moves; tips are still drawn full. A shifted pair that `mesh.tipClearance` fails needs the
65
+ addendum it gives on each gear, which shortens the printed tips. Assemblies with gears off their nominal centre, or
66
+ rings close to their pinions, may give new warnings or failures in `cad check`.
67
+
68
+ ## 0.3.5: follows @rulecad/fea 0.3.0
69
+
70
+ Also in 0.3.5 as published (added after the release, to put the record straight): the publish run took the commit
71
+ merged just after the release, so 0.3.5 holds #573 too. `mesh()` and `contact()` judge a pair at the centre distance
72
+ given, with its tips and its ring, and `mesh.tipClearance` is new: a pair off its centre that passed can now warn or
73
+ fail. The next release's notes describe it in full.
74
+
75
+ ### Changed
76
+
77
+ - Takes @rulecad/fea 0.3.0.
78
+
7
79
  ## 0.3.4: the compound planetary printed in place (#162)
8
80
 
9
81
  ### Added
package/README.md CHANGED
@@ -24,7 +24,7 @@ const solid = spur({ module: 1, teeth: 18, width: 6, bore: 5 }); // a JSCAD soli
24
24
 
25
25
  ```text
26
26
  30 -0.4286 1.63 0.15
27
- pass mesh.contact, pass mesh.centre
27
+ pass mesh.contact, pass mesh.tipClearance, pass mesh.centre
28
28
  4.286
29
29
  18 16.914 20 15.5
30
30
  pass: module 1: 3.6 lines across a tooth at its pitch circle
@@ -75,14 +75,36 @@ What every gear takes:
75
75
  - **`centre`**: how far apart the axes must be (for a rack, the pinion's axis above the rack's pitch line). Shifted
76
76
  gears move apart by their working pressure angle.
77
77
  - **`ratio`**: turns of b for each turn of a, negative when they turn opposite ways. For a rack, mm of travel a turn.
78
- - **`contact`**: the contact ratio, how many pairs of teeth share the load on average. Under 1 the drive jumps; under
79
- 1.2 it is rough.
78
+ - **`contact`**: the contact ratio, how many pairs of teeth share the load on average, where the axes are: at the
79
+ `centre` you gave, or else at the one they need. Under 1 the drive jumps; under 1.2 it is rough. Apart from their
80
+ place the gears roll at a new working pressure angle (cos α_w = (r_b1 ± r_b2) / a) and the teeth overlap less: 20
81
+ teeth on 40 at module 1 share 1.63 pairs at their 30 mm, 1.17 at 30.5 mm and 0.74 at 31 mm, where the drive jumps.
82
+ `pressureAngle` is the working angle at that centre too. A pair whose teeth don't reach across fails, whatever a
83
+ helical's twist would add, and so do axes so close that the base circles overlap.
84
+ - **`clearance`**: the room under the tips, in mm, at that centre (0.25 module as drawn).
80
85
  - **`backlash`**: the play, with the centre you gave.
81
86
  - **`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 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
84
- `mesh.pressure` (gears that can't mesh), `mesh.interference` (a ring too near its pinion's size), `mesh.helix` (helical
85
- gears that cross: outer gears that mesh are of opposite hands; a pinion and its ring or rack, of the same hand).
87
+ - **`findings`**: `mesh.contact` (each finding names the centre when you gave one off the gears' own), `mesh.centre`
88
+ (axes closer than the gears need eat their backlash, 2 Δa tan α of it, and past it jam the teeth; too far apart,
89
+ they knock), `mesh.tipClearance`, `mesh.module` and `mesh.pressure` (gears that can't mesh), `mesh.interference`
90
+ (a pinion's tips fouling its ring's), `mesh.helix` (helical gears that cross: outer gears that mesh are of opposite
91
+ hands; a pinion and its ring or rack, of the same hand).
92
+
93
+ **Tips of shifted gears.** Profile shift pushes a gear's tips out further than it pushes the pair apart, so two
94
+ shifted gears lose the room under their tips they were drawn with: 18 teeth on 18, both shifted a whole module, have
95
+ none, and the tips dig 0.165 mm into the roots. ISO 21771 and KHK shorten each tip by (x₁ + x₂ − y) modules, y being
96
+ how far the shift moved the centre. Each gear is drawn on its own, without its mate, so its tips are drawn full;
97
+ `mesh.tipClearance` fails a pair whose tips reach the mate's roots, warns under a tenth of a module of room, and says
98
+ the `addendum` that gives the room back (it is in the finding's `values.addendum`, the first gear's and the
99
+ second's, reckoned at the centre the gears need: tips that dig in only because the axes are set too close are told to
100
+ move the axes). For KHK's worked pair (module 3, 12 and 24 teeth shifted 0.6 and 0.36) that is 0.873 on both, which gives
101
+ the tips KHK lists, 44.84 and 79.40 mm.
102
+
103
+ **A pinion in a ring.** As each pinion tooth leaves the ring's space its tip swings out past the corner of the ring's
104
+ tooth. `mesh.interference` tests that by KHK's trochoid condition, which honours the shift and the centre, and
105
+ allows each flank a quarter of each gear's backlash: 20 teeth in a ring of 26 or 27 fail, 28 clear by the backlash
106
+ alone (a warn), 30 clear outright. It no longer goes by the number of teeth between them, which failed rings that
107
+ clear and passed shifted pinions that don't (12 shifted 0.3 in 24).
86
108
 
87
109
  `phase(a, b, angle)` is the turn that puts a tooth of a in a space of b: b's angle is `ratio × a's angle + phase`,
88
110
  with `angle` the direction from a's centre to b's. `rackPhase(pinion, at)` does the same for a rack.
@@ -166,7 +188,13 @@ and at each step turns the driven gear each way until its outline meets the driv
166
188
  - **backlash**: the turn between the two meetings, as play at the pitch circle (`mesh.backlash`, a warn when it falls
167
189
  under 0.02 mm);
168
190
  - **transmission error**: how far the driven gear's forward meeting wanders across a tooth (`mesh.error`);
169
- - **interference**: steps where the outlines already cross where the ratio puts them (`mesh.interference`).
191
+ - **interference**: steps where the outlines already cross where the ratio puts them (`mesh.interference`);
192
+ - **lost contact**: steps where the driven gear turns half a tooth either way without meeting the driver, so the axes
193
+ are too far apart for the teeth to drive (`mesh.contact`, a fail). The backlash is then left out, and `backlash` is
194
+ `{ least: null, most: null }` if no step touched.
195
+
196
+ It tests every edge where the teeth can meet: inside both tip circles, which for outer gears is close round the pitch
197
+ point and for a pinion in a ring can be much of the way round.
170
198
 
171
199
  `a` and `b` are the parameters the models take; `b.kind` is `'internal'` for a ring or `'rack'` for a rack. For 18
172
200
  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
@@ -238,14 +266,15 @@ a point.
238
266
  | Undercut | none when z sin²α / 2 ≥ h_a − x (17 teeth at 20°) | ISO 21771; KHK |
239
267
  | Working pressure angle | inv α_w = inv α + 2 tan α (x₁ + x₂) / (z₁ + z₂) | ISO 21771 |
240
268
  | Centre distance | a = m (z₁ + z₂) cos α / 2 cos α_w; a ring: z₂ − z₁ | ISO 21771 |
241
- | Contact ratio | (√(r_a1² − r_b1²) + √(r_a2² − r_b2²) − a sin α_w) / (π m cos α) | Dudley (1984); KHK |
242
- | Rack contact | (√(r_a² − r_b²) − r sin α + m (h_a − x) / sin α) / (π m cos α) | KHK |
269
+ | Contact ratio | (√(r_a1² − r_b1²) + √(r_a2² − r_b2²) − a sin α_w) / (π m cos α), at the centre a used, cos α_w = (r_b1 + r_b2) / a | Dudley (1984); KHK |
270
+ | Rack contact | (√(r_a² − r_b²) − r sin α + (m h_a − (a − r)) / sin α) / (π m cos α), a the pinion's axis above the pitch line | KHK |
271
+ | Room under the tips | a − r_a1 − r_f2 (a ring: r_f2 − a − r_a1 and r_a2 − a − r_f1); shifted tips shortened by (x₁ + x₂ − y) m | ISO 21771; KHK |
243
272
  | Root fillet | 0.38 m | ISO 53 profile A |
244
273
  | Helical twist | the profile turns z tan β / r up the width | ISO 21771 (transverse module) |
245
274
  | Overlap ratio | ε_β = b tan β / π m (each half of a herringbone on its own) | ISO 21771 |
246
275
  | Thrust | F_a = F_t tan β | Dudley (1984) |
247
276
  | Flank lean at the tip | tan β_a = tan β · r_a / r | ISO 21771 |
248
- | Ring and pinion | at least 10 teeth apart (12 to be safe) at 20° | KHK, internal gears |
277
+ | Ring and pinion (trochoid interference) | θ₁ z₁/z₂ + inv α_w − inv α_a2 ≥ θ₂; θ₁ = acos((r_a2² − r_a1² − a²) / 2a r_a1) + inv α_a1 − inv α_w, θ₂ = acos((a² + r_a2² − r_a1²) / 2a r_a2) | KHK, internal gears |
249
278
  | Planetary ratio | (ω_s − ω_c) / (ω_r − ω_c) = −R / S; ring held, sun in: 1 + R / S | Willis; Dudley (1984), ch. 3 |
250
279
  | Planetary assembly | R = S + 2P; (S + R) / n whole; 2a sin(π / n) > d_a,planet | KHK, planetary gears |
251
280
  | Compound play at the output | j_s \|1 − r2/r1\| / 2 + j_r1 (1 + r2/r1) / 2 + j_r2, over the output ring's pitch radius | The three contacts' slips along their pitch circles, planets free of a carrier (this package) |
package/contact.d.mts CHANGED
@@ -13,6 +13,13 @@ export function contact(a: object, b: object & {
13
13
  rows: ({
14
14
  angle: number;
15
15
  interferes: boolean;
16
+ apart?: undefined;
17
+ backlash?: undefined;
18
+ error?: undefined;
19
+ } | {
20
+ angle: number;
21
+ apart: boolean;
22
+ interferes?: undefined;
16
23
  backlash?: undefined;
17
24
  error?: undefined;
18
25
  } | {
@@ -20,6 +27,7 @@ export function contact(a: object, b: object & {
20
27
  backlash: any;
21
28
  error: any;
22
29
  interferes?: undefined;
30
+ apart?: undefined;
23
31
  })[];
24
32
  backlash: {
25
33
  least: number;
package/contact.mjs CHANGED
@@ -1,29 +1,76 @@
1
1
  // The mesh measured, not reckoned: the drawn teeth turned through a pitch, and at each step the driven gear turned
2
2
  // each way until its outline meets the driver's. The turn between the two meetings is the backlash as drawn (with the
3
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 './core.mjs';
4
+ // transmission error; outlines that cross where the ratio puts them interfere; a driven gear that turns half a tooth
5
+ // either way and meets nothing has lost contact. Plain 2D geometry: the outlines' edges where the teeth can meet (the
6
+ // lens where each gear's teeth reach inside the other's tip circle: by the pitch point for outer gears, round much of
7
+ // the ring for a pinion close in a ring), tested for crossing.
8
+ import {
9
+ spurProfile,
10
+ internalProfile,
11
+ rackProfile,
12
+ gearModel,
13
+ internalModel,
14
+ rackModel,
15
+ mesh,
16
+ phase,
17
+ rackPhase,
18
+ } from './core.mjs';
7
19
 
8
20
  const DEG = Math.PI / 180;
9
21
  const fmt = (x, d = 3) => String(+x.toFixed(d));
10
22
 
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;
23
+ // Two edges crossing (each strictly through the other)
24
+ function crosses([p, q], [r, s]) {
25
+ const d1 = (s[0] - r[0]) * (p[1] - r[1]) - (s[1] - r[1]) * (p[0] - r[0]),
26
+ d2 = (s[0] - r[0]) * (q[1] - r[1]) - (s[1] - r[1]) * (q[0] - r[0]);
27
+ const d3 = (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]),
28
+ d4 = (q[0] - p[0]) * (s[1] - p[1]) - (q[1] - p[1]) * (s[0] - p[0]);
29
+ return d1 * d2 < 0 && d3 * d4 < 0;
30
+ }
31
+ // A set of edges filed in square cells of side h, so each edge of the other set is tested only against those near it
32
+ // (a pinion close in a ring meets it round much of its turn, and every edge against every edge is slow)
33
+ function filed(A, h) {
34
+ const cells = new Map();
35
+ for (const e of A) {
36
+ const [p, q] = e;
37
+ for (let i = Math.floor(Math.min(p[0], q[0]) / h); i <= Math.floor(Math.max(p[0], q[0]) / h); i++) {
38
+ for (let j = Math.floor(Math.min(p[1], q[1]) / h); j <= Math.floor(Math.max(p[1], q[1]) / h); j++) {
39
+ const k = i * 65536 + j,
40
+ list = cells.get(k);
41
+ if (list) list.push(e);
42
+ else cells.set(k, [e]);
43
+ }
20
44
  }
21
45
  }
22
- return false;
46
+ return (B) => {
47
+ for (const e of B) {
48
+ const [r, s] = e;
49
+ for (let i = Math.floor(Math.min(r[0], s[0]) / h); i <= Math.floor(Math.max(r[0], s[0]) / h); i++) {
50
+ for (let j = Math.floor(Math.min(r[1], s[1]) / h); j <= Math.floor(Math.max(r[1], s[1]) / h); j++) {
51
+ const list = cells.get(i * 65536 + j);
52
+ if (list) for (const d of list) if (crosses(d, e)) return true;
53
+ }
54
+ }
55
+ }
56
+ return false;
57
+ };
23
58
  }
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; };
59
+ const turn = (pts, a, [cx, cy] = [0, 0], [dx, dy] = [0, 0]) => {
60
+ const c = Math.cos(a),
61
+ s = Math.sin(a);
62
+ return pts.map(([x, y]) => [x * c - y * s + cx + dx, x * s + y * c + cy + dy]);
63
+ };
64
+ // Only the loop's edges with an end where the mate's teeth can reach (the rest never meet)
65
+ const edgesWhere = (pts, by) => {
66
+ const out = [];
67
+ for (let i = 0; i < pts.length; i++) {
68
+ const p = pts[i],
69
+ q = pts[(i + 1) % pts.length];
70
+ if (by(p) || by(q)) out.push([p, q]);
71
+ }
72
+ return out;
73
+ };
27
74
 
28
75
  /**
29
76
  * The mesh of a pair as drawn: backlash, transmission error and interference across one pitch of the driver.
@@ -33,47 +80,124 @@ const near = (pts, [px, py], r) => { const by = ([x, y]) => (x - px) ** 2 + (y -
33
80
  */
34
81
  export function contact(a, b, { centre, steps = 24 } = {}) {
35
82
  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;
83
+ const ga = gearModel(a),
84
+ gb = kind === 'internal' ? internalModel(b) : kind === 'rack' ? rackModel(b) : gearModel(b);
85
+ const m = mesh(ga, gb, centre === undefined ? {} : { centre }),
86
+ c = centre ?? m.centre,
87
+ mod = ga.module;
38
88
  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];
89
+ const B =
90
+ kind === 'internal'
91
+ ? internalProfile(b).loops[1]
92
+ : kind === 'rack'
93
+ ? rackProfile(b).loops[0]
94
+ : spurProfile(b).loops[0];
40
95
  // 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;
96
+ const per = kind === 'rack' ? (Math.PI * ga.pitch) / 360 : m.ratio,
97
+ r2 = kind === 'rack' ? null : gb.pitch / 2;
42
98
  const base = kind === 'rack' ? rackPhase(ga, 0) : phase(ga, gb, kind === 'internal' ? 180 : 0);
43
99
  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]);
100
+ // Where the teeth can meet: the driver's edges inside the driven's tip circle (outside it, for a ring; under the
101
+ // rack's tip line), the driven's inside the driver's tip circle, each with a little to spare for the edges' length.
102
+ // (It was once a circle of 3.5 modules round the pitch point, which missed a pinion fouling its ring further round.)
103
+ const O1 = kind === 'rack' ? [0, c] : [0, 0],
104
+ spare = 0.25 * mod,
105
+ ra1 = ga.tip / 2 + spare;
106
+ const d2 = ([x, y], [px, py]) => (x - px) ** 2 + (y - py) ** 2;
107
+ const inDriven =
108
+ kind === 'rack'
109
+ ? ([, y]) => y < gb.tipLine + spare
110
+ : kind === 'internal'
111
+ ? (p) => d2(p, O2) > (gb.tip / 2 - spare) ** 2
112
+ : (p) => d2(p, O2) < (gb.tip / 2 + spare) ** 2;
113
+ const inDriver = (p) => d2(p, O1) < ra1 * ra1,
114
+ cell = 0.5 * mod;
115
+ const drivenAt = (deg, nudge) =>
116
+ kind === 'rack'
117
+ ? 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
118
+ : turn(B, (m.ratio * deg + base + nudge) * DEG, O2);
119
+ const driverAt = (deg) => turn(A, deg * DEG, O1);
49
120
  // 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;
121
+ const span = kind === 'rack' ? (Math.PI * mod) / 2 : 180 / gb.teeth;
51
122
  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; }
123
+ const D = filed(edgesWhere(driverAt(deg), inDriven), cell),
124
+ hits = (x) => D(edgesWhere(drivenAt(deg, dir * x), inDriver));
125
+ if (!hits(span)) return Infinity;
126
+ let lo = 0,
127
+ hi = span;
128
+ for (let i = 0; i < 40; i++) {
129
+ const mid = (lo + hi) / 2;
130
+ if (hits(mid)) hi = mid;
131
+ else lo = mid;
132
+ }
56
133
  return lo;
57
134
  };
58
135
  const rows = [];
59
136
  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; }
137
+ const deg = (i * 360) / ga.teeth / steps,
138
+ D = filed(edgesWhere(driverAt(deg), inDriven), cell);
139
+ const clash = D(edgesWhere(drivenAt(deg, 0), inDriver));
140
+ if (clash) {
141
+ rows.push({ angle: deg, interferes: true });
142
+ continue;
143
+ }
63
144
  // 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));
145
+ const ahead = meet(deg, Math.sign(per)),
146
+ behind = meet(deg, -Math.sign(per));
147
+ // half a tooth either way and nothing met: no tooth is there to push (or to hold it back)
148
+ if (!Number.isFinite(ahead) || !Number.isFinite(behind)) {
149
+ rows.push({ angle: deg, apart: true });
150
+ continue;
151
+ }
65
152
  const toMm = (x) => (kind === 'rack' ? x : x * DEG * r2);
66
153
  rows.push({ angle: deg, backlash: toMm(ahead + behind), error: toMm(behind) });
67
154
  }
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) });
155
+ const out = [],
156
+ f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
157
+ const bad = rows.filter((x) => x.interferes),
158
+ apart = rows.filter((x) => x.apart);
159
+ const ok = rows.filter((x) => !x.interferes && !x.apart),
160
+ bl = ok.map((x) => x.backlash),
161
+ te = ok.map((x) => x.error);
162
+ const least = ok.length ? Math.min(...bl) : null,
163
+ most = ok.length ? Math.max(...bl) : null,
164
+ swing = te.length ? Math.max(...te) - Math.min(...te) : 0;
165
+ if (bad.length)
166
+ f(
167
+ 'fail',
168
+ `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`,
169
+ 'mesh.interference',
170
+ { at: bad.map((x) => x.angle) },
171
+ );
172
+ if (apart.length)
173
+ f(
174
+ 'fail',
175
+ `the teeth don't touch at ${apart.length} of ${steps} steps (from ${fmt(apart[0].angle, 2)}° of the driver): turned half a tooth either way, the driven meets nothing, so at the ${fmt(c)} mm centre${centre !== undefined && Math.abs(c - m.centre) > 0.005 ? ` (${fmt(m.centre)} mm is where they mesh)` : ''} they lose contact and slip: bring the axes closer`,
176
+ 'mesh.contact',
177
+ { at: apart.map((x) => x.angle), centre: c },
178
+ );
73
179
  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 });
180
+ if (least < 0.02)
181
+ f(
182
+ 'warn',
183
+ `as drawn, the play falls to ${fmt(least)} mm: printed teeth that come out a little fat will bind`,
184
+ 'mesh.backlash',
185
+ { least, most },
186
+ );
187
+ else
188
+ f(
189
+ 'pass',
190
+ `as drawn, ${fmt(least)}${fmt(most) !== fmt(least) ? ` to ${fmt(most)}` : ''} mm of play at the pitch circle (${fmt(m.backlash)} mm reckoned)`,
191
+ 'mesh.backlash',
192
+ { least, most },
193
+ );
194
+ f(
195
+ 'info',
196
+ `transmission error ${fmt(swing * 1000, 0)} µm across a tooth, as drawn (outlines of ${A.length / ga.teeth} points a tooth)`,
197
+ 'mesh.error',
198
+ { swing },
199
+ );
77
200
  }
201
+ // (least and most are null when no step had the teeth touching both ways)
78
202
  return { rows, backlash: { least, most }, error: swing, reckoned: m.backlash, findings: out };
79
203
  }