@rulecad/cad 0.8.0 → 0.9.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/AGENTS.md +24 -10
- package/CHANGELOG.md +404 -0
- package/EXAMPLES.md +40 -38
- package/README.md +193 -67
- package/TUTORIAL.md +2 -2
- package/bin/cad.mjs +2250 -512
- package/lib/animate.mjs +173 -49
- package/lib/assembly.mjs +1847 -434
- package/lib/beams.mjs +256 -52
- package/lib/belts.mjs +66 -15
- package/lib/board-work.mjs +12 -0
- package/lib/bought.mjs +309 -53
- package/lib/buildpage.mjs +179 -29
- package/lib/carried.mjs +201 -0
- package/lib/clone.mjs +194 -42
- package/lib/design.mjs +649 -121
- package/lib/drive.mjs +187 -63
- package/lib/edit.mjs +899 -191
- package/lib/errors.mjs +16 -3
- package/lib/explain.mjs +62 -13
- package/lib/fasten.mjs +2702 -507
- package/lib/fittings.mjs +715 -121
- package/lib/fixings.mjs +21 -9
- package/lib/framebuild.mjs +790 -175
- package/lib/framecheck.mjs +1901 -368
- package/lib/framegen.mjs +100 -22
- package/lib/frameheat.mjs +225 -42
- package/lib/frameprint.mjs +513 -108
- package/lib/frames.mjs +81 -17
- package/lib/framesize.mjs +792 -120
- package/lib/frametip.mjs +235 -0
- package/lib/gait.mjs +135 -23
- package/lib/grip.mjs +62 -13
- package/lib/guides.mjs +1403 -230
- package/lib/host.mjs +123 -21
- package/lib/inplace.mjs +171 -36
- package/lib/jigs.mjs +360 -78
- package/lib/joins.mjs +697 -118
- package/lib/jsonedit.mjs +88 -26
- package/lib/kept.mjs +84 -0
- package/lib/kit.mjs +438 -68
- package/lib/layout.mjs +2 -1
- package/lib/links.mjs +149 -42
- package/lib/list.mjs +326 -64
- package/lib/live-guard.mjs +68 -0
- package/lib/live-shape.mjs +96 -24
- package/lib/live.mjs +189 -59
- package/lib/loads.mjs +579 -130
- package/lib/machine.mjs +372 -74
- package/lib/magnets.mjs +162 -44
- package/lib/measured.mjs +417 -94
- package/lib/mesh.mjs +526 -119
- package/lib/meshcmd.mjs +215 -60
- package/lib/motors.mjs +1021 -213
- package/lib/motorsvg.mjs +114 -32
- package/lib/names.mjs +3 -1
- package/lib/optimise.mjs +978 -283
- package/lib/order.mjs +519 -112
- package/lib/panels.mjs +482 -94
- package/lib/parts.mjs +78 -23
- package/lib/pins.mjs +163 -44
- package/lib/plates.mjs +223 -35
- package/lib/player.mjs +37 -9
- package/lib/power.mjs +64 -13
- package/lib/preview.mjs +83 -16
- package/lib/profilemeasure.mjs +572 -129
- package/lib/rules.mjs +15 -5
- package/lib/sandbox-hooks.mjs +77 -19
- package/lib/sandbox-run.mjs +117 -18
- package/lib/sandbox.mjs +509 -60
- package/lib/schema.mjs +42 -11
- package/lib/seats.mjs +227 -49
- package/lib/services.mjs +261 -52
- package/lib/sheet.mjs +851 -178
- package/lib/slotnuts.mjs +213 -34
- package/lib/targets/gist.mjs +4 -1
- package/lib/targets.mjs +22 -4
- package/lib/task.mjs +376 -79
- package/lib/versions.mjs +111 -28
- package/lib/wires.mjs +228 -45
- package/lib/workspace.mjs +197 -12
- package/package.json +19 -19
- package/schemas/assembly.schema.json +2 -2
package/lib/frametip.mjs
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// frame.tip: whether a frame standing free on its feet tips over, under its weight and what it carries alone, and
|
|
2
|
+
// under the side push frame.rack racks it with.
|
|
3
|
+
//
|
|
4
|
+
// A foot stands on the floor: the floor pushes it up and never pulls it down. The frame solve holds each foot both
|
|
5
|
+
// ways (it has to, to be solved at all), so a foot the frame would lift off is held down by the solve, and no figure
|
|
6
|
+
// of the solve says the frame tips. This check does not read how the solve shares the load among the feet: it reads
|
|
7
|
+
// the sum of what the feet give (their forces and their moments, a fixed foot's moment counting as the floor pulling
|
|
8
|
+
// one side of it down), which is the frame's weight and loads whatever the joins' springs, and takes moments about
|
|
9
|
+
// each line the frame could tip over: each edge of the outline round its feet, seen from above. Over an edge, the
|
|
10
|
+
// weight's moment about it holds the frame down (restoring) and the push's turns it over (overturning); where the
|
|
11
|
+
// push's moment is the larger, the feet on the other side lift and the frame falls. The feet are taken at their beam
|
|
12
|
+
// ends' axes, so the outline is the smallest it can be: a foot's pad standing wider only adds to what holds it.
|
|
13
|
+
//
|
|
14
|
+
// Only a frame standing free is checked: on its feet, or on its lowest ends where nothing says where it is held.
|
|
15
|
+
// Supports said in "frame": { "supports" } are where it is held (bolted to a bench, clamped to a wall), both ways,
|
|
16
|
+
// and are not checked for tipping.
|
|
17
|
+
// Units: mm, N, N·mm.
|
|
18
|
+
|
|
19
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
|
|
20
|
+
sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]],
|
|
21
|
+
scale = (a, k) => [a[0] * k, a[1] * k, a[2] * k],
|
|
22
|
+
cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]],
|
|
23
|
+
unit = (v) => scale(v, 1 / Math.hypot(...v));
|
|
24
|
+
const n3 = (x, d = 3) => String(+Number(x).toFixed(d)),
|
|
25
|
+
r3 = (x) => +Number(x).toFixed(3),
|
|
26
|
+
fig = (x) => String(+Number(x).toPrecision(3));
|
|
27
|
+
const and = (xs) => (xs.length > 1 ? `${xs.slice(0, -1).join(', ')} and ${xs[xs.length - 1]}` : (xs[0] ?? ''));
|
|
28
|
+
|
|
29
|
+
// within this many mm of an edge, a foot is on it (it stays down as the frame tips over that edge)
|
|
30
|
+
const ON_EDGE = 1;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The outline round points on the floor, seen from above: its corners in order round it (Andrew's monotone chain).
|
|
34
|
+
* @param {{ u: number, v: number }[]} pts @returns {number[]} indices of the outline's corners, anticlockwise
|
|
35
|
+
*/
|
|
36
|
+
function outline(pts) {
|
|
37
|
+
const ix = pts.map((_, i) => i).sort((a, b) => pts[a].u - pts[b].u || pts[a].v - pts[b].v);
|
|
38
|
+
const turn = (o, a, b) =>
|
|
39
|
+
(pts[a].u - pts[o].u) * (pts[b].v - pts[o].v) - (pts[a].v - pts[o].v) * (pts[b].u - pts[o].u);
|
|
40
|
+
const half = (order) => {
|
|
41
|
+
const h = [];
|
|
42
|
+
for (const i of order) {
|
|
43
|
+
while (h.length >= 2 && turn(h[h.length - 2], h[h.length - 1], i) <= 1e-9) h.pop();
|
|
44
|
+
h.push(i);
|
|
45
|
+
}
|
|
46
|
+
h.pop();
|
|
47
|
+
return h;
|
|
48
|
+
};
|
|
49
|
+
return [...half(ix), ...half([...ix].reverse())];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* How a frame standing free on its feet tips over: for each case solved under weight, the moment its weight holds it
|
|
54
|
+
* down with about each edge of its feet's outline; and each way it is racked, the least side push at its top that tips
|
|
55
|
+
* it, over which feet, lifting which. Null when the frame is held where "frame": { "supports" } says (`held` then
|
|
56
|
+
* names them) or stands on fewer than two places.
|
|
57
|
+
* @param {object} fm frameModelOf's model
|
|
58
|
+
* @param {{ case: { label: string }, spring: { reactions: Record<string, number[]> } }[]} results each case's solve
|
|
59
|
+
* @param {{ F: number, top: string[], loadFrom: string } | null} rk rackOf's push, or null where it has no height
|
|
60
|
+
* @param {{ dir: number[], name: string }[]} ways the two ways across the frame it is racked (sideways(down))
|
|
61
|
+
*/
|
|
62
|
+
export function tipOf(fm, results, rk, ways) {
|
|
63
|
+
const kept = fm.supports.filter((sp) => fm.nodeAt(sp.beam, sp.z) != null);
|
|
64
|
+
if (kept.some((sp) => sp.said)) return { held: kept.map((sp) => sp.where) };
|
|
65
|
+
const up = scale(fm.down, -1),
|
|
66
|
+
[e1, e2] = ways.map((w) => w.dir);
|
|
67
|
+
const feet = [];
|
|
68
|
+
for (const sp of kept) {
|
|
69
|
+
const node = fm.nodeAt(sp.beam, sp.z);
|
|
70
|
+
if (!feet.some((x) => x.node === node)) {
|
|
71
|
+
const p = fm.nodes[node];
|
|
72
|
+
feet.push({ where: sp.where, node, p, u: dot(p, e1), v: dot(p, e2) });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (!feet.length) return null;
|
|
76
|
+
// the edges the frame can tip over: each with a foot on it, its outward normal on the floor and the axis it turns
|
|
77
|
+
// about as it tips that way (up × the normal: turning about it lifts what lies inside)
|
|
78
|
+
const hull = outline(feet);
|
|
79
|
+
const edges = [];
|
|
80
|
+
const span = Math.max(...feet.map((a) => Math.max(...feet.map((b) => Math.hypot(a.u - b.u, a.v - b.v)))));
|
|
81
|
+
let line = false;
|
|
82
|
+
const flat = (v) => sub(v, scale(up, dot(v, up))),
|
|
83
|
+
mid = scale(
|
|
84
|
+
feet.reduce((t, x) => [t[0] + x.p[0], t[1] + x.p[1], t[2] + x.p[2]], [0, 0, 0]),
|
|
85
|
+
1 / feet.length,
|
|
86
|
+
);
|
|
87
|
+
if (hull.length >= 3) {
|
|
88
|
+
for (let i = 0; i < hull.length; i++) {
|
|
89
|
+
const a = feet[hull[i]],
|
|
90
|
+
b = feet[hull[(i + 1) % hull.length]];
|
|
91
|
+
let n = unit(cross(sub(b.p, a.p), up));
|
|
92
|
+
if (dot(flat(sub(mid, a.p)), n) > 0) n = scale(n, -1); // (outward: away from the middle of the feet)
|
|
93
|
+
edges.push({ q: a.p, n });
|
|
94
|
+
}
|
|
95
|
+
} else if (span > ON_EDGE) {
|
|
96
|
+
// feet in one line: the frame can tip over either end of the line; across it, nothing but the feet's pads holds it
|
|
97
|
+
line = true;
|
|
98
|
+
const [a, b] = [feet[hull[0]], feet[hull[hull.length - 1]]],
|
|
99
|
+
along = unit(flat(sub(b.p, a.p)));
|
|
100
|
+
edges.push({ q: b.p, n: along }, { q: a.p, n: scale(along, -1) });
|
|
101
|
+
} else return null;
|
|
102
|
+
for (const e of edges) {
|
|
103
|
+
e.t = cross(up, e.n);
|
|
104
|
+
e.on = feet.filter((x) => Math.abs(dot(sub(x.p, e.q), e.n)) <= ON_EDGE).map((x) => x.where);
|
|
105
|
+
e.lift = feet.filter((x) => dot(sub(x.p, e.q), e.n) < -ON_EDGE).map((x) => x.where);
|
|
106
|
+
}
|
|
107
|
+
// each case: what the feet give, summed, is the weight and loads the other way round
|
|
108
|
+
const cases = results.map((r) => {
|
|
109
|
+
let A = [0, 0, 0],
|
|
110
|
+
M = [0, 0, 0];
|
|
111
|
+
for (const [n, six] of Object.entries(r.spring.reactions)) {
|
|
112
|
+
const F = six.slice(0, 3),
|
|
113
|
+
p = fm.nodes[n];
|
|
114
|
+
A = sub(A, F);
|
|
115
|
+
M = sub(M, [six[3] + cross(p, F)[0], six[4] + cross(p, F)[1], six[5] + cross(p, F)[2]]);
|
|
116
|
+
}
|
|
117
|
+
const W = Math.max(0, dot(A, fm.down));
|
|
118
|
+
// about each edge: the moment of the loads turning the frame over it (above 0 tips it), so its weight's hold is the
|
|
119
|
+
// negative
|
|
120
|
+
const hold = edges.map((e) => -dot(sub(M, cross(e.q, A)), e.t));
|
|
121
|
+
return { label: r.case.label, W, hold };
|
|
122
|
+
});
|
|
123
|
+
// under its weight alone: a case whose loads turn it over an edge (beyond a hundredth of a mm of its weight's lever)
|
|
124
|
+
const falls = [];
|
|
125
|
+
for (const c of cases)
|
|
126
|
+
c.hold.forEach((h, i) => {
|
|
127
|
+
if (h < -0.01 * Math.max(c.W, 1e-9)) falls.push({ case: c.label, edge: i, moment: -h, W: c.W });
|
|
128
|
+
});
|
|
129
|
+
// each way it is racked, both senses: the least push at the top that turns it over an edge, in any case
|
|
130
|
+
const pushes = [];
|
|
131
|
+
if (rk)
|
|
132
|
+
for (const { dir, name } of ways) {
|
|
133
|
+
let worst = null;
|
|
134
|
+
for (const sense of [1, -1]) {
|
|
135
|
+
const d = scale(dir, sense);
|
|
136
|
+
edges.forEach((e, i) => {
|
|
137
|
+
// the push's moment about the edge per newton: its arm, in mm
|
|
138
|
+
const arm = rk.top.reduce((t, n) => t + dot(cross(sub(fm.nodes[n], e.q), d), e.t), 0) / rk.top.length;
|
|
139
|
+
if (!(arm > 1e-6)) return;
|
|
140
|
+
for (const c of cases) {
|
|
141
|
+
const tips = Math.max(0, c.hold[i]) / arm;
|
|
142
|
+
if (!worst || tips < worst.tips) worst = { tips, arm, hold: c.hold[i], W: c.W, case: c.label, edge: i, d };
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
if (worst) pushes.push({ name, dir, ...worst, on: edges[worst.edge].on, lift: edges[worst.edge].lift });
|
|
147
|
+
}
|
|
148
|
+
return { feet, edges, cases, falls, pushes, line, multi: results.length > 1 };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* frame.tip's findings from tipOf: a fail where the frame tips under its weight alone, and each way it is racked, a
|
|
153
|
+
* fail where a push less than frame.rack's tips it over (else a pass with the push that would). An info where it is
|
|
154
|
+
* held where "frame": { "supports" } says, or stands on its feet in one line.
|
|
155
|
+
* @param {ReturnType<typeof tipOf>} tip @param {{ F: number, loadFrom: string } | null} rk @param {Function} f the check's finding writer
|
|
156
|
+
*/
|
|
157
|
+
export function tipCheck(tip, rk, f) {
|
|
158
|
+
if (!tip) return;
|
|
159
|
+
if (tip.held) {
|
|
160
|
+
f(
|
|
161
|
+
'info',
|
|
162
|
+
`the frame is held where "frame": { "supports" } says (${and(tip.held)}), both ways, as bolted or clamped down: it is not checked for tipping over. Standing free, give it feet instead, and frame.tip checks it`,
|
|
163
|
+
null,
|
|
164
|
+
{ code: 'frame.tip', refs: { held: tip.held } },
|
|
165
|
+
);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const edgeSaid = (i) => and(tip.edges[i].on),
|
|
169
|
+
liftSaid = (i) => (tip.edges[i].lift.length ? and(tip.edges[i].lift) : 'the rest of it');
|
|
170
|
+
const seen = new Set();
|
|
171
|
+
for (const x of tip.falls) {
|
|
172
|
+
if (seen.has(x.edge)) continue;
|
|
173
|
+
seen.add(x.edge);
|
|
174
|
+
f(
|
|
175
|
+
'fail',
|
|
176
|
+
`under its own weight and what it carries${tip.multi ? ` (${x.case})` : ''}, the frame tips over ${edgeSaid(x.edge)}: what it carries and the loads on it turn it over them with ${fig(x.moment)} N·mm more than its weight holds it down with (${fig(x.W)} N in all), lifting ${liftSaid(x.edge)}. Move what it carries in over its feet, widen its base, or bolt it down and say so with "frame": { "supports" }`,
|
|
177
|
+
`tips over ${edgeSaid(x.edge)}`,
|
|
178
|
+
{
|
|
179
|
+
code: 'frame.tip',
|
|
180
|
+
values: { weight: r3(x.W), beyond: r3(x.moment), tips: 0 },
|
|
181
|
+
refs: { case: x.case, on: tip.edges[x.edge].on, lifts: tip.edges[x.edge].lift },
|
|
182
|
+
},
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (tip.line)
|
|
186
|
+
f(
|
|
187
|
+
'info',
|
|
188
|
+
`the frame stands on ${and(tip.feet.map((x) => x.where))}, in one line: across that line only its feet's pads hold it up, which the frame model does not have, so its tipping is judged along the line alone`,
|
|
189
|
+
null,
|
|
190
|
+
{ code: 'frame.tip', refs: { feet: tip.feet.map((x) => x.where) } },
|
|
191
|
+
);
|
|
192
|
+
if (!rk || tip.falls.length) return;
|
|
193
|
+
for (const p of tip.pushes) {
|
|
194
|
+
const over = p.tips < rk.F,
|
|
195
|
+
turning = rk.F * p.arm;
|
|
196
|
+
const text = `pushed ${p.name} at the top with ${n3(rk.F, 1)} N (${rk.loadFrom}), the frame ${over ? 'tips' : 'stands'}: over ${and(p.on)}, the push turns it with ${fig(turning)} N·mm (${n3(rk.F, 1)} N at ${n3(p.arm, 1)} mm above them) and its ${fig(p.W)} N holds it down with ${fig(p.hold)} N·mm${tip.multi ? ` (${p.case})` : ''}, so ${fig(p.tips)} N there tips it, lifting ${and(p.lift)}${over ? `. Widen its base, carry the weight lower or nearer the middle, add weight low down, or bolt it down and say so with "frame": { "supports" }` : `: ${n3(p.tips / rk.F, 2)} times the push`}`;
|
|
197
|
+
f(over ? 'fail' : 'pass', text, over ? `tips ${p.name} at ${fig(p.tips)} N` : null, {
|
|
198
|
+
code: 'frame.tip',
|
|
199
|
+
values: {
|
|
200
|
+
load: r3(rk.F),
|
|
201
|
+
tips: r3(p.tips),
|
|
202
|
+
overturning: r3(turning),
|
|
203
|
+
restoring: r3(p.hold),
|
|
204
|
+
arm: r3(p.arm),
|
|
205
|
+
weight: r3(p.W),
|
|
206
|
+
},
|
|
207
|
+
refs: {
|
|
208
|
+
direction: p.name,
|
|
209
|
+
dir: p.d.map(r3),
|
|
210
|
+
on: p.on,
|
|
211
|
+
lifts: p.lift,
|
|
212
|
+
case: p.case,
|
|
213
|
+
load: rk.loadFrom,
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* For frame.rack: the push that tips the frame over pushed this way, standing free (0 where its weight alone tips it;
|
|
221
|
+
* null where it is held down or not found), and a sentence saying so. @param {ReturnType<typeof tipOf>} tip
|
|
222
|
+
* @param {string} name the way, as sideways names it @returns {{ tips: number | null, tipSaid: string }}
|
|
223
|
+
*/
|
|
224
|
+
export function tipsWay(tip, name) {
|
|
225
|
+
const tips = tip?.falls?.length ? 0 : (tip?.pushes?.find((p) => p.name === name)?.tips ?? null);
|
|
226
|
+
return {
|
|
227
|
+
tips,
|
|
228
|
+
tipSaid:
|
|
229
|
+
tips == null
|
|
230
|
+
? ''
|
|
231
|
+
: tips === 0
|
|
232
|
+
? '. Standing free, it tips over under its own loads, with no push (frame.tip)'
|
|
233
|
+
: `. Standing free, ${fig(tips)} N pushed this way at the top tips it over (frame.tip)`,
|
|
234
|
+
};
|
|
235
|
+
}
|
package/lib/gait.mjs
CHANGED
|
@@ -10,57 +10,169 @@ const n3 = (x, d = 2) => String(Number(x.toFixed(d)));
|
|
|
10
10
|
|
|
11
11
|
/** The gait as written, its legs as chains, refused by name when the assembly is built. */
|
|
12
12
|
export function gaitOf(a, motion, num) {
|
|
13
|
-
const g = a.gait;
|
|
13
|
+
const g = a.gait;
|
|
14
|
+
if (!g) return null;
|
|
14
15
|
if (!Array.isArray(g.legs) || !g.legs.length) throw new Error('gait: legs is a list of feet, <item>.<anchor>');
|
|
15
16
|
const legs = g.legs.map((ref) => {
|
|
16
|
-
const [id, name] = String(ref).split('.'),
|
|
17
|
-
|
|
17
|
+
const [id, name] = String(ref).split('.'),
|
|
18
|
+
mark = motion.marks.find((m) => m.id === id && m.name === name);
|
|
19
|
+
if (!mark)
|
|
20
|
+
throw new Error(`gait: no anchor "${ref}" (items: ${[...new Set(motion.marks.map((m) => m.id))].join(', ')})`);
|
|
18
21
|
// A leg's gearbox sits beside its links, so the foot's plane is not the hip anchor's: it stands in its own plane, over the hip
|
|
19
|
-
const c = chainFromMotion(motion, { tip: id, tool: [mark.frame[3], mark.frame[7], mark.frame[11]] }),
|
|
22
|
+
const c = chainFromMotion(motion, { tip: id, tool: [mark.frame[3], mark.frame[7], mark.frame[11]] }),
|
|
23
|
+
zero = fk(
|
|
24
|
+
c,
|
|
25
|
+
c.names.map(() => 0),
|
|
26
|
+
),
|
|
27
|
+
hip = zero.frames[0].at;
|
|
20
28
|
return { name: id, chain: c, at: [hip[0], zero.position[1], hip[2]] };
|
|
21
29
|
});
|
|
22
|
-
const out = {
|
|
30
|
+
const out = {
|
|
31
|
+
legs,
|
|
32
|
+
pattern: g.pattern,
|
|
33
|
+
stride: num(g.stride),
|
|
34
|
+
lift: num(g.lift),
|
|
35
|
+
period: num(g.period),
|
|
36
|
+
height: num(g.height),
|
|
37
|
+
mass: num(g.mass),
|
|
38
|
+
...(g.steps ? { steps: num(g.steps) } : {}),
|
|
39
|
+
...(g.com ? { com: g.com.map(num) } : {}),
|
|
40
|
+
};
|
|
23
41
|
return out;
|
|
24
42
|
}
|
|
25
43
|
|
|
26
44
|
// The joint values of a frame of the cycle, in the assembly's terms
|
|
27
|
-
export const frameValues = (s, frame, jointValues) => {
|
|
45
|
+
export const frameValues = (s, frame, jointValues) => {
|
|
46
|
+
// a joint behind a gearbox is moved by the motor's shaft it follows
|
|
28
47
|
const set = {};
|
|
29
|
-
for (const e of frame.feet) {
|
|
48
|
+
for (const e of frame.feet) {
|
|
49
|
+
const l = s.gait.legs.find((x) => x.name === e.leg);
|
|
50
|
+
l.chain.names.forEach((n, i) => {
|
|
51
|
+
let j = s.motion.joints.find((x) => x.id === n),
|
|
52
|
+
v = e.q[i];
|
|
53
|
+
while (j.follow) {
|
|
54
|
+
const [lead, r = 1, o = 0] = j.follow;
|
|
55
|
+
v = (v - o) / r;
|
|
56
|
+
j = s.motion.joints.find((x) => x.id === lead);
|
|
57
|
+
}
|
|
58
|
+
set[j.id] = [v];
|
|
59
|
+
});
|
|
60
|
+
}
|
|
30
61
|
return jointValues(s.motion, set);
|
|
31
62
|
};
|
|
32
63
|
export const framesOf = (s) => (s.gaitRun ??= gait(s.gait));
|
|
33
64
|
|
|
34
|
-
export function gaitChecks(s, f, { jointValues,
|
|
65
|
+
export function gaitChecks(s, f, { jointValues, sweepOne }) {
|
|
35
66
|
if (!s.gait) return;
|
|
36
67
|
const r = framesOf(s);
|
|
37
|
-
for (const x of r.findings)
|
|
68
|
+
for (const x of r.findings)
|
|
69
|
+
f(x.level, `gait: ${x.text}`, x.level === 'pass' || x.level === 'info' ? null : 'gait: fails', {
|
|
70
|
+
code: x.code,
|
|
71
|
+
values: x.values,
|
|
72
|
+
refs: x.refs,
|
|
73
|
+
});
|
|
38
74
|
if (!r.summary?.torque) return;
|
|
39
|
-
const lift = r.summary.clearance,
|
|
40
|
-
|
|
75
|
+
const lift = r.summary.clearance,
|
|
76
|
+
min = s.a.gait.clearance != null ? s.num(s.a.gait.clearance) : s.gait.lift * 0.5;
|
|
77
|
+
f(
|
|
78
|
+
lift >= min ? 'pass' : 'fail',
|
|
79
|
+
`gait: the swing foot clears the ground by ${n3(lift, 1)} mm (wanted ${n3(min, 1)})`,
|
|
80
|
+
lift >= min ? null : 'gait: foot drags',
|
|
81
|
+
{ code: 'gait.clearance', values: { lift, min } },
|
|
82
|
+
);
|
|
41
83
|
// Each driven joint's torque through the cycle, against its motor
|
|
42
84
|
const motion = s.motion;
|
|
43
85
|
for (const [id, t] of Object.entries(r.summary.torque)) {
|
|
44
|
-
const j = motion.joints.find((x) => x.id === id),
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
86
|
+
const j = motion.joints.find((x) => x.id === id),
|
|
87
|
+
tr = j && trainFrom(motion, id),
|
|
88
|
+
d = tr && motion.drives.find((x) => x.joint === tr.root);
|
|
89
|
+
if (!d) {
|
|
90
|
+
f(
|
|
91
|
+
'warn',
|
|
92
|
+
`gait: nothing drives ${id}, so its ${n3(t, 3)} N m through the cycle isn't checked`,
|
|
93
|
+
`gait: ${id} undriven`,
|
|
94
|
+
{ code: 'gait.torque', refs: { joint: id } },
|
|
95
|
+
);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const speed = workingSpeed(d.model, d.spec, d.supply),
|
|
99
|
+
avail =
|
|
100
|
+
atCurrent(d.model, d.spec, performance(d.model, { speed, supply: d.supply }).motor.torque) *
|
|
101
|
+
tr.ratio *
|
|
102
|
+
tr.efficiency,
|
|
103
|
+
margin = avail / t,
|
|
104
|
+
level = margin < 1 ? 'fail' : margin < 2 ? 'warn' : 'pass';
|
|
105
|
+
f(
|
|
106
|
+
level,
|
|
107
|
+
`gait: ${id} needs ${n3(t, 3)} N m at its worst in the cycle, and ${d.id} gives ${n3(avail, 3)} N m there, ${n3(margin, 1)} times over`,
|
|
108
|
+
level === 'pass' ? null : `gait: ${id} ${n3(margin, 1)}×`,
|
|
109
|
+
{ code: 'motor.torque', refs: { joint: id, item: d.id }, values: { needed: t, available: avail, margin } },
|
|
110
|
+
);
|
|
48
111
|
}
|
|
49
112
|
// Leg against leg, leg against body: the cycle as a sweep
|
|
50
|
-
const every = Math.max(1, Math.floor(r.frames.length / 12));
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
113
|
+
const every = Math.max(1, Math.floor(r.frames.length / 12));
|
|
114
|
+
let clashes = null,
|
|
115
|
+
first = null;
|
|
116
|
+
for (const [k, frame] of r.frames.entries()) {
|
|
117
|
+
if (k % every) continue;
|
|
118
|
+
const c = sweepOne(frameValues(s, frame, jointValues));
|
|
119
|
+
if (c.length && !clashes) {
|
|
120
|
+
clashes = c;
|
|
121
|
+
first = frame.t;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (clashes)
|
|
125
|
+
f(
|
|
126
|
+
'fail',
|
|
127
|
+
`gait: at ${n3(first * 100, 0)}% of the cycle ${clashes
|
|
128
|
+
.slice(0, 3)
|
|
129
|
+
.map(([a, b]) => `${a} hits ${b}`)
|
|
130
|
+
.join(', ')}`,
|
|
131
|
+
'gait: clash',
|
|
132
|
+
{ code: 'gait.sweep', refs: { at: first } },
|
|
133
|
+
);
|
|
134
|
+
else
|
|
135
|
+
f('pass', `gait: nothing collides through the cycle (${Math.ceil(r.frames.length / every)} poses of it)`, null, {
|
|
136
|
+
code: 'gait.sweep',
|
|
137
|
+
});
|
|
54
138
|
}
|
|
55
139
|
|
|
56
140
|
// A gait as joint values through its cycle, for the viewer's Motion panel and cad animate: the joints that lead
|
|
57
141
|
// (a motor's shaft in place of a gearbox's output), and their values at each frame
|
|
58
142
|
export function gaitSequence(s) {
|
|
59
143
|
if (!s.gait) return null;
|
|
60
|
-
const r = framesOf(s);
|
|
61
|
-
|
|
144
|
+
const r = framesOf(s);
|
|
145
|
+
if (!r.frames.length) return null;
|
|
146
|
+
const leads = (id) => {
|
|
147
|
+
let j = s.motion.joints.find((x) => x.id === id);
|
|
148
|
+
while (j.follow) j = s.motion.joints.find((x) => x.id === j.follow[0]);
|
|
149
|
+
return j.id;
|
|
150
|
+
};
|
|
62
151
|
const names = [...new Set(s.gait.legs.flatMap((l) => l.chain.names).map(leads))];
|
|
63
|
-
const toLead = (id, q) => {
|
|
152
|
+
const toLead = (id, q) => {
|
|
153
|
+
let j = s.motion.joints.find((x) => x.id === id),
|
|
154
|
+
v = q;
|
|
155
|
+
while (j.follow) {
|
|
156
|
+
const [lead, ratio = 1, offset = 0] = j.follow;
|
|
157
|
+
v = (v - offset) / ratio;
|
|
158
|
+
j = s.motion.joints.find((x) => x.id === lead);
|
|
159
|
+
}
|
|
160
|
+
return v;
|
|
161
|
+
};
|
|
64
162
|
const round = (x) => Number(x.toFixed(4));
|
|
65
|
-
return {
|
|
163
|
+
return {
|
|
164
|
+
kind: 'gait',
|
|
165
|
+
period: s.gait.period,
|
|
166
|
+
names,
|
|
167
|
+
q: r.frames.map((f) =>
|
|
168
|
+
names.map((n) => {
|
|
169
|
+
for (const e of f.feet) {
|
|
170
|
+
const l = s.gait.legs.find((x) => x.name === e.leg);
|
|
171
|
+
const i = l.chain.names.findIndex((m) => leads(m) === n);
|
|
172
|
+
if (i >= 0) return round(toLead(l.chain.names[i], e.q[i]));
|
|
173
|
+
}
|
|
174
|
+
return 0;
|
|
175
|
+
}),
|
|
176
|
+
),
|
|
177
|
+
};
|
|
66
178
|
}
|
package/lib/grip.mjs
CHANGED
|
@@ -5,19 +5,68 @@ import { leafModel } from '@rulecad/flexures';
|
|
|
5
5
|
import { peekRuleSet } from './rules.mjs';
|
|
6
6
|
import { materialOf } from './list.mjs';
|
|
7
7
|
|
|
8
|
-
const G = 9.80665,
|
|
8
|
+
const G = 9.80665,
|
|
9
|
+
n3 = (x, d = 2) => String(Number(x.toFixed(d)));
|
|
9
10
|
|
|
10
11
|
export function gripChecks(s, f) {
|
|
11
|
-
const g = s.a.task?.grip;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
const g = s.a.task?.grip;
|
|
13
|
+
if (!g) return;
|
|
14
|
+
const code = 'tool.grip',
|
|
15
|
+
d = s.motion.drives.find((x) => x.id === g.servo),
|
|
16
|
+
payload = s.task.payload ?? 0;
|
|
17
|
+
if (!d)
|
|
18
|
+
throw new Error(
|
|
19
|
+
`task.grip: servo "${g.servo}" isn't a motor item (${s.motion.drives.map((x) => x.id).join(', ') || 'none'})`,
|
|
20
|
+
);
|
|
21
|
+
if (d.model.type !== 'servo')
|
|
22
|
+
throw new Error(`task.grip: ${g.servo} is a ${d.model.type}: the grip is worked out for a servo through its horn`);
|
|
23
|
+
const name = String(g.material ?? materialOf(s.project).name).toLowerCase(),
|
|
24
|
+
rules = peekRuleSet(name).rules,
|
|
25
|
+
E = rules.modulus.value,
|
|
26
|
+
strength = rules.strength.value;
|
|
27
|
+
const horn = s.num(g.horn),
|
|
28
|
+
travel = s.num(g.travel),
|
|
29
|
+
lever = g.lever != null ? s.num(g.lever) : 1,
|
|
30
|
+
mu = s.num(g.friction ?? 0.4),
|
|
31
|
+
L = { length: s.num(g.leaf.length), thickness: s.num(g.leaf.thickness), width: s.num(g.leaf.width) },
|
|
32
|
+
m = leafModel(L, { E });
|
|
33
|
+
const torque = d.model.figures.stall_torque.value * (g.torque_share != null ? s.num(g.torque_share) : 0.6),
|
|
34
|
+
pull = ((torque * 1000) / horn / 2) * lever,
|
|
35
|
+
spring = m.stiffness * travel,
|
|
36
|
+
grip = pull - spring,
|
|
37
|
+
accel = (s.task.accel ?? 0) / 1000,
|
|
38
|
+
need = payload * (G + accel);
|
|
39
|
+
const held = 2 * mu * grip,
|
|
40
|
+
margin = need > 0 ? held / need : Infinity,
|
|
41
|
+
want = g.margin != null ? s.num(g.margin) : 2,
|
|
42
|
+
stress = m.stress(travel),
|
|
43
|
+
strain = stress / strength,
|
|
44
|
+
values = { grip, pull, spring, held, need, margin, stress };
|
|
45
|
+
if (grip <= 0)
|
|
46
|
+
f(
|
|
47
|
+
'fail',
|
|
48
|
+
`the grip: the servo pulls each finger with ${n3(pull)} N and the leaves take ${n3(spring)} N to close: it can't close them. A longer or thinner leaf`,
|
|
49
|
+
'grip: too stiff',
|
|
50
|
+
{ code, values },
|
|
51
|
+
);
|
|
52
|
+
else if (margin < 1)
|
|
53
|
+
f(
|
|
54
|
+
'fail',
|
|
55
|
+
`the grip: ${n3(grip)} N a finger holds ${n3(held)} N at μ ${mu}, and the payload needs ${n3(need)} N: it drops`,
|
|
56
|
+
'grip: drops',
|
|
57
|
+
{ code, values },
|
|
58
|
+
);
|
|
59
|
+
else
|
|
60
|
+
f(
|
|
61
|
+
margin < want ? 'warn' : 'pass',
|
|
62
|
+
`the grip: ${n3(grip)} N a finger (${n3(pull)} N from the servo at ${n3(torque, 3)} N m, less ${n3(spring)} N to bend the leaves) holds ${n3(held)} N at μ ${mu}, against ${n3(need)} N for ${n3(payload * 1000, 0)} g: ${n3(margin, 1)} times over`,
|
|
63
|
+
margin < want ? `grip: ${n3(margin, 1)}×` : null,
|
|
64
|
+
{ code, values },
|
|
65
|
+
);
|
|
66
|
+
f(
|
|
67
|
+
strain > 1 ? 'fail' : strain > 0.6 ? 'warn' : 'pass',
|
|
68
|
+
`the fingers at full close: ${n3(stress, 1)} MPa in a ${name.toUpperCase()} leaf, ${n3(strain * 100, 0)}% of its ${strength} MPa`,
|
|
69
|
+
strain > 0.6 ? `finger ${n3(strain * 100, 0)}% strength` : null,
|
|
70
|
+
{ code: 'tool.strain', values: { stress, strength } },
|
|
71
|
+
);
|
|
23
72
|
}
|