Skip to content

Commit

Permalink
Added rapid speed overrides (winder#2489)
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler authored Mar 20, 2024
1 parent 6a63d63 commit 9df910f
Show file tree
Hide file tree
Showing 15 changed files with 599 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.google.gson.JsonObject;
import com.willwinder.universalgcodesender.communicator.ICommunicator;
import com.willwinder.universalgcodesender.firmware.IOverrideManager;
import com.willwinder.universalgcodesender.firmware.g2core.G2CoreOverrideManager;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.listeners.ControllerStatus;
import com.willwinder.universalgcodesender.listeners.ControllerStatusBuilder;
Expand All @@ -42,16 +40,13 @@ public class G2CoreController extends TinyGController {
* A temporary flag for emulating a JOG state when parsing the controller status
*/
private boolean isJogging = false;
private final IOverrideManager overrideManager;

public G2CoreController() {
super();
overrideManager = new G2CoreOverrideManager(this, getCommunicator());
}

public G2CoreController(ICommunicator communicator) {
super(communicator);
overrideManager = new G2CoreOverrideManager(this, communicator);
}

@Override
Expand Down Expand Up @@ -195,9 +190,4 @@ protected ControllerStatus parseControllerStatus(JsonObject jo) {

return controllerStatus;
}

@Override
public IOverrideManager getOverrideManager() {
return overrideManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.model.PartialPosition;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.UnitUtils.Units;
import com.willwinder.universalgcodesender.services.MessageService;
import com.willwinder.universalgcodesender.types.GcodeCommand;
import com.willwinder.universalgcodesender.types.GrblFeedbackMessage;
import com.willwinder.universalgcodesender.types.GrblSettingMessage;
Expand Down Expand Up @@ -94,7 +95,7 @@ public GrblController(ICommunicator communicator) {
this.firmwareSettings = new GrblFirmwareSettings(this);
this.comm.addListener(firmwareSettings);
this.initializer = new GrblControllerInitializer(this);
this.overrideManager = new GrblOverrideManager(this, communicator);
this.overrideManager = new GrblOverrideManager(this, communicator, messageService);
}

public GrblController() {
Expand All @@ -111,6 +112,12 @@ public IFirmwareSettings getFirmwareSettings() {
return firmwareSettings;
}

@Override
public void setMessageService(MessageService messageService) {
super.setMessageService(messageService);
overrideManager.setMessageService(messageService);
}

/***********************
* API Implementation. *
***********************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ This file is part of Universal Gcode Sender (UGS).
package com.willwinder.universalgcodesender.firmware;

import com.willwinder.universalgcodesender.IController;
import static com.willwinder.universalgcodesender.Utils.roundToNearestStepValue;
import com.willwinder.universalgcodesender.communicator.ICommunicator;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.listeners.ControllerStatus;
import com.willwinder.universalgcodesender.listeners.DefaultControllerListener;
import com.willwinder.universalgcodesender.listeners.OverridePercents;
import com.willwinder.universalgcodesender.listeners.OverrideType;
import com.willwinder.universalgcodesender.model.Overrides;

/**
* An abstract override manager with some default implementation of common functions.
Expand All @@ -41,6 +39,7 @@ public abstract class AbstractOverrideManager implements IOverrideManager {

protected int targetFeedSpeed = 100;
protected int targetSpindleSpeed = 100;
protected int targetRapidSpeed = 100;

private boolean isRunning = false;
private long lastSentCommand = 0;
Expand All @@ -60,6 +59,7 @@ public void onControllerStatus(ControllerStatus controllerStatus) {
if (!isRunning) {
targetFeedSpeed = controllerStatus.getOverrides().feed();
targetSpindleSpeed = controllerStatus.getOverrides().spindle();
targetRapidSpeed = controllerStatus.getOverrides().rapid();
return;
}

Expand All @@ -72,59 +72,18 @@ public void onControllerStatus(ControllerStatus controllerStatus) {
OverridePercents currentOverridePercents = controllerStatus.getOverrides();
adjustFeedOverride(currentOverridePercents);
adjustSpindleOverride(currentOverridePercents);
adjustRapidOverride(currentOverridePercents);

if (hasSettled()) {
stop();
}
}

protected void adjustFeedOverride(OverridePercents currentOverridePercents) {
if (currentOverridePercents.feed() == targetFeedSpeed) {
return;
}

float currentFeed = currentOverridePercents.feed();
int majorSteps = (int) ((targetFeedSpeed - currentFeed) / getSpeedMajorStep(OverrideType.FEED_SPEED));
int minorSteps = (int) ((targetFeedSpeed - currentFeed) / getSpeedMinorStep(OverrideType.FEED_SPEED));

try {
if (majorSteps < 0) {
sendOverrideCommand(Overrides.CMD_FEED_OVR_COARSE_MINUS);
} else if (majorSteps > 0) {
sendOverrideCommand(Overrides.CMD_FEED_OVR_COARSE_PLUS);
} else if (minorSteps < 0) {
sendOverrideCommand(Overrides.CMD_FEED_OVR_FINE_MINUS);
} else if (minorSteps > 0) {
sendOverrideCommand(Overrides.CMD_FEED_OVR_FINE_PLUS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected abstract void adjustFeedOverride(OverridePercents currentOverridePercents);

protected void adjustSpindleOverride(OverridePercents currentOverridePercents) {
if (currentOverridePercents.spindle() == targetSpindleSpeed) {
return;
}
protected abstract void adjustSpindleOverride(OverridePercents currentOverridePercents) ;

float currentSpindle = currentOverridePercents.spindle();
int majorSteps = (int) ((targetSpindleSpeed - currentSpindle) / getSpeedMajorStep(OverrideType.SPINDLE_SPEED));
int minorSteps = (int) ((targetSpindleSpeed - currentSpindle) / getSpeedMinorStep(OverrideType.SPINDLE_SPEED));

try {
if (majorSteps < 0) {
sendOverrideCommand(Overrides.CMD_SPINDLE_OVR_COARSE_MINUS);
} else if (majorSteps > 0) {
sendOverrideCommand(Overrides.CMD_SPINDLE_OVR_COARSE_PLUS);
} else if (minorSteps < 0) {
sendOverrideCommand(Overrides.CMD_SPINDLE_OVR_FINE_MINUS);
} else if (minorSteps > 0) {
sendOverrideCommand(Overrides.CMD_SPINDLE_OVR_FINE_PLUS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected abstract void adjustRapidOverride(OverridePercents currentOverridePercents);

protected abstract int getSpeedMinorStep(OverrideType overrideType);

Expand Down Expand Up @@ -161,18 +120,6 @@ private void stop() {
@Override
public boolean hasSettled() {
OverridePercents overrides = controller.getControllerStatus().getOverrides();
return overrides.spindle() == targetSpindleSpeed && overrides.feed() == targetFeedSpeed;
}

@Override
public void setSpeedTarget(OverrideType type, int percent) {
percent = (int) Math.round(roundToNearestStepValue(percent, getSpeedMin(type), getSpeedMax(type), getSpeedStep(type)));
if (type == OverrideType.FEED_SPEED) {
targetFeedSpeed = percent;
} else if (type == OverrideType.SPINDLE_SPEED) {
targetSpindleSpeed = percent;
}

start();
return overrides.spindle() == targetSpindleSpeed && overrides.feed() == targetFeedSpeed && overrides.rapid() == targetRapidSpeed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.universalgcodesender.listeners.OverrideType;
import com.willwinder.universalgcodesender.model.Overrides;
import com.willwinder.universalgcodesender.services.MessageService;

import java.util.List;

Expand All @@ -31,7 +32,7 @@ This file is part of Universal Gcode Sender (UGS).
*/
public class DefaultOverrideManager implements IOverrideManager {
@Override
public void setSpeedTarget(OverrideType type, int value) {
public void setSliderTarget(OverrideType type, int value) {
// Not implemented
}

Expand All @@ -41,17 +42,17 @@ public boolean hasSettled() {
}

@Override
public int getSpeedMax(OverrideType type) {
public int getSliderMax(OverrideType type) {
return 0;
}

@Override
public int getSpeedMin(OverrideType type) {
public int getSliderMin(OverrideType type) {
return 0;
}

@Override
public int getSpeedStep(OverrideType type) {
public int getSliderStep(OverrideType type) {
return 0;
}

Expand All @@ -61,17 +62,17 @@ public void sendOverrideCommand(Overrides command) {
}

@Override
public int getSpeedDefault(OverrideType overrideType) {
public int getSliderDefault(OverrideType overrideType) {
return 0;
}

@Override
public int getSpeedTargetValue(OverrideType type) {
public int getSliderTargetValue(OverrideType type) {
return 0;
}

@Override
public List<OverrideType> getSpeedTypes() {
public List<OverrideType> getSliderTypes() {
return List.of();
}

Expand All @@ -80,6 +81,11 @@ public List<OverrideType> getToggleTypes() {
return List.of();
}

@Override
public List<OverrideType> getRadioTypes() {
return List.of();
}

@Override
public boolean isAvailable() {
return false;
Expand All @@ -94,4 +100,29 @@ public void toggle(OverrideType overrideType) {
public boolean isToggled(OverrideType overrideType) {
return false;
}

@Override
public int getRadioDefault(OverrideType type) {
return 0;
}

@Override
public void setRadioTarget(OverrideType type, int value) {
// Not implemented
}

@Override
public void setMessageService(MessageService messageService) {
// Not implemented
}

@Override
public List<Integer> getSliderSteps(OverrideType type) {
return List.of();
}

@Override
public List<Integer> getRadioSteps(OverrideType type) {
return List.of();
}
}
Loading

0 comments on commit 9df910f

Please sign in to comment.