Skip to content

Commit 5fb7640

Browse files
authored
Merge pull request #3 from MitchBradley/master
Units in Grbl mode
2 parents 1ed6ff8 + fa23f6f commit 5fb7640

File tree

1 file changed

+104
-10
lines changed

1 file changed

+104
-10
lines changed

src/app.js

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ controller.on('serialport:open', function(options) {
4646
Cookies.set('cnc.port', port);
4747
Cookies.set('cnc.baudrate', baudrate);
4848

49+
if (controllerType == 'Grbl') {
50+
// Read the settings so we can determine the units for position reports
51+
// This will trigger a Grbl:settings callback to set grblReportingUnits
52+
53+
// This has a problem: The first status report arrives before the
54+
// settings report, so interpreting the numbers from the first status
55+
// report is ambiguous. Subsequent status reports are interpreted correctly.
56+
// We work around that by deferring status reports until the settings report.
57+
controller.writeln('$$');
58+
}
59+
4960
root.location = '#/axes';
5061
});
5162

@@ -151,27 +162,110 @@ controller.on('serialport:read', function(data) {
151162
console.log('%cR%c', style, '', data);
152163
});
153164

165+
// GRBL reports position in units according to the $13 setting,
166+
// independent of the GCode in/mm parser state.
167+
// We track the $13 value by watching for the Grbl:settings event and by
168+
// watching for manual changes via serialport:write. Upon initial connection,
169+
// we issue a settings request in serialport:open.
170+
var grblReportingUnits; // initially undefined
171+
154172
controller.on('serialport:write', function(data) {
155173
var style = 'font-weight: bold; line-height: 20px; padding: 2px 4px; border: 1px solid; color: #00529B; background: #BDE5F8';
156174
console.log('%cW%c', style, '', data);
175+
176+
// Track manual changes to the Grbl position reporting units setting
177+
// We are looking for either $13=0 or $13=1
178+
if (cnc.controllerType == 'Grbl') {
179+
cmd = data.split('=');
180+
if (cmd.length == 2 && cmd[0] == "$13") {
181+
grblReportingUnits = cmd[1];
182+
}
183+
}
157184
});
158185

159-
controller.on('Grbl:state', function(data) {
186+
// This is a copy of the Grbl:state report that came in before the Grbl:settings report
187+
var savedGrblState;
188+
189+
function renderGrblState(data) {
160190
var status = data.status || {};
161191
var activeState = status.activeState;
162192
var mpos = status.mpos;
163193
var wpos = status.wpos;
164194
var IDLE = 'Idle', RUN = 'Run';
165195
var canClick = [IDLE, RUN].indexOf(activeState) >= 0;
166196

197+
var parserstate = data.parserstate || {};
198+
199+
// Unit conversion factor - depends on both $13 setting and parser units
200+
var factor = 1.0;
201+
// Number of postdecimal digits to display; 3 for in, 4 for mm
202+
var digits = 4;
203+
204+
var mlabel = 'MPos:';
205+
var wlabel = 'WPos:';
206+
207+
switch (parserstate.modal.units) {
208+
case 'G20':
209+
mlabel = 'MPos (in):';
210+
wlabel = 'WPos (in):';
211+
digits = 4;
212+
factor = grblReportingUnits == 0 ? 1/25.4 : 1.0 ;
213+
break;
214+
case 'G21':
215+
mlabel = 'MPos (mm):';
216+
wlabel = 'WPos (mm):';
217+
digits = 3;
218+
factor = grblReportingUnits == 0 ? 1.0 : 25.4;
219+
break;
220+
}
221+
222+
console.log(grblReportingUnits, factor);
223+
224+
mpos.x = (mpos.x * factor).toFixed(digits);
225+
mpos.y = (mpos.y * factor).toFixed(digits);
226+
mpos.z = (mpos.z * factor).toFixed(digits);
227+
228+
wpos.x = (wpos.x * factor).toFixed(digits);
229+
wpos.y = (wpos.y * factor).toFixed(digits);
230+
wpos.z = (wpos.y * factor).toFixed(digits);
231+
167232
$('[data-route="axes"] .control-pad .btn').prop('disabled', !canClick);
168233
$('[data-route="axes"] [data-name="active-state"]').text(activeState);
234+
$('[data-route="axes"] [data-name="mpos-label"]').text(mlabel);
169235
$('[data-route="axes"] [data-name="mpos-x"]').text(mpos.x);
170236
$('[data-route="axes"] [data-name="mpos-y"]').text(mpos.y);
171237
$('[data-route="axes"] [data-name="mpos-z"]').text(mpos.z);
238+
$('[data-route="axes"] [data-name="wpos-label"]').text(wlabel);
172239
$('[data-route="axes"] [data-name="wpos-x"]').text(wpos.x);
173240
$('[data-route="axes"] [data-name="wpos-y"]').text(wpos.y);
174241
$('[data-route="axes"] [data-name="wpos-z"]').text(wpos.z);
242+
}
243+
244+
controller.on('Grbl:state', function(data) {
245+
// If we do not yet know the reporting units from the $13 setting, we copy
246+
// the data for later processing when we do know.
247+
if (typeof grblReportingUnits == 'undefined') {
248+
console.log("DEFER");
249+
savedGrblState = JSON.parse(JSON.stringify(data));
250+
} else {
251+
console.log("STATE");
252+
renderGrblState(data);
253+
}
254+
});
255+
256+
controller.on('Grbl:settings', function(data) {
257+
var settings = data.settings || {};
258+
console.log("SETTINGS");
259+
if (settings['$13'] != undefined) {
260+
grblReportingUnits = settings['$13'];
261+
262+
if (typeof savedGrblState != 'undefined') {
263+
renderGrblState(savedGrblState);
264+
// Don't re-render the state if we get later settings reports,
265+
// as the savedGrblState is probably stale.
266+
savedGrblState = undefined;
267+
}
268+
}
175269
});
176270

177271
controller.on('Smoothie:state', function(data) {
@@ -219,21 +313,21 @@ controller.on('TinyG:state', function(data) {
219313
case 'G20':
220314
mlabel = 'MPos (in):';
221315
wlabel = 'WPos (in):';
222-
// TinyG reports machine coordinates in mm regardless of the in/mm mode
223-
mpos.x = (mpos.x / 25.4).toFixed(4);
224-
mpos.y = (mpos.y / 25.4).toFixed(4);
225-
mpos.z = (mpos.z / 25.4).toFixed(4);
226-
// TinyG reports work coordinates according to the in/mm mode
316+
// TinyG reports machine coordinates in mm regardless of the in/mm mode
317+
mpos.x = (mpos.x / 25.4).toFixed(4);
318+
mpos.y = (mpos.y / 25.4).toFixed(4);
319+
mpos.z = (mpos.z / 25.4).toFixed(4);
320+
// TinyG reports work coordinates according to the in/mm mode
227321
wpos.x = Number(wpos.x).toFixed(4);
228322
wpos.y = Number(wpos.y).toFixed(4);
229323
wpos.z = Number(wpos.z).toFixed(4);
230-
break;
324+
break;
231325
case 'G21':
232326
mlabel = 'MPos (mm):';
233327
wlabel = 'WPos (mm):';
234-
mpos.x = Number(mpos.x).toFixed(3);
235-
mpos.y = Number(mpos.y).toFixed(3);
236-
mpos.z = Number(mpos.z).toFixed(3);
328+
mpos.x = Number(mpos.x).toFixed(3);
329+
mpos.y = Number(mpos.y).toFixed(3);
330+
mpos.z = Number(mpos.z).toFixed(3);
237331
wpos.x = Number(wpos.x).toFixed(3);
238332
wpos.y = Number(wpos.y).toFixed(3);
239333
wpos.z = Number(wpos.z).toFixed(3);

0 commit comments

Comments
 (0)