Skip to content

Commit

Permalink
GCode-based drivers: deduplicate code, remove spaces from output
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmax committed Dec 22, 2023
1 parent dd51990 commit 5106508
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,17 +528,20 @@ protected void setFocus(PrintStream out, double focus) throws IOException {
protected void move(PrintStream out, double x, double y, double resolution) throws IOException {
x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution);
y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution);
currentSpeed = getTravel_speed();

String append = "";

if (blankLaserDuringRapids)
{
currentPower = Double.NaN; // set to invalid value to force new S-value at next G1
sendLine("G0 X%s Y%s F%d S0", formatDouble(x, getGCodeDigits()), formatDouble(y, getGCodeDigits()), (int) (travel_speed));
}
else
append = " S0"; }
if (isSendFeedDuringRapids())
{
sendLine("G0 X%s Y%s F%d", formatDouble(x, getGCodeDigits()), formatDouble(y, getGCodeDigits()), (int) (travel_speed));
currentSpeed = getTravel_speed();
append += String.format(FORMAT_LOCALE, " F%f", currentSpeed);
}
sendLine("G0 X%s Y%s " + append, formatDouble(x, getGCodeDigits()), formatDouble(y, getGCodeDigits()), (int) (travel_speed));

}

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

protected void sendLine(String text, Object... parameters) throws IOException
{
if (text.startsWith("G0") || text.startsWith("G1")) {
// Remove spaces from all standard moves.
// Leave all other commands unchanged because they could be user-specific display messages from the init code.
text = text.replace(" ", "");
}
out.format(FORMAT_LOCALE, text+LINEEND(), parameters);
out.flush();
if (isWaitForOKafterEachLine())
Expand Down Expand Up @@ -917,14 +925,14 @@ else if (this.port != null)
public void sendJob(LaserJob job, ProgressListener pl, List<String> warnings) throws IllegalJobException, Exception {
sendOrSaveJob(job, pl, warnings, null);
}

/***
* Send the job to the port or file.
* If a filePrintStream is given (!= null), write the job to the file.
* Else (filePrintStream = null), connect to the configured network port or serial port.
*/
public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnings, PrintStream filePrintStream) throws IllegalJobException, Exception {

pl.progressChanged(this, 0);
pl.taskChanged(this, "checking job");
checkJob(job);
Expand All @@ -934,7 +942,7 @@ public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnin
if (filePrintStream != null) { // write to file
this.out = filePrintStream;
} else { // send to network
connect(pl);
connect(pl);
}
pl.taskChanged(this, "sending");
try {
Expand All @@ -945,7 +953,7 @@ public void sendOrSaveJob(LaserJob job, ProgressListener pl, List<String> warnin
if (filePrintStream != null) { // write to file
filePrintStream.close();
} else { // send to network
disconnect(this.jobName);
disconnect(this.jobName);
}
}
pl.taskChanged(this, "sent.");
Expand Down Expand Up @@ -1322,4 +1330,16 @@ public GenericGcodeDriver clone() {
return clone;
}

/**
* Send the F (travelFeed) during G0 rapid moves?
*
* true: Yes. Best for compatibility.
* 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.)
*/
// TODO: make configurable in the future
protected boolean isSendFeedDuringRapids()
{
return true;
}

}
47 changes: 9 additions & 38 deletions src/main/java/de/thomas_oster/liblasercut/drivers/Grbl.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ public String getModelName()
protected void sendLineWithoutWait(String text, Object... parameters) throws IOException
{
boolean wasSetWaitingForOk = isWaitForOKafterEachLine();
setWaitForOKafterEachLine(false);
sendLine(text, parameters);
setWaitForOKafterEachLine(wasSetWaitingForOk);
try {
setWaitForOKafterEachLine(false);
sendLine(text, parameters);
} finally {
setWaitForOKafterEachLine(wasSetWaitingForOk);
}
}

/**
Expand Down Expand Up @@ -158,43 +161,11 @@ protected String waitForIdentificationLine(ProgressListener pl) throws IOExcepti
return null;
}

/**
* Send a G0 rapid move to Grbl.
* Doesn't include travel speed since grbl ignores that anyway.
*/
@Override
protected void move(PrintStream out, double x, double y, double resolution) throws IOException {
x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution);
y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution);
currentSpeed = getTravel_speed();
if (blankLaserDuringRapids)
{
currentPower = -1; // set to invalid value to force new S-value at next G1
sendLine("G0 X%f Y%f S0", x, y);
}
else
protected boolean isSendFeedDuringRapids()
{
sendLine("G0 X%f Y%f", x, y);
}
}

/**
* Send a line of gcode to the cutter, stripping out any whitespace in the process
*/
@Override
protected void sendLine(String text, Object... parameters) throws IOException
{
out.format(FORMAT_LOCALE, text.replace(" ", "")+LINEEND(), parameters);
// System.out.println(String.format(FORMAT_LOCALE, "> "+text+LINEEND(), parameters));
out.flush();
if (isWaitForOKafterEachLine())
{
String line = waitForLine();
if (!"ok".equals(line))
{
throw new IOException("Lasercutter did not respond 'ok', but '"+line+"'instead.");
}
}
// Grbl ignores the F parameter during G0.
return false;
}

@Override
Expand Down
146 changes: 73 additions & 73 deletions test-output/de.thomas_oster.liblasercut.drivers.GenericGcodeDriver.out
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
G21
G90
G0 Z0
G0 X0.508 Y0.508 F3600
G1 X25.4 Y50.8 S1 F1200
G1 X50.8 Y0
G0 X101.6 Y0 F3600
G1 X76.2 Y5.08 F1200
G1 X76.1492 Y20.32
G1 X76.1492 Y22.86
G1 X76.0984 Y25.4
G1 X76.1492 Y30.48
G1 X76.2 Y45.72
G1 X101.6 Y50.8
G0 X0 Y1.8796 F3600
G1 X0.6604 Y1.8796 S0 F1200
G1 X0.762 Y1.8796 S1
G1 X0.8636 Y1.8796
G1 X0.9144 Y1.8796
G1 X1.0668 Y1.8796
G1 X1.1176 Y1.8796
G1 X6.096 Y1.8796 S0
G0 X0 Y1.9304 F3600
G1 X0.6604 Y1.9304 F1200
G1 X0.8128 Y1.9304 S1
G1 X0.8636 Y1.9304
G1 X0.9652 Y1.9304
G1 X1.0668 Y1.9304
G1 X1.1176 Y1.9304
G1 X6.096 Y1.9304 S0
G0 X0 Y1.9812 F3600
G1 X0.6604 Y1.9812 F1200
G1 X1.1176 Y1.9812 S1
G1 X6.096 Y1.9812 S0
G0 X0 Y2.032 F3600
G1 X0.6604 Y2.032 F1200
G1 X0.762 Y2.032 S1
G1 X1.1176 Y2.032
G1 X6.096 Y2.032 S0
G0 X0 Y2.0828 F3600
G1 X0.6604 Y2.0828 F1200
G1 X1.1176 Y2.0828 S1
G1 X6.096 Y2.0828 S0
G0 X0 Y2.1336 F3600
G1 X0.6604 Y2.1336 F1200
G1 X0.762 Y2.1336 S1
G1 X1.1176 Y2.1336
G1 X6.096 Y2.1336 S0
G0 X0 Y3.9116 F3600
G1 X2.2352 Y3.9116 F1200
G1 X2.3368 Y3.9116 S1
G1 X2.3876 Y3.9116 S0
G1 X2.54 Y3.9116 S1
G1 X7.5184 Y3.9116 S0
G0 X0 Y3.9624 F3600
G1 X2.286 Y3.9624 F1200
G1 X2.3368 Y3.9624 S1
G1 X2.4384 Y3.9624 S0
G1 X2.54 Y3.9624 S1
G1 X7.5184 Y3.9624 S0
G0 X0 Y4.0132 F3600
G1 X2.1336 Y4.0132 F1200
G1 X2.1844 Y4.0132 S0.75
G1 X2.2352 Y4.0132 S0
G1 X2.5908 Y4.0132 S0.75
G1 X7.5692 Y4.0132 S0
G0 X0 Y4.064 F3600
G1 X2.2352 Y4.064 F1200
G1 X2.5908 Y4.064 S0.5
G1 X7.5692 Y4.064 S0
G0 X0 Y4.1656 F3600
G1 X2.1336 Y4.1656 F1200
G1 X2.2352 Y4.1656 S1
G1 X7.2136 Y4.1656 S0
G0 X0 Y0
G0Z0
G0X0.508Y0.508F3600.000000
G1X25.4Y50.8S1F1200
G1X50.8Y0
G0X101.6Y0F3600.000000
G1X76.2Y5.08F1200
G1X76.1492Y20.32
G1X76.1492Y22.86
G1X76.0984Y25.4
G1X76.1492Y30.48
G1X76.2Y45.72
G1X101.6Y50.8
G0X0Y1.8796F3600.000000
G1X0.6604Y1.8796S0F1200
G1X0.762Y1.8796S1
G1X0.8636Y1.8796
G1X0.9144Y1.8796
G1X1.0668Y1.8796
G1X1.1176Y1.8796
G1X6.096Y1.8796S0
G0X0Y1.9304F3600.000000
G1X0.6604Y1.9304F1200
G1X0.8128Y1.9304S1
G1X0.8636Y1.9304
G1X0.9652Y1.9304
G1X1.0668Y1.9304
G1X1.1176Y1.9304
G1X6.096Y1.9304S0
G0X0Y1.9812F3600.000000
G1X0.6604Y1.9812F1200
G1X1.1176Y1.9812S1
G1X6.096Y1.9812S0
G0X0Y2.032F3600.000000
G1X0.6604Y2.032F1200
G1X0.762Y2.032S1
G1X1.1176Y2.032
G1X6.096Y2.032S0
G0X0Y2.0828F3600.000000
G1X0.6604Y2.0828F1200
G1X1.1176Y2.0828S1
G1X6.096Y2.0828S0
G0X0Y2.1336F3600.000000
G1X0.6604Y2.1336F1200
G1X0.762Y2.1336S1
G1X1.1176Y2.1336
G1X6.096Y2.1336S0
G0X0Y3.9116F3600.000000
G1X2.2352Y3.9116F1200
G1X2.3368Y3.9116S1
G1X2.3876Y3.9116S0
G1X2.54Y3.9116S1
G1X7.5184Y3.9116S0
G0X0Y3.9624F3600.000000
G1X2.286Y3.9624F1200
G1X2.3368Y3.9624S1
G1X2.4384Y3.9624S0
G1X2.54Y3.9624S1
G1X7.5184Y3.9624S0
G0X0Y4.0132F3600.000000
G1X2.1336Y4.0132F1200
G1X2.1844Y4.0132S0.75
G1X2.2352Y4.0132S0
G1X2.5908Y4.0132S0.75
G1X7.5692Y4.0132S0
G0X0Y4.064F3600.000000
G1X2.2352Y4.064F1200
G1X2.5908Y4.064S0.5
G1X7.5692Y4.064S0
G0X0Y4.1656F3600.000000
G1X2.1336Y4.1656F1200
G1X2.2352Y4.1656S1
G1X7.2136Y4.1656S0
G0X0Y0
2 changes: 1 addition & 1 deletion test-output/de.thomas_oster.liblasercut.drivers.Grbl.out
Original file line number Diff line number Diff line change
@@ -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
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
Expand Down
Loading

0 comments on commit 5106508

Please sign in to comment.