Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display the pin status immediately #2343

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 27 additions & 45 deletions ugs-core/src/com/willwinder/universalgcodesender/GrblUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2022 Will Winder
Copyright 2012-2023 Will Winder

This file is part of Universal Gcode Sender (UGS).

Expand Down Expand Up @@ -29,8 +29,6 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.listeners.MessageType;
import com.willwinder.universalgcodesender.model.*;
import com.willwinder.universalgcodesender.model.UnitUtils.Units;
import com.willwinder.universalgcodesender.services.MessageService;
import com.willwinder.universalgcodesender.types.GcodeCommand;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
Expand Down Expand Up @@ -73,7 +71,7 @@ public class GrblUtils {
public static final String GCODE_RESET_COORDINATES_TO_ZERO_V8 = "G92 X0 Y0 Z0";

/**
* For setting a the coordinate to a specific position on an axis.
* For setting a coordinate to a specific position on an axis.
* First string parameter should be either X, Y or Z. The second parameter should be a floating point number in
* the format 0.000
*/
Expand Down Expand Up @@ -275,27 +273,27 @@ static protected Position parseProbePosition(final String response, final Units
*/
private static final String STATUS_REGEX = "<.*>";
private static final Pattern STATUS_PATTERN = Pattern.compile(STATUS_REGEX);
public static Boolean isGrblStatusString(final String response) {
public static boolean isGrblStatusString(final String response) {
return STATUS_PATTERN.matcher(response).find();
}

private static final String PROBE_REGEX = "\\[PRB:.*]";
private static final Pattern PROBE_PATTERN = Pattern.compile(PROBE_REGEX);
static protected Boolean isGrblProbeMessage(final String response) {
static protected boolean isGrblProbeMessage(final String response) {
return PROBE_PATTERN.matcher(response).find();
}

private static final String FEEDBACK_REGEX = "\\[.*]";
private static final Pattern FEEDBACK_PATTERN = Pattern.compile(FEEDBACK_REGEX);
public static Boolean isGrblFeedbackMessage(final String response, Capabilities c) {
public static boolean isGrblFeedbackMessage(final String response, Capabilities c) {
if (c.hasCapability(GrblCapabilitiesConstants.V1_FORMAT)) {
return isGrblFeedbackMessageV1(response);
} else {
return FEEDBACK_PATTERN.matcher(response).find();
}
}

public static Boolean isGrblFeedbackMessageV1(final String response) {
public static boolean isGrblFeedbackMessageV1(final String response) {
return response.startsWith("[GC:");
}

Expand All @@ -313,7 +311,7 @@ public static String parseFeedbackMessageV1(final String response) {

private static final String SETTING_REGEX = "\\$\\d+=.+";
private static final Pattern SETTING_PATTERN = Pattern.compile(SETTING_REGEX);
static protected Boolean isGrblSettingMessage(final String response) {
static protected boolean isGrblSettingMessage(final String response) {
return SETTING_PATTERN.matcher(response).find();
}

Expand Down Expand Up @@ -371,12 +369,12 @@ public static ControllerStatus getStatusFromStatusStringV1(ControllerStatus last
Position WCO = null;

OverridePercents overrides = null;
EnabledPins pins = null;
EnabledPins pins = EnabledPins.EMPTY_PINS;
AccessoryStates accessoryStates = null;

double feedSpeed = 0;
double spindleSpeed = 0;
if(lastStatus != null) {
if (lastStatus != null) {
feedSpeed = lastStatus.getFeedSpeed();
spindleSpeed = lastStatus.getSpindleSpeed();
}
Expand Down Expand Up @@ -447,19 +445,12 @@ else if (part.startsWith("A:")) {

if (!isOverrideReport && lastStatus != null) {
overrides = lastStatus.getOverrides();
pins = lastStatus.getEnabledPins();
accessoryStates = lastStatus.getAccessoryStates();
}
else if (isOverrideReport) {
// If this is an override report and the 'Pn:' field wasn't sent
// set all pins to a disabled state.
if (pins == null) {
pins = new EnabledPins("");
}
// Likewise for accessory states.
if (accessoryStates == null) {
accessoryStates = new AccessoryStates("");
}

if (accessoryStates == null && !isOverrideReport && lastStatus != null) {
accessoryStates = lastStatus.getAccessoryStates();
} else if (accessoryStates == null) {
accessoryStates = AccessoryStates.EMPTY_ACCESSORY_STATE;
}

ControllerState state = getControllerStateFromStateString(stateString);
Expand Down Expand Up @@ -514,28 +505,19 @@ public static boolean isGrblStatusStringV1(String response) {
}

public static ControllerState getControllerStateFromStateString(String stateString) {
switch (stateString.toLowerCase()) {
case "jog":
return ControllerState.JOG;
case "run":
return ControllerState.RUN;
case "hold":
return ControllerState.HOLD;
case "door":
return ControllerState.DOOR;
case "home":
return ControllerState.HOME;
case "idle":
return ControllerState.IDLE;
case "alarm":
return ControllerState.ALARM;
case "check":
return ControllerState.CHECK;
case "sleep":
return ControllerState.SLEEP;
default:
return ControllerState.DISCONNECTED;
}
return switch (stateString.toLowerCase()) {
case "jog" -> ControllerState.JOG;
case "run" -> ControllerState.RUN;
case "hold" -> ControllerState.HOLD;
case "door" -> ControllerState.DOOR;
case "home" -> ControllerState.HOME;
case "idle" -> ControllerState.IDLE;
case "alarm" -> ControllerState.ALARM;
case "check" -> ControllerState.CHECK;
case "sleep" -> ControllerState.SLEEP;
case "tool" -> ControllerState.TOOL;
default -> ControllerState.UNKNOWN;
};
}

// Optionally look for 6 axes (ABC support as extended by Grbl ESP 32)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 Will Winder
Copyright 2018-2023 Will Winder

This file is part of Universal Gcode Sender (UGS).

Expand Down Expand Up @@ -82,6 +82,11 @@ public enum ControllerState {
*/
CONNECTING,

/**
* When the controller has entered a tool change mode
*/
TOOL,

/**
* When the machine is in an unknown state
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2016-2021 Will Winder
Copyright 2016-2023 Will Winder

This file is part of Universal Gcode Sender (UGS).

Expand Down Expand Up @@ -140,6 +140,8 @@ public String getSubState() {
}

public static class EnabledPins {
public static final EnabledPins EMPTY_PINS = new EnabledPins("");

final public boolean X;
final public boolean Y;
final public boolean Z;
Expand Down Expand Up @@ -179,6 +181,7 @@ public int hashCode() {
}

public static class AccessoryStates {
public static final AccessoryStates EMPTY_ACCESSORY_STATE = new AccessoryStates("");
final public boolean SpindleCW;
final public boolean SpindleCCW;
final public boolean Flood;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,68 @@ public void getStatusFromStatusStringShouldReturnAlarmState() {
assertEquals(ControllerState.ALARM, controllerStatus.getState());
}

@Test
public void getStatusFromStatusStringV1ShouldReturnPinStatus() {
Capabilities version = new Capabilities();
version.addCapability(GrblCapabilitiesConstants.REAL_TIME);

String status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0>";
ControllerStatus controllerStatus = GrblUtils.getStatusFromStatusStringV1(null, status, MM);
assertFalse(controllerStatus.getEnabledPins().CycleStart);

status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0|Pn:S>";
controllerStatus = GrblUtils.getStatusFromStatusStringV1(null, status, MM);
assertTrue(controllerStatus.getEnabledPins().CycleStart);

status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0>";
controllerStatus = GrblUtils.getStatusFromStatusStringV1(controllerStatus, status, MM);
assertFalse(controllerStatus.getEnabledPins().CycleStart);
}

@Test
public void getStatusFromStatusStringV1ShouldReturnAccessoryStates() {
Capabilities version = new Capabilities();
version.addCapability(GrblCapabilitiesConstants.REAL_TIME);

String status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0>";
ControllerStatus controllerStatus = GrblUtils.getStatusFromStatusStringV1(null, status, MM);
assertFalse(controllerStatus.getAccessoryStates().Flood);

status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0|Ov:100,100,100|A:F>";
controllerStatus = GrblUtils.getStatusFromStatusStringV1(controllerStatus, status, MM);
assertTrue(controllerStatus.getAccessoryStates().Flood);

status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0>";
controllerStatus = GrblUtils.getStatusFromStatusStringV1(controllerStatus, status, MM);
assertTrue("The accessory states should be retained even if it isn't included in the report", controllerStatus.getAccessoryStates().Flood);

status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0|Ov:100,100,100>";
controllerStatus = GrblUtils.getStatusFromStatusStringV1(controllerStatus, status, MM);
assertFalse("The accessory states should be set to disabled if not included in overrides report", controllerStatus.getAccessoryStates().Flood);

status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0|A:F>";
controllerStatus = GrblUtils.getStatusFromStatusStringV1(controllerStatus, status, MM);
assertTrue("The accessory state should be set even if not a overrides report", controllerStatus.getAccessoryStates().Flood);
}

@Test
public void getStatusFromStatusStringV1ShouldReturnState() {
Capabilities version = new Capabilities();
version.addCapability(GrblCapabilitiesConstants.REAL_TIME);

String status = "<Idle|MPos:0.000,0.000,0.000|FS:0,0>";
ControllerStatus controllerStatus = GrblUtils.getStatusFromStatusStringV1(null, status, MM);
assertEquals(ControllerState.IDLE, controllerStatus.getState());

status = "<Test|MPos:0.000,0.000,0.000|FS:0,0>";
controllerStatus = GrblUtils.getStatusFromStatusStringV1(null, status, MM);
assertEquals(ControllerState.UNKNOWN, controllerStatus.getState());

status = "<Tool|MPos:0.000,0.000,0.000|FS:0,0>";
controllerStatus = GrblUtils.getStatusFromStatusStringV1(null, status, MM);
assertEquals(ControllerState.TOOL, controllerStatus.getState());
}

/**
* Test of getWorkPositionFromStatusString method, of class GrblUtils.
*/
Expand Down
Loading