Search in sources :

Example 6 with ControllerStatus

use of com.willwinder.universalgcodesender.listeners.ControllerStatus in project Universal-G-Code-Sender by winder.

the class GrblController method rawResponseHandler.

@Override
protected void rawResponseHandler(String response) {
    String processed = response;
    try {
        boolean verbose = false;
        if (GrblUtils.isOkResponse(response)) {
            this.commandComplete(processed);
        } else // Error case.
        if (GrblUtils.isOkErrorAlarmResponse(response)) {
            if (GrblUtils.isAlarmResponse(response)) {
                // this is not updating the state to Alarm in the GUI, and the alarm is no longer being processed
                // TODO: Find a builder library.
                this.controllerStatus = new ControllerStatus(lookupCode(response, true), this.controllerStatus.getMachineCoord(), this.controllerStatus.getWorkCoord(), this.controllerStatus.getFeedSpeed(), this.controllerStatus.getSpindleSpeed(), this.controllerStatus.getOverrides(), this.controllerStatus.getWorkCoordinateOffset(), this.controllerStatus.getEnabledPins(), this.controllerStatus.getAccessoryStates());
                dispatchStatusString(this.controllerStatus);
                dispatchStateChange(COMM_IDLE);
            }
            // If there is an active command, mark it as completed with error
            Optional<GcodeCommand> activeCommand = this.getActiveCommand();
            if (activeCommand.isPresent()) {
                processed = String.format(Localization.getString("controller.exception.sendError"), activeCommand.get().getCommandString(), lookupCode(response, false)).replaceAll("\\.\\.", "\\.");
                this.errorMessageForConsole(processed + "\n");
                this.commandComplete(processed);
            } else {
                processed = String.format(Localization.getString("controller.exception.unexpectedError"), lookupCode(response, false)).replaceAll("\\.\\.", "\\.");
                this.errorMessageForConsole(processed + "\n");
            }
            checkStreamFinished();
            processed = "";
        } else if (GrblUtils.isGrblVersionString(response)) {
            this.isReady = true;
            resetBuffers();
            // single step mode
            if (getControlState() != COMM_CHECK) {
                this.controllerStatus = null;
            }
            this.stopPollingPosition();
            positionPollTimer = createPositionPollTimer();
            this.beginPollingPosition();
            // In case a reset occurred while streaming.
            if (this.isStreaming()) {
                checkStreamFinished();
            }
            this.grblVersion = GrblUtils.getVersionDouble(response);
            this.grblVersionLetter = GrblUtils.getVersionLetter(response);
            this.capabilities = GrblUtils.getGrblStatusCapabilities(this.grblVersion, this.grblVersionLetter);
            try {
                this.sendCommandImmediately(createCommand(GrblUtils.GRBL_VIEW_SETTINGS_COMMAND));
                this.sendCommandImmediately(createCommand(GrblUtils.GRBL_VIEW_PARSER_STATE_COMMAND));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            Logger.getLogger(GrblController.class.getName()).log(Level.CONFIG, "{0} = {1}{2}", new Object[] { Localization.getString("controller.log.version"), this.grblVersion, this.grblVersionLetter });
            Logger.getLogger(GrblController.class.getName()).log(Level.CONFIG, "{0} = {1}", new Object[] { Localization.getString("controller.log.realtime"), this.capabilities.hasCapability(GrblCapabilitiesConstants.REAL_TIME) });
        } else if (GrblUtils.isGrblProbeMessage(response)) {
            Position p = GrblUtils.parseProbePosition(response, getFirmwareSettings().getReportingUnits());
            if (p != null) {
                dispatchProbeCoordinates(p);
            }
        } else if (GrblUtils.isGrblStatusString(response)) {
            // Only 1 poll is sent at a time so don't decrement, reset to zero.
            this.outstandingPolls = 0;
            // Status string goes to verbose console
            verbose = true;
            this.handleStatusString(response);
        } else if (GrblUtils.isGrblFeedbackMessage(response, capabilities)) {
            GrblFeedbackMessage grblFeedbackMessage = new GrblFeedbackMessage(response);
            // Convert feedback message to raw commands to update modal state.
            this.updateParserModalState(new GcodeCommand(GrblUtils.parseFeedbackMessage(response, capabilities)));
            this.verboseMessageForConsole(grblFeedbackMessage.toString() + "\n");
            setDistanceModeCode(grblFeedbackMessage.getDistanceMode());
            setUnitsCode(grblFeedbackMessage.getUnits());
            dispatchStateChange(COMM_IDLE);
        } else if (GrblUtils.isGrblSettingMessage(response)) {
            GrblSettingMessage message = new GrblSettingMessage(response);
            processed = message.toString();
        }
        if (StringUtils.isNotBlank(processed)) {
            if (verbose) {
                this.verboseMessageForConsole(processed + "\n");
            } else {
                this.messageForConsole(processed + "\n");
            }
        }
    } catch (Exception e) {
        String message = "";
        if (e.getMessage() != null) {
            message = ": " + e.getMessage();
        }
        message = Localization.getString("controller.error.response") + " <" + processed + ">" + message;
        logger.log(Level.SEVERE, message, e);
        this.errorMessageForConsole(message + "\n");
    }
}
Also used : GrblSettingMessage(com.willwinder.universalgcodesender.types.GrblSettingMessage) ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus) Position(com.willwinder.universalgcodesender.model.Position) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) GrblFeedbackMessage(com.willwinder.universalgcodesender.types.GrblFeedbackMessage)

Example 7 with ControllerStatus

use of com.willwinder.universalgcodesender.listeners.ControllerStatus in project Universal-G-Code-Sender by winder.

the class GrblUtils method getStatusFromStatusString.

/**
 * Parses a GRBL status string in the legacy format or v1.x format:
 * legacy: <status,WPos:1,2,3,MPos:1,2,3>
 * 1.x: <status|WPos:1,2,3|Bf:0,0|WCO:0,0,0>
 * @param lastStatus required for the 1.x version which requires WCO coords
 *                   and override status from previous status updates.
 * @param status the raw status string
 * @param version capabilities flags
 * @param reportingUnits units
 * @return
 */
protected static ControllerStatus getStatusFromStatusString(ControllerStatus lastStatus, final String status, final Capabilities version, Units reportingUnits) {
    // Legacy status.
    if (!version.hasCapability(GrblCapabilitiesConstants.V1_FORMAT)) {
        return new ControllerStatus(getStateFromStatusString(status, version), getMachinePositionFromStatusString(status, version, reportingUnits), getWorkPositionFromStatusString(status, version, reportingUnits));
    } else {
        String state = "";
        Position MPos = null;
        Position WPos = null;
        Position WCO = null;
        OverridePercents overrides = null;
        EnabledPins pins = null;
        AccessoryStates accessoryStates = null;
        Double feedSpeed = null;
        Double spindleSpeed = null;
        boolean isOverrideReport = false;
        // Parse out the status messages.
        for (String part : status.substring(0, status.length() - 1).split("\\|")) {
            if (part.startsWith("<")) {
                int idx = part.indexOf(':');
                if (idx == -1)
                    state = part.substring(1);
                else
                    state = part.substring(1, idx);
            } else if (part.startsWith("MPos:")) {
                MPos = GrblUtils.getPositionFromStatusString(status, machinePattern, reportingUnits);
            } else if (part.startsWith("WPos:")) {
                WPos = GrblUtils.getPositionFromStatusString(status, workPattern, reportingUnits);
            } else if (part.startsWith("WCO:")) {
                WCO = GrblUtils.getPositionFromStatusString(status, wcoPattern, reportingUnits);
            } else if (part.startsWith("Ov:")) {
                isOverrideReport = true;
                String[] overrideParts = part.substring(3).trim().split(",");
                if (overrideParts.length == 3) {
                    overrides = new OverridePercents(Integer.parseInt(overrideParts[0]), Integer.parseInt(overrideParts[1]), Integer.parseInt(overrideParts[2]));
                }
            } else if (part.startsWith("F:")) {
                feedSpeed = Double.parseDouble(part.substring(2));
            } else if (part.startsWith("FS:")) {
                String[] parts = part.substring(3).split(",");
                feedSpeed = Double.parseDouble(parts[0]);
                spindleSpeed = Double.parseDouble(parts[1]);
            } else if (part.startsWith("Pn:")) {
                String value = part.substring(part.indexOf(':') + 1);
                pins = new EnabledPins(value);
            } else if (part.startsWith("A:")) {
                String value = part.substring(part.indexOf(':') + 1);
                accessoryStates = new AccessoryStates(value);
            }
        }
        // Grab WCO from state information if necessary.
        if (WCO == null) {
            // Grab the work coordinate offset.
            if (lastStatus != null && lastStatus.getWorkCoordinateOffset() != null) {
                WCO = lastStatus.getWorkCoordinateOffset();
            } else {
                WCO = new Position(0, 0, 0, reportingUnits);
            }
        }
        // Calculate missing coordinate with WCO
        if (WPos == null) {
            WPos = new Position(MPos.x - WCO.x, MPos.y - WCO.y, MPos.z - WCO.z, reportingUnits);
        }
        if (MPos == null) {
            MPos = new Position(WPos.x + WCO.x, WPos.y + WCO.y, WPos.z + WCO.z, reportingUnits);
        }
        if (!isOverrideReport && lastStatus != null) {
            overrides = lastStatus.getOverrides();
            pins = lastStatus.getEnabledPins();
            accessoryStates = lastStatus.getAccessoryStates();
        } else if (isOverrideReport) {
            // set all pins to a disabled state.
            if (pins == null) {
                pins = new EnabledPins("");
            }
            // Likewise for accessory states.
            if (accessoryStates == null) {
                accessoryStates = new AccessoryStates("");
            }
        }
        return new ControllerStatus(state, MPos, WPos, feedSpeed, spindleSpeed, overrides, WCO, pins, accessoryStates);
    }
}
Also used : ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus) Position(com.willwinder.universalgcodesender.model.Position) AccessoryStates(com.willwinder.universalgcodesender.listeners.ControllerStatus.AccessoryStates) EnabledPins(com.willwinder.universalgcodesender.listeners.ControllerStatus.EnabledPins) OverridePercents(com.willwinder.universalgcodesender.listeners.ControllerStatus.OverridePercents)

Example 8 with ControllerStatus

use of com.willwinder.universalgcodesender.listeners.ControllerStatus in project Universal-G-Code-Sender by winder.

the class GrblUtilsTest method getStatusFromStringVersion1WithoutAccessoryStatusString.

@Test
public void getStatusFromStringVersion1WithoutAccessoryStatusString() {
    String status = "<Idle|WPos:4.0,5.0,6.0|WCO:7.0,8.0,9.0|Ov:1,2,3|FS:12345.7,65432.1|F:12345.6>";
    Capabilities version = new Capabilities();
    version.addCapability(GrblCapabilitiesConstants.V1_FORMAT);
    UnitUtils.Units unit = UnitUtils.Units.MM;
    ControllerStatus controllerStatus = GrblUtils.getStatusFromStatusString(null, status, version, unit);
    assertFalse(controllerStatus.getAccessoryStates().Flood);
    assertFalse(controllerStatus.getAccessoryStates().Mist);
    assertFalse(controllerStatus.getAccessoryStates().SpindleCCW);
    assertFalse(controllerStatus.getAccessoryStates().SpindleCW);
}
Also used : UnitUtils(com.willwinder.universalgcodesender.model.UnitUtils) ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus) Test(org.junit.Test)

Example 9 with ControllerStatus

use of com.willwinder.universalgcodesender.listeners.ControllerStatus in project Universal-G-Code-Sender by winder.

the class GrblUtilsTest method getStatusFromStringVersion1WithoutMachineCoordinateStatusString.

@Test
public void getStatusFromStringVersion1WithoutMachineCoordinateStatusString() {
    String status = "<Idle|WPos:4.0,5.0,6.0|WCO:7.0,8.0,9.0|Ov:1,2,3|F:12345.6|FS:12345.7,65432.1|Pn:XYZPDHRS|A:SFMC>";
    Capabilities version = new Capabilities();
    version.addCapability(GrblCapabilitiesConstants.V1_FORMAT);
    UnitUtils.Units unit = UnitUtils.Units.MM;
    ControllerStatus controllerStatus = GrblUtils.getStatusFromStatusString(null, status, version, unit);
    assertEquals(new Position(11, 13, 15, UnitUtils.Units.MM), controllerStatus.getMachineCoord());
    assertEquals(new Position(4, 5, 6, UnitUtils.Units.MM), controllerStatus.getWorkCoord());
    assertEquals(new Position(7, 8, 9, UnitUtils.Units.MM), controllerStatus.getWorkCoordinateOffset());
}
Also used : UnitUtils(com.willwinder.universalgcodesender.model.UnitUtils) ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus) Position(com.willwinder.universalgcodesender.model.Position) Test(org.junit.Test)

Example 10 with ControllerStatus

use of com.willwinder.universalgcodesender.listeners.ControllerStatus in project Universal-G-Code-Sender by winder.

the class GrblUtilsTest method getStatusFromStringVersion1WithCompleteStatusString.

@Test
public void getStatusFromStringVersion1WithCompleteStatusString() {
    String status = "<Idle|MPos:1.1,2.2,3.3|WPos:4.4,5.5,6.6|WCO:7.7,8.8,9.9|Ov:1,2,3|F:12345.6|FS:12345.7,65432.1|Pn:XYZPDHRS|A:SFMC>";
    Capabilities version = new Capabilities();
    version.addCapability(GrblCapabilitiesConstants.V1_FORMAT);
    UnitUtils.Units unit = UnitUtils.Units.MM;
    ControllerStatus controllerStatus = GrblUtils.getStatusFromStatusString(null, status, version, unit);
    assertEquals("Idle", controllerStatus.getState());
    assertEquals(new Position(1.1, 2.2, 3.3, UnitUtils.Units.MM), controllerStatus.getMachineCoord());
    assertEquals(new Position(4.4, 5.5, 6.6, UnitUtils.Units.MM), controllerStatus.getWorkCoord());
    assertEquals(new Position(7.7, 8.8, 9.9, UnitUtils.Units.MM), controllerStatus.getWorkCoordinateOffset());
    assertEquals(1, controllerStatus.getOverrides().feed);
    assertEquals(2, controllerStatus.getOverrides().rapid);
    assertEquals(3, controllerStatus.getOverrides().spindle);
    assertEquals(Double.valueOf(12345.7), controllerStatus.getFeedSpeed());
    assertEquals(Double.valueOf(65432.1), controllerStatus.getSpindleSpeed());
    assertTrue(controllerStatus.getEnabledPins().CycleStart);
    assertTrue(controllerStatus.getEnabledPins().Door);
    assertTrue(controllerStatus.getEnabledPins().Hold);
    assertTrue(controllerStatus.getEnabledPins().SoftReset);
    assertTrue(controllerStatus.getEnabledPins().Probe);
    assertTrue(controllerStatus.getEnabledPins().X);
    assertTrue(controllerStatus.getEnabledPins().Y);
    assertTrue(controllerStatus.getEnabledPins().Z);
    assertTrue(controllerStatus.getAccessoryStates().Flood);
    assertTrue(controllerStatus.getAccessoryStates().Mist);
    assertTrue(controllerStatus.getAccessoryStates().SpindleCCW);
    assertTrue(controllerStatus.getAccessoryStates().SpindleCW);
}
Also used : UnitUtils(com.willwinder.universalgcodesender.model.UnitUtils) ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus) Position(com.willwinder.universalgcodesender.model.Position) Test(org.junit.Test)

Aggregations

ControllerStatus (com.willwinder.universalgcodesender.listeners.ControllerStatus)10 UnitUtils (com.willwinder.universalgcodesender.model.UnitUtils)7 Test (org.junit.Test)7 Position (com.willwinder.universalgcodesender.model.Position)6 JsonObject (com.google.gson.JsonObject)1 AccessoryStates (com.willwinder.universalgcodesender.listeners.ControllerStatus.AccessoryStates)1 EnabledPins (com.willwinder.universalgcodesender.listeners.ControllerStatus.EnabledPins)1 OverridePercents (com.willwinder.universalgcodesender.listeners.ControllerStatus.OverridePercents)1 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)1 GrblFeedbackMessage (com.willwinder.universalgcodesender.types.GrblFeedbackMessage)1 GrblSettingMessage (com.willwinder.universalgcodesender.types.GrblSettingMessage)1 IOException (java.io.IOException)1