Search in sources :

Example 1 with UNKNOWN

use of com.willwinder.universalgcodesender.gcode.util.Code.UNKNOWN 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)

Aggregations

Iterables (com.google.common.collect.Iterables)1 CommandProcessor (com.willwinder.universalgcodesender.gcode.processors.CommandProcessor)1 Stats (com.willwinder.universalgcodesender.gcode.processors.Stats)1 Code (com.willwinder.universalgcodesender.gcode.util.Code)1 Motion (com.willwinder.universalgcodesender.gcode.util.Code.ModalGroup.Motion)1 UNKNOWN (com.willwinder.universalgcodesender.gcode.util.Code.UNKNOWN)1 GcodeParserException (com.willwinder.universalgcodesender.gcode.util.GcodeParserException)1 Plane (com.willwinder.universalgcodesender.gcode.util.Plane)1 PlaneFormatter (com.willwinder.universalgcodesender.gcode.util.PlaneFormatter)1 Localization (com.willwinder.universalgcodesender.i18n.Localization)1 Position (com.willwinder.universalgcodesender.model.Position)1 UnitUtils (com.willwinder.universalgcodesender.model.UnitUtils)1 PointSegment (com.willwinder.universalgcodesender.types.PointSegment)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