Skip to content

Commit

Permalink
GCode-based driver improvements: Force laser off, Adjustable precision
Browse files Browse the repository at this point in the history
* Respect "Force laser off during G0" setting also when moving focus (Z axis)
* Added settings to configure number of digits used for XYZ as well as S gCodes to allow slimmer gCodes when desired as most machines won't need 6 trailing digits of precision. Less digits gives gCode processor more time to run its main loop.

Contributed by @McNugget6750
  • Loading branch information
McNugget6750 authored Dec 9, 2021
1 parent 5b7c43e commit 360f950
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
* This class implements a driver for a generic GRBL GCode Lasercutter.
* It should contain all possible options and is inteded to be the superclass
Expand Down Expand Up @@ -105,6 +108,8 @@ public class GenericGcodeDriver extends LaserCutter {
protected static final String SETTING_UPLOAD_METHOD = "Upload method";
protected static final String SETTING_RASTER_PADDING = "Extra padding at ends of raster scanlines (mm)";
protected static final String SETTING_API_KEY = "Api-Key/Password for Octoprint";
protected static final String SETTING_GCODE_DIGITS = "Decimal places used for XY coordinates";
protected static final String SETTING_SCODE_DIGITS = "Decimal places used for power (S) value";

protected static final Locale FORMAT_LOCALE = Locale.US;

Expand Down Expand Up @@ -458,6 +463,15 @@ public LaserProperty getLaserPropertyForRasterPart()
return new FloatPowerSpeedFocusProperty();
}

protected String formatDouble(double value, int decimalPlaces)
{
Locale locale = new Locale("en", "US");
DecimalFormat coordinateFormat = (DecimalFormat)NumberFormat.getNumberInstance(locale);
coordinateFormat.applyPattern("###.##");
coordinateFormat.setMaximumFractionDigits(decimalPlaces);
return coordinateFormat.format(value);
}

protected void writeVectorGCode(VectorPart vp, double resolution) throws UnsupportedEncodingException, IOException {
for (VectorCommand cmd : vp.getCommandList()) {
switch (cmd.getType()) {
Expand Down Expand Up @@ -494,45 +508,52 @@ 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)
{
sendLine("G0 Z%f", focus);
currentFocus = focus;

if (currentFocus != focus) {
String append = "";
if (blankLaserDuringRapids) {
append = " S0";
currentPower = -1; // set to invalid value to force new S-value at next G1
}
sendLine("G0 Z%s" + append, formatDouble(focus, getGCodeDigits()));
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();

if (blankLaserDuringRapids)
{
currentPower = 0.0;
sendLine("G0 X%f Y%f F%d S0", x, y, (int) (travel_speed));
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
{
sendLine("G0 X%f Y%f F%d", x, y, (int) (travel_speed));
sendLine("G0 X%s Y%s F%d", 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 = "";

if (nextPower != currentPower)
{
append += String.format(FORMAT_LOCALE, " S%f", nextPower);
append += String.format(FORMAT_LOCALE, " S%s", formatDouble(nextPower, getSCodeDigits()));
currentPower = nextPower;
}
if (nextSpeed != currentSpeed)
{
append += String.format(FORMAT_LOCALE, " F%d", (int) (max_speed*nextSpeed/100.0));
currentSpeed = nextSpeed;
}
sendLine("G1 X%f Y%f"+append, x, y);
sendLine("G1 X%s Y%s" + append, formatDouble(x, getGCodeDigits()), formatDouble(y, getGCodeDigits()));
}

private void writeInitializationCode() throws IOException {
Expand Down Expand Up @@ -1057,6 +1078,30 @@ public void setApiKey(String apiKey)
this.apiKey = apiKey;
}

private Integer gCodeDigits = 6;

public Integer getGCodeDigits()
{
if (gCodeDigits == null) gCodeDigits = 6;
return gCodeDigits;
}
public void setGCodeDigits(Integer gCodeDigits)
{
this.gCodeDigits = gCodeDigits;
}

private Integer sCodeDigits = 0;

public Integer getSCodeDigits()
{
if (sCodeDigits == null) sCodeDigits = 0;

This comment has been minimized.

Copy link
@t-oster

t-oster Jul 19, 2022

Owner

the default of 0 breaks existing settings.

return sCodeDigits;
}
public void setSCodeDigits(Integer sCodeDigits)
{
this.sCodeDigits = sCodeDigits;
}

private static final String[] SETTINGS_LIST = new String[]{
SETTING_UPLOAD_METHOD,
SETTING_BAUDRATE,
Expand Down Expand Up @@ -1084,7 +1129,9 @@ public void setApiKey(String apiKey)
SETTING_FILE_EXPORT_PATH,
SETTING_USE_BIDIRECTIONAL_RASTERING,
SETTING_RASTER_PADDING,
SETTING_API_KEY
SETTING_API_KEY,
SETTING_GCODE_DIGITS,
SETTING_SCODE_DIGITS
};

@Override
Expand Down Expand Up @@ -1148,6 +1195,10 @@ public Object getProperty(String attribute) {
return this.getRasterPadding();
} else if (SETTING_API_KEY.equals(attribute)) {
return this.getApiKey();
} else if (SETTING_GCODE_DIGITS.equals(attribute)) {
return this.getGCodeDigits();
} else if (SETTING_SCODE_DIGITS.equals(attribute)) {
return this.getSCodeDigits();
}

return null;
Expand Down Expand Up @@ -1209,6 +1260,10 @@ public void setProperty(String attribute, Object value) {
this.setRasterPadding(Math.abs((Double)value));
} else if (SETTING_API_KEY.equals(attribute)) {
this.setApiKey((String) value);
} else if (SETTING_GCODE_DIGITS.equals(attribute)) {
this.setGCodeDigits((Integer) value);
} else if (SETTING_SCODE_DIGITS.equals(attribute)) {
this.setSCodeDigits((Integer) value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected void move(PrintStream out, double x, double y, double resolution) thro
currentSpeed = getTravel_speed();
if (blankLaserDuringRapids)
{
currentPower = 0.0;
currentPower = -1; // set to invalid value to force new S-value at next G1
sendLine("G0 X%f Y%f S0", x, y);
}
else
Expand Down
142 changes: 71 additions & 71 deletions test-output/de.thomas_oster.liblasercut.drivers.GenericGcodeDriver.out
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
G21
G90
G0 X0.508000 Y0.508000 F3600
G1 X25.400000 Y50.800000 S1.000000 F1200
G1 X50.800000 Y0.000000
G0 X101.600000 Y0.000000 F3600
G1 X76.200000 Y5.080000 F1200
G1 X76.149200 Y20.320000
G1 X76.149200 Y22.860000
G1 X76.098400 Y25.400000
G1 X76.149200 Y30.480000
G1 X76.200000 Y45.720000
G1 X101.600000 Y50.800000
G0 X0.000000 Y1.879600 F3600
G1 X0.660400 Y1.879600 S0.000000 F1200
G1 X0.762000 Y1.879600 S1.000000
G1 X0.863600 Y1.879600
G1 X0.914400 Y1.879600
G1 X1.066800 Y1.879600
G1 X1.117600 Y1.879600
G1 X6.096000 Y1.879600 S0.000000
G0 X0.000000 Y1.930400 F3600
G1 X0.660400 Y1.930400 F1200
G1 X0.812800 Y1.930400 S1.000000
G1 X0.863600 Y1.930400
G1 X0.965200 Y1.930400
G1 X1.066800 Y1.930400
G1 X1.117600 Y1.930400
G1 X6.096000 Y1.930400 S0.000000
G0 X0.000000 Y1.981200 F3600
G1 X0.660400 Y1.981200 F1200
G1 X1.117600 Y1.981200 S1.000000
G1 X6.096000 Y1.981200 S0.000000
G0 X0.000000 Y2.032000 F3600
G1 X0.660400 Y2.032000 F1200
G1 X0.762000 Y2.032000 S1.000000
G1 X1.117600 Y2.032000
G1 X6.096000 Y2.032000 S0.000000
G0 X0.000000 Y2.082800 F3600
G1 X0.660400 Y2.082800 F1200
G1 X1.117600 Y2.082800 S1.000000
G1 X6.096000 Y2.082800 S0.000000
G0 X0.000000 Y2.133600 F3600
G1 X0.660400 Y2.133600 F1200
G1 X0.762000 Y2.133600 S1.000000
G1 X1.117600 Y2.133600
G1 X6.096000 Y2.133600 S0.000000
G0 X0.000000 Y3.911600 F3600
G1 X2.235200 Y3.911600 F1200
G1 X2.336800 Y3.911600 S1.000000
G1 X2.387600 Y3.911600 S0.000000
G1 X2.540000 Y3.911600 S1.000000
G1 X7.518400 Y3.911600 S0.000000
G0 X0.000000 Y3.962400 F3600
G1 X2.286000 Y3.962400 F1200
G1 X2.336800 Y3.962400 S1.000000
G1 X2.438400 Y3.962400 S0.000000
G1 X2.540000 Y3.962400 S1.000000
G1 X7.518400 Y3.962400 S0.000000
G0 X0.000000 Y4.013200 F3600
G1 X2.133600 Y4.013200 F1200
G1 X2.184400 Y4.013200 S0.750000
G1 X2.235200 Y4.013200 S0.000000
G1 X2.590800 Y4.013200 S0.750000
G1 X7.569200 Y4.013200 S0.000000
G0 X0.000000 Y4.064000 F3600
G1 X2.235200 Y4.064000 F1200
G1 X2.590800 Y4.064000 S0.500000
G1 X7.569200 Y4.064000 S0.000000
G0 X0.000000 Y4.165600 F3600
G1 X2.133600 Y4.165600 F1200
G1 X2.235200 Y4.165600 S1.000000
G1 X7.213600 Y4.165600 S0.000000
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 S1
G1 X2.2352 Y4.0132 S0
G1 X2.5908 Y4.0132 S1
G1 X7.5692 Y4.0132 S0
G0 X0 Y4.064 F3600
G1 X2.2352 Y4.064 F1200
G1 X2.5908 Y4.064 S0
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
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 @@
G21G90M3G0X0.508000Y0.508000S0G1X25.400000Y50.800000S1000.000000F1200G1X50.800000Y0.000000G0X101.600000Y0.000000S0G1X76.200000Y5.080000S1000.000000F1200G1X76.149200Y20.320000G1X76.149200Y22.860000G1X76.098400Y25.400000G1X76.149200Y30.480000G1X76.200000Y45.720000G1X101.600000Y50.800000G0X0.000000Y1.879600S0G1X0.660400Y1.879600F1200G1X0.762000Y1.879600S1000.000000G1X0.863600Y1.879600G1X0.914400Y1.879600G1X1.066800Y1.879600G1X1.117600Y1.879600G1X6.096000Y1.879600S0.000000G0X0.000000Y1.930400S0G1X0.660400Y1.930400F1200G1X0.812800Y1.930400S1000.000000G1X0.863600Y1.930400G1X0.965200Y1.930400G1X1.066800Y1.930400G1X1.117600Y1.930400G1X6.096000Y1.930400S0.000000G0X0.000000Y1.981200S0G1X0.660400Y1.981200F1200G1X1.117600Y1.981200S1000.000000G1X6.096000Y1.981200S0.000000G0X0.000000Y2.032000S0G1X0.660400Y2.032000F1200G1X0.762000Y2.032000S1000.000000G1X1.117600Y2.032000G1X6.096000Y2.032000S0.000000G0X0.000000Y2.082800S0G1X0.660400Y2.082800F1200G1X1.117600Y2.082800S1000.000000G1X6.096000Y2.082800S0.000000G0X0.000000Y2.133600S0G1X0.660400Y2.133600F1200G1X0.762000Y2.133600S1000.000000G1X1.117600Y2.133600G1X6.096000Y2.133600S0.000000G0X0.000000Y3.911600S0G1X2.235200Y3.911600F1200G1X2.336800Y3.911600S1000.000000G1X2.387600Y3.911600S0.000000G1X2.540000Y3.911600S1000.000000G1X7.518400Y3.911600S0.000000G0X0.000000Y3.962400S0G1X2.286000Y3.962400F1200G1X2.336800Y3.962400S1000.000000G1X2.438400Y3.962400S0.000000G1X2.540000Y3.962400S1000.000000G1X7.518400Y3.962400S0.000000G0X0.000000Y4.013200S0G1X2.133600Y4.013200F1200G1X2.184400Y4.013200S750.000000G1X2.235200Y4.013200S0.000000G1X2.590800Y4.013200S750.000000G1X7.569200Y4.013200S0.000000G0X0.000000Y4.064000S0G1X2.235200Y4.064000F1200G1X2.590800Y4.064000S500.000000G1X7.569200Y4.064000S0.000000G0X0.000000Y4.165600S0G1X2.133600Y4.165600F1200G1X2.235200Y4.165600S1000.000000G1X7.213600Y4.165600S0.000000M5G0X0Y0
G21G90M3G0X0.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
Expand Down
Loading

0 comments on commit 360f950

Please sign in to comment.