Skip to content

Commit

Permalink
Bugfixes for TinyG (#2212)
Browse files Browse the repository at this point in the history
* Minor adjustments for logging to make it easier to debug.
* Fixed minor problem with entity comparator not behaving as expected when sorting spatially
* Make sure tinyg is using single step mode
* Fix problem with tinyg not liking empty lines
* Fix problem with position not being immutable
  • Loading branch information
breiler authored May 3, 2023
1 parent a7396d7 commit 08cb7f3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/UGS_Platform.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class TinyGUtils {

public static final byte COMMAND_PAUSE = '!';
public static final byte COMMAND_RESUME = '~';
public static final byte COMMAND_STATUS = '?';
public static final byte COMMAND_QUEUE_FLUSH = '%';
public static final byte COMMAND_KILL_JOB = 0x04;
public static final byte COMMAND_ENQUIRE_STATUS = 0x05;
Expand Down Expand Up @@ -175,7 +174,7 @@ public static ControllerStatus updateControllerStatus(final ControllerStatus las
if (isStatusResponse(response)) {
JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_REPORT);

Position workCoord = lastControllerStatus.getWorkCoord();
Position workCoord = new Position(lastControllerStatus.getWorkCoord());
UnitUtils.Units feedSpeedUnits = lastControllerStatus.getFeedSpeedUnits();
if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_UNIT)) {
UnitUtils.Units units = statusResultObject.get(FIELD_STATUS_REPORT_UNIT).getAsInt() == 1 ? UnitUtils.Units.MM : UnitUtils.Units.INCH;
Expand All @@ -196,7 +195,7 @@ public static ControllerStatus updateControllerStatus(final ControllerStatus las
}

// The machine coordinates are always in MM, make sure the position is using that unit before updating the values
Position machineCoord = lastControllerStatus.getMachineCoord().getPositionIn(UnitUtils.Units.MM);
Position machineCoord = new Position(lastControllerStatus.getMachineCoord().getPositionIn(UnitUtils.Units.MM));
if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_MPOX)) {
machineCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_MPOX).getAsDouble());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This file is part of Universal Gcode Sender (UGS).
public class TinyGCommunicator extends BufferedCommunicator {

public TinyGCommunicator() {
setSingleStepMode(true);
super.setSingleStepMode(true);
}

@Override
Expand All @@ -38,4 +38,8 @@ public int getBufferSize() {
protected void sendingCommand(String response) {
// no-op for this protocol.
}

public void setSingleStepMode(boolean enable) {
// Do not allow changing this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public static Integer getResponseStatusCode(String response) {

private static String convertCommandToJson(String command) {
String ret;

// wrap in json
if ("\n".equals(command) ||
if (StringUtils.isEmpty(command) ||
"\n".equals(command) ||
"\r\n".equals(command) ||
"?".equals(command)) {
// this is a status request cmd
Expand Down Expand Up @@ -82,19 +84,6 @@ public static boolean isRecieveQueueReportResponse(String response) {
public void appendResponse(String response) {
super.appendResponse(response);


/*JsonObject jo = TinyGUtils.jsonToObject(response);
if(TinyGUtils.isStatusResponse(jo))
if (TinyGUtils.isErrorResponse(jo)) {
command.setOk(false);
command.setError(true);
} else {
command.setOk(true);
command.setError(false);
}
command.setDone(true);*/

if (TinyGGcodeCommand.isOkErrorResponse(response)) {
Integer responseStatusCode = getResponseStatusCode(response);
if (responseStatusCode == 0) {
Expand All @@ -104,5 +93,21 @@ public void appendResponse(String response) {
}
setDone(true);
}

if (isOkTextResponse(response)) {
setOk(true);
setDone(true);
} else if (isErrorTextResponse(response)) {
setError(true);
setDone(true);
}
}

private static boolean isErrorTextResponse(String response) {
return response.startsWith("tinyg [mm] err") || response.startsWith("tinyg [inch] err");
}

private static boolean isOkTextResponse(String response) {
return response.startsWith("tinyg [mm] ok") || response.startsWith("tinyg [inch] ok");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,43 +67,30 @@ private static Coordinate getLastCoordinate(Geometry geometry) {
return coordinates[coordinates.length - 1];
}

/**
* Returns the square of the distance between two coordinates.
*
* @param c1 the first coordinate
* @param c2 the second coordinate
* @return the distance squared
*/
private static double distanceSq(Coordinate c1, Coordinate c2) {
double px = c1.getX() - c2.getX();
double py = c1.getY() - c2.getY();
return (px * px + py * py);
}

@Override
public int compare(Geometry o1, Geometry o2) {
int order = 0;
Coordinate lastCoordinate = getLastCoordinate(o1);
Coordinate nextCoordinate = getFirstCoordinate(o2);

double squaredDistance = distanceSq(lastCoordinate, nextCoordinate);
if (squaredDistance < 0) order -= 1;
if (squaredDistance > 0) order += 1;

double gridWidth = envelope.getWidth() / NUMBER_OF_GRIDS;
double gridHeight = envelope.getHeight() / NUMBER_OF_GRIDS;

long e1GridX = Math.round((lastCoordinate.getX() - envelope.getMinX()) / gridWidth);
long e1GridY = Math.round((lastCoordinate.getY() - envelope.getMinY()) / gridHeight);

long e2GridX = Math.round((nextCoordinate.getX() - envelope.getMinX()) / gridWidth);
long e2GridY = Math.round((nextCoordinate.getY() - envelope.getMinY()) / gridHeight);

if (e1GridX < e2GridX) order -= 10;
if (e1GridX > e2GridX) order += 10;
if (e1GridY < e2GridY) order -= 100;
if (e1GridY > e2GridY) order += 100;

int order = 0;
try {
Coordinate lastCoordinate = getLastCoordinate(o1);
Coordinate nextCoordinate = getFirstCoordinate(o2);

double gridWidth = envelope.getWidth() / NUMBER_OF_GRIDS;
double gridHeight = envelope.getHeight() / NUMBER_OF_GRIDS;

long e1GridX = Math.round((lastCoordinate.getX() - envelope.getMinX()) / gridWidth);
long e1GridY = Math.round((lastCoordinate.getY() - envelope.getMinY()) / gridHeight);

long e2GridX = Math.round((nextCoordinate.getX() - envelope.getMinX()) / gridWidth);
long e2GridY = Math.round((nextCoordinate.getY() - envelope.getMinY()) / gridHeight);

if (e1GridX < e2GridX) order -= 1;
if (e1GridX > e2GridX) order += 1;
if (e1GridY < e2GridY) order -= 10;
if (e1GridY > e2GridY) order += 10;
} catch (Exception e) {
e.printStackTrace();
}
return order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void exportAndLoadGcode(String name) {
designWriter.write(file, ControllerFactory.getController());
CentralLookup.getDefault().lookup(BackendAPI.class).setGcodeFile(file);
} catch (Exception e) {
throw new RuntimeException("Could not generate gcode");
throw new RuntimeException("Could not generate gcode", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This file is part of Universal Gcode Sender (UGS).
import org.openide.util.ImageUtilities;

import java.awt.*;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
Expand All @@ -55,6 +56,7 @@ This file is part of Universal Gcode Sender (UGS).
path = "Toolbars/Connection",
position = 980)})
public class FirmwareAction extends CallableSystemAction implements UGSEventListener {
private static final Logger LOGGER = Logger.getLogger(FirmwareAction.class.getSimpleName());
public static final String ICON_BASE = "resources/icons/firmware.svg";

private final BackendAPI backend;
Expand All @@ -75,7 +77,7 @@ private void setFirmware() {
}

private void firmwareUpdated() {
System.out.println("firmware updated " + backend.getSettings().getFirmwareVersion());
LOGGER.info("Changed to firmware " + backend.getSettings().getFirmwareVersion());
firmwareCombo.setSelectedItem( backend.getSettings().getFirmwareVersion());
}

Expand Down

0 comments on commit 08cb7f3

Please sign in to comment.