From 9456dfabcbeb6238829f1daa15b76eb971fe437e Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Tue, 10 Jul 2018 10:02:41 +0200 Subject: [PATCH 01/13] Added homing command with gcode "G28.2 X0 Y0 Z0" --- .../com/willwinder/universalgcodesender/TinyGController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java index f16e7976d..ff50a5c61 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java @@ -255,7 +255,7 @@ private void sendInitCommands() { @Override public void performHomingCycle() throws Exception { - throw new UnsupportedOperationException(NOT_SUPPORTED_YET); + sendCommandImmediately(new GcodeCommand("G28.2 X0 Y0 Z0")); } @Override From ee1756d4ed0419272a5bae2ece836286fd515240 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Wed, 11 Jul 2018 15:58:16 +0200 Subject: [PATCH 02/13] Added support for parsing gcode states from the responses. Added support for changing work coordinates. --- .../universalgcodesender/TinyGController.java | 24 +- .../universalgcodesender/TinyGUtils.java | 206 ++++++++++++++-- .../universalgcodesender/TinyGUtilsTest.java | 229 ++++++++++++++++++ 3 files changed, 428 insertions(+), 31 deletions(-) create mode 100644 ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java index ff50a5c61..4d5ca1188 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java @@ -143,11 +143,6 @@ protected Boolean isIdleEvent() { return getControlState() == COMM_IDLE || getControlState() == COMM_CHECK; } - @Override - public void restoreParserModalState() { - // no op - } - @Override protected void rawResponseHandler(String response) { JsonObject jo; @@ -215,8 +210,10 @@ private void updateControllerStatus(JsonObject jo) { ControllerState previousState = controllerStatus.getState(); UGSEvent.ControlState previousControlState = getControlState(previousState); + TinyGUtils.updateGcodeState(this, jo); + // Notify our listeners about the new status - controllerStatus = TinyGUtils.updateControllerStatus(controllerStatus, jo); + controllerStatus = TinyGUtils.updateControllerStatus(controllerStatus, getCurrentGcodeState(), jo); dispatchStatusString(controllerStatus); // Notify state change to our listeners @@ -244,6 +241,9 @@ private void sendInitCommands() { // 0=off, 1=filtered, 2=verbose comm.queueStringForComm("{sv:1}"); + // Configure status reports + comm.queueStringForComm("{sr:{posx:t, posy:t, posz:t, mpox:t, mpoy:t, mpoz:t, plan:t, vel:t, unit:t, stat:t, dist:t, admo:t, frmo:t, coor:t}}"); + // Request initial status report comm.queueStringForComm("{sr:n}"); @@ -260,7 +260,8 @@ public void performHomingCycle() throws Exception { @Override public void resetCoordinatesToZero() throws Exception { - throw new UnsupportedOperationException(NOT_SUPPORTED_YET); + String command = TinyGUtils.generateResetCoordinatesToZeroCommand(controllerStatus, getCurrentGcodeState()); + sendCommandImmediately(new GcodeCommand(command)); } @Override @@ -291,11 +292,7 @@ public void viewParserState() throws Exception { @Override public void updateParserModalState(GcodeCommand command) { - if (command.getCommandString().startsWith("{\"gc")) { - String gcode = StringUtils.substringBetween(command.getCommandString(), "{\"gc\":\"", "\""); - GcodeCommand gcodeCommand = new GcodeCommand(gcode); - super.updateParserModalState(gcodeCommand); - } + // This is handled by the rawResponseHandler } @Override @@ -311,7 +308,8 @@ public void softReset() throws Exception { @Override public void setWorkPosition(Axis axis, double position) throws Exception { - throw new UnsupportedOperationException(NOT_SUPPORTED_YET); + String command = TinyGUtils.generateSetWorkPositionCommand(controllerStatus, getCurrentGcodeState(), axis, position); + sendCommandImmediately(new GcodeCommand(command)); } @Override diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index 257637564..d5ba07108 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -20,12 +20,18 @@ This file is part of Universal Gcode Sender (UGS). import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import com.willwinder.universalgcodesender.gcode.GcodeState; +import com.willwinder.universalgcodesender.gcode.util.Code; +import com.willwinder.universalgcodesender.gcode.util.Plane; import com.willwinder.universalgcodesender.listeners.ControllerState; import com.willwinder.universalgcodesender.listeners.ControllerStatus; +import com.willwinder.universalgcodesender.model.Axis; import com.willwinder.universalgcodesender.model.Position; import com.willwinder.universalgcodesender.model.UnitUtils; import org.apache.commons.lang3.StringUtils; +import static com.willwinder.universalgcodesender.gcode.util.Code.G20; + /** * Common utils for TinyG controllers * @@ -45,16 +51,24 @@ public class TinyGUtils { public static final String COMMAND_STATUS_REPORT = "{sr:n}"; public static final String COMMAND_KILL_ALARM_LOCK = "{clear:n}"; - public static final String FIELD_FIRMWARE_VERSION = "fv"; - public static final String FIELD_RESPONSE = "r"; + private static final String FIELD_FIRMWARE_VERSION = "fv"; + private static final String FIELD_RESPONSE = "r"; public static final String FIELD_STATUS_RESULT = "sr"; - public static final String FIELD_STATUS_RESULT_UNIT = "unit"; - public static final String FIELD_STATUS_RESULT_POSX = "posx"; - public static final String FIELD_STATUS_REPORT_POSY = "posy"; - public static final String FIELD_STATUS_REPORT_POSZ = "posz"; - public static final String FIELD_STATUS_REPORT_VELOCITY = "vel"; - public static final String FIELD_STATUS_REPORT_STATUS = "stat"; + private static final String FIELD_STATUS_RESULT_UNIT = "unit"; + private static final String FIELD_STATUS_RESULT_POSX = "posx"; + private static final String FIELD_STATUS_REPORT_POSY = "posy"; + private static final String FIELD_STATUS_REPORT_POSZ = "posz"; + private static final String FIELD_STATUS_REPORT_VELOCITY = "vel"; + private static final String FIELD_STATUS_REPORT_COORD = "coor"; + private static final String FIELD_STATUS_REPORT_PLANE = "plan"; + private static final String FIELD_STATUS_REPORT_DISTANCE_MODE = "dist"; + private static final String FIELD_STATUS_REPORT_ARC_DISTANCE_MODE = "admo"; + private static final String FIELD_STATUS_REPORT_FEED_MODE = "frmo"; + private static final String FIELD_STATUS_REPORT_STATUS = "stat"; + private static final String FIELD_STATUS_REPORT_MPOX = "mpox"; + private static final String FIELD_STATUS_REPORT_MPOY = "mpoy"; + private static final String FIELD_STATUS_REPORT_MPOZ = "mpoz"; private static JsonParser parser = new JsonParser(); @@ -98,7 +112,7 @@ public static boolean isReadyResponse(JsonObject response) { JsonObject jo = response.getAsJsonObject(FIELD_RESPONSE); if (jo.has("msg")) { String msg = jo.get("msg").getAsString(); - return StringUtils.equals(msg,"SYSTEM READY"); + return StringUtils.equals(msg, "SYSTEM READY"); } } return false; @@ -115,26 +129,36 @@ public static boolean isStatusResponse(JsonObject response) { * @param response the response string from the controller * @return a new updated controller status */ - public static ControllerStatus updateControllerStatus(final ControllerStatus lastControllerStatus, final JsonObject response) { + public static ControllerStatus updateControllerStatus(final ControllerStatus lastControllerStatus, final GcodeState lastGcodeState, final JsonObject response) { if (response.has(FIELD_STATUS_RESULT)) { JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_RESULT); - Position machineCoord = lastControllerStatus.getMachineCoord(); + UnitUtils.Units currentUnits = lastGcodeState.units == G20 ? UnitUtils.Units.INCH : UnitUtils.Units.MM; + + Position workCoord = lastControllerStatus.getWorkCoord().getPositionIn(currentUnits); if (statusResultObject.has(FIELD_STATUS_RESULT_POSX)) { - machineCoord.setX(statusResultObject.get(FIELD_STATUS_RESULT_POSX).getAsDouble()); + workCoord.setX(statusResultObject.get(FIELD_STATUS_RESULT_POSX).getAsDouble()); } if (statusResultObject.has(FIELD_STATUS_REPORT_POSY)) { - machineCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_POSY).getAsDouble()); + workCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_POSY).getAsDouble()); } if (statusResultObject.has(FIELD_STATUS_REPORT_POSZ)) { - machineCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_POSZ).getAsDouble()); + workCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_POSZ).getAsDouble()); + } + + Position machineCoord = lastControllerStatus.getMachineCoord().getPositionIn(UnitUtils.Units.MM); + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOX)) { + machineCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_MPOX).getAsDouble()); + } + + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOY)) { + machineCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_MPOY).getAsDouble()); } - if (statusResultObject.has(FIELD_STATUS_RESULT_UNIT)) { - UnitUtils.Units units = statusResultObject.get(FIELD_STATUS_RESULT_UNIT).getAsInt() == 0 ? UnitUtils.Units.INCH : UnitUtils.Units.MM; - machineCoord = new Position(machineCoord.getX(), machineCoord.getY(), machineCoord.getZ(), units); + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOZ)) { + machineCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_MPOZ).getAsDouble()); } Double feedSpeed = lastControllerStatus.getFeedSpeed(); @@ -155,7 +179,7 @@ public static ControllerStatus updateControllerStatus(final ControllerStatus las ControllerStatus.EnabledPins enabledPins = lastControllerStatus.getEnabledPins(); ControllerStatus.AccessoryStates accessoryStates = lastControllerStatus.getAccessoryStates(); - return new ControllerStatus(stateString, state, machineCoord, machineCoord, feedSpeed, spindleSpeed, overrides, workCoordinateOffset, enabledPins, accessoryStates); + return new ControllerStatus(stateString, state, machineCoord.getPositionIn(currentUnits), workCoord.getPositionIn(currentUnits), feedSpeed, spindleSpeed, overrides, workCoordinateOffset, enabledPins, accessoryStates); } return lastControllerStatus; @@ -206,4 +230,150 @@ private static String getStateAsString(int state) { ControllerState controllerState = getState(state); return controllerState.name(); } + + /** + * Generates a command for resetting the coordinates for the current coordinate system to zero. + * + * @param controllerStatus the current controller status + * @param gcodeState the current gcode state + * @return a string with the command to reset the coordinate system to zero + */ + public static String generateResetCoordinatesToZeroCommand(ControllerStatus controllerStatus, GcodeState gcodeState) { + int offsetCode = convertOffsetGcodeToCode(gcodeState.offset); + Position machineCoord = controllerStatus.getMachineCoord(); + return "G10 L2 P" + offsetCode + + " X" + Utils.formatter.format(machineCoord.get(Axis.X)) + + " Y" + Utils.formatter.format(machineCoord.get(Axis.Y)) + + " Z" + Utils.formatter.format(machineCoord.get(Axis.Z)); + } + + /** + * Generates a command for setting the axis to a position in the current coordinate system + * + * @param controllerStatus the current controller status + * @param gcodeState the current gcode state + * @param axis the axis to set + * @param position the position to set + * @return a command for setting the position + */ + public static String generateSetWorkPositionCommand(ControllerStatus controllerStatus, GcodeState gcodeState, Axis axis, double position) { + int offsetCode = convertOffsetGcodeToCode(gcodeState.offset); + Position machineCoord = controllerStatus.getMachineCoord(); + double coordinate = -(position - machineCoord.get(axis)); + return "G10 L2 P" + offsetCode + " " + + axis.name() + Utils.formatter.format(coordinate); + } + + private static int convertOffsetGcodeToCode(Code offsetGcode) { + switch (offsetGcode) { + case G54: + return 1; + case G55: + return 2; + case G56: + return 3; + case G57: + return 4; + case G58: + return 5; + case G59: + return 6; + default: + return 0; + } + } + + private static Code convertOffsetCodeToGcode(int offsetCode) { + switch (offsetCode) { + case 1: + return Code.G54; + case 2: + return Code.G55; + case 3: + return Code.G56; + case 4: + return Code.G57; + case 5: + return Code.G58; + case 6: + return Code.G59; + default: + return Code.G53; + } + } + + /** + * Updates the Gcode state from the response if it contains a status line + * + * @param controller the controller to update + * @param response the response to parse the gcode state from + */ + public static void updateGcodeState(TinyGController controller, JsonObject response) { + if (response.has(TinyGUtils.FIELD_STATUS_RESULT)) { + GcodeState gcodeState = controller.getCurrentGcodeState(); + JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_RESULT); + + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { + int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); + gcodeState.offset = TinyGUtils.convertOffsetCodeToGcode(offsetCode); + } + + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_RESULT_UNIT)) { + int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_RESULT_UNIT).getAsInt(); + // 0=inch, 1=mm + if (units == 0) { + gcodeState.units = Code.G20; + controller.setUnitsCode(Code.G20.name()); + } else { + gcodeState.units = Code.G21; + controller.setUnitsCode(Code.G21.name()); + } + } + + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_PLANE)) { + int plane = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_PLANE).getAsInt(); + // 0=XY plane, 1=XZ plane, 2=YZ plane + if (plane == 0) { + gcodeState.plane = Plane.XY; + } else if (plane == 1) { + gcodeState.plane = Plane.ZX; + } else if (plane == 2) { + gcodeState.plane = Plane.YZ; + } + } + + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE)) { + int feedMode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE).getAsInt(); + // 0=units-per-minute-mode, 1=inverse-time-mode + if (feedMode == 0) { + gcodeState.feedMode = Code.G93; + } else if (feedMode == 1) { + gcodeState.feedMode = Code.G94; + } + } + + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE)) { + int distance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE).getAsInt(); + // 0=absolute distance mode, 1=incremental distance mode + if (distance == 0) { + gcodeState.distanceMode = Code.G90; + controller.setDistanceModeCode(Code.G90.name()); + } else if (distance == 1) { + gcodeState.distanceMode = Code.G91; + controller.setDistanceModeCode(Code.G91.name()); + } + } + + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE)) { + int arcDistance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE).getAsInt(); + // 0=absolute distance mode, 1=incremental distance mode + if (arcDistance == 0) { + gcodeState.arcDistanceMode = Code.G90_1; + } else if (arcDistance == 1) { + gcodeState.arcDistanceMode = Code.G91_1; + } + } + } + + } } diff --git a/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java b/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java new file mode 100644 index 000000000..065d38155 --- /dev/null +++ b/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java @@ -0,0 +1,229 @@ +/* + Copyright 2018 Will Winder + + This file is part of Universal Gcode Sender (UGS). + + UGS is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + UGS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with UGS. If not, see . + */ +package com.willwinder.universalgcodesender; + +import com.google.gson.JsonObject; +import com.willwinder.universalgcodesender.gcode.GcodeState; +import com.willwinder.universalgcodesender.gcode.util.Code; +import com.willwinder.universalgcodesender.gcode.util.Plane; +import com.willwinder.universalgcodesender.listeners.ControllerState; +import com.willwinder.universalgcodesender.listeners.ControllerStatus; +import com.willwinder.universalgcodesender.model.Axis; +import com.willwinder.universalgcodesender.model.Position; +import com.willwinder.universalgcodesender.model.UnitUtils; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests for the TinyGUtils class + * + * @author Joacim Breiler + */ +public class TinyGUtilsTest { + + @Test + public void generateSetWorkPositionCommandShouldGenerateGcode() { + ControllerStatus controllerStatus = new ControllerStatus("", ControllerState.UNKNOWN, new Position(10, 10, 10, UnitUtils.Units.MM), new Position(10, 10, 10, UnitUtils.Units.MM)); + GcodeState gcodeState = new GcodeState(); + + gcodeState.offset = Code.G54; + String command = TinyGUtils.generateSetWorkPositionCommand(controllerStatus, gcodeState, Axis.X, 5); + assertEquals("G10 L2 P1 X5", command); + + gcodeState.offset = Code.G55; + command = TinyGUtils.generateSetWorkPositionCommand(controllerStatus, gcodeState, Axis.Y, 15); + assertEquals("G10 L2 P2 Y-5", command); + + gcodeState.offset = Code.G56; + command = TinyGUtils.generateSetWorkPositionCommand(controllerStatus, gcodeState, Axis.Z, 0); + assertEquals("G10 L2 P3 Z10", command); + + gcodeState.offset = Code.G57; + command = TinyGUtils.generateSetWorkPositionCommand(controllerStatus, gcodeState, Axis.Z, 0); + assertEquals("G10 L2 P4 Z10", command); + + gcodeState.offset = Code.G58; + command = TinyGUtils.generateSetWorkPositionCommand(controllerStatus, gcodeState, Axis.Z, 0); + assertEquals("G10 L2 P5 Z10", command); + + gcodeState.offset = Code.G59; + command = TinyGUtils.generateSetWorkPositionCommand(controllerStatus, gcodeState, Axis.Z, 0); + assertEquals("G10 L2 P6 Z10", command); + } + + @Test + public void generateResetCoordinatesToZeroCommandShouldGenerateGcode() { + ControllerStatus controllerStatus = new ControllerStatus("", ControllerState.UNKNOWN, new Position(10, 10, 10, UnitUtils.Units.MM), new Position(10, 10, 10, UnitUtils.Units.MM)); + GcodeState gcodeState = new GcodeState(); + + gcodeState.offset = Code.G54; + String command = TinyGUtils.generateResetCoordinatesToZeroCommand(controllerStatus, gcodeState); + assertEquals("G10 L2 P1 X10 Y10 Z10", command); + + gcodeState.offset = Code.G55; + command = TinyGUtils.generateResetCoordinatesToZeroCommand(controllerStatus, gcodeState); + assertEquals("G10 L2 P2 X10 Y10 Z10", command); + } + + @Test + public void updateGcodeStateShouldUpdateOffset() { + // Given + GcodeState gcodeState = new GcodeState(); + TinyGController controller = mock(TinyGController.class); + when(controller.getCurrentGcodeState()).thenReturn(gcodeState); + + // When + JsonObject response = TinyGUtils.jsonToObject("{sr:{coor:2}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G55, gcodeState.offset); + } + + @Test + public void updateGcodeStateShouldUpdateUnit() { + // Given + GcodeState gcodeState = new GcodeState(); + TinyGController controller = mock(TinyGController.class); + when(controller.getCurrentGcodeState()).thenReturn(gcodeState); + + // When switch to inch + JsonObject response = TinyGUtils.jsonToObject("{sr:{unit:1}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G21, gcodeState.units); + verify(controller).setUnitsCode("G21"); + + + // When switch to mm + response = TinyGUtils.jsonToObject("{sr:{unit:0}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G20, gcodeState.units); + verify(controller).setUnitsCode("G20"); + } + + + @Test + public void updateGcodeStateShouldUpdatePlane() { + // Given + GcodeState gcodeState = new GcodeState(); + TinyGController controller = mock(TinyGController.class); + when(controller.getCurrentGcodeState()).thenReturn(gcodeState); + + // When switch XY + JsonObject response = TinyGUtils.jsonToObject("{sr:{plan:0}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Plane.XY, gcodeState.plane); + + + // When switch to ZX + response = TinyGUtils.jsonToObject("{sr:{plan:1}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Plane.ZX, gcodeState.plane); + + + // When switch to YZ + response = TinyGUtils.jsonToObject("{sr:{plan:2}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Plane.YZ, gcodeState.plane); + } + + @Test + public void updateGcodeStateShouldUpdateFeedMode() { + // Given + GcodeState gcodeState = new GcodeState(); + TinyGController controller = mock(TinyGController.class); + when(controller.getCurrentGcodeState()).thenReturn(gcodeState); + + // When switch to units per minute mode + JsonObject response = TinyGUtils.jsonToObject("{sr:{frmo:0}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G93, gcodeState.feedMode); + + + // When switch to inverse time mode + response = TinyGUtils.jsonToObject("{sr:{frmo:1}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G94, gcodeState.feedMode); + } + + + @Test + public void updateGcodeStateShouldUpdateDistanceMode() { + // Given + GcodeState gcodeState = new GcodeState(); + TinyGController controller = mock(TinyGController.class); + when(controller.getCurrentGcodeState()).thenReturn(gcodeState); + + // When switch to units per minute mode + JsonObject response = TinyGUtils.jsonToObject("{sr:{dist:0}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G90, gcodeState.distanceMode); + + + // When switch to inverse time mode + response = TinyGUtils.jsonToObject("{sr:{dist:1}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G91, gcodeState.distanceMode); + } + + @Test + public void updateGcodeStateShouldUpdateArcDistanceMode() { + // Given + GcodeState gcodeState = new GcodeState(); + TinyGController controller = mock(TinyGController.class); + when(controller.getCurrentGcodeState()).thenReturn(gcodeState); + + // When switch to units per minute mode + JsonObject response = TinyGUtils.jsonToObject("{sr:{admo:0}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G90_1, gcodeState.arcDistanceMode); + + + // When switch to inverse time mode + response = TinyGUtils.jsonToObject("{sr:{admo:1}}"); + TinyGUtils.updateGcodeState(controller, response); + + // Then + assertEquals(Code.G91_1, gcodeState.arcDistanceMode); + } +} From 347b2225dfacb7eda4568507905626f8d5c2d875 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 12 Jul 2018 07:35:58 +0200 Subject: [PATCH 03/13] Now updates the gcode state using the gcode parser --- .../universalgcodesender/TinyGController.java | 18 ++-- .../universalgcodesender/TinyGUtils.java | 46 ++++----- .../universalgcodesender/TinyGUtilsTest.java | 95 +++++++------------ 3 files changed, 66 insertions(+), 93 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java index 6cccd7915..6777f17ab 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java @@ -36,6 +36,7 @@ This file is part of Universal Gcode Sender (UGS). import org.apache.commons.lang3.StringUtils; import java.io.File; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -211,7 +212,9 @@ private void updateControllerStatus(JsonObject jo) { ControllerState previousState = controllerStatus.getState(); UGSEvent.ControlState previousControlState = getControlState(previousState); - TinyGUtils.updateGcodeState(this, jo); + // Update the internal state + List gcodeList = TinyGUtils.convertStatusReportToGcode(jo); + gcodeList.forEach(gcode -> updateParserModalState(new GcodeCommand(gcode))); // Notify our listeners about the new status controllerStatus = TinyGUtils.updateControllerStatus(controllerStatus, getCurrentGcodeState(), jo); @@ -254,6 +257,14 @@ private void sendInitCommands() { setStatusUpdateRate(getStatusUpdateRate()); } + @Override + public void updateParserModalState(GcodeCommand command) { + // Prevent internal TinyG commands to update the parser modal state + if(!command.getCommandString().startsWith("{")) { + super.updateParserModalState(command); + } + } + @Override public void performHomingCycle() throws Exception { sendCommandImmediately(new GcodeCommand("G28.2 X0 Y0 Z0")); @@ -291,11 +302,6 @@ public void viewParserState() throws Exception { } } - @Override - public void updateParserModalState(GcodeCommand command) { - // This is handled by the rawResponseHandler - } - @Override public void softReset() throws Exception { // TODO This doesn't work, it will disconnect from the host diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index d5ba07108..704ec87b1 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -22,7 +22,6 @@ This file is part of Universal Gcode Sender (UGS). import com.google.gson.JsonParser; import com.willwinder.universalgcodesender.gcode.GcodeState; import com.willwinder.universalgcodesender.gcode.util.Code; -import com.willwinder.universalgcodesender.gcode.util.Plane; import com.willwinder.universalgcodesender.listeners.ControllerState; import com.willwinder.universalgcodesender.listeners.ControllerStatus; import com.willwinder.universalgcodesender.model.Axis; @@ -30,6 +29,9 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.model.UnitUtils; import org.apache.commons.lang3.StringUtils; +import java.util.ArrayList; +import java.util.List; + import static com.willwinder.universalgcodesender.gcode.util.Code.G20; /** @@ -50,11 +52,9 @@ public class TinyGUtils { public static final String COMMAND_STATUS_REPORT = "{sr:n}"; public static final String COMMAND_KILL_ALARM_LOCK = "{clear:n}"; - + public static final String FIELD_STATUS_RESULT = "sr"; private static final String FIELD_FIRMWARE_VERSION = "fv"; private static final String FIELD_RESPONSE = "r"; - - public static final String FIELD_STATUS_RESULT = "sr"; private static final String FIELD_STATUS_RESULT_UNIT = "unit"; private static final String FIELD_STATUS_RESULT_POSX = "posx"; private static final String FIELD_STATUS_REPORT_POSY = "posy"; @@ -305,28 +305,26 @@ private static Code convertOffsetCodeToGcode(int offsetCode) { /** * Updates the Gcode state from the response if it contains a status line * - * @param controller the controller to update - * @param response the response to parse the gcode state from + * @param response the response to parse the gcode state from + * @return a list of gcodes representing the state of the controllers */ - public static void updateGcodeState(TinyGController controller, JsonObject response) { + public static List convertStatusReportToGcode(JsonObject response) { + List gcodeList = new ArrayList<>(); if (response.has(TinyGUtils.FIELD_STATUS_RESULT)) { - GcodeState gcodeState = controller.getCurrentGcodeState(); JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_RESULT); if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); - gcodeState.offset = TinyGUtils.convertOffsetCodeToGcode(offsetCode); + gcodeList.add(TinyGUtils.convertOffsetCodeToGcode(offsetCode).name()); } if (statusResultObject.has(TinyGUtils.FIELD_STATUS_RESULT_UNIT)) { int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_RESULT_UNIT).getAsInt(); // 0=inch, 1=mm if (units == 0) { - gcodeState.units = Code.G20; - controller.setUnitsCode(Code.G20.name()); + gcodeList.add(Code.G20.name()); } else { - gcodeState.units = Code.G21; - controller.setUnitsCode(Code.G21.name()); + gcodeList.add(Code.G21.name()); } } @@ -334,11 +332,11 @@ public static void updateGcodeState(TinyGController controller, JsonObject respo int plane = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_PLANE).getAsInt(); // 0=XY plane, 1=XZ plane, 2=YZ plane if (plane == 0) { - gcodeState.plane = Plane.XY; + gcodeList.add(Code.G17.name()); } else if (plane == 1) { - gcodeState.plane = Plane.ZX; + gcodeList.add(Code.G18.name()); } else if (plane == 2) { - gcodeState.plane = Plane.YZ; + gcodeList.add(Code.G19.name()); } } @@ -346,9 +344,9 @@ public static void updateGcodeState(TinyGController controller, JsonObject respo int feedMode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE).getAsInt(); // 0=units-per-minute-mode, 1=inverse-time-mode if (feedMode == 0) { - gcodeState.feedMode = Code.G93; + gcodeList.add(Code.G93.name()); } else if (feedMode == 1) { - gcodeState.feedMode = Code.G94; + gcodeList.add(Code.G94.name()); } } @@ -356,11 +354,9 @@ public static void updateGcodeState(TinyGController controller, JsonObject respo int distance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE).getAsInt(); // 0=absolute distance mode, 1=incremental distance mode if (distance == 0) { - gcodeState.distanceMode = Code.G90; - controller.setDistanceModeCode(Code.G90.name()); + gcodeList.add(Code.G90.name()); } else if (distance == 1) { - gcodeState.distanceMode = Code.G91; - controller.setDistanceModeCode(Code.G91.name()); + gcodeList.add(Code.G91.name()); } } @@ -368,12 +364,12 @@ public static void updateGcodeState(TinyGController controller, JsonObject respo int arcDistance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE).getAsInt(); // 0=absolute distance mode, 1=incremental distance mode if (arcDistance == 0) { - gcodeState.arcDistanceMode = Code.G90_1; + gcodeList.add(Code.G90_1.name()); } else if (arcDistance == 1) { - gcodeState.arcDistanceMode = Code.G91_1; + gcodeList.add(Code.G91_1.name()); } } } - + return gcodeList; } } diff --git a/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java b/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java index 065d38155..ec6ad1029 100644 --- a/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java +++ b/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java @@ -29,7 +29,10 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.model.UnitUtils; import org.junit.Test; +import java.util.List; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -86,144 +89,112 @@ public void generateResetCoordinatesToZeroCommandShouldGenerateGcode() { } @Test - public void updateGcodeStateShouldUpdateOffset() { - // Given - GcodeState gcodeState = new GcodeState(); - TinyGController controller = mock(TinyGController.class); - when(controller.getCurrentGcodeState()).thenReturn(gcodeState); - + public void convertStatusReportShouldHandleOffset() { // When JsonObject response = TinyGUtils.jsonToObject("{sr:{coor:2}}"); - TinyGUtils.updateGcodeState(controller, response); + List result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G55, gcodeState.offset); + assertTrue(result.contains(Code.G55.name())); } @Test - public void updateGcodeStateShouldUpdateUnit() { - // Given - GcodeState gcodeState = new GcodeState(); - TinyGController controller = mock(TinyGController.class); - when(controller.getCurrentGcodeState()).thenReturn(gcodeState); - + public void convertStatusReportShouldHandleUnit() { // When switch to inch JsonObject response = TinyGUtils.jsonToObject("{sr:{unit:1}}"); - TinyGUtils.updateGcodeState(controller, response); + List result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G21, gcodeState.units); - verify(controller).setUnitsCode("G21"); + assertTrue(result.contains(Code.G21.name())); // When switch to mm response = TinyGUtils.jsonToObject("{sr:{unit:0}}"); - TinyGUtils.updateGcodeState(controller, response); + result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G20, gcodeState.units); - verify(controller).setUnitsCode("G20"); + assertTrue(result.contains(Code.G20.name())); } @Test - public void updateGcodeStateShouldUpdatePlane() { - // Given - GcodeState gcodeState = new GcodeState(); - TinyGController controller = mock(TinyGController.class); - when(controller.getCurrentGcodeState()).thenReturn(gcodeState); - + public void convertStatusReportShouldHandlePlane() { // When switch XY JsonObject response = TinyGUtils.jsonToObject("{sr:{plan:0}}"); - TinyGUtils.updateGcodeState(controller, response); + List result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Plane.XY, gcodeState.plane); + assertTrue(result.contains(Code.G17.name())); // When switch to ZX response = TinyGUtils.jsonToObject("{sr:{plan:1}}"); - TinyGUtils.updateGcodeState(controller, response); + result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Plane.ZX, gcodeState.plane); + assertTrue(result.contains(Code.G18.name())); // When switch to YZ response = TinyGUtils.jsonToObject("{sr:{plan:2}}"); - TinyGUtils.updateGcodeState(controller, response); + result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Plane.YZ, gcodeState.plane); + assertTrue(result.contains(Code.G19.name())); } @Test - public void updateGcodeStateShouldUpdateFeedMode() { - // Given - GcodeState gcodeState = new GcodeState(); - TinyGController controller = mock(TinyGController.class); - when(controller.getCurrentGcodeState()).thenReturn(gcodeState); - + public void convertStatusReportShouldHandleFeedMode() { // When switch to units per minute mode JsonObject response = TinyGUtils.jsonToObject("{sr:{frmo:0}}"); - TinyGUtils.updateGcodeState(controller, response); + List result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G93, gcodeState.feedMode); + assertTrue(result.contains(Code.G93.name())); // When switch to inverse time mode response = TinyGUtils.jsonToObject("{sr:{frmo:1}}"); - TinyGUtils.updateGcodeState(controller, response); + result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G94, gcodeState.feedMode); + assertTrue(result.contains(Code.G94.name())); } @Test - public void updateGcodeStateShouldUpdateDistanceMode() { - // Given - GcodeState gcodeState = new GcodeState(); - TinyGController controller = mock(TinyGController.class); - when(controller.getCurrentGcodeState()).thenReturn(gcodeState); - + public void convertStatusReportShouldHandleDistanceMode() { // When switch to units per minute mode JsonObject response = TinyGUtils.jsonToObject("{sr:{dist:0}}"); - TinyGUtils.updateGcodeState(controller, response); + List result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G90, gcodeState.distanceMode); + assertTrue(result.contains(Code.G90.name())); // When switch to inverse time mode response = TinyGUtils.jsonToObject("{sr:{dist:1}}"); - TinyGUtils.updateGcodeState(controller, response); + result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G91, gcodeState.distanceMode); + assertTrue(result.contains(Code.G91.name())); } @Test - public void updateGcodeStateShouldUpdateArcDistanceMode() { - // Given - GcodeState gcodeState = new GcodeState(); - TinyGController controller = mock(TinyGController.class); - when(controller.getCurrentGcodeState()).thenReturn(gcodeState); - + public void convertStatusReportShouldHandleArcDistanceMode() { // When switch to units per minute mode JsonObject response = TinyGUtils.jsonToObject("{sr:{admo:0}}"); - TinyGUtils.updateGcodeState(controller, response); + List result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G90_1, gcodeState.arcDistanceMode); + assertTrue(result.contains(Code.G90_1.name())); // When switch to inverse time mode response = TinyGUtils.jsonToObject("{sr:{admo:1}}"); - TinyGUtils.updateGcodeState(controller, response); + result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertEquals(Code.G91_1, gcodeState.arcDistanceMode); + assertTrue(result.contains(Code.G91_1.name())); } } From 077b1c1b13cc8d08117245f5fa55d69ea565168d Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 12 Jul 2018 08:00:43 +0200 Subject: [PATCH 04/13] Removed duplicate code after code review --- .../universalgcodesender/TinyGUtils.java | 45 ++------------- .../model/WorkCoordinateSystem.java | 55 ++++++++++++++++--- 2 files changed, 51 insertions(+), 49 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index 704ec87b1..8b59f8af5 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -27,6 +27,7 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.model.Axis; import com.willwinder.universalgcodesender.model.Position; import com.willwinder.universalgcodesender.model.UnitUtils; +import com.willwinder.universalgcodesender.model.WorkCoordinateSystem; import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; @@ -239,7 +240,7 @@ private static String getStateAsString(int state) { * @return a string with the command to reset the coordinate system to zero */ public static String generateResetCoordinatesToZeroCommand(ControllerStatus controllerStatus, GcodeState gcodeState) { - int offsetCode = convertOffsetGcodeToCode(gcodeState.offset); + int offsetCode = WorkCoordinateSystem.fromGCode(gcodeState.offset).getPValue(); Position machineCoord = controllerStatus.getMachineCoord(); return "G10 L2 P" + offsetCode + " X" + Utils.formatter.format(machineCoord.get(Axis.X)) + @@ -257,51 +258,13 @@ public static String generateResetCoordinatesToZeroCommand(ControllerStatus cont * @return a command for setting the position */ public static String generateSetWorkPositionCommand(ControllerStatus controllerStatus, GcodeState gcodeState, Axis axis, double position) { - int offsetCode = convertOffsetGcodeToCode(gcodeState.offset); + int offsetCode = WorkCoordinateSystem.fromGCode(gcodeState.offset).getPValue(); Position machineCoord = controllerStatus.getMachineCoord(); double coordinate = -(position - machineCoord.get(axis)); return "G10 L2 P" + offsetCode + " " + axis.name() + Utils.formatter.format(coordinate); } - private static int convertOffsetGcodeToCode(Code offsetGcode) { - switch (offsetGcode) { - case G54: - return 1; - case G55: - return 2; - case G56: - return 3; - case G57: - return 4; - case G58: - return 5; - case G59: - return 6; - default: - return 0; - } - } - - private static Code convertOffsetCodeToGcode(int offsetCode) { - switch (offsetCode) { - case 1: - return Code.G54; - case 2: - return Code.G55; - case 3: - return Code.G56; - case 4: - return Code.G57; - case 5: - return Code.G58; - case 6: - return Code.G59; - default: - return Code.G53; - } - } - /** * Updates the Gcode state from the response if it contains a status line * @@ -315,7 +278,7 @@ public static List convertStatusReportToGcode(JsonObject response) { if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); - gcodeList.add(TinyGUtils.convertOffsetCodeToGcode(offsetCode).name()); + gcodeList.add(WorkCoordinateSystem.fromPValue(offsetCode).getCGode().name()); } if (statusResultObject.has(TinyGUtils.FIELD_STATUS_RESULT_UNIT)) { diff --git a/ugs-core/src/com/willwinder/universalgcodesender/model/WorkCoordinateSystem.java b/ugs-core/src/com/willwinder/universalgcodesender/model/WorkCoordinateSystem.java index 5561548ac..3909c65bd 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/model/WorkCoordinateSystem.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/model/WorkCoordinateSystem.java @@ -1,5 +1,5 @@ /* - Copyright 2017 Will Winder + Copyright 2017-2018 Will Winder This file is part of Universal Gcode Sender (UGS). @@ -18,24 +18,63 @@ This file is part of Universal Gcode Sender (UGS). */ package com.willwinder.universalgcodesender.model; +import com.willwinder.universalgcodesender.gcode.util.Code; + +import java.util.Arrays; + /** + * An enum with the different work coordinate systems * * @author wwinder */ public enum WorkCoordinateSystem { - G54(1), - G55(2), - G56(3), - G57(4), - G58(5), - G59(6); + G53(0, Code.G53), + G54(1, Code.G54), + G55(2, Code.G55), + G56(3, Code.G56), + G57(4, Code.G57), + G58(5, Code.G58), + G59(6, Code.G59); final int pValue; - WorkCoordinateSystem(int pValue) { + final Code gcode; + + WorkCoordinateSystem(int pValue, Code gcode) { this.pValue = pValue; + this.gcode = gcode; } public int getPValue() { return pValue; } + + public Code getGcode() { + return gcode; + } + + /** + * Returns the work coordinate system from it's index value + * + * @param pValue the coordinate index + * @return the work coordinate system if found else G54 + */ + public static WorkCoordinateSystem fromPValue(int pValue) { + return Arrays.stream(values()) + .filter(value -> value.getPValue() == pValue) + .findFirst() + .orElse(G54); + } + + /** + * Returns the work coordinate system from it's GCode + * + * @param code the coordinate gcode + * @return the work coordinate system if found else G54 + */ + public static WorkCoordinateSystem fromGCode(Code code) { + return Arrays.stream(values()) + .filter(value -> value.name().equalsIgnoreCase(code.name()) ) + .findFirst() + .orElse(G54); + } } From a0fc521a89dcc81c5b3f7444d9ec039cf5401c5e Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 12 Jul 2018 08:05:08 +0200 Subject: [PATCH 05/13] I'm a stupid monkey --- .../src/com/willwinder/universalgcodesender/TinyGUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index 8b59f8af5..a83aa4bde 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -278,7 +278,7 @@ public static List convertStatusReportToGcode(JsonObject response) { if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); - gcodeList.add(WorkCoordinateSystem.fromPValue(offsetCode).getCGode().name()); + gcodeList.add(WorkCoordinateSystem.fromPValue(offsetCode).getGcode().name()); } if (statusResultObject.has(TinyGUtils.FIELD_STATUS_RESULT_UNIT)) { From ae2c4d659406088b296f17168183aa28eab732e7 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 12 Jul 2018 08:21:43 +0200 Subject: [PATCH 06/13] Fixed codacy warnings --- .../com/willwinder/universalgcodesender/TinyGUtilsTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java b/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java index ec6ad1029..fa6758f2d 100644 --- a/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java +++ b/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java @@ -21,7 +21,6 @@ This file is part of Universal Gcode Sender (UGS). import com.google.gson.JsonObject; import com.willwinder.universalgcodesender.gcode.GcodeState; import com.willwinder.universalgcodesender.gcode.util.Code; -import com.willwinder.universalgcodesender.gcode.util.Plane; import com.willwinder.universalgcodesender.listeners.ControllerState; import com.willwinder.universalgcodesender.listeners.ControllerStatus; import com.willwinder.universalgcodesender.model.Axis; @@ -33,9 +32,6 @@ This file is part of Universal Gcode Sender (UGS). import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; /** * Tests for the TinyGUtils class From b77ec1ce6463895a37eb65ede13b12a0fc85ac42 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 12 Jul 2018 09:22:31 +0200 Subject: [PATCH 07/13] Attempt to fix codacy warning about NPath complexity --- .../universalgcodesender/TinyGController.java | 2 +- .../universalgcodesender/TinyGUtils.java | 249 +++++++++++------- 2 files changed, 159 insertions(+), 92 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java index 6777f17ab..4ad53e2fd 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java @@ -186,7 +186,7 @@ protected void rawResponseHandler(String response) { dispatchConsoleMessage(MessageType.INFO, response + "\n"); checkStreamFinished(); } else if (TinyGGcodeCommand.isOkErrorResponse(response)) { - if (jo.get("r").getAsJsonObject().has(TinyGUtils.FIELD_STATUS_RESULT)) { + if (jo.get("r").getAsJsonObject().has(TinyGUtils.FIELD_STATUS_REPORT)) { updateControllerStatus(jo.get("r").getAsJsonObject()); checkStreamFinished(); } else if (rowsRemaining() > 0) { diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index a83aa4bde..0ddf3e993 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -32,6 +32,7 @@ This file is part of Universal Gcode Sender (UGS). import java.util.ArrayList; import java.util.List; +import java.util.Optional; import static com.willwinder.universalgcodesender.gcode.util.Code.G20; @@ -53,11 +54,11 @@ public class TinyGUtils { public static final String COMMAND_STATUS_REPORT = "{sr:n}"; public static final String COMMAND_KILL_ALARM_LOCK = "{clear:n}"; - public static final String FIELD_STATUS_RESULT = "sr"; + public static final String FIELD_STATUS_REPORT = "sr"; private static final String FIELD_FIRMWARE_VERSION = "fv"; private static final String FIELD_RESPONSE = "r"; - private static final String FIELD_STATUS_RESULT_UNIT = "unit"; - private static final String FIELD_STATUS_RESULT_POSX = "posx"; + private static final String FIELD_STATUS_REPORT_UNIT = "unit"; + private static final String FIELD_STATUS_REPORT_POSX = "posx"; private static final String FIELD_STATUS_REPORT_POSY = "posy"; private static final String FIELD_STATUS_REPORT_POSZ = "posz"; private static final String FIELD_STATUS_REPORT_VELOCITY = "vel"; @@ -131,48 +132,16 @@ public static boolean isStatusResponse(JsonObject response) { * @return a new updated controller status */ public static ControllerStatus updateControllerStatus(final ControllerStatus lastControllerStatus, final GcodeState lastGcodeState, final JsonObject response) { - if (response.has(FIELD_STATUS_RESULT)) { - JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_RESULT); + if (response.has(FIELD_STATUS_REPORT)) { + JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_REPORT); UnitUtils.Units currentUnits = lastGcodeState.units == G20 ? UnitUtils.Units.INCH : UnitUtils.Units.MM; - Position workCoord = lastControllerStatus.getWorkCoord().getPositionIn(currentUnits); - if (statusResultObject.has(FIELD_STATUS_RESULT_POSX)) { - workCoord.setX(statusResultObject.get(FIELD_STATUS_RESULT_POSX).getAsDouble()); - } - - if (statusResultObject.has(FIELD_STATUS_REPORT_POSY)) { - workCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_POSY).getAsDouble()); - } - - if (statusResultObject.has(FIELD_STATUS_REPORT_POSZ)) { - workCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_POSZ).getAsDouble()); - } - - Position machineCoord = lastControllerStatus.getMachineCoord().getPositionIn(UnitUtils.Units.MM); - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOX)) { - machineCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_MPOX).getAsDouble()); - } - - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOY)) { - machineCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_MPOY).getAsDouble()); - } - - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOZ)) { - machineCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_MPOZ).getAsDouble()); - } - - Double feedSpeed = lastControllerStatus.getFeedSpeed(); - if (statusResultObject.has(FIELD_STATUS_REPORT_VELOCITY)) { - feedSpeed = statusResultObject.get(FIELD_STATUS_REPORT_VELOCITY).getAsDouble(); - } - - ControllerState state = lastControllerStatus.getState(); - String stateString = lastControllerStatus.getStateString(); - if (statusResultObject.has(FIELD_STATUS_REPORT_STATUS)) { - state = getState(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); - stateString = getStateAsString(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); - } + Position workCoord = parseWorkCoordinatesFromStatusReport(lastControllerStatus, statusResultObject, currentUnits); + Position machineCoord = parseMachineCoordinatesFromStatusReport(lastControllerStatus, statusResultObject); + Double feedSpeed = parseFeedSpeedFromStatusReport(lastControllerStatus, statusResultObject); + ControllerState state = parseStateFromStatusReport(lastControllerStatus, statusResultObject); + String stateString = parseStateStringFromStatusReport(lastControllerStatus, statusResultObject); Double spindleSpeed = lastControllerStatus.getSpindleSpeed(); ControllerStatus.OverridePercents overrides = lastControllerStatus.getOverrides(); @@ -186,6 +155,62 @@ public static ControllerStatus updateControllerStatus(final ControllerStatus las return lastControllerStatus; } + private static String parseStateStringFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject) { + String stateString = lastControllerStatus.getStateString(); + if (statusResultObject.has(FIELD_STATUS_REPORT_STATUS)) { + stateString = getStateAsString(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); + } + return stateString; + } + + private static ControllerState parseStateFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject) { + ControllerState state = lastControllerStatus.getState(); + if (statusResultObject.has(FIELD_STATUS_REPORT_STATUS)) { + state = getState(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); + } + return state; + } + + private static Double parseFeedSpeedFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject) { + Double feedSpeed = lastControllerStatus.getFeedSpeed(); + if (statusResultObject.has(FIELD_STATUS_REPORT_VELOCITY)) { + feedSpeed = statusResultObject.get(FIELD_STATUS_REPORT_VELOCITY).getAsDouble(); + } + return feedSpeed; + } + + private static Position parseMachineCoordinatesFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject) { + Position machineCoord = lastControllerStatus.getMachineCoord().getPositionIn(UnitUtils.Units.MM); + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOX)) { + machineCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_MPOX).getAsDouble()); + } + + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOY)) { + machineCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_MPOY).getAsDouble()); + } + + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOZ)) { + machineCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_MPOZ).getAsDouble()); + } + return machineCoord; + } + + private static Position parseWorkCoordinatesFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject, UnitUtils.Units currentUnits) { + Position workCoord = lastControllerStatus.getWorkCoord().getPositionIn(currentUnits); + if (statusResultObject.has(FIELD_STATUS_REPORT_POSX)) { + workCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_POSX).getAsDouble()); + } + + if (statusResultObject.has(FIELD_STATUS_REPORT_POSY)) { + workCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_POSY).getAsDouble()); + } + + if (statusResultObject.has(FIELD_STATUS_REPORT_POSZ)) { + workCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_POSZ).getAsDouble()); + } + return workCoord; + } + /** * Maps between the TinyG state to a ControllerState * @@ -273,66 +298,108 @@ public static String generateSetWorkPositionCommand(ControllerStatus controllerS */ public static List convertStatusReportToGcode(JsonObject response) { List gcodeList = new ArrayList<>(); - if (response.has(TinyGUtils.FIELD_STATUS_RESULT)) { - JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_RESULT); + if (response.has(TinyGUtils.FIELD_STATUS_REPORT)) { + JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_REPORT); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { - int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); - gcodeList.add(WorkCoordinateSystem.fromPValue(offsetCode).getGcode().name()); - } + parseCoordinateOffsetFromStatusReport(statusResultObject) + .ifPresent(code -> gcodeList.add(code.name())); + + parseUnitFromStatusReport(statusResultObject) + .ifPresent(code -> gcodeList.add(code.name())); + + parsePlaneFromStatusReport(statusResultObject) + .ifPresent(code -> gcodeList.add(code.name())); + + parseFeedModeFromStatusReport(statusResultObject) + .ifPresent(code -> gcodeList.add(code.name())); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_RESULT_UNIT)) { - int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_RESULT_UNIT).getAsInt(); - // 0=inch, 1=mm - if (units == 0) { - gcodeList.add(Code.G20.name()); - } else { - gcodeList.add(Code.G21.name()); - } + parseDistanceModeFromStatusReport(statusResultObject) + .ifPresent(code -> gcodeList.add(code.name())); + + parseArcDistanceModeFromStatusReport(statusResultObject) + .ifPresent(code -> gcodeList.add(code.name())); + } + return gcodeList; + } + + private static Optional parseCoordinateOffsetFromStatusReport(JsonObject statusResultObject) { + Optional result = Optional.empty(); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { + int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); + result = Optional.of(WorkCoordinateSystem.fromPValue(offsetCode).getGcode()); + } + return result; + } + + private static Optional parseArcDistanceModeFromStatusReport(JsonObject statusResultObject) { + Optional result = Optional.empty(); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE)) { + int arcDistance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE).getAsInt(); + // 0=absolute distance mode, 1=incremental distance mode + if (arcDistance == 0) { + result = Optional.of(Code.G90_1); + } else if (arcDistance == 1) { + result = Optional.of(Code.G91_1); } + } + return result; + } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_PLANE)) { - int plane = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_PLANE).getAsInt(); - // 0=XY plane, 1=XZ plane, 2=YZ plane - if (plane == 0) { - gcodeList.add(Code.G17.name()); - } else if (plane == 1) { - gcodeList.add(Code.G18.name()); - } else if (plane == 2) { - gcodeList.add(Code.G19.name()); - } + private static Optional parseDistanceModeFromStatusReport(JsonObject statusResultObject) { + Optional result = Optional.empty(); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE)) { + int distance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE).getAsInt(); + // 0=absolute distance mode, 1=incremental distance mode + if (distance == 0) { + result = Optional.of(Code.G90); + } else if (distance == 1) { + result = Optional.of(Code.G91); } + } + return result; + } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE)) { - int feedMode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE).getAsInt(); - // 0=units-per-minute-mode, 1=inverse-time-mode - if (feedMode == 0) { - gcodeList.add(Code.G93.name()); - } else if (feedMode == 1) { - gcodeList.add(Code.G94.name()); - } + private static Optional parseFeedModeFromStatusReport(JsonObject statusResultObject) { + Optional result = Optional.empty(); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE)) { + int feedMode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE).getAsInt(); + // 0=units-per-minute-mode, 1=inverse-time-mode + if (feedMode == 0) { + result = Optional.of(Code.G93); + } else if (feedMode == 1) { + result = Optional.of(Code.G94); } + } + return result; + } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE)) { - int distance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE).getAsInt(); - // 0=absolute distance mode, 1=incremental distance mode - if (distance == 0) { - gcodeList.add(Code.G90.name()); - } else if (distance == 1) { - gcodeList.add(Code.G91.name()); - } + private static Optional parsePlaneFromStatusReport(JsonObject statusResultObject) { + Optional result = Optional.empty(); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_PLANE)) { + int plane = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_PLANE).getAsInt(); + // 0=XY plane, 1=XZ plane, 2=YZ plane + if (plane == 0) { + result = Optional.of(Code.G17); + } else if (plane == 1) { + result = Optional.of(Code.G18); + } else if (plane == 2) { + result = Optional.of(Code.G19); } + } + return result; + } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE)) { - int arcDistance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE).getAsInt(); - // 0=absolute distance mode, 1=incremental distance mode - if (arcDistance == 0) { - gcodeList.add(Code.G90_1.name()); - } else if (arcDistance == 1) { - gcodeList.add(Code.G91_1.name()); - } + private static Optional parseUnitFromStatusReport(JsonObject statusResultObject) { + Optional result = Optional.empty(); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_UNIT)) { + int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_UNIT).getAsInt(); + // 0=inch, 1=mm + if (units == 0) { + result = Optional.of(Code.G20); + } else { + result = Optional.of(Code.G21); } } - return gcodeList; + return result; } } From c244cb7daa4b64dcd1437caef89630cdf8a6adbd Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 12 Jul 2018 09:28:40 +0200 Subject: [PATCH 08/13] Fixed codacy warning --- .../src/com/willwinder/universalgcodesender/TinyGUtils.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index 0ddf3e993..0e9048353 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -34,8 +34,6 @@ This file is part of Universal Gcode Sender (UGS). import java.util.List; import java.util.Optional; -import static com.willwinder.universalgcodesender.gcode.util.Code.G20; - /** * Common utils for TinyG controllers * @@ -135,7 +133,7 @@ public static ControllerStatus updateControllerStatus(final ControllerStatus las if (response.has(FIELD_STATUS_REPORT)) { JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_REPORT); - UnitUtils.Units currentUnits = lastGcodeState.units == G20 ? UnitUtils.Units.INCH : UnitUtils.Units.MM; + UnitUtils.Units currentUnits = lastGcodeState.units == Code.G20 ? UnitUtils.Units.INCH : UnitUtils.Units.MM; Position workCoord = parseWorkCoordinatesFromStatusReport(lastControllerStatus, statusResultObject, currentUnits); Position machineCoord = parseMachineCoordinatesFromStatusReport(lastControllerStatus, statusResultObject); From 67b6cc67cf224cbbc416c8c0fc6f325198df6d26 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 12 Jul 2018 10:04:04 +0200 Subject: [PATCH 09/13] Revert "Attempt to fix codacy warning about NPath complexity" This reverts commit b77ec1ce6463895a37eb65ede13b12a0fc85ac42. --- .../universalgcodesender/TinyGController.java | 2 +- .../universalgcodesender/TinyGUtils.java | 249 +++++++----------- 2 files changed, 92 insertions(+), 159 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java index 4ad53e2fd..6777f17ab 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java @@ -186,7 +186,7 @@ protected void rawResponseHandler(String response) { dispatchConsoleMessage(MessageType.INFO, response + "\n"); checkStreamFinished(); } else if (TinyGGcodeCommand.isOkErrorResponse(response)) { - if (jo.get("r").getAsJsonObject().has(TinyGUtils.FIELD_STATUS_REPORT)) { + if (jo.get("r").getAsJsonObject().has(TinyGUtils.FIELD_STATUS_RESULT)) { updateControllerStatus(jo.get("r").getAsJsonObject()); checkStreamFinished(); } else if (rowsRemaining() > 0) { diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index 0e9048353..99f6f108c 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -32,7 +32,6 @@ This file is part of Universal Gcode Sender (UGS). import java.util.ArrayList; import java.util.List; -import java.util.Optional; /** * Common utils for TinyG controllers @@ -52,11 +51,11 @@ public class TinyGUtils { public static final String COMMAND_STATUS_REPORT = "{sr:n}"; public static final String COMMAND_KILL_ALARM_LOCK = "{clear:n}"; - public static final String FIELD_STATUS_REPORT = "sr"; + public static final String FIELD_STATUS_RESULT = "sr"; private static final String FIELD_FIRMWARE_VERSION = "fv"; private static final String FIELD_RESPONSE = "r"; - private static final String FIELD_STATUS_REPORT_UNIT = "unit"; - private static final String FIELD_STATUS_REPORT_POSX = "posx"; + private static final String FIELD_STATUS_RESULT_UNIT = "unit"; + private static final String FIELD_STATUS_RESULT_POSX = "posx"; private static final String FIELD_STATUS_REPORT_POSY = "posy"; private static final String FIELD_STATUS_REPORT_POSZ = "posz"; private static final String FIELD_STATUS_REPORT_VELOCITY = "vel"; @@ -130,83 +129,59 @@ public static boolean isStatusResponse(JsonObject response) { * @return a new updated controller status */ public static ControllerStatus updateControllerStatus(final ControllerStatus lastControllerStatus, final GcodeState lastGcodeState, final JsonObject response) { - if (response.has(FIELD_STATUS_REPORT)) { - JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_REPORT); + if (response.has(FIELD_STATUS_RESULT)) { + JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_RESULT); UnitUtils.Units currentUnits = lastGcodeState.units == Code.G20 ? UnitUtils.Units.INCH : UnitUtils.Units.MM; - Position workCoord = parseWorkCoordinatesFromStatusReport(lastControllerStatus, statusResultObject, currentUnits); - Position machineCoord = parseMachineCoordinatesFromStatusReport(lastControllerStatus, statusResultObject); - Double feedSpeed = parseFeedSpeedFromStatusReport(lastControllerStatus, statusResultObject); - ControllerState state = parseStateFromStatusReport(lastControllerStatus, statusResultObject); - String stateString = parseStateStringFromStatusReport(lastControllerStatus, statusResultObject); - - Double spindleSpeed = lastControllerStatus.getSpindleSpeed(); - ControllerStatus.OverridePercents overrides = lastControllerStatus.getOverrides(); - Position workCoordinateOffset = lastControllerStatus.getWorkCoordinateOffset(); - ControllerStatus.EnabledPins enabledPins = lastControllerStatus.getEnabledPins(); - ControllerStatus.AccessoryStates accessoryStates = lastControllerStatus.getAccessoryStates(); - - return new ControllerStatus(stateString, state, machineCoord.getPositionIn(currentUnits), workCoord.getPositionIn(currentUnits), feedSpeed, spindleSpeed, overrides, workCoordinateOffset, enabledPins, accessoryStates); - } + Position workCoord = lastControllerStatus.getWorkCoord().getPositionIn(currentUnits); + if (statusResultObject.has(FIELD_STATUS_RESULT_POSX)) { + workCoord.setX(statusResultObject.get(FIELD_STATUS_RESULT_POSX).getAsDouble()); + } - return lastControllerStatus; - } + if (statusResultObject.has(FIELD_STATUS_REPORT_POSY)) { + workCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_POSY).getAsDouble()); + } - private static String parseStateStringFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject) { - String stateString = lastControllerStatus.getStateString(); - if (statusResultObject.has(FIELD_STATUS_REPORT_STATUS)) { - stateString = getStateAsString(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); - } - return stateString; - } + if (statusResultObject.has(FIELD_STATUS_REPORT_POSZ)) { + workCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_POSZ).getAsDouble()); + } - private static ControllerState parseStateFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject) { - ControllerState state = lastControllerStatus.getState(); - if (statusResultObject.has(FIELD_STATUS_REPORT_STATUS)) { - state = getState(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); - } - return state; - } + Position machineCoord = lastControllerStatus.getMachineCoord().getPositionIn(UnitUtils.Units.MM); + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOX)) { + machineCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_MPOX).getAsDouble()); + } - private static Double parseFeedSpeedFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject) { - Double feedSpeed = lastControllerStatus.getFeedSpeed(); - if (statusResultObject.has(FIELD_STATUS_REPORT_VELOCITY)) { - feedSpeed = statusResultObject.get(FIELD_STATUS_REPORT_VELOCITY).getAsDouble(); - } - return feedSpeed; - } + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOY)) { + machineCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_MPOY).getAsDouble()); + } - private static Position parseMachineCoordinatesFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject) { - Position machineCoord = lastControllerStatus.getMachineCoord().getPositionIn(UnitUtils.Units.MM); - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOX)) { - machineCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_MPOX).getAsDouble()); - } + if (statusResultObject.has(FIELD_STATUS_REPORT_MPOZ)) { + machineCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_MPOZ).getAsDouble()); + } - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOY)) { - machineCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_MPOY).getAsDouble()); - } + Double feedSpeed = lastControllerStatus.getFeedSpeed(); + if (statusResultObject.has(FIELD_STATUS_REPORT_VELOCITY)) { + feedSpeed = statusResultObject.get(FIELD_STATUS_REPORT_VELOCITY).getAsDouble(); + } - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOZ)) { - machineCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_MPOZ).getAsDouble()); - } - return machineCoord; - } + ControllerState state = lastControllerStatus.getState(); + String stateString = lastControllerStatus.getStateString(); + if (statusResultObject.has(FIELD_STATUS_REPORT_STATUS)) { + state = getState(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); + stateString = getStateAsString(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); + } - private static Position parseWorkCoordinatesFromStatusReport(ControllerStatus lastControllerStatus, JsonObject statusResultObject, UnitUtils.Units currentUnits) { - Position workCoord = lastControllerStatus.getWorkCoord().getPositionIn(currentUnits); - if (statusResultObject.has(FIELD_STATUS_REPORT_POSX)) { - workCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_POSX).getAsDouble()); - } + Double spindleSpeed = lastControllerStatus.getSpindleSpeed(); + ControllerStatus.OverridePercents overrides = lastControllerStatus.getOverrides(); + Position workCoordinateOffset = lastControllerStatus.getWorkCoordinateOffset(); + ControllerStatus.EnabledPins enabledPins = lastControllerStatus.getEnabledPins(); + ControllerStatus.AccessoryStates accessoryStates = lastControllerStatus.getAccessoryStates(); - if (statusResultObject.has(FIELD_STATUS_REPORT_POSY)) { - workCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_POSY).getAsDouble()); + return new ControllerStatus(stateString, state, machineCoord.getPositionIn(currentUnits), workCoord.getPositionIn(currentUnits), feedSpeed, spindleSpeed, overrides, workCoordinateOffset, enabledPins, accessoryStates); } - if (statusResultObject.has(FIELD_STATUS_REPORT_POSZ)) { - workCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_POSZ).getAsDouble()); - } - return workCoord; + return lastControllerStatus; } /** @@ -296,108 +271,66 @@ public static String generateSetWorkPositionCommand(ControllerStatus controllerS */ public static List convertStatusReportToGcode(JsonObject response) { List gcodeList = new ArrayList<>(); - if (response.has(TinyGUtils.FIELD_STATUS_REPORT)) { - JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_REPORT); + if (response.has(TinyGUtils.FIELD_STATUS_RESULT)) { + JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_RESULT); - parseCoordinateOffsetFromStatusReport(statusResultObject) - .ifPresent(code -> gcodeList.add(code.name())); - - parseUnitFromStatusReport(statusResultObject) - .ifPresent(code -> gcodeList.add(code.name())); - - parsePlaneFromStatusReport(statusResultObject) - .ifPresent(code -> gcodeList.add(code.name())); - - parseFeedModeFromStatusReport(statusResultObject) - .ifPresent(code -> gcodeList.add(code.name())); - - parseDistanceModeFromStatusReport(statusResultObject) - .ifPresent(code -> gcodeList.add(code.name())); - - parseArcDistanceModeFromStatusReport(statusResultObject) - .ifPresent(code -> gcodeList.add(code.name())); - } - return gcodeList; - } - - private static Optional parseCoordinateOffsetFromStatusReport(JsonObject statusResultObject) { - Optional result = Optional.empty(); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { - int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); - result = Optional.of(WorkCoordinateSystem.fromPValue(offsetCode).getGcode()); - } - return result; - } + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { + int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); + gcodeList.add(WorkCoordinateSystem.fromPValue(offsetCode).getGcode().name()); + } - private static Optional parseArcDistanceModeFromStatusReport(JsonObject statusResultObject) { - Optional result = Optional.empty(); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE)) { - int arcDistance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE).getAsInt(); - // 0=absolute distance mode, 1=incremental distance mode - if (arcDistance == 0) { - result = Optional.of(Code.G90_1); - } else if (arcDistance == 1) { - result = Optional.of(Code.G91_1); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_RESULT_UNIT)) { + int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_RESULT_UNIT).getAsInt(); + // 0=inch, 1=mm + if (units == 0) { + gcodeList.add(Code.G20.name()); + } else { + gcodeList.add(Code.G21.name()); + } } - } - return result; - } - private static Optional parseDistanceModeFromStatusReport(JsonObject statusResultObject) { - Optional result = Optional.empty(); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE)) { - int distance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE).getAsInt(); - // 0=absolute distance mode, 1=incremental distance mode - if (distance == 0) { - result = Optional.of(Code.G90); - } else if (distance == 1) { - result = Optional.of(Code.G91); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_PLANE)) { + int plane = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_PLANE).getAsInt(); + // 0=XY plane, 1=XZ plane, 2=YZ plane + if (plane == 0) { + gcodeList.add(Code.G17.name()); + } else if (plane == 1) { + gcodeList.add(Code.G18.name()); + } else if (plane == 2) { + gcodeList.add(Code.G19.name()); + } } - } - return result; - } - private static Optional parseFeedModeFromStatusReport(JsonObject statusResultObject) { - Optional result = Optional.empty(); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE)) { - int feedMode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE).getAsInt(); - // 0=units-per-minute-mode, 1=inverse-time-mode - if (feedMode == 0) { - result = Optional.of(Code.G93); - } else if (feedMode == 1) { - result = Optional.of(Code.G94); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE)) { + int feedMode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE).getAsInt(); + // 0=units-per-minute-mode, 1=inverse-time-mode + if (feedMode == 0) { + gcodeList.add(Code.G93.name()); + } else if (feedMode == 1) { + gcodeList.add(Code.G94.name()); + } } - } - return result; - } - private static Optional parsePlaneFromStatusReport(JsonObject statusResultObject) { - Optional result = Optional.empty(); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_PLANE)) { - int plane = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_PLANE).getAsInt(); - // 0=XY plane, 1=XZ plane, 2=YZ plane - if (plane == 0) { - result = Optional.of(Code.G17); - } else if (plane == 1) { - result = Optional.of(Code.G18); - } else if (plane == 2) { - result = Optional.of(Code.G19); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE)) { + int distance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE).getAsInt(); + // 0=absolute distance mode, 1=incremental distance mode + if (distance == 0) { + gcodeList.add(Code.G90.name()); + } else if (distance == 1) { + gcodeList.add(Code.G91.name()); + } } - } - return result; - } - private static Optional parseUnitFromStatusReport(JsonObject statusResultObject) { - Optional result = Optional.empty(); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_UNIT)) { - int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_UNIT).getAsInt(); - // 0=inch, 1=mm - if (units == 0) { - result = Optional.of(Code.G20); - } else { - result = Optional.of(Code.G21); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE)) { + int arcDistance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE).getAsInt(); + // 0=absolute distance mode, 1=incremental distance mode + if (arcDistance == 0) { + gcodeList.add(Code.G90_1.name()); + } else if (arcDistance == 1) { + gcodeList.add(Code.G91_1.name()); + } } } - return result; + return gcodeList; } } From c7baa09f5470c7385bbaee2659c5fdfb5acfb738 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 12 Jul 2018 10:11:20 +0200 Subject: [PATCH 10/13] Reverted to the previous status result parsing after code review (ignoring codacy warnings). --- .../universalgcodesender/TinyGController.java | 2 +- .../universalgcodesender/TinyGUtils.java | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java index 6777f17ab..4ad53e2fd 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java @@ -186,7 +186,7 @@ protected void rawResponseHandler(String response) { dispatchConsoleMessage(MessageType.INFO, response + "\n"); checkStreamFinished(); } else if (TinyGGcodeCommand.isOkErrorResponse(response)) { - if (jo.get("r").getAsJsonObject().has(TinyGUtils.FIELD_STATUS_RESULT)) { + if (jo.get("r").getAsJsonObject().has(TinyGUtils.FIELD_STATUS_REPORT)) { updateControllerStatus(jo.get("r").getAsJsonObject()); checkStreamFinished(); } else if (rowsRemaining() > 0) { diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index 99f6f108c..23ea45198 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -51,11 +51,11 @@ public class TinyGUtils { public static final String COMMAND_STATUS_REPORT = "{sr:n}"; public static final String COMMAND_KILL_ALARM_LOCK = "{clear:n}"; - public static final String FIELD_STATUS_RESULT = "sr"; + public static final String FIELD_STATUS_REPORT = "sr"; private static final String FIELD_FIRMWARE_VERSION = "fv"; private static final String FIELD_RESPONSE = "r"; - private static final String FIELD_STATUS_RESULT_UNIT = "unit"; - private static final String FIELD_STATUS_RESULT_POSX = "posx"; + private static final String FIELD_STATUS_REPORT_UNIT = "unit"; + private static final String FIELD_STATUS_REPORT_POSX = "posx"; private static final String FIELD_STATUS_REPORT_POSY = "posy"; private static final String FIELD_STATUS_REPORT_POSZ = "posz"; private static final String FIELD_STATUS_REPORT_VELOCITY = "vel"; @@ -129,14 +129,14 @@ public static boolean isStatusResponse(JsonObject response) { * @return a new updated controller status */ public static ControllerStatus updateControllerStatus(final ControllerStatus lastControllerStatus, final GcodeState lastGcodeState, final JsonObject response) { - if (response.has(FIELD_STATUS_RESULT)) { - JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_RESULT); + if (response.has(FIELD_STATUS_REPORT)) { + JsonObject statusResultObject = response.getAsJsonObject(FIELD_STATUS_REPORT); UnitUtils.Units currentUnits = lastGcodeState.units == Code.G20 ? UnitUtils.Units.INCH : UnitUtils.Units.MM; Position workCoord = lastControllerStatus.getWorkCoord().getPositionIn(currentUnits); - if (statusResultObject.has(FIELD_STATUS_RESULT_POSX)) { - workCoord.setX(statusResultObject.get(FIELD_STATUS_RESULT_POSX).getAsDouble()); + if (statusResultObject.has(FIELD_STATUS_REPORT_POSX)) { + workCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_POSX).getAsDouble()); } if (statusResultObject.has(FIELD_STATUS_REPORT_POSY)) { @@ -147,6 +147,7 @@ public static ControllerStatus updateControllerStatus(final ControllerStatus las workCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_POSZ).getAsDouble()); } + // 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); if (statusResultObject.has(FIELD_STATUS_REPORT_MPOX)) { machineCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_MPOX).getAsDouble()); @@ -271,16 +272,16 @@ public static String generateSetWorkPositionCommand(ControllerStatus controllerS */ public static List convertStatusReportToGcode(JsonObject response) { List gcodeList = new ArrayList<>(); - if (response.has(TinyGUtils.FIELD_STATUS_RESULT)) { - JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_RESULT); + if (response.has(TinyGUtils.FIELD_STATUS_REPORT)) { + JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_REPORT); if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); gcodeList.add(WorkCoordinateSystem.fromPValue(offsetCode).getGcode().name()); } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_RESULT_UNIT)) { - int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_RESULT_UNIT).getAsInt(); + if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_UNIT)) { + int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_UNIT).getAsInt(); // 0=inch, 1=mm if (units == 0) { gcodeList.add(Code.G20.name()); From c8bbfd79e6a4d7564657583b76e790f2e82f2156 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Fri, 13 Jul 2018 07:46:36 +0200 Subject: [PATCH 11/13] Use the code .toString() instead of .name() to get the right gcode. Fixes problem with parsing of gcode state for arc-distance mode. --- .../universalgcodesender/TinyGUtils.java | 18 +++++++++--------- .../GcodePreprocessorUtilsTest.java | 18 ++++++++++++++++++ .../universalgcodesender/TinyGUtilsTest.java | 4 ++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index 23ea45198..71998f667 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -284,9 +284,9 @@ public static List convertStatusReportToGcode(JsonObject response) { int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_UNIT).getAsInt(); // 0=inch, 1=mm if (units == 0) { - gcodeList.add(Code.G20.name()); + gcodeList.add(Code.G20.toString()); } else { - gcodeList.add(Code.G21.name()); + gcodeList.add(Code.G21.toString()); } } @@ -294,11 +294,11 @@ public static List convertStatusReportToGcode(JsonObject response) { int plane = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_PLANE).getAsInt(); // 0=XY plane, 1=XZ plane, 2=YZ plane if (plane == 0) { - gcodeList.add(Code.G17.name()); + gcodeList.add(Code.G17.toString()); } else if (plane == 1) { - gcodeList.add(Code.G18.name()); + gcodeList.add(Code.G18.toString()); } else if (plane == 2) { - gcodeList.add(Code.G19.name()); + gcodeList.add(Code.G19.toString()); } } @@ -306,9 +306,9 @@ public static List convertStatusReportToGcode(JsonObject response) { int feedMode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE).getAsInt(); // 0=units-per-minute-mode, 1=inverse-time-mode if (feedMode == 0) { - gcodeList.add(Code.G93.name()); + gcodeList.add(Code.G93.toString()); } else if (feedMode == 1) { - gcodeList.add(Code.G94.name()); + gcodeList.add(Code.G94.toString()); } } @@ -326,9 +326,9 @@ public static List convertStatusReportToGcode(JsonObject response) { int arcDistance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE).getAsInt(); // 0=absolute distance mode, 1=incremental distance mode if (arcDistance == 0) { - gcodeList.add(Code.G90_1.name()); + gcodeList.add(Code.G90_1.toString()); } else if (arcDistance == 1) { - gcodeList.add(Code.G91_1.name()); + gcodeList.add(Code.G91_1.toString()); } } } diff --git a/ugs-core/test/com/willwinder/universalgcodesender/GcodePreprocessorUtilsTest.java b/ugs-core/test/com/willwinder/universalgcodesender/GcodePreprocessorUtilsTest.java index fa3ae0f4a..eb7d043ec 100644 --- a/ugs-core/test/com/willwinder/universalgcodesender/GcodePreprocessorUtilsTest.java +++ b/ugs-core/test/com/willwinder/universalgcodesender/GcodePreprocessorUtilsTest.java @@ -224,4 +224,22 @@ public void testExtractMotion() throws Exception { .hasFieldOrPropertyWithValue("extracted", "G03X0") .hasFieldOrPropertyWithValue("remainder", "G53F100S1300"); } + + @Test + public void testSplitCommand() { + List splitted = GcodePreprocessorUtils.splitCommand("G53F100S1300"); + assertEquals(3, splitted.size()); + assertEquals("G53", splitted.get(0)); + assertEquals("F100", splitted.get(1)); + assertEquals("S1300", splitted.get(2)); + + splitted = GcodePreprocessorUtils.splitCommand("G53G90.1S1300"); + assertEquals(3, splitted.size()); + assertEquals("G90.1", splitted.get(1)); + + splitted = GcodePreprocessorUtils.splitCommand("G53G90_1S1300"); + assertEquals(4, splitted.size()); + assertEquals("G90", splitted.get(1)); + assertEquals("1", splitted.get(2)); + } } diff --git a/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java b/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java index fa6758f2d..61cd22125 100644 --- a/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java +++ b/ugs-core/test/com/willwinder/universalgcodesender/TinyGUtilsTest.java @@ -183,7 +183,7 @@ public void convertStatusReportShouldHandleArcDistanceMode() { List result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertTrue(result.contains(Code.G90_1.name())); + assertTrue(result.contains(Code.G90_1.toString())); // When switch to inverse time mode @@ -191,6 +191,6 @@ public void convertStatusReportShouldHandleArcDistanceMode() { result = TinyGUtils.convertStatusReportToGcode(response); // Then - assertTrue(result.contains(Code.G91_1.name())); + assertTrue(result.contains(Code.G91_1.toString())); } } From 61e5858e54c28affa2068901a7ff7bf4ec239cab Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Sat, 14 Jul 2018 09:54:45 +0200 Subject: [PATCH 12/13] Fixed issue where numeric fields can be returned with true/false causing error messages --- .../universalgcodesender/TinyGUtils.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java index 71998f667..e1b5fcdf9 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGUtils.java @@ -29,6 +29,7 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.model.UnitUtils; import com.willwinder.universalgcodesender.model.WorkCoordinateSystem; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; import java.util.ArrayList; import java.util.List; @@ -135,40 +136,40 @@ public static ControllerStatus updateControllerStatus(final ControllerStatus las UnitUtils.Units currentUnits = lastGcodeState.units == Code.G20 ? UnitUtils.Units.INCH : UnitUtils.Units.MM; Position workCoord = lastControllerStatus.getWorkCoord().getPositionIn(currentUnits); - if (statusResultObject.has(FIELD_STATUS_REPORT_POSX)) { + if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_POSX)) { workCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_POSX).getAsDouble()); } - if (statusResultObject.has(FIELD_STATUS_REPORT_POSY)) { + if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_POSY)) { workCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_POSY).getAsDouble()); } - if (statusResultObject.has(FIELD_STATUS_REPORT_POSZ)) { + if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_POSZ)) { workCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_POSZ).getAsDouble()); } // 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); - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOX)) { + if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_MPOX)) { machineCoord.setX(statusResultObject.get(FIELD_STATUS_REPORT_MPOX).getAsDouble()); } - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOY)) { + if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_MPOY)) { machineCoord.setY(statusResultObject.get(FIELD_STATUS_REPORT_MPOY).getAsDouble()); } - if (statusResultObject.has(FIELD_STATUS_REPORT_MPOZ)) { + if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_MPOZ)) { machineCoord.setZ(statusResultObject.get(FIELD_STATUS_REPORT_MPOZ).getAsDouble()); } Double feedSpeed = lastControllerStatus.getFeedSpeed(); - if (statusResultObject.has(FIELD_STATUS_REPORT_VELOCITY)) { + if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_VELOCITY)) { feedSpeed = statusResultObject.get(FIELD_STATUS_REPORT_VELOCITY).getAsDouble(); } ControllerState state = lastControllerStatus.getState(); String stateString = lastControllerStatus.getStateString(); - if (statusResultObject.has(FIELD_STATUS_REPORT_STATUS)) { + if (hasNumericField(statusResultObject, FIELD_STATUS_REPORT_STATUS)) { state = getState(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); stateString = getStateAsString(statusResultObject.get(FIELD_STATUS_REPORT_STATUS).getAsInt()); } @@ -275,12 +276,12 @@ public static List convertStatusReportToGcode(JsonObject response) { if (response.has(TinyGUtils.FIELD_STATUS_REPORT)) { JsonObject statusResultObject = response.getAsJsonObject(TinyGUtils.FIELD_STATUS_REPORT); - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_COORD)) { + if (hasNumericField(statusResultObject, TinyGUtils.FIELD_STATUS_REPORT_COORD)) { int offsetCode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_COORD).getAsInt(); gcodeList.add(WorkCoordinateSystem.fromPValue(offsetCode).getGcode().name()); } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_UNIT)) { + if (hasNumericField(statusResultObject, TinyGUtils.FIELD_STATUS_REPORT_UNIT)) { int units = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_UNIT).getAsInt(); // 0=inch, 1=mm if (units == 0) { @@ -290,7 +291,7 @@ public static List convertStatusReportToGcode(JsonObject response) { } } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_PLANE)) { + if (hasNumericField(statusResultObject, TinyGUtils.FIELD_STATUS_REPORT_PLANE)) { int plane = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_PLANE).getAsInt(); // 0=XY plane, 1=XZ plane, 2=YZ plane if (plane == 0) { @@ -302,7 +303,7 @@ public static List convertStatusReportToGcode(JsonObject response) { } } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE)) { + if (hasNumericField(statusResultObject, TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE)) { int feedMode = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_FEED_MODE).getAsInt(); // 0=units-per-minute-mode, 1=inverse-time-mode if (feedMode == 0) { @@ -312,7 +313,7 @@ public static List convertStatusReportToGcode(JsonObject response) { } } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE)) { + if (hasNumericField(statusResultObject, TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE)) { int distance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_DISTANCE_MODE).getAsInt(); // 0=absolute distance mode, 1=incremental distance mode if (distance == 0) { @@ -322,7 +323,7 @@ public static List convertStatusReportToGcode(JsonObject response) { } } - if (statusResultObject.has(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE)) { + if (hasNumericField(statusResultObject, TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE)) { int arcDistance = statusResultObject.get(TinyGUtils.FIELD_STATUS_REPORT_ARC_DISTANCE_MODE).getAsInt(); // 0=absolute distance mode, 1=incremental distance mode if (arcDistance == 0) { @@ -334,4 +335,9 @@ public static List convertStatusReportToGcode(JsonObject response) { } return gcodeList; } + + private static boolean hasNumericField(JsonObject statusResultObject, String fieldName) { + return statusResultObject.has(fieldName) && + NumberUtils.isNumber(statusResultObject.get(fieldName).getAsString()); + } } From 939539200f89d99428db0c0f8bfc9e430ead5981 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Mon, 16 Jul 2018 15:02:48 +0200 Subject: [PATCH 13/13] Fixed bad merge after code review --- .../com/willwinder/universalgcodesender/TinyGController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java index 4ad53e2fd..487a3bbe6 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/TinyGController.java @@ -267,7 +267,7 @@ public void updateParserModalState(GcodeCommand command) { @Override public void performHomingCycle() throws Exception { - sendCommandImmediately(new GcodeCommand("G28.2 X0 Y0 Z0")); + sendCommandImmediately(new GcodeCommand("G28.2 Z0 X0 Y0")); } @Override