Skip to content

Commit 1fc9026

Browse files
committed
GCode-based drivers: deduplicate code, remove spaces from output
1 parent 15d09f2 commit 1fc9026

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
@@ -527,17 +527,20 @@ protected void setFocus(PrintStream out, double focus) throws IOException {
527527
protected void move(PrintStream out, double x, double y, double resolution) throws IOException {
528528
x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution);
529529
y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution);
530-
currentSpeed = getTravel_speed();
530+
531+
String append = "";
531532

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

543546
protected void line(PrintStream out, double x, double y, double resolution) throws IOException {
@@ -588,6 +591,11 @@ private void writeShutdownCode() throws IOException {
588591

589592
protected void sendLine(String text, Object... parameters) throws IOException
590593
{
594+
if (text.startsWith("G0") || text.startsWith("G1")) {
595+
// Remove spaces from all standard moves.
596+
// Leave all other commands unchanged because they could be user-specific display messages from the init code.
597+
text = text.replace(" ", "");
598+
}
591599
out.format(FORMAT_LOCALE, text+LINEEND(), parameters);
592600
out.flush();
593601
if (isWaitForOKafterEachLine())
@@ -916,14 +924,14 @@ else if (this.port != null)
916924
public void sendJob(LaserJob job, ProgressListener pl, List<String> warnings) throws IllegalJobException, Exception {
917925
sendOrSaveJob(job, pl, warnings, null);
918926
}
919-
927+
920928
/***
921929
* Send the job to the port or file.
922930
* If a filePrintStream is given (!= null), write the job to the file.
923931
* Else (filePrintStream = null), connect to the configured network port or serial port.
924932
*/
925933
public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnings, PrintStream filePrintStream) throws IllegalJobException, Exception {
926-
934+
927935
pl.progressChanged(this, 0);
928936
pl.taskChanged(this, "checking job");
929937
checkJob(job);
@@ -933,7 +941,7 @@ public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnin
933941
if (filePrintStream != null) { // write to file
934942
this.out = filePrintStream;
935943
} else { // send to network
936-
connect(pl);
944+
connect(pl);
937945
}
938946
pl.taskChanged(this, "sending");
939947
try {
@@ -944,7 +952,7 @@ public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnin
944952
if (filePrintStream != null) { // write to file
945953
filePrintStream.close();
946954
} else { // send to network
947-
disconnect(this.jobName);
955+
disconnect(this.jobName);
948956
}
949957
}
950958
pl.taskChanged(this, "sent.");
@@ -1300,4 +1308,16 @@ public GenericGcodeDriver clone() {
13001308
return clone;
13011309
}
13021310

1311+
/**
1312+
* Send the F (travelFeed) during G0 rapid moves?
1313+
*
1314+
* true: Yes. Best for compatibility.
1315+
* 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.)
1316+
*/
1317+
// TODO: make configurable in the future
1318+
protected boolean isSendFeedDuringRapids()
1319+
{
1320+
return true;
1321+
}
1322+
13031323
}

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 S1
64-
G1 X2.2352 Y4.0132 S0
65-
G1 X2.5908 Y4.0132 S1
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
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.0132S1
64+
G1X2.2352Y4.0132S0
65+
G1X2.5908Y4.0132S1
66+
G1X7.5692Y4.0132S0
67+
G0X0Y4.064F3600.000000
68+
G1X2.2352Y4.064F1200
69+
G1X2.5908Y4.064S0
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)