From 348037a2abb17fa9bbf74bb8af5c63e57cdd053e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 7 Aug 2019 12:52:56 -0700 Subject: [PATCH] setAbsolutePosition on Control+ motors --- README.md | 2 +- docs/BoostMoveHub.html | 261 +++++++++++++++++++++++++++++++++++- docs/ControlPlusHub.html | 261 +++++++++++++++++++++++++++++++++++- docs/PUPHub.html | 261 +++++++++++++++++++++++++++++++++++- docs/boostmovehub.js.html | 34 +++++ docs/controlplushub.js.html | 34 +++++ docs/index.html | 2 +- docs/puphub.js.html | 34 +++++ docs/quicksearch.html | 2 +- package.json | 2 +- src/boostmovehub.ts | 37 +++++ src/controlplushub.ts | 37 +++++ src/puphub.ts | 37 +++++ 13 files changed, 994 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b50f208..a7e182c 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ In addition, the Hubs themselves have certain built-in features which this libra | Powered Up Hub | 88009 | RGB LED
Button | 60197
60198
76112 | | Powered Up Remote | 88010 | RGB LED
Left and Right Control Buttons
Button | 60197
60198 | | Duplo Train Base | 28743 | RGB LED/Headlights
Speaker
Speedometer
Motor
Color and Distance Sensor
Button | 10874
10875 | -| Control+ Hub | 22127 | RGB LED
Button | 42099
42100 | +| Control+ Hub | 22127 | RGB LED
Button
Tilt Sensor
Accelerometer | 42099
42100 | ### Known Issues and Limitations diff --git a/docs/BoostMoveHub.html b/docs/BoostMoveHub.html index 7da73ba..21c3216 100644 --- a/docs/BoostMoveHub.html +++ b/docs/BoostMoveHub.html @@ -1257,7 +1257,7 @@ @@ -2088,6 +2088,263 @@ + + + + +
+
+

setAbsolutePosition(port, pos [, speed])

+ + +
+
+ + +
+

Tell motor to goto an absolute position

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDefaultDescription
port + + +string + + + + + + + + + + + + +
pos + + +number + + + + + + + + + + + + +

The position of the motor to go to

speed + + +number +| + +Array.<number> + + + + + + + <optional>
+ + + + + +
+ + 100 + +

A value between 1 - 100 should be set (Direction does not apply when going to absolute position)

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command (ie. once the motor is finished).

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + +
@@ -2665,7 +2922,7 @@ diff --git a/docs/ControlPlusHub.html b/docs/ControlPlusHub.html index 8c1ce02..492e30e 100644 --- a/docs/ControlPlusHub.html +++ b/docs/ControlPlusHub.html @@ -1257,7 +1257,7 @@ @@ -2088,6 +2088,263 @@ + + + + +
+
+

setAbsolutePosition(port, pos [, speed])

+ + +
+
+ + +
+

Tell motor to goto an absolute position

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDefaultDescription
port + + +string + + + + + + + + + + + + +
pos + + +number + + + + + + + + + + + + +

The position of the motor to go to

speed + + +number +| + +Array.<number> + + + + + + + <optional>
+ + + + + +
+ + 100 + +

A value between 1 - 100 should be set (Direction does not apply when going to absolute position)

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command (ie. once the motor is finished).

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + +
@@ -2665,7 +2922,7 @@ diff --git a/docs/PUPHub.html b/docs/PUPHub.html index 3ef6626..60505b5 100644 --- a/docs/PUPHub.html +++ b/docs/PUPHub.html @@ -1257,7 +1257,7 @@ @@ -2088,6 +2088,263 @@ + + + + +
+
+

setAbsolutePosition(port, pos [, speed])

+ + +
+
+ + +
+

Tell motor to goto an absolute position

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDefaultDescription
port + + +string + + + + + + + + + + + + +
pos + + +number + + + + + + + + + + + + +

The position of the motor to go to

speed + + +number +| + +Array.<number> + + + + + + + <optional>
+ + + + + +
+ + 100 + +

A value between 1 - 100 should be set (Direction does not apply when going to absolute position)

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command (ie. once the motor is finished).

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + +
@@ -2665,7 +2922,7 @@ diff --git a/docs/boostmovehub.js.html b/docs/boostmovehub.js.html index 1c04001..997bdd5 100644 --- a/docs/boostmovehub.js.html +++ b/docs/boostmovehub.js.html @@ -278,6 +278,40 @@ class BoostMoveHub extends lpf2hub_1.LPF2Hub { }; }); } + /** + * Tell motor to goto an absolute position + * @method BoostMoveHub#setAbsolutePosition + * @param {string} port + * @param {number} pos The position of the motor to go to + * @param {number | Array.<number>} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) + * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + */ + setAbsolutePosition(port, pos, speed = 100) { + const portObj = this._portLookup(port); + if (!(portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR)) { + throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); + } + portObj.cancelEventTimer(); + return new Promise((resolve, reject) => { + portObj.busy = true; + let data = null; + if (this._virtualPorts[portObj.id]) { + data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + data.writeUInt32LE(pos, 4); + data.writeUInt32LE(pos, 8); + } + else { + // @ts-ignore: The type of speed is properly checked at the start + data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + data.writeUInt32LE(pos, 4); + } + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + portObj.finished = () => { + return resolve(); + }; + }); + } /** * Fully (hard) stop the motor on a given port. * @method BoostMoveHub#brakeMotor diff --git a/docs/controlplushub.js.html b/docs/controlplushub.js.html index 037be03..4242191 100644 --- a/docs/controlplushub.js.html +++ b/docs/controlplushub.js.html @@ -273,6 +273,40 @@ class ControlPlusHub extends lpf2hub_1.LPF2Hub { }; }); } + /** + * Tell motor to goto an absolute position + * @method ControlPlusHub#setAbsolutePosition + * @param {string} port + * @param {number} pos The position of the motor to go to + * @param {number | Array.<number>} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) + * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + */ + setAbsolutePosition(port, pos, speed = 100) { + const portObj = this._portLookup(port); + if (!(portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR)) { + throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); + } + portObj.cancelEventTimer(); + return new Promise((resolve, reject) => { + portObj.busy = true; + let data = null; + if (this._virtualPorts[portObj.id]) { + data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + data.writeUInt32LE(pos, 4); + data.writeUInt32LE(pos, 8); + } + else { + // @ts-ignore: The type of speed is properly checked at the start + data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + data.writeUInt32LE(pos, 4); + } + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + portObj.finished = () => { + return resolve(); + }; + }); + } /** * Fully (hard) stop the motor on a given port. * @method ControlPlusHub#brakeMotor diff --git a/docs/index.html b/docs/index.html index d998439..dc6909c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -264,7 +264,7 @@ Control+ Hub 22127 -RGB LED
Button +RGB LED
Button
Tilt Sensor
Accelerometer 42099
42100 diff --git a/docs/puphub.js.html b/docs/puphub.js.html index 02762f9..95393b4 100644 --- a/docs/puphub.js.html +++ b/docs/puphub.js.html @@ -272,6 +272,40 @@ class PUPHub extends lpf2hub_1.LPF2Hub { }; }); } + /** + * Tell motor to goto an absolute position + * @method PUPHub#setAbsolutePosition + * @param {string} port + * @param {number} pos The position of the motor to go to + * @param {number | Array.<number>} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) + * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + */ + setAbsolutePosition(port, pos, speed = 100) { + const portObj = this._portLookup(port); + if (!(portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR)) { + throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); + } + portObj.cancelEventTimer(); + return new Promise((resolve, reject) => { + portObj.busy = true; + let data = null; + if (this._virtualPorts[portObj.id]) { + data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + data.writeUInt32LE(pos, 4); + data.writeUInt32LE(pos, 8); + } + else { + // @ts-ignore: The type of speed is properly checked at the start + data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + data.writeUInt32LE(pos, 4); + } + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + portObj.finished = () => { + return resolve(); + }; + }); + } /** * Fully (hard) stop the motor on a given port. * @method PUPHub#brakeMotor diff --git a/docs/quicksearch.html b/docs/quicksearch.html index 741eea1..0cd78c2 100644 --- a/docs/quicksearch.html +++ b/docs/quicksearch.html @@ -7,7 +7,7 @@