Skip to content

Commit

Permalink
Store visualizer settings (winder#1964)
Browse files Browse the repository at this point in the history
Now stores which renderables that should be active and added those settings to the visualizer options.
Also added soft limits for FluidNC with the possibility to invert the machine boundaries.
  • Loading branch information
breiler committed Aug 17, 2022
1 parent 77ec8c4 commit 3b20050
Show file tree
Hide file tree
Showing 31 changed files with 814 additions and 294 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.willwinder.universalgcodesender.firmware.fluidnc;

import com.willwinder.universalgcodesender.Capabilities;
import com.willwinder.universalgcodesender.CapabilitiesConstants;
import com.willwinder.universalgcodesender.ConnectionWatchTimer;
import com.willwinder.universalgcodesender.GrblCapabilitiesConstants;
import com.willwinder.universalgcodesender.GrblCommunicator;
Expand All @@ -12,7 +11,6 @@
import com.willwinder.universalgcodesender.StatusPollTimer;
import com.willwinder.universalgcodesender.connection.ConnectionDriver;
import com.willwinder.universalgcodesender.connection.ConnectionException;
import com.willwinder.universalgcodesender.firmware.FirmwareSetting;
import com.willwinder.universalgcodesender.firmware.FirmwareSettingsException;
import com.willwinder.universalgcodesender.firmware.IFirmwareSettings;
import com.willwinder.universalgcodesender.firmware.fluidnc.commands.FluidNCCommand;
Expand Down Expand Up @@ -410,7 +408,7 @@ public GcodeState getCurrentGcodeState() {
}

@Override
public void beginStreaming() throws Exception {
public void beginStreaming() {
// Send all queued commands and streams then kick off the stream.
try {
if (streamCommands != null) {
Expand Down Expand Up @@ -557,16 +555,15 @@ private void initializeController() {
sendAndWaitForCompletion(this, new GetErrorCodesCommand(), 3000);
sendAndWaitForCompletion(this, new GetAlarmCodesCommand(), 3000);

FluidNCUtils.addCapabilities(capabilities, semanticVersion);

refreshFirmwareSettings();

// Fetch the gcode state
messageService.dispatchMessage(MessageType.INFO, "*** Fetching device state\n");
GetParserStateCommand getParserStateCommand = sendAndWaitForCompletion(this, new GetParserStateCommand());
String state = getParserStateCommand.getState().orElseThrow(() -> new ConnectionException("Could not get controller state"));
gcodeParser.addCommand(state);

refreshFirmwareSettings();
FluidNCUtils.addCapabilities(capabilities, semanticVersion, firmwareSettings);

// Toggle the state to force UI update
setControllerState(ControllerState.CONNECTING);
setControllerState(ControllerState.IDLE);
Expand All @@ -588,27 +585,6 @@ private void initializeController() {
private void refreshFirmwareSettings() throws FirmwareSettingsException {
messageService.dispatchMessage(MessageType.INFO, "*** Fetching device settings\n");
firmwareSettings.refresh();
firmwareSettings.getAllSettings().forEach(setting -> {
addCapabilityIfSettingStartsWith(setting, "axes/a", CapabilitiesConstants.A_AXIS);
addCapabilityIfSettingStartsWith(setting, "axes/b", CapabilitiesConstants.B_AXIS);
addCapabilityIfSettingStartsWith(setting, "axes/c", CapabilitiesConstants.C_AXIS);
addCapabilityIfSettingStartsWith(setting, "axes/x", CapabilitiesConstants.X_AXIS);
addCapabilityIfSettingStartsWith(setting, "axes/y", CapabilitiesConstants.Y_AXIS);
addCapabilityIfSettingStartsWith(setting, "axes/z", CapabilitiesConstants.Z_AXIS);
});
}

/**
* Adds a capability if there is a setting which key starts with the given setting key.
*
* @param setting the setting to check
* @param settingKey the key which the setting key should start with
* @param capabilitiesConstant the capabilities constant to add
*/
private void addCapabilityIfSettingStartsWith(FirmwareSetting setting, String settingKey, String capabilitiesConstant) {
if (StringUtils.startsWith(setting.getKey().toLowerCase(), settingKey.toLowerCase())) {
capabilities.addCapability(capabilitiesConstant);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.willwinder.universalgcodesender.firmware.fluidnc;

import com.willwinder.universalgcodesender.IController;
import com.willwinder.universalgcodesender.Utils;
import com.willwinder.universalgcodesender.firmware.FirmwareSetting;
import com.willwinder.universalgcodesender.firmware.FirmwareSettingsException;
import com.willwinder.universalgcodesender.firmware.IFirmwareSettings;
import com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener;
import com.willwinder.universalgcodesender.firmware.fluidnc.commands.FluidNCCommand;
import com.willwinder.universalgcodesender.firmware.fluidnc.commands.GetFirmwareSettingsCommand;
import com.willwinder.universalgcodesender.firmware.fluidnc.commands.SystemCommand;
import com.willwinder.universalgcodesender.model.Axis;
import com.willwinder.universalgcodesender.model.UnitUtils;
import com.willwinder.universalgcodesender.utils.ControllerUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -65,6 +67,7 @@ public FirmwareSetting setValue(String key, String value) throws FirmwareSetting
if (systemCommand.isOk()) {
FirmwareSetting firmwareSetting = new FirmwareSetting(key, value, "", "", "");
settings.put(key, firmwareSetting);
listeners.forEach(l -> l.onUpdatedFirmwareSetting(firmwareSetting));
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -125,7 +128,9 @@ public void setHardLimitsEnabled(boolean enabled) throws FirmwareSettingsExcepti

@Override
public boolean isSoftLimitsEnabled() throws FirmwareSettingsException {
return false;
return getSetting("axes/x/soft_limits").filter(s -> StringUtils.equalsIgnoreCase("true", s.getValue())).isPresent() ||
getSetting("axes/y/soft_limits").filter(s -> StringUtils.equalsIgnoreCase("true", s.getValue())).isPresent() ||
getSetting("axes/z/soft_limits").filter(s -> StringUtils.equalsIgnoreCase("true", s.getValue())).isPresent();
}

@Override
Expand Down Expand Up @@ -155,12 +160,20 @@ public double getStepsPerMillimeter(Axis axis) throws FirmwareSettingsException

@Override
public void setSoftLimit(Axis axis, double limit) throws FirmwareSettingsException {

setValue("axes/" + axis.name().toLowerCase() + "/max_travel_mm", Utils.formatter.format(limit));
}

@Override
public double getSoftLimit(Axis axis) throws FirmwareSettingsException {
return 0;
return getSetting("axes/" + axis.name().toLowerCase() + "/max_travel_mm")
.map(s -> {
try {
return Utils.formatter.parse(s.getValue()).doubleValue();
} catch (ParseException e) {
return 0d;
}
})
.orElse(0d);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.willwinder.universalgcodesender.CapabilitiesConstants;
import com.willwinder.universalgcodesender.GrblUtils;
import com.willwinder.universalgcodesender.IController;
import com.willwinder.universalgcodesender.firmware.FirmwareSetting;
import com.willwinder.universalgcodesender.firmware.FirmwareSettingsException;
import com.willwinder.universalgcodesender.firmware.IFirmwareSettings;
import com.willwinder.universalgcodesender.firmware.fluidnc.commands.GetStatusCommand;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.listeners.ControllerStatus;
Expand All @@ -12,6 +15,7 @@
import com.willwinder.universalgcodesender.model.UnitUtils;
import com.willwinder.universalgcodesender.services.MessageService;
import com.willwinder.universalgcodesender.utils.SemanticVersion;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.Optional;
Expand Down Expand Up @@ -183,16 +187,46 @@ public static GetStatusCommand queryForStatusReport(IController controller, Mess
});
}

public static void addCapabilities(Capabilities capabilities, SemanticVersion version) {
public static void addCapabilities(Capabilities capabilities, SemanticVersion version, IFirmwareSettings firmwareSettings) {
capabilities.addCapability(CapabilitiesConstants.JOGGING);
capabilities.addCapability(CapabilitiesConstants.RETURN_TO_ZERO);
capabilities.addCapability(CapabilitiesConstants.CONTINUOUS_JOGGING);
capabilities.addCapability(CapabilitiesConstants.HOMING);
capabilities.addCapability(CapabilitiesConstants.FIRMWARE_SETTINGS);
capabilities.addCapability(CapabilitiesConstants.OVERRIDES);

try {
if (firmwareSettings.isSoftLimitsEnabled()) {
capabilities.addCapability(CapabilitiesConstants.SOFT_LIMITS);
}

firmwareSettings.getAllSettings().forEach(setting -> {
addCapabilityIfSettingStartsWith(capabilities, setting, "axes/a", CapabilitiesConstants.A_AXIS);
addCapabilityIfSettingStartsWith(capabilities, setting, "axes/b", CapabilitiesConstants.B_AXIS);
addCapabilityIfSettingStartsWith(capabilities, setting, "axes/c", CapabilitiesConstants.C_AXIS);
addCapabilityIfSettingStartsWith(capabilities, setting, "axes/x", CapabilitiesConstants.X_AXIS);
addCapabilityIfSettingStartsWith(capabilities, setting, "axes/y", CapabilitiesConstants.Y_AXIS);
addCapabilityIfSettingStartsWith(capabilities, setting, "axes/z", CapabilitiesConstants.Z_AXIS);
});
} catch (FirmwareSettingsException e) {
// Never mind
}

if (version.compareTo(new SemanticVersion(3, 5, 2)) >= 0) {
capabilities.addCapability(CapabilitiesConstants.FILE_SYSTEM);
}
}

/**
* Adds a capability if there is a setting which key starts with the given setting key.
*
* @param setting the setting to check
* @param settingKey the key which the setting key should start with
* @param capabilitiesConstant the capabilities constant to add
*/
private static void addCapabilityIfSettingStartsWith(Capabilities capabilities, FirmwareSetting setting, String settingKey, String capabilitiesConstant) {
if (StringUtils.startsWith(setting.getKey().toLowerCase(), settingKey.toLowerCase())) {
capabilities.addCapability(capabilitiesConstant);
}
}
}
42 changes: 38 additions & 4 deletions ugs-core/src/resources/MessagesBundle_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -303,22 +303,56 @@ platform.window.serialconsole = Console
platform.window.serialconsole.tooltip = Console displaying messages to and from the controller.
platform.window.visualizer = Visualizer
platform.window.visualizer.tooltip = 3D view of the current operation.
platform.visualizer.edit.options.title = Visualizer options
platform.visualizer.color.background = Background Color
platform.visualizer.color.tool = Tool Color
platform.visualizer.tool = Show tool location
platform.visualizer.color.tool = Tool color
platform.visualizer.tool.desc = Shows the tool location
platform.visualizer.model = Show model
platform.visualizer.model.desc = Shows the tool path for the model
platform.visualizer.mouseover = Show the mouse position
platform.visualizer.mouseover.desc = Show the mouse position
platform.visualizer.orientation.cube = Shows orientation cube
platform.visualizer.orientation.cube.desc = Shows the orientation of the scene using a 3D cube
platform.visualizer.selection = Show selected lines
platform.visualizer.selection.desc = Show the selected lines in the editor
platform.visualizer.size.display = Show the size
platform.visualizer.size.display.desc = Shows the model size in the currently selected units
platform.visualizer.editor.position = Show the editor position
platform.visualizer.editor.position.desc = Shows the position of the selected line in the editor
platform.visualizer.probe.preview = Show probe preview
platform.visualizer.probe.preview.desc = Show probe preview
platform.visualizer.dowel.preview = Show dowel preview
platform.visualizer.dowel.preview.desc = Show dowel preview
platform.visualizer.highlight = Show highlighted lines
platform.visualizer.highlight.desc = Highlights the selected lines from the editor
platform.visualizer.color.highlight = Editor Line Highlight Color
platform.visualizer.color.linear = Linear Movement Color (G1)
platform.visualizer.color.rapid = Rapid Movement Color (G0)
platform.visualizer.color.arc = Arc Movement Color (G2/G3)
platform.visualizer.color.plunge = Plunge/Raise Movement Color
platform.visualizer.color.completed = Color of lines which have been completed
platform.visualizer.color.highlight = Editor Line Highlight Color
platform.visualizer.grid = Show coordinates and plane
platform.visualizer.grid.desc = Show coordinates and plane
platform.visualizer.color.xy-grid = Color (and opacity) of XY grid lines.
platform.visualizer.color.xy-plane = Color (and opacity) of XY plane.
platform.visualizer.color.x-axis = Color (and opacity) of X-Axis line.
platform.visualizer.color.y-axis = Color (and opacity) of Y-Axis line.
platform.visualizer.color.z-axis = Color (and opacity) of Z-Axis line.
platform.visualizer.color.sizedisplay = Color of size display lines and text.
platform.visualizer.color.boundry-base = Color (and opacity) of the machine base
platform.visualizer.color.boundry-sides = Color (and opacity) of the machine sides
platform.visualizer.color.boundry-base = Boundary base color (and opacity)
platform.visualizer.color.boundry-sides = Boundary side color (and opacity)
platform.visualizer.boundary = Show machine boundary
platform.visualizer.boundary.desc = Shows the machine boundary as a bounding box. The size and position of the boundary will be determined by the controller settings.
platform.visualizer.boundary.invert.x = Invert boundary X
platform.visualizer.boundary.invert.y = Invert boundary Y
platform.visualizer.boundary.invert.z = Invert boundary Z
platform.visualizer.boundary.invert.desc = On some controllers the machine max/min is inverted making the machine boundaries to be rendered in the wrong place. Use this setting to invert the axis.
platform.visualizer.autolevel.preview = Show auto leveler preview
platform.visualizer.autolevel.preview.desc = Shows the auto leveler preview
platform.visualizer.color.surface.high = Auto leveler high color
platform.visualizer.color.surface.low = Auto leveler low color

PendantMenu.item.StartServer = Start...
PendantMenu.item.StopServer = Stop...
restore = Restore defaults
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.willwinder.universalgcodesender.firmware.fluidnc;

import com.willwinder.universalgcodesender.Capabilities;
import com.willwinder.universalgcodesender.firmware.IFirmwareSettings;
import com.willwinder.universalgcodesender.utils.SemanticVersion;
import org.junit.Test;

import static com.willwinder.universalgcodesender.CapabilitiesConstants.FILE_SYSTEM;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public class FluidNCUtilsTest {

Expand Down Expand Up @@ -50,10 +52,11 @@ public void parseVersionShouldReturnMajorMinorVersions() {
@Test
public void addCapabilitiesShouldAddFileSystem() {
Capabilities capabilities = new Capabilities();
FluidNCUtils.addCapabilities(capabilities, new SemanticVersion(3, 5, 1));
IFirmwareSettings firmwareSettings = mock(IFirmwareSettings.class);
FluidNCUtils.addCapabilities(capabilities, new SemanticVersion(3, 5, 1), firmwareSettings);
assertFalse(capabilities.hasCapability(FILE_SYSTEM));

FluidNCUtils.addCapabilities(capabilities, new SemanticVersion(3, 5, 2));
FluidNCUtils.addCapabilities(capabilities, new SemanticVersion(3, 5, 2), firmwareSettings);
assertTrue(capabilities.hasCapability(FILE_SYSTEM));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ class DowelPreview(val description: String, val generator: DowelGenerator) : Ren
}
}
}

override fun isEnabled(): Boolean {
return VisualizerOptions.getBooleanOption(VisualizerOptions.VISUALIZER_OPTION_DOWEL_PREVIEW, true)
}

override fun setEnabled(enabled: Boolean) {
VisualizerOptions.setBooleanOption(VisualizerOptions.VISUALIZER_OPTION_DOWEL_PREVIEW, enabled)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbm.visualizer.shared.Renderable;
import com.willwinder.ugs.platform.probe.ProbeService.ProbeParameters;
import com.willwinder.ugs.platform.probe.renderable.ProbeRenderableHelpers.Side;
import com.willwinder.universalgcodesender.model.Position;

import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_PROBE_PREVIEW;
import static com.willwinder.ugs.platform.probe.renderable.ProbeRenderableHelpers.Side.NEGATIVE;
import static com.willwinder.ugs.platform.probe.renderable.ProbeRenderableHelpers.Side.POSITIVE;
import static com.willwinder.ugs.platform.probe.renderable.ProbeRenderableHelpers.drawTouchPlate;
import static com.willwinder.ugs.platform.probe.renderable.ProbeRenderableHelpers.drawArrow;
import com.willwinder.universalgcodesender.model.Position;
import static com.willwinder.ugs.platform.probe.renderable.ProbeRenderableHelpers.drawTouchPlate;

/**
*
Expand Down Expand Up @@ -203,4 +205,14 @@ public void draw(GLAutoDrawable drawable, boolean idle, Position machineCoord, P
drawXYZ(gl, X, Y);
}
}
}

@Override
public boolean isEnabled() {
return VisualizerOptions.getBooleanOption(VISUALIZER_OPTION_PROBE_PREVIEW, true);
}

@Override
public void setEnabled(boolean enabled) {
VisualizerOptions.setBooleanOption(VISUALIZER_OPTION_PROBE_PREVIEW, enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbm.visualizer.shared.Renderable;
import com.willwinder.universalgcodesender.model.Position;

import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_PROBE_PREVIEW;

/**
*
* @author wwinder
Expand Down Expand Up @@ -98,4 +100,14 @@ public void draw(GLAutoDrawable drawable, boolean idle, Position machineCoord, P
gl.glTranslated(0, 0, zAbs - 1);
glut.glutSolidCone(.2, 1, slices, stacks);
}

@Override
public boolean isEnabled() {
return VisualizerOptions.getBooleanOption(VISUALIZER_OPTION_PROBE_PREVIEW, true);
}

@Override
public void setEnabled(boolean enabled) {
VisualizerOptions.setBooleanOption(VISUALIZER_OPTION_PROBE_PREVIEW, enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ This file is part of Universal Gcode Sender (UGS).

import java.awt.Color;

import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_EDITOR_POSITION;

/**
* Displays a pointer on the given line number
*
Expand Down Expand Up @@ -85,7 +87,7 @@ public void draw(GLAutoDrawable drawable, boolean idle, Position machineCoord, P
}

public void setLineNumber(int lineNumber) {
if(lineNumber < 0) {
if (lineNumber < 0) {
position = null;
return;
}
Expand All @@ -95,4 +97,14 @@ public void setLineNumber(int lineNumber) {
.findFirst()
.ifPresent(lineSegment -> position = lineSegment.getEnd());
}

@Override
public boolean isEnabled() {
return VisualizerOptions.getBooleanOption(VISUALIZER_OPTION_EDITOR_POSITION, true);
}

@Override
public void setEnabled(boolean enabled) {
VisualizerOptions.setBooleanOption(VISUALIZER_OPTION_EDITOR_POSITION, enabled);
}
}
Loading

0 comments on commit 3b20050

Please sign in to comment.