Search in sources :

Example 1 with Position

use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.

the class GrblController method handleStatusString.

// No longer a listener event
private void handleStatusString(final String string) {
    if (this.capabilities == null) {
        return;
    }
    ControlState before = getControlState();
    String beforeState = controllerStatus == null ? "" : controllerStatus.getState();
    controllerStatus = GrblUtils.getStatusFromStatusString(controllerStatus, string, capabilities, getFirmwareSettings().getReportingUnits());
    // Make UGS more responsive to the state being reported by GRBL.
    if (before != getControlState()) {
        this.dispatchStateChange(getControlState());
    }
    // GRBL 1.1 jog complete transition
    if (StringUtils.equals(beforeState, "Jog") && controllerStatus.getState().equals("Idle")) {
        this.comm.cancelSend();
    }
    // Set and restore the step mode when transitioning from CHECK mode to IDLE.
    if (before == COMM_CHECK && getControlState() != COMM_CHECK) {
        setSingleStepMode(temporaryCheckSingleStepMode);
    } else if (before != COMM_CHECK && getControlState() == COMM_CHECK) {
        temporaryCheckSingleStepMode = getSingleStepMode();
        setSingleStepMode(true);
    }
    // pausing.
    if (isCanceling) {
        if (attemptsRemaining > 0 && lastLocation != null) {
            attemptsRemaining--;
            // If the machine goes into idle, we no longer need to cancel.
            if (this.controllerStatus.getState().equals("Idle") || this.controllerStatus.getState().equalsIgnoreCase("Check")) {
                isCanceling = false;
                // Make sure the GUI gets updated
                this.dispatchStateChange(getControlState());
            } else // Otherwise check if the machine is Hold/Queue and stopped.
            if ((this.controllerStatus.getState().equals("Hold") || this.controllerStatus.getState().equals("Queue")) && lastLocation.equals(this.controllerStatus.getMachineCoord())) {
                try {
                    this.issueSoftReset();
                } catch (Exception e) {
                    this.errorMessageForConsole(e.getMessage() + "\n");
                }
                isCanceling = false;
            }
            if (isCanceling && attemptsRemaining == 0) {
                this.errorMessageForConsole(Localization.getString("grbl.exception.cancelReset") + "\n");
            }
        }
        lastLocation = new Position(this.controllerStatus.getMachineCoord());
    }
    // Save max Z location
    if (this.controllerStatus != null && this.getUnitsCode() != null && this.controllerStatus.getMachineCoord() != null) {
        Units u = this.getUnitsCode().equalsIgnoreCase("G21") ? Units.MM : Units.INCH;
        double zLocationMM = this.controllerStatus.getMachineCoord().z;
        if (u == Units.INCH)
            zLocationMM *= 26.4;
        if (zLocationMM > this.maxZLocationMM) {
            maxZLocationMM = zLocationMM;
        }
    }
    dispatchStatusString(controllerStatus);
}
Also used : ControlState(com.willwinder.universalgcodesender.model.UGSEvent.ControlState) Position(com.willwinder.universalgcodesender.model.Position) Units(com.willwinder.universalgcodesender.model.UnitUtils.Units)

Example 2 with Position

use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.

the class GcodeParser method handleGCode.

/**
 * Branch parser to handle specific gcode command.
 *
 * A copy of the state object should go in the resulting GcodeMeta object.
 */
private static GcodeMeta handleGCode(final Code code, List<String> args, int line, GcodeState state, boolean hasAxisWords) throws GcodeParserException {
    GcodeMeta meta = new GcodeMeta();
    meta.code = code;
    Position nextPoint = null;
    // If it is a movement code make sure it has some coordinates.
    if (code.consumesMotion()) {
        nextPoint = GcodePreprocessorUtils.updatePointWithCommand(args, state.currentPoint, state.inAbsoluteMode);
        if (nextPoint == null) {
            if (!code.motionOptional()) {
                throw new GcodeParserException(Localization.getString("parser.gcode.missing-axis-commands") + ": " + code);
            }
        }
    }
    if (nextPoint == null && meta.point != null) {
        nextPoint = meta.point.point();
    }
    switch(code) {
        case G0:
            meta.point = addLinearPointSegment(nextPoint, true, line, state);
            break;
        case G1:
            meta.point = addLinearPointSegment(nextPoint, false, line, state);
            break;
        // Arc command.
        case G2:
            meta.point = addArcPointSegment(nextPoint, true, args, line, state);
            break;
        case G3:
            meta.point = addArcPointSegment(nextPoint, false, args, line, state);
            break;
        case G17:
        case G18:
        case G19:
        case G17_1:
        case G18_1:
        case G19_1:
            state.plane = Plane.lookup(code);
            break;
        // inch
        case G20:
            state.isMetric = false;
            state.units = G20;
            state.currentPoint = state.currentPoint.getPositionIn(UnitUtils.Units.INCH);
            break;
        // mm
        case G21:
            state.isMetric = true;
            state.units = G21;
            state.currentPoint = state.currentPoint.getPositionIn(UnitUtils.Units.MM);
            break;
        // probe toward workpiece, stop on contact, signal error if failure
        case G38_2:
        // probe toward workpiece, stop on contact
        case G38_3:
        // probe away from workpiece, stop on loss of contact, signal error if failure
        case G38_4:
        case // probe away from workpiece, stop on loss of contact
        G38_5:
            meta.point = addProbePointSegment(nextPoint, true, line, state);
            break;
        // These are not used in the visualizer.
        case G54:
        case G55:
        case G56:
        case G57:
        case G58:
        case G59:
        case G59_1:
        case G59_2:
        case G59_3:
            state.offset = code;
            break;
        case G90:
            state.inAbsoluteMode = true;
            state.distanceMode = G90;
            break;
        case G91:
            state.inAbsoluteMode = false;
            state.distanceMode = G91;
            break;
        case G90_1:
            state.inAbsoluteIJKMode = true;
            state.arcDistanceMode = G90_1;
            break;
        case G91_1:
            state.inAbsoluteIJKMode = false;
            state.arcDistanceMode = G91_1;
            break;
        case G93:
        case G94:
        case G95:
            state.feedMode = code;
            break;
        default:
            break;
    }
    if (code.getType() == Motion) {
        state.currentMotionMode = code;
    }
    meta.state = state.copy();
    return meta;
}
Also used : Position(com.willwinder.universalgcodesender.model.Position) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException)

Example 3 with Position

use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.

the class GcodeParser method reset.

/**
 * Resets the current state.
 */
public void reset() {
    this.statsProcessor = new Stats();
    this.state.currentPoint = new Position();
    this.state.commandNumber = -1;
}
Also used : Position(com.willwinder.universalgcodesender.model.Position) Stats(com.willwinder.universalgcodesender.gcode.processors.Stats)

Example 4 with Position

use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.

the class GcodeParser method addArcPointSegment.

/**
 * Create a PointSegment representing the arc command.
 */
private static PointSegment addArcPointSegment(Position nextPoint, boolean clockwise, List<String> args, int line, GcodeState state) {
    if (nextPoint == null) {
        return null;
    }
    PointSegment ps = new PointSegment(nextPoint, line);
    Position center = GcodePreprocessorUtils.updateCenterWithCommand(args, state.currentPoint, nextPoint, state.inAbsoluteIJKMode, clockwise, new PlaneFormatter(state.plane));
    double radius = GcodePreprocessorUtils.parseCoord(args, 'R');
    // Calculate radius if necessary.
    if (Double.isNaN(radius)) {
        radius = Math.sqrt(Math.pow(state.currentPoint.x - center.x, 2.0) + Math.pow(state.currentPoint.y - center.y, 2.0));
    }
    ps.setIsMetric(state.isMetric);
    ps.setArcCenter(center);
    ps.setIsArc(true);
    ps.setRadius(radius);
    ps.setIsClockwise(clockwise);
    ps.setPlaneState(state.plane);
    // Save off the endpoint.
    state.currentPoint = nextPoint;
    return ps;
}
Also used : Position(com.willwinder.universalgcodesender.model.Position) PointSegment(com.willwinder.universalgcodesender.types.PointSegment) PlaneFormatter(com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)

Example 5 with Position

use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.

the class GcodePreprocessorUtils method convertRToCenter.

/**
 * Helper method for to convert IJK syntax to center point.
 * @return the center of rotation between two points with IJK codes.
 */
private static Position convertRToCenter(Position start, Position end, double radius, boolean absoluteIJK, boolean clockwise, PlaneFormatter plane) {
    double R = radius;
    Position center = new Position();
    // This math is copied from GRBL in gcode.c
    double x = plane.axis0(end) - plane.axis0(start);
    double y = plane.axis1(end) - plane.axis1(start);
    double h_x2_div_d = 4 * R * R - x * x - y * y;
    // if (h_x2_div_d < 0) { System.out.println("Error computing arc radius."); }
    h_x2_div_d = (-Math.sqrt(h_x2_div_d)) / Math.hypot(x, y);
    if (!clockwise) {
        h_x2_div_d = -h_x2_div_d;
    }
    // should be used.
    if (R < 0) {
        h_x2_div_d = -h_x2_div_d;
        // TODO: Places that use this need to run ABS on radius.
        radius = -radius;
    }
    double offsetX = 0.5 * (x - (y * h_x2_div_d));
    double offsetY = 0.5 * (y + (x * h_x2_div_d));
    if (!absoluteIJK) {
        plane.setAxis0(center, plane.axis0(start) + offsetX);
        plane.setAxis1(center, plane.axis1(start) + offsetY);
    } else {
        plane.setAxis0(center, offsetX);
        plane.setAxis1(center, offsetY);
    }
    return center;
}
Also used : Position(com.willwinder.universalgcodesender.model.Position)

Aggregations

Position (com.willwinder.universalgcodesender.model.Position)66 Test (org.junit.Test)33 GcodeState (com.willwinder.universalgcodesender.gcode.GcodeState)15 GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)7 ControllerStatus (com.willwinder.universalgcodesender.listeners.ControllerStatus)6 UnitUtils (com.willwinder.universalgcodesender.model.UnitUtils)6 GcodeParserException (com.willwinder.universalgcodesender.gcode.util.GcodeParserException)5 Units (com.willwinder.universalgcodesender.model.UnitUtils.Units)5 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)4 Settings (com.willwinder.universalgcodesender.utils.Settings)4 ArrayList (java.util.ArrayList)4 ProbeParameters (com.willwinder.ugs.platform.probe.ProbeService.ProbeParameters)3 GcodeParser (com.willwinder.universalgcodesender.gcode.GcodeParser)3 PlaneFormatter (com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)3 UGSEvent (com.willwinder.universalgcodesender.model.UGSEvent)3 AutoLevelSettings (com.willwinder.universalgcodesender.utils.Settings.AutoLevelSettings)3 SplitCommand (com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand)2 CommentProcessor (com.willwinder.universalgcodesender.gcode.processors.CommentProcessor)2 Code (com.willwinder.universalgcodesender.gcode.util.Code)2 PointSegment (com.willwinder.universalgcodesender.types.PointSegment)2