Skip to content

Commit

Permalink
Fix for not default origin position is 0,0,0 (winder#2423)
Browse files Browse the repository at this point in the history
This fix will make the UGS fetch the first point from the controller and display it in the visualizer instead of using a default 0,0,0.
  • Loading branch information
breiler authored Jan 15, 2024
1 parent 35c392c commit 9fba1ae
Show file tree
Hide file tree
Showing 64 changed files with 820 additions and 878 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public GcodeState() {
this.feedRate = 0;
this.spindleSpeed = 0;

this.currentPoint = new Position(0, 0, 0, Units.MM);
this.currentPoint = new Position(Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Units.MM);
}

public GcodeState copy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ This file is part of Universal Gcode Sender (UGS).
* @author wwinder
*/
public class ArcExpander implements CommandProcessor {
final private boolean convertToLines;
final private double length;
final private DecimalFormat df;
private final boolean convertToLines;
private final double length;
private final DecimalFormat df;

@Override
public String getHelp() {
Expand Down Expand Up @@ -105,6 +105,10 @@ public List<String> processCommand(String command, GcodeState state) throws Gcod
start, end, ps.center(), ps.isClockwise(),
ps.getRadius(), 0, this.length, new PlaneFormatter(ps.getPlaneState()));

if (points.isEmpty()) {
return results;
}

// That function returns the first and last points. Exclude the first
// point because the previous gcode command ends there already.
points.remove(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ This file is part of Universal Gcode Sender (UGS).

import com.google.common.collect.ImmutableList;
import com.willwinder.universalgcodesender.gcode.GcodeParser;
import static com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.normalizeCommand;
import com.willwinder.universalgcodesender.gcode.GcodeState;
import com.willwinder.universalgcodesender.gcode.util.GcodeParserException;
import com.willwinder.universalgcodesender.model.Position;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

import static com.willwinder.universalgcodesender.gcode.GcodePreprocessorUtils.normalizeCommand;

public class RunFromProcessor implements CommandProcessor {
private int lineNumber;
private GcodeParser parser;
private Double clearanceHeight = 0.0;
private Double clearanceHeight = 0.0d;

/**
* Truncates gcode to the specified line, and rewrites the preamble with the GcodeState.
Expand Down Expand Up @@ -67,11 +67,27 @@ public List<String> processCommand(String command, GcodeState state) throws Gcod
return ImmutableList.of(command);
}

private ImmutableList<String> getSkippedLinesState(String command) {
private List<String> getSkippedLinesState(String command) {
Position pos = parser.getCurrentState().currentPoint;
String moveToClearanceHeight = "G0Z" + clearanceHeight;
String moveToXY = "G0X" + pos.x + "Y" + pos.y;
String plunge = "G1Z" + pos.z;

String moveToClearanceHeight = "";
if (!Double.isNaN(pos.z)) {
moveToClearanceHeight = "G0Z" + clearanceHeight;
}

String moveToXY = "G0";
if(!Double.isNaN(pos.x)) {
moveToXY += "X" + pos.x;
}

if(!Double.isNaN(pos.y)) {
moveToXY += "Y" + pos.y;
}

String plunge = "";
if (!Double.isNaN(pos.z)) {
plunge = "G1Z" + pos.z;
}

GcodeState s = parser.getCurrentState();
String normalized = command;
Expand All @@ -98,14 +114,16 @@ private ImmutableList<String> getSkippedLinesState(String command) {

// Append normalized command
normalized
);
).stream()
.filter(line -> !StringUtils.isEmpty(line))
.toList();
}

private ImmutableList<String> skipLine(String command) throws GcodeParserException {
createParser();
parser.addCommand(command);
Position pos = parser.getCurrentState().currentPoint;
clearanceHeight = Math.max(clearanceHeight, pos.z);
clearanceHeight = Math.max(clearanceHeight, Double.isNaN(pos.z) ? 0 : pos.z);
return ImmutableList.of();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ This file is part of Universal Gcode Sender (UGS).

import com.google.common.io.Files;
import com.willwinder.universalgcodesender.IController;
import com.willwinder.universalgcodesender.gcode.ICommandCreator;
import com.willwinder.universalgcodesender.gcode.DefaultCommandCreator;
import com.willwinder.universalgcodesender.gcode.GcodeParser;
import com.willwinder.universalgcodesender.gcode.GcodeState;
import com.willwinder.universalgcodesender.gcode.GcodeStats;
import com.willwinder.universalgcodesender.gcode.ICommandCreator;
import com.willwinder.universalgcodesender.gcode.processors.CommandProcessor;
import com.willwinder.universalgcodesender.gcode.processors.CommentProcessor;
import com.willwinder.universalgcodesender.gcode.processors.DecimalProcessor;
Expand Down Expand Up @@ -102,6 +102,18 @@ public GUIBackend(UGSEventDispatcher eventDispatcher) {
// GUI API //
/////////////

/**
* This allows us to visualize a file without loading a controller profile.
*/
private static void initializeWithFallbackProcessors(GcodeParser parser) {
// Comment processor must come first otherwise we try to parse codes
// out of the comments, like an f-code when we see "(feed rate is 100)"
parser.addCommandProcessor(new CommentProcessor());
parser.addCommandProcessor(new WhitespaceProcessor());
parser.addCommandProcessor(new M30Processor());
parser.addCommandProcessor(new DecimalProcessor(4));
}

@Override
public void addUGSEventListener(UGSEventListener listener) {
eventDispatcher.addListener(listener);
Expand Down Expand Up @@ -227,18 +239,6 @@ public void applySettings(Settings settings) throws Exception {
}
}

/**
* This allows us to visualize a file without loading a controller profile.
*/
private static void initializeWithFallbackProcessors(GcodeParser parser) {
// Comment processor must come first otherwise we try to parse codes
// out of the comments, like an f-code when we see "(feed rate is 100)"
parser.addCommandProcessor(new CommentProcessor());
parser.addCommandProcessor(new WhitespaceProcessor());
parser.addCommandProcessor(new M30Processor());
parser.addCommandProcessor(new DecimalProcessor(4));
}

@Override
public void sendGcodeCommand(String commandText) throws Exception {
sendGcodeCommand(false, commandText);
Expand Down Expand Up @@ -316,6 +316,11 @@ public Position getWorkPosition() {
return controller != null ? controller.getControllerStatus().getWorkCoord() : new Position(0, 0, 0, Units.MM);
}

@Override
public void setWorkPosition(PartialPosition position) throws Exception {
controller.setWorkPosition(position);
}

@Override
public Position getMachinePosition() {
return controller != null ? controller.getControllerStatus().getMachineCoord() : new Position(0, 0, 0, Units.MM);
Expand Down Expand Up @@ -347,17 +352,6 @@ private File getTempDir() {
return tempDir;
}

@Override
public void setGcodeFile(File file) throws Exception {
unsetGcodeFile();

logger.log(Level.INFO, "Setting gcode file. {0}", file.getAbsolutePath());

this.gcodeFile = file;
eventDispatcher.sendUGSEvent(new FileStateEvent(FileState.OPENING_FILE));
processGcodeFile();
}

@Override
public void unsetGcodeFile() throws Exception {
if (gcodeStream != null) {
Expand Down Expand Up @@ -466,6 +460,17 @@ public File getGcodeFile() {
return this.gcodeFile;
}

@Override
public void setGcodeFile(File file) throws Exception {
unsetGcodeFile();

logger.log(Level.INFO, "Setting gcode file. {0}", file.getAbsolutePath());

this.gcodeFile = file;
eventDispatcher.sendUGSEvent(new FileStateEvent(FileState.OPENING_FILE));
processGcodeFile();
}

@Override
public File getProcessedGcodeFile() {
logger.log(Level.FINEST, String.format("Getting processed gcode file (%s).", this.processedGcodeFile));
Expand Down Expand Up @@ -649,15 +654,15 @@ public void toggleCheckMode() throws Exception {
this.controller.toggleCheckMode();
}

///////////////////////
// Utility functions //
///////////////////////

@Override
public void issueSoftReset() throws Exception {
this.controller.issueSoftReset();
}

///////////////////////
// Utility functions //
///////////////////////

@Override
public void requestParserState() throws Exception {
this.controller.viewParserState();
Expand Down Expand Up @@ -716,11 +721,6 @@ public ICommandCreator getCommandCreator() {
return controller.getCommandCreator();
}

@Override
public void setWorkPosition(PartialPosition position) throws Exception {
controller.setWorkPosition(position);
}

@Override
public void setWorkPositionUsingExpression(final Axis axis, final String expression) throws Exception {
Units preferredUnits = getSettings().getPreferredUnits();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public List<LineSegment> toObjFromReader(IGcodeStreamReader reader,
GcodeParser gp = getParser();

// Save the state
Position start = new Position(gp.getCurrentState().getUnits());
Position start = new Position(Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, gp.getCurrentState().getUnits());
double spindleSpeed = 0;

while (reader.getNumRowsRemaining() > 0) {
Expand Down
Loading

0 comments on commit 9fba1ae

Please sign in to comment.