Search in sources :

Example 46 with GcodeCommand

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

the class GrblControllerTest method testPauseAndCancelSend.

@Test
public void testPauseAndCancelSend() throws Exception {
    System.out.println("Pause + cancelSend");
    GrblController instance = new GrblController(mgc);
    setState(instance, COMM_SENDING);
    instance.openCommPort("blah", 1234);
    // Test 1.1 cancel throws an exception (Grbl 0.7).
    instance.rawResponseHandler("Grbl 0.7");
    boolean threwException = false;
    try {
        instance.pauseStreaming();
        instance.cancelSend();
    } catch (Exception ex) {
        threwException = true;
    }
    assertTrue(threwException);
    assertEquals(0, mgc.numCancelSendCalls);
    instance.resumeStreaming();
    // Test 1.2 Cancel when nothing is running (Grbl 0.8c).
    // Check for soft reset.
    instance.rawResponseHandler("Grbl 0.8c");
    instance.pauseStreaming();
    instance.cancelSend();
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    assertEquals(1, mgc.numCancelSendCalls);
    assertEquals(1, mgc.numSoftResetCalls);
    instance.resumeStreaming();
    // Test 2.1
    // Add 30 commands, start send, cancel before any sending. (Grbl 0.7)
    instance.rawResponseHandler("Grbl 0.7");
    for (int i = 0; i < 30; i++) {
        instance.queueCommand(instance.createCommand("G0X" + i));
    }
    try {
        instance.beginStreaming();
    } catch (Exception ex) {
        fail("Unexpected exception from GrblController: " + ex.getMessage());
    }
    instance.pauseStreaming();
    boolean exceptionThrown = false;
    try {
        instance.cancelSend();
    } catch (Exception e) {
        exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
    assertEquals(30, instance.rowsInSend());
    // Note: It is hoped that one day cancel will proactively clear out the
    // in progress commands. But that day is not today.
    assertEquals(30, instance.rowsRemaining());
    instance.resumeStreaming();
    instance.cancelSend();
    // Test 2.2
    // Add 30 commands, start send, cancel before any sending. (Grbl 0.8c)
    // setUp();
    // instance = new GrblController(mgc);
    instance.rawResponseHandler("Grbl 0.8c");
    for (int i = 0; i < 30; i++) {
        instance.queueCommand(instance.createCommand("G0X" + i));
    }
    try {
        instance.beginStreaming();
    } catch (Exception ex) {
        fail("Unexpected exception from GrblController: " + ex.getMessage());
    }
    instance.pauseStreaming();
    instance.cancelSend();
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    assertEquals(0, instance.rowsInSend());
    assertEquals(0, instance.rowsRemaining());
    instance.resumeStreaming();
    // Test 3.1 - N/A, exception thrown.
    // Test 3.2
    // Add 30 commands, start send, cancel after sending 15. (Grbl 0.8c)
    instance.rawResponseHandler("Grbl 0.8c");
    for (int i = 0; i < 30; i++) {
        instance.queueCommand(instance.createCommand("G0X" + i));
    }
    try {
        instance.beginStreaming();
        for (int i = 0; i < 15; i++) {
            GcodeCommand command = new GcodeCommand("G0 X1");
            command.setSent(true);
            command.setResponse("ok");
            instance.commandSent(command);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception from command sent: " + ex.getMessage());
    }
    instance.pauseStreaming();
    instance.cancelSend();
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    assertEquals(15, instance.rowsSent());
    assertEquals(0, instance.rowsInSend());
    // Left this failing because it should be possible to make it work
    // this way someday.
    assertEquals(0, instance.rowsRemaining());
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 47 with GcodeCommand

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

the class GrblControllerTest method rawResponseHandlerWithKnownErrorShouldWriteMessageToConsole.

@Test
public void rawResponseHandlerWithKnownErrorShouldWriteMessageToConsole() throws Exception {
    // Given
    GrblController instance = new GrblController(mgc);
    instance.setDistanceModeCode("G90");
    instance.setUnitsCode("G21");
    instance.openCommPort("foo", 2400);
    instance.commandSent(new GcodeCommand("G0"));
    ControllerListener controllerListener = mock(ControllerListener.class);
    instance.addListener(controllerListener);
    // When
    instance.rawResponseHandler("error:1");
    // Then
    String genericErrorMessage = "Error while processing response <error:1>\n";
    verify(controllerListener, times(0)).messageForConsole(ControllerListener.MessageType.ERROR, genericErrorMessage);
    String errorMessage = "An error was detected while sending 'G0': (error:1) G-code words consist of a letter and a value. Letter was not found. Streaming has been paused.\n";
    verify(controllerListener).messageForConsole(ControllerListener.MessageType.ERROR, errorMessage);
    verify(controllerListener, times(1)).messageForConsole(any(), anyString());
    instance.removeListener(controllerListener);
    assertFalse(instance.getActiveCommand().isPresent());
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) ControllerListener(com.willwinder.universalgcodesender.listeners.ControllerListener) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 48 with GcodeCommand

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

the class GrblControllerTest method rawResponseHandlerWithUnknownErrorShouldWriteGenericMessageToConsole.

@Test
public void rawResponseHandlerWithUnknownErrorShouldWriteGenericMessageToConsole() throws Exception {
    // Given
    GrblController instance = new GrblController(mgc);
    instance.setDistanceModeCode("G90");
    instance.setUnitsCode("G21");
    instance.openCommPort("foo", 2400);
    instance.commandSent(new GcodeCommand("G21"));
    ControllerListener controllerListener = mock(ControllerListener.class);
    instance.addListener(controllerListener);
    // When
    instance.rawResponseHandler("error:18");
    // Then
    String genericErrorMessage = "An error was detected while sending 'G21': (error:18) An unknown error has occurred. Streaming has been paused.\n";
    verify(controllerListener, times(1)).messageForConsole(ControllerListener.MessageType.ERROR, genericErrorMessage);
    verify(controllerListener, times(1)).messageForConsole(any(), anyString());
    instance.removeListener(controllerListener);
    assertFalse(instance.getActiveCommand().isPresent());
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) ControllerListener(com.willwinder.universalgcodesender.listeners.ControllerListener) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 49 with GcodeCommand

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

the class GcodeParserTest method autoLevelerProcessorSet.

@Test
public void autoLevelerProcessorSet() throws Exception {
    System.out.println("autoLevelerProcessorSet");
    GcodeParser gcp = new GcodeParser();
    gcp.addCommandProcessor(new CommentProcessor());
    gcp.addCommandProcessor(new ArcExpander(true, 0.1));
    gcp.addCommandProcessor(new LineSplitter(1));
    Position[][] grid = { { new Position(-5, -5, 0, MM), new Position(-5, 35, 0, MM) }, { new Position(35, -5, 0, MM), new Position(35, 35, 0, MM) } };
    gcp.addCommandProcessor(new MeshLeveler(0, grid, Units.MM));
    Path output = Files.createTempFile("autoleveler_processor_set_test.nc", "");
    // Copy resource to temp file since my parser methods need it that way.
    URL file = this.getClass().getClassLoader().getResource("./gcode/circle_test.nc");
    File tempFile = File.createTempFile("temp", "file");
    IOUtils.copy(file.openStream(), FileUtils.openOutputStream(tempFile));
    GcodeParserUtils.processAndExport(gcp, tempFile, output.toFile());
    GcodeStreamReader reader = new GcodeStreamReader(output.toFile());
    file = this.getClass().getClassLoader().getResource("./gcode/circle_test.nc.processed");
    Files.lines(Paths.get(file.toURI())).forEach((t) -> {
        try {
            GcodeCommand c = reader.getNextCommand();
            if (c == null) {
                Assert.fail("Reached end of gcode reader before end of expected commands.");
            }
            Assert.assertEquals(c.getCommandString(), t);
        } catch (IOException ex) {
            Assert.fail("Unexpected exception.");
        }
    });
    assertEquals(1027, reader.getNumRows());
    output.toFile().delete();
}
Also used : Path(java.nio.file.Path) Position(com.willwinder.universalgcodesender.model.Position) LineSplitter(com.willwinder.universalgcodesender.gcode.processors.LineSplitter) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException) URL(java.net.URL) CommentProcessor(com.willwinder.universalgcodesender.gcode.processors.CommentProcessor) MeshLeveler(com.willwinder.universalgcodesender.gcode.processors.MeshLeveler) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) ArcExpander(com.willwinder.universalgcodesender.gcode.processors.ArcExpander) File(java.io.File) Test(org.junit.Test)

Example 50 with GcodeCommand

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

the class GcodeTilerTopComponent method generateOneTile.

private void generateOneTile(double offsetX, double offsetY, PrintWriter output) {
    String gcodeFile = backend.getGcodeFile().getAbsolutePath();
    UnitUtils.Units u = selectedUnit(this.units.getSelectedIndex());
    GcodeParser parser = new GcodeParser();
    parser.addCommandProcessor(new Translator(new Position(offsetX, offsetY, 0.0, u)));
    parser.addCommandProcessor(new M30Processor());
    output.println(GcodeUtils.unitCommand(u) + "G90");
    output.println("G0X" + offsetX + "Y" + offsetY);
    try {
        File file = new File(gcodeFile);
        try {
            try (GcodeStreamReader gsr = new GcodeStreamReader(file)) {
                while (gsr.getNumRowsRemaining() > 0) {
                    GcodeCommand next = gsr.getNextCommand();
                    applyTranslation(next.getCommandString(), parser, output);
                }
            }
        } catch (GcodeStreamReader.NotGcodeStreamFile e) {
            try (FileInputStream fstream = new FileInputStream(file);
                DataInputStream dis = new DataInputStream(fstream);
                BufferedReader fileStream = new BufferedReader(new InputStreamReader(dis))) {
                String line;
                while ((line = fileStream.readLine()) != null) {
                    applyTranslation(line, parser, output);
                }
            }
        }
    } catch (GcodeParserException | IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Position(com.willwinder.universalgcodesender.model.Position) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) UnitUtils(com.willwinder.universalgcodesender.model.UnitUtils) Translator(com.willwinder.universalgcodesender.gcode.processors.Translator) BufferedReader(java.io.BufferedReader) GcodeStreamReader(com.willwinder.universalgcodesender.utils.GcodeStreamReader) M30Processor(com.willwinder.universalgcodesender.gcode.processors.M30Processor) GcodeParserException(com.willwinder.universalgcodesender.gcode.util.GcodeParserException) File(java.io.File)

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