Skip to content

Commit 2698cc7

Browse files
committed
interpeter: allow comments that print params, to set the float format
printting out number to users often uses excessive decimals. now &d,%f or %.xf (x being a number) is interpeted to be a float format for params. -add test for printing formatted params -add docs on formatting of parameter numbers in comments
1 parent 4eee9ab commit 2698cc7

5 files changed

Lines changed: 120 additions & 2 deletions

File tree

docs/src/gcode/overview.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,26 @@ are replaced by the value of the named parameter. Named parameters
10751075
will have white space removed from them. So, '\#<named parameter>'
10761076
will be converted to '#<namedparameter>'.
10771077

1078+
Parameter numbers can be formatted. eg:
1079+
1080+
----
1081+
(DEBUG, value = %d#<some_value>)
1082+
----
1083+
will print the value rounded to an integer.
1084+
1085+
* %lf is default if there is no formatting string
1086+
* %d = 0 decimals
1087+
* %f = four decimals
1088+
* %.xf = x (0-9) number of decimals
1089+
1090+
The formatting will be preformed on all parameters in the same line unless
1091+
changed. ie. multiple formatting is allowed in one line.
1092+
1093+
The formatting string does not need to be right beside the parameter.
1094+
1095+
If the formatting string is created with the wrong pattern it will be
1096+
printed as characters.
1097+
10781098
[[gcode:file-requirements]]
10791099
== File Requirements(((File Requirements)))
10801100

src/emc/rs274ngc/interp_convert.cc

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ int Interp::convert_param_comment(char *comment, char *expanded, int len)
12771277
FORCE_LC_NUMERIC_C;
12781278
int i;
12791279
char param[LINELEN+1];
1280+
char format[5] = "%lf";
12801281
int paramNumber;
12811282
int stat;
12821283
double value;
@@ -1286,7 +1287,65 @@ int Interp::convert_param_comment(char *comment, char *expanded, int len)
12861287

12871288
while(*comment)
12881289
{
1289-
if(*comment == '#')
1290+
1291+
if(*comment == '%')
1292+
{
1293+
// skip over the '%'
1294+
comment++;
1295+
1296+
// convenient integer looking
1297+
if(*comment == 'd')
1298+
{
1299+
comment++;
1300+
strcpy(format, "%.0f");
1301+
}
1302+
// convenient 4 position float
1303+
else if(*comment == 'f')
1304+
{
1305+
comment++;
1306+
strcpy(format, "%.4f");
1307+
}
1308+
// arbitrary 0-9 position float
1309+
else if(*comment == '.')
1310+
{
1311+
comment++;
1312+
if(isdigit(*comment))
1313+
{
1314+
// forward to the (hopefully) letter f
1315+
comment++;
1316+
if(*comment == 'f')
1317+
{
1318+
// back up to get the digit into format
1319+
comment--;
1320+
format[0] = '%';
1321+
format[1] = '.';
1322+
format[2] = *comment;
1323+
format[3] = 'f';
1324+
format[4] = 0;
1325+
comment++;
1326+
comment++;
1327+
}
1328+
else
1329+
{
1330+
// not a format string so,
1331+
// back up to the digit to continue processing
1332+
comment--;
1333+
*expanded++ = '.';
1334+
1335+
}
1336+
}
1337+
else
1338+
{
1339+
*expanded++ = '.';
1340+
}
1341+
1342+
}
1343+
else
1344+
{
1345+
*expanded++ = '%';
1346+
}
1347+
}
1348+
else if(*comment == '#')
12901349
{
12911350
found = 0;
12921351
logDebug("a parameter");
@@ -1378,7 +1437,7 @@ int Interp::convert_param_comment(char *comment, char *expanded, int len)
13781437
{
13791438
// avoid -0.0/0.0 issues
13801439
double pvalue = equal(value, 0.0) ? 0.0 : value;
1381-
int n = snprintf(valbuf, VAL_LEN, "%lf", pvalue);
1440+
int n = snprintf(valbuf, VAL_LEN, format, pvalue);
13821441
bool fail = (n >= VAL_LEN || n < 0);
13831442
if(fail)
13841443
rtapi_strxcpy(valbuf, "######");
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
N..... USE_LENGTH_UNITS(CANON_UNITS_MM)
2+
N..... SET_G5X_OFFSET(1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000)
3+
N..... SET_G92_OFFSET(0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000)
4+
N..... SET_XY_ROTATION(0.0000)
5+
N..... SET_FEED_REFERENCE(CANON_XYZ)
6+
N..... ON_RESET()
7+
N..... MESSAGE(" native value = 1.123457")
8+
N..... MESSAGE(" Test round to integer: 1")
9+
N..... MESSAGE(" Test round to 4 decimals: 1.1235")
10+
N..... MESSAGE(" Test format separate from param: The value is: 1.1235")
11+
N..... MESSAGE(" Test round to 7 decimals: 1.1234568")
12+
N..... MESSAGE(" native value2 = 2.345679")
13+
N..... MESSAGE(" Test2 round all params in line to 4 decimals: The values are: 1.1235, 2.3457")
14+
N..... MESSAGE(" Test2 only round last value to integer: The values are: 1.123457, 2")
15+
N..... SET_G5X_OFFSET(1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000)
16+
N..... SET_XY_ROTATION(0.0000)
17+
N..... SET_FEED_MODE(0, 0)
18+
N..... SET_FEED_RATE(0.0000)
19+
N..... STOP_SPINDLE_TURNING(0)
20+
N..... SET_SPINDLE_MODE(0 0.0000)
21+
N..... PROGRAM_END()
22+
N..... ON_RESET()
23+
N..... ON_RESET()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#<value> = 1.123456789
2+
3+
(DEBUG, native value = #<value>)
4+
(DEBUG, Test round to integer: %d#<value>)
5+
(DEBUG, Test round to 4 decimals: %f#<value>)
6+
(DEBUG, Test format separate from param: %f The value is: #<value>)
7+
(DEBUG, Test round to 7 decimals: %.7f#<value>)
8+
9+
#<value2> = 2.3456789
10+
(DEBUG, native value2 = #<value2>)
11+
(DEBUG, Test2 round all params in line to 4 decimals: %f The values are: #<value>, #<value2>)
12+
(DEBUG, Test2 only round last value to integer: The values are: #<value>, %d#<value2>)
13+
M2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
rs274 -g test.ngc | awk '{$1=""; print}'
3+
exit ${PIPESTATUS[0]}

0 commit comments

Comments
 (0)