Skip to content

Commit cc75fc9

Browse files
committed
minimally change values and codes
1 parent 1729aeb commit cc75fc9

File tree

1 file changed

+76
-73
lines changed

1 file changed

+76
-73
lines changed

src/pygcode/dialects/linuxcnc.py

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@
1818

1919
# ======================== WORDS ========================
2020

21-
REGEX_FLOAT = re.compile(r'^\s*-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float
22-
REGEX_INT = re.compile(r'^\s*-?\d+')
21+
REGEX_NUMBER = re.compile(r'^\s*-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float
2322
REGEX_POSITIVEINT = re.compile(r'^\s*\d+')
2423
REGEX_CODE = re.compile(r'^\s*\d+(\.\d)?') # float, but can't be negative
2524

26-
# Value cleaning functions
27-
def _clean_codestr(value):
28-
if value < 10:
29-
return "0%g" % value
30-
return "%g" % value
25+
def CLASS_NUMBER(value):
26+
if isinstance(value, (int, float)):
27+
return value
28+
if '.' in value:
29+
return float(value)
30+
return int(value)
3131

32+
# Value cleaning functions
3233
CLEAN_NONE = lambda v: v
3334

34-
def CLEAN_FLOAT(v):
35+
def CLEAN_NUMBER(v):
36+
if isinstance(v, int):
37+
return str(v)
3538
fstr = "{0:g}".format(round(v, 3))
3639
if '.' not in fstr:
3740
return fstr + '.'
3841
return fstr
39-
CLEAN_CODE = _clean_codestr
42+
CLEAN_CODE = lambda v: '{:02}'.format(v)
4043
CLEAN_INT = lambda v: "%g" % v
4144

4245
WORD_MAP = {
@@ -45,74 +48,74 @@ def CLEAN_FLOAT(v):
4548

4649
# Rotational Axes
4750
'A': WordType(
48-
cls=float,
49-
value_regex=REGEX_FLOAT,
51+
cls=CLASS_NUMBER,
52+
value_regex=REGEX_NUMBER,
5053
description="Absolute or incremental position of A axis (rotational axis around X axis)",
51-
clean_value=CLEAN_FLOAT,
54+
clean_value=CLEAN_NUMBER,
5255
),
5356
'B': WordType(
54-
cls=float,
55-
value_regex=REGEX_FLOAT,
57+
cls=CLASS_NUMBER,
58+
value_regex=REGEX_NUMBER,
5659
description="Absolute or incremental position of B axis (rotational axis around Y axis)",
57-
clean_value=CLEAN_FLOAT,
60+
clean_value=CLEAN_NUMBER,
5861
),
5962
'C': WordType(
60-
cls=float,
61-
value_regex=REGEX_FLOAT,
63+
cls=CLASS_NUMBER,
64+
value_regex=REGEX_NUMBER,
6265
description="Absolute or incremental position of C axis (rotational axis around Z axis)",
63-
clean_value=CLEAN_FLOAT,
66+
clean_value=CLEAN_NUMBER,
6467
),
6568
'D': WordType(
66-
cls=float,
67-
value_regex=REGEX_FLOAT,
69+
cls=CLASS_NUMBER,
70+
value_regex=REGEX_NUMBER,
6871
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.",
69-
clean_value=CLEAN_FLOAT,
72+
clean_value=CLEAN_NUMBER,
7073
),
7174
# Feed Rates
7275
'E': WordType(
73-
cls=float,
74-
value_regex=REGEX_FLOAT,
76+
cls=CLASS_NUMBER,
77+
value_regex=REGEX_NUMBER,
7578
description="Precision feedrate for threading on lathes",
76-
clean_value=CLEAN_FLOAT,
79+
clean_value=CLEAN_NUMBER,
7780
),
7881
'F': WordType(
79-
cls=float,
80-
value_regex=REGEX_FLOAT,
82+
cls=CLASS_NUMBER,
83+
value_regex=REGEX_NUMBER,
8184
description="Feedrate",
82-
clean_value=CLEAN_FLOAT,
85+
clean_value=CLEAN_NUMBER,
8386
),
8487
# G-Codes
8588
'G': WordType(
86-
cls=float,
89+
cls=CLASS_NUMBER,
8790
value_regex=REGEX_CODE,
8891
description="Address for preparatory commands",
89-
clean_value=CLEAN_CODE,
92+
clean_value=CLEAN_NONE,
9093
),
9194
# Tool Offsets
9295
'H': WordType(
93-
cls=float,
94-
value_regex=REGEX_FLOAT,
96+
cls=CLASS_NUMBER,
97+
value_regex=REGEX_NUMBER,
9598
description="Defines tool length offset; Incremental axis corresponding to C axis (e.g., on a turn-mill)",
96-
clean_value=CLEAN_FLOAT,
99+
clean_value=CLEAN_NUMBER,
97100
),
98101
# Arc radius center coords
99102
'I': WordType(
100-
cls=float,
101-
value_regex=REGEX_FLOAT,
103+
cls=CLASS_NUMBER,
104+
value_regex=REGEX_NUMBER,
102105
description="Defines arc center in X axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.",
103-
clean_value=CLEAN_FLOAT,
106+
clean_value=CLEAN_NUMBER,
104107
),
105108
'J': WordType(
106-
cls=float,
107-
value_regex=REGEX_FLOAT,
109+
cls=CLASS_NUMBER,
110+
value_regex=REGEX_NUMBER,
108111
description="Defines arc center in Y axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.",
109-
clean_value=CLEAN_FLOAT,
112+
clean_value=CLEAN_NUMBER,
110113
),
111114
'K': WordType(
112-
cls=float,
113-
value_regex=REGEX_FLOAT,
115+
cls=CLASS_NUMBER,
116+
value_regex=REGEX_NUMBER,
114117
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.",
115-
clean_value=CLEAN_FLOAT,
118+
clean_value=CLEAN_NUMBER,
116119
),
117120
# Loop Count
118121
'L': WordType(
@@ -123,10 +126,10 @@ def CLEAN_FLOAT(v):
123126
),
124127
# Miscellaneous Function
125128
'M': WordType(
126-
cls=float,
129+
cls=CLASS_NUMBER,
127130
value_regex=REGEX_CODE,
128131
description="Miscellaneous function",
129-
clean_value=CLEAN_CODE,
132+
clean_value=CLEAN_NONE,
130133
),
131134
# Line Number
132135
'N': WordType(
@@ -144,31 +147,31 @@ def CLEAN_FLOAT(v):
144147
),
145148
# Parameter (arbitrary parameter)
146149
'P': WordType(
147-
cls=float, # parameter is often an integer, but can be a float
148-
value_regex=REGEX_FLOAT,
150+
cls=CLASS_NUMBER, # parameter is often an integer, but can be a float
151+
value_regex=REGEX_NUMBER,
149152
description="Serves as parameter address for various G and M codes",
150-
clean_value=CLEAN_FLOAT,
153+
clean_value=CLEAN_NUMBER,
151154
),
152155
# Peck increment
153156
'Q': WordType(
154-
cls=float,
155-
value_regex=REGEX_FLOAT,
157+
cls=CLASS_NUMBER,
158+
value_regex=REGEX_NUMBER,
156159
description="Depth to increase on each peck; Peck increment in canned cycles",
157-
clean_value=CLEAN_FLOAT,
160+
clean_value=CLEAN_NUMBER,
158161
),
159162
# Arc Radius
160163
'R': WordType(
161-
cls=float,
162-
value_regex=REGEX_FLOAT,
164+
cls=CLASS_NUMBER,
165+
value_regex=REGEX_NUMBER,
163166
description="Defines size of arc radius, or defines retract height in milling canned cycles",
164-
clean_value=CLEAN_FLOAT,
167+
clean_value=CLEAN_NUMBER,
165168
),
166169
# Spindle speed
167170
'S': WordType(
168-
cls=float,
169-
value_regex=REGEX_FLOAT,
171+
cls=CLASS_NUMBER,
172+
value_regex=REGEX_NUMBER,
170173
description="Defines speed, either spindle speed or surface speed depending on mode",
171-
clean_value=CLEAN_FLOAT,
174+
clean_value=CLEAN_NUMBER,
172175
),
173176
# Tool Selecton
174177
'T': WordType(
@@ -179,41 +182,41 @@ def CLEAN_FLOAT(v):
179182
),
180183
# Incremental axes
181184
'U': WordType(
182-
cls=float,
183-
value_regex=REGEX_FLOAT,
185+
cls=CLASS_NUMBER,
186+
value_regex=REGEX_NUMBER,
184187
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').",
185-
clean_value=CLEAN_FLOAT,
188+
clean_value=CLEAN_NUMBER,
186189
),
187190
'V': WordType(
188-
cls=float,
189-
value_regex=REGEX_FLOAT,
191+
cls=CLASS_NUMBER,
192+
value_regex=REGEX_NUMBER,
190193
description="Incremental axis corresponding to Y axis",
191-
clean_value=CLEAN_FLOAT,
194+
clean_value=CLEAN_NUMBER,
192195
),
193196
'W': WordType(
194-
cls=float,
195-
value_regex=REGEX_FLOAT,
197+
cls=CLASS_NUMBER,
198+
value_regex=REGEX_NUMBER,
196199
description="Incremental axis corresponding to Z axis (typically only lathe group A controls)",
197-
clean_value=CLEAN_FLOAT,
200+
clean_value=CLEAN_NUMBER,
198201
),
199202
# Linear Axes
200203
'X': WordType(
201-
cls=float,
202-
value_regex=REGEX_FLOAT,
204+
cls=CLASS_NUMBER,
205+
value_regex=REGEX_NUMBER,
203206
description="Absolute or incremental position of X axis.",
204-
clean_value=CLEAN_FLOAT,
207+
clean_value=CLEAN_NUMBER,
205208
),
206209
'Y': WordType(
207-
cls=float,
208-
value_regex=REGEX_FLOAT,
210+
cls=CLASS_NUMBER,
211+
value_regex=REGEX_NUMBER,
209212
description="Absolute or incremental position of Y axis.",
210-
clean_value=CLEAN_FLOAT,
213+
clean_value=CLEAN_NUMBER,
211214
),
212215
'Z': WordType(
213-
cls=float,
214-
value_regex=REGEX_FLOAT,
216+
cls=CLASS_NUMBER,
217+
value_regex=REGEX_NUMBER,
215218
description="Absolute or incremental position of Z axis.",
216-
clean_value=CLEAN_FLOAT,
219+
clean_value=CLEAN_NUMBER,
217220
),
218221
}
219222

0 commit comments

Comments
 (0)