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

Spindle Delay Customization #506

Merged
merged 5 commits into from
May 29, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gSender",
"version": "1.4.7",
"version": "1.4.8",
"description": "Electron sender for GRBL based CNC machines",
"author": {
"name": "Sienci Labs <hi@sienci.com>",
Expand Down
14 changes: 6 additions & 8 deletions src/app/containers/Preferences/Preferences.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class PreferencesPage extends PureComponent {
},
showWarning: store.get('widgets.visualizer.showWarning'),
showLineWarnings: store.get('widgets.visualizer.showLineWarnings'),
spindleDelay: store.get('widgets.spindle.delay'),
shouldWarnZero: store.get('workspace.shouldWarnZero', false),
ipRange: store.get('widgets.connection.ip', [192, 168, 5, 1]),
toolChange: {
Expand Down Expand Up @@ -528,13 +529,9 @@ class PreferencesPage extends PureComponent {

pubsub.publish('spindle:updated', newSpindleValue);
},
handleDelayToggle: (hasDelay) => {
const { spindle } = this.state;
handleDelayChange: (delay) => {
this.setState({
spindle: {
...spindle,
delay: hasDelay
}
spindleDelay: delay
});
}
},
Expand Down Expand Up @@ -863,7 +860,8 @@ class PreferencesPage extends PureComponent {
safeRetractHeight,
customDecimalPlaces,
spindle,
toolChange
toolChange,
spindleDelay
} = this.state;

store.set('workspace.reverseWidgets', reverseWidgets);
Expand All @@ -883,7 +881,7 @@ class PreferencesPage extends PureComponent {
store.replace('workspace[probeProfile]', probe);
store.set('widgets.spindle.spindleMax', spindle.spindleMax);
store.set('widgets.spindle.spindleMin', spindle.spindleMin);
store.set('widgets.spindle.delay', spindle.delay);
store.set('widgets.spindle.delay', spindleDelay);
this.probeConfig.set('retractionDistance', probeSettings.retractionDistance);
this.probeConfig.set('probeFeedrate', probeSettings.normalFeedrate);
this.probeConfig.set('probeFastFeedrate', probeSettings.fastFeedrate);
Expand Down
35 changes: 19 additions & 16 deletions src/app/containers/Preferences/SpindleLaser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import controller from 'app/lib/controller';

import SettingWrapper from '../components/SettingWrapper';
import Fieldset from '../components/Fieldset';
import Input from '../components/Input';
import GeneralArea from '../components/GeneralArea';
import TooltipCustom from '../../../components/TooltipCustom/ToolTip';
import Laser from './Laser';
Expand All @@ -14,8 +15,14 @@ import { collectUserUsageData } from '../../../lib/heatmap';
import { USAGE_TOOL_NAME } from '../../../constants';

const SpindleLaser = ({ active, state, actions }) => {
const { spindle } = state;
const spindleActions = actions.spindle;
const [delay, setDelay] = useState(spindle.delay);
const [machineProfile, setMachineProfile] = useState(store.get('workspace.machineProfile', {}));
const [delay, setDelay] = useState(store.get('widgets.spindle.delay', false));

useEffect(() => {
setDelay(spindle.delay);
}, [spindle]);

useEffect(() => {
const timeout = setTimeout(() => {
Expand Down Expand Up @@ -55,13 +62,7 @@ const SpindleLaser = ({ active, state, actions }) => {
setMachineProfile(updatedObj);
};

const handleSpindleDelayToggle = () => {
const spindleActions = actions.spindle;
spindleActions.handleDelayToggle(!delay);
setDelay(!delay);
};

const { spindle, laserOnOutline } = machineProfile;
const { spindle: isSpindle, laserOnOutline } = machineProfile;

return (
<SettingWrapper title="Spindle/Laser" show={active}>
Expand All @@ -71,28 +72,30 @@ const SpindleLaser = ({ active, state, actions }) => {
<TooltipCustom content="Enable or Disable Spindle/Laser" location="default">
<ToggleSwitch
label="Spindle/Laser"
checked={spindle}
checked={isSpindle}
onChange={handleToggle}
style={{ marginBottom: '1rem' }}
/>
</TooltipCustom>
<TooltipCustom content="Enable or Disable Laser ON during Outline" location="default">
<ToggleSwitch
label="Laser ON during Outline"
disabled={!spindle}
disabled={!isSpindle}
checked={laserOnOutline}
onChange={handleONToggle}
style={{ marginBottom: '1rem' }}
/>
</TooltipCustom>
<TooltipCustom content="Add delay after spindle ON" location="default">
<ToggleSwitch
<TooltipCustom content="Add delay after spindle. Please reload your file after changing" location="default">
<Input
label="Delay After Start"
checked={delay}
onChange={handleSpindleDelayToggle}
size="small"
style={{ marginBottom: '1rem' }}
units="s"
value={delay}
additionalProps={{ type: 'number', min: 0 }}
hasRounding={false}
onChange={(e) => spindleActions.handleDelayChange(e.target.value)}
/>
<p style={{ fontSize: '0.9rem', color: '#737373' }}>Please reload your file after changing delay.</p>
</TooltipCustom>
</Fieldset>
<Spindle state={state} actions={actions} />
Expand Down
5 changes: 5 additions & 0 deletions src/app/sagas/controllerSagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export function* initialize() {

const machineProfile = store.get('workspace.machineProfile');
const showLineWarnings = store.get('widgets.visualizer.showLineWarnings');
const delay = store.get('widgets.spindle.delay');
// Reset homing run flag to prevent rapid position without running homing
reduxStore.dispatch({
type: controllerActions.RESET_HOMING,
Expand All @@ -357,6 +358,10 @@ export function* initialize() {
if (showLineWarnings) {
controller.command('settings:updated', { showLineWarnings });
}

if (delay !== undefined) {
controller.command('settings:updated', { spindleDelay: delay });
}
const hooks = store.get('workspace.toolChangeHooks', {});
const toolChangeOption = store.get('workspace.toolChangeOption', 'Ignore');
const toolChangeContext = {
Expand Down
2 changes: 1 addition & 1 deletion src/app/store/defaultState.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ const defaultState = {
speed: 1000,
spindleMax: 30000,
spindleMin: 10000,
delay: false,
delay: 0,
laser: {
power: 100,
duration: 1,
Expand Down
28 changes: 13 additions & 15 deletions src/app/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import get from 'lodash/get';
import set from 'lodash/set';
// import merge from 'lodash/merge';
import uniq from 'lodash/uniq';
import { isEmpty, isNumber, isString } from 'lodash';
import { isEmpty } from 'lodash';
import semver from 'semver';
import settings from '../config/settings';
import ImmutableStore from '../lib/immutable-store';
Expand Down Expand Up @@ -160,31 +160,20 @@ const normalizeState = (state) => {
};

const merge = (base, saved) => {
const numberRegex = /^-?\d+.?\d*$/;

const baseIsObject = base instanceof Object;
const baseIsArray = Array.isArray(base);

const savedIsObject = saved instanceof Object;
const savedIsArray = Array.isArray(saved);

const fromStringToNumber = isNumber(base) && isString(saved) && numberRegex.test(saved);
const fromNumberToString = isNumber(saved) && isString(base) && numberRegex.test(base);

// if they are both not objects, use saved. migration will be made later if needed
if (
(!(baseIsObject) || baseIsArray) &&
(!(savedIsObject) || savedIsArray)
) {
// if they are the same type, use saved
// when numbers are rounded, they become strings
// so if it changes between those two types, and the string is a number, keep saved
if ((typeof base === typeof saved) || fromStringToNumber || fromNumberToString) {
return saved;
}
// if they are not, default structure changed, so use base
return base;
return saved;
// if one is an object and the other isn't, then default structure changed, so use base
} else if ((!(baseIsObject) || baseIsArray) || (!(savedIsObject) || savedIsArray)) {
// if one is an object and the other isn't, then default structure changed, so use base
return base;
}

Expand Down Expand Up @@ -255,6 +244,15 @@ const migrateStore = () => {
return;
}

if (semver.lt(cnc.version, '1.4.8')) {
const delay = store.get('widgets.spindle.delay');
if (delay) {
store.set('widgets.spindle.delay', 1);
} else {
store.set('widgets.spindle.delay', 0);
}
}

if (semver.lt(cnc.version, '1.4.4')) {
store.replace('widgets.connection.controller.type', 'Grbl');
}
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gSender",
"version": "1.4.7",
"version": "1.4.8",
"productName": "gSender",
"description": "CNC Milling Controller",
"author": {
Expand Down
6 changes: 3 additions & 3 deletions src/server/controllers/Grbl/GrblController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1453,13 +1453,13 @@ class GrblController {

// add delay to spindle startup if enabled
const preferences = store.get('preferences', {});
const delay = _.get(preferences, 'spindle.delay', false);
const delay = _.get(preferences, 'spindleDelay', 0);

// test if there is a G4 command already
const delayRegex = new RegExp('(G4 ?P?[0-9]+)');
// only add one if there isn't
if (delay && !delayRegex.test(gcode)) {
gcode = gcode.replace(/\b(?:S\d* ?M[34]|M[34] ?S\d*)\b/g, '$& G4 P1');
if (Number(delay) && !delayRegex.test(gcode)) {
gcode = gcode.replace(/\b(?:S\d* ?M[34]|M[34] ?S\d*)\b/g, `$& G4 P${delay}`);
}

const gcodeWithoutComments = gcode.replace(bracketCommentLine, '');
Expand Down
6 changes: 3 additions & 3 deletions src/server/controllers/Grblhal/GrblHalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1496,10 +1496,10 @@ class GrblHalController {

// add delay to spindle startup if enabled
const preferences = store.get('preferences', {});
const delay = _.get(preferences, 'spindle.delay', false);
const delay = _.get(preferences, 'spindleDelay', 0);

if (delay) {
gcode = gcode.replace(/\b(?:S\d* ?M[34]|M[34] ?S\d*)\b(?! ?G4 ?P?\b)/g, '$& G4 P1');
if (Number(delay)) {
gcode = gcode.replace(/\b(?:S\d* ?M[34]|M[34] ?S\d*)\b(?! ?G4 ?P?\b)/g, `$& G4 P${delay}`);
}

const ok = this.sender.load(name, gcode + '\n', context);
Expand Down
Loading