minimally change values and codes

This commit is contained in:
Walker Inman 2019-07-25 23:48:43 -04:00
parent 1729aeb9be
commit cc75fc9df9

View File

@ -18,25 +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
def CLEAN_FLOAT(v): def CLEAN_NUMBER(v):
if isinstance(v, int):
return str(v)
fstr = "{0:g}".format(round(v, 3)) fstr = "{0:g}".format(round(v, 3))
if '.' not in fstr: if '.' not in fstr:
return fstr + '.' return fstr + '.'
return fstr return fstr
CLEAN_CODE = _clean_codestr CLEAN_CODE = lambda v: '{:02}'.format(v)
CLEAN_INT = lambda v: "%g" % v CLEAN_INT = lambda v: "%g" % v
WORD_MAP = { WORD_MAP = {
@ -45,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(
@ -123,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(
@ -144,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(
@ -179,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,
), ),
} }