From a76289287935e55db80d357d24f36dd1a4b77b27 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Thu, 19 Jul 2018 15:46:01 -0400 Subject: [PATCH 1/2] Do not send coast if another motor command has been sent --- src/extensions/scratch3_ev3/index.js | 32 +++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index 7171f9777..360a739b8 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -1,6 +1,7 @@ const ArgumentType = require('../../extension-support/argument-type'); const BlockType = require('../../extension-support/block-type'); const Cast = require('../../util/cast'); +const uid = require('../../util/uid'); // const log = require('../../util/log'); const Base64Util = require('../../util/base64-util'); const BTSession = require('../../io/btSession'); @@ -135,7 +136,8 @@ class EV3 { this._motors = { speeds: [50, 50, 50, 50], positions: [0, 0, 0, 0], - busy: [0, 0, 0, 0] + busy: [0, 0, 0, 0], + commandId: [null, null, null, null] }; this._pollingIntervalID = null; this._pollingCounter = 0; @@ -186,7 +188,8 @@ class EV3 { this._motors = { speeds: [50, 50, 50, 50], positions: [0, 0, 0, 0], - busy: [0, 0, 0, 0] + busy: [0, 0, 0, 0], + commandId: [null, null, null, null] }; this._pollingIntervalID = null; } @@ -296,10 +299,7 @@ class EV3 { // Set motor to busy // this._motors.busy[port] = 1; - // Send coast message - setTimeout(() => { - this.motorCoast(port); - }, time); + this.coastAfter(port, time); // Yield for turn time + brake time const coastTime = 100; // TODO: calculate coasting or set flag @@ -331,10 +331,7 @@ class EV3 { // Set motor to busy // this._motors.busy[port] = 1; - // Send coast message - setTimeout(() => { - this.motorCoast(port); - }, time); + this.coastAfter(port, time); // Yield for time const coastTime = 100; // TODO: calculate coasting or set flag @@ -345,6 +342,21 @@ class EV3 { }); } + coastAfter(port, time) { + // Set the motor command id to check before starting coast + const commandId = uid(); + this._motors.commandId[port] = commandId; + + // Send coast message + setTimeout(() => { + // Do not send coast if another motor command changed the command id. + if (this._motors.commandId[port] === commandId) { + this.motorCoast(port); + this._motors.commandId[port] = null; + } + }, time); + } + motorCoast (port) { const cmd = []; From 4570184c6b5cc90be4ad5c8222a9e939ddf2d2b4 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Thu, 19 Jul 2018 16:41:07 -0400 Subject: [PATCH 2/2] Fix linting --- src/extensions/scratch3_ev3/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index 360a739b8..9b1bcba02 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -342,7 +342,7 @@ class EV3 { }); } - coastAfter(port, time) { + coastAfter (port, time) { // Set the motor command id to check before starting coast const commandId = uid(); this._motors.commandId[port] = commandId;