Skip to content

Commit

Permalink
Support for keeping motors energized indefinitely (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Jun 21, 2017
1 parent ff29fa6 commit a48de34
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 12 deletions.
44 changes: 40 additions & 4 deletions src/app/controllers/TinyG/TinyG.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
class TinyGParser {
parse(data) {
const parsers = [
TinyGParserResultMotorTimeout,
TinyGParserResultPowerManagement,
TinyGParserResultQueueReports,
TinyGParserResultStatusReports,
Expand Down Expand Up @@ -78,10 +79,31 @@ class TinyGParser {
}
}

class TinyGParserResultMotorTimeout {
static parse(data) {
const mt = _.get(data, 'r.mt');
if (typeof mt === 'undefined') {
return null;
}

const footer = _.get(data, 'f') || [];
const statusCode = footer[1];
const payload = {};
if (mt && statusCode === 0) {
payload.mt = mt;
}

return {
type: TinyGParserResultMotorTimeout,
payload: payload
};
}
}

// https://github.com/synthetos/TinyG/wiki/Power-Management
class TinyGParserResultPowerManagement {
static parse(data) {
const pwr = _.get(data, 'r.pwr') || _.get(data, 'pwr');
const pwr = _.get(data, 'r.pwr');
if (typeof pwr === 'undefined') {
return null;
}
Expand Down Expand Up @@ -210,9 +232,12 @@ class TinyGParserResultReceiveReports {

class TinyG extends events.EventEmitter {
state = {
// Motor Timeout
mt: 0,
// Power Management
// {"pwr":{"1":0,"2":0,"3":0,"4":0}}
pwr: {},
pwr: {
// {"1":0,"2":0,"3":0,"4":0}
},
// Queue Reports
qr: 0,
// Status Reports
Expand Down Expand Up @@ -280,7 +305,18 @@ class TinyG extends events.EventEmitter {
const result = this.parser.parse(data) || {};
const { type, payload } = result;

if (type === TinyGParserResultPowerManagement) {
if (type === TinyGParserResultMotorTimeout) {
const { mt = this.state.mt } = payload;

if (this.state.mt !== mt) {
this.state = { // enforce change
...this.state,
mt: mt
};
}

this.emit('mt', payload.mt);
} else if (type === TinyGParserResultPowerManagement) {
const { pwr = this.state.pwr } = payload;

if (!_.isEqual(this.state.pwr, pwr)) {
Expand Down
53 changes: 53 additions & 0 deletions src/app/controllers/TinyG/TinyGController.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class TinyGController {
// Workflow
workflow = null;

// Power Management
motorEnergizeTimer = null;
motorEnergizeTimeout = null;

dataFilter = (line, context) => {
// Machine position
const {
Expand Down Expand Up @@ -525,6 +529,9 @@ class TinyGController {
// System settings
{ cmd: '{sys:n}' },

// Request motor timeout
{ cmd: '{mt:n}' },

// Request motor states
{ cmd: '{pwr:n}' },

Expand Down Expand Up @@ -983,6 +990,52 @@ class TinyGController {
this.command(socket, 'gcode', '{mto:0.25}');
}
},
'motor:energize': () => {
const { mt = 0 } = this.state;

if (this.motorEnergizeTimer || !mt) {
return;
}

this.command(socket, 'gcode', '{me:0}');
this.command(socket, 'gcode', '{pwr:n}');

// Setup a timer to keep motors energized indefinitely
this.motorEnergizeTimer = setInterval(() => {
this.command(socket, 'gcode', '{me:0}');
this.command(socket, 'gcode', '{pwr:n}');
}, mt * 1000 - 500);

// Set a timeout value so the motors will not run longer than 30 minutes
if (this.motorEnergizeTimeout) {
clearTimeout(this.motorEnergizeTimeout);
this.motorEnergizeTimeout = null;
}
this.motorEnergizeTimeout = setTimeout(() => {
this.motorEnergizeTimeout = null;

if (this.motorEnergizeTimer) {
clearInterval(this.motorEnergizeTimer);
this.motorEnergizeTimer = null;
}

this.command(socket, 'gcode', '{md:0}');
this.command(socket, 'gcode', '{pwr:n}');
}, 30 * 60 * 1000);
},
'motor:deenergize': () => {
if (this.motorEnergizeTimer) {
clearInterval(this.motorEnergizeTimer);
this.motorEnergizeTimer = null;
}
if (this.motorEnergizeTimeout) {
clearTimeout(this.motorEnergizeTimeout);
this.motorEnergizeTimeout = null;
}

this.command(socket, 'gcode', '{md:0}');
this.command(socket, 'gcode', '{pwr:n}');
},
'lasertest:on': () => {
const [power = 0, duration = 0, maxS = 1000] = args;
const commands = [
Expand Down
25 changes: 17 additions & 8 deletions src/web/widgets/TinyG/TinyG.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TinyG extends PureComponent {
const ovF = (fv >= 0.99) ? Math.round(mfo * 100) || 0 : 0;
const ovS = (fv >= 0.98) ? Math.round(sso * 100) || 0 : 0;
const ovT = (fv >= 0.99) ? Math.round(mto * 100) || 0 : 0;
const mt = get(controllerState, 'mt');
const pwr = get(controllerState, 'pwr');
const machineState = get(controllerState, 'sr.machineState');
const machineStateText = {
Expand Down Expand Up @@ -107,12 +108,17 @@ class TinyG extends PureComponent {
</Panel.Heading>
{panel.powerManagement.expanded && pwr &&
<Panel.Body>
<div className="row no-gutters">
<div className="col col-xs-12">
{i18n._('Motor Timeout: {{mt}} sec', { mt: mt })}
</div>
</div>
<div className="row no-gutters" style={{ marginBottom: 10 }}>
<div className="col col-xs-12">
<Button
btnStyle="flat"
onClick={() => {
controller.writeln('{me:0}');
controller.command('motor:energize');
}}
>
<i className="fa fa-flash" />
Expand All @@ -121,7 +127,7 @@ class TinyG extends PureComponent {
<Button
btnStyle="flat"
onClick={() => {
controller.writeln('{md:0}');
controller.command('motor:deenergize');
}}
>
<i className="fa fa-remove" />
Expand All @@ -135,7 +141,14 @@ class TinyG extends PureComponent {
{i18n._('Motor {{n}}', { n: key })}
</div>
<div className="col col-xs-8">
<div className={styles.well}>{value}</div>
<ProgressBar
style={{ marginBottom: 0 }}
bsStyle="info"
min={0}
max={1}
now={value}
label={<span className={styles.progressbarLabel}>{value}</span>}
/>
</div>
</div>
))}
Expand Down Expand Up @@ -169,11 +182,7 @@ class TinyG extends PureComponent {
min={this.plannerBufferMin}
max={this.plannerBufferMax}
now={plannerBuffer}
label={
<span className={styles.progressbarLabel}>
{plannerBuffer}
</span>
}
label={<span className={styles.progressbarLabel}>{plannerBuffer}</span>}
/>
</div>
</div>
Expand Down

0 comments on commit a48de34

Please sign in to comment.