Merge pull request #20591 from jalapenopuzzle/jalapenopuzzle/cam_snapmaker_machine_types

CAM: snapmaker improve support for different machine variants and toolheads
This commit is contained in:
sliptonic
2025-05-05 10:47:07 -05:00
committed by GitHub
2 changed files with 627 additions and 107 deletions

View File

@@ -16,7 +16,9 @@
# * implied. See the Licence for the specific language governing *
# * permissions and limitations under the Licence. *
# ***************************************************************************
import argparse
import re
from typing import List
import FreeCAD
@@ -63,7 +65,7 @@ class TestSnapmakerPost(PathTestUtils.PathTestBase):
"""Unit test tear down"""
pass
def get_gcode(self, ops: [str], arguments: str) -> str:
def get_gcode(self, ops: List[str], arguments: str) -> str:
"""Get postprocessed gcode from a list of operations and postprocessor arguments"""
self.profile_op.Path = Path.Path(ops)
self.job.PostProcessorArgs = "--no-show-editor --no-gui --no-thumbnail " + arguments
@@ -75,10 +77,10 @@ class TestSnapmakerPost(PathTestUtils.PathTestBase):
expected_header = """\
;Header Start
;header_type: cnc
;machine: Snapmaker 2 A350(T)
;Post Processor: Snapmaker_post
;Cam File: boxtest.fcstd
;Output Time: \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{0,6}
;machine: Snapmaker 2 A350 50W CNC module
;Post Processor: snapmaker_post
;CAM File: boxtest.fcstd
;Output Time: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{0,6}
;thumbnail: deactivated."""
expected_body = """\
@@ -107,7 +109,7 @@ M5
"""
# test header and body with comments
gcode = self.get_gcode([], "--machine=A350 --toolhead=50W --spindle-percent")
gcode = self.get_gcode([], "--machine=A350 --toolhead=50W_CNC")
g_lines = gcode.splitlines()
e_lines = expected_header.splitlines() + expected_body.splitlines()
@@ -117,16 +119,14 @@ M5
if exp.startswith(";Output Time:"):
self.assertTrue(re.match(exp, line) is not None)
else:
self.assertTrue(line, exp)
self.assertEqual(exp, line)
# test body without header
gcode = self.get_gcode([], "--machine=A350 --toolhead=50W --spindle-percent --no-header")
gcode = self.get_gcode([], "--machine=A350 --toolhead=50W_CNC --no-header")
self.assertEqual(gcode, expected_body)
# test body without comments
gcode = self.get_gcode(
[], "--machine=A350 --toolhead=50W --spindle-percent --no-header --no-comments"
)
gcode = self.get_gcode([], "--machine=A350 --toolhead=50W_CNC --no-header --no-comments")
expected = "".join(
[line for line in expected_body.splitlines(keepends=True) if not line.startswith(";")]
)
@@ -137,9 +137,7 @@ M5
command = Path.Command("G0 X10 Y20 Z30")
expected = "G0 X10.000 Y20.000 Z30.000"
gcode = self.get_gcode(
[command], "--machine=A350 --toolhead=50W --spindle-percent --no-header"
)
gcode = self.get_gcode([command], "--machine=A350 --toolhead=50W_CNC --no-header")
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
@@ -150,7 +148,7 @@ M5
expected = "G0 X10.00 Y20.00 Z30.00"
gcode = self.get_gcode(
[command], "--machine=A350 --toolhead=50W --spindle-percent --no-header --precision=2"
[command], "--machine=A350 --toolhead=50W_CNC --no-header --precision=2"
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
@@ -162,7 +160,7 @@ M5
gcode = self.get_gcode(
[command],
"--machine=A350 --toolhead=50W --spindle-percent --no-header --line-numbers --line-number=10 --line-increment=2",
"--machine=A350 --toolhead=50W_CNC --no-header --line-numbers --line-number=10 --line-increment=2",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
@@ -171,7 +169,7 @@ M5
"""Test Pre-amble"""
gcode = self.get_gcode(
[],
"--machine=A350 --toolhead=50W --spindle-percent --no-header --preamble='G18 G55' --no-comments",
"--machine=A350 --toolhead=50W_CNC --no-header --preamble='G18 G55' --no-comments",
)
result = gcode.splitlines()[0]
self.assertEqual(result, "G18 G55")
@@ -180,7 +178,7 @@ M5
"""Test Post-amble"""
gcode = self.get_gcode(
[],
"--machine=A350 --toolhead=50W --spindle-percent --no-header --postamble='G0 Z50\nM2' --no-comments",
"--machine=A350 --toolhead=50W_CNC --no-header --postamble='G0 Z50\nM2' --no-comments",
)
result = gcode.splitlines()[-2]
self.assertEqual(result, "G0 Z50")
@@ -193,9 +191,7 @@ M5
# test inches conversion
expected = "G0 X0.3937 Y0.7874 Z1.1811"
gcode = self.get_gcode(
[command], "--machine=A350 --toolhead=50W --spindle-percent --no-header --inches"
)
gcode = self.get_gcode([command], "--machine=A350 --toolhead=50W_CNC --no-header --inches")
self.assertEqual(gcode.splitlines()[3], "G20")
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
@@ -204,7 +200,7 @@ M5
expected = "G0 X0.39 Y0.79 Z1.18"
gcode = self.get_gcode(
[command],
"--machine=A350 --toolhead=50W --spindle-percent --no-header --inches --precision=2",
"--machine=A350 --toolhead=50W_CNC --no-header --inches --precision=2",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
@@ -217,7 +213,7 @@ M5
expected = "G0 Y30.000"
gcode = self.get_gcode(
[c0, c1], "--machine=A350 --toolhead=50W --spindle-percent --no-header --axis-modal"
[c0, c1], "--machine=A350 --toolhead=50W_CNC --no-header --axis-modal"
)
result = gcode.splitlines()[19]
self.assertEqual(result, expected)
@@ -228,35 +224,361 @@ M5
c0 = Path.Command("M6 T2")
c1 = Path.Command("M3 S3000")
gcode = self.get_gcode(
[c0, c1], "--machine=A350 --toolhead=50W --spindle-percent --no-header"
)
gcode = self.get_gcode([c0, c1], "--machine=A350 --toolhead=50W_CNC --no-header")
self.assertEqual(gcode.splitlines()[19:22], ["M5", "M76", "M6 T2"])
self.assertEqual(
gcode.splitlines()[22], "M3 P25"
) # no TLO on Snapmaker (G43 inserted after tool change)
def test_spindle(self):
def test_models(self):
"""Test the various models, and also test models that don't exist cause an error."""
command = Path.Command("G0 X10 Y20 Z30")
expected = "G0 X10.000 Y20.000 Z30.000"
with self.assertRaises(SystemExit):
self.get_gcode(
[command],
"--no-header",
)
with self.assertRaises(SystemExit):
gcode = self.get_gcode(
[command],
"--machine=robot --no-header",
)
gcode = self.get_gcode(
[command],
"--machine=Original --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
gcode = self.get_gcode(
[command],
"--machine=A150 --toolhead=50W_CNC --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
gcode = self.get_gcode(
[command],
"--machine=A250 --toolhead=50W_CNC --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
gcode = self.get_gcode(
[command],
"--machine=A250T --toolhead=50W_CNC --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
gcode = self.get_gcode(
[command],
"--machine=A250T --toolhead=200W_CNC --bracing-kit --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
gcode = self.get_gcode(
[command],
"--machine=A350 --toolhead=50W_CNC --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
gcode = self.get_gcode(
[command],
"--machine=A350T --toolhead=50W_CNC --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
gcode = self.get_gcode(
[command],
"--machine=A350T --toolhead=200W_CNC --bracing-kit --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
gcode = self.get_gcode(
[command],
"--machine=Artisan --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
def test_mod_kits(self):
"""Test the various mod kits against various models."""
# Reference for boundaries with the bracing kit and quick swap kit combinations
# [1] https://support.snapmaker.com/hc/en-us/articles/20786910972311-FAQ-for-Bracing-Kit-for-Snapmaker-2-0-Linear-Modules#h_01HN4Z7S9WJE5BRT492WR0CKH1
# Reference for quick swap kit
# [2] https://support.snapmaker.com/hc/en-us/articles/15320624494103-Pre-sale-FAQ-for-Quick-Swap-Kit
command = Path.Command("G0 X10 Y20 Z30")
expected = "G0 X10.000 Y20.000 Z30.000"
gcode = self.get_gcode(
[command],
"--machine=Original --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], [])
# https://forum.snapmaker.com/t/cnc-work-area-size/5178
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=125, Y=125, Z=50))
gcode = self.get_gcode(
[command],
"--machine=Original --quick-swap --no-header",
)
# I don't understand why export returns the arguments
# if snapmaker_process_arguments fails.
self.assertTrue(isinstance(gcode, argparse.Namespace))
self.assertFalse(isinstance(gcode, str))
gcode = self.get_gcode(
[command],
"--machine=Original --bracing-kit --no-header",
)
# I don't understand why export returns the arguments
# if snapmaker_process_arguments fails.
self.assertTrue(isinstance(gcode, argparse.Namespace))
self.assertFalse(isinstance(gcode, str))
# This is incompatible according to [2]
gcode = self.get_gcode(
[command],
"--machine=A150 --quick-swap --no-header",
)
# I don't understand why export returns the arguments
# if snapmaker_process_arguments fails.
self.assertTrue(isinstance(gcode, argparse.Namespace))
self.assertFalse(isinstance(gcode, str))
gcode = self.get_gcode(
[command],
"--machine=Artisan --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], [])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=400, Y=400, Z=400))
gcode = self.get_gcode(
[command],
"--machine=Artisan --quick-swap --no-header",
)
# I don't understand why export returns the arguments
# if snapmaker_process_arguments fails.
self.assertTrue(isinstance(gcode, argparse.Namespace))
self.assertFalse(isinstance(gcode, str))
gcode = self.get_gcode(
[command],
"--machine=Artisan --bracing-kit --no-header",
)
# I don't understand why export returns the arguments
# if snapmaker_process_arguments fails.
self.assertTrue(isinstance(gcode, argparse.Namespace))
self.assertFalse(isinstance(gcode, str))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A150 --toolhead=50W_CNC --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], [])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=145, Y=160, Z=90))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A150 --toolhead=50W_CNC --bracing-kit --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["BK"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=145, Y=148, Z=90))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A250 --toolhead=50W_CNC --bracing-kit --quick-swap --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["QS", "BK"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=230, Y=223, Z=180))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A250T --toolhead=50W_CNC --quick-swap --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["QS"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=230, Y=235, Z=180))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A250T --toolhead=200W_CNC --bracing-kit --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["BK"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=230, Y=225, Z=180))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A350 --toolhead=50W_CNC --bracing-kit --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["BK"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=320, Y=338, Z=275))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A350 --toolhead=50W_CNC --quick-swap --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["QS"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=320, Y=335, Z=275))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A350 --toolhead=50W_CNC --bracing-kit --quick-swap --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["QS", "BK"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=320, Y=323, Z=275))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A350T --toolhead=50W_CNC --bracing-kit --quick-swap --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["QS", "BK"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=320, Y=323, Z=275))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A350T --toolhead=200W_CNC --bracing-kit --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["BK"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=320, Y=325, Z=275))
# This test case is covered in reference [1]
gcode = self.get_gcode(
[command],
"--machine=A350T --toolhead=200W_CNC --bracing-kit --quick-swap --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["MOD_KITS_INSTALLED"], ["QS", "BK"])
self.assertEqual(self.post.values["BOUNDARIES"], dict(X=320, Y=310, Z=275))
def test_toolhead_selection(self):
"""Test automatic selection of toolhead where appropriate"""
# check succeeds
command = Path.Command("G0 X10 Y20 Z30")
expected = "G0 X10.000 Y20.000 Z30.000"
gcode = self.get_gcode(
[command],
"--machine=Original --no-header",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["TOOLHEAD_NAME"], "Original CNC module")
gcode = self.get_gcode(
[command],
"--machine=A350 --no-header",
)
self.assertFalse(isinstance(gcode, str))
gcode = self.get_gcode(
[command],
"--machine=A350T --no-header",
)
self.assertFalse(isinstance(gcode, str))
# check succeed with artisan (which base is bigger)
gcode = self.get_gcode(
[command],
"--machine=Artisan --no-header --boundaries-check",
)
result = gcode.splitlines()[18]
self.assertEqual(result, expected)
self.assertEqual(self.post.values["TOOLHEAD_NAME"], "200W CNC module")
def test_spindle_percent_rpm_auto_select(self):
"""Test automatic selection of spindle speed rpm vs percent"""
command = Path.Command("M3 S2100")
# test original toolhead
gcode = self.get_gcode([command], "--machine=Original --no-header")
self.assertEqual(gcode.splitlines()[18], "M3 P30")
command = Path.Command("M3 S3600")
# test 50W toolhead
gcode = self.get_gcode([command], "--machine=A350 --toolhead=50W_CNC --no-header")
self.assertEqual(gcode.splitlines()[18], "M3 P30")
# test 200W toolhead
gcode = self.get_gcode(
[command], "--machine=A350 --toolhead=200W_CNC --bracing-kit --no-header"
)
self.assertEqual(gcode.splitlines()[18], "M3 S3600")
# test 200W toolhead
gcode = self.get_gcode([command], "--machine=Artisan --no-header")
self.assertEqual(gcode.splitlines()[18], "M3 S3600")
def test_spindle_percent(self):
"""Test spindle speed conversion from RPM to percents"""
command = Path.Command("M3 S3600")
# test 50W toolhead
gcode = self.get_gcode(
[command], "--machine=A350 --toolhead=50W --spindle-percent --no-header"
[command], "--machine=A350 --toolhead=50W_CNC --spindle-percent --no-header"
)
self.assertEqual(gcode.splitlines()[18], "M3 P30")
# test 200W toolhead
gcode = self.get_gcode(
[command], "--machine=A350 --toolhead=200W --spindle-percent --no-header"
[command],
"--machine=A350 --toolhead=200W_CNC --bracing-kit --spindle-percent --no-header",
)
self.assertEqual(gcode.splitlines()[18], "M3 P20")
# test custom spindle speed extrema
gcode = self.get_gcode(
[command],
"--machine=A350 --toolhead=200W --spindle-percent --no-header --spindle-speeds=3000,4000",
"--machine=A350 --toolhead=200W_CNC --bracing-kit --spindle-percent --no-header --spindle-speeds=3000,4000",
)
self.assertEqual(gcode.splitlines()[18], "M3 P90")
@@ -265,7 +587,7 @@ M5
command = Path.Command("(comment)")
gcode = self.get_gcode(
[command], "--machine=A350 --toolhead=50W --spindle-percent --no-header"
[command], "--machine=A350 --toolhead=50W_CNC --spindle-percent --no-header"
)
result = gcode.splitlines()[18]
expected = ";comment"
@@ -279,7 +601,7 @@ M5
gcode = self.get_gcode(
[command],
"--machine=A350 --toolhead=50W --spindle-percent --no-header --boundaries-check",
"--machine=A350 --toolhead=50W_CNC --no-header --boundaries-check",
)
self.assertTrue(self.post.check_boundaries(gcode.splitlines()))
@@ -288,20 +610,20 @@ M5
c1 = Path.Command("G02 Y260")
gcode = self.get_gcode(
[c0, c1],
"--machine=A350 --toolhead=50W --spindle-percent --no-header --boundaries-check",
"--machine=A350 --toolhead=50W_CNC --no-header --boundaries-check",
)
self.assertFalse(self.post.check_boundaries(gcode.splitlines()))
# check succeed with artisan (which base is bigger)
gcode = self.get_gcode(
[c0, c1],
"--machine=artisan --toolhead=50W --spindle-percent --no-header --boundaries-check",
"--machine=Artisan --no-header --boundaries-check",
)
self.assertTrue(self.post.check_boundaries(gcode.splitlines()))
# check fails with custom boundaries
gcode = self.get_gcode(
[c0, c1],
"--machine=A350 --toolhead=50W --spindle-percent --no-header --boundaries-check --boundaries='50,400,10'",
"--machine=A350 --toolhead=50W_CNC --no-header --boundaries-check --boundaries='50,400,10'",
)
self.assertFalse(self.post.check_boundaries(gcode.splitlines()))