Search in sources :

Example 1 with Code

use of com.willwinder.universalgcodesender.gcode.util.Code in project Universal-G-Code-Sender by winder.

the class GcodeParser method processCommand.

/**
 * Process commend given an initial state. This method will not modify its
 * input parameters.
 *
 * @param includeNonMotionStates Create gcode meta responses even if there is no motion, for example "F100" will not
 * return a GcodeMeta entry unless this flag is set to true.
 */
public static List<GcodeMeta> processCommand(String command, int line, final GcodeState inputState, boolean includeNonMotionStates) throws GcodeParserException {
    List<String> args = GcodePreprocessorUtils.splitCommand(command);
    if (args.isEmpty())
        return null;
    // Initialize with original state
    GcodeState state = inputState.copy();
    state.commandNumber = line;
    // handle M codes.
    // codes = GcodePreprocessorUtils.parseCodes(args, 'M');
    // handleMCode(for each codes);
    List<String> fCodes = GcodePreprocessorUtils.parseCodes(args, 'F');
    if (!fCodes.isEmpty()) {
        try {
            state.speed = Double.parseDouble(Iterables.getOnlyElement(fCodes));
        } catch (IllegalArgumentException e) {
            throw new GcodeParserException("Multiple F-codes on one line.");
        }
    }
    List<String> sCodes = GcodePreprocessorUtils.parseCodes(args, 'S');
    if (!sCodes.isEmpty()) {
        try {
            state.spindleSpeed = Double.parseDouble(Iterables.getOnlyElement(sCodes));
        } catch (IllegalArgumentException e) {
            throw new GcodeParserException("Multiple S-codes on one line.");
        }
    }
    // Gather G codes.
    Set<Code> gCodes = GcodePreprocessorUtils.getGCodes(args);
    boolean hasAxisWords = GcodePreprocessorUtils.hasAxisWords(args);
    // Error to mix group 1 (Motion) and certain group 0 (NonModal) codes (G10, G28, G30, G92)
    Collection<Code> motionCodes = gCodes.stream().filter(c -> c.consumesMotion()).collect(Collectors.toList());
    // 1 motion code per line.
    if (motionCodes.size() > 1) {
        throw new GcodeParserException(Localization.getString("parser.gcode.multiple-axis-commands") + ": " + StringUtils.join(motionCodes, ", "));
    }
    // If there are axis words and nothing to use them, add the currentMotionMode.
    if (hasAxisWords && motionCodes.isEmpty() && state.currentMotionMode != null) {
        gCodes.add(state.currentMotionMode);
    }
    // Apply each code to the state.
    List<GcodeMeta> results = new ArrayList<>();
    for (Code i : gCodes) {
        if (i == UNKNOWN) {
            logger.warning("An unknown gcode command was detected in: " + command);
        } else {
            GcodeMeta meta = handleGCode(i, args, line, state, hasAxisWords);
            meta.command = command;
            // Commands like 'G21' don't return a point segment.
            if (meta.point != null) {
                meta.point.setSpeed(state.speed);
            }
            results.add(meta);
        }
    }
    // Return updated state / command.
    if (results.isEmpty() && includeNonMotionStates) {
        GcodeMeta meta = new GcodeMeta();
        meta.state = state;
        meta.command = command;
        meta.code = state.currentMotionMode;
        return Collections.singletonList(meta);
    }
    return results;
}
Also used : Plane(com.willwinder.universalgcodesender.gcode.util.Plane) UnitUtils(com.willwinder.universalgcodesender.model.UnitUtils) Iterables(com.google.common.collect.Iterables) UNKNOWN(com.willwinder.universalgcodesender.gcode.util.Code.UNKNOWN) java.util(java.util) Stats(com.willwinder.universalgcodesender.gcode.processors.Stats) Code(com.willwinder.universalgcodesender.gcode.util.Code) Position(com.willwinder.universalgcodesender.model.Position) Motion(com.willwinder.universalgcodesender.gcode.util.Code.ModalGroup.Motion) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) CommandProcessor(com.willwinder.universalgcodesender.gcode.processors.CommandProcessor) PointSegment(com.willwinder.universalgcodesender.types.PointSegment) Localization(com.willwinder.universalgcodesender.i18n.Localization) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) PlaneFormatter(com.willwinder.universalgcodesender.gcode.util.PlaneFormatter) Code(com.willwinder.universalgcodesender.gcode.util.Code) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException)

Example 2 with Code

use of com.willwinder.universalgcodesender.gcode.util.Code in project Universal-G-Code-Sender by winder.

the class GcodePreprocessorUtils method extractMotion.

/**
 * Return extracted motion words and remainder words.
 * If the code is G0 or G1 and G53 is found, it will also be extracted:
 * http://linuxcnc.org/docs/html/gcode/g-code.html#gcode:g53
 */
public static SplitCommand extractMotion(Code code, String command) {
    List<String> args = splitCommand(command);
    if (args.isEmpty())
        return null;
    StringBuilder extracted = new StringBuilder();
    StringBuilder remainder = new StringBuilder();
    boolean includeG53 = code == G0 || code == G1;
    for (String arg : args) {
        char c = arg.charAt(0);
        Code lookup = Code.lookupCode(arg);
        if (lookup.getType() == Motion && lookup != code)
            return null;
        if (lookup == code || isMotionWord(c) || (includeG53 && lookup == G53)) {
            extracted.append(arg);
        } else {
            remainder.append(arg);
        }
    }
    if (extracted.length() == 0)
        return null;
    SplitCommand sc = new SplitCommand();
    sc.extracted = extracted.toString();
    sc.remainder = remainder.toString();
    return sc;
}
Also used : Code(com.willwinder.universalgcodesender.gcode.util.Code)

Example 3 with Code

use of com.willwinder.universalgcodesender.gcode.util.Code in project Universal-G-Code-Sender by winder.

the class LineSplitter method processCommand.

@Override
public List<String> processCommand(String commandString, GcodeState state) throws GcodeParserException {
    List<GcodeParser.GcodeMeta> commands = GcodeParser.processCommand(commandString, 0, state);
    List<String> results = new ArrayList<>();
    Code code = hasLine(commands);
    if (code == null) {
        return Collections.singletonList(commandString);
    }
    SplitCommand sc = GcodePreprocessorUtils.extractMotion(code, commandString);
    if (sc.remainder.length() > 0) {
        results.add(sc.remainder);
    }
    GcodeMeta command = Iterables.getLast(commands);
    if (command == null || command.point == null) {
        throw new GcodeParserException("Internal parser error: missing data.");
    }
    // line length
    Position start = state.currentPoint;
    Position end = command.point.point();
    Position current = start;
    double length = start.distance(end);
    // Check if line needs splitting.
    if (length > this.maxSegmentLength) {
        int numSegments = (int) Math.ceil(length / this.maxSegmentLength);
        double segmentLength = length / Math.ceil(length / this.maxSegmentLength);
        // Create line segments, stop before the last one which uses the end point.
        for (int i = 1; i < numSegments; i++) {
            double k = 1 / (length / (i * segmentLength));
            double newX = start.x + k * (end.x - start.x);
            double newY = start.y + k * (end.y - start.y);
            double newZ = start.z + k * (end.z - start.z);
            Position next = new Position(newX, newY, newZ, start.getUnits());
            results.add(GcodePreprocessorUtils.generateLineFromPoints(command.code, current, next, command.state.inAbsoluteMode, null));
            current = next;
        }
        // Add the last line point.
        results.add(GcodePreprocessorUtils.generateLineFromPoints(command.code, current, end, command.state.inAbsoluteMode, null));
    } else {
        return Collections.singletonList(commandString);
    }
    return results;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) ArrayList(java.util.ArrayList) SplitCommand(com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) Code(com.willwinder.universalgcodesender.gcode.util.Code)

Example 4 with Code

use of com.willwinder.universalgcodesender.gcode.util.Code in project Universal-G-Code-Sender by winder.

the class ArcExpander method processCommand.

@Override
public List<String> processCommand(String command, GcodeState state) throws GcodeParserException {
    if (state.currentPoint == null)
        throw new GcodeParserException(Localization.getString("parser.processor.arc.start-error"));
    List<String> results = new ArrayList<>();
    List<GcodeMeta> commands = GcodeParser.processCommand(command, 0, state);
    // If this is not an arc, there is nothing to do.
    Code c = hasArcCommand(commands);
    if (c == null) {
        return Collections.singletonList(command);
    }
    SplitCommand sc = GcodePreprocessorUtils.extractMotion(c, command);
    if (sc.remainder.length() > 0) {
        results.add(sc.remainder);
    }
    GcodeMeta arcMeta = Iterables.getLast(commands);
    PointSegment ps = arcMeta.point;
    Position start = state.currentPoint;
    Position end = arcMeta.point.point();
    List<Position> points = GcodePreprocessorUtils.generatePointsAlongArcBDring(start, end, ps.center(), ps.isClockwise(), ps.getRadius(), 0, this.length, new PlaneFormatter(ps.getPlaneState()));
    // That function returns the first and last points. Exclude the first
    // point because the previous gcode command ends there already.
    points.remove(0);
    if (convertToLines) {
        // Tack the speed onto the first line segment in case the arc also
        // changed the feed value.
        String feed = "F" + arcMeta.point.getSpeed();
        for (Position point : points) {
            results.add(GcodePreprocessorUtils.generateLineFromPoints(G1, start, point, state.inAbsoluteMode, df) + feed);
            start = point;
            feed = "";
        }
    } else {
        // TODO: Generate arc segments.
        throw new UnsupportedOperationException("I have not implemented this.");
    }
    return results;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) ArrayList(java.util.ArrayList) SplitCommand(com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand) Code(com.willwinder.universalgcodesender.gcode.util.Code) PointSegment(com.willwinder.universalgcodesender.types.PointSegment) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) PlaneFormatter(com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)

Aggregations

Code (com.willwinder.universalgcodesender.gcode.util.Code)4 GcodeParserException (com.willwinder.universalgcodesender.gcode.util.GcodeParserException)3 Position (com.willwinder.universalgcodesender.model.Position)3 GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)2 SplitCommand (com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.SplitCommand)2 PlaneFormatter (com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)2 PointSegment (com.willwinder.universalgcodesender.types.PointSegment)2 ArrayList (java.util.ArrayList)2 Iterables (com.google.common.collect.Iterables)1 CommandProcessor (com.willwinder.universalgcodesender.gcode.processors.CommandProcessor)1 Stats (com.willwinder.universalgcodesender.gcode.processors.Stats)1 Motion (com.willwinder.universalgcodesender.gcode.util.Code.ModalGroup.Motion)1 UNKNOWN (com.willwinder.universalgcodesender.gcode.util.Code.UNKNOWN)1 Plane (com.willwinder.universalgcodesender.gcode.util.Plane)1 Localization (com.willwinder.universalgcodesender.i18n.Localization)1 UnitUtils (com.willwinder.universalgcodesender.model.UnitUtils)1 java.util (java.util)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1