dechatter 0.0.0-stage → 0.1.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/web/wizard.js ADDED
@@ -0,0 +1,190 @@
1
+ // Setup wizard: the session's setup as five short forms. Values are kept in millimetres, as
2
+ // the setup file has them; on a machine in inches they are only shown and typed in inches.
3
+
4
+ const MM_PER_INCH = 25.4;
5
+ const LENGTH_UNITS = {
6
+ length: { mm: 'mm', in: 'in' },
7
+ lengths: { mm: 'mm', in: 'in' },
8
+ feed: { mm: 'mm/min', in: 'in/min' },
9
+ };
10
+
11
+ export const STEPS = [
12
+ {
13
+ title: 'Machine',
14
+ hint: 'The mill this tool runs in.',
15
+ fields: [
16
+ { at: 'machine.name', label: 'Name', type: 'text', optional: true },
17
+ { at: 'machine.max_rpm', label: 'Highest spindle speed', type: 'int', unit: 'rpm' },
18
+ { at: 'machine.min_rpm', label: 'Lowest usable spindle speed', type: 'int', unit: 'rpm' },
19
+ { at: 'machine.spindle_power_w', label: 'Spindle motor power', type: 'number', unit: 'W' },
20
+ { at: 'machine.weight_kg', label: 'Machine weight', type: 'number', unit: 'kg', note: 'Light machines start with a lighter chip load.' },
21
+ {
22
+ at: 'machine.spindle',
23
+ label: 'Spindle speed is set by',
24
+ type: 'choice',
25
+ options: [['program', 'the program (M3 S)'], ['manual', 'me, by hand']],
26
+ note: 'By hand, the program stops before each speed and tells you what to set.',
27
+ },
28
+ ],
29
+ },
30
+ {
31
+ title: 'Tool',
32
+ hint: 'One session per tool assembly: holder, cutter and stickout. Change any of them and the results no longer apply.',
33
+ fields: [
34
+ { at: 'tool.diameter_mm', label: 'Cutter diameter', type: 'length' },
35
+ { at: 'tool.flutes', label: 'Flutes', type: 'int' },
36
+ { at: 'tool.stickout_mm', label: 'Stickout from the holder', type: 'length' },
37
+ { at: 'tool.holder', label: 'Holder', type: 'text', optional: true },
38
+ { at: 'tool.material', label: 'Cutter material', type: 'choice', options: [['carbide', 'carbide'], ['hss', 'high-speed steel']] },
39
+ ],
40
+ },
41
+ {
42
+ title: 'Cut',
43
+ hint: 'Each depth is tested on its own, stepping the spindle speed and the radial width.',
44
+ fields: [
45
+ { at: 'cut.material', label: 'Work material', type: 'choice', options: 'materials' },
46
+ { at: 'cut.axial_depth_mm', label: 'Axial depth of cut', type: 'length', note: 'The deepest cut you want. It is tested first.' },
47
+ { at: 'cut.other_depths_mm', label: 'Shallower depths to compare', type: 'lengths', optional: true, placeholder: 'none', note: 'Separated by commas. Tested afterwards, starting from the widths that were stable.' },
48
+ { at: 'cut.direction', label: 'Direction', type: 'choice', options: [['conventional', 'conventional'], ['climb', 'climb']] },
49
+ { at: 'cut.width_max_mm', label: 'Widest radial cut to try', type: 'length', optional: true, placeholder: 'half the diameter' },
50
+ { at: 'cut.chip_load_mm', label: 'Chip load per tooth', type: 'length', optional: true, placeholder: 'from the material' },
51
+ { at: 'cut.speed_min_rpm', label: 'Slowest speed to try', type: 'int', unit: 'rpm', optional: true, placeholder: 'a quarter of the fastest' },
52
+ { at: 'cut.speed_max_rpm', label: 'Fastest speed to try', type: 'int', unit: 'rpm', optional: true, placeholder: 'machine or material limit' },
53
+ ],
54
+ },
55
+ {
56
+ title: 'Stock and program',
57
+ hint: 'A block the test can cut up, clamped low and rigidly.',
58
+ fields: [
59
+ { at: 'stock.length_mm', label: 'Stock length (X)', type: 'length', note: 'Every pass cuts the full length.' },
60
+ { at: 'stock.width_mm', label: 'Stock width to use (Y)', type: 'length', note: 'Material the test may consume from the front face.' },
61
+ { at: 'program.coolant', label: 'Coolant', type: 'choice', options: [['none', 'none'], ['flood', 'flood'], ['mist', 'mist']], note: 'Coolant noise makes chatter harder to hear.' },
62
+ { at: 'program.dwell_s', label: 'Pause at speed before each pass', type: 'number', unit: 's' },
63
+ { at: 'program.clearance_mm', label: 'Clearance around the stock', type: 'length' },
64
+ { at: 'program.safe_z_mm', label: 'Safe height (Z)', type: 'length' },
65
+ { at: 'program.plunge_feed_mm_min', label: 'Plunge feed', type: 'feed' },
66
+ { at: 'program.truing_width_mm', label: 'Truing pass width', type: 'length' },
67
+ ],
68
+ },
69
+ {
70
+ title: 'Planner',
71
+ hint: 'How boldly the next cuts are chosen. The defaults suit most sessions.',
72
+ fields: [
73
+ { at: 'planner.round1_speeds', label: 'Speeds in round 1', type: 'int' },
74
+ { at: 'planner.round1_width_fractions', label: 'Round 1 widths', type: 'list', note: 'Fractions of the widest radial cut, separated by commas.' },
75
+ { at: 'planner.batch_size', label: 'Most cuts in a later round', type: 'int' },
76
+ { at: 'planner.min_probability', label: 'Chance of being stable a cut needs', type: 'number', note: 'Assuming the cut before it passes. Between 0 and 1.' },
77
+ { at: 'planner.probability_floor', label: 'The same, on measured cuts alone', type: 'number' },
78
+ { at: 'planner.stop_improvement', label: 'Stop when a cut would add less than', type: 'number', note: 'As a fraction of the best removal rate so far.' },
79
+ ],
80
+ },
81
+ ];
82
+
83
+ // Enough decimals that a hundred-thousandth of an inch survives the trip to millimetres.
84
+ const tidy = (number) => Number(number.toFixed(5));
85
+
86
+ function shown(field, value, units) {
87
+ if (value === null || value === undefined) {
88
+ return '';
89
+ }
90
+ if (field.type === 'lengths') {
91
+ return value.map((length) => (units === 'in' ? tidy(length / MM_PER_INCH) : length)).join(', ');
92
+ }
93
+ if (field.type in LENGTH_UNITS && units === 'in') {
94
+ return String(tidy(value / MM_PER_INCH));
95
+ }
96
+ if (field.type === 'list') {
97
+ return value.join(', ');
98
+ }
99
+ return String(value);
100
+ }
101
+
102
+ function typed(field, text, units) {
103
+ if (field.type === 'text' || field.type === 'choice') {
104
+ return text.trim();
105
+ }
106
+ if (field.type === 'list') {
107
+ return text.split(',').map((part) => Number(part.trim()));
108
+ }
109
+ if (field.type === 'lengths') {
110
+ const lengths = text.trim() === '' ? [] : text.split(',').map((part) => Number(part.trim()));
111
+ return lengths.map((length) => (units === 'in' ? tidy(length * MM_PER_INCH) : length));
112
+ }
113
+ if (text.trim() === '' || Number.isNaN(Number(text))) {
114
+ return null;
115
+ }
116
+ const number = Number(text);
117
+ if (field.type === 'int') {
118
+ return Math.round(number);
119
+ }
120
+ if (field.type in LENGTH_UNITS && units === 'in') {
121
+ return tidy(number * MM_PER_INCH);
122
+ }
123
+ return number;
124
+ }
125
+
126
+ function control(field, value, units, materials) {
127
+ if (field.type === 'choice') {
128
+ const options = field.options === 'materials' ? materials.map((name) => [name, name]) : field.options;
129
+ const select = document.createElement('select');
130
+ select.append(...options.map(([id, label]) => Object.assign(document.createElement('option'), { value: id, textContent: label })));
131
+ select.value = value;
132
+ return select;
133
+ }
134
+ const input = document.createElement('input');
135
+ input.value = shown(field, value, units);
136
+ input.required = !field.optional;
137
+ input.placeholder = field.placeholder ?? '';
138
+ if (field.type === 'list' || field.type === 'lengths') {
139
+ input.pattern = String.raw`\s*\d*\.?\d+(\s*,\s*\d*\.?\d+)*\s*`;
140
+ input.title = 'Numbers separated by commas';
141
+ return input;
142
+ }
143
+ if (field.type === 'text') {
144
+ return input;
145
+ }
146
+ input.type = 'number';
147
+ input.step = field.type === 'int' ? '1' : 'any';
148
+ input.min = field.type === 'int' ? '1' : '0';
149
+ return input;
150
+ }
151
+
152
+ // Fill `form` with the fields of one step.
153
+ export function draw(form, step, values, units, materials) {
154
+ form.replaceChildren(
155
+ ...step.fields.map((field) => {
156
+ const [section, key] = field.at.split('.');
157
+ const input = control(field, values[section][key], units, materials);
158
+ input.name = field.at;
159
+ // What the user was shown. Only a field they change is written back: a length
160
+ // shown rounded in inches must not come back as a slightly different length.
161
+ input.dataset.shown = input.value;
162
+ const row = document.createElement('label');
163
+ const unit = LENGTH_UNITS[field.type]?.[units] ?? field.unit ?? '';
164
+ row.append(
165
+ Object.assign(document.createElement('span'), { textContent: field.label }),
166
+ input,
167
+ Object.assign(document.createElement('small'), { textContent: unit }),
168
+ );
169
+ if (field.note) {
170
+ row.append(Object.assign(document.createElement('em'), { textContent: field.note }));
171
+ }
172
+ return row;
173
+ }),
174
+ );
175
+ }
176
+
177
+ // Copy what the form shows back into `values`.
178
+ export function read(form, step, values, units) {
179
+ for (const field of step.fields) {
180
+ const [section, key] = field.at.split('.');
181
+ const input = form.elements[field.at];
182
+ // Back and a change of units read a step without validating it: a half-typed or invalid
183
+ // entry is dropped there, never stored.
184
+ if (input.value === input.dataset.shown || !input.checkValidity()) {
185
+ continue;
186
+ }
187
+ values[section][key] = typed(field, input.value, units);
188
+ input.dataset.shown = input.value;
189
+ }
190
+ }