Merge branch 'swinman-master'

This commit is contained in:
Jesus Zen Droïd 2024-03-08 12:48:22 +01:00
commit 7e1ed6da9b
10 changed files with 122 additions and 171 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@
# build # build
build/* build/*
src/pygcode.egg-info/*
dist/pygcode-*.egg

View File

@ -1,46 +0,0 @@
Metadata-Version: 1.1
Name: pygcode
Version: 0.2.1
Summary: Basic g-code parser, interpreter, and encoder library.
Home-page: https://github.com/fragmuffin/pygcode
Author: Peter Boin
Author-email: peter.boin@gmail.com
License: GPLv3
Description-Content-Type: UNKNOWN
Description: =======
pygcode
=======
GCODE Parser for Python
Currently in development, ``pygcode`` is a low-level GCode interpreter
for python.
Installation
============
Install using ``pip``
``pip install pygcode``
or `download directly from PyPi <https://pypi.python.org/pypi/pygcode>`__
Documentation
=============
`Check out the wiki <https://github.com/fragmuffin/pygcode/wiki>`__ for documentation.
Keywords: gcode,cnc,parser,interpreter
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Manufacturing
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering

View File

@ -1,21 +0,0 @@
README.rst
setup.cfg
setup.py
scripts/pygcode-crop
scripts/pygcode-norm
src/pygcode/__init__.py
src/pygcode/block.py
src/pygcode/comment.py
src/pygcode/exceptions.py
src/pygcode/gcodes.py
src/pygcode/line.py
src/pygcode/machine.py
src/pygcode/transform.py
src/pygcode/utils.py
src/pygcode/words.py
src/pygcode.egg-info/PKG-INFO
src/pygcode.egg-info/SOURCES.txt
src/pygcode.egg-info/dependency_links.txt
src/pygcode.egg-info/not-zip-safe
src/pygcode.egg-info/requires.txt
src/pygcode.egg-info/top_level.txt

View File

@ -1 +0,0 @@

View File

@ -1 +0,0 @@

View File

@ -1,3 +0,0 @@
argparse
euclid3
six

View File

@ -1 +0,0 @@
pygcode

View File

@ -18,20 +18,28 @@ from .utils import WordType
# ======================== WORDS ======================== # ======================== WORDS ========================
REGEX_FLOAT = re.compile(r'^\s*-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float REGEX_NUMBER = re.compile(r'^\s*-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float
REGEX_INT = re.compile(r'^\s*-?\d+')
REGEX_POSITIVEINT = re.compile(r'^\s*\d+') REGEX_POSITIVEINT = re.compile(r'^\s*\d+')
REGEX_CODE = re.compile(r'^\s*\d+(\.\d)?') # float, but can't be negative REGEX_CODE = re.compile(r'^\s*\d+(\.\d)?') # float, but can't be negative
# Value cleaning functions def CLASS_NUMBER(value):
def _clean_codestr(value): if isinstance(value, (int, float)):
if value < 10: return value
return "0%g" % value if '.' in value:
return "%g" % value return float(value)
return int(value)
# Value cleaning functions
CLEAN_NONE = lambda v: v CLEAN_NONE = lambda v: v
CLEAN_FLOAT = lambda v: "{0:g}".format(round(v, 3))
CLEAN_CODE = _clean_codestr def CLEAN_NUMBER(v):
if isinstance(v, int):
return str(v)
fstr = "{0:g}".format(round(v, 3)) # FIXME bad practice for inches
if '.' not in fstr:
return fstr + '.'
return fstr
CLEAN_CODE = lambda v: '{:02}'.format(v)
CLEAN_INT = lambda v: "%g" % v CLEAN_INT = lambda v: "%g" % v
WORD_MAP = { WORD_MAP = {
@ -40,74 +48,74 @@ WORD_MAP = {
# Rotational Axes # Rotational Axes
'A': WordType( 'A': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Absolute or incremental position of A axis (rotational axis around X axis)", description="Absolute or incremental position of A axis (rotational axis around X axis)",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'B': WordType( 'B': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Absolute or incremental position of B axis (rotational axis around Y axis)", description="Absolute or incremental position of B axis (rotational axis around Y axis)",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'C': WordType( 'C': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Absolute or incremental position of C axis (rotational axis around Z axis)", description="Absolute or incremental position of C axis (rotational axis around Z axis)",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'D': WordType( 'D': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Defines diameter or radial offset used for cutter compensation. D is used for depth of cut on lathes. It is used for aperture selection and commands on photoplotters.", description="Defines diameter or radial offset used for cutter compensation. D is used for depth of cut on lathes. It is used for aperture selection and commands on photoplotters.",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# Feed Rates # Feed Rates
'E': WordType( 'E': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Precision feedrate for threading on lathes", description="Precision feedrate for threading on lathes",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'F': WordType( 'F': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Feedrate", description="Feedrate",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# G-Codes # G-Codes
'G': WordType( 'G': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_CODE, value_regex=REGEX_CODE,
description="Address for preparatory commands", description="Address for preparatory commands",
clean_value=CLEAN_CODE, clean_value=CLEAN_NONE,
), ),
# Tool Offsets # Tool Offsets
'H': WordType( 'H': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Defines tool length offset; Incremental axis corresponding to C axis (e.g., on a turn-mill)", description="Defines tool length offset; Incremental axis corresponding to C axis (e.g., on a turn-mill)",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# Arc radius center coords # Arc radius center coords
'I': WordType( 'I': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Defines arc center in X axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.", description="Defines arc center in X axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'J': WordType( 'J': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Defines arc center in Y axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.", description="Defines arc center in Y axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'K': WordType( 'K': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Defines arc center in Z axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles, equal to L address.", description="Defines arc center in Z axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles, equal to L address.",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# Loop Count # Loop Count
'L': WordType( 'L': WordType(
@ -118,10 +126,10 @@ WORD_MAP = {
), ),
# Miscellaneous Function # Miscellaneous Function
'M': WordType( 'M': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_CODE, value_regex=REGEX_CODE,
description="Miscellaneous function", description="Miscellaneous function",
clean_value=CLEAN_CODE, clean_value=CLEAN_NONE,
), ),
# Line Number # Line Number
'N': WordType( 'N': WordType(
@ -139,31 +147,31 @@ WORD_MAP = {
), ),
# Parameter (arbitrary parameter) # Parameter (arbitrary parameter)
'P': WordType( 'P': WordType(
cls=float, # parameter is often an integer, but can be a float cls=CLASS_NUMBER, # parameter is often an integer, but can be a float
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Serves as parameter address for various G and M codes", description="Serves as parameter address for various G and M codes",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# Peck increment # Peck increment
'Q': WordType( 'Q': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Depth to increase on each peck; Peck increment in canned cycles", description="Depth to increase on each peck; Peck increment in canned cycles",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# Arc Radius # Arc Radius
'R': WordType( 'R': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Defines size of arc radius, or defines retract height in milling canned cycles", description="Defines size of arc radius, or defines retract height in milling canned cycles",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# Spindle speed # Spindle speed
'S': WordType( 'S': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Defines speed, either spindle speed or surface speed depending on mode", description="Defines speed, either spindle speed or surface speed depending on mode",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# Tool Selecton # Tool Selecton
'T': WordType( 'T': WordType(
@ -174,41 +182,41 @@ WORD_MAP = {
), ),
# Incremental axes # Incremental axes
'U': WordType( 'U': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Incremental axis corresponding to X axis (typically only lathe group A controls) Also defines dwell time on some machines (instead of 'P' or 'X').", description="Incremental axis corresponding to X axis (typically only lathe group A controls) Also defines dwell time on some machines (instead of 'P' or 'X').",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'V': WordType( 'V': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Incremental axis corresponding to Y axis", description="Incremental axis corresponding to Y axis",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'W': WordType( 'W': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Incremental axis corresponding to Z axis (typically only lathe group A controls)", description="Incremental axis corresponding to Z axis (typically only lathe group A controls)",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
# Linear Axes # Linear Axes
'X': WordType( 'X': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Absolute or incremental position of X axis.", description="Absolute or incremental position of X axis.",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'Y': WordType( 'Y': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Absolute or incremental position of Y axis.", description="Absolute or incremental position of Y axis.",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
'Z': WordType( 'Z': WordType(
cls=float, cls=CLASS_NUMBER,
value_regex=REGEX_FLOAT, value_regex=REGEX_NUMBER,
description="Absolute or incremental position of Z axis.", description="Absolute or incremental position of Z axis.",
clean_value=CLEAN_FLOAT, clean_value=CLEAN_NUMBER,
), ),
} }

View File

@ -42,12 +42,12 @@ from .exceptions import GCodeParameterError, GCodeWordStrError
# #
# There are 15 groups: # There are 15 groups:
# ref: http://linuxcnc.org/docs/html/gcode/overview.html#_modal_groups # ref: http://linuxcnc.org/docs/html/gcode/overview.html#_modal_groups
# ref: https://www.haascnc.com/content/dam/haascnc/en/service/manual/operator/english---mill-ngc---operator's-manual---2017.pdf
# #
# Table 5. G-Code Modal Groups # Table 5. G-Code Modal Groups
# MODAL GROUP MEANING MEMBER WORDS # MODAL GROUP MEANING MEMBER WORDS
# Non-modal codes (Group 0) G4, G10 G28, G30, G53, G92, G92.1, G92.2, G92.3, # Non-modal codes (Group 0) G4, G10 G28, G30, G53, G92, G92.1, G92.2, G92.3,
# Motion (Group 1) G0, G1, G2, G3, G33, G38.x, G73, G76, G80, G81 # Motion (Group 1) G0, G1, G2, G3, G33, G38.x,
# G82, G83, G84, G85, G86, G87, G88,G89
# Plane selection (Group 2) G17, G18, G19, G17.1, G18.1, G19.1 # Plane selection (Group 2) G17, G18, G19, G17.1, G18.1, G19.1
# Distance Mode (Group 3) G90, G91 # Distance Mode (Group 3) G90, G91
# Arc IJK Distance Mode (Group 4) G90.1, G91.1 # Arc IJK Distance Mode (Group 4) G90.1, G91.1
@ -55,6 +55,8 @@ from .exceptions import GCodeParameterError, GCodeWordStrError
# Units (Group 6) G20, G21 # Units (Group 6) G20, G21
# Cutter Diameter Compensation (Group 7) G40, G41, G42, G41.1, G42.1 # Cutter Diameter Compensation (Group 7) G40, G41, G42, G41.1, G42.1
# Tool Length Offset (Group 8) G43, G43.1, G49 # Tool Length Offset (Group 8) G43, G43.1, G49
# Can Cycles (Group 9) G73, G76, G80, G81, G82, G83,
# G84, G85, G86, G87, G88, G89
# Canned Cycles Return Mode (Group 10) G98, G99 # Canned Cycles Return Mode (Group 10) G98, G99
# Coordinate System (Group 12) G54, G55, G56, G57, G58, G59, # Coordinate System (Group 12) G54, G55, G56, G57, G58, G59,
# G59.1, G59.2, G59.3 # G59.1, G59.2, G59.3
@ -81,6 +83,7 @@ MODAL_GROUP_MAP = {
'units': 6, 'units': 6,
'cutter_diameter_comp': 7, 'cutter_diameter_comp': 7,
'tool_length_offset': 8, 'tool_length_offset': 8,
'canned_cycle': 9,
'canned_cycles_return': 10, 'canned_cycles_return': 10,
'coordinate_system': 12, 'coordinate_system': 12,
'control_mode': 13, 'control_mode': 13,
@ -393,7 +396,6 @@ class GCodeProgramName(GCodeDefinition):
# G38.2 - G38.5 Straight Probe # G38.2 - G38.5 Straight Probe
# G33 K Spindle Synchronized Motion # G33 K Spindle Synchronized Motion
# G33.1 K Rigid Tapping # G33.1 K Rigid Tapping
# G80 Cancel Canned Cycle
class GCodeMotion(GCode): class GCodeMotion(GCode):
param_letters = set('XYZABCUVW') param_letters = set('XYZABCUVW')
@ -509,21 +511,6 @@ class GCodeRigidTapping(GCodeMotion):
word_key = Word('G', 33.1) word_key = Word('G', 33.1)
class GCodeCancelCannedCycle(GCodeMotion):
"""G80: Cancel Canned Cycle"""
word_key = Word('G', 80)
# Modal Group
# Technically G80 belongs to the motion modal group, however it's often
# expressed in the same line as another motion command.
# This is alowed, but executed just prior to any other motion command
# eg: G00 G80
# will leave the machine in rapid motion mode
# Just running G80 will leave machine with no motion mode.
modal_group = None
exec_order = 241
def _process(self, machine):
machine.mode.motion = None
# ======================= Canned Cycles ======================= # ======================= Canned Cycles =======================
@ -536,10 +523,11 @@ class GCodeCancelCannedCycle(GCodeMotion):
# G85 R L (P) Boring Cycle, Feed Out # G85 R L (P) Boring Cycle, Feed Out
# G89 R L (P) Boring Cycle, Dwell, Feed Out # G89 R L (P) Boring Cycle, Dwell, Feed Out
# G76 P Z I J R K Q H L E Threading Cycle # G76 P Z I J R K Q H L E Threading Cycle
# G80 Cancel Canned Cycle
class GCodeCannedCycle(GCode): class GCodeCannedCycle(GCode):
param_letters = set('XYZUVW') param_letters = set('XYZUVW')
modal_group = MODAL_GROUP_MAP['motion'] modal_group = MODAL_GROUP_MAP['canned_cycle']
exec_order = 242 exec_order = 242
def _process(self, machine): def _process(self, machine):
@ -554,11 +542,12 @@ class GCodeCannedCycle(GCode):
moveto_coords.pop(machine.mode.plane_selection.normal_axis, None) moveto_coords.pop(machine.mode.plane_selection.normal_axis, None)
# Process action 'L' times # Process action 'L' times
loop_count = self.L if hasattr(self, 'L'):
if (loop_count is None) or (loop_count <= 0): loop_count = self.L
loop_count = 1 if (loop_count is None) or (loop_count <= 0):
for i in range(loop_count): loop_count = 1
machine.move_to(**moveto_coords) for i in range(loop_count):
machine.move_to(**moveto_coords)
class GCodeDrillingCycle(GCodeCannedCycle): class GCodeDrillingCycle(GCodeCannedCycle):
@ -577,7 +566,7 @@ class GCodeDrillingCycleDwell(GCodeCannedCycle):
class GCodeDrillingCyclePeck(GCodeCannedCycle): class GCodeDrillingCyclePeck(GCodeCannedCycle):
"""G83: Drilling Cycle, Peck""" """G83: Drilling Cycle, Peck"""
param_letters = GCodeCannedCycle.param_letters | set('RLQ') param_letters = GCodeCannedCycle.param_letters | set('RLQ') | set('IJK')
word_key = Word('G', 83) word_key = Word('G', 83)
modal_param_letters = GCodeCannedCycle.param_letters | set('RQ') modal_param_letters = GCodeCannedCycle.param_letters | set('RQ')
@ -608,6 +597,19 @@ class GCodeThreadingCycle(GCodeCannedCycle):
param_letters = GCodeCannedCycle.param_letters | set('PZIJRKQHLE') param_letters = GCodeCannedCycle.param_letters | set('PZIJRKQHLE')
word_key = Word('G', 76) word_key = Word('G', 76)
class GCodeCancelCannedCycle(GCodeCannedCycle):
""" G80: Cancel Canned Cycle """
param_letters = set()
word_key = Word('G', 80)
# Modal Group
# Technically G80 belongs to the motion modal group, however it's often
# expressed in the same line as another motion command.
# This is alowed, but executed just prior to any other motion command
# eg: G00 G80
# will leave the machine in rapid motion mode
# Just running G80 will leave machine with no motion mode.
exec_order = 241
# ======================= Distance Mode ======================= # ======================= Distance Mode =======================
# CODE PARAMETERS DESCRIPTION # CODE PARAMETERS DESCRIPTION

View File

@ -268,6 +268,14 @@ class Mode(object):
# > $G # > $G
# > [GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0] # > [GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0]
# ref: https://github.com/gnea/grbl/wiki/Grbl-v1.1-Commands#g---view-gcode-parser-state # ref: https://github.com/gnea/grbl/wiki/Grbl-v1.1-Commands#g---view-gcode-parser-state
# NOTE : there is no default mode for some machines (eg haas), so if the
# previous program leaves the machine in G91 it will remain in G91 at the
# beginning of the next program.. it is good practice to start programs
# with a few "safe startup" blocks for example
# G21 ( metric )
# G0 G17 G40 G49 G80 G90
# G54 ( set wcs )
default_mode = ''' default_mode = '''
G0 (movement: rapid) G0 (movement: rapid)
G17 (plane_selection: X/Y plane) G17 (plane_selection: X/Y plane)
@ -407,6 +415,10 @@ class Machine(object):
self.state.cur_coord_sys = coord_sys_mode.coord_system_id self.state.cur_coord_sys = coord_sys_mode.coord_system_id
# TODO: convert coord systems between inches/mm, G20/G21 respectively # TODO: convert coord systems between inches/mm, G20/G21 respectively
# NOTE : on at least a Haas -- this cannot be changed when running a
# program -- the G20 / G21 codes don't actually change units, but if
# a G20 / G21 appears in the code and the machine settings do not match
# an error will be thrown.
def modal_gcode(self, modal_params): def modal_gcode(self, modal_params):
""" """