@rulecad/motors 0.1.2 → 0.1.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/README.md CHANGED
@@ -102,3 +102,24 @@ motor's limit is a `motor.speed` failure.
102
102
 
103
103
  A stepper's real curve is on its datasheet when the maker gives one; the kit doesn't have one for any motor yet, says so
104
104
  in `how`, and the straight model is on the cautious side of most motors but not all of them. Measure yours.
105
+
106
+ ## Drivers and the power budget
107
+
108
+ A stepper's current is set on its driver, and the driver's heat and the supply's size follow from it. `driver(name)` reads a driver's rule set; `setting(d, current, { cooled, supply, microsteps })` gives the trimmer voltage and whatever is wrong with the choice; `powerBudget(parts, { voltage, current, margin })` adds up the machine against a supply.
109
+
110
+ ```js
111
+ import { driver, setting, powerBudget } from '@rulecad/motors'
112
+
113
+ setting(driver('a4988'), 1.2).vref // 0.653 V, by the module's sense resistor: read yours
114
+ powerBudget([{ name: 'shoulder', current: 1.5, resistance: 1.1 }], { voltage: 24, current: 5 })
115
+ ```
116
+
117
+ | Figure | Formula | Source |
118
+ |---|---|---|
119
+ | A4988 current limit | I = Vref / (8 Rs) | Pololu's A4988 carrier page |
120
+ | DRV8825 current limit | I = 2 Vref | Pololu's DRV8825 carrier page |
121
+ | TMC2209 current (Vref) | Irms = (325 mV / (Rs + 20 mΩ)) (1 / √2) (Vref / 2.5 V) | BigTreeTech and FYSETC module pages |
122
+ | A stepper's draw | P = 2 I² R / 0.85, both coils at the set current, through a chopper about 85% efficient | Copper loss; the efficiency is an estimate |
123
+
124
+ A chopper draws power, not the phase current, so a motor set to 2 A takes much less than 2 A from a 24 V supply. Microsteps add resolution, not accuracy.
125
+
package/drivers.d.mts ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * A driver module from its rule set.
3
+ * @param {string} name a4988, drv8825 or tmc2209
4
+ * @param {{ variant?: string }} [o] a board's variant (an older sense resistor, say)
5
+ * @returns {{ name: string, voltage: number[], logic: number[], bare: number, cooled: number, sense: number, gain: number, formula: string, maxMicrosteps: number, figures: object }}
6
+ */
7
+ export function driver(name: string, { variant }?: {
8
+ variant?: string;
9
+ }): {
10
+ name: string;
11
+ voltage: number[];
12
+ logic: number[];
13
+ bare: number;
14
+ cooled: number;
15
+ sense: number;
16
+ gain: number;
17
+ formula: string;
18
+ maxMicrosteps: number;
19
+ figures: object;
20
+ };
21
+ /**
22
+ * The trimmer voltage for a coil current, and whether the module is fit for it.
23
+ * @param {ReturnType<typeof driver>} d @param {number} current A per coil @param {{ cooled?: boolean, supply?: number, microsteps?: number }} [o]
24
+ * @returns {{ vref: number, findings: object[] }}
25
+ */
26
+ export function setting(d: ReturnType<typeof driver>, current: number, { cooled, supply, microsteps }?: {
27
+ cooled?: boolean;
28
+ supply?: number;
29
+ microsteps?: number;
30
+ }): {
31
+ vref: number;
32
+ findings: object[];
33
+ };
34
+ /**
35
+ * The power a machine draws, against a supply. Each part is what it draws at its worst, with the duty it runs at; a
36
+ * stepper's is its copper loss, both coils at the set current, since a chopper draws power and not phase current.
37
+ * @param {{ name: string, watts?: number, current?: number, resistance?: number, voltage?: number, duty?: number }[]} parts
38
+ * current and resistance: a stepper's coil; current and voltage: anything else
39
+ * @param {{ voltage: number, current?: number, margin?: number, duty?: number }} supply current: the supply's rating, to check against
40
+ * @returns {{ watts: number, amps: number, needed: number, parts: object[], findings: object[] }}
41
+ */
42
+ export function powerBudget(parts: {
43
+ name: string;
44
+ watts?: number;
45
+ current?: number;
46
+ resistance?: number;
47
+ voltage?: number;
48
+ duty?: number;
49
+ }[], { voltage, current, margin, duty }?: {
50
+ voltage: number;
51
+ current?: number;
52
+ margin?: number;
53
+ duty?: number;
54
+ }): {
55
+ watts: number;
56
+ amps: number;
57
+ needed: number;
58
+ parts: object[];
59
+ findings: object[];
60
+ };
61
+ /** How much of a supply's power reaches the coils: a chopper driver, about. */
62
+ export const DRIVER_EFFICIENCY: 0.85;
package/drivers.mjs ADDED
@@ -0,0 +1,66 @@
1
+ // Drivers and the power budget. A stepper's current is set on its driver: the driver's limits and the trimmer's voltage
2
+ // follow from it, and the supply's size follows from the whole machine. Figures come from the rule sets; findings are in
3
+ // the kit's shape: { level, text, code, values }.
4
+ import a4988 from '@rulecad/rules/a4988.json' with { type: 'json' };
5
+ import drv8825 from '@rulecad/rules/drv8825.json' with { type: 'json' };
6
+ import tmc2209 from '@rulecad/rules/tmc2209.json' with { type: 'json' };
7
+
8
+ const SETS = { a4988, drv8825, tmc2209 };
9
+ const n3 = (x, d = 3) => String(Number(x.toFixed(d)));
10
+ /** How much of a supply's power reaches the coils: a chopper driver, about. */
11
+ export const DRIVER_EFFICIENCY = 0.85;
12
+
13
+ /**
14
+ * A driver module from its rule set.
15
+ * @param {string} name a4988, drv8825 or tmc2209
16
+ * @param {{ variant?: string }} [o] a board's variant (an older sense resistor, say)
17
+ * @returns {{ name: string, voltage: number[], logic: number[], bare: number, cooled: number, sense: number, gain: number, formula: string, maxMicrosteps: number, figures: object }}
18
+ */
19
+ export function driver(name, { variant } = {}) {
20
+ const set = SETS[String(name).toLowerCase()];
21
+ if (!set) throw new Error(`driver: no driver "${name}" (${Object.keys(SETS).join(', ')})`);
22
+ if (variant != null && !set.variants?.[variant]) throw new Error(`driver: ${set.name} has no variant "${variant}" (${Object.keys(set.variants || {}).join(', ') || 'none'})`);
23
+ const over = variant ? set.variants[variant].values : {}, v = (k) => over[k] ?? set.rules[k]?.value;
24
+ return { name: set.name, variant: variant ?? null, voltage: [v('motor_voltage_min'), v('motor_voltage_max')], logic: [v('logic_voltage_min'), v('logic_voltage_max')], bare: v('current_bare'), cooled: v('current_cooled'), sense: v('sense_resistor'), gain: v('vref_gain'), formula: v('vref_formula'), maxMicrosteps: v('max_microsteps'), figures: set.rules, uart: set.rules.uart_note != null };
25
+ }
26
+
27
+ /**
28
+ * The trimmer voltage for a coil current, and whether the module is fit for it.
29
+ * @param {ReturnType<typeof driver>} d @param {number} current A per coil @param {{ cooled?: boolean, supply?: number, microsteps?: number }} [o]
30
+ * @returns {{ vref: number, findings: object[] }}
31
+ */
32
+ export function setting(d, current, { cooled = false, supply, microsteps } = {}) {
33
+ if (!(current > 0)) throw new Error(`setting: current is above 0 A (got ${current})`);
34
+ const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) }), limit = cooled ? d.cooled : d.bare, vref = current / d.gain;
35
+ if (current > d.cooled) f('fail', `${d.name} carries ${d.cooled} A per coil at most, with cooling, and this asks ${n3(current, 2)} A`, 'motor.driver', { current, limit: d.cooled });
36
+ else if (current > limit) f('warn', `${d.name} carries about ${d.bare} A with no heat sink, and this asks ${n3(current, 2)} A: fit a heat sink and moving air (up to ${d.cooled} A)`, 'motor.driver', { current, limit });
37
+ if (supply != null && (supply < d.voltage[0] || supply > d.voltage[1])) f('fail', `${d.name} takes ${d.voltage[0]} to ${d.voltage[1]} V, and the supply is ${supply} V`, 'motor.driver', { supply, range: d.voltage });
38
+ if (microsteps != null && microsteps > d.maxMicrosteps) f('fail', `${d.name} goes down to 1/${d.maxMicrosteps} of a step, and this asks 1/${microsteps}`, 'motor.driver', { microsteps });
39
+ return { vref, findings: out };
40
+ }
41
+
42
+ /**
43
+ * The power a machine draws, against a supply. Each part is what it draws at its worst, with the duty it runs at; a
44
+ * stepper's is its copper loss, both coils at the set current, since a chopper draws power and not phase current.
45
+ * @param {{ name: string, watts?: number, current?: number, resistance?: number, voltage?: number, duty?: number }[]} parts
46
+ * current and resistance: a stepper's coil; current and voltage: anything else
47
+ * @param {{ voltage: number, current?: number, margin?: number, duty?: number }} supply current: the supply's rating, to check against
48
+ * @returns {{ watts: number, amps: number, needed: number, parts: object[], findings: object[] }}
49
+ */
50
+ export function powerBudget(parts, { voltage, current, margin = 1.25, duty = 1 } = {}) {
51
+ if (!(voltage > 0)) throw new Error(`powerBudget: give the supply's voltage (got ${voltage})`);
52
+ if (!Array.isArray(parts) || !parts.length) throw new Error('powerBudget: give the parts that draw power');
53
+ const rows = parts.map((p) => {
54
+ const d = p.duty ?? duty, full = p.watts ?? (p.resistance != null ? (2 * p.current * p.current * p.resistance) / DRIVER_EFFICIENCY : p.current * (p.voltage ?? voltage));
55
+ if (!(full >= 0) || !Number.isFinite(full)) throw new Error(`powerBudget: ${p.name}: give watts, or a current with a resistance or a voltage`);
56
+ return { name: p.name, watts: full, duty: d, average: full * d };
57
+ });
58
+ const watts = rows.reduce((t, p) => t + p.watts, 0), amps = watts / voltage, needed = amps * margin, findings = [];
59
+ if (current != null) {
60
+ const v = { amps, needed, rating: current, watts };
61
+ if (current < amps) findings.push({ level: 'fail', text: `the machine draws ${n3(amps, 2)} A at ${voltage} V (${n3(watts, 1)} W), and the supply gives ${current} A`, code: 'power.supply', values: v });
62
+ else if (current < needed) findings.push({ level: 'fail', text: `the machine draws ${n3(amps, 2)} A at ${voltage} V (${n3(watts, 1)} W): with ${margin} times in hand it wants ${n3(needed, 2)} A, and the supply gives ${current} A`, code: 'power.supply', values: v });
63
+ else findings.push({ level: 'pass', text: `the machine draws ${n3(amps, 2)} A at ${voltage} V (${n3(watts, 1)} W), and the supply gives ${current} A, ${n3(current / amps, 1)} times over`, code: 'power.supply', values: v });
64
+ }
65
+ return { watts, amps, needed, parts: rows, findings };
66
+ }
package/motors.d.mts CHANGED
@@ -235,3 +235,4 @@ export type Motor = {
235
235
  axis: number[];
236
236
  }>;
237
237
  };
238
+ export { driver, setting, powerBudget, DRIVER_EFFICIENCY } from "./drivers.mjs";
package/motors.mjs CHANGED
@@ -20,6 +20,8 @@ import m2 from '@rulecad/rules/m2.json' with { type: 'json' };
20
20
  import m3 from '@rulecad/rules/m3.json' with { type: 'json' };
21
21
  import m4 from '@rulecad/rules/m4.json' with { type: 'json' };
22
22
 
23
+ export { driver, setting, powerBudget, DRIVER_EFFICIENCY } from './drivers.mjs';
24
+
23
25
  const { cuboid, cylinder, polygon } = jscad.primitives;
24
26
  const { extrudeLinear } = jscad.extrusions;
25
27
  const { translate } = jscad.transforms;
@@ -123,7 +125,7 @@ export function motorModel(which, change = {}) {
123
125
  };
124
126
  if (type === 'stepper') m.stepAngle = v('step_angle') / m.ratio;
125
127
  // Anchors an assembly places a motor by: the shaft where it leaves the motor, its tip, and the mounting face
126
- m.anchors = { face: { origin: [0, 0, 0], axis: [0, 0, 1] }, shaft: { origin: [0, 0, base], axis: [0, 0, 1] }, tip: { origin: [0, 0, tip], axis: [0, 0, 1] }, ...(type === 'servo' ? { horn: { origin: [0, 0, tip], axis: [0, 0, 1] } } : {}), ...Object.fromEntries(holes.map((h, i) => [`hole${i + 1}`, { origin: [h[0], h[1], 0], axis: [0, 0, 1] }])) };
128
+ m.anchors = { face: { origin: [0, 0, 0], axis: [0, 0, 1] }, connector: { origin: [centre[0], centre[1] + depth / 2, -m.body.length / 2], axis: [0, 1, 0] }, shaft: { origin: [0, 0, base], axis: [0, 0, 1] }, tip: { origin: [0, 0, tip], axis: [0, 0, 1] }, ...(type === 'servo' ? { horn: { origin: [0, 0, tip], axis: [0, 0, 1] } } : {}), ...Object.fromEntries(holes.map((h, i) => [`hole${i + 1}`, { origin: [h[0], h[1], 0], axis: [0, 0, 1] }])) };
127
129
  return m;
128
130
  }
129
131
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/motors",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Motors for printed machines: steppers, DC gear motors, servos and brushless motors from their datasheets, as models with sourced figures, solids to place in an assembly, the mounts and D-bores a part takes for them, and what torque and speed reach the output.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,6 +17,8 @@
17
17
  "files": [
18
18
  "motors.mjs",
19
19
  "diagram.mjs",
20
+ "drivers.mjs",
21
+ "drivers.d.mts",
20
22
  "motors.d.mts",
21
23
  "diagram.d.mts",
22
24
  "README.md",
@@ -31,5 +33,5 @@
31
33
  "bugs": { "url": "https://github.com/JoniBach/rulecad/issues" },
32
34
  "scripts": { "test": "node --test" },
33
35
  "peerDependencies": { "@jscad/modeling": "^2.12.0" },
34
- "dependencies": { "@rulecad/rules": "^0.3.6" }
36
+ "dependencies": { "@rulecad/rules": "^0.3.8" }
35
37
  }