From cc75fc9df95fc4257a4b2b0ccd4449fedd2f45fc Mon Sep 17 00:00:00 2001 From: Walker Inman Date: Thu, 25 Jul 2019 23:48:43 -0400 Subject: [PATCH] minimally change values and codes --- src/pygcode/dialects/linuxcnc.py | 149 ++++++++++++++++--------------- 1 file changed, 76 insertions(+), 73 deletions(-) diff --git a/src/pygcode/dialects/linuxcnc.py b/src/pygcode/dialects/linuxcnc.py index ff72221..68932a7 100644 --- a/src/pygcode/dialects/linuxcnc.py +++ b/src/pygcode/dialects/linuxcnc.py @@ -18,25 +18,28 @@ from .utils import WordType # ======================== WORDS ======================== -REGEX_FLOAT = re.compile(r'^\s*-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float -REGEX_INT = re.compile(r'^\s*-?\d+') +REGEX_NUMBER = re.compile(r'^\s*-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float REGEX_POSITIVEINT = re.compile(r'^\s*\d+') REGEX_CODE = re.compile(r'^\s*\d+(\.\d)?') # float, but can't be negative -# Value cleaning functions -def _clean_codestr(value): - if value < 10: - return "0%g" % value - return "%g" % value +def CLASS_NUMBER(value): + if isinstance(value, (int, float)): + return value + if '.' in value: + return float(value) + return int(value) +# Value cleaning functions CLEAN_NONE = lambda v: v -def CLEAN_FLOAT(v): +def CLEAN_NUMBER(v): + if isinstance(v, int): + return str(v) fstr = "{0:g}".format(round(v, 3)) if '.' not in fstr: return fstr + '.' return fstr -CLEAN_CODE = _clean_codestr +CLEAN_CODE = lambda v: '{:02}'.format(v) CLEAN_INT = lambda v: "%g" % v WORD_MAP = { @@ -45,74 +48,74 @@ WORD_MAP = { # Rotational Axes 'A': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Absolute or incremental position of A axis (rotational axis around X axis)", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'B': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Absolute or incremental position of B axis (rotational axis around Y axis)", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'C': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Absolute or incremental position of C axis (rotational axis around Z axis)", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'D': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + 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.", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), # Feed Rates 'E': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Precision feedrate for threading on lathes", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'F': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Feedrate", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), # G-Codes 'G': WordType( - cls=float, + cls=CLASS_NUMBER, value_regex=REGEX_CODE, description="Address for preparatory commands", - clean_value=CLEAN_CODE, + clean_value=CLEAN_NONE, ), # Tool Offsets 'H': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, 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 'I': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + 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.", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'J': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + 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.", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'K': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + 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.", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), # Loop Count 'L': WordType( @@ -123,10 +126,10 @@ WORD_MAP = { ), # Miscellaneous Function 'M': WordType( - cls=float, + cls=CLASS_NUMBER, value_regex=REGEX_CODE, description="Miscellaneous function", - clean_value=CLEAN_CODE, + clean_value=CLEAN_NONE, ), # Line Number 'N': WordType( @@ -144,31 +147,31 @@ WORD_MAP = { ), # Parameter (arbitrary parameter) 'P': WordType( - cls=float, # parameter is often an integer, but can be a float - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, # parameter is often an integer, but can be a float + value_regex=REGEX_NUMBER, description="Serves as parameter address for various G and M codes", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), # Peck increment 'Q': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Depth to increase on each peck; Peck increment in canned cycles", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), # Arc Radius 'R': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Defines size of arc radius, or defines retract height in milling canned cycles", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), # Spindle speed 'S': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Defines speed, either spindle speed or surface speed depending on mode", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), # Tool Selecton 'T': WordType( @@ -179,41 +182,41 @@ WORD_MAP = { ), # Incremental axes 'U': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + 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').", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'V': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Incremental axis corresponding to Y axis", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'W': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Incremental axis corresponding to Z axis (typically only lathe group A controls)", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), # Linear Axes 'X': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Absolute or incremental position of X axis.", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'Y': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Absolute or incremental position of Y axis.", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), 'Z': WordType( - cls=float, - value_regex=REGEX_FLOAT, + cls=CLASS_NUMBER, + value_regex=REGEX_NUMBER, description="Absolute or incremental position of Z axis.", - clean_value=CLEAN_FLOAT, + clean_value=CLEAN_NUMBER, ), }