BlueGit

timfa timdraw

fix: onderkant van het A4 10 mm omhoog

De onderrand van het papier valt samen met Y=-297. Een geweigerd $-commando tijdens het tekenen wordt opnieuw gestuurd zodra de machine stilstaat.

timfa Tim Falken · pushed by timfa

Authored 2026-09-29 · 7 weeks ago

Hash
c87e024959b65b6024dcbe77f2d0e322a7c9ad03
Parent
c6722395
5 files changed +47 −12
static/app.js +2 −2
@@ -1,8 +1,8 @@
1 1 const PAPER_LEFT = -10;
2 2 const PAPER_TOP = 4;
3 const PAPER_BOTTOM = -307;
3 const PAPER_BOTTOM = -297;
4 4 const A4W = 210;
5 const A4H = 311;
5 const A4H = PAPER_TOP - PAPER_BOTTOM;
6 6 const PEN_MM = 0.4;
7 7
8 8 const $ = (id) => document.getElementById(id);
@@ -1,8 +1,8 @@
1const PAPER_LEFT = -10;
1const PAPER_LEFT = -10;
2const PAPER_TOP = 4;
2const PAPER_TOP = 4;
3const PAPER_BOTTOM = -307;
3const PAPER_BOTTOM = -297;
4const A4W = 210;
4const A4W = 210;
5const A4H = 311;
5const A4H = PAPER_TOP - PAPER_BOTTOM;
6const PEN_MM = 0.4;
6const PEN_MM = 0.4;
7
7
8const $ = (id) => document.getElementById(id);
8const $ = (id) => document.getElementById(id);
timdraw/geom.py +2 −2
@@ -9,11 +9,11 @@ Polyline = list[Point]
9 9
10 10 # Fysiek papier t.o.v. machinehome. Y omhoog is positief.
11 11 # De linkerrand ligt 10 mm links van X=0, de bovenrand 4 mm boven Y=0.
12 # X=200 valt op de rechterrand. Y=-297 valt 10 mm boven de onderrand.
12 # X=200 valt op de rechterrand. Y=-297 valt op de onderrand.
13 13 PAPER_LEFT = -10.0
14 14 PAPER_TOP = 4.0
15 15 PAPER_RIGHT = 200.0
16 PAPER_BOTTOM = -307.0
16 PAPER_BOTTOM = -297.0
17 17 A4_W = PAPER_RIGHT - PAPER_LEFT
18 18 A4_H = PAPER_TOP - PAPER_BOTTOM
19 19
@@ -9,11 +9,11 @@ Polyline = list[Point]
9
9
10# Fysiek papier t.o.v. machinehome. Y omhoog is positief.
10# Fysiek papier t.o.v. machinehome. Y omhoog is positief.
11# De linkerrand ligt 10 mm links van X=0, de bovenrand 4 mm boven Y=0.
11# De linkerrand ligt 10 mm links van X=0, de bovenrand 4 mm boven Y=0.
12# X=200 valt op de rechterrand. Y=-297 valt 10 mm boven de onderrand.
12# X=200 valt op de rechterrand. Y=-297 valt op de onderrand.
13PAPER_LEFT = -10.0
13PAPER_LEFT = -10.0
14PAPER_TOP = 4.0
14PAPER_TOP = 4.0
15PAPER_RIGHT = 200.0
15PAPER_RIGHT = 200.0
16PAPER_BOTTOM = -307.0
16PAPER_BOTTOM = -297.0
17A4_W = PAPER_RIGHT - PAPER_LEFT
17A4_W = PAPER_RIGHT - PAPER_LEFT
18A4_H = PAPER_TOP - PAPER_BOTTOM
18A4_H = PAPER_TOP - PAPER_BOTTOM
19
19
timdraw/grbl.py +20 −2
@@ -25,6 +25,12 @@ except ImportError: # pragma: no cover
25 25 _STATE = Path.home() / ".config" / "timdraw" / "last-device.json"
26 26
27 27
28 def grbl_error_code(ack: str) -> str:
29 if not ack.startswith("error:"):
30 return ""
31 return ack[6:].strip()
32
33
28 34 def chord_for_arc(line: str) -> str:
29 35 """Rechte lijn naar hetzelfde eindpunt, als GRBL een boog met error 33 weigert."""
30 36 words = dict(re.findall(r"([XYF])([+-]?(?:\d+\.\d*|\.\d+|\d+))", line))
@@ -658,13 +664,25 @@ class Machine:
658 664 return
659 665 self._write_raw((line + "\n").encode())
660 666 ack = self._wait_ack(timeout)
661 if ack.startswith("error:33") and line.startswith(("G2 ", "G3 ")):
667 retries = 0
668 while grbl_error_code(ack) == "8" and line.startswith("$") and retries < 8 and not self._stop.is_set():
669 # $ mag alleen in Idle. De beweging was nog bezig; even wachten en opnieuw.
670 retries += 1
671 with self._state_lock:
672 if grbl_error_code(self.error) == "8":
673 self.error = ""
674 self._wait_until_idle(30)
675 if self._stop.is_set():
676 return
677 self._write_raw((line + "\n").encode())
678 ack = self._wait_ack(timeout)
679 if grbl_error_code(ack) == "33" and line.startswith(("G2 ", "G3 ")):
662 680 self._arc_fallbacks += 1
663 681 self._write_raw((chord_for_arc(line) + "\n").encode())
664 682 ack = self._wait_ack(timeout)
665 683 if not ack.startswith("error"):
666 684 with self._state_lock:
667 if self.error.startswith("error:33"):
685 if grbl_error_code(self.error) == "33":
668 686 self.error = ""
669 687 if ack.startswith("error"):
670 688 raise RuntimeError(ack)
@@ -25,6 +25,12 @@ except ImportError: # pragma: no cover
25_STATE = Path.home() / ".config" / "timdraw" / "last-device.json"
25_STATE = Path.home() / ".config" / "timdraw" / "last-device.json"
26
26
27
27
28def grbl_error_code(ack: str) -> str:
29 if not ack.startswith("error:"):
30 return ""
31 return ack[6:].strip()
32
33
28def chord_for_arc(line: str) -> str:
34def chord_for_arc(line: str) -> str:
29 """Rechte lijn naar hetzelfde eindpunt, als GRBL een boog met error 33 weigert."""
35 """Rechte lijn naar hetzelfde eindpunt, als GRBL een boog met error 33 weigert."""
30 words = dict(re.findall(r"([XYF])([+-]?(?:\d+\.\d*|\.\d+|\d+))", line))
36 words = dict(re.findall(r"([XYF])([+-]?(?:\d+\.\d*|\.\d+|\d+))", line))
@@ -658,13 +664,25 @@ class Machine:
658 return
664 return
659 self._write_raw((line + "\n").encode())
665 self._write_raw((line + "\n").encode())
660 ack = self._wait_ack(timeout)
666 ack = self._wait_ack(timeout)
661 if ack.startswith("error:33") and line.startswith(("G2 ", "G3 ")):
667 retries = 0
668 while grbl_error_code(ack) == "8" and line.startswith("$") and retries < 8 and not self._stop.is_set():
669 # $ mag alleen in Idle. De beweging was nog bezig; even wachten en opnieuw.
670 retries += 1
671 with self._state_lock:
672 if grbl_error_code(self.error) == "8":
673 self.error = ""
674 self._wait_until_idle(30)
675 if self._stop.is_set():
676 return
677 self._write_raw((line + "\n").encode())
678 ack = self._wait_ack(timeout)
679 if grbl_error_code(ack) == "33" and line.startswith(("G2 ", "G3 ")):
662 self._arc_fallbacks += 1
680 self._arc_fallbacks += 1
663 self._write_raw((chord_for_arc(line) + "\n").encode())
681 self._write_raw((chord_for_arc(line) + "\n").encode())
664 ack = self._wait_ack(timeout)
682 ack = self._wait_ack(timeout)
665 if not ack.startswith("error"):
683 if not ack.startswith("error"):
666 with self._state_lock:
684 with self._state_lock:
667 if self.error.startswith("error:33"):
685 if grbl_error_code(self.error) == "33":
668 self.error = ""
686 self.error = ""
669 if ack.startswith("error"):
687 if ack.startswith("error"):
670 raise RuntimeError(ack)
688 raise RuntimeError(ack)
timdraw/selftest.py +19 −6
@@ -81,7 +81,7 @@ def test_text_and_gcode() -> None:
81 81 check("grijswaarden tekenen", gray_job["stats"]["drawMm"] > 100, str(gray_job["stats"]))
82 82 corner = plan([{
83 83 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
84 "strokes": [{"pts": [(10, 307), (30, 287)]}],
84 "strokes": [{"pts": [(10, 297), (30, 277)]}],
85 85 }], {"origin": "top-left"})
86 86 check("homehoek blijft X0 Y0", "X0.000 Y0.000" in corner["gcode"], corner["gcode"])
87 87 check("blad ligt rechts en onder het nulpunt", "X20.000 Y-20.000" in corner["gcode"])
@@ -92,8 +92,8 @@ def test_text_and_gcode() -> None:
92 92 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
93 93 "strokes": [
94 94 {"pts": [(110, 110), (130, 110)]},
95 {"pts": [(20, 307), (10, 307)]},
96 {"pts": [(20, 297), (20, 307)]},
95 {"pts": [(20, 297), (10, 297)]},
96 {"pts": [(20, 287), (20, 297)]},
97 97 ],
98 98 }], {"origin": "top-left", "returnHome": False, "travelSpeed": 4000})
99 99 moves = [line for line in linked["gcode"].splitlines() if line.startswith("G1 X")]
@@ -105,7 +105,7 @@ def test_text_and_gcode() -> None:
105 105 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
106 106 "strokes": [{"pts": [(110, 110), (130, 110)]}]},
107 107 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
108 "strokes": [{"pts": [(10, 307), (30, 307)]}]},
108 "strokes": [{"pts": [(10, 297), (30, 297)]}]},
109 109 ], {"origin": "top-left", "perObject": True, "returnHome": False, "travelSpeed": 4000})
110 110 grouped_moves = [line for line in grouped["gcode"].splitlines() if line.startswith("G1 X")]
111 111 check("eerst het eerste object", "X100.000" in grouped_moves[0], " | ".join(grouped_moves[:3]))
@@ -113,7 +113,7 @@ def test_text_and_gcode() -> None:
113 113 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
114 114 "strokes": [{"pts": [(110, 110), (130, 110)]}]},
115 115 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
116 "strokes": [{"pts": [(10, 307), (30, 307)]}]},
116 "strokes": [{"pts": [(10, 297), (30, 297)]}]},
117 117 ], {"origin": "top-left", "returnHome": False, "travelSpeed": 4000})
118 118 mixed_moves = [line for line in mixed["gcode"].splitlines() if line.startswith("G1 X")]
119 119 check("zonder groeperen de dichtstbijzijnde", mixed_moves[0].startswith("G1 X0.000 Y0.000"), " | ".join(mixed_moves[:2]))
@@ -279,11 +279,24 @@ def test_virtual_machine() -> None:
279 279 finally:
280 280 machine.disconnect()
281 281
282 machine, grbl = connect(0, 0)
283 try:
284 before = grbl.commands.count("$1=255")
285 grbl.reject_dollar = 1
286 machine._job_line("$1=255", 5)
287 check(
288 "error 8 wordt opnieuw gestuurd",
289 grbl.commands.count("$1=255") == before + 2 and machine.error == "",
290 machine.error,
291 )
292 finally:
293 machine.disconnect()
294
282 295 machine, grbl = connect(0, 0)
283 296 try:
284 297 corner = plan([{
285 298 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
286 "strokes": [{"pts": [(10, 307), (30, 287)]}],
299 "strokes": [{"pts": [(10, 297), (30, 277)]}],
287 300 }], {"origin": "top-left"})
288 301 gcode = corner["gcode"] + "\nG3 X30.000 Y-20.000 I0.000 J10.000 F3000\n"
289 302 machine.start_job(gcode)
@@ -81,7 +81,7 @@ def test_text_and_gcode() -> None:
81 check("grijswaarden tekenen", gray_job["stats"]["drawMm"] > 100, str(gray_job["stats"]))
81 check("grijswaarden tekenen", gray_job["stats"]["drawMm"] > 100, str(gray_job["stats"]))
82 corner = plan([{
82 corner = plan([{
83 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
83 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
84 "strokes": [{"pts": [(10, 307), (30, 287)]}],
84 "strokes": [{"pts": [(10, 297), (30, 277)]}],
85 }], {"origin": "top-left"})
85 }], {"origin": "top-left"})
86 check("homehoek blijft X0 Y0", "X0.000 Y0.000" in corner["gcode"], corner["gcode"])
86 check("homehoek blijft X0 Y0", "X0.000 Y0.000" in corner["gcode"], corner["gcode"])
87 check("blad ligt rechts en onder het nulpunt", "X20.000 Y-20.000" in corner["gcode"])
87 check("blad ligt rechts en onder het nulpunt", "X20.000 Y-20.000" in corner["gcode"])
@@ -92,8 +92,8 @@ def test_text_and_gcode() -> None:
92 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
92 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
93 "strokes": [
93 "strokes": [
94 {"pts": [(110, 110), (130, 110)]},
94 {"pts": [(110, 110), (130, 110)]},
95 {"pts": [(20, 307), (10, 307)]},
95 {"pts": [(20, 297), (10, 297)]},
96 {"pts": [(20, 297), (20, 307)]},
96 {"pts": [(20, 287), (20, 297)]},
97 ],
97 ],
98 }], {"origin": "top-left", "returnHome": False, "travelSpeed": 4000})
98 }], {"origin": "top-left", "returnHome": False, "travelSpeed": 4000})
99 moves = [line for line in linked["gcode"].splitlines() if line.startswith("G1 X")]
99 moves = [line for line in linked["gcode"].splitlines() if line.startswith("G1 X")]
@@ -105,7 +105,7 @@ def test_text_and_gcode() -> None:
105 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
105 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
106 "strokes": [{"pts": [(110, 110), (130, 110)]}]},
106 "strokes": [{"pts": [(110, 110), (130, 110)]}]},
107 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
107 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
108 "strokes": [{"pts": [(10, 307), (30, 307)]}]},
108 "strokes": [{"pts": [(10, 297), (30, 297)]}]},
109 ], {"origin": "top-left", "perObject": True, "returnHome": False, "travelSpeed": 4000})
109 ], {"origin": "top-left", "perObject": True, "returnHome": False, "travelSpeed": 4000})
110 grouped_moves = [line for line in grouped["gcode"].splitlines() if line.startswith("G1 X")]
110 grouped_moves = [line for line in grouped["gcode"].splitlines() if line.startswith("G1 X")]
111 check("eerst het eerste object", "X100.000" in grouped_moves[0], " | ".join(grouped_moves[:3]))
111 check("eerst het eerste object", "X100.000" in grouped_moves[0], " | ".join(grouped_moves[:3]))
@@ -113,7 +113,7 @@ def test_text_and_gcode() -> None:
113 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
113 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
114 "strokes": [{"pts": [(110, 110), (130, 110)]}]},
114 "strokes": [{"pts": [(110, 110), (130, 110)]}]},
115 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
115 {"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
116 "strokes": [{"pts": [(10, 307), (30, 307)]}]},
116 "strokes": [{"pts": [(10, 297), (30, 297)]}]},
117 ], {"origin": "top-left", "returnHome": False, "travelSpeed": 4000})
117 ], {"origin": "top-left", "returnHome": False, "travelSpeed": 4000})
118 mixed_moves = [line for line in mixed["gcode"].splitlines() if line.startswith("G1 X")]
118 mixed_moves = [line for line in mixed["gcode"].splitlines() if line.startswith("G1 X")]
119 check("zonder groeperen de dichtstbijzijnde", mixed_moves[0].startswith("G1 X0.000 Y0.000"), " | ".join(mixed_moves[:2]))
119 check("zonder groeperen de dichtstbijzijnde", mixed_moves[0].startswith("G1 X0.000 Y0.000"), " | ".join(mixed_moves[:2]))
@@ -279,11 +279,24 @@ def test_virtual_machine() -> None:
279 finally:
279 finally:
280 machine.disconnect()
280 machine.disconnect()
281
281
282 machine, grbl = connect(0, 0)
283 try:
284 before = grbl.commands.count("$1=255")
285 grbl.reject_dollar = 1
286 machine._job_line("$1=255", 5)
287 check(
288 "error 8 wordt opnieuw gestuurd",
289 grbl.commands.count("$1=255") == before + 2 and machine.error == "",
290 machine.error,
291 )
292 finally:
293 machine.disconnect()
294
282 machine, grbl = connect(0, 0)
295 machine, grbl = connect(0, 0)
283 try:
296 try:
284 corner = plan([{
297 corner = plan([{
285 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
298 "x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
286 "strokes": [{"pts": [(10, 307), (30, 287)]}],
299 "strokes": [{"pts": [(10, 297), (30, 277)]}],
287 }], {"origin": "top-left"})
300 }], {"origin": "top-left"})
288 gcode = corner["gcode"] + "\nG3 X30.000 Y-20.000 I0.000 J10.000 F3000\n"
301 gcode = corner["gcode"] + "\nG3 X30.000 Y-20.000 I0.000 J10.000 F3000\n"
289 machine.start_job(gcode)
302 machine.start_job(gcode)
timdraw/virtual.py +4 −0
@@ -31,6 +31,7 @@ class VirtualGrbl:
31 31 self.jog_queue: list[tuple[float, float]] = []
32 32 self.drawn: list[tuple[float, float]] = []
33 33 self.commands: list[str] = []
34 self.reject_dollar = 0
34 35 self._polls = 0
35 36 self._line = ""
36 37
@@ -111,6 +112,9 @@ class VirtualGrbl:
111 112 if line.startswith("$$"):
112 113 return ["$110=6000", "$111=6000", "ok"]
113 114 if line.startswith("$"):
115 if self.reject_dollar > 0:
116 self.reject_dollar -= 1
117 return ["error:8"]
114 118 return ["ok"]
115 119 return self._motion(line)
116 120
@@ -31,6 +31,7 @@ class VirtualGrbl:
31 self.jog_queue: list[tuple[float, float]] = []
31 self.jog_queue: list[tuple[float, float]] = []
32 self.drawn: list[tuple[float, float]] = []
32 self.drawn: list[tuple[float, float]] = []
33 self.commands: list[str] = []
33 self.commands: list[str] = []
34 self.reject_dollar = 0
34 self._polls = 0
35 self._polls = 0
35 self._line = ""
36 self._line = ""
36
37
@@ -111,6 +112,9 @@ class VirtualGrbl:
111 if line.startswith("$$"):
112 if line.startswith("$$"):
112 return ["$110=6000", "$111=6000", "ok"]
113 return ["$110=6000", "$111=6000", "ok"]
113 if line.startswith("$"):
114 if line.startswith("$"):
115 if self.reject_dollar > 0:
116 self.reject_dollar -= 1
117 return ["error:8"]
114 return ["ok"]
118 return ["ok"]
115 return self._motion(line)
119 return self._motion(line)
116
120