Search in sources :

Example 26 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand 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 27 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GrblController method killAlarmLock.

@Override
public void killAlarmLock() throws Exception {
    if (this.isCommOpen()) {
        String gcode = GrblUtils.getKillAlarmLockCommand(this.grblVersion, this.grblVersionLetter);
        if (!"".equals(gcode)) {
            GcodeCommand command = createCommand(gcode);
            this.sendCommandImmediately(command);
            return;
        }
    }
    // Throw exception
    super.killAlarmLock();
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Example 28 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GrblController method toggleCheckMode.

@Override
public void toggleCheckMode() throws Exception {
    if (this.isCommOpen()) {
        String gcode = GrblUtils.getToggleCheckModeCommand(this.grblVersion, this.grblVersionLetter);
        if (!"".equals(gcode)) {
            GcodeCommand command = createCommand(gcode);
            this.sendCommandImmediately(command);
            return;
        }
    }
    // Throw exception
    super.toggleCheckMode();
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Example 29 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class AbstractController method commandComplete.

/**
 * Notify controller that the next command has completed with response and
 * that the stream is complete once the last command has finished.
 */
public void commandComplete(String response) throws UnexpectedCommand {
    if (this.activeCommands.isEmpty()) {
        throw new UnexpectedCommand(Localization.getString("controller.exception.unexpectedCommand"));
    }
    GcodeCommand command = this.activeCommands.remove(0);
    command.setResponse(response);
    updateParserModalState(command);
    this.numCommandsCompleted++;
    if (this.activeCommands.isEmpty()) {
        this.setCurrentState(COMM_IDLE);
    }
    dispatchCommandComplete(command);
    checkStreamFinished();
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Example 30 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class AbstractController method jogMachine.

@Override
public void jogMachine(int dirX, int dirY, int dirZ, double stepSize, double feedRate, UnitUtils.Units units) throws Exception {
    logger.log(Level.INFO, "Adjusting manual location.");
    // Format step size from spinner.
    String formattedStepSize = Utils.formatter.format(stepSize);
    String formattedFeedRate = Utils.formatter.format(feedRate);
    String commandString = GcodeUtils.generateXYZ("G91G0", units, formattedStepSize, formattedFeedRate, dirX, dirY, dirZ);
    GcodeCommand command = createCommand(commandString);
    command.setTemporaryParserModalChange(true);
    sendCommandImmediately(command);
    restoreParserModalState();
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Aggregations

GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)51 Test (org.junit.Test)26 GcodeStreamTest (com.willwinder.universalgcodesender.utils.GcodeStreamTest)19 IOException (java.io.IOException)11 Connection (com.willwinder.universalgcodesender.connection.Connection)4 Position (com.willwinder.universalgcodesender.model.Position)4 ControllerListener (com.willwinder.universalgcodesender.listeners.ControllerListener)3 GcodeStreamReader (com.willwinder.universalgcodesender.utils.GcodeStreamReader)3 File (java.io.File)3 LinkedList (java.util.LinkedList)3 GcodeParser (com.willwinder.universalgcodesender.gcode.GcodeParser)2 EasyMock.anyString (org.easymock.EasyMock.anyString)2 UnexpectedCommand (com.willwinder.universalgcodesender.AbstractController.UnexpectedCommand)1 GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)1 ArcExpander (com.willwinder.universalgcodesender.gcode.processors.ArcExpander)1 CommentProcessor (com.willwinder.universalgcodesender.gcode.processors.CommentProcessor)1 LineSplitter (com.willwinder.universalgcodesender.gcode.processors.LineSplitter)1 M30Processor (com.willwinder.universalgcodesender.gcode.processors.M30Processor)1 MeshLeveler (com.willwinder.universalgcodesender.gcode.processors.MeshLeveler)1 Translator (com.willwinder.universalgcodesender.gcode.processors.Translator)1