Skip to content

Commit

Permalink
Unit tracking enhancement (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Sep 26, 2017
1 parent 0cb6c1a commit 6f11c9a
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 165 deletions.
45 changes: 31 additions & 14 deletions src/app/controllers/Grbl/GrblController.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,20 +579,20 @@ class GrblController {
this.controller.getWorkPosition(this.controller.state)
);

// Grbl state
if (this.state !== this.controller.state) {
this.state = this.controller.state;
this.emit('controller:state', GRBL, this.state);
this.emit('Grbl:state', this.state); // Backward compatibility
}

// Grbl settings
if (this.settings !== this.controller.settings) {
this.settings = this.controller.settings;
this.emit('controller:settings', GRBL, this.settings);
this.emit('Grbl:settings', this.settings); // Backward compatibility
}

// Grbl state
if (this.state !== this.controller.state) {
this.state = this.controller.state;
this.emit('controller:state', GRBL, this.state);
this.emit('Grbl:state', this.state); // Backward compatibility
}

// Check the ready flag
if (!(this.ready)) {
// Wait for the bootloader to complete before sending commands
Expand Down Expand Up @@ -677,8 +677,8 @@ class GrblController {
ready: this.ready,
controller: {
type: this.type,
state: this.state,
settings: this.settings
settings: this.settings,
state: this.state
},
workflowState: this.workflow.state,
feeder: this.feeder.toJSON(),
Expand Down Expand Up @@ -813,16 +813,16 @@ class GrblController {
inuse: true
});
}
if (!_.isEmpty(this.state)) {
// controller state
socket.emit('controller:state', GRBL, this.state);
socket.emit('Grbl:state', this.state); // Backward compatibility
}
if (!_.isEmpty(this.settings)) {
// controller settings
socket.emit('controller:settings', GRBL, this.settings);
socket.emit('Grbl:settings', this.settings); // Backward compatibility
}
if (!_.isEmpty(this.state)) {
// controller state
socket.emit('controller:state', GRBL, this.state);
socket.emit('Grbl:state', this.state); // Backward compatibility
}
if (this.workflow) {
// workflow state
socket.emit('workflow:state', this.workflow.state);
Expand Down Expand Up @@ -1166,6 +1166,23 @@ class GrblController {
this.emit('serialport:write', data, context);
this.serialport.write(data);
log.silly(`> ${data}`);

// Grbl settings: $0-$255
const r = cmd.match(/^(\$\d{1,3})=([\d\.]+)$/);
if (r) {
const name = r[1];
const value = Number(r[2]);
if ((name === '$13') && (value >= 0) && (value <= 65535)) {
const nextSettings = {
...this.controller.settings,
settings: {
...this.controller.settings.settings,
[name]: value ? '1' : '0'
}
};
this.controller.settings = nextSettings; // enforce change
}
}
}
writeln(data, context) {
if (_.includes(GRBL_REALTIME_COMMANDS, data)) {
Expand Down
28 changes: 14 additions & 14 deletions src/app/controllers/Smoothie/SmoothieController.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,20 +521,20 @@ class SmoothieController {
this.controller.getWorkPosition(this.controller.state)
);

// Smoothie state
if (this.state !== this.controller.state) {
this.state = this.controller.state;
this.emit('controller:state', SMOOTHIE, this.state);
this.emit('Smoothie:state', this.state); // Backward compatibility
}

// Smoothie settings
if (this.settings !== this.controller.settings) {
this.settings = this.controller.settings;
this.emit('controller:settings', SMOOTHIE, this.settings);
this.emit('Smoothie:settings', this.settings); // Backward compatibility
}

// Smoothie state
if (this.state !== this.controller.state) {
this.state = this.controller.state;
this.emit('controller:state', SMOOTHIE, this.state);
this.emit('Smoothie:state', this.state); // Backward compatibility
}

// Check the ready flag
if (!(this.ready)) {
// Wait for the bootloader to complete before sending commands
Expand Down Expand Up @@ -696,8 +696,8 @@ class SmoothieController {
ready: this.ready,
controller: {
type: this.type,
state: this.state,
settings: this.settings
settings: this.settings,
state: this.state
},
workflowState: this.workflow.state,
feeder: this.feeder.toJSON(),
Expand Down Expand Up @@ -832,16 +832,16 @@ class SmoothieController {
inuse: true
});
}
if (!_.isEmpty(this.state)) {
// controller state
socket.emit('controller:state', SMOOTHIE, this.state);
socket.emit('Smoothie:state', this.state); // Backward compatibility
}
if (!_.isEmpty(this.settings)) {
// controller settings
socket.emit('controller:settings', SMOOTHIE, this.settings);
socket.emit('Smoothie:settings', this.settings); // Backward compatibility
}
if (!_.isEmpty(this.state)) {
// controller state
socket.emit('controller:state', SMOOTHIE, this.state);
socket.emit('Smoothie:state', this.state); // Backward compatibility
}
if (this.workflow) {
// workflow state
socket.emit('workflow:state', this.workflow.state);
Expand Down
26 changes: 13 additions & 13 deletions src/app/controllers/TinyG/TinyGController.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,20 +467,20 @@ class TinyGController {
this.controller.getWorkPosition(this.controller.state)
);

// TinyG state
if (this.state !== this.controller.state) {
this.state = this.controller.state;
this.emit('controller:state', TINYG, this.state);
this.emit('TinyG:state', this.state); // Backward compatibility
}

// TinyG settings
if (this.settings !== this.controller.settings) {
this.settings = this.controller.settings;
this.emit('controller:settings', TINYG, this.settings);
this.emit('TinyG:settings', this.settings); // Backward compatibility
}

// TinyG state
if (this.state !== this.controller.state) {
this.state = this.controller.state;
this.emit('controller:state', TINYG, this.state);
this.emit('TinyG:state', this.state); // Backward compatibility
}

// Check the ready flag
if (!(this.ready)) {
// Wait for the bootloader to complete before sending commands
Expand Down Expand Up @@ -709,8 +709,8 @@ class TinyGController {
ready: this.ready,
controller: {
type: this.type,
state: this.state,
settings: this.settings,
state: this.state,
footer: this.controller.footer
},
workflowState: this.workflow.state,
Expand Down Expand Up @@ -846,16 +846,16 @@ class TinyGController {
inuse: true
});
}
if (!_.isEmpty(this.state)) {
// controller state
socket.emit('controller:state', TINYG, this.state);
socket.emit('TinyG:state', this.state); // Backward compatibility
}
if (!_.isEmpty(this.settings)) {
// controller settings
socket.emit('controller:settings', TINYG, this.settings);
socket.emit('TinyG:settings', this.settings); // Backward compatibility
}
if (!_.isEmpty(this.state)) {
// controller state
socket.emit('controller:state', TINYG, this.state);
socket.emit('TinyG:state', this.state); // Backward compatibility
}
if (this.workflow) {
// workflow state
socket.emit('workflow:state', this.workflow.state);
Expand Down
118 changes: 66 additions & 52 deletions src/web/widgets/Axes/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,26 @@ class AxesWidget extends PureComponent {
});
}
},
'controller:state': (type, state) => {
'controller:settings': (type, controllerSettings) => {
this.setState(state => ({
controller: {
...state.controller,
type: type,
settings: controllerSettings
}
}));
},
'controller:state': (type, controllerState) => {
// Grbl
if (type === GRBL) {
const { status, parserstate } = { ...state };
const { status, parserstate } = { ...controllerState };
const { mpos, wpos } = status;
const { modal = {} } = { ...parserstate };
const units = {
'G20': IMPERIAL_UNITS,
'G21': METRIC_UNITS
}[modal.units] || this.state.units;
const $13 = Number(get(controller.settings, 'settings.$13', 0)) || 0;

let customDistance = this.config.get('jog.customDistance');
if (units === IMPERIAL_UNITS) {
Expand All @@ -355,27 +365,34 @@ class AxesWidget extends PureComponent {
customDistance = Number(customDistance).toFixed(3) * 1;
}

this.setState({
this.setState(state => ({
units: units,
controller: {
...state.controller,
type: type,
state: state
state: controllerState
},
machinePosition: {
...this.state.machinePosition,
// Machine position are reported in mm ($13=0) or inches ($13=1)
machinePosition: mapValues({
...state.machinePosition,
...mpos
},
workPosition: {
...this.state.workPosition,
}, (val) => {
return ($13 > 0) ? in2mm(val) : val;
}),
// Work position are reported in mm ($13=0) or inches ($13=1)
workPosition: mapValues({
...state.workPosition,
...wpos
},
}, val => {
return ($13 > 0) ? in2mm(val) : val;
}),
customDistance: customDistance
});
}));
}

// Smoothie
if (type === SMOOTHIE) {
const { status, parserstate } = { ...state };
const { status, parserstate } = { ...controllerState };
const { mpos, wpos } = status;
const { modal = {} } = { ...parserstate };
const units = {
Expand All @@ -391,36 +408,34 @@ class AxesWidget extends PureComponent {
customDistance = Number(customDistance).toFixed(3) * 1;
}

// Machine position are reported in current units
const machinePosition = mapValues({
...this.state.machinePosition,
...mpos
}, (val) => {
return (units === IMPERIAL_UNITS) ? in2mm(val) : val;
});
// Work position are reported in current units
const workPosition = mapValues({
...this.state.workPosition,
...wpos
}, (val) => {
return (units === IMPERIAL_UNITS) ? in2mm(val) : val;
});

this.setState({
this.setState(state => ({
units: units,
controller: {
...state.controller,
type: type,
state: state
state: controllerState
},
machinePosition: machinePosition,
workPosition: workPosition,
// Machine position are reported in current units
machinePosition: mapValues({
...state.machinePosition,
...mpos
}, (val) => {
return (units === IMPERIAL_UNITS) ? in2mm(val) : val;
}),
// Work position are reported in current units
workPosition: mapValues({
...state.workPosition,
...wpos
}, (val) => {
return (units === IMPERIAL_UNITS) ? in2mm(val) : val;
}),
customDistance: customDistance
});
}));
}

// TinyG
if (type === TINYG) {
const { sr } = { ...state };
const { sr } = { ...controllerState };
const { mpos, wpos, modal = {} } = sr;
const units = {
'G20': IMPERIAL_UNITS,
Expand All @@ -435,30 +450,28 @@ class AxesWidget extends PureComponent {
customDistance = Number(customDistance).toFixed(3) * 1;
}

// https://github.com/synthetos/g2/wiki/Status-Reports
// Canonical machine position are always reported in millimeters with no offsets.
const machinePosition = {
...this.state.machinePosition,
...mpos
};
// Work position are reported in current units, and also apply any offsets.
const workPosition = mapValues({
...this.state.workPosition,
...wpos
}, (val) => {
return (units === IMPERIAL_UNITS) ? in2mm(val) : val;
});

this.setState({
this.setState(state => ({
units: units,
controller: {
...state.controller,
type: type,
state: state
state: controllerState
},
machinePosition: machinePosition,
workPosition: workPosition,
// https://github.com/synthetos/g2/wiki/Status-Reports
// Canonical machine position are always reported in millimeters with no offsets.
machinePosition: {
...state.machinePosition,
...mpos
},
// Work position are reported in current units, and also apply any offsets.
workPosition: mapValues({
...state.workPosition,
...wpos
}, (val) => {
return (units === IMPERIAL_UNITS) ? in2mm(val) : val;
}),
customDistance: customDistance
});
}));
}
}
};
Expand Down Expand Up @@ -503,6 +516,7 @@ class AxesWidget extends PureComponent {
units: METRIC_UNITS,
controller: {
type: controller.type,
settings: controller.settings,
state: controller.state
},
modal: {
Expand Down
Loading

0 comments on commit 6f11c9a

Please sign in to comment.