Skip to content

Commit

Permalink
Make machine boundaries handle inversed homing positions. (#2655)
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler authored Dec 22, 2024
1 parent f8255f3 commit 00a8ab1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ public class CapabilitiesConstants {
* in the right, far, upper corner which is then set to 0, therefore all coordinates in the machine space is
* defined with negative values.
* <p>
* Some controllers have the option to inverse this, making the machine zero at the left, lower, close corner of
* the machine.
* Some controllers have the option to set the homed position as machine zero.
* <p>
* By defining this capability we can account for this in the visualizer.
*/
public static final String MACHINE_POSITION_IN_POSITIVE_SPACE = "MACHINE_POSITION_IN_POSITIVE_SPACE";
public static final String HOMING_SETS_MACHINE_ZERO_POSITION = "HOMING_SETS_ZERO_POSITION";

/**
* Does the controller support variable spindles (typically PWM or through RS484 interfaces)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ protected static Capabilities getGrblStatusCapabilities(final double version, fi
}

if (options.isEnabled(GrblBuildOption.HOMING_FORCE_ORIGIN_ENABLED)) {
ret.addCapability(CapabilitiesConstants.MACHINE_POSITION_IN_POSITIVE_SPACE);
ret.addCapability(CapabilitiesConstants.HOMING_SETS_MACHINE_ZERO_POSITION);
}

if (options.isEnabled(GrblBuildOption.VARIABLE_SPINDLE_ENABLED)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,32 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.ugs.nbm.visualizer.renderables;

import static com.jogamp.opengl.GL.GL_LINES;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions;
import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_BOUNDARY_INVERT_X;
import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_BOUNDARY_INVERT_Y;
import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_BOUNDARY_INVERT_Z;
import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_BOUNDRY;
import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_X;
import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_Y;
import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.VISUALIZER_OPTION_Z;
import com.willwinder.ugs.nbm.visualizer.shared.Renderable;
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.universalgcodesender.CapabilitiesConstants;
import com.willwinder.universalgcodesender.firmware.FirmwareSettingsException;
import com.willwinder.universalgcodesender.firmware.IFirmwareSettings;
import com.willwinder.universalgcodesender.model.*;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.model.Axis;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.model.PartialPosition;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.UGSEvent;
import com.willwinder.universalgcodesender.model.UnitUtils;
import com.willwinder.universalgcodesender.model.events.ControllerStateEvent;
import com.willwinder.universalgcodesender.model.events.FirmwareSettingEvent;

import static com.jogamp.opengl.GL.GL_LINES;
import static com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions.*;

/**
* Displays the machine boundries based on the soft limits
*
Expand Down Expand Up @@ -61,7 +74,7 @@ public MachineBoundries(String title) {
}

private void onUGSEvent(UGSEvent event) {
if (!isFirmwareSettingsEvent(event)) {
if (!isFirmwareSettingsEvent(event) && !isControllerIdleEvent(event)) {
return;
}

Expand All @@ -78,11 +91,16 @@ private void onUGSEvent(UGSEvent event) {
minPositionBuilder.setValue(axis, -softLimit);
}
minPosition = minPositionBuilder.build();
updateSettingsFromController();
} catch (FirmwareSettingsException ignored) {
// Never mind this.
}
}

private boolean isControllerIdleEvent(UGSEvent event) {
return event instanceof ControllerStateEvent controllerStateEvent && (controllerStateEvent.getState() == ControllerState.IDLE || controllerStateEvent.getState() == ControllerState.ALARM);
}

private boolean isFirmwareSettingsEvent(UGSEvent event) {
// This will prevent us from accessing the firmware settings before the init
// processes has finished and it will also prevent us from accessing the
Expand Down Expand Up @@ -118,9 +136,34 @@ public void reloadPreferences(VisualizerOptions vo) {
xAxisColor = VisualizerOptions.colorToFloatArray(vo.getOptionForKey(VISUALIZER_OPTION_X).value);
yAxisColor = VisualizerOptions.colorToFloatArray(vo.getOptionForKey(VISUALIZER_OPTION_Y).value);
zAxisColor = VisualizerOptions.colorToFloatArray(vo.getOptionForKey(VISUALIZER_OPTION_Z).value);
updateSettingsFromController();
}

private void updateSettingsFromController() {
BackendAPI backendAPI = CentralLookup.getDefault().lookup(BackendAPI.class);
invertX = VisualizerOptions.getBooleanOption(VISUALIZER_OPTION_BOUNDARY_INVERT_X, false) ? -1 : 1;
invertY = VisualizerOptions.getBooleanOption(VISUALIZER_OPTION_BOUNDARY_INVERT_Y, false) ? -1 : 1;
invertZ = VisualizerOptions.getBooleanOption(VISUALIZER_OPTION_BOUNDARY_INVERT_Z, false) ? -1 : 1;

if (backendAPI == null || backendAPI.getController() == null) {
return;
}

boolean homingSetsZeroPosition = backendAPI.getController().getCapabilities().hasCapability(CapabilitiesConstants.HOMING_SETS_MACHINE_ZERO_POSITION);
if (homingSetsZeroPosition) {
invertBasedOnHomingDirection(backendAPI);
}
}

private void invertBasedOnHomingDirection(BackendAPI backendAPI) {
IFirmwareSettings settings = backendAPI.getController().getFirmwareSettings();
invertX = isHomingDirectionInverted(settings, Axis.X) ? -invertX : invertX;
invertY = isHomingDirectionInverted(settings, Axis.Y) ? -invertY : invertY;
invertZ = isHomingDirectionInverted(settings, Axis.Z) ? -invertZ : invertZ;
}

private static boolean isHomingDirectionInverted(IFirmwareSettings settings, Axis axis) {
return settings.isHomingDirectionInverted(axis);
}

@Override
Expand Down

0 comments on commit 00a8ab1

Please sign in to comment.