Search in sources :

Example 6 with ControllerState

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

the class GrblUtils method getStatusFromStatusStringV1.

/**
 * Parses a GRBL status string in in the v1.x format:
 * 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 reportingUnits units
 * @return the parsed controller status
 */
public static ControllerStatus getStatusFromStatusStringV1(ControllerStatus lastStatus, String status, Units reportingUnits) {
    String stateString = "";
    Position MPos = null;
    Position WPos = null;
    Position WCO = null;
    OverridePercents overrides = null;
    EnabledPins pins = null;
    AccessoryStates accessoryStates = null;
    double feedSpeed = 0;
    double spindleSpeed = 0;
    if (lastStatus != null) {
        feedSpeed = lastStatus.getFeedSpeed();
        spindleSpeed = lastStatus.getSpindleSpeed();
    }
    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)
                stateString = part.substring(1);
            else
                stateString = 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 = parseFeedSpeed(part);
        } 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, 0, 0, 0, reportingUnits);
        }
    }
    // Calculate missing coordinate with WCO
    if (WPos == null && MPos != null) {
        WPos = new Position(MPos.x - WCO.x, MPos.y - WCO.y, MPos.z - WCO.z, MPos.a - WCO.a, MPos.b - WCO.b, MPos.c - WCO.c, reportingUnits);
    } else if (MPos == null && WPos != null) {
        MPos = new Position(WPos.x + WCO.x, WPos.y + WCO.y, WPos.z + WCO.z, WPos.a + WCO.a, WPos.b + WCO.b, WPos.c + WCO.c, 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("");
        }
    }
    ControllerState state = getControllerStateFromStateString(stateString);
    return new ControllerStatus(state, MPos, WPos, feedSpeed, reportingUnits, spindleSpeed, overrides, WCO, pins, accessoryStates);
}
Also used : ControllerState(com.willwinder.universalgcodesender.listeners.ControllerState) ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus) AccessoryStates(com.willwinder.universalgcodesender.listeners.ControllerStatus.AccessoryStates) EnabledPins(com.willwinder.universalgcodesender.listeners.ControllerStatus.EnabledPins) OverridePercents(com.willwinder.universalgcodesender.listeners.ControllerStatus.OverridePercents)

Example 7 with ControllerState

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

the class GrblUtils method getStatusFromStatusStringLegacy.

/**
 * Parses a GRBL status string in the legacy format:
 * legacy: <status,WPos:1,2,3,MPos:1,2,3>
 * @param status the raw status string
 * @param version capabilities flags
 * @param reportingUnits units
 * @return the parsed controller status
 */
private static ControllerStatus getStatusFromStatusStringLegacy(String status, Capabilities version, Units reportingUnits) {
    String stateString = StringUtils.defaultString(getStateFromStatusString(status, version), "unknown");
    ControllerState state = getControllerStateFromStateString(stateString);
    return new ControllerStatus(state, getMachinePositionFromStatusString(status, version, reportingUnits), getWorkPositionFromStatusString(status, version, reportingUnits));
}
Also used : ControllerState(com.willwinder.universalgcodesender.listeners.ControllerState) ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus)

Example 8 with ControllerState

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

the class TinyGController method updateControllerStatus.

private void updateControllerStatus(JsonObject jo) {
    // Save the old state
    ControllerState previousState = controllerStatus.getState();
    CommunicatorState previousControlState = getControlState(previousState);
    // Update the internal state
    List<String> gcodeList = TinyGUtils.convertStatusReportToGcode(jo);
    gcodeList.forEach(gcode -> updateParserModalState(new GcodeCommand(gcode)));
    // Notify our listeners about the new status
    controllerStatus = parseControllerStatus(jo);
    dispatchStatusString(controllerStatus);
    // Notify state change to our listeners
    CommunicatorState newControlState = getControlState(controllerStatus.getState());
    if (!previousControlState.equals(newControlState)) {
        LOGGER.log(Level.FINE, "Changing state from " + previousControlState + " to " + newControlState);
        setCurrentState(newControlState);
    }
}
Also used : ControllerState(com.willwinder.universalgcodesender.listeners.ControllerState) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) TinyGGcodeCommand(com.willwinder.universalgcodesender.types.TinyGGcodeCommand)

Aggregations

ControllerState (com.willwinder.universalgcodesender.listeners.ControllerState)8 ControllerStatus (com.willwinder.universalgcodesender.listeners.ControllerStatus)3 ControllerStateEvent (com.willwinder.universalgcodesender.model.events.ControllerStateEvent)3 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 PartialPosition (com.willwinder.universalgcodesender.model.PartialPosition)1 Position (com.willwinder.universalgcodesender.model.Position)1 UnitUtils (com.willwinder.universalgcodesender.model.UnitUtils)1 ProbeEvent (com.willwinder.universalgcodesender.model.events.ProbeEvent)1 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)1 TinyGGcodeCommand (com.willwinder.universalgcodesender.types.TinyGGcodeCommand)1