From 1e787f29629fd9e64c0a4b0b838aa772f0e2a8e6 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 8 Jan 2019 13:24:49 -0800 Subject: [PATCH] Added hardStopMotor to motor controls --- DOCS.md | 78 +++++++++++++++++++++++++++++++++++-------- README.md | 78 +++++++++++++++++++++++++++++++++++-------- src/boostmovehub.ts | 11 ++++++ src/duplotrainbase.ts | 41 ++++++++++++++--------- src/hub.ts | 25 ++++++-------- src/puphub.ts | 11 ++++++ src/wedo2smarthub.ts | 11 ++++++ 7 files changed, 200 insertions(+), 55 deletions(-) diff --git a/DOCS.md b/DOCS.md index ee1b98c..1978866 100644 --- a/DOCS.md +++ b/DOCS.md @@ -100,6 +100,7 @@ Emits when a Powered UP Hub device is found. * [.setLEDRGB(red, green, blue)](#WeDo2SmartHub+setLEDRGB) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#WeDo2SmartHub+setMotorSpeed) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#WeDo2SmartHub+rampMotorSpeed) ⇒ Promise + * [.hardStopMotor(port)](#WeDo2SmartHub+hardStopMotor) ⇒ Promise * [.playTone(frequency, time)](#WeDo2SmartHub+playTone) ⇒ Promise * [.setLightBrightness(port, brightness, [time])](#WeDo2SmartHub+setLightBrightness) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise @@ -234,6 +235,18 @@ Ramp the motor speed on a given port. | toSpeed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | | time | number | How long the ramp should last (in milliseconds). | + + +### weDo2SmartHub.hardStopMotor(port) ⇒ Promise +Fully (hard) stop the motor on a given port. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) +**Returns**: Promise - Resolved upon successful completion of command. + +| Param | Type | +| --- | --- | +| port | string | + ### weDo2SmartHub.playTone(frequency, time) ⇒ Promise @@ -446,6 +459,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [.setMotorSpeed(port, speed, [time])](#BoostMoveHub+setMotorSpeed) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#BoostMoveHub+rampMotorSpeed) ⇒ Promise * [.setMotorAngle(port, angle, [speed])](#BoostMoveHub+setMotorAngle) ⇒ Promise + * [.hardStopMotor(port)](#BoostMoveHub+hardStopMotor) ⇒ Promise * [.setLightBrightness(port, brightness, [time])](#BoostMoveHub+setLightBrightness) ⇒ Promise * [.setName(name)](#LPF2Hub+setName) ⇒ Promise * [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ Promise @@ -574,6 +588,18 @@ Rotate a motor by a given angle. | angle | number | | How much the motor should be rotated (in degrees). | | [speed] | number \| Array.<number> | 100 | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. | + + +### boostMoveHub.hardStopMotor(port) ⇒ Promise +Fully (hard) stop the motor on a given port. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Returns**: Promise - Resolved upon successful completion of command. + +| Param | Type | +| --- | --- | +| port | string | + ### boostMoveHub.setLightBrightness(port, brightness, [time]) ⇒ Promise @@ -832,6 +858,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [.batteryLevel](#Hub+batteryLevel) * [.setMotorSpeed(port, speed, [time])](#PUPHub+setMotorSpeed) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#PUPHub+rampMotorSpeed) ⇒ Promise + * [.hardStopMotor(port)](#PUPHub+hardStopMotor) ⇒ Promise * [.setLightBrightness(port, brightness, [time])](#PUPHub+setLightBrightness) ⇒ Promise * [.setName(name)](#LPF2Hub+setName) ⇒ Promise * [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ Promise @@ -945,6 +972,18 @@ Ramp the motor speed on a given port. | toSpeed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | | time | number | How long the ramp should last (in milliseconds). | + + +### pupHub.hardStopMotor(port) ⇒ Promise +Fully (hard) stop the motor on a given port. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Returns**: Promise - Resolved upon successful completion of command. + +| Param | Type | +| --- | --- | +| port | string | + ### pupHub.setLightBrightness(port, brightness, [time]) ⇒ Promise @@ -1439,8 +1478,9 @@ A combined color and distance event, emits when the sensor is activated. * [.batteryLevel](#Hub+batteryLevel) * [.setLEDColor(color)](#DuploTrainBase+setLEDColor) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#DuploTrainBase+setMotorSpeed) ⇒ Promise - * [.playSound(sound)](#DuploTrainBase+playSound) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainBase+rampMotorSpeed) ⇒ Promise + * [.hardStopMotor(port)](#DuploTrainBase+hardStopMotor) ⇒ Promise + * [.playSound(sound)](#DuploTrainBase+playSound) ⇒ Promise * [.setName(name)](#LPF2Hub+setName) ⇒ Promise * [.setLEDRGB(red, green, blue)](#LPF2Hub+setLEDRGB) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise @@ -1546,18 +1586,6 @@ Set the motor speed on a given port. | speed | number \| Array.<number> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. | | [time] | number | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. | - - -### duploTrainBase.playSound(sound) ⇒ Promise -Play a built-in train sound. - -**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) -**Returns**: Promise - Resolved upon successful issuance of command. - -| Param | Type | -| --- | --- | -| sound | [DuploTrainBaseSound](#DuploTrainBaseSound) | - ### duploTrainBase.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ Promise @@ -1573,6 +1601,30 @@ Ramp the motor speed on a given port. | toSpeed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | | time | number | How long the ramp should last (in milliseconds). | + + +### duploTrainBase.hardStopMotor(port) ⇒ Promise +Fully (hard) stop the motor on a given port. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Returns**: Promise - Resolved upon successful completion of command. + +| Param | Type | +| --- | --- | +| port | string | + + + +### duploTrainBase.playSound(sound) ⇒ Promise +Play a built-in train sound. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Returns**: Promise - Resolved upon successful issuance of command. + +| Param | Type | +| --- | --- | +| sound | [DuploTrainBaseSound](#DuploTrainBaseSound) | + ### duploTrainBase.setName(name) ⇒ Promise diff --git a/README.md b/README.md index 2dadfb8..27542a8 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,7 @@ Emits when a Powered UP Hub device is found. * [.setLEDRGB(red, green, blue)](#WeDo2SmartHub+setLEDRGB) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#WeDo2SmartHub+setMotorSpeed) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#WeDo2SmartHub+rampMotorSpeed) ⇒ Promise + * [.hardStopMotor(port)](#WeDo2SmartHub+hardStopMotor) ⇒ Promise * [.playTone(frequency, time)](#WeDo2SmartHub+playTone) ⇒ Promise * [.setLightBrightness(port, brightness, [time])](#WeDo2SmartHub+setLightBrightness) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise @@ -327,6 +328,18 @@ Ramp the motor speed on a given port. | toSpeed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | | time | number | How long the ramp should last (in milliseconds). | + + +### weDo2SmartHub.hardStopMotor(port) ⇒ Promise +Fully (hard) stop the motor on a given port. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) +**Returns**: Promise - Resolved upon successful completion of command. + +| Param | Type | +| --- | --- | +| port | string | + ### weDo2SmartHub.playTone(frequency, time) ⇒ Promise @@ -539,6 +552,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [.setMotorSpeed(port, speed, [time])](#BoostMoveHub+setMotorSpeed) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#BoostMoveHub+rampMotorSpeed) ⇒ Promise * [.setMotorAngle(port, angle, [speed])](#BoostMoveHub+setMotorAngle) ⇒ Promise + * [.hardStopMotor(port)](#BoostMoveHub+hardStopMotor) ⇒ Promise * [.setLightBrightness(port, brightness, [time])](#BoostMoveHub+setLightBrightness) ⇒ Promise * [.setName(name)](#LPF2Hub+setName) ⇒ Promise * [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ Promise @@ -667,6 +681,18 @@ Rotate a motor by a given angle. | angle | number | | How much the motor should be rotated (in degrees). | | [speed] | number \| Array.<number> | 100 | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. | + + +### boostMoveHub.hardStopMotor(port) ⇒ Promise +Fully (hard) stop the motor on a given port. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Returns**: Promise - Resolved upon successful completion of command. + +| Param | Type | +| --- | --- | +| port | string | + ### boostMoveHub.setLightBrightness(port, brightness, [time]) ⇒ Promise @@ -925,6 +951,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [.batteryLevel](#Hub+batteryLevel) * [.setMotorSpeed(port, speed, [time])](#PUPHub+setMotorSpeed) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#PUPHub+rampMotorSpeed) ⇒ Promise + * [.hardStopMotor(port)](#PUPHub+hardStopMotor) ⇒ Promise * [.setLightBrightness(port, brightness, [time])](#PUPHub+setLightBrightness) ⇒ Promise * [.setName(name)](#LPF2Hub+setName) ⇒ Promise * [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ Promise @@ -1038,6 +1065,18 @@ Ramp the motor speed on a given port. | toSpeed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | | time | number | How long the ramp should last (in milliseconds). | + + +### pupHub.hardStopMotor(port) ⇒ Promise +Fully (hard) stop the motor on a given port. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Returns**: Promise - Resolved upon successful completion of command. + +| Param | Type | +| --- | --- | +| port | string | + ### pupHub.setLightBrightness(port, brightness, [time]) ⇒ Promise @@ -1532,8 +1571,9 @@ A combined color and distance event, emits when the sensor is activated. * [.batteryLevel](#Hub+batteryLevel) * [.setLEDColor(color)](#DuploTrainBase+setLEDColor) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#DuploTrainBase+setMotorSpeed) ⇒ Promise - * [.playSound(sound)](#DuploTrainBase+playSound) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainBase+rampMotorSpeed) ⇒ Promise + * [.hardStopMotor(port)](#DuploTrainBase+hardStopMotor) ⇒ Promise + * [.playSound(sound)](#DuploTrainBase+playSound) ⇒ Promise * [.setName(name)](#LPF2Hub+setName) ⇒ Promise * [.setLEDRGB(red, green, blue)](#LPF2Hub+setLEDRGB) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise @@ -1639,18 +1679,6 @@ Set the motor speed on a given port. | speed | number \| Array.<number> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. | | [time] | number | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. | - - -### duploTrainBase.playSound(sound) ⇒ Promise -Play a built-in train sound. - -**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) -**Returns**: Promise - Resolved upon successful issuance of command. - -| Param | Type | -| --- | --- | -| sound | [DuploTrainBaseSound](#DuploTrainBaseSound) | - ### duploTrainBase.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ Promise @@ -1666,6 +1694,30 @@ Ramp the motor speed on a given port. | toSpeed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | | time | number | How long the ramp should last (in milliseconds). | + + +### duploTrainBase.hardStopMotor(port) ⇒ Promise +Fully (hard) stop the motor on a given port. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Returns**: Promise - Resolved upon successful completion of command. + +| Param | Type | +| --- | --- | +| port | string | + + + +### duploTrainBase.playSound(sound) ⇒ Promise +Play a built-in train sound. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Returns**: Promise - Resolved upon successful issuance of command. + +| Param | Type | +| --- | --- | +| sound | [DuploTrainBaseSound](#DuploTrainBaseSound) | + ### duploTrainBase.setName(name) ⇒ Promise diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index c013dd4..7532dbc 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -192,6 +192,17 @@ export class BoostMoveHub extends LPF2Hub { } + /** + * Fully (hard) stop the motor on a given port. + * @method BoostMoveHub#hardStopMotor + * @param {string} port + * @returns {Promise} Resolved upon successful completion of command. + */ + public hardStopMotor (port: string) { + return this.setMotorSpeed(port, 127); + } + + /** * Set the light brightness on a given port. * @method BoostMoveHub#setLightBrightness diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index f8a6ab6..c125456 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -135,21 +135,6 @@ export class DuploTrainBase extends LPF2Hub { } - /** - * Play a built-in train sound. - * @method DuploTrainBase#playSound - * @param {DuploTrainBaseSound} sound - * @returns {Promise} Resolved upon successful issuance of command. - */ - public playSound (sound: number) { - return new Promise((resolve, reject) => { - const data = Buffer.from([0x81, 0x01, 0x11, 0x51, 0x01, sound]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - }); - } - - /** * Ramp the motor speed on a given port. * @method DuploTrainBase#rampMotorSpeed @@ -172,4 +157,30 @@ export class DuploTrainBase extends LPF2Hub { } + /** + * Fully (hard) stop the motor on a given port. + * @method DuploTrainBase#hardStopMotor + * @param {string} port + * @returns {Promise} Resolved upon successful completion of command. + */ + public hardStopMotor (port: string) { + return this.setMotorSpeed(port, 127); + } + + + /** + * Play a built-in train sound. + * @method DuploTrainBase#playSound + * @param {DuploTrainBaseSound} sound + * @returns {Promise} Resolved upon successful issuance of command. + */ + public playSound (sound: number) { + return new Promise((resolve, reject) => { + const data = Buffer.from([0x81, 0x01, 0x11, 0x51, 0x01, sound]); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + return resolve(); + }); + } + + } diff --git a/src/hub.ts b/src/hub.ts index 51beb19..8c172cf 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -313,21 +313,18 @@ export class Hub extends EventEmitter { if (!this.useSpeedMap) { return speed; } - if (speed > 0) { - if (speed > 100) { - speed = 100; - } - return speed; - // return Math.round((speed - 1) * (97 - 15) / (100 - 1) + 15); // Forward, minimum speed is 15, maximum speed is 97 - } else if (speed < 0) { - if (speed < -100) { - speed = -100; - } - return speed; - // return Math.round((speed - -100) * (240 - 158) / (-1 - -100) + 158); // In reverse, minimum speed is 240, maximum speed is 158 - } else { - return 0; + + if (speed === 127) { + return 127; // Hard stop } + + if (speed > 100) { + speed = 100; + } else if (speed < -100) { + speed = -100; + } + + return speed; } diff --git a/src/puphub.ts b/src/puphub.ts index e0885ee..6618e8b 100644 --- a/src/puphub.ts +++ b/src/puphub.ts @@ -146,6 +146,17 @@ export class PUPHub extends LPF2Hub { } + /** + * Fully (hard) stop the motor on a given port. + * @method PUPHub#hardStopMotor + * @param {string} port + * @returns {Promise} Resolved upon successful completion of command. + */ + public hardStopMotor (port: string) { + return this.setMotorSpeed(port, 127); + } + + /** * Set the light brightness on a given port. * @method PUPHub#setLightBrightness diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index 946535f..2fe304a 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -180,6 +180,17 @@ export class WeDo2SmartHub extends Hub { } + /** + * Fully (hard) stop the motor on a given port. + * @method WeDo2SmartHub#hardStopMotor + * @param {string} port + * @returns {Promise} Resolved upon successful completion of command. + */ + public hardStopMotor (port: string) { + return this.setMotorSpeed(port, 127); + } + + /** * Play a tone on the Hub's in-built buzzer * @method WeDo2SmartHub#playTone