BlueGit

timfa timdraw

feat: rode stip over de geplande lijn laten lopen

De stip sprong met elke statusmeting. Hij volgt nu de lijn die de pen hoort te tekenen, zodat een hoek een hoek blijft.

timfa Tim Falken · pushed by timfa

Authored 2026-09-29 · 7 weeks ago

Hash
c6722395aa4f0d4def39f3096eefc6bccab77fa5
Parent
4a0b37af
4 files changed +270 −7
static/app.js +158 −7
@@ -23,6 +23,16 @@ let textTimer = 0;
23 23 let userDisconnected = false;
24 24 let autoTried = "";
25 25 let pen = null;
26 let penJob = false;
27 let penRoute = null;
28 let penGuide = null;
29 let penAlong = 0;
30 let penGoal = 0;
31 let penHint = 0;
32 let penSpeed = 3000;
33 let penFrame = 0;
34 let penStamp = 0;
35 let penDraw = null;
26 36 const PORT_KEY = "timdraw-port";
27 37
28 38 const defaults = {
@@ -373,6 +383,114 @@ function drawPen ()
373 383 ctx.restore();
374 384 }
375 385
386 function homePage ()
387 {
388 const home = pageFromMachine(0, 0);
389 return [home.x, home.y];
390 }
391
392 function showPen (page, speed)
393 {
394 penSpeed = speed;
395 const hit = penRoute && penProject(penRoute, page.x, page.y, penHint);
396 if (hit && hit.err <= 12)
397 {
398 if (penGuide !== penRoute)
399 {
400 const back = pen ? penProject(penRoute, pen.x, pen.y, penHint) : hit;
401 penAlong = back.err <= 12 ? back.along : hit.along;
402 penGuide = penRoute;
403 }
404 penGoal = hit.along;
405 penHint = hit.along;
406 return;
407 }
408 const from = pen || page;
409 const length = Math.hypot(page.x - from.x, page.y - from.y);
410 penGuide = { pts: [[from.x, from.y], [page.x, page.y]], cum: [0, length] };
411 penAlong = 0;
412 penGoal = length;
413 }
414
415 function tickPen (now)
416 {
417 if (!penJob)
418 {
419 penFrame = 0;
420 return;
421 }
422 penFrame = requestAnimationFrame(tickPen);
423 if (!penGuide) return;
424 const dt = penStamp ? Math.min(0.05, (now - penStamp) / 1000) : 0;
425 penStamp = now;
426 const next = penStep(penAlong, penGoal, penSpeed, dt);
427 if (Math.abs(next - penAlong) < 1e-4) return;
428 penAlong = next;
429 const point = penAt(penGuide, penAlong);
430 pen = { x: point[0], y: point[1] };
431 if (!drag) draw();
432 }
433
434 function startPenJob (page, speed)
435 {
436 penJob = true;
437 penRoute = penRouteFrom(penDraw || (preview && preview.draw), homePage());
438 penGuide = penRoute;
439 penSpeed = speed;
440 if (penRoute)
441 {
442 const hit = penProject(penRoute, page.x, page.y, 0);
443 if (hit.err <= 12)
444 {
445 penAlong = hit.along;
446 penGoal = hit.along;
447 penHint = hit.along;
448 const point = penAt(penRoute, penAlong);
449 pen = { x: point[0], y: point[1] };
450 }
451 else
452 {
453 penGuide = null;
454 pen = page;
455 penAlong = 0;
456 penGoal = 0;
457 penHint = 0;
458 }
459 }
460 else
461 {
462 pen = page;
463 penAlong = 0;
464 penGoal = 0;
465 penHint = 0;
466 }
467 if (!penFrame)
468 {
469 penStamp = 0;
470 penFrame = requestAnimationFrame(tickPen);
471 }
472 }
473
474 function stopPenJob ()
475 {
476 const was = penJob || pen;
477 penJob = false;
478 pen = null;
479 penRoute = null;
480 penGuide = null;
481 penDraw = null;
482 penAlong = 0;
483 penGoal = 0;
484 penHint = 0;
485 if (penFrame)
486 {
487 cancelAnimationFrame(penFrame);
488 penFrame = 0;
489 penStamp = 0;
490 }
491 if (was && !drag) draw();
492 }
493
376 494 function draw ()
377 495 {
378 496 const view = layout();
@@ -981,8 +1099,31 @@ $("btn-pen-down").onclick = () => machineCall("/api/pen", { down: true, ...readS
981 1099 $("btn-unlock").onclick = () => machineCall("/api/unlock");
982 1100 $("btn-home").onclick = () => machineCall("/api/home", {}, "Homen beweegt naar de eindschakelaars. Doorgaan?");
983 1101 $("btn-release").onclick = () => machineCall("/api/release");
984 $("btn-plot").onclick = () => machineCall("/api/plot", { objects, settings: readSettings() });
985 $("btn-trace").onclick = () => machineCall("/api/trace", { objects, settings: readSettings() });
1102 async function rememberPath (trace)
1103 {
1104 const settings = readSettings();
1105 if (trace) settings.trace = true;
1106 try
1107 {
1108 const planned = await api("/api/preview", { objects, settings });
1109 penDraw = planned.draw || [];
1110 }
1111 catch
1112 {
1113 penDraw = (preview && preview.draw) || [];
1114 }
1115 }
1116
1117 $("btn-plot").onclick = async () =>
1118 {
1119 await rememberPath(false);
1120 machineCall("/api/plot", { objects, settings: readSettings() });
1121 };
1122 $("btn-trace").onclick = async () =>
1123 {
1124 await rememberPath(true);
1125 machineCall("/api/trace", { objects, settings: readSettings() });
1126 };
986 1127 $("btn-pause").onclick = async () =>
987 1128 {
988 1129 const path = $("btn-pause").dataset.paused === "1" ? "/api/resume" : "/api/pause";
@@ -1086,11 +1227,21 @@ function paintStatus (status)
1086 1227 }
1087 1228 const progress = status.jobActive ? (status.progress || 0) : (status.progress === 1 ? 1 : 0);
1088 1229 $("bar").style.width = `${Math.round((progress || 0) * 100)}%`;
1089 const nextPen = status.jobActive && status.wpos ? pageFromMachine(status.wpos[0], status.wpos[1]) : null;
1090 const penSame = (!pen && !nextPen)
1091 || (pen && nextPen && Math.abs(pen.x - nextPen.x) < 0.05 && Math.abs(pen.y - nextPen.y) < 0.05);
1092 pen = nextPen;
1093 if (!penSame && !drag) draw();
1230 if (status.jobActive && status.wpos)
1231 {
1232 const page = pageFromMachine(status.wpos[0], status.wpos[1]);
1233 const down = Number($("pen-down").value);
1234 const up = Number($("pen-up").value);
1235 const drawing = Math.abs(status.wpos[2] - down) <= Math.abs(status.wpos[2] - up);
1236 const speed = drawing ? Number($("draw-speed").value) : Number($("travel-speed").value);
1237 if (!penJob) startPenJob(page, speed);
1238 else showPen(page, speed);
1239 if (!drag) draw();
1240 }
1241 else if (penJob || pen)
1242 {
1243 stopPenJob();
1244 }
1094 1245 if (status.message && status.jobActive) toast(status.message);
1095 1246 }
1096 1247
@@ -23,6 +23,16 @@ let textTimer = 0;
23let userDisconnected = false;
23let userDisconnected = false;
24let autoTried = "";
24let autoTried = "";
25let pen = null;
25let pen = null;
26let penJob = false;
27let penRoute = null;
28let penGuide = null;
29let penAlong = 0;
30let penGoal = 0;
31let penHint = 0;
32let penSpeed = 3000;
33let penFrame = 0;
34let penStamp = 0;
35let penDraw = null;
26const PORT_KEY = "timdraw-port";
36const PORT_KEY = "timdraw-port";
27
37
28const defaults = {
38const defaults = {
@@ -373,6 +383,114 @@ function drawPen ()
373 ctx.restore();
383 ctx.restore();
374}
384}
375
385
386function homePage ()
387{
388 const home = pageFromMachine(0, 0);
389 return [home.x, home.y];
390}
391
392function showPen (page, speed)
393{
394 penSpeed = speed;
395 const hit = penRoute && penProject(penRoute, page.x, page.y, penHint);
396 if (hit && hit.err <= 12)
397 {
398 if (penGuide !== penRoute)
399 {
400 const back = pen ? penProject(penRoute, pen.x, pen.y, penHint) : hit;
401 penAlong = back.err <= 12 ? back.along : hit.along;
402 penGuide = penRoute;
403 }
404 penGoal = hit.along;
405 penHint = hit.along;
406 return;
407 }
408 const from = pen || page;
409 const length = Math.hypot(page.x - from.x, page.y - from.y);
410 penGuide = { pts: [[from.x, from.y], [page.x, page.y]], cum: [0, length] };
411 penAlong = 0;
412 penGoal = length;
413}
414
415function tickPen (now)
416{
417 if (!penJob)
418 {
419 penFrame = 0;
420 return;
421 }
422 penFrame = requestAnimationFrame(tickPen);
423 if (!penGuide) return;
424 const dt = penStamp ? Math.min(0.05, (now - penStamp) / 1000) : 0;
425 penStamp = now;
426 const next = penStep(penAlong, penGoal, penSpeed, dt);
427 if (Math.abs(next - penAlong) < 1e-4) return;
428 penAlong = next;
429 const point = penAt(penGuide, penAlong);
430 pen = { x: point[0], y: point[1] };
431 if (!drag) draw();
432}
433
434function startPenJob (page, speed)
435{
436 penJob = true;
437 penRoute = penRouteFrom(penDraw || (preview && preview.draw), homePage());
438 penGuide = penRoute;
439 penSpeed = speed;
440 if (penRoute)
441 {
442 const hit = penProject(penRoute, page.x, page.y, 0);
443 if (hit.err <= 12)
444 {
445 penAlong = hit.along;
446 penGoal = hit.along;
447 penHint = hit.along;
448 const point = penAt(penRoute, penAlong);
449 pen = { x: point[0], y: point[1] };
450 }
451 else
452 {
453 penGuide = null;
454 pen = page;
455 penAlong = 0;
456 penGoal = 0;
457 penHint = 0;
458 }
459 }
460 else
461 {
462 pen = page;
463 penAlong = 0;
464 penGoal = 0;
465 penHint = 0;
466 }
467 if (!penFrame)
468 {
469 penStamp = 0;
470 penFrame = requestAnimationFrame(tickPen);
471 }
472}
473
474function stopPenJob ()
475{
476 const was = penJob || pen;
477 penJob = false;
478 pen = null;
479 penRoute = null;
480 penGuide = null;
481 penDraw = null;
482 penAlong = 0;
483 penGoal = 0;
484 penHint = 0;
485 if (penFrame)
486 {
487 cancelAnimationFrame(penFrame);
488 penFrame = 0;
489 penStamp = 0;
490 }
491 if (was && !drag) draw();
492}
493
376function draw ()
494function draw ()
377{
495{
378 const view = layout();
496 const view = layout();
@@ -981,8 +1099,31 @@ $("btn-pen-down").onclick = () => machineCall("/api/pen", { down: true, ...readS
981$("btn-unlock").onclick = () => machineCall("/api/unlock");
1099$("btn-unlock").onclick = () => machineCall("/api/unlock");
982$("btn-home").onclick = () => machineCall("/api/home", {}, "Homen beweegt naar de eindschakelaars. Doorgaan?");
1100$("btn-home").onclick = () => machineCall("/api/home", {}, "Homen beweegt naar de eindschakelaars. Doorgaan?");
983$("btn-release").onclick = () => machineCall("/api/release");
1101$("btn-release").onclick = () => machineCall("/api/release");
984$("btn-plot").onclick = () => machineCall("/api/plot", { objects, settings: readSettings() });
1102async function rememberPath (trace)
985$("btn-trace").onclick = () => machineCall("/api/trace", { objects, settings: readSettings() });
1103{
1104 const settings = readSettings();
1105 if (trace) settings.trace = true;
1106 try
1107 {
1108 const planned = await api("/api/preview", { objects, settings });
1109 penDraw = planned.draw || [];
1110 }
1111 catch
1112 {
1113 penDraw = (preview && preview.draw) || [];
1114 }
1115}
1116
1117$("btn-plot").onclick = async () =>
1118{
1119 await rememberPath(false);
1120 machineCall("/api/plot", { objects, settings: readSettings() });
1121};
1122$("btn-trace").onclick = async () =>
1123{
1124 await rememberPath(true);
1125 machineCall("/api/trace", { objects, settings: readSettings() });
1126};
986$("btn-pause").onclick = async () =>
1127$("btn-pause").onclick = async () =>
987{
1128{
988 const path = $("btn-pause").dataset.paused === "1" ? "/api/resume" : "/api/pause";
1129 const path = $("btn-pause").dataset.paused === "1" ? "/api/resume" : "/api/pause";
@@ -1086,11 +1227,21 @@ function paintStatus (status)
1086 }
1227 }
1087 const progress = status.jobActive ? (status.progress || 0) : (status.progress === 1 ? 1 : 0);
1228 const progress = status.jobActive ? (status.progress || 0) : (status.progress === 1 ? 1 : 0);
1088 $("bar").style.width = `${Math.round((progress || 0) * 100)}%`;
1229 $("bar").style.width = `${Math.round((progress || 0) * 100)}%`;
1089 const nextPen = status.jobActive && status.wpos ? pageFromMachine(status.wpos[0], status.wpos[1]) : null;
1230 if (status.jobActive && status.wpos)
1090 const penSame = (!pen && !nextPen)
1231 {
1091 || (pen && nextPen && Math.abs(pen.x - nextPen.x) < 0.05 && Math.abs(pen.y - nextPen.y) < 0.05);
1232 const page = pageFromMachine(status.wpos[0], status.wpos[1]);
1092 pen = nextPen;
1233 const down = Number($("pen-down").value);
1093 if (!penSame && !drag) draw();
1234 const up = Number($("pen-up").value);
1235 const drawing = Math.abs(status.wpos[2] - down) <= Math.abs(status.wpos[2] - up);
1236 const speed = drawing ? Number($("draw-speed").value) : Number($("travel-speed").value);
1237 if (!penJob) startPenJob(page, speed);
1238 else showPen(page, speed);
1239 if (!drag) draw();
1240 }
1241 else if (penJob || pen)
1242 {
1243 stopPenJob();
1244 }
1094 if (status.message && status.jobActive) toast(status.message);
1245 if (status.message && status.jobActive) toast(status.message);
1095}
1246}
1096
1247
static/index.html +1 −0
@@ -206,6 +206,7 @@
206 206 <button id="connect-close" type="button">Sluiten</button>
207 207 </div>
208 208 </dialog>
209 <script src="/static/penpath.js"></script>
209 210 <script src="/static/app.js"></script>
210 211 </body>
211 212
@@ -206,6 +206,7 @@
206 <button id="connect-close" type="button">Sluiten</button>
206 <button id="connect-close" type="button">Sluiten</button>
207 </div>
207 </div>
208 </dialog>
208 </dialog>
209 <script src="/static/penpath.js"></script>
209 <script src="/static/app.js"></script>
210 <script src="/static/app.js"></script>
210</body>
211</body>
211
212
static/penpath.js added +84 −0
@@ -0,0 +1,84 @@
1 // De rode stip loopt over de geplande lijn, niet in een sprong tussen twee metingen.
2
3 function penRouteFrom (strokes, home)
4 {
5 const pts = [];
6 const push = (x, y) =>
7 {
8 const last = pts[pts.length - 1];
9 if (last && Math.hypot(last[0] - x, last[1] - y) < 0.05) return;
10 pts.push([x, y]);
11 };
12 if (home) push(home[0], home[1]);
13 for (const stroke of strokes || [])
14 {
15 for (const point of stroke || []) push(point[0], point[1]);
16 }
17 if (home) push(home[0], home[1]);
18 if (pts.length < 2) return null;
19 const cum = [0];
20 for (let i = 1; i < pts.length; i++)
21 {
22 cum.push(cum[i - 1] + Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]));
23 }
24 return { pts, cum };
25 }
26
27 function penProject (route, x, y, hint)
28 {
29 const pts = route.pts;
30 const cum = route.cum;
31 let best = { along: 0, err: Infinity };
32 let ahead = null;
33 for (let i = 0; i < pts.length - 1; i++)
34 {
35 const ax = pts[i][0];
36 const ay = pts[i][1];
37 const bx = pts[i + 1][0];
38 const by = pts[i + 1][1];
39 const dx = bx - ax;
40 const dy = by - ay;
41 const len2 = dx * dx + dy * dy;
42 let t = len2 < 1e-9 ? 0 : ((x - ax) * dx + (y - ay) * dy) / len2;
43 if (t < 0) t = 0;
44 else if (t > 1) t = 1;
45 const px = ax + dx * t;
46 const py = ay + dy * t;
47 const err = Math.hypot(x - px, y - py);
48 const along = cum[i] + Math.hypot(px - ax, py - ay);
49 if (err < best.err) best = { along, err };
50 if (along + 1e-6 >= hint - 1 && (!ahead || err < ahead.err)) ahead = { along, err };
51 }
52 if (ahead && ahead.err <= Math.max(2.5, best.err + 0.4)) return ahead;
53 return best;
54 }
55
56 function penAt (route, along)
57 {
58 const pts = route.pts;
59 const cum = route.cum;
60 const total = cum[cum.length - 1];
61 const dist = Math.max(0, Math.min(total, along));
62 let lo = 1;
63 let hi = cum.length - 1;
64 while (lo < hi)
65 {
66 const mid = (lo + hi) >> 1;
67 if (cum[mid] < dist) lo = mid + 1;
68 else hi = mid;
69 }
70 const span = cum[lo] - cum[lo - 1];
71 const t = span > 1e-6 ? (dist - cum[lo - 1]) / span : 0;
72 return [
73 pts[lo - 1][0] + (pts[lo][0] - pts[lo - 1][0]) * t,
74 pts[lo - 1][1] + (pts[lo][1] - pts[lo - 1][1]) * t,
75 ];
76 }
77
78 function penStep (along, goal, mmPerMin, dt)
79 {
80 const gap = goal - along;
81 if (Math.abs(gap) < 0.02) return goal;
82 const step = Math.min(Math.abs(gap), Math.max(1, mmPerMin) / 60 * dt);
83 return along + Math.sign(gap) * step;
84 }
@@ -0,0 +1,84 @@
1// De rode stip loopt over de geplande lijn, niet in een sprong tussen twee metingen.
2
3function penRouteFrom (strokes, home)
4{
5 const pts = [];
6 const push = (x, y) =>
7 {
8 const last = pts[pts.length - 1];
9 if (last && Math.hypot(last[0] - x, last[1] - y) < 0.05) return;
10 pts.push([x, y]);
11 };
12 if (home) push(home[0], home[1]);
13 for (const stroke of strokes || [])
14 {
15 for (const point of stroke || []) push(point[0], point[1]);
16 }
17 if (home) push(home[0], home[1]);
18 if (pts.length < 2) return null;
19 const cum = [0];
20 for (let i = 1; i < pts.length; i++)
21 {
22 cum.push(cum[i - 1] + Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]));
23 }
24 return { pts, cum };
25}
26
27function penProject (route, x, y, hint)
28{
29 const pts = route.pts;
30 const cum = route.cum;
31 let best = { along: 0, err: Infinity };
32 let ahead = null;
33 for (let i = 0; i < pts.length - 1; i++)
34 {
35 const ax = pts[i][0];
36 const ay = pts[i][1];
37 const bx = pts[i + 1][0];
38 const by = pts[i + 1][1];
39 const dx = bx - ax;
40 const dy = by - ay;
41 const len2 = dx * dx + dy * dy;
42 let t = len2 < 1e-9 ? 0 : ((x - ax) * dx + (y - ay) * dy) / len2;
43 if (t < 0) t = 0;
44 else if (t > 1) t = 1;
45 const px = ax + dx * t;
46 const py = ay + dy * t;
47 const err = Math.hypot(x - px, y - py);
48 const along = cum[i] + Math.hypot(px - ax, py - ay);
49 if (err < best.err) best = { along, err };
50 if (along + 1e-6 >= hint - 1 && (!ahead || err < ahead.err)) ahead = { along, err };
51 }
52 if (ahead && ahead.err <= Math.max(2.5, best.err + 0.4)) return ahead;
53 return best;
54}
55
56function penAt (route, along)
57{
58 const pts = route.pts;
59 const cum = route.cum;
60 const total = cum[cum.length - 1];
61 const dist = Math.max(0, Math.min(total, along));
62 let lo = 1;
63 let hi = cum.length - 1;
64 while (lo < hi)
65 {
66 const mid = (lo + hi) >> 1;
67 if (cum[mid] < dist) lo = mid + 1;
68 else hi = mid;
69 }
70 const span = cum[lo] - cum[lo - 1];
71 const t = span > 1e-6 ? (dist - cum[lo - 1]) / span : 0;
72 return [
73 pts[lo - 1][0] + (pts[lo][0] - pts[lo - 1][0]) * t,
74 pts[lo - 1][1] + (pts[lo][1] - pts[lo - 1][1]) * t,
75 ];
76}
77
78function penStep (along, goal, mmPerMin, dt)
79{
80 const gap = goal - along;
81 if (Math.abs(gap) < 0.02) return goal;
82 const step = Math.min(Math.abs(gap), Math.max(1, mmPerMin) / 60 * dt);
83 return along + Math.sign(gap) * step;
84}
timdraw/selftest.py +27 −0
@@ -6,6 +6,7 @@ Draaien: python3 -m timdraw.selftest
6 6
7 7 from pathlib import Path
8 8 import math
9 import subprocess
9 10 import time
10 11
11 12 from timdraw.font import render_text
@@ -187,6 +188,31 @@ def _near(points, x: float, y: float) -> bool:
187 188 return any(abs(px - x) < 0.02 and abs(py - y) < 0.02 for px, py in points)
188 189
189 190
191 def test_pen_lerp() -> None:
192 """De stip volgt de geplande lijn, ook om een hoek, in plaats van de koorde."""
193 script = r"""
194 const fs = require("fs");
195 const vm = require("vm");
196 const context = { console };
197 vm.runInNewContext(fs.readFileSync("static/penpath.js", "utf8"), context);
198 const home = [10, 307];
199 const route = context.penRouteFrom([[[60, 307], [60, 257]]], home);
200 const corner = context.penAt(route, 50);
201 if (Math.hypot(corner[0] - 60, corner[1] - 307) > 0.02) throw new Error("hoek " + corner);
202 const leg = context.penAt(route, 75);
203 if (Math.hypot(leg[0] - 60, leg[1] - 282) > 0.02) throw new Error("been " + leg);
204 const hit = context.penProject(route, 60, 282, 0);
205 if (Math.abs(hit.along - 75) > 0.05) throw new Error("project " + JSON.stringify(hit));
206 const back = context.penProject(route, 10, 307, 100);
207 const end = route.cum[route.cum.length - 1];
208 if (Math.abs(back.along - end) > 0.05) throw new Error("terug " + back.along);
209 const step = context.penStep(0, 100, 3000, 0.2);
210 if (Math.abs(step - 10) > 0.02) throw new Error("stap " + step);
211 """
212 run = subprocess.run(["node", "-e", script], cwd=Path(__file__).resolve().parents[1], capture_output=True, text=True)
213 check("stip loopt over de hoek", run.returncode == 0, (run.stderr or run.stdout).strip())
214
215
190 216 def test_virtual_machine() -> None:
191 217 """Jog, papier en error 33 tegen een GRBL die MPos stuurt en jog te vroeg bevestigt."""
192 218 machine, grbl = connect(40, -20, wco=(5, -3, 0))
@@ -285,5 +311,6 @@ if __name__ == "__main__":
285 311 test_svg("Dolphin.svg", True)
286 312 test_svg("wiresphere.svg", False)
287 313 test_text_and_gcode()
314 test_pen_lerp()
288 315 test_virtual_machine()
289 316 print("alle controles geslaagd")
@@ -6,6 +6,7 @@ Draaien: python3 -m timdraw.selftest
6
6
7from pathlib import Path
7from pathlib import Path
8import math
8import math
9import subprocess
9import time
10import time
10
11
11from timdraw.font import render_text
12from timdraw.font import render_text
@@ -187,6 +188,31 @@ def _near(points, x: float, y: float) -> bool:
187 return any(abs(px - x) < 0.02 and abs(py - y) < 0.02 for px, py in points)
188 return any(abs(px - x) < 0.02 and abs(py - y) < 0.02 for px, py in points)
188
189
189
190
191def test_pen_lerp() -> None:
192 """De stip volgt de geplande lijn, ook om een hoek, in plaats van de koorde."""
193 script = r"""
194const fs = require("fs");
195const vm = require("vm");
196const context = { console };
197vm.runInNewContext(fs.readFileSync("static/penpath.js", "utf8"), context);
198const home = [10, 307];
199const route = context.penRouteFrom([[[60, 307], [60, 257]]], home);
200const corner = context.penAt(route, 50);
201if (Math.hypot(corner[0] - 60, corner[1] - 307) > 0.02) throw new Error("hoek " + corner);
202const leg = context.penAt(route, 75);
203if (Math.hypot(leg[0] - 60, leg[1] - 282) > 0.02) throw new Error("been " + leg);
204const hit = context.penProject(route, 60, 282, 0);
205if (Math.abs(hit.along - 75) > 0.05) throw new Error("project " + JSON.stringify(hit));
206const back = context.penProject(route, 10, 307, 100);
207const end = route.cum[route.cum.length - 1];
208if (Math.abs(back.along - end) > 0.05) throw new Error("terug " + back.along);
209const step = context.penStep(0, 100, 3000, 0.2);
210if (Math.abs(step - 10) > 0.02) throw new Error("stap " + step);
211"""
212 run = subprocess.run(["node", "-e", script], cwd=Path(__file__).resolve().parents[1], capture_output=True, text=True)
213 check("stip loopt over de hoek", run.returncode == 0, (run.stderr or run.stdout).strip())
214
215
190def test_virtual_machine() -> None:
216def test_virtual_machine() -> None:
191 """Jog, papier en error 33 tegen een GRBL die MPos stuurt en jog te vroeg bevestigt."""
217 """Jog, papier en error 33 tegen een GRBL die MPos stuurt en jog te vroeg bevestigt."""
192 machine, grbl = connect(40, -20, wco=(5, -3, 0))
218 machine, grbl = connect(40, -20, wco=(5, -3, 0))
@@ -285,5 +311,6 @@ if __name__ == "__main__":
285 test_svg("Dolphin.svg", True)
311 test_svg("Dolphin.svg", True)
286 test_svg("wiresphere.svg", False)
312 test_svg("wiresphere.svg", False)
287 test_text_and_gcode()
313 test_text_and_gcode()
314 test_pen_lerp()
288 test_virtual_machine()
315 test_virtual_machine()
289 print("alle controles geslaagd")
316 print("alle controles geslaagd")