Skip to content

Commit

Permalink
Implement character-counting protocol for Grbl. Closes t-oster#74
Browse files Browse the repository at this point in the history
  • Loading branch information
jekhor committed Jan 7, 2017
1 parent 904b520 commit 025e4ec
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/com/t_oster/liblasercut/drivers/GenericGcodeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ protected void line(PrintStream out, double x, double y, double resolution) thro
sendLine("G1 X%f Y%f"+append, x, y);
}

private void writeInitializationCode() throws IOException {
protected void writeInitializationCode() throws IOException {
if (preJobGcode != null)
{
for (String line : preJobGcode.split(","))
Expand All @@ -506,7 +506,7 @@ private void writeInitializationCode() throws IOException {
}


private void writeShutdownCode() throws IOException {
protected void writeShutdownCode() throws IOException {
if (postJobGcode != null)
{
for (String line : postJobGcode.split(","))
Expand Down Expand Up @@ -671,7 +671,7 @@ protected String connect_serial(CommPortIdentifier i, ProgressListener pl) throw
* Used to buffer the file before uploading via http
*/
private ByteArrayOutputStream outputBuffer;
private String jobName;
protected String jobName;
protected void connect(ProgressListener pl) throws IOException, PortInUseException, NoSuchPortException, UnsupportedCommOperationException
{
outputBuffer = null;
Expand Down
153 changes: 145 additions & 8 deletions src/com/t_oster/liblasercut/drivers/Grbl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import com.t_oster.liblasercut.ProgressListener;
import com.t_oster.liblasercut.platform.Util;
import com.t_oster.liblasercut.*;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayDeque;

/**
* This class implements a driver for Grbl based firmwares.
Expand All @@ -33,8 +35,11 @@
*/
public class Grbl extends GenericGcodeDriver
{
protected ArrayDeque<Integer> commandLenQueue;

public Grbl()
{
commandLenQueue = new ArrayDeque<Integer>();
//set some grbl-specific defaults
setLineend("CR");
setIdentificationLine("Grbl");
Expand Down Expand Up @@ -105,6 +110,24 @@ public void setAutoHome(boolean auto_home)
this.autoHome = auto_home;
}

protected boolean simpleStreamMode = true;

public boolean isSimpleStreamMode()
{
return simpleStreamMode;
}

public void setSimpleStreamMode(boolean mode) throws IOException
{
if (mode != simpleStreamMode) {
if (!simpleStreamMode)
waitForCommandsCompletion();
else
commandLenQueue.clear();

simpleStreamMode = mode;
}
}

@Override
public String getModelName()
Expand All @@ -119,6 +142,53 @@ protected void sendLineWithoutWait(String text, Object... parameters) throws IOE
sendLine(text, parameters);
setWaitForOKafterEachLine(wasSetWaitingForOk);
}


protected static final int GRBL_BUF_LEN = 128;

protected Integer getBufferedCommandsLen()
{
Integer len = 0;
for (Integer c : commandLenQueue)
len += c;
return len;
}

protected void sendLineCC(String text, Object... parameters) throws IOException
{
String outStr = String.format(FORMAT_LOCALE, text+LINEEND(), parameters);
int len = outStr.length();

while (in.ready() || (getBufferedCommandsLen() + len > GRBL_BUF_LEN)) {
String line = waitForLine();
if (!"ok".equals(line))
{
throw new IOException("Lasercutter did not respond 'ok', but '"+line+"'instead.");
}
commandLenQueue.remove();
System.out.println(String.format(FORMAT_LOCALE, "%d" + LINEEND(), getBufferedCommandsLen(), parameters));
}

commandLenQueue.add(len);
out.print(outStr);
//TODO: Remove
System.out.println(String.format(FORMAT_LOCALE, "%d> %s", getBufferedCommandsLen(), outStr));
out.flush();
}

protected void waitForCommandsCompletion() throws IOException
{
while (!commandLenQueue.isEmpty()) {
String line = waitForLine();
if (!"ok".equals(line))
{
throw new IOException("Lasercutter did not respond 'ok', but '"+line+"'instead.");
}
commandLenQueue.remove();
System.out.println(String.format(FORMAT_LOCALE, "%d" + LINEEND(), getBufferedCommandsLen()));
}
}


/**
* Initializes Grbl, handling issuing of soft-reset and initial homing
Expand Down Expand Up @@ -187,14 +257,7 @@ protected void move(PrintStream out, double x, double y, double resolution) thro
}
}

/**
* Send a line of gcode to the cutter, stripping out any whitespace in the process
* @param text
* @param parameters
* @throws IOException
*/
@Override
protected void sendLine(String text, Object... parameters) throws IOException
protected void sendLineSimple(String text, Object... parameters) throws IOException
{
out.format(FORMAT_LOCALE, text.replace(" ", "")+LINEEND(), parameters);
//TODO: Remove
Expand All @@ -210,6 +273,80 @@ protected void sendLine(String text, Object... parameters) throws IOException
}
}

/**
* Send a line of gcode to the cutter, stripping out any whitespace in the process
* @param text
* @param parameters
* @throws IOException
*/
@Override

protected void sendLine(String text, Object... parameters) throws IOException
{
if (isSimpleStreamMode())
sendLineSimple(text, parameters);
else
sendLineCC(text, parameters);
}

@Override
public void sendJob(LaserJob job, ProgressListener pl, List<String> warnings) 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);
pl.taskChanged(this, "sending");
try {
writeInitializationCode();

boolean wasSimpleMode = isSimpleStreamMode();
setSimpleStreamMode( false );

pl.progressChanged(this, 20);
int i = 0;
int max = job.getParts().size();
for (JobPart p : job.getParts())
{
if (p instanceof Raster3dPart || p instanceof RasterPart)
{
p = convertRasterizableToVectorPart((RasterizableJobPart) p, p.getDPI(), getUseBidirectionalRastering());
}
if (p instanceof VectorPart)
{
//TODO: in direct mode use progress listener to indicate progress
//of individual job
writeVectorGCode((VectorPart) p, p.getDPI());
}
i++;
pl.progressChanged(this, 20 + (int) (i*(double) 60/max));
}
setSimpleStreamMode(wasSimpleMode);
writeShutdownCode();
disconnect(job.getName()+".gcode");
}
catch (IOException e) {
pl.taskChanged(this, "disconnecting");
disconnect(this.jobName);
throw e;
}
pl.taskChanged(this, "sent.");
pl.progressChanged(this, 100);
}

@Override
protected void setKeysMissingFromDeserialization()
{
super.setKeysMissingFromDeserialization();
commandLenQueue = new ArrayDeque<Integer>();
simpleStreamMode = true;
}

@Override
public Grbl clone()
{
Expand Down

0 comments on commit 025e4ec

Please sign in to comment.