Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import de.thomas_oster.liblasercut.VectorCommand;
import de.thomas_oster.liblasercut.VectorPart;
import de.thomas_oster.liblasercut.platform.Util;
import static de.thomas_oster.liblasercut.utils.Assertion.assertThat;
import net.sf.corn.httpclient.HttpClient;
import net.sf.corn.httpclient.HttpResponse;
import purejavacomm.CommPort;
Expand All @@ -56,6 +57,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -496,11 +498,11 @@ protected void writeVectorGCode(VectorPart vp, double resolution) throws Unsuppo
}
}
}
protected double currentPower = -1;
protected double currentSpeed = -1;
private double nextPower = -1;
private double nextSpeed = -1;
private double currentFocus = 0;
protected double currentPower = Double.NaN;
protected double currentSpeed = Double.NaN;
private double nextPower = Double.NaN;
private double nextSpeed = Double.NaN;
private double currentFocus = Double.NaN;

protected void setSpeed(double speedInPercent) {
nextSpeed = speedInPercent;
Expand All @@ -509,7 +511,7 @@ protected void setSpeed(double speedInPercent) {
protected void setPower(double powerInPercent) {
nextPower = powerInPercent/100.0*spindleMax;
}

protected void setFocus(PrintStream out, double focus) throws IOException {

if (currentFocus != focus) {
Expand All @@ -519,36 +521,40 @@ protected void setFocus(PrintStream out, double focus) throws IOException {
currentPower = -1; // set to invalid value to force new S-value at next G1
}
sendLine("G0 Z%s" + append, formatDouble(focus, getGCodeDigits()));
currentFocus = focus;
currentFocus = focus;
}
}

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 = -1; // 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
currentPower = Double.NaN; // set to invalid value to force new S-value at next G1
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 {
x = isFlipXaxis() ? getBedWidth() - Util.px2mm(x, resolution) : Util.px2mm(x, resolution);
y = isFlipYaxis() ? getBedHeight() - Util.px2mm(y, resolution) : Util.px2mm(y, resolution);
String append = "";

assertThat(!Double.isNaN(nextPower));
if (nextPower != currentPower)
{
append += String.format(FORMAT_LOCALE, " S%s", formatDouble(nextPower, getSCodeDigits()));
currentPower = nextPower;
}
assertThat(!Double.isNaN(nextSpeed));
if (nextSpeed != currentSpeed)
{
append += String.format(FORMAT_LOCALE, " F%d", (int) (max_speed*nextSpeed/100.0));
Expand Down Expand Up @@ -586,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 @@ -912,31 +923,47 @@ else if (this.port != null)

@Override
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);
this.currentPower = -1;
this.currentSpeed = -1;

pl.taskChanged(this, "checking job");
checkJob(job);
this.jobName = job.getName()+".gcode";
job.applyStartPoint();
pl.taskChanged(this, "connecting...");
connect(pl);
if (filePrintStream != null) { // write to file
this.out = filePrintStream;
} else { // send to network
connect(pl);
}
pl.taskChanged(this, "sending");
try {
writeJobCode(job, pl);
disconnect(job.getName()+".gcode");
}
catch (IOException e) {
finally {
pl.taskChanged(this, "disconnecting");
disconnect(this.jobName);
throw e;
if (filePrintStream != null) { // write to file
filePrintStream.close();
} else { // send to network
disconnect(this.jobName);
}
}
pl.taskChanged(this, "sent.");
pl.progressChanged(this, 100);
}

public void writeJobCode(LaserJob job, ProgressListener pl) throws IOException {
currentPower = Double.NaN;
currentSpeed = Double.NaN;
currentFocus = Double.NaN;
writeInitializationCode();
pl.progressChanged(this, 20);
int i = 0;
Expand Down Expand Up @@ -965,16 +992,12 @@ public void writeJobCode(LaserJob job, ProgressListener pl) throws IOException {

@Override
public void saveJob(OutputStream fileOutputStream, LaserJob job) throws IllegalJobException, Exception {
this.currentPower = -1;
this.currentSpeed = -1;

checkJob(job);
boolean wasSetWaitingForOk = isWaitForOKafterEachLine();
try (PrintStream ps = new LinefeedPrintStream(fileOutputStream))
{
this.out = ps;
setWaitForOKafterEachLine( false );
writeJobCode(job, new ProgressListenerDummy());
sendOrSaveJob(job, new ProgressListenerDummy(), new ArrayList<String>(), ps);
} finally {
setWaitForOKafterEachLine(wasSetWaitingForOk);
}
Expand Down Expand Up @@ -1307,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
Loading