Skip to content

Commit 52f9a4f

Browse files
committed
GCode-based drivers: deduplicate code, remove spaces from output
1 parent a3b75ba commit 52f9a4f

File tree

6 files changed

+254
-263
lines changed

6 files changed

+254
-263
lines changed

src/main/java/de/thomas_oster/liblasercut/drivers/GenericGcodeDriver.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,16 +508,19 @@ protected void setFocus(PrintStream out, double focus) throws IOException {
508508
protected void move(PrintStream out, double x, double y, double resolution) throws IOException {
509509
x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution);
510510
y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution);
511-
currentSpeed = getTravel_speed();
511+
512+
String append = "";
512513
if (blankLaserDuringRapids)
513514
{
514515
currentPower = Double.NaN;
515-
sendLine("G0 X%f Y%f F%d S0", x, y, (int) (travel_speed));
516+
append = " S0";
516517
}
517-
else
518+
if (isSendFeedDuringRapids())
518519
{
519-
sendLine("G0 X%f Y%f F%d", x, y, (int) (travel_speed));
520+
currentSpeed = getTravel_speed();
521+
append += String.format(FORMAT_LOCALE, " F%f", currentSpeed);
520522
}
523+
sendLine("G0 X%f Y%f " + append, x, y, (int) (travel_speed));
521524
}
522525

523526
protected void line(PrintStream out, double x, double y, double resolution) throws IOException {
@@ -568,6 +571,11 @@ private void writeShutdownCode() throws IOException {
568571

569572
protected void sendLine(String text, Object... parameters) throws IOException
570573
{
574+
if (text.startsWith("G0") || text.startsWith("G1")) {
575+
// Remove spaces from all standard moves.
576+
// Leave all other commands unchanged because they could be user-specific display messages from the init code.
577+
text = text.replace(" ", "");
578+
}
571579
out.format(FORMAT_LOCALE, text+LINEEND(), parameters);
572580
out.flush();
573581
if (isWaitForOKafterEachLine())
@@ -1246,4 +1254,16 @@ public GenericGcodeDriver clone() {
12461254
return clone;
12471255
}
12481256

1257+
/**
1258+
* Send the F (travelFeed) during G0 rapid moves?
1259+
*
1260+
* true: Yes. Best for compatibility.
1261+
* false: No. Shorter G-Code and still works for standards-compliant G-Code interpreters. (Note that G0 means "move as fast as possible", so specifying a feed-speed makes no sense.)
1262+
*/
1263+
// TODO: make configurable in the future
1264+
protected boolean isSendFeedDuringRapids()
1265+
{
1266+
return true;
1267+
}
1268+
12491269
}

src/main/java/de/thomas_oster/liblasercut/drivers/Grbl.java

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ public String getModelName()
114114
protected void sendLineWithoutWait(String text, Object... parameters) throws IOException
115115
{
116116
boolean wasSetWaitingForOk = isWaitForOKafterEachLine();
117-
setWaitForOKafterEachLine(false);
118-
sendLine(text, parameters);
119-
setWaitForOKafterEachLine(wasSetWaitingForOk);
117+
try {
118+
setWaitForOKafterEachLine(false);
119+
sendLine(text, parameters);
120+
} finally {
121+
setWaitForOKafterEachLine(wasSetWaitingForOk);
122+
}
120123
}
121124

122125
/**
@@ -158,45 +161,13 @@ protected String waitForIdentificationLine(ProgressListener pl) throws IOExcepti
158161
return null;
159162
}
160163

161-
/**
162-
* Send a G0 rapid move to Grbl.
163-
* Doesn't include travel speed since grbl ignores that anyway.
164-
*/
165-
@Override
166-
protected void move(PrintStream out, double x, double y, double resolution) throws IOException {
167-
x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution);
168-
y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution);
169-
currentSpeed = getTravel_speed();
170-
if (blankLaserDuringRapids)
171-
{
172-
currentPower = 0.0;
173-
sendLine("G0 X%f Y%f S0", x, y);
174-
}
175-
else
176-
{
177-
sendLine("G0 X%f Y%f", x, y);
178-
}
179-
}
180-
181-
/**
182-
* Send a line of gcode to the cutter, stripping out any whitespace in the process
183-
*/
184164
@Override
185-
protected void sendLine(String text, Object... parameters) throws IOException
165+
protected boolean isSendFeedDuringRapids()
186166
{
187-
out.format(FORMAT_LOCALE, text.replace(" ", "")+LINEEND(), parameters);
188-
// System.out.println(String.format(FORMAT_LOCALE, "> "+text+LINEEND(), parameters));
189-
out.flush();
190-
if (isWaitForOKafterEachLine())
191-
{
192-
String line = waitForLine();
193-
if (!"ok".equals(line))
194-
{
195-
throw new IOException("Lasercutter did not respond 'ok', but '"+line+"'instead.");
196-
}
197-
}
167+
// Grbl ignores the F parameter during G0.
168+
return false;
198169
}
199-
170+
200171
@Override
201172
public Grbl clone()
202173
{
Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
11
G21
22
G90
3-
G0 Z0.000000
4-
G0 X0.508000 Y0.508000 F3600
5-
G1 X25.400000 Y50.800000 S1.000000 F1200
6-
G1 X50.800000 Y0.000000
7-
G0 X101.600000 Y0.000000 F3600
8-
G1 X76.200000 Y5.080000 F1200
9-
G1 X76.149200 Y20.320000
10-
G1 X76.149200 Y22.860000
11-
G1 X76.098400 Y25.400000
12-
G1 X76.149200 Y30.480000
13-
G1 X76.200000 Y45.720000
14-
G1 X101.600000 Y50.800000
15-
G0 X0.000000 Y1.879600 F3600
16-
G1 X0.660400 Y1.879600 S0.000000 F1200
17-
G1 X0.762000 Y1.879600 S1.000000
18-
G1 X0.863600 Y1.879600
19-
G1 X0.914400 Y1.879600
20-
G1 X1.066800 Y1.879600
21-
G1 X1.117600 Y1.879600
22-
G1 X6.096000 Y1.879600 S0.000000
23-
G0 X0.000000 Y1.930400 F3600
24-
G1 X0.660400 Y1.930400 F1200
25-
G1 X0.812800 Y1.930400 S1.000000
26-
G1 X0.863600 Y1.930400
27-
G1 X0.965200 Y1.930400
28-
G1 X1.066800 Y1.930400
29-
G1 X1.117600 Y1.930400
30-
G1 X6.096000 Y1.930400 S0.000000
31-
G0 X0.000000 Y1.981200 F3600
32-
G1 X0.660400 Y1.981200 F1200
33-
G1 X1.117600 Y1.981200 S1.000000
34-
G1 X6.096000 Y1.981200 S0.000000
35-
G0 X0.000000 Y2.032000 F3600
36-
G1 X0.660400 Y2.032000 F1200
37-
G1 X0.762000 Y2.032000 S1.000000
38-
G1 X1.117600 Y2.032000
39-
G1 X6.096000 Y2.032000 S0.000000
40-
G0 X0.000000 Y2.082800 F3600
41-
G1 X0.660400 Y2.082800 F1200
42-
G1 X1.117600 Y2.082800 S1.000000
43-
G1 X6.096000 Y2.082800 S0.000000
44-
G0 X0.000000 Y2.133600 F3600
45-
G1 X0.660400 Y2.133600 F1200
46-
G1 X0.762000 Y2.133600 S1.000000
47-
G1 X1.117600 Y2.133600
48-
G1 X6.096000 Y2.133600 S0.000000
49-
G0 X0.000000 Y3.911600 F3600
50-
G1 X2.235200 Y3.911600 F1200
51-
G1 X2.336800 Y3.911600 S1.000000
52-
G1 X2.387600 Y3.911600 S0.000000
53-
G1 X2.540000 Y3.911600 S1.000000
54-
G1 X7.518400 Y3.911600 S0.000000
55-
G0 X0.000000 Y3.962400 F3600
56-
G1 X2.286000 Y3.962400 F1200
57-
G1 X2.336800 Y3.962400 S1.000000
58-
G1 X2.438400 Y3.962400 S0.000000
59-
G1 X2.540000 Y3.962400 S1.000000
60-
G1 X7.518400 Y3.962400 S0.000000
61-
G0 X0.000000 Y4.013200 F3600
62-
G1 X2.133600 Y4.013200 F1200
63-
G1 X2.184400 Y4.013200 S0.750000
64-
G1 X2.235200 Y4.013200 S0.000000
65-
G1 X2.590800 Y4.013200 S0.750000
66-
G1 X7.569200 Y4.013200 S0.000000
67-
G0 X0.000000 Y4.064000 F3600
68-
G1 X2.235200 Y4.064000 F1200
69-
G1 X2.590800 Y4.064000 S0.500000
70-
G1 X7.569200 Y4.064000 S0.000000
71-
G0 X0.000000 Y4.165600 F3600
72-
G1 X2.133600 Y4.165600 F1200
73-
G1 X2.235200 Y4.165600 S1.000000
74-
G1 X7.213600 Y4.165600 S0.000000
75-
G0 X0 Y0
3+
G0Z0.000000
4+
G0X0.508000Y0.508000F3600.000000
5+
G1X25.400000Y50.800000S1.000000F1200
6+
G1X50.800000Y0.000000
7+
G0X101.600000Y0.000000F3600.000000
8+
G1X76.200000Y5.080000F1200
9+
G1X76.149200Y20.320000
10+
G1X76.149200Y22.860000
11+
G1X76.098400Y25.400000
12+
G1X76.149200Y30.480000
13+
G1X76.200000Y45.720000
14+
G1X101.600000Y50.800000
15+
G0X0.000000Y1.879600F3600.000000
16+
G1X0.660400Y1.879600S0.000000F1200
17+
G1X0.762000Y1.879600S1.000000
18+
G1X0.863600Y1.879600
19+
G1X0.914400Y1.879600
20+
G1X1.066800Y1.879600
21+
G1X1.117600Y1.879600
22+
G1X6.096000Y1.879600S0.000000
23+
G0X0.000000Y1.930400F3600.000000
24+
G1X0.660400Y1.930400F1200
25+
G1X0.812800Y1.930400S1.000000
26+
G1X0.863600Y1.930400
27+
G1X0.965200Y1.930400
28+
G1X1.066800Y1.930400
29+
G1X1.117600Y1.930400
30+
G1X6.096000Y1.930400S0.000000
31+
G0X0.000000Y1.981200F3600.000000
32+
G1X0.660400Y1.981200F1200
33+
G1X1.117600Y1.981200S1.000000
34+
G1X6.096000Y1.981200S0.000000
35+
G0X0.000000Y2.032000F3600.000000
36+
G1X0.660400Y2.032000F1200
37+
G1X0.762000Y2.032000S1.000000
38+
G1X1.117600Y2.032000
39+
G1X6.096000Y2.032000S0.000000
40+
G0X0.000000Y2.082800F3600.000000
41+
G1X0.660400Y2.082800F1200
42+
G1X1.117600Y2.082800S1.000000
43+
G1X6.096000Y2.082800S0.000000
44+
G0X0.000000Y2.133600F3600.000000
45+
G1X0.660400Y2.133600F1200
46+
G1X0.762000Y2.133600S1.000000
47+
G1X1.117600Y2.133600
48+
G1X6.096000Y2.133600S0.000000
49+
G0X0.000000Y3.911600F3600.000000
50+
G1X2.235200Y3.911600F1200
51+
G1X2.336800Y3.911600S1.000000
52+
G1X2.387600Y3.911600S0.000000
53+
G1X2.540000Y3.911600S1.000000
54+
G1X7.518400Y3.911600S0.000000
55+
G0X0.000000Y3.962400F3600.000000
56+
G1X2.286000Y3.962400F1200
57+
G1X2.336800Y3.962400S1.000000
58+
G1X2.438400Y3.962400S0.000000
59+
G1X2.540000Y3.962400S1.000000
60+
G1X7.518400Y3.962400S0.000000
61+
G0X0.000000Y4.013200F3600.000000
62+
G1X2.133600Y4.013200F1200
63+
G1X2.184400Y4.013200S0.750000
64+
G1X2.235200Y4.013200S0.000000
65+
G1X2.590800Y4.013200S0.750000
66+
G1X7.569200Y4.013200S0.000000
67+
G0X0.000000Y4.064000F3600.000000
68+
G1X2.235200Y4.064000F1200
69+
G1X2.590800Y4.064000S0.500000
70+
G1X7.569200Y4.064000S0.000000
71+
G0X0.000000Y4.165600F3600.000000
72+
G1X2.133600Y4.165600F1200
73+
G1X2.235200Y4.165600S1.000000
74+
G1X7.213600Y4.165600S0.000000
75+
G0X0Y0

test-output/de.thomas_oster.liblasercut.drivers.Grbl.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
G21G90M3G0Z0.000000G0X0.508000Y0.508000S0G1X25.400000Y50.800000S1000.000000F1200G1X50.800000Y0.000000G0X101.600000Y0.000000S0G1X76.200000Y5.080000S1000.000000F1200G1X76.149200Y20.320000G1X76.149200Y22.860000G1X76.098400Y25.400000G1X76.149200Y30.480000G1X76.200000Y45.720000G1X101.600000Y50.800000G0X0.000000Y1.879600S0G1X0.660400Y1.879600F1200G1X0.762000Y1.879600S1000.000000G1X0.863600Y1.879600G1X0.914400Y1.879600G1X1.066800Y1.879600G1X1.117600Y1.879600G1X6.096000Y1.879600S0.000000G0X0.000000Y1.930400S0G1X0.660400Y1.930400F1200G1X0.812800Y1.930400S1000.000000G1X0.863600Y1.930400G1X0.965200Y1.930400G1X1.066800Y1.930400G1X1.117600Y1.930400G1X6.096000Y1.930400S0.000000G0X0.000000Y1.981200S0G1X0.660400Y1.981200F1200G1X1.117600Y1.981200S1000.000000G1X6.096000Y1.981200S0.000000G0X0.000000Y2.032000S0G1X0.660400Y2.032000F1200G1X0.762000Y2.032000S1000.000000G1X1.117600Y2.032000G1X6.096000Y2.032000S0.000000G0X0.000000Y2.082800S0G1X0.660400Y2.082800F1200G1X1.117600Y2.082800S1000.000000G1X6.096000Y2.082800S0.000000G0X0.000000Y2.133600S0G1X0.660400Y2.133600F1200G1X0.762000Y2.133600S1000.000000G1X1.117600Y2.133600G1X6.096000Y2.133600S0.000000G0X0.000000Y3.911600S0G1X2.235200Y3.911600F1200G1X2.336800Y3.911600S1000.000000G1X2.387600Y3.911600S0.000000G1X2.540000Y3.911600S1000.000000G1X7.518400Y3.911600S0.000000G0X0.000000Y3.962400S0G1X2.286000Y3.962400F1200G1X2.336800Y3.962400S1000.000000G1X2.438400Y3.962400S0.000000G1X2.540000Y3.962400S1000.000000G1X7.518400Y3.962400S0.000000G0X0.000000Y4.013200S0G1X2.133600Y4.013200F1200G1X2.184400Y4.013200S750.000000G1X2.235200Y4.013200S0.000000G1X2.590800Y4.013200S750.000000G1X7.569200Y4.013200S0.000000G0X0.000000Y4.064000S0G1X2.235200Y4.064000F1200G1X2.590800Y4.064000S500.000000G1X7.569200Y4.064000S0.000000G0X0.000000Y4.165600S0G1X2.133600Y4.165600F1200G1X2.235200Y4.165600S1000.000000G1X7.213600Y4.165600S0.000000M5G0X0Y0
1+
G21G90M3G0Z0.000000G0X0.508000Y0.508000S0G1X25.400000Y50.800000S1000.000000F1200G1X50.800000Y0.000000G0X101.600000Y0.000000S0G1X76.200000Y5.080000S1000.000000G1X76.149200Y20.320000G1X76.149200Y22.860000G1X76.098400Y25.400000G1X76.149200Y30.480000G1X76.200000Y45.720000G1X101.600000Y50.800000G0X0.000000Y1.879600S0G1X0.660400Y1.879600S0.000000G1X0.762000Y1.879600S1000.000000G1X0.863600Y1.879600G1X0.914400Y1.879600G1X1.066800Y1.879600G1X1.117600Y1.879600G1X6.096000Y1.879600S0.000000G0X0.000000Y1.930400S0G1X0.660400Y1.930400S0.000000G1X0.812800Y1.930400S1000.000000G1X0.863600Y1.930400G1X0.965200Y1.930400G1X1.066800Y1.930400G1X1.117600Y1.930400G1X6.096000Y1.930400S0.000000G0X0.000000Y1.981200S0G1X0.660400Y1.981200S0.000000G1X1.117600Y1.981200S1000.000000G1X6.096000Y1.981200S0.000000G0X0.000000Y2.032000S0G1X0.660400Y2.032000S0.000000G1X0.762000Y2.032000S1000.000000G1X1.117600Y2.032000G1X6.096000Y2.032000S0.000000G0X0.000000Y2.082800S0G1X0.660400Y2.082800S0.000000G1X1.117600Y2.082800S1000.000000G1X6.096000Y2.082800S0.000000G0X0.000000Y2.133600S0G1X0.660400Y2.133600S0.000000G1X0.762000Y2.133600S1000.000000G1X1.117600Y2.133600G1X6.096000Y2.133600S0.000000G0X0.000000Y3.911600S0G1X2.235200Y3.911600S0.000000G1X2.336800Y3.911600S1000.000000G1X2.387600Y3.911600S0.000000G1X2.540000Y3.911600S1000.000000G1X7.518400Y3.911600S0.000000G0X0.000000Y3.962400S0G1X2.286000Y3.962400S0.000000G1X2.336800Y3.962400S1000.000000G1X2.438400Y3.962400S0.000000G1X2.540000Y3.962400S1000.000000G1X7.518400Y3.962400S0.000000G0X0.000000Y4.013200S0G1X2.133600Y4.013200S0.000000G1X2.184400Y4.013200S750.000000G1X2.235200Y4.013200S0.000000G1X2.590800Y4.013200S750.000000G1X7.569200Y4.013200S0.000000G0X0.000000Y4.064000S0G1X2.235200Y4.064000S0.000000G1X2.590800Y4.064000S500.000000G1X7.569200Y4.064000S0.000000G0X0.000000Y4.165600S0G1X2.133600Y4.165600S0.000000G1X2.235200Y4.165600S1000.000000G1X7.213600Y4.165600S0.000000M5G0X0Y0

0 commit comments

Comments
 (0)