Skip to content

Commit

Permalink
[MI-1388]: fixed jog values reconverting on UI
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiabeluli committed Mar 5, 2024
1 parent e45de78 commit 5bd0fb3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 87 deletions.
8 changes: 6 additions & 2 deletions src/app/containers/Preferences/General/JoggingPresets.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import defaultState from 'app/store/defaultState';
import Fieldset from '../components/Fieldset';
import Input from '../components/Input';
import styles from '../index.styl';
import { convertToImperial, convertToMetric } from '../calculate';
import { convertAllPresetsUnits, convertToImperial, convertToMetric } from '../calculate';
import { IMPERIAL_UNITS, METRIC_UNITS } from '../../../constants';

export default class JoggingPresets extends Component {
Expand Down Expand Up @@ -286,7 +286,11 @@ export default class JoggingPresets extends Component {

store.replace('widgets.axes.jog', defaultJogPresets);
this.setState({ jogSpeeds: this.getJogSpeeds() });
pubsub.publish('jogSpeeds', defaultJogPresets);

// convert before publishing
const { units } = this.state;
let convertedDefaults = units === IMPERIAL_UNITS ? convertAllPresetsUnits(units, defaultJogPresets) : defaultJogPresets;
pubsub.publish('jogSpeeds', convertedDefaults);

this.showToast();
}
Expand Down
22 changes: 22 additions & 0 deletions src/app/containers/Preferences/calculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* of Sienci Labs Inc. in Waterloo, Ontario, Canada.
*
*/
import { METRIC_UNITS } from '../../constants';

const CALC_UNIT = 25.4;

Expand All @@ -30,3 +31,24 @@ export const convertToImperial = (val) => {
export const convertToMetric = (val) => {
return Number((val * CALC_UNIT).toFixed(2));
};

export const convertPresetUnits = (units, preset) => {
const conversionFunc = units === METRIC_UNITS ? convertToMetric : convertToImperial;
let convertedPreset = JSON.parse(JSON.stringify(preset));
for (const key of Object.keys(preset)) {
convertedPreset[key] = conversionFunc(preset[key]);
if (key === 'feedrate') {
convertedPreset[key] = Number(convertedPreset[key]).toFixed(0);
}
}
return convertedPreset;
};

export const convertAllPresetsUnits = (units, jog) => {
const { rapid, normal, precise } = jog;
const convertedRapid = convertPresetUnits(units, rapid);
const convertedNormal = convertPresetUnits(units, normal);
const convertedPrecise = convertPresetUnits(units, precise);

return { rapid: convertedRapid, normal: convertedNormal, precise: convertedPrecise };
};
106 changes: 21 additions & 85 deletions src/app/widgets/JogControl/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ import styles from './index.styl';
import useKeybinding from '../../lib/useKeybinding';
import { JoystickLoop, checkThumbsticskAreIdle } from './JoystickLoop';
import { MPGHelper } from './MPGHelper';
import { convertToImperial, convertToMetric } from '../../containers/Preferences/calculate';
import { convertAllPresetsUnits, convertToImperial, convertToMetric } from '../../containers/Preferences/calculate';

class AxesWidget extends PureComponent {
static propTypes = {
Expand Down Expand Up @@ -422,18 +422,8 @@ class AxesWidget extends PureComponent {
pubsub.publish('jogSpeeds', { xyStep, zStep, feedrate });
},
setJogFromPreset: (presetKey) => {
const { jog, units, needsConversion } = this.state;
const jogObj = jog[presetKey];

const presetNeedsConversion = needsConversion[presetKey];

if (units === IMPERIAL_UNITS && presetNeedsConversion) {
jogObj.zStep = convertToImperial(jogObj.zStep);
jogObj.xyStep = convertToImperial(jogObj.xyStep);
jogObj.aStep = convertToImperial(jogObj.aStep);
jogObj.feedrate = Number(convertToImperial(jogObj.feedrate).toFixed(0));
this.setState(prev => ({ needsConversion: { ...prev.needsConversion, [presetKey]: false } }));
}
const { jog } = this.state;
const jogObj = jog[presetKey]; // since the state is already converted to the correct units, we don't need to do that here

this.setState({
jog: {
Expand Down Expand Up @@ -964,25 +954,27 @@ class AxesWidget extends PureComponent {
shuttleControl = null;

updateJogPresets = () => {
const { jog } = this.state;
const { jog, units, selectedSpeed } = this.state;
const data = store.get('widgets.axes.jog');

if (!data) {
return;
}

const { rapid, normal, precise } = data;
// convert store data if necessary
const jogObj = units === IMPERIAL_UNITS ? convertAllPresetsUnits(units, store.get('widgets.axes.jog')) : data;

if (jog.rapid === rapid && jog.normal === normal && jog.precise === precise) {
// if the same as our current state, dont bother changing anything
if (jog.rapid === jogObj.rapid && jog.normal === jogObj.normal && jog.precise === jogObj.precise) {
return;
}

// change rapid, normal, precise and the currently displayed values
this.setState({
jog: {
...jog,
rapid,
normal,
precise
...jogObj,
...jogObj[selectedSpeed]
}
});
}
Expand Down Expand Up @@ -1254,31 +1246,12 @@ class AxesWidget extends PureComponent {
}
}

convertPresetUnits(units, preset) {
const conversionFunc = units === METRIC_UNITS ? convertToMetric : convertToImperial;
let convertedPreset = JSON.parse(JSON.stringify(preset));
for (const key of Object.keys(preset)) {
convertedPreset[key] = conversionFunc(preset[key]);
if (key === 'feedrate') {
convertedPreset[key] = Number(convertedPreset[key]).toFixed(0);
}
}
return convertedPreset;
}

convertAllPresetsUnits(units, initJog) {
const jog = initJog || this.state.jog;
const { rapid, normal, precise } = jog;
const convertedRapid = this.convertPresetUnits(units, rapid);
const convertedNormal = this.convertPresetUnits(units, normal);
const convertedPrecise = this.convertPresetUnits(units, precise);

return { convertedRapid, convertedNormal, convertedPrecise };
}

getInitialState() {
const initialUnits = store.get('workspace.units', METRIC_UNITS);
let { rapid, normal, precise } = initialUnits === IMPERIAL_UNITS ? this.convertAllPresetsUnits(initialUnits, store.get('widgets.axes.jog')) : store.get('widgets.axes.jog');
// convert to imperial if necessary.
// we keep track of the current values converted to the correct units,
// but the store will always be metric.
const { rapid, normal, precise } = initialUnits === IMPERIAL_UNITS ? convertAllPresetsUnits(initialUnits, store.get('widgets.axes.jog')) : store.get('widgets.axes.jog');

return {
minimized: this.config.get('minimized', false),
Expand Down Expand Up @@ -1310,11 +1283,11 @@ class AxesWidget extends PureComponent {
c: '0.000'
},
jog: {
xyStep: this.getInitialXYStep(),
ayStep: this.getInitialXAStep(),
zStep: this.getInitialZStep(),
aStep: this.getInitialAStep(),
feedrate: this.getInitialFeedRate(),
xyStep: normal.xyStep, // use normal as the default display
ayStep: normal.ayStep,
zStep: normal.zStep,
aStep: normal.aStep,
feedrate: normal.feedrate,
rapid,
normal,
precise,
Expand All @@ -1325,46 +1298,9 @@ class AxesWidget extends PureComponent {
},
prevJog: null,
prevDirection: null,
needsConversion: initialUnits === IMPERIAL_UNITS
? { rapid: true, normal: true, precise: true, }
: { rapid: false, normal: false, precise: false, }
};
}

getInitialXYStep() {
const units = store.get('workspace.units', METRIC_UNITS);
const speeds = this.config.get('jog.normal');

return (units === METRIC_UNITS) ? speeds.xyStep : convertToImperial(speeds.xyStep);
}

getInitialXAStep() {
const units = store.get('workspace.units', METRIC_UNITS);
const speeds = this.config.get('jog.normal');

return (units === METRIC_UNITS) ? speeds.xaStep : convertToImperial(speeds.xaStep);
}

getInitialZStep() {
const units = store.get('workspace.units', METRIC_UNITS);
const speeds = this.config.get('jog.normal');

return (units === METRIC_UNITS) ? speeds.zStep : convertToImperial(speeds.zStep);
}

getInitialAStep() {
const units = store.get('workspace.units', METRIC_UNITS);
const speeds = this.config.get('jog.normal');

return (units === METRIC_UNITS) ? speeds.aStep : convertToImperial(speeds.aStep);
}

getInitialFeedRate() {
const units = store.get('workspace.units', METRIC_UNITS);
const speeds = this.config.get('jog.normal');

return (units === METRIC_UNITS) ? speeds.feedrate : convertToImperial(speeds.feedrate);
}

changeUnits(units) {
const oldUnits = this.state.units;
Expand All @@ -1383,7 +1319,7 @@ class AxesWidget extends PureComponent {
feedrate = Number(convertToMetric(feedrate).toFixed(0));
}

const { rapid, normal, precise } = this.convertAllPresetsUnits(units);
const { rapid, normal, precise } = convertAllPresetsUnits(units, jog);

this.setState({
units: units,
Expand Down

0 comments on commit 5bd0fb3

Please sign in to comment.