Skip to content

Commit

Permalink
Bugfix for handling gcode state G38 (#2422)
Browse files Browse the repository at this point in the history
* When using TCP it will block while establishing the connection and will not display the connection status or message until after it has done a TCP handshake. Added fix to make sure the status and message is dispatched before the connection attempt.
* Allow a G38 command without axis to be able to set the initial parser state
  • Loading branch information
breiler authored Jan 13, 2024
1 parent 1f5b917 commit bdecfec
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,32 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.universalgcodesender;

import com.willwinder.universalgcodesender.communicator.ICommunicatorListener;
import static com.willwinder.universalgcodesender.Utils.formatter;
import com.willwinder.universalgcodesender.communicator.ICommunicator;
import com.willwinder.universalgcodesender.communicator.ICommunicatorListener;
import com.willwinder.universalgcodesender.connection.ConnectionDriver;
import com.willwinder.universalgcodesender.gcode.GcodeParser;
import com.willwinder.universalgcodesender.gcode.GcodeState;
import com.willwinder.universalgcodesender.gcode.ICommandCreator;
import com.willwinder.universalgcodesender.gcode.util.GcodeUtils;
import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.listeners.*;
import com.willwinder.universalgcodesender.model.*;
import com.willwinder.universalgcodesender.listeners.ControllerListener;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.listeners.ControllerStatus;
import com.willwinder.universalgcodesender.listeners.MessageType;
import com.willwinder.universalgcodesender.model.Alarm;
import com.willwinder.universalgcodesender.model.Axis;
import com.willwinder.universalgcodesender.model.CommunicatorState;
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_CHECK;
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_DISCONNECTED;
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_IDLE;
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_SENDING;
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_SENDING_PAUSED;
import com.willwinder.universalgcodesender.model.PartialPosition;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.UnitUtils;
import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;
import static com.willwinder.universalgcodesender.model.UnitUtils.scaleUnits;
import com.willwinder.universalgcodesender.services.MessageService;
import com.willwinder.universalgcodesender.types.GcodeCommand;
import com.willwinder.universalgcodesender.utils.IGcodeStreamReader;
Expand All @@ -46,11 +62,6 @@ This file is part of Universal Gcode Sender (UGS).
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.willwinder.universalgcodesender.Utils.formatter;
import static com.willwinder.universalgcodesender.model.CommunicatorState.*;
import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;
import static com.willwinder.universalgcodesender.model.UnitUtils.scaleUnits;

/**
* Abstract Control layer, coordinates all aspects of control.
*
Expand Down Expand Up @@ -344,9 +355,9 @@ public Boolean openCommPort(ConnectionDriver connectionDriver, String port, int
}

// No point in checking response, it throws an exception on errors.
this.comm.connect(connectionDriver, port, portRate);
this.setCurrentState(COMM_IDLE);
this.setControllerState(ControllerState.CONNECTING);
this.comm.connect(connectionDriver, port, portRate);

if (isCommOpen()) {
dispatchConsoleMessage(MessageType.INFO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ public Boolean openCommPort(ConnectionDriver connectionDriver, String port, int

isInitialized = false;
positionPollTimer.stop();
communicator.connect(connectionDriver, port, portRate);
setControllerState(ControllerState.CONNECTING);
messageService.dispatchMessage(MessageType.INFO, "*** Connecting to " + connectionDriver.getProtocol() + port + ":" + portRate + "\n");
communicator.connect(connectionDriver, port, portRate);

ThreadHelper.invokeLater(() -> {
if (StringUtils.isEmpty(firmwareVariant) || semanticVersion == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2017 Will Winder
Copyright 2017-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -53,10 +53,10 @@ public enum Code {
G2(Motion, false, true),
G3(Motion, false, true),
G33(Motion),
G38_2(Motion),
G38_3(Motion),
G38_4(Motion),
G38_5(Motion),
G38_2(Motion, false, true),
G38_3(Motion, false, true),
G38_4(Motion, false, true),
G38_5(Motion, false, true),
G73(Motion),
G76(Motion),
G80(Motion, false, true),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2017-2023 Will Winder
Copyright 2017-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -23,6 +23,14 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.gcode.GcodeParser;
import com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils;
import com.willwinder.universalgcodesender.gcode.GcodeState;
import static com.willwinder.universalgcodesender.gcode.util.Code.G20;
import static com.willwinder.universalgcodesender.gcode.util.Code.G21;
import static com.willwinder.universalgcodesender.gcode.util.Code.G90;
import static com.willwinder.universalgcodesender.gcode.util.Code.G90_1;
import static com.willwinder.universalgcodesender.gcode.util.Code.G91;
import static com.willwinder.universalgcodesender.gcode.util.Code.G91_1;
import static com.willwinder.universalgcodesender.gcode.util.Code.ModalGroup.Motion;
import static com.willwinder.universalgcodesender.gcode.util.Code.UNKNOWN;
import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.UnitUtils;
Expand All @@ -49,17 +57,7 @@ This file is part of Universal Gcode Sender (UGS).
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static com.willwinder.universalgcodesender.gcode.util.Code.G20;
import static com.willwinder.universalgcodesender.gcode.util.Code.G21;
import static com.willwinder.universalgcodesender.gcode.util.Code.G90;
import static com.willwinder.universalgcodesender.gcode.util.Code.G90_1;
import static com.willwinder.universalgcodesender.gcode.util.Code.G91;
import static com.willwinder.universalgcodesender.gcode.util.Code.G91_1;
import static com.willwinder.universalgcodesender.gcode.util.Code.ModalGroup.Motion;
import static com.willwinder.universalgcodesender.gcode.util.Code.UNKNOWN;

/**
*
* @author wwinder
*/
public class GcodeParserUtils {
Expand Down Expand Up @@ -174,6 +172,10 @@ public static List<GcodeParser.GcodeMeta> processCommand(String command, int lin
}

private static PointSegment addProbePointSegment(Position nextPoint, boolean fastTraverse, int line, GcodeState state) {
if (nextPoint == null) {
return null;
}

PointSegment ps = addLinearPointSegment(nextPoint, fastTraverse, line, state);
ps.setIsProbe(true);
return ps;
Expand Down Expand Up @@ -391,7 +393,7 @@ private static void preprocessAndWrite(GcodeParser gcp, IGcodeWriter gsw, String
// Parse the gcode for the buffer.
Collection<String> lines = gcp.preprocessCommand(command, gcp.getCurrentState());

for(String processedLine : lines) {
for (String processedLine : lines) {
gsw.addLine(command, processedLine, comment, idx);
}

Expand All @@ -400,6 +402,7 @@ private static void preprocessAndWrite(GcodeParser gcp, IGcodeWriter gsw, String

/**
* Attempts to read the input file in GcodeStream format.
*
* @return whether or not we succeed processing the file.
*/
private static boolean processAndExportGcodeStream(GcodeParser gcp, InputStream input, IGcodeWriter output)
Expand All @@ -424,14 +427,15 @@ private static boolean processAndExportGcodeStream(GcodeParser gcp, InputStream

/**
* Attempts to read the input file in gcode-text format.
*
* @return whether or not we succeed processing the file.
*/
private static void processAndExportText(GcodeParser gcp, BufferedReader input, IGcodeWriter output)
throws IOException, GcodeParserException {
// Preprocess a regular gcode file.
try(BufferedReader br = input) {
try (BufferedReader br = input) {
int i = 0;
for(String line; (line = br.readLine()) != null; ) {
for (String line; (line = br.readLine()) != null; ) {
i++;

String comment = GcodePreprocessorUtils.parseComment(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import com.google.common.collect.Iterables;
import com.willwinder.universalgcodesender.gcode.GcodeParser;
import com.willwinder.universalgcodesender.gcode.GcodeState;
import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.model.Position;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

import static com.willwinder.universalgcodesender.gcode.util.Code.G0;
import static com.willwinder.universalgcodesender.gcode.util.Code.G1;
import static com.willwinder.universalgcodesender.gcode.util.Code.G3;
import static com.willwinder.universalgcodesender.gcode.util.Code.G38_2;
import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.model.Position;
import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class GcodeParserUtilsTest {
@Test
Expand All @@ -36,10 +36,11 @@ public void multipleAxisWordCommands() {
}

@Test
public void missingAxisWords() {
assertThatThrownBy(() -> GcodeParserUtils.processCommand("G38.2", 0, new GcodeState()))
.isInstanceOf(GcodeParserException.class)
.hasMessage(Localization.getString("parser.gcode.missing-axis-commands") + ": G38.2");
public void missingAxisWords() throws GcodeParserException {
List<GcodeParser.GcodeMeta> metaList = GcodeParserUtils.processCommand("G38.2", 0, new GcodeState());
GcodeParser.GcodeMeta meta = Iterables.getOnlyElement(metaList);
assertThat(meta.code).isEqualTo(G38_2);
assertThat(meta.state.currentPoint).isEqualTo(new Position(0, 0, 0, MM));
}

@Test
Expand Down

0 comments on commit bdecfec

Please sign in to comment.