fix: aansluitende boog vanaf het punt waar de pen staat
Een pad dat vlak bij het vorige begint hield de pen neer, maar de boog rekende vanaf zijn eigen beginpunt. GRBL wees die af met error 33.
@@ -13,6 +13,7 @@ from timdraw.geom import (
13
13
dedupe,
14
14
dist,
15
15
fit_arcs,
16
grbl_arc_ok,
16
17
is_closed,
17
18
length_of,
18
19
transform_point,
@@ -295,6 +296,10 @@ def plan(objects: list[dict], settings: dict | None = None) -> dict:
295
296
return min(BED_W, max(0.0, mx)), min(y_max, max(y_min, my))
296
297
297
298
page_at: Point | None = None
299
machine_at: Point | None = None
300
301
def same_spot(a: Point, b: Point) -> bool:
302
return f"{a[0]:.3f}" == f"{b[0]:.3f}" and f"{a[1]:.3f}" == f"{b[1]:.3f}"
298
303
299
304
for pts in draw:
300
305
machine = dedupe([machine_point(x, y) for x, y in pts], 0.05)
@@ -308,6 +313,17 @@ def plan(objects: list[dict], settings: dict | None = None) -> dict:
308
313
if page_at is not None:
309
314
travel.append([page_at, pts[0]])
310
315
travel_mm += dist(page_at, pts[0])
316
machine_at = (x0, y0)
317
elif machine_at is None or not same_spot(machine_at, (x0, y0)):
318
# De pen blijft neer, maar een boog telt vanaf het echte beginpunt.
319
# Zonder dit lijntje zit de pen nog op het vorige eindpunt en wijst GRBL de boog af.
320
gcode.append(f"G1 X{x0:.3f} Y{y0:.3f}{set_feed(travel_speed if trace else draw_speed)}")
321
if machine_at is not None:
322
if trace:
323
travel_mm += dist(machine_at, (x0, y0))
324
else:
325
draw_mm += dist(machine_at, (x0, y0))
326
machine_at = (x0, y0)
311
327
if trace:
312
328
speed = travel_speed
313
329
travel_mm += length_of(machine)
@@ -316,10 +332,13 @@ def plan(objects: list[dict], settings: dict | None = None) -> dict:
316
332
speed = draw_speed
317
333
draw_mm += length_of(machine)
318
334
for code, x, y, i_off, j_off in fit_arcs(machine):
335
if code != "G1" and (machine_at is None or not grbl_arc_ok(machine_at, (x, y), i_off, j_off)):
336
code = "G1"
319
337
if code == "G1":
320
338
gcode.append(f"G1 X{x:.3f} Y{y:.3f}{set_feed(speed)}")
321
339
else:
322
340
gcode.append(f"{code} X{x:.3f} Y{y:.3f} I{i_off:.3f} J{j_off:.3f}{set_feed(speed)}")
341
machine_at = (x, y)
323
342
page_at = pts[-1]
324
343
if not trace:
325
344
pen(False)
@@ -13,6 +13,7 @@ from timdraw.geom import (13
dedupe,13
dedupe,14
dist,14
dist,15
fit_arcs,15
fit_arcs,16
grbl_arc_ok,16
is_closed,17
is_closed,17
length_of,18
length_of,18
transform_point,19
transform_point,@@ -295,6 +296,10 @@ def plan(objects: list[dict], settings: dict | None = None) -> dict:295
return min(BED_W, max(0.0, mx)), min(y_max, max(y_min, my))296
return min(BED_W, max(0.0, mx)), min(y_max, max(y_min, my))296
297
297
page_at: Point | None = None298
page_at: Point | None = None299
machine_at: Point | None = None300
301
def same_spot(a: Point, b: Point) -> bool:302
return f"{a[0]:.3f}" == f"{b[0]:.3f}" and f"{a[1]:.3f}" == f"{b[1]:.3f}"298
303
299
for pts in draw:304
for pts in draw:300
machine = dedupe([machine_point(x, y) for x, y in pts], 0.05)305
machine = dedupe([machine_point(x, y) for x, y in pts], 0.05)@@ -308,6 +313,17 @@ def plan(objects: list[dict], settings: dict | None = None) -> dict:308
if page_at is not None:313
if page_at is not None:309
travel.append([page_at, pts[0]])314
travel.append([page_at, pts[0]])310
travel_mm += dist(page_at, pts[0])315
travel_mm += dist(page_at, pts[0])316
machine_at = (x0, y0)317
elif machine_at is None or not same_spot(machine_at, (x0, y0)):318
# De pen blijft neer, maar een boog telt vanaf het echte beginpunt.319
# Zonder dit lijntje zit de pen nog op het vorige eindpunt en wijst GRBL de boog af.320
gcode.append(f"G1 X{x0:.3f} Y{y0:.3f}{set_feed(travel_speed if trace else draw_speed)}")321
if machine_at is not None:322
if trace:323
travel_mm += dist(machine_at, (x0, y0))324
else:325
draw_mm += dist(machine_at, (x0, y0))326
machine_at = (x0, y0)311
if trace:327
if trace:312
speed = travel_speed328
speed = travel_speed313
travel_mm += length_of(machine)329
travel_mm += length_of(machine)@@ -316,10 +332,13 @@ def plan(objects: list[dict], settings: dict | None = None) -> dict:316
speed = draw_speed332
speed = draw_speed317
draw_mm += length_of(machine)333
draw_mm += length_of(machine)318
for code, x, y, i_off, j_off in fit_arcs(machine):334
for code, x, y, i_off, j_off in fit_arcs(machine):335
if code != "G1" and (machine_at is None or not grbl_arc_ok(machine_at, (x, y), i_off, j_off)):336
code = "G1"319
if code == "G1":337
if code == "G1":320
gcode.append(f"G1 X{x:.3f} Y{y:.3f}{set_feed(speed)}")338
gcode.append(f"G1 X{x:.3f} Y{y:.3f}{set_feed(speed)}")321
else:339
else:322
gcode.append(f"{code} X{x:.3f} Y{y:.3f} I{i_off:.3f} J{j_off:.3f}{set_feed(speed)}")340
gcode.append(f"{code} X{x:.3f} Y{y:.3f} I{i_off:.3f} J{j_off:.3f}{set_feed(speed)}")341
machine_at = (x, y)323
page_at = pts[-1]342
page_at = pts[-1]324
if not trace:343
if not trace:325
pen(False)344
pen(False)@@ -614,7 +614,8 @@ class Machine:
614
614
if line.startswith("$"):
615
615
while pending and not self._stop.is_set():
616
616
used -= pending[0][0]
617
ack = self._accept_ack(pending.pop(0)[1])
617
line_done = pending.pop(0)[1]
618
ack = self._accept_ack(line_done, bool(pending))
618
619
if ack.startswith("error"):
619
620
raise RuntimeError(ack)
620
621
self._wait_until_idle(60)
@@ -627,7 +628,8 @@ class Machine:
627
628
size = len(line) + 1
628
629
while used + size > 110 and pending and not self._stop.is_set():
629
630
used -= pending[0][0]
630
ack = self._accept_ack(pending.pop(0)[1])
631
line_done = pending.pop(0)[1]
632
ack = self._accept_ack(line_done, bool(pending))
631
633
if ack.startswith("error"):
632
634
raise RuntimeError(ack)
633
635
if self._stop.is_set():
@@ -638,7 +640,8 @@ class Machine:
638
640
sent += 1
639
641
self.progress = sent / len(lines)
640
642
while pending and not self._stop.is_set():
641
ack = self._accept_ack(pending.pop(0)[1])
643
line_done = pending.pop(0)[1]
644
ack = self._accept_ack(line_done, bool(pending))
642
645
if ack.startswith("error"):
643
646
raise RuntimeError(ack)
644
647
if not self._stop.is_set():
@@ -659,17 +662,24 @@ class Machine:
659
662
self.job_paused = False
660
663
self._query()
661
664
662
def _accept_ack(self, line: str) -> str:
665
def _accept_ack(self, line: str, more_in_flight: bool = False) -> str:
663
666
"""Antwoord op een regel die al in de buffer staat. Een afgekeurde boog wordt een lijn."""
664
667
ack = self._wait_ack(60)
665
if grbl_error_code(ack) == "33" and line.startswith(("G2 ", "G3 ")):
666
self._arc_fallbacks += 1
667
self._write_raw((chord_for_arc(line) + "\n").encode())
668
ack = self._wait_ack(60)
669
if not ack.startswith("error"):
670
with self._state_lock:
671
if grbl_error_code(self.error) == "33":
672
self.error = ""
668
if grbl_error_code(ack) != "33" or not line.startswith(("G2 ", "G3 ")):
669
return ack
670
self._arc_fallbacks += 1
671
with self._state_lock:
672
if grbl_error_code(self.error) == "33":
673
self.error = ""
674
if more_in_flight:
675
# Er staan al regels achter deze boog. Een lijn nu zou dáárna pas lopen.
676
return "ok"
677
self._write_raw((chord_for_arc(line) + "\n").encode())
678
ack = self._wait_ack(60)
679
if not ack.startswith("error"):
680
with self._state_lock:
681
if grbl_error_code(self.error) == "33":
682
self.error = ""
673
683
return ack
674
684
675
685
def _job_line(self, line: str, timeout: float) -> None:
@@ -614,7 +614,8 @@ class Machine:614
if line.startswith("$"):614
if line.startswith("$"):615
while pending and not self._stop.is_set():615
while pending and not self._stop.is_set():616
used -= pending[0][0]616
used -= pending[0][0]617
ack = self._accept_ack(pending.pop(0)[1])617
line_done = pending.pop(0)[1]618
ack = self._accept_ack(line_done, bool(pending))618
if ack.startswith("error"):619
if ack.startswith("error"):619
raise RuntimeError(ack)620
raise RuntimeError(ack)620
self._wait_until_idle(60)621
self._wait_until_idle(60)@@ -627,7 +628,8 @@ class Machine:627
size = len(line) + 1628
size = len(line) + 1628
while used + size > 110 and pending and not self._stop.is_set():629
while used + size > 110 and pending and not self._stop.is_set():629
used -= pending[0][0]630
used -= pending[0][0]630
ack = self._accept_ack(pending.pop(0)[1])631
line_done = pending.pop(0)[1]632
ack = self._accept_ack(line_done, bool(pending))631
if ack.startswith("error"):633
if ack.startswith("error"):632
raise RuntimeError(ack)634
raise RuntimeError(ack)633
if self._stop.is_set():635
if self._stop.is_set():@@ -638,7 +640,8 @@ class Machine:638
sent += 1640
sent += 1639
self.progress = sent / len(lines)641
self.progress = sent / len(lines)640
while pending and not self._stop.is_set():642
while pending and not self._stop.is_set():641
ack = self._accept_ack(pending.pop(0)[1])643
line_done = pending.pop(0)[1]644
ack = self._accept_ack(line_done, bool(pending))642
if ack.startswith("error"):645
if ack.startswith("error"):643
raise RuntimeError(ack)646
raise RuntimeError(ack)644
if not self._stop.is_set():647
if not self._stop.is_set():@@ -659,17 +662,24 @@ class Machine:659
self.job_paused = False662
self.job_paused = False660
self._query()663
self._query()661
664
662
def _accept_ack(self, line: str) -> str:665
def _accept_ack(self, line: str, more_in_flight: bool = False) -> str:663
"""Antwoord op een regel die al in de buffer staat. Een afgekeurde boog wordt een lijn."""666
"""Antwoord op een regel die al in de buffer staat. Een afgekeurde boog wordt een lijn."""664
ack = self._wait_ack(60)667
ack = self._wait_ack(60)665
if grbl_error_code(ack) == "33" and line.startswith(("G2 ", "G3 ")):668
if grbl_error_code(ack) != "33" or not line.startswith(("G2 ", "G3 ")):666
self._arc_fallbacks += 1669
return ack667
self._write_raw((chord_for_arc(line) + "\n").encode())670
self._arc_fallbacks += 1668
ack = self._wait_ack(60)671
with self._state_lock:669
if not ack.startswith("error"):672
if grbl_error_code(self.error) == "33":670
with self._state_lock:673
self.error = ""671
if grbl_error_code(self.error) == "33":674
if more_in_flight:672
self.error = ""675
# Er staan al regels achter deze boog. Een lijn nu zou dáárna pas lopen.676
return "ok"677
self._write_raw((chord_for_arc(line) + "\n").encode())678
ack = self._wait_ack(60)679
if not ack.startswith("error"):680
with self._state_lock:681
if grbl_error_code(self.error) == "33":682
self.error = ""673
return ack683
return ack674
684
675
def _job_line(self, line: str, timeout: float) -> None:685
def _job_line(self, line: str, timeout: float) -> None:@@ -6,6 +6,7 @@ Draaien: python3 -m timdraw.selftest
6
6
7
7
from pathlib import Path
8
8
import math
9
import re
9
10
import subprocess
10
11
import time
11
12
@@ -167,6 +168,35 @@ def test_text_and_gcode() -> None:
167
168
check("elipsboog haalt error 33 niet", grbl_arc_ok(pos, (ex, ey), i_off, j_off))
168
169
pos = (float(f"{ex:.3f}"), float(f"{ey:.3f}"))
169
170
check("scheve boog wordt geweigerd", not grbl_arc_ok((0, 0), (0, 10.02), 0, -10))
171
curve = [
172
(
173
28.02 + 12 * math.cos(math.pi / 2 - i * math.pi / 32),
174
8.01 + 12 * math.sin(math.pi / 2 - i * math.pi / 32),
175
)
176
for i in range(17)
177
]
178
joined = plan([
179
{"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
180
"strokes": [{"pts": [(10, 20), (28, 20)]}]},
181
{"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,
182
"strokes": [{"pts": curve}]},
183
], {"origin": "top-left", "returnHome": False})
184
pos = None
185
widest = 0.0
186
arcs = 0
187
for line in joined["gcode"].splitlines():
188
if not line.startswith(("G1 ", "G2 ", "G3 ")):
189
continue
190
words = dict(re.findall(r"([XYIJ])([+-]?(?:\d+\.\d*|\.\d+|\d+))", line))
191
if line.startswith(("G2 ", "G3 ")) and pos is not None and "I" in words:
192
arcs += 1
193
radius = math.hypot(float(words["I"]), float(words["J"]))
194
target = math.hypot(float(words["X"]) - (pos[0] + float(words["I"])), float(words["Y"]) - (pos[1] + float(words["J"])))
195
widest = max(widest, abs(target - radius))
196
if "X" in words and "Y" in words:
197
pos = (float(words["X"]), float(words["Y"]))
198
check("aansluitende boog blijft een boog", arcs >= 1, str(arcs))
199
check("aansluitende boog haalt error 33 niet", widest <= 0.005, f"{widest:.4f} mm")
170
200
check(
171
201
"geweigerde boog wordt een lijn",
172
202
chord_for_arc("G3 X0.000 Y10.000 I-10.000 J0.000 F3000") == "G1 X0.000 Y10.000 F3000",
@@ -6,6 +6,7 @@ Draaien: python3 -m timdraw.selftest6
6
7
from pathlib import Path7
from pathlib import Path8
import math8
import math9
import re9
import subprocess10
import subprocess10
import time11
import time11
12
@@ -167,6 +168,35 @@ def test_text_and_gcode() -> None:167
check("elipsboog haalt error 33 niet", grbl_arc_ok(pos, (ex, ey), i_off, j_off))168
check("elipsboog haalt error 33 niet", grbl_arc_ok(pos, (ex, ey), i_off, j_off))168
pos = (float(f"{ex:.3f}"), float(f"{ey:.3f}"))169
pos = (float(f"{ex:.3f}"), float(f"{ey:.3f}"))169
check("scheve boog wordt geweigerd", not grbl_arc_ok((0, 0), (0, 10.02), 0, -10))170
check("scheve boog wordt geweigerd", not grbl_arc_ok((0, 0), (0, 10.02), 0, -10))171
curve = [172
(173
28.02 + 12 * math.cos(math.pi / 2 - i * math.pi / 32),174
8.01 + 12 * math.sin(math.pi / 2 - i * math.pi / 32),175
)176
for i in range(17)177
]178
joined = plan([179
{"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,180
"strokes": [{"pts": [(10, 20), (28, 20)]}]},181
{"x": 0, "y": 0, "scale": 1, "rotation": 0, "drawLine": True, "drawFill": False,182
"strokes": [{"pts": curve}]},183
], {"origin": "top-left", "returnHome": False})184
pos = None185
widest = 0.0186
arcs = 0187
for line in joined["gcode"].splitlines():188
if not line.startswith(("G1 ", "G2 ", "G3 ")):189
continue190
words = dict(re.findall(r"([XYIJ])([+-]?(?:\d+\.\d*|\.\d+|\d+))", line))191
if line.startswith(("G2 ", "G3 ")) and pos is not None and "I" in words:192
arcs += 1193
radius = math.hypot(float(words["I"]), float(words["J"]))194
target = math.hypot(float(words["X"]) - (pos[0] + float(words["I"])), float(words["Y"]) - (pos[1] + float(words["J"])))195
widest = max(widest, abs(target - radius))196
if "X" in words and "Y" in words:197
pos = (float(words["X"]), float(words["Y"]))198
check("aansluitende boog blijft een boog", arcs >= 1, str(arcs))199
check("aansluitende boog haalt error 33 niet", widest <= 0.005, f"{widest:.4f} mm")170
check(200
check(171
"geweigerde boog wordt een lijn",201
"geweigerde boog wordt een lijn",172
chord_for_arc("G3 X0.000 Y10.000 I-10.000 J0.000 F3000") == "G1 X0.000 Y10.000 F3000",202
chord_for_arc("G3 X0.000 Y10.000 I-10.000 J0.000 F3000") == "G1 X0.000 Y10.000 F3000",