Skip to content

Commit

Permalink
Add a new event that is triggered when the controller is ready to sta…
Browse files Browse the repository at this point in the history
…rt (#437) (#460)

* Update translation keys

* Add a new event that is triggered when the controller is ready to start (#437)
  • Loading branch information
cheton committed Apr 20, 2019
1 parent e45ef3e commit ae7f547
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/app/containers/Settings/Events/CreateRecord.jsx
Expand Up @@ -134,6 +134,7 @@ class CreateRecord extends PureComponent {
<option value="startup">{i18n._('Startup (System only)')}</option>
<option value="port:open">{i18n._('Open a serial port (System only)')}</option>
<option value="port:close">{i18n._('Close a serial port (System only)')}</option>
<option value="controller:ready">{i18n._('Ready to start')}</option>
<option value="gcode:load">{i18n._('G-code: Load')}</option>
<option value="gcode:unload">{i18n._('G-code: Unload')}</option>
<option value="gcode:start">{i18n._('G-code: Start')}</option>
Expand Down
7 changes: 4 additions & 3 deletions src/app/containers/Settings/Events/TableRecords.jsx
Expand Up @@ -112,9 +112,10 @@ class TableRecords extends PureComponent {
key: 'event',
render: (value, row, index) => {
const eventText = {
'startup': i18n._('Startup'),
'port:open': i18n._('Open a serial port'),
'port:close': i18n._('Close a serial port'),
'startup': i18n._('Startup (System only)'),
'port:open': i18n._('Open a serial port (System only)'),
'port:close': i18n._('Close a serial port (System only)'),
'controller:ready': i18n._('Ready to start'),
'gcode:load': i18n._('G-code: Load'),
'gcode:unload': i18n._('G-code: Unload'),
'gcode:start': i18n._('G-code: Start'),
Expand Down
1 change: 1 addition & 0 deletions src/app/containers/Settings/Events/UpdateRecord.jsx
Expand Up @@ -134,6 +134,7 @@ class UpdateRecord extends PureComponent {
<option value="startup">{i18n._('Startup (System only)')}</option>
<option value="port:open">{i18n._('Open a serial port (System only)')}</option>
<option value="port:close">{i18n._('Close a serial port (System only)')}</option>
<option value="controller:ready">{i18n._('Ready to start')}</option>
<option value="gcode:load">{i18n._('G-code: Load')}</option>
<option value="gcode:unload">{i18n._('G-code: Unload')}</option>
<option value="gcode:start">{i18n._('G-code: Start')}</option>
Expand Down
12 changes: 6 additions & 6 deletions src/package.json
Expand Up @@ -12,20 +12,20 @@
},
"main": "main.js",
"dependencies": {
"@babel/polyfill": "~7.4.0",
"@babel/runtime": "~7.4.2",
"@babel/polyfill": "~7.4.3",
"@babel/runtime": "~7.4.3",
"bcrypt-nodejs": "0.0.3",
"body-parser": "~1.18.3",
"chalk": "~2.4.2",
"commander": "~2.19.0",
"commander": "~2.20.0",
"compression": "~1.7.4",
"connect-multiparty": "~2.2.0",
"connect-restreamer": "~1.0.3",
"consolidate": "~0.15.1",
"cookie-parser": "~1.4.4",
"debug": "~4.1.1",
"deep-keys": "~0.5.0",
"electron-store": "~2.0.0",
"electron-store": "~3.1.0",
"ensure-array": "~1.0.0",
"errorhandler": "~1.5.0",
"escodegen": "~1.11.1",
Expand All @@ -37,7 +37,7 @@
"gcode-parser": "~1.3.6",
"hogan.js": "~3.0.2",
"http-proxy": "~1.17.0",
"i18next": "~15.0.8",
"i18next": "~15.0.9",
"i18next-express-middleware": "~1.7.3",
"i18next-node-fs-backend": "~2.1.2",
"is-electron": "~2.2.0",
Expand All @@ -48,7 +48,7 @@
"morgan": "~1.9.1",
"range_check": "~1.4.0",
"registry-auth-token": "~3.4.0",
"registry-url": "~5.0.0",
"registry-url": "~5.1.0",
"rimraf": "~2.6.3",
"serialport": "~7.1.4",
"serve-favicon": "~2.5.0",
Expand Down
31 changes: 18 additions & 13 deletions src/server/controllers/Grbl/GrblController.js
Expand Up @@ -543,22 +543,19 @@ class GrblController {
this.runner.on('startup', (res) => {
this.emit('serialport:read', res.raw);

// Check the initialized flag
// The startup message always prints upon startup, after a reset, or at program end.
// Setting the initial state when Grbl has completed re-initializing all systems.
this.clearActionValues();

// Set ready flag to true when a startup message has arrived
this.ready = true;

if (!this.initialized) {
this.initialized = true;

// https://github.com/cncjs/cncjs/issues/206
// $13=0 (report in mm)
// $13=1 (report in inches)
this.writeln('$$');
// Initialize controller
this.initController();
}

// Set the ready flag to true when a startup message has arrived
this.ready = true;

// The startup message always prints upon startup, after a reset, or at program end.
// Setting the initial state when Grbl has completed re-initializing all systems.
this.clearActionValues();
});

this.runner.on('others', (res) => {
Expand Down Expand Up @@ -673,7 +670,6 @@ class GrblController {

// Check the ready flag
if (!(this.ready)) {
// Wait for the bootloader to complete before sending commands
return;
}

Expand Down Expand Up @@ -704,6 +700,15 @@ class GrblController {
}
}, 250);
}
async initController() {
// https://github.com/cncjs/cncjs/issues/206
// $13=0 (report in mm)
// $13=1 (report in inches)
this.writeln('$$');

await delay(50);
this.event.trigger('controller:ready');
}
populateContext(context) {
// Machine position
const {
Expand Down
24 changes: 18 additions & 6 deletions src/server/controllers/Marlin/MarlinController.js
Expand Up @@ -10,6 +10,7 @@ import Workflow, {
WORKFLOW_STATE_PAUSED,
WORKFLOW_STATE_RUNNING
} from '../../lib/Workflow';
import delay from '../../lib/delay';
import ensurePositiveNumber from '../../lib/ensure-positive-number';
import evaluateAssignmentExpression from '../../lib/evaluate-assignment-expression';
import logger from '../../lib/logger';
Expand Down Expand Up @@ -81,6 +82,7 @@ class MarlinController {
// Marlin
controller = null;
ready = false;
initialized = false;
state = {};
settings = {};
feedOverride = 100;
Expand Down Expand Up @@ -560,12 +562,16 @@ class MarlinController {
this.runner.on('start', (res) => {
this.emit('serialport:read', res.raw);

// M115: Get Firmware Version and Capabilities
this.command('gcode', 'M115');

// Set ready flag to true when receiving a start message
// Note: It might have chance of receiving garbage characters on startup due to electronic noise.
// Set ready flag to true when a start message has arrived
// It might have chance of receiving garbage characters on startup due to electronic noise.
this.ready = true;

if (!this.initialized) {
this.initialized = true;

// Initialize controller
this.initController();
}
});

this.runner.on('echo', (res) => {
Expand Down Expand Up @@ -715,7 +721,6 @@ class MarlinController {

// Check the ready flag
if (!(this.ready)) {
// Wait for the bootloader to complete before sending commands
return;
}

Expand Down Expand Up @@ -756,6 +761,13 @@ class MarlinController {
}
}, 250);
}
async initController() {
// M115: Get firmware version and capabilities
this.command('gcode', 'M115');

await delay(50);
this.event.trigger('controller:ready');
}
populateContext(context) {
// Work position
const {
Expand Down
15 changes: 9 additions & 6 deletions src/server/controllers/Smoothie/SmoothieController.js
Expand Up @@ -577,7 +577,6 @@ class SmoothieController {

// Check the ready flag
if (!(this.ready)) {
// Wait for the bootloader to complete before sending commands
return;
}

Expand Down Expand Up @@ -728,13 +727,11 @@ class SmoothieController {
}
}
async initController() {
// Wait for the bootloader to complete before sending commands
await delay(1000);

// Check if it is Smoothieware
this.command('gcode', 'version');

this.ready = true;
await delay(50);
this.event.trigger('controller:ready');
}
get status() {
return {
Expand Down Expand Up @@ -768,7 +765,7 @@ class SmoothieController {
this.connection.on('close', this.connectionEventListener.close);
this.connection.on('error', this.connectionEventListener.error);

this.connection.open((err) => {
this.connection.open(async (err) => {
if (err) {
log.error(`Error opening serial port "${port}":`, err);
this.emit('serialport:error', { err: err, port: port });
Expand Down Expand Up @@ -805,6 +802,12 @@ class SmoothieController {
this.command('unload');
}

// Wait for the bootloader to complete before sending commands
await delay(1000);

// Set ready flag to true
this.ready = true;

// Initialize controller
this.initController();
});
Expand Down
15 changes: 9 additions & 6 deletions src/server/controllers/TinyG/TinyGController.js
Expand Up @@ -618,7 +618,6 @@ class TinyGController {

// Check the ready flag
if (!(this.ready)) {
// Wait for the bootloader to complete before sending commands
return;
}

Expand Down Expand Up @@ -668,9 +667,6 @@ class TinyGController {
return json.replace(/"/g, '').replace(/true/g, 't');
};

// Wait for the bootloader to complete before sending commands
await delay(1000);

// Enable JSON mode
// 0=text mode, 1=JSON mode
send('{ej:1}');
Expand Down Expand Up @@ -733,7 +729,8 @@ class TinyGController {
// Request status report
send('{sr:n}');

this.ready = true;
await delay(50);
this.event.trigger('controller:ready');
}
populateContext(context) {
// Machine position
Expand Down Expand Up @@ -886,7 +883,7 @@ class TinyGController {
this.connection.on('close', this.connectionEventListener.close);
this.connection.on('error', this.connectionEventListener.error);

this.connection.open((err) => {
this.connection.open(async (err) => {
if (err) {
log.error(`Error opening serial port "${port}":`, err);
this.emit('serialport:error', { err: err, port: port });
Expand Down Expand Up @@ -923,6 +920,12 @@ class TinyGController {
this.command('unload');
}

// Wait for the bootloader to complete before sending commands
await delay(1000);

// Set ready flag to true
this.ready = true;

// Initialize controller
this.initController();
});
Expand Down

0 comments on commit ae7f547

Please sign in to comment.