Skip to content

Commit 5106508

Browse files
committed
GCode-based drivers: deduplicate code, remove spaces from output
1 parent dd51990 commit 5106508

File tree

6 files changed

+258
-267
lines changed

6 files changed

+258
-267
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -528,17 +528,20 @@ protected void setFocus(PrintStream out, double focus) throws IOException {
528528
protected void move(PrintStream out, double x, double y, double resolution) throws IOException {
529529
x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution);
530530
y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution);
531-
currentSpeed = getTravel_speed();
531+
532+
String append = "";
532533

533534
if (blankLaserDuringRapids)
534535
{
535536
currentPower = Double.NaN; // set to invalid value to force new S-value at next G1
536-
sendLine("G0 X%s Y%s F%d S0", formatDouble(x, getGCodeDigits()), formatDouble(y, getGCodeDigits()), (int) (travel_speed));
537-
}
538-
else
537+
append = " S0"; }
538+
if (isSendFeedDuringRapids())
539539
{
540-
sendLine("G0 X%s Y%s F%d", formatDouble(x, getGCodeDigits()), formatDouble(y, getGCodeDigits()), (int) (travel_speed));
540+
currentSpeed = getTravel_speed();
541+
append += String.format(FORMAT_LOCALE, " F%f", currentSpeed);
541542
}
543+
sendLine("G0 X%s Y%s " + append, formatDouble(x, getGCodeDigits()), formatDouble(y, getGCodeDigits()), (int) (travel_speed));
544+
542545
}
543546

544547
protected void line(PrintStream out, double x, double y, double resolution) throws IOException {
@@ -589,6 +592,11 @@ private void writeShutdownCode() throws IOException {
589592

590593
protected void sendLine(String text, Object... parameters) throws IOException
591594
{
595+
if (text.startsWith("G0") || text.startsWith("G1")) {
596+
// Remove spaces from all standard moves.
597+
// Leave all other commands unchanged because they could be user-specific display messages from the init code.
598+
text = text.replace(" ", "");
599+
}
592600
out.format(FORMAT_LOCALE, text+LINEEND(), parameters);
593601
out.flush();
594602
if (isWaitForOKafterEachLine())
@@ -917,14 +925,14 @@ else if (this.port != null)
917925
public void sendJob(LaserJob job, ProgressListener pl, List<String> warnings) throws IllegalJobException, Exception {
918926
sendOrSaveJob(job, pl, warnings, null);
919927
}
920-
928+
921929
/***
922930
* Send the job to the port or file.
923931
* If a filePrintStream is given (!= null), write the job to the file.
924932
* Else (filePrintStream = null), connect to the configured network port or serial port.
925933
*/
926934
public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnings, PrintStream filePrintStream) throws IllegalJobException, Exception {
927-
935+
928936
pl.progressChanged(this, 0);
929937
pl.taskChanged(this, "checking job");
930938
checkJob(job);
@@ -934,7 +942,7 @@ public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnin
934942
if (filePrintStream != null) { // write to file
935943
this.out = filePrintStream;
936944
} else { // send to network
937-
connect(pl);
945+
connect(pl);
938946
}
939947
pl.taskChanged(this, "sending");
940948
try {
@@ -945,7 +953,7 @@ public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnin
945953
if (filePrintStream != null) { // write to file
946954
filePrintStream.close();
947955
} else { // send to network
948-
disconnect(this.jobName);
956+
disconnect(this.jobName);
949957
}
950958
}
951959
pl.taskChanged(this, "sent.");
@@ -1322,4 +1330,16 @@ public GenericGcodeDriver clone() {
13221330
return clone;
13231331
}
13241332

1333+
/**
1334+
* Send the F (travelFeed) during G0 rapid moves?
1335+
*
1336+
* true: Yes. Best for compatibility.
1337+
* 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.)
1338+
*/
1339+
// TODO: make configurable in the future
1340+
protected boolean isSendFeedDuringRapids()
1341+
{
1342+
return true;
1343+
}
1344+
13251345
}

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

Lines changed: 9 additions & 38 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,43 +161,11 @@ 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-
*/
165164
@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 = -1; // set to invalid value to force new S-value at next G1
173-
sendLine("G0 X%f Y%f S0", x, y);
174-
}
175-
else
165+
protected boolean isSendFeedDuringRapids()
176166
{
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-
*/
184-
@Override
185-
protected void sendLine(String text, Object... parameters) throws IOException
186-
{
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
}
199170

200171
@Override
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
4-
G0 X0.508 Y0.508 F3600
5-
G1 X25.4 Y50.8 S1 F1200
6-
G1 X50.8 Y0
7-
G0 X101.6 Y0 F3600
8-
G1 X76.2 Y5.08 F1200
9-
G1 X76.1492 Y20.32
10-
G1 X76.1492 Y22.86
11-
G1 X76.0984 Y25.4
12-
G1 X76.1492 Y30.48
13-
G1 X76.2 Y45.72
14-
G1 X101.6 Y50.8
15-
G0 X0 Y1.8796 F3600
16-
G1 X0.6604 Y1.8796 S0 F1200
17-
G1 X0.762 Y1.8796 S1
18-
G1 X0.8636 Y1.8796
19-
G1 X0.9144 Y1.8796
20-
G1 X1.0668 Y1.8796
21-
G1 X1.1176 Y1.8796
22-
G1 X6.096 Y1.8796 S0
23-
G0 X0 Y1.9304 F3600
24-
G1 X0.6604 Y1.9304 F1200
25-
G1 X0.8128 Y1.9304 S1
26-
G1 X0.8636 Y1.9304
27-
G1 X0.9652 Y1.9304
28-
G1 X1.0668 Y1.9304
29-
G1 X1.1176 Y1.9304
30-
G1 X6.096 Y1.9304 S0
31-
G0 X0 Y1.9812 F3600
32-
G1 X0.6604 Y1.9812 F1200
33-
G1 X1.1176 Y1.9812 S1
34-
G1 X6.096 Y1.9812 S0
35-
G0 X0 Y2.032 F3600
36-
G1 X0.6604 Y2.032 F1200
37-
G1 X0.762 Y2.032 S1
38-
G1 X1.1176 Y2.032
39-
G1 X6.096 Y2.032 S0
40-
G0 X0 Y2.0828 F3600
41-
G1 X0.6604 Y2.0828 F1200
42-
G1 X1.1176 Y2.0828 S1
43-
G1 X6.096 Y2.0828 S0
44-
G0 X0 Y2.1336 F3600
45-
G1 X0.6604 Y2.1336 F1200
46-
G1 X0.762 Y2.1336 S1
47-
G1 X1.1176 Y2.1336
48-
G1 X6.096 Y2.1336 S0
49-
G0 X0 Y3.9116 F3600
50-
G1 X2.2352 Y3.9116 F1200
51-
G1 X2.3368 Y3.9116 S1
52-
G1 X2.3876 Y3.9116 S0
53-
G1 X2.54 Y3.9116 S1
54-
G1 X7.5184 Y3.9116 S0
55-
G0 X0 Y3.9624 F3600
56-
G1 X2.286 Y3.9624 F1200
57-
G1 X2.3368 Y3.9624 S1
58-
G1 X2.4384 Y3.9624 S0
59-
G1 X2.54 Y3.9624 S1
60-
G1 X7.5184 Y3.9624 S0
61-
G0 X0 Y4.0132 F3600
62-
G1 X2.1336 Y4.0132 F1200
63-
G1 X2.1844 Y4.0132 S0.75
64-
G1 X2.2352 Y4.0132 S0
65-
G1 X2.5908 Y4.0132 S0.75
66-
G1 X7.5692 Y4.0132 S0
67-
G0 X0 Y4.064 F3600
68-
G1 X2.2352 Y4.064 F1200
69-
G1 X2.5908 Y4.064 S0.5
70-
G1 X7.5692 Y4.064 S0
71-
G0 X0 Y4.1656 F3600
72-
G1 X2.1336 Y4.1656 F1200
73-
G1 X2.2352 Y4.1656 S1
74-
G1 X7.2136 Y4.1656 S0
75-
G0 X0 Y0
3+
G0Z0
4+
G0X0.508Y0.508F3600.000000
5+
G1X25.4Y50.8S1F1200
6+
G1X50.8Y0
7+
G0X101.6Y0F3600.000000
8+
G1X76.2Y5.08F1200
9+
G1X76.1492Y20.32
10+
G1X76.1492Y22.86
11+
G1X76.0984Y25.4
12+
G1X76.1492Y30.48
13+
G1X76.2Y45.72
14+
G1X101.6Y50.8
15+
G0X0Y1.8796F3600.000000
16+
G1X0.6604Y1.8796S0F1200
17+
G1X0.762Y1.8796S1
18+
G1X0.8636Y1.8796
19+
G1X0.9144Y1.8796
20+
G1X1.0668Y1.8796
21+
G1X1.1176Y1.8796
22+
G1X6.096Y1.8796S0
23+
G0X0Y1.9304F3600.000000
24+
G1X0.6604Y1.9304F1200
25+
G1X0.8128Y1.9304S1
26+
G1X0.8636Y1.9304
27+
G1X0.9652Y1.9304
28+
G1X1.0668Y1.9304
29+
G1X1.1176Y1.9304
30+
G1X6.096Y1.9304S0
31+
G0X0Y1.9812F3600.000000
32+
G1X0.6604Y1.9812F1200
33+
G1X1.1176Y1.9812S1
34+
G1X6.096Y1.9812S0
35+
G0X0Y2.032F3600.000000
36+
G1X0.6604Y2.032F1200
37+
G1X0.762Y2.032S1
38+
G1X1.1176Y2.032
39+
G1X6.096Y2.032S0
40+
G0X0Y2.0828F3600.000000
41+
G1X0.6604Y2.0828F1200
42+
G1X1.1176Y2.0828S1
43+
G1X6.096Y2.0828S0
44+
G0X0Y2.1336F3600.000000
45+
G1X0.6604Y2.1336F1200
46+
G1X0.762Y2.1336S1
47+
G1X1.1176Y2.1336
48+
G1X6.096Y2.1336S0
49+
G0X0Y3.9116F3600.000000
50+
G1X2.2352Y3.9116F1200
51+
G1X2.3368Y3.9116S1
52+
G1X2.3876Y3.9116S0
53+
G1X2.54Y3.9116S1
54+
G1X7.5184Y3.9116S0
55+
G0X0Y3.9624F3600.000000
56+
G1X2.286Y3.9624F1200
57+
G1X2.3368Y3.9624S1
58+
G1X2.4384Y3.9624S0
59+
G1X2.54Y3.9624S1
60+
G1X7.5184Y3.9624S0
61+
G0X0Y4.0132F3600.000000
62+
G1X2.1336Y4.0132F1200
63+
G1X2.1844Y4.0132S0.75
64+
G1X2.2352Y4.0132S0
65+
G1X2.5908Y4.0132S0.75
66+
G1X7.5692Y4.0132S0
67+
G0X0Y4.064F3600.000000
68+
G1X2.2352Y4.064F1200
69+
G1X2.5908Y4.064S0.5
70+
G1X7.5692Y4.064S0
71+
G0X0Y4.1656F3600.000000
72+
G1X2.1336Y4.1656F1200
73+
G1X2.2352Y4.1656S1
74+
G1X7.2136Y4.1656S0
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-
G21G90M3G0Z0S0G0X0.508000Y0.508000S0G1X25.4Y50.8S1000F1200G1X50.8Y0G0X101.600000Y0.000000S0G1X76.2Y5.08S1000F1200G1X76.1492Y20.32G1X76.1492Y22.86G1X76.0984Y25.4G1X76.1492Y30.48G1X76.2Y45.72G1X101.6Y50.8G0X0.000000Y1.879600S0G1X0.6604Y1.8796S0F1200G1X0.762Y1.8796S1000G1X0.8636Y1.8796G1X0.9144Y1.8796G1X1.0668Y1.8796G1X1.1176Y1.8796G1X6.096Y1.8796S0G0X0.000000Y1.930400S0G1X0.6604Y1.9304S0F1200G1X0.8128Y1.9304S1000G1X0.8636Y1.9304G1X0.9652Y1.9304G1X1.0668Y1.9304G1X1.1176Y1.9304G1X6.096Y1.9304S0G0X0.000000Y1.981200S0G1X0.6604Y1.9812S0F1200G1X1.1176Y1.9812S1000G1X6.096Y1.9812S0G0X0.000000Y2.032000S0G1X0.6604Y2.032S0F1200G1X0.762Y2.032S1000G1X1.1176Y2.032G1X6.096Y2.032S0G0X0.000000Y2.082800S0G1X0.6604Y2.0828S0F1200G1X1.1176Y2.0828S1000G1X6.096Y2.0828S0G0X0.000000Y2.133600S0G1X0.6604Y2.1336S0F1200G1X0.762Y2.1336S1000G1X1.1176Y2.1336G1X6.096Y2.1336S0G0X0.000000Y3.911600S0G1X2.2352Y3.9116S0F1200G1X2.3368Y3.9116S1000G1X2.3876Y3.9116S0G1X2.54Y3.9116S1000G1X7.5184Y3.9116S0G0X0.000000Y3.962400S0G1X2.286Y3.9624S0F1200G1X2.3368Y3.9624S1000G1X2.4384Y3.9624S0G1X2.54Y3.9624S1000G1X7.5184Y3.9624S0G0X0.000000Y4.013200S0G1X2.1336Y4.0132S0F1200G1X2.1844Y4.0132S750G1X2.2352Y4.0132S0G1X2.5908Y4.0132S750G1X7.5692Y4.0132S0G0X0.000000Y4.064000S0G1X2.2352Y4.064S0F1200G1X2.5908Y4.064S500G1X7.5692Y4.064S0G0X0.000000Y4.165600S0G1X2.1336Y4.1656S0F1200G1X2.2352Y4.1656S1000G1X7.2136Y4.1656S0M5G0X0Y0
1+
G21G90M3G0Z0S0G0X0.508Y0.508S0G1X25.4Y50.8S1000F1200G1X50.8Y0G0X101.6Y0S0G1X76.2Y5.08S1000G1X76.1492Y20.32G1X76.1492Y22.86G1X76.0984Y25.4G1X76.1492Y30.48G1X76.2Y45.72G1X101.6Y50.8G0X0Y1.8796S0G1X0.6604Y1.8796S0G1X0.762Y1.8796S1000G1X0.8636Y1.8796G1X0.9144Y1.8796G1X1.0668Y1.8796G1X1.1176Y1.8796G1X6.096Y1.8796S0G0X0Y1.9304S0G1X0.6604Y1.9304S0G1X0.8128Y1.9304S1000G1X0.8636Y1.9304G1X0.9652Y1.9304G1X1.0668Y1.9304G1X1.1176Y1.9304G1X6.096Y1.9304S0G0X0Y1.9812S0G1X0.6604Y1.9812S0G1X1.1176Y1.9812S1000G1X6.096Y1.9812S0G0X0Y2.032S0G1X0.6604Y2.032S0G1X0.762Y2.032S1000G1X1.1176Y2.032G1X6.096Y2.032S0G0X0Y2.0828S0G1X0.6604Y2.0828S0G1X1.1176Y2.0828S1000G1X6.096Y2.0828S0G0X0Y2.1336S0G1X0.6604Y2.1336S0G1X0.762Y2.1336S1000G1X1.1176Y2.1336G1X6.096Y2.1336S0G0X0Y3.9116S0G1X2.2352Y3.9116S0G1X2.3368Y3.9116S1000G1X2.3876Y3.9116S0G1X2.54Y3.9116S1000G1X7.5184Y3.9116S0G0X0Y3.9624S0G1X2.286Y3.9624S0G1X2.3368Y3.9624S1000G1X2.4384Y3.9624S0G1X2.54Y3.9624S1000G1X7.5184Y3.9624S0G0X0Y4.0132S0G1X2.1336Y4.0132S0G1X2.1844Y4.0132S750G1X2.2352Y4.0132S0G1X2.5908Y4.0132S750G1X7.5692Y4.0132S0G0X0Y4.064S0G1X2.2352Y4.064S0G1X2.5908Y4.064S500G1X7.5692Y4.064S0G0X0Y4.1656S0G1X2.1336Y4.1656S0G1X2.2352Y4.1656S1000G1X7.2136Y4.1656S0M5G0X0Y0

0 commit comments

Comments
 (0)