@rulecad/gears 0.3.2 → 0.3.4
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 +44 -0
- package/README.md +28 -1
- package/contact.mjs +1 -1
- package/core.d.mts +419 -0
- package/core.mjs +698 -0
- package/diagram.mjs +1 -1
- package/gears.d.mts +2 -420
- package/gears.mjs +5 -685
- package/package.json +4 -1
- package/planetary.d.mts +38 -2
- package/planetary.mjs +67 -6
- package/tooth.mjs +1 -1
- package/worm.mjs +1 -1
package/core.mjs
ADDED
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
// Gears for printed parts: involute spur gears, racks and internal (ring) gears drawn from their rules, each with its
|
|
2
|
+
// numbers (pitch, base, tip and root diameters, tooth thickness), how a pair meshes (centre distance, ratio, contact
|
|
3
|
+
// ratio, the phase that puts a tooth in a gap) and how it prints. The involute relations are the standard ones (ISO 53
|
|
4
|
+
// and ISO 21771; Dudley, Handbook of Practical Gear Design, 1984; KHK, Gear Technical Reference); the printing rules
|
|
5
|
+
// are the kit's own, from the printer's line and layer.
|
|
6
|
+
//
|
|
7
|
+
// Every gear is drawn flat: its axis along z, its first tooth on +x, its face on the bed at z = 0. A spur gear printed
|
|
8
|
+
// this way has its teeth in the layers, where the filament is strongest, and needs no supports. Units: mm, degrees.
|
|
9
|
+
//
|
|
10
|
+
// This is the package's core: the code planetary.mjs, contact.mjs, tooth.mjs, worm.mjs and diagram.mjs build on. They
|
|
11
|
+
// import it, and gears.mjs gathers them all, so no module imports one that imports it back (a loop that a loader
|
|
12
|
+
// without cycle support, such as the JSCAD web app's, goes round until the stack runs out: issue #278).
|
|
13
|
+
import jscad from '@jscad/modeling';
|
|
14
|
+
|
|
15
|
+
const { extrudeFromSlices, slice } = jscad.extrusions;
|
|
16
|
+
const { mat4 } = jscad.maths;
|
|
17
|
+
|
|
18
|
+
const DEG = Math.PI / 180;
|
|
19
|
+
const inv = (a) => Math.tan(a) - a; // the involute function
|
|
20
|
+
const TAU = 2 * Math.PI;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The defaults, each with the rule it comes from. Coefficients are times the module.
|
|
24
|
+
* @type {Record<string, { value: number, unit: string, rule: string, kind: string }>}
|
|
25
|
+
*/
|
|
26
|
+
export const DEFAULTS = {
|
|
27
|
+
pressureAngle: { value: 20, unit: 'deg', rule: 'The standard pressure angle (ISO 53): stronger teeth than 14.5°, and the one tables and cutters use', kind: 'spec' },
|
|
28
|
+
addendum: { value: 1, unit: '× module', rule: 'Tooth height above the pitch circle (ISO 53 basic rack)', kind: 'spec' },
|
|
29
|
+
dedendum: { value: 1.25, unit: '× module', rule: 'Depth below the pitch circle: the addendum and 0.25 module of clearance under the mate\'s tip (ISO 53)', kind: 'spec' },
|
|
30
|
+
rootFillet: { value: 0.38, unit: '× module', rule: 'The root fillet of the ISO 53 profile A rack: a rounded root is where a tooth stops cracking', kind: 'spec' },
|
|
31
|
+
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' },
|
|
32
|
+
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' },
|
|
33
|
+
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' },
|
|
34
|
+
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' },
|
|
35
|
+
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' },
|
|
36
|
+
printGap: { value: 0.3, unit: 'mm', rule: 'The gap each side between gears printed in place, in mesh, that still prints apart: the X1C\'s print_gap. An estimate until the fit test plate measures it; the sampler plate\'s gears bound on pegs printed 0.105 mm clear each side, so it is no smaller than that', kind: 'estimate' },
|
|
37
|
+
helix: { value: 30, unit: 'deg', rule: 'The helix angle of a herringbone printed in place: steep enough that the rings cannot walk off the planets, and each half prints at 30° from the layers', kind: 'default' },
|
|
38
|
+
};
|
|
39
|
+
const D = (k) => DEFAULTS[k].value;
|
|
40
|
+
|
|
41
|
+
// ---------- refusing what isn't a gear ----------
|
|
42
|
+
|
|
43
|
+
const isNum = (x) => typeof x === 'number' && Number.isFinite(x);
|
|
44
|
+
const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
|
|
45
|
+
/** A size above 0, by name. */
|
|
46
|
+
function size(what, k, v) { if (!(isNum(v) && v > 0)) throw new Error(`${what}: ${k} is a size above 0 (got ${shown(v)})`); return v; }
|
|
47
|
+
function whole(what, k, v, least) { if (!(Number.isInteger(v) && v >= least)) throw new Error(`${what}: ${k} is a whole number of ${least} or more (got ${shown(v)})`); return v; }
|
|
48
|
+
function between(what, k, v, lo, hi, unit = '') { if (!(isNum(v) && v >= lo && v <= hi)) throw new Error(`${what}: ${k} is a number from ${lo} to ${hi}${unit} (got ${shown(v)})`); return v; }
|
|
49
|
+
function atLeast0(what, k, v) { if (!(isNum(v) && v >= 0)) throw new Error(`${what}: ${k} is 0 or more (got ${shown(v)})`); return v; }
|
|
50
|
+
|
|
51
|
+
// The parameters every gear shares, checked and with their defaults
|
|
52
|
+
function common(what, p) {
|
|
53
|
+
if (!p || typeof p !== 'object') throw new Error(`${what}: give the gear's parameters as an object ({ module, teeth, … }), not ${shown(p)}`);
|
|
54
|
+
const module = size(what, 'module', p.module);
|
|
55
|
+
const pressureAngle = between(what, 'pressureAngle', p.pressureAngle ?? D('pressureAngle'), 10, 35, '°');
|
|
56
|
+
const shift = between(what, 'shift', p.shift ?? 0, -1, 1.5, ' (times the module)');
|
|
57
|
+
const backlash = atLeast0(what, 'backlash', p.backlash ?? D('backlash'));
|
|
58
|
+
const addendum = between(what, 'addendum', p.addendum ?? D('addendum'), 0.5, 1.5, ' (times the module)');
|
|
59
|
+
const dedendum = between(what, 'dedendum', p.dedendum ?? D('dedendum'), addendum, 2, ' (times the module, at least the addendum)');
|
|
60
|
+
const rootFillet = between(what, 'rootFillet', p.rootFillet ?? D('rootFillet'), 0, 0.6, ' (times the module)');
|
|
61
|
+
const tipRound = between(what, 'tipRound', p.tipRound ?? D('tipRound'), 0, 0.4, ' (times the module)');
|
|
62
|
+
// Helical teeth: the spur profile turned along the width by the helix angle (the module and pressure angle are the
|
|
63
|
+
// transverse ones, in the gear's own plane, so a helical gear's centre distance is a spur gear's)
|
|
64
|
+
const helix = between(what, 'helix', p.helix ?? 0, 0, 45, '°');
|
|
65
|
+
if (p.hand != null && p.hand !== 'right' && p.hand !== 'left') throw new Error(`${what}: hand is "right" or "left" (got ${shown(p.hand)})`);
|
|
66
|
+
if (p.herringbone != null && typeof p.herringbone !== 'boolean') throw new Error(`${what}: herringbone is true or false (got ${shown(p.herringbone)})`);
|
|
67
|
+
if (p.herringbone && !helix) throw new Error(`${what}: a herringbone needs a helix angle (say helix: 30)`);
|
|
68
|
+
return { module, pressureAngle, shift, backlash, addendum, dedendum, rootFillet, tipRound, helix, hand: p.hand ?? 'right', herringbone: !!p.herringbone };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ---------- the numbers ----------
|
|
72
|
+
|
|
73
|
+
/** @typedef {{ module: number, teeth: number, pressureAngle?: number, shift?: number, backlash?: number, addendum?: number, dedendum?: number, rootFillet?: number, tipRound?: number }} GearSpec */
|
|
74
|
+
/**
|
|
75
|
+
* @typedef {object} GearModel
|
|
76
|
+
* @property {'external' | 'internal'} kind
|
|
77
|
+
* @property {number} module @property {number} teeth @property {number} pressureAngle degrees @property {number} shift times the module
|
|
78
|
+
* @property {number} backlash mm, this gear's share is half
|
|
79
|
+
* @property {number} pitch pitch diameter, module × teeth @property {number} base base circle diameter @property {number} tip tip diameter
|
|
80
|
+
* @property {number} root root diameter @property {number} thickness tooth thickness on the pitch circle (an arc, mm), less half the backlash
|
|
81
|
+
* @property {number} circularPitch π × module @property {number} basePitch π × module × cos(pressure angle)
|
|
82
|
+
* @property {number} topLand the tooth's width across its tip (mm) @property {number} rootThickness the tooth's width where it meets the root (mm)
|
|
83
|
+
* @property {{ pressureAngle: number, addendum: number, dedendum: number, rootFillet: number, tipRound: number }} rack the basic rack it was drawn from
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
// The half-angle a tooth (or, for an internal gear, a space) takes at radius ρ, from its thickness s on the pitch circle
|
|
87
|
+
// of radius r: s / 2r + inv α − inv φ, where cos φ = rb / ρ. Under the base circle the flank runs straight down.
|
|
88
|
+
const halfAt = (s, r, rb, rho) => s / (2 * r) + inv(Math.acos(rb / r)) - inv(Math.acos(rb / Math.max(rho, rb)));
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A spur gear's numbers: its circles, tooth thickness, and the limits it was checked against. Refuses a gear whose teeth
|
|
92
|
+
* would be undercut (fewer than about 17 at 20°: give it a profile shift) or come to a point.
|
|
93
|
+
* @param {GearSpec} p
|
|
94
|
+
* @returns {GearModel}
|
|
95
|
+
*/
|
|
96
|
+
export function gearModel(p) {
|
|
97
|
+
const what = 'gearModel', c = common(what, p), teeth = whole(what, 'teeth', p?.teeth, 6);
|
|
98
|
+
const { module: m, pressureAngle, shift: x, backlash } = c, a = pressureAngle * DEG;
|
|
99
|
+
// Undercut: the basic rack's tip line reaches under the base circle when z sin²α / 2 < addendum − x (a whisker of
|
|
100
|
+
// slack, so 17 teeth at 20° pass, as tables have it)
|
|
101
|
+
const need = c.addendum - (teeth * Math.sin(a) ** 2) / 2;
|
|
102
|
+
if (x < need - 0.01) {
|
|
103
|
+
throw new Error(`${what}: ${teeth} teeth at ${pressureAngle}° are undercut (the mate's tip would cut under the base circle, or the tooth be cut away there): give a profile shift of at least ${(Math.ceil(need * 100) / 100).toFixed(2)} (shift: ${(Math.ceil(need * 100) / 100).toFixed(2)}), or ${Math.ceil((2 * (c.addendum - x)) / Math.sin(a) ** 2 - 0.1)} teeth or more`);
|
|
104
|
+
}
|
|
105
|
+
const r = (m * teeth) / 2, rb = r * Math.cos(a), ra = r + m * (c.addendum + x), rf = r - m * (c.dedendum - x);
|
|
106
|
+
const s = m * (Math.PI / 2 + 2 * x * Math.tan(a)) - backlash / 2;
|
|
107
|
+
if (!(s > 0)) throw new Error(`${what}: a backlash of ${backlash} mm leaves no tooth (at most ${(2 * m * (Math.PI / 2 + 2 * x * Math.tan(a))).toFixed(2)} mm)`);
|
|
108
|
+
const tipHalf = halfAt(s, r, rb, ra);
|
|
109
|
+
if (!(tipHalf > 0)) throw new Error(`${what}: the teeth come to a point before the tip circle: less profile shift (${x} now), or a smaller addendum`);
|
|
110
|
+
const rootHalf = halfAt(s, r, rb, Math.max(rf, rb));
|
|
111
|
+
return {
|
|
112
|
+
kind: 'external', module: m, teeth, pressureAngle, shift: x, backlash,
|
|
113
|
+
helix: c.helix, hand: c.hand, herringbone: c.herringbone, thrust: c.herringbone ? 0 : Math.tan(c.helix * DEG),
|
|
114
|
+
pitch: 2 * r, base: 2 * rb, tip: 2 * ra, root: 2 * rf, thickness: s,
|
|
115
|
+
circularPitch: Math.PI * m, basePitch: Math.PI * m * Math.cos(a),
|
|
116
|
+
topLand: 2 * ra * Math.sin(tipHalf), rootThickness: 2 * Math.max(rf, rb) * Math.sin(rootHalf),
|
|
117
|
+
rack: { pressureAngle, addendum: c.addendum, dedendum: c.dedendum, rootFillet: c.rootFillet, tipRound: c.tipRound },
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* An internal (ring) gear's numbers: the teeth point inwards, tip circle inside the pitch circle. Its spaces are shaped
|
|
123
|
+
* as an outer gear's teeth would be. `rim` is the ring's outer diameter (default: the root diameter and six modules).
|
|
124
|
+
* @param {GearSpec & { rim?: number }} p
|
|
125
|
+
* @returns {GearModel & { rim: number, space: number }}
|
|
126
|
+
*/
|
|
127
|
+
export function internalModel(p) {
|
|
128
|
+
const what = 'internalModel', c = common(what, p), teeth = whole(what, 'teeth', p?.teeth, 12);
|
|
129
|
+
const { module: m, pressureAngle, shift: x, backlash } = c, a = pressureAngle * DEG;
|
|
130
|
+
const r = (m * teeth) / 2, rb = r * Math.cos(a), ra = r - m * (c.addendum - x), rf = r + m * (c.dedendum + x);
|
|
131
|
+
// the space on the pitch circle: π m minus the tooth (m (π/2 − 2x tan α)), widened by this gear's half of the backlash
|
|
132
|
+
const e = m * (Math.PI / 2 + 2 * x * Math.tan(a)) + backlash / 2, s = Math.PI * m - e;
|
|
133
|
+
if (!(s > 0)) throw new Error(`${what}: a backlash of ${backlash} mm leaves no tooth`);
|
|
134
|
+
const rim = p.rim ?? 2 * rf + 6 * m;
|
|
135
|
+
if (!(isNum(rim) && rim > 2 * rf + 2 * m)) throw new Error(`${what}: rim is the ring's outer diameter, more than the root diameter and two modules (${(2 * rf + 2 * m).toFixed(2)} mm; got ${shown(p.rim)})`);
|
|
136
|
+
// An internal tooth's width at a radius is the pitch angle less the space's
|
|
137
|
+
const toothHalf = (rho) => Math.PI / teeth - halfAt(e, r, rb, rho);
|
|
138
|
+
if (!(toothHalf(ra) > 0)) throw new Error(`${what}: the teeth come to a point at the tip circle: less profile shift`);
|
|
139
|
+
return {
|
|
140
|
+
kind: 'internal', module: m, teeth, pressureAngle, shift: x, backlash,
|
|
141
|
+
helix: c.helix, hand: c.hand, herringbone: c.herringbone, thrust: c.herringbone ? 0 : Math.tan(c.helix * DEG),
|
|
142
|
+
pitch: 2 * r, base: 2 * rb, tip: 2 * ra, root: 2 * rf, thickness: s, space: e, rim,
|
|
143
|
+
circularPitch: Math.PI * m, basePitch: Math.PI * m * Math.cos(a),
|
|
144
|
+
topLand: 2 * Math.max(ra, rb) * Math.sin(toothHalf(Math.max(ra, rb))), rootThickness: 2 * rf * Math.sin(toothHalf(rf)),
|
|
145
|
+
rack: { pressureAngle, addendum: c.addendum, dedendum: c.dedendum, rootFillet: c.rootFillet, tipRound: c.tipRound },
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A rack's numbers: a gear of infinite radius, its teeth straight-flanked at the pressure angle. Its pitch line is y = 0,
|
|
151
|
+
* teeth up +y, the first space centred at x = 0. `height` is how much bar there is under the roots (default 3 modules).
|
|
152
|
+
* @param {GearSpec & { height?: number }} p
|
|
153
|
+
*/
|
|
154
|
+
export function rackModel(p) {
|
|
155
|
+
const what = 'rackModel', c = common(what, p), teeth = whole(what, 'teeth', p?.teeth, 1);
|
|
156
|
+
const { module: m, pressureAngle, backlash } = c, a = pressureAngle * DEG;
|
|
157
|
+
if (p.shift) throw new Error(`${what}: a rack has no profile shift (shift the pinion instead; got ${shown(p.shift)})`);
|
|
158
|
+
const height = p.height === undefined ? 3 * m : size(what, 'height', p.height);
|
|
159
|
+
const s = (Math.PI * m) / 2 - backlash / 2;
|
|
160
|
+
if (!(s > 0)) throw new Error(`${what}: a backlash of ${backlash} mm leaves no tooth`);
|
|
161
|
+
const tipHalf = s / 2 - m * c.addendum * Math.tan(a);
|
|
162
|
+
if (!(tipHalf > 0)) throw new Error(`${what}: the teeth come to a point: a smaller addendum or pressure angle`);
|
|
163
|
+
return {
|
|
164
|
+
kind: 'rack', module: m, teeth, pressureAngle, shift: 0, backlash, height, thickness: s,
|
|
165
|
+
helix: c.helix, hand: c.hand, herringbone: c.herringbone, thrust: c.herringbone ? 0 : Math.tan(c.helix * DEG),
|
|
166
|
+
length: teeth * Math.PI * m, tipLine: m * c.addendum, rootLine: -m * c.dedendum,
|
|
167
|
+
circularPitch: Math.PI * m, basePitch: Math.PI * m * Math.cos(a),
|
|
168
|
+
topLand: 2 * tipHalf, rootThickness: s + 2 * m * c.dedendum * Math.tan(a),
|
|
169
|
+
rack: { pressureAngle, addendum: c.addendum, dedendum: c.dedendum, rootFillet: c.rootFillet, tipRound: c.tipRound },
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ---------- the outline ----------
|
|
174
|
+
|
|
175
|
+
// How many points each piece of a tooth gets. The same for every tooth and every slice of a solid, so slices loft
|
|
176
|
+
// point to point.
|
|
177
|
+
const N = { root: 3, fillet: 6, flank: 14, round: 4, tip: 3 };
|
|
178
|
+
|
|
179
|
+
const dist = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
180
|
+
// The nearest point of the flank to c: a scan, then a golden-section search round the best
|
|
181
|
+
function nearest(flank, q0, q1, c) {
|
|
182
|
+
let best = q0, bd = Infinity;
|
|
183
|
+
for (let i = 0; i <= 48; i++) { const q = q0 + ((q1 - q0) * i) / 48, d = dist(flank(q), c); if (d < bd) { bd = d; best = q; } }
|
|
184
|
+
let lo = Math.max(q0, best - (q1 - q0) / 48), hi = Math.min(q1, best + (q1 - q0) / 48);
|
|
185
|
+
const g = (Math.sqrt(5) - 1) / 2;
|
|
186
|
+
for (let i = 0; i < 40; i++) {
|
|
187
|
+
const a = hi - g * (hi - lo), b = lo + g * (hi - lo);
|
|
188
|
+
if (dist(flank(a), c) < dist(flank(b), c)) hi = b; else lo = a;
|
|
189
|
+
}
|
|
190
|
+
const q = (lo + hi) / 2;
|
|
191
|
+
return { q, d: dist(flank(q), c) };
|
|
192
|
+
}
|
|
193
|
+
// Points on an arc about c from a to b (the short way), n of them, a included and b not
|
|
194
|
+
function arc(c, a, b, n) {
|
|
195
|
+
const r = dist(a, c), a0 = Math.atan2(a[1] - c[1], a[0] - c[0]);
|
|
196
|
+
let d = Math.atan2(b[1] - c[1], b[0] - c[0]) - a0;
|
|
197
|
+
while (d > Math.PI) d -= TAU;
|
|
198
|
+
while (d < -Math.PI) d += TAU;
|
|
199
|
+
return Array.from({ length: n }, (_, i) => { const t = a0 + (d * i) / n; return [c[0] + r * Math.cos(t), c[1] + r * Math.sin(t)]; });
|
|
200
|
+
}
|
|
201
|
+
// n points along a curve from q0 to q1, evenly spaced along its length, q0 included and q1 not
|
|
202
|
+
function along(curve, q0, q1, n) {
|
|
203
|
+
const fine = Array.from({ length: 121 }, (_, i) => curve(q0 + ((q1 - q0) * i) / 120)), at = [0];
|
|
204
|
+
for (let i = 1; i < fine.length; i++) at.push(at[i - 1] + dist(fine[i], fine[i - 1]));
|
|
205
|
+
const total = at.at(-1), out = [];
|
|
206
|
+
for (let k = 0, j = 0; k < n; k++) {
|
|
207
|
+
const want = (total * k) / n;
|
|
208
|
+
while (j < fine.length - 2 && at[j + 1] < want) j++;
|
|
209
|
+
const f = (want - at[j]) / (at[j + 1] - at[j] || 1);
|
|
210
|
+
out.push([fine[j][0] + f * (fine[j + 1][0] - fine[j][0]), fine[j][1] + f * (fine[j + 1][1] - fine[j][1])]);
|
|
211
|
+
}
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Half a tooth, from the middle of the space before it to the middle of its tip: the root, the fillet, the flank, the
|
|
216
|
+
// tip's round and the tip. The same for a gear (t an angle, levels radii) and a rack (t along x, levels heights).
|
|
217
|
+
// The fillet and the round are each a circle touching the flank and the root (or tip) circle; one that doesn't fit is
|
|
218
|
+
// made smaller until it does, and its size is reported.
|
|
219
|
+
function half(g) {
|
|
220
|
+
const fit = (want, base, sign, tMid, tooth) => {
|
|
221
|
+
// the circle's centre runs along a level ρ off the base circle (sign: outward from the root, inward from the tip)
|
|
222
|
+
for (let rho = want, tries = 0; ; rho *= 0.8, tries++) {
|
|
223
|
+
const level = base + sign * rho;
|
|
224
|
+
if (rho < 1e-4 || tries > 40) return null;
|
|
225
|
+
if (level <= g.rootLevel || level >= g.tipLevel) continue;
|
|
226
|
+
const tFlank = g.flankT(level), lim = tooth ? tFlank + (tMid - tFlank) * 0.85 : tMid + (tFlank - tMid) * 0.05;
|
|
227
|
+
const d = (t) => nearest(g.flank, g.q0, g.q1, g.point(t, level)).d;
|
|
228
|
+
if (d(lim) < rho) continue; // no room: smaller
|
|
229
|
+
let lo = tFlank, hi = lim; // d(lo) ≈ 0, d(hi) ≥ rho
|
|
230
|
+
for (let i = 0; i < 60; i++) { const mid = (lo + hi) / 2; if (d(mid) < rho) lo = mid; else hi = mid; }
|
|
231
|
+
const c = g.point(hi, level), n = nearest(g.flank, g.q0, g.q1, c);
|
|
232
|
+
return { rho, c, q: n.q, touch: g.flank(n.q), foot: g.point(hi, base), t: hi };
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const root = g.fillet > 0 ? fit(g.fillet, g.rootLevel, 1, g.t0, false) : null;
|
|
236
|
+
const tip = g.round > 0 ? fit(g.round, g.tipLevel, -1, 0, true) : null;
|
|
237
|
+
// without a fillet (or a round), the corner itself
|
|
238
|
+
const rootQ = root ? root.q : g.q0, tipQ = tip ? tip.q : g.q1;
|
|
239
|
+
const rootFoot = root ? root.foot : g.flank(g.q0), tipFoot = tip ? tip.foot : g.flank(g.q1);
|
|
240
|
+
const rootT = root ? root.t : g.flankT(g.rootLevel), tipT = tip ? tip.t : g.flankT(g.tipLevel);
|
|
241
|
+
const pts = [
|
|
242
|
+
...Array.from({ length: N.root }, (_, i) => g.point(g.t0 + ((rootT - g.t0) * i) / N.root, g.rootLevel)),
|
|
243
|
+
...(root ? arc(root.c, root.foot, root.touch, N.fillet) : Array(N.fillet).fill(rootFoot).map((p, i) => g.flank(g.q0 + ((rootQ - g.q0) * i) / N.fillet))),
|
|
244
|
+
...along(g.flank, rootQ, tipQ, N.flank),
|
|
245
|
+
...(tip ? arc(tip.c, tip.touch, tip.foot, N.round) : Array.from({ length: N.round }, (_, i) => g.flank(tipQ + ((g.q1 - tipQ) * i) / N.round))),
|
|
246
|
+
...Array.from({ length: N.tip }, (_, i) => g.point(tipT + ((0 - tipT) * i) / N.tip, g.tipLevel)),
|
|
247
|
+
];
|
|
248
|
+
return { pts, fillet: root ? root.rho : 0, round: tip ? tip.rho : 0, wantFillet: g.fillet, wantRound: g.round };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// A gear's (or an internal gear's spaces') outline: one loop, counter-clockwise, tooth 0 on +x. grow moves it outwards
|
|
252
|
+
// by that much along its normal (an involute's offset is the same involute, thinner by grow / rb each side).
|
|
253
|
+
function loop({ teeth: z, thickness: s, r, rb, ra, rf, fillet, round, grow = 0 }) {
|
|
254
|
+
s += (2 * grow * r) / rb; ra += grow; rf += grow;
|
|
255
|
+
fillet = Math.max(1e-3, fillet - grow); round = Math.max(1e-3, round + grow);
|
|
256
|
+
const hAt = (rho) => halfAt(s, r, rb, rho), rLow = rf, rHigh = ra;
|
|
257
|
+
const flank = (rho) => { const h = hAt(rho); return [rho * Math.cos(h), -rho * Math.sin(h)]; };
|
|
258
|
+
const h = half({
|
|
259
|
+
t0: -Math.PI / z, rootLevel: rf, tipLevel: ra, q0: rLow, q1: rHigh, flank, fillet, round,
|
|
260
|
+
flankT: (rho) => -hAt(rho), point: (t, rho) => [rho * Math.cos(t), rho * Math.sin(t)],
|
|
261
|
+
});
|
|
262
|
+
const tooth = [...h.pts, [ra, 0], ...h.pts.slice(1).reverse().map(([x, y]) => [x, -y])];
|
|
263
|
+
if (!Number.isInteger(z)) return { tooth, fillet: h.fillet, round: h.round };
|
|
264
|
+
const out = [];
|
|
265
|
+
for (let k = 0; k < z; k++) {
|
|
266
|
+
const c = Math.cos((TAU * k) / z), sn = Math.sin((TAU * k) / z);
|
|
267
|
+
for (const [x, y] of tooth) out.push([x * c - y * sn, x * sn + y * c]);
|
|
268
|
+
}
|
|
269
|
+
return { points: out, fillet: h.fillet, round: h.round };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const circle = (d, n, grow = 0) => Array.from({ length: n }, (_, i) => [((d / 2 + grow) * Math.cos((TAU * i) / n)), ((d / 2 + grow) * Math.sin((TAU * i) / n))]);
|
|
273
|
+
// A bore: a circle, or with a flat (a D shape) the points past the flat brought onto it. grow: outwards, into the hole.
|
|
274
|
+
function bore(d, flat, grow = 0) {
|
|
275
|
+
const pts = circle(d, 48, grow);
|
|
276
|
+
if (!flat) return pts;
|
|
277
|
+
const at = d / 2 - flat + grow;
|
|
278
|
+
return pts.map(([x, y]) => (x > at ? [at, y] : [x, y]));
|
|
279
|
+
}
|
|
280
|
+
const reversed = (pts) => [...pts].reverse();
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* A spur gear's outline in 2D, as loops of [x, y] points (the first the outside, counter-clockwise; then holes,
|
|
284
|
+
* clockwise), and the fillet and round it was drawn with (smaller than asked where the asked one didn't fit).
|
|
285
|
+
* @param {GearSpec & { bore?: number, flat?: number }} p
|
|
286
|
+
* @param {number} [grow] outwards along the outline's normal, mm (negative: inwards)
|
|
287
|
+
*/
|
|
288
|
+
export function spurProfile(p, grow = 0) {
|
|
289
|
+
const g = gearModel(p), r = g.pitch / 2, m = g.module;
|
|
290
|
+
const l = loop({ teeth: g.teeth, thickness: g.thickness, r, rb: g.base / 2, ra: g.tip / 2, rf: g.root / 2, fillet: g.rack.rootFillet * m, round: g.rack.tipRound * m, grow });
|
|
291
|
+
const loops = [l.points];
|
|
292
|
+
if (p.bore != null) {
|
|
293
|
+
size('spur', 'bore', p.bore);
|
|
294
|
+
if (p.bore > g.root - 4 * m) throw new Error(`spur: a bore of ${p.bore} mm leaves under two modules of web under the teeth (at most ${(g.root - 4 * m).toFixed(2)} mm)`);
|
|
295
|
+
if (p.flat != null && !(size('spur', 'flat', p.flat) < p.bore / 2)) throw new Error(`spur: flat is how deep the bore's flat cuts, under half the bore (got ${p.flat})`);
|
|
296
|
+
loops.push(reversed(bore(p.bore, p.flat, -grow)));
|
|
297
|
+
}
|
|
298
|
+
return { loops, fillet: l.fillet, round: l.round, model: g };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** An internal gear's outline: the rim (counter-clockwise), then the teeth's inner outline (clockwise). */
|
|
302
|
+
export function internalProfile(p, grow = 0) {
|
|
303
|
+
const g = internalModel(p), r = g.pitch / 2, m = g.module;
|
|
304
|
+
// the spaces, drawn as an outer gear's teeth: their "tips" are the ring's roots (the ring's root fillet there), their
|
|
305
|
+
// "roots" the ring's tips (its round there). Growing the ring is shrinking the spaces.
|
|
306
|
+
const l = loop({ teeth: g.teeth, thickness: g.space, r, rb: g.base / 2, ra: g.root / 2, rf: g.tip / 2, fillet: g.rack.tipRound * m, round: g.rack.rootFillet * m, grow: -grow });
|
|
307
|
+
// the ring's tooth 0 on +x: the spaces' loop has a space on +x, so turn it half a pitch
|
|
308
|
+
const c = Math.cos(Math.PI / g.teeth), s = Math.sin(Math.PI / g.teeth), turned = l.points.map(([x, y]) => [x * c - y * s, x * s + y * c]);
|
|
309
|
+
return { loops: [circle(g.rim, 96, grow), reversed(turned)], fillet: l.round, round: l.fillet, model: g };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** A rack's outline: one loop, counter-clockwise, its pitch line on y = 0, teeth up +y from x = 0. */
|
|
313
|
+
export function rackProfile(p, grow = 0) {
|
|
314
|
+
const g = rackModel(p), m = g.module, a = g.pressureAngle * DEG, pitch = Math.PI * m;
|
|
315
|
+
const s = g.thickness + (2 * grow) / Math.cos(a), yt = g.tipLine + grow, yr = g.rootLine + grow;
|
|
316
|
+
const w = (y) => s / 2 - y * Math.tan(a); // half the tooth's width at height y
|
|
317
|
+
const flank = (y) => [-w(y), y];
|
|
318
|
+
const h = half({
|
|
319
|
+
t0: -pitch / 2, rootLevel: yr, tipLevel: yt, q0: yr, q1: yt, flank,
|
|
320
|
+
fillet: Math.max(1e-3, g.rack.rootFillet * m - grow), round: Math.max(1e-3, g.rack.tipRound * m + grow),
|
|
321
|
+
flankT: (y) => -w(y), point: (t, y) => [t, y],
|
|
322
|
+
});
|
|
323
|
+
const tooth = [...h.pts, [0, yt], ...h.pts.slice(1).reverse().map(([x, y]) => [-x, y])];
|
|
324
|
+
const top = [];
|
|
325
|
+
for (let k = 0; k < g.teeth; k++) for (const [x, y] of tooth) top.push([x + (k + 0.5) * pitch, y]);
|
|
326
|
+
top.push([g.length, yr]);
|
|
327
|
+
const bottom = yr - g.height - grow;
|
|
328
|
+
return { loops: [[[0, bottom], [g.length, bottom], ...reversed(top)]], fillet: h.fillet, round: h.round, model: g };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ---------- solids ----------
|
|
332
|
+
|
|
333
|
+
// A solid from loops at heights: each slice is the loops at a height, grown (a chamfer: drawn smaller there) and turned
|
|
334
|
+
// (a helix), all with the same points, so the walls join point to point. Caps are triangulated with their holes.
|
|
335
|
+
function loft(profile, width, { chamfer, twist, slide, scale }) {
|
|
336
|
+
const [bottom, top] = Array.isArray(chamfer) ? chamfer : [chamfer, 0];
|
|
337
|
+
if (bottom + top >= width) throw new Error(`the chamfers (${bottom} and ${top} mm) are as tall as the gear (${width} mm)`);
|
|
338
|
+
// heights: the chamfers' ends, and for a twist enough between that no slice turns more than a degree or so
|
|
339
|
+
const zs = [0, ...(bottom > 0 ? [bottom] : []), ...(top > 0 ? [width - top] : []), width];
|
|
340
|
+
if (twist) {
|
|
341
|
+
const most = Math.max(...[0, 0.25, 0.5, 0.75, 1].map((f) => Math.abs(twist(width * f)))), n = Math.ceil(most / (1.5 * DEG));
|
|
342
|
+
for (let i = 1; i < n; i++) zs.push((width * i) / n);
|
|
343
|
+
zs.push(width / 2); // (a herringbone's apex)
|
|
344
|
+
}
|
|
345
|
+
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); }
|
|
346
|
+
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)
|
|
347
|
+
const heights = [...new Set(zs.map((z) => +z.toFixed(9)))].sort((x, y) => x - y);
|
|
348
|
+
const slices = heights.map((z) => {
|
|
349
|
+
const grow = z < bottom ? -(bottom - z) : z > width - top ? -(z - (width - top)) : 0;
|
|
350
|
+
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;
|
|
351
|
+
const at = (p) => [p[0] * c - p[1] * s + dx, p[0] * s + p[1] * c];
|
|
352
|
+
const sides = loops.flatMap((pts) => pts.map((p, i) => [at(p), at(pts[(i + 1) % pts.length])]));
|
|
353
|
+
return slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, z]), slice.fromSides(sides));
|
|
354
|
+
});
|
|
355
|
+
return extrudeFromSlices({ numberOfSlices: slices.length, repair: false, callback: (t, i) => slices[i] }, slices[0]);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** @typedef {{ width: number, chamfer?: number | [number, number], bore?: number, flat?: number }} Solid width up z; chamfer at the bed face (and the top); bore and flat for a shaft */
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* A spur gear: the profile extruded up z, its first tooth on +x, the bed face chamfered.
|
|
362
|
+
* @param {GearSpec & Solid} p
|
|
363
|
+
* @returns {object} a JSCAD geom3
|
|
364
|
+
*/
|
|
365
|
+
export function spur(p) {
|
|
366
|
+
const width = size('spur', 'width', p?.width), chamfer = chamfers('spur', p, width);
|
|
367
|
+
const { model: g } = spurProfile(p); // (refuses here, before any slicing)
|
|
368
|
+
return loft((grow) => spurProfile(p, grow).loops, width, { chamfer, twist: twistOf(g, width) });
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// How far a helical gear's profile has turned at height z: z tan β / r, the right hand turning anticlockwise up z; a
|
|
372
|
+
// herringbone turns to the middle and back. Gears that mesh outside each other are of opposite hands; a pinion and its
|
|
373
|
+
// ring, or its rack, of the same hand.
|
|
374
|
+
function twistOf(g, width) {
|
|
375
|
+
if (!g.helix) return null;
|
|
376
|
+
const k = (g.hand === 'left' ? -1 : 1) * Math.tan(g.helix * DEG) / (g.pitch / 2);
|
|
377
|
+
return g.herringbone ? (z) => k * Math.min(z, width - z) : (z) => k * z;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** A helical gear: a spur gear's profile turned along its width by the helix angle. Meshes with one of the opposite hand. @param {GearSpec & Solid & { helix: number, hand?: 'right' | 'left' }} p */
|
|
381
|
+
export function helical(p) {
|
|
382
|
+
if (!(p?.helix > 0)) throw new Error(`helical: give a helix angle above 0° (got ${shown(p?.helix)}); 15° to 30° print well`);
|
|
383
|
+
return spur({ ...p, herringbone: false });
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** A herringbone gear: helical one way to the middle of its width and back, so it pushes no thrust along its axis and stays on its shaft. Prints with no supports. @param {GearSpec & Solid & { helix?: number }} p */
|
|
387
|
+
export function herringbone(p) { return spur({ ...p, helix: p?.helix ?? 30, herringbone: true }); }
|
|
388
|
+
|
|
389
|
+
/** An internal (ring) gear, its rim round it. @param {GearSpec & Solid & { rim?: number }} p */
|
|
390
|
+
export function internal(p) {
|
|
391
|
+
const width = size('internal', 'width', p?.width), chamfer = chamfers('internal', p, width);
|
|
392
|
+
const { model: g } = internalProfile(p);
|
|
393
|
+
return loft((grow) => internalProfile(p, grow).loops, width, { chamfer, twist: twistOf(g, width) });
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** A rack along x, its teeth up +y, its width up z. @param {GearSpec & Solid & { height?: number }} p */
|
|
397
|
+
export function rack(p) {
|
|
398
|
+
const width = size('rack', 'width', p?.width), chamfer = chamfers('rack', p, width);
|
|
399
|
+
const { model: g } = rackProfile(p);
|
|
400
|
+
// a helical rack's teeth slide along it with height as its pinion's turn: z tan β, the same hand as the pinion
|
|
401
|
+
const k = g.helix ? (g.hand === 'left' ? -1 : 1) * Math.tan(g.helix * DEG) : 0;
|
|
402
|
+
return loft((grow) => rackProfile(p, grow).loops, width, { chamfer, ...(k ? { slide: g.herringbone ? (z) => k * Math.min(z, width - z) : (z) => k * z } : {}) });
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function chamfers(what, p, width) {
|
|
406
|
+
const most = 0.25 * p.module, c = p.chamfer ?? Math.min(D('chamfer'), most), pair = Array.isArray(c) ? c : [c, 0]; // (the default: no more than the teeth can lose)
|
|
407
|
+
if (pair.length !== 2 || !pair.every((x) => isNum(x) && x >= 0)) throw new Error(`${what}: chamfer is a size in mm (0 or more), or [bed face, top face] (got ${shown(c)})`);
|
|
408
|
+
if (pair.some((x) => x > most)) throw new Error(`${what}: a chamfer over a quarter module (${most.toFixed(2)} mm) eats the tips: at most that`);
|
|
409
|
+
if (pair[0] + pair[1] >= width) throw new Error(`${what}: the chamfers (${pair.join(' and ')} mm) are as tall as the gear (${width} mm)`);
|
|
410
|
+
return pair;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// ---------- meshing ----------
|
|
414
|
+
|
|
415
|
+
// The working pressure angle of shifted gears: inv αw = inv α + 2 tan α (x1 ± x2) / (z1 ± z2), solved by Newton
|
|
416
|
+
function working(a, sum, zs) {
|
|
417
|
+
const want = inv(a) + (2 * Math.tan(a) * sum) / zs;
|
|
418
|
+
let w = a;
|
|
419
|
+
for (let i = 0; i < 30; i++) w -= (inv(w) - want) / Math.tan(w) ** 2;
|
|
420
|
+
return w;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* How two gears mesh: the centre distance they need (with their profile shifts), the ratio (turns of b for one of a,
|
|
425
|
+
* negative when they turn opposite ways; for a rack, mm for one turn of the pinion), the contact ratio (how many teeth
|
|
426
|
+
* share the load on average: under 1 the drive jumps, under 1.2 it is rough), and findings. Give `centre` (mm) to have
|
|
427
|
+
* a centre distance checked: closer than needed jams the teeth, further adds backlash.
|
|
428
|
+
* @param {GearModel} a @param {GearModel} b @param {{ centre?: number }} [o]
|
|
429
|
+
*/
|
|
430
|
+
export function mesh(a, b, { centre, width } = {}) {
|
|
431
|
+
const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
|
|
432
|
+
for (const [k, g] of [['a', a], ['b', b]]) if (!g || !['external', 'internal', 'rack'].includes(g.kind)) throw new Error(`mesh: ${k} is a gear's model (gearModel, internalModel or rackModel), not ${shown(g)}`);
|
|
433
|
+
if (a.kind !== 'external' && b.kind !== 'external') throw new Error(`mesh: a ${a.kind} and a ${b.kind} don't mesh: one of them is an outer gear`);
|
|
434
|
+
if (b.kind === 'external' && a.kind !== 'external') return flip(mesh(b, a, { centre, width }));
|
|
435
|
+
if (Math.abs(a.module - b.module) > 1e-9) f('fail', `modules differ (${a.module} and ${b.module} mm): teeth of different sizes don't mesh`, 'mesh.module');
|
|
436
|
+
if (Math.abs(a.pressureAngle - b.pressureAngle) > 1e-9) f('fail', `pressure angles differ (${a.pressureAngle}° and ${b.pressureAngle}°): the flanks don't roll on each other`, 'mesh.pressure');
|
|
437
|
+
const m = a.module, al = a.pressureAngle * DEG, pb = Math.PI * m * Math.cos(al);
|
|
438
|
+
// Helical teeth: the same angle, opposite hands outside each other, the same hand on a ring or a rack
|
|
439
|
+
const ha = a.helix ?? 0, hb = b.helix ?? 0;
|
|
440
|
+
if (Math.abs(ha - hb) > 1e-9) f('fail', `helix angles differ (${ha}° and ${hb}°): the teeth cross each other`, 'mesh.helix');
|
|
441
|
+
else if (ha > 0) {
|
|
442
|
+
const same = b.kind !== 'external';
|
|
443
|
+
if ((a.hand === b.hand) !== same) f('fail', `both are ${a.hand}-handed: ${same ? `a pinion and its ${b.kind === 'rack' ? 'rack' : 'ring'} are of the same hand` : 'outer gears that mesh are of opposite hands'}`, 'mesh.helix');
|
|
444
|
+
if (!!a.herringbone !== !!b.herringbone) f('fail', 'a herringbone meshes only with a herringbone', 'mesh.helix');
|
|
445
|
+
}
|
|
446
|
+
const ra1 = a.tip / 2, rb1 = a.base / 2, r1 = a.pitch / 2;
|
|
447
|
+
let need, ratio, contact, aw = al;
|
|
448
|
+
if (b.kind === 'external') {
|
|
449
|
+
aw = working(al, a.shift + b.shift, a.teeth + b.teeth);
|
|
450
|
+
need = ((a.teeth + b.teeth) * m * Math.cos(al)) / (2 * Math.cos(aw));
|
|
451
|
+
ratio = -a.teeth / b.teeth;
|
|
452
|
+
contact = (Math.sqrt(ra1 ** 2 - rb1 ** 2) + Math.sqrt((b.tip / 2) ** 2 - (b.base / 2) ** 2) - need * Math.sin(aw)) / pb;
|
|
453
|
+
} else if (b.kind === 'internal') {
|
|
454
|
+
if (b.teeth <= a.teeth) throw new Error(`mesh: a ring of ${b.teeth} teeth can't hold a pinion of ${a.teeth}`);
|
|
455
|
+
aw = working(al, b.shift - a.shift, b.teeth - a.teeth);
|
|
456
|
+
need = ((b.teeth - a.teeth) * m * Math.cos(al)) / (2 * Math.cos(aw));
|
|
457
|
+
ratio = a.teeth / b.teeth;
|
|
458
|
+
const rb2 = b.base / 2, ra2 = Math.max(b.tip / 2, rb2); // (a ring's tip under its base circle doesn't roll)
|
|
459
|
+
contact = (Math.sqrt(ra1 ** 2 - rb1 ** 2) - Math.sqrt(ra2 ** 2 - rb2 ** 2) + need * Math.sin(aw)) / pb;
|
|
460
|
+
const gap = b.teeth - a.teeth;
|
|
461
|
+
// Rule of thumb (KHK, Gear Technical Reference, internal gears): with full-depth 20° teeth and no shift, a ring
|
|
462
|
+
// under about 10 teeth more than its pinion fouls it (involute or trochoid interference); 12 is the safe side
|
|
463
|
+
if (gap < 10) f('fail', `the ring has ${gap} teeth more than its pinion: under 10, their tips foul each other (interference); more teeth on the ring, or fewer on the pinion`, 'mesh.interference', { gap });
|
|
464
|
+
else if (gap < 12) f('warn', `the ring has ${gap} teeth more than its pinion: near the limit of 10 where tips foul; check it by contact`, 'mesh.interference', { gap });
|
|
465
|
+
} else { // a rack
|
|
466
|
+
need = r1 + a.shift * m; // the pinion's centre above the rack's pitch line
|
|
467
|
+
ratio = Math.PI * a.pitch; // mm a turn
|
|
468
|
+
contact = (Math.sqrt(ra1 ** 2 - rb1 ** 2) - r1 * Math.sin(al) + (m * (b.rack.addendum - a.shift)) / Math.sin(al)) / pb;
|
|
469
|
+
}
|
|
470
|
+
// the overlap ratio: how far a tooth's own twist carries the contact on across the width (εβ = b tan β / π m, each
|
|
471
|
+
// half of a herringbone on its own)
|
|
472
|
+
const overlap = ha > 0 && width !== undefined ? ((a.herringbone ? width / 2 : width) * Math.tan(ha * DEG)) / (Math.PI * m) : 0;
|
|
473
|
+
if (overlap) contact += overlap;
|
|
474
|
+
const sense = b.kind === 'rack' ? `${fmt(ratio)} mm of rack a turn (${fmt(ratio / 360, 4)} mm a degree)` : `${fmt(Math.abs(ratio), 4)} turns of the second for each turn of the first (${a.teeth}:${b.teeth}), ${ratio < 0 ? 'turning opposite ways' : 'the same way'}`;
|
|
475
|
+
if (contact < 1) f('fail', `contact ratio ${fmt(contact)}: under 1, a pair of teeth lets go before the next takes up, and the drive jumps: more teeth, or a longer addendum`, 'mesh.contact', { contact });
|
|
476
|
+
else if (contact < 1.2) f('warn', `contact ratio ${fmt(contact)}: under 1.2 the drive is rough, and a printed tooth's errors can make it jump`, 'mesh.contact', { contact });
|
|
477
|
+
else f('pass', `contact ratio ${fmt(contact)}${overlap ? ` (${fmt(contact - overlap)} across the teeth and ${fmt(overlap)} along their twist)` : ''}: ${fmt(contact)} pairs of teeth share the load on average`, 'mesh.contact', { contact });
|
|
478
|
+
let backlash = a.backlash / 2 + b.backlash / 2;
|
|
479
|
+
if (centre !== undefined) {
|
|
480
|
+
if (!isNum(centre)) throw new Error(`mesh: centre is a distance in mm (got ${shown(centre)})`);
|
|
481
|
+
const off = centre - need;
|
|
482
|
+
// 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
|
|
483
|
+
const added = 2 * off * Math.tan(aw);
|
|
484
|
+
backlash += added;
|
|
485
|
+
// closer than they need eats the backlash (2 Δa tan αw of it) and the clearance under the tips (0.25 m); past
|
|
486
|
+
// either, the teeth jam
|
|
487
|
+
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;
|
|
488
|
+
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 });
|
|
489
|
+
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 });
|
|
490
|
+
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 });
|
|
491
|
+
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 });
|
|
492
|
+
}
|
|
493
|
+
const thrust = a.herringbone || !ha ? 0 : Math.tan(ha * DEG);
|
|
494
|
+
return { kind: b.kind, centre: need, ratio, contact, overlap, thrust, pressureAngle: aw / DEG, backlash, text: sense, findings: out };
|
|
495
|
+
}
|
|
496
|
+
const flip = (r) => ({ ...r, ratio: r.kind === 'rack' ? r.ratio : 1 / r.ratio });
|
|
497
|
+
const fmt = (x, d = 2) => String(+x.toFixed(d));
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* The phase that puts a tooth in a gap: b's angle is ratio × a's angle + phase. Both gears have their first tooth at
|
|
501
|
+
* their own angle 0; `angle` is the direction from a's centre to b's, in degrees, in the frame both angles are measured in.
|
|
502
|
+
* For a pinion in a ring, a is the pinion. Returns degrees, in [0, 360 / b's teeth).
|
|
503
|
+
* @param {GearModel} a @param {GearModel} b @param {number} [angle]
|
|
504
|
+
*/
|
|
505
|
+
export function phase(a, b, angle = 0) {
|
|
506
|
+
const z1 = a.teeth, z2 = b.teeth, beta = angle * DEG, pitch2 = TAU / z2;
|
|
507
|
+
let p;
|
|
508
|
+
if (a.kind === 'external' && b.kind === 'external') p = beta + Math.PI - Math.PI / z2 + (z1 / z2) * beta; // a tooth on the line meets a space
|
|
509
|
+
else if (a.kind === 'external' && b.kind === 'internal') { const g = beta + Math.PI; p = g - Math.PI / z2 - (z1 / z2) * g; } // the pinion's tooth outward, along ring to pinion
|
|
510
|
+
else throw new Error('phase: give an outer gear and an outer or ring gear (for a rack, rackPhase)');
|
|
511
|
+
p = ((p % pitch2) + pitch2) % pitch2;
|
|
512
|
+
return p / DEG;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Where a rack must stand so the pinion's teeth fall in its spaces: the rack's travel is (π × pitch diameter / 360) ×
|
|
517
|
+
* the pinion's angle + this, in mm. The rack lies along x with its pitch line on y = 0 and its first space at x = 0;
|
|
518
|
+
* the pinion's centre is at x = `at`, above the rack, its first tooth pointing along +x at angle 0.
|
|
519
|
+
* @param {GearModel} pinion @param {number} [at] @returns {number} mm, in [0, the circular pitch)
|
|
520
|
+
*/
|
|
521
|
+
export function rackPhase(pinion, at = 0) {
|
|
522
|
+
const r = pinion.pitch / 2, p = Math.PI * pinion.module;
|
|
523
|
+
// at angle −90° the pinion's first tooth points down at the rack, and must sit over a space (spaces at x = kp): the
|
|
524
|
+
// travel there, r (−π/2) + phase, is at, so the phase is at + r π/2. (With the sign the other way the rack sat half a
|
|
525
|
+
// tooth out for an odd number of teeth, and right for an even one, where the error is whole pitches.)
|
|
526
|
+
const s = at + r * (Math.PI / 2);
|
|
527
|
+
return ((s % p) + p) % p;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// ---------- printing ----------
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* How a gear prints on a printer ({ line, layer, nozzle?, wall_min? }): the module against the nozzle, lines across a
|
|
534
|
+
* tooth at its root and its tip, and the width in layers. Findings in the kit's shape, each placed on the first tooth.
|
|
535
|
+
* @param {GearModel} g @param {{ line: number, layer: number, nozzle?: number, wall_min?: number }} printer @param {number} [width]
|
|
536
|
+
*/
|
|
537
|
+
export function printable(g, printer, width) {
|
|
538
|
+
if (!g || !['external', 'internal', 'rack'].includes(g.kind)) throw new Error(`printable: give a gear's model (gearModel, internalModel or rackModel), not ${shown(g)}`);
|
|
539
|
+
if (!printer || !(printer.line > 0) || !(printer.layer > 0)) throw new Error(`printable: the printer needs a line and a layer above 0 mm (got ${shown(printer)})`);
|
|
540
|
+
const out = [], f = (level, text, code, at) => out.push({ level, text, code, ...(at ? { at } : {}) });
|
|
541
|
+
const { line, layer } = printer, nozzle = printer.nozzle ?? line / 1.05, least = printer.wall_min ?? 2 * line;
|
|
542
|
+
const tipAt = g.kind === 'rack' ? [0.5 * g.circularPitch, g.tipLine, 0] : [g.tip / 2, 0, 0];
|
|
543
|
+
const rootAt = g.kind === 'rack' ? [0.5 * g.circularPitch, g.rootLine, 0] : [(g.kind === 'internal' ? g.root : Math.max(g.root, g.base)) / 2, 0, 0];
|
|
544
|
+
const floor = 2 * nozzle;
|
|
545
|
+
if (g.module < floor - 1e-9) f('fail', `module ${g.module} on a ${fmt(nozzle)} mm nozzle: under ${fmt(floor)} (twice the nozzle) a tooth is under three lines at its pitch circle and prints as a blob; module ${fmt(floor)} or more`, 'gear.module', tipAt);
|
|
546
|
+
else f('pass', `module ${g.module}: ${fmt(g.thickness / line, 1)} lines across a tooth at its pitch circle`, 'gear.module');
|
|
547
|
+
const rootLines = g.rootThickness / line;
|
|
548
|
+
if (g.rootThickness < least - 1e-9) f('fail', `a tooth is ${fmt(g.rootThickness)} mm across at its root, under the thinnest wall (${fmt(least)} mm): a larger module`, 'gear.root', rootAt);
|
|
549
|
+
else f('pass', `a tooth is ${fmt(g.rootThickness)} mm across at its root, ${fmt(rootLines, 1)} lines`, 'gear.root', rootAt);
|
|
550
|
+
if (g.topLand < line) f('warn', `the tips are ${fmt(g.topLand)} mm across, under a line (${line} mm): they print as one bead, rounded off; fine for meshing, but don't count on the tip`, 'gear.tip', tipAt);
|
|
551
|
+
if (g.helix > 0) {
|
|
552
|
+
// the flank leans most at the tip: tan β_a = tan β × r_a / r
|
|
553
|
+
const ra = g.kind === 'rack' ? 1 : g.tip / (g.kind === 'internal' ? g.root : g.pitch), lean = Math.atan(Math.tan(g.helix * DEG) * Math.max(1, ra)) / DEG;
|
|
554
|
+
if (lean > 45) f('warn', `the teeth lean ${fmt(lean, 1)}° from upright at their tips: past 45° the flanks sag as they print; a smaller helix angle`, 'gear.helix', tipAt);
|
|
555
|
+
else f('pass', `the teeth lean ${fmt(lean, 1)}° from upright at their tips: prints with no supports`, 'gear.helix');
|
|
556
|
+
if (!g.herringbone) f('info', `a helix of ${g.helix}° pushes ${fmt(Math.tan(g.helix * DEG), 2)} N along the axis for each newton it drives: hold the gear on its shaft, or make it a herringbone`, 'gear.thrust');
|
|
557
|
+
}
|
|
558
|
+
if (width !== undefined) {
|
|
559
|
+
const layers = width / layer;
|
|
560
|
+
if (Math.abs(layers - Math.round(layers)) > 0.05) f('warn', `${width} mm wide is ${fmt(layers)} layers of ${layer} mm: a whole number prints as drawn`, 'gear.layers');
|
|
561
|
+
}
|
|
562
|
+
return out;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Planetary gearboxes: the teeth for a ratio, laid out in phase
|
|
566
|
+
/**
|
|
567
|
+
* How much of the power a stage passes on: DEFAULTS.efficiency per mesh a tooth force goes through. A planetary's
|
|
568
|
+
* goes through two (sun to planet, planet to ring); a worm's is its own, from its lead angle and friction.
|
|
569
|
+
* @param {'spur' | 'helical' | 'herringbone' | 'rack' | 'internal' | 'bevel' | 'planetary' | 'worm'} kind
|
|
570
|
+
* @param {{ efficiency?: number, worm?: { efficiency: number } }} [o] efficiency: your measured figure per mesh
|
|
571
|
+
* @returns {number} from 0 to 1
|
|
572
|
+
*/
|
|
573
|
+
export function stageEfficiency(kind, o = {}) {
|
|
574
|
+
const each = o.efficiency ?? D('efficiency');
|
|
575
|
+
if (!(typeof each === 'number' && each > 0 && each <= 1)) throw new Error(`stageEfficiency: efficiency is from 0 to 1 (got ${shown(o.efficiency)})`);
|
|
576
|
+
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; }
|
|
577
|
+
if (['spur', 'helical', 'herringbone', 'rack', 'internal', 'bevel'].includes(kind)) return each;
|
|
578
|
+
if (kind === 'planetary') return each ** 2;
|
|
579
|
+
throw new Error(`stageEfficiency: no kind of stage ${shown(kind)} (spur, helical, herringbone, rack, internal, bevel, planetary or worm)`);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
// ---------- strength ----------
|
|
584
|
+
|
|
585
|
+
// The Lewis form factor for 20° full-depth teeth, by number of teeth (Budynas and Nisbett, Shigley's Mechanical
|
|
586
|
+
// Engineering Design, 10th ed., Table 14-2); a rack is 0.485. Between rows, read along a straight line.
|
|
587
|
+
const LEWIS = [[12, 0.245], [13, 0.261], [14, 0.277], [15, 0.290], [16, 0.296], [17, 0.303], [18, 0.309], [19, 0.314], [20, 0.322], [21, 0.328], [22, 0.331], [24, 0.337], [26, 0.346], [28, 0.353], [30, 0.359], [34, 0.371], [38, 0.384], [43, 0.397], [50, 0.409], [60, 0.422], [75, 0.435], [100, 0.447], [150, 0.460], [300, 0.472], [400, 0.480]];
|
|
588
|
+
/** The Lewis form factor Y for z teeth (20° full depth); Infinity for a rack. @param {number} z */
|
|
589
|
+
export function lewisFactor(z) {
|
|
590
|
+
if (z === Infinity) return 0.485;
|
|
591
|
+
if (z <= LEWIS[0][0]) return LEWIS[0][1];
|
|
592
|
+
if (z >= LEWIS.at(-1)[0]) return LEWIS.at(-1)[1] + ((0.485 - LEWIS.at(-1)[1]) * (1 - 400 / z));
|
|
593
|
+
const i = LEWIS.findIndex(([n]) => n >= z), [n0, y0] = LEWIS[i - 1], [n1, y1] = LEWIS[i];
|
|
594
|
+
return y0 + ((y1 - y0) * (z - n0)) / (n1 - n0);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* How strong a gear's teeth are by the Lewis equation: the bending stress at the root of a tooth carrying the whole
|
|
599
|
+
* load at its tip, σ = F_t / (b m Y), against the material's strength. Give a torque (N·mm) for the stress and safety
|
|
600
|
+
* factor at it; give a safety factor for the torque the gear carries at it. Helical teeth are read in their normal
|
|
601
|
+
* section (m cos β, z / cos³β teeth). A speed (rpm) adds Barth's velocity factor, (3.05 + V) / 3.05 for a cut (here a
|
|
602
|
+
* printed) profile. A first number: the root fillet and a tooth's neighbours help, a printed tooth's layers and
|
|
603
|
+
* voids hurt. Where it matters, measure it, or analyse the tooth (fea).
|
|
604
|
+
* @param {GearModel} g
|
|
605
|
+
* @param {{ width: number, material: { strength: number, name?: string }, torque?: number, safety?: number, speed?: number }} o
|
|
606
|
+
*/
|
|
607
|
+
export function strength(g, o = {}) {
|
|
608
|
+
if (!g || !['external', 'internal', 'rack'].includes(g.kind)) throw new Error(`strength: give a gear's model, not ${shown(g)}`);
|
|
609
|
+
const width = size('strength', 'width', o.width), sigma = o.material?.strength;
|
|
610
|
+
if (!(isNum(sigma) && sigma > 0)) throw new Error(`strength: the material needs a strength above 0 MPa (got ${shown(sigma)})`);
|
|
611
|
+
if (o.torque !== undefined) size('strength', 'torque', o.torque);
|
|
612
|
+
const safety = o.safety === undefined ? 2 : size('strength', 'safety', o.safety);
|
|
613
|
+
const beta = (g.helix ?? 0) * DEG, mn = g.module * Math.cos(beta);
|
|
614
|
+
const teeth = g.kind === 'rack' ? Infinity : g.teeth / Math.cos(beta) ** 3;
|
|
615
|
+
// an internal gear's teeth are stouter at the root than an outer gear's of as many teeth: read as a rack's, a little kind
|
|
616
|
+
const Y = g.kind === 'internal' ? lewisFactor(Infinity) : lewisFactor(teeth);
|
|
617
|
+
const r = g.kind === 'rack' ? null : g.pitch / 2;
|
|
618
|
+
const V = o.speed !== undefined && r ? (size('strength', 'speed', o.speed) * 2 * Math.PI * r) / 60000 : 0, Kv = (3.05 + V) / 3.05;
|
|
619
|
+
const perNewton = Kv / (width * mn * Y); // MPa for each newton at the pitch circle
|
|
620
|
+
const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
|
|
621
|
+
const name = o.material.name ?? 'the material';
|
|
622
|
+
const result = { Y, velocity: Kv, force: (sigma / safety) / perNewton, torque: r ? ((sigma / safety) / perNewton) * r : null, findings: out };
|
|
623
|
+
if (o.torque !== undefined) {
|
|
624
|
+
if (!r) throw new Error('strength: a rack takes a force, not a torque (the torque is its pinion\'s)');
|
|
625
|
+
const F = o.torque / r, stress = F * perNewton, n = sigma / stress;
|
|
626
|
+
Object.assign(result, { stress, safety: n });
|
|
627
|
+
f(n >= safety ? 'pass' : n >= 1 ? 'warn' : 'fail', `at ${fmt(o.torque / 1000, 3)} N·m the teeth see ${fmt(stress, 1)} MPa at their roots (Lewis, Y ${fmt(Y, 3)}${Kv > 1 ? `, ×${fmt(Kv, 2)} for speed` : ''}): ${fmt(n, 1)}× under ${name}'s ${sigma} MPa${n >= safety ? '' : n >= 1 ? `, under the ${safety}× asked` : ': they break'}`, 'gear.strength', { stress, safety: n });
|
|
628
|
+
} 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 });
|
|
629
|
+
return result;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// ---------- bevel gears ----------
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* A straight bevel gear's numbers, for a mate of `mate` teeth with the shafts at `shaftAngle` (90° by default): the
|
|
636
|
+
* pitch cone angle δ (tan δ = sin Σ / (z₂/z₁ + cos Σ)), the cone distance R (to the apex along the pitch cone), the
|
|
637
|
+
* virtual teeth z / cos δ of its back cone (Tredgold), and the face width's limit R / 3. Module and diameters at the
|
|
638
|
+
* back (large) end.
|
|
639
|
+
* @param {{ module: number, teeth: number, mate: number, shaftAngle?: number, width?: number, pressureAngle?: number, backlash?: number }} p
|
|
640
|
+
*/
|
|
641
|
+
export function bevelModel(p) {
|
|
642
|
+
const what = 'bevelModel', c = common(what, p), z = whole(what, 'teeth', p?.teeth, 6), mate = whole(what, 'mate', p?.mate, 6);
|
|
643
|
+
const sigma = between(what, 'shaftAngle', p.shaftAngle ?? 90, 30, 150, '°') * DEG, a = c.pressureAngle * DEG;
|
|
644
|
+
const delta = Math.atan2(Math.sin(sigma), mate / z + Math.cos(sigma)), d = c.module * z, R = d / (2 * Math.sin(delta));
|
|
645
|
+
const virtual = z / Math.cos(delta);
|
|
646
|
+
const need = c.addendum - (virtual * Math.sin(a) ** 2) / 2;
|
|
647
|
+
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`);
|
|
648
|
+
const width = p.width === undefined ? R / 3 : size(what, 'width', p.width);
|
|
649
|
+
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`);
|
|
650
|
+
const g = gearModel({ ...p, teeth: z, helix: 0 });
|
|
651
|
+
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 };
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* 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
|
|
656
|
+
* profile of its back end scaled toward the apex up the face width, so every tooth runs straight to the apex and the
|
|
657
|
+
* pitch surface is the cone. (A printable approximation to the octoidal profile: close for teeth of 12 or more.)
|
|
658
|
+
* @param {{ module: number, teeth: number, mate: number, shaftAngle?: number, width?: number, bore?: number, chamfer?: number }} p
|
|
659
|
+
*/
|
|
660
|
+
export function bevel(p) {
|
|
661
|
+
const g = bevelModel(p), h = g.height, H = g.coneDistance * Math.cos(g.pitchAngle * DEG), scale = (z) => 1 - z / H;
|
|
662
|
+
const chamfer = chamfers('bevel', { ...p, chamfer: p.chamfer ?? 0 }, h);
|
|
663
|
+
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`);
|
|
664
|
+
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 });
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// A bevel gear's section across its axis at its back end, by Tredgold's construction: the tooth drawn on the back
|
|
668
|
+
// cone, developed flat as a spur gear of z / cos δ teeth; wrapped back onto the back cone (heights along its slant,
|
|
669
|
+
// angles × 1 / cos δ); then seen from the apex onto the plane of the back end's pitch circle. Every section nearer the
|
|
670
|
+
// apex is this one scaled toward the axis, the teeth running straight to the apex.
|
|
671
|
+
function bevelSection(g, grow = 0) {
|
|
672
|
+
const d = g.pitchAngle * DEG, m = g.module, zv = g.virtualTeeth, a = g.pressureAngle * DEG;
|
|
673
|
+
const rv = (m * zv) / 2, r = g.pitch / 2, H = r / Math.tan(d); // the developed pitch radius; the apex over the pitch circle
|
|
674
|
+
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 });
|
|
675
|
+
const mapped = tooth.map(([x, y]) => {
|
|
676
|
+
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
|
|
677
|
+
const q = rho * Math.cos(d), zq = -r * Math.tan(d) + rho * Math.sin(d); // on the back cone: radius, height
|
|
678
|
+
const k = q * (H / (H - zq)); // seen from the apex, on the plane z = 0
|
|
679
|
+
return [k * Math.cos(phi), k * Math.sin(phi)];
|
|
680
|
+
});
|
|
681
|
+
const out = [];
|
|
682
|
+
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]); }
|
|
683
|
+
return out;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* How two bevel gears mesh: turns of b for one of a, the cone angles (they add to the shaft angle), and the contact
|
|
688
|
+
* ratio of their back cones' equivalent spur gears (Tredgold). @param {object} a @param {object} b the gears' parameters
|
|
689
|
+
*/
|
|
690
|
+
export function bevelMesh(a, b) {
|
|
691
|
+
const ga = bevelModel({ ...a, mate: b.teeth }), gb = bevelModel({ ...b, mate: a.teeth });
|
|
692
|
+
const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
|
|
693
|
+
if (Math.abs(ga.shaftAngle - gb.shaftAngle) > 1e-9) f('fail', `shaft angles differ (${ga.shaftAngle}° and ${gb.shaftAngle}°)`, 'mesh.bevel');
|
|
694
|
+
const virtual = (g) => gearModel({ module: g.module, teeth: Math.max(6, Math.round(g.virtualTeeth)), shift: Math.max(g.shift, 0), pressureAngle: g.pressureAngle });
|
|
695
|
+
const back = mesh(virtual(ga), virtual(gb));
|
|
696
|
+
for (const x of back.findings) out.push({ ...x, text: `on the back cones: ${x.text}` });
|
|
697
|
+
return { ratio: ga.teeth / gb.teeth, pitchAngles: [ga.pitchAngle, gb.pitchAngle], coneDistance: ga.coneDistance, contact: back.contact, findings: out };
|
|
698
|
+
}
|