setAbsolutePosition on Control+ motors
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nathan Kellenicki 2019-08-07 12:52:56 -07:00
parent 6651b79188
commit 348037a2ab
13 changed files with 994 additions and 10 deletions

View File

@ -46,7 +46,7 @@ In addition, the Hubs themselves have certain built-in features which this libra
| Powered Up Hub | <a href="https://brickset.com/sets/88009-1/">88009</a> | RGB LED<br />Button | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a><br /><a href="https://brickset.com/sets/76112-1/">76112</a> |
| Powered Up Remote | <a href="https://brickset.com/sets/88010-1/">88010</a> | RGB LED<br />Left and Right Control Buttons<br />Button | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> |
| Duplo Train Base | 28743 | RGB LED/Headlights<br />Speaker<br />Speedometer<br />Motor<br />Color and Distance Sensor<br />Button | <a href="https://brickset.com/sets/10874-1/">10874</a><br /><a href="https://brickset.com/sets/10875-1/">10875</a> |
| Control+ Hub | 22127 | RGB LED<br />Button | <a href="https://brickset.com/sets/42099-1/">42099</a><br /><a href="https://brickset.com/sets/42100-1/">42100</a> |
| Control+ Hub | 22127 | RGB LED<br />Button<br />Tilt Sensor<br />Accelerometer | <a href="https://brickset.com/sets/42099-1/">42099</a><br /><a href="https://brickset.com/sets/42100-1/">42100</a> |
### Known Issues and Limitations

View File

@ -1257,7 +1257,7 @@
<ul class="dummy">
<li>
<a href="boostmovehub.js.html">boostmovehub.js</a>,
<a href="boostmovehub.js.html#sunlight-1-line-195">line 195</a>
<a href="boostmovehub.js.html#sunlight-1-line-229">line 229</a>
</li>
</ul>
</dd>
@ -2088,6 +2088,263 @@
</dd>
<hr>
<dt>
<h4 class="name" id="setAbsolutePosition"><span class="type-signature"></span>setAbsolutePosition(port, pos [, speed])</h4>
</dt>
<dd>
<div class="description">
<p>Tell motor to goto an absolute position</p>
</div>
<h5>Parameters:</h5>
<table class="params table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Argument</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>port</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"></td>
</tr>
<tr>
<td class="name"><code>pos</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>The position of the motor to go to</p></td>
</tr>
<tr>
<td class="name"><code>speed</code></td>
<td class="type">
<span class="param-type">number</span>
|
<span class="param-type">Array.&lt;number></span>
</td>
<td class="attributes">
&lt;optional><br>
</td>
<td class="default">
100
</td>
<td class="description last"><p>A value between 1 - 100 should be set (Direction does not apply when going to absolute position)</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source method-doc-label method-doc-details-label">Source:</dt>
<dd class="tag-source">
<ul class="dummy">
<li>
<a href="boostmovehub.js.html">boostmovehub.js</a>,
<a href="boostmovehub.js.html#sunlight-1-line-195">line 195</a>
</li>
</ul>
</dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p>Resolved upon successful completion of command (ie. once the motor is finished).</p>
</div>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise</span>
</dd>
</dl>
</dd>
@ -2665,7 +2922,7 @@
<ul class="dummy">
<li>
<a href="boostmovehub.js.html">boostmovehub.js</a>,
<a href="boostmovehub.js.html#sunlight-1-line-204">line 204</a>
<a href="boostmovehub.js.html#sunlight-1-line-238">line 238</a>
</li>
</ul>
</dd>

View File

@ -1257,7 +1257,7 @@
<ul class="dummy">
<li>
<a href="controlplushub.js.html">controlplushub.js</a>,
<a href="controlplushub.js.html#sunlight-1-line-190">line 190</a>
<a href="controlplushub.js.html#sunlight-1-line-224">line 224</a>
</li>
</ul>
</dd>
@ -2088,6 +2088,263 @@
</dd>
<hr>
<dt>
<h4 class="name" id="setAbsolutePosition"><span class="type-signature"></span>setAbsolutePosition(port, pos [, speed])</h4>
</dt>
<dd>
<div class="description">
<p>Tell motor to goto an absolute position</p>
</div>
<h5>Parameters:</h5>
<table class="params table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Argument</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>port</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"></td>
</tr>
<tr>
<td class="name"><code>pos</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>The position of the motor to go to</p></td>
</tr>
<tr>
<td class="name"><code>speed</code></td>
<td class="type">
<span class="param-type">number</span>
|
<span class="param-type">Array.&lt;number></span>
</td>
<td class="attributes">
&lt;optional><br>
</td>
<td class="default">
100
</td>
<td class="description last"><p>A value between 1 - 100 should be set (Direction does not apply when going to absolute position)</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source method-doc-label method-doc-details-label">Source:</dt>
<dd class="tag-source">
<ul class="dummy">
<li>
<a href="controlplushub.js.html">controlplushub.js</a>,
<a href="controlplushub.js.html#sunlight-1-line-190">line 190</a>
</li>
</ul>
</dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p>Resolved upon successful completion of command (ie. once the motor is finished).</p>
</div>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise</span>
</dd>
</dl>
</dd>
@ -2665,7 +2922,7 @@
<ul class="dummy">
<li>
<a href="controlplushub.js.html">controlplushub.js</a>,
<a href="controlplushub.js.html#sunlight-1-line-199">line 199</a>
<a href="controlplushub.js.html#sunlight-1-line-233">line 233</a>
</li>
</ul>
</dd>

View File

@ -1257,7 +1257,7 @@
<ul class="dummy">
<li>
<a href="puphub.js.html">puphub.js</a>,
<a href="puphub.js.html#sunlight-1-line-189">line 189</a>
<a href="puphub.js.html#sunlight-1-line-223">line 223</a>
</li>
</ul>
</dd>
@ -2088,6 +2088,263 @@
</dd>
<hr>
<dt>
<h4 class="name" id="setAbsolutePosition"><span class="type-signature"></span>setAbsolutePosition(port, pos [, speed])</h4>
</dt>
<dd>
<div class="description">
<p>Tell motor to goto an absolute position</p>
</div>
<h5>Parameters:</h5>
<table class="params table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Argument</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>port</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"></td>
</tr>
<tr>
<td class="name"><code>pos</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>The position of the motor to go to</p></td>
</tr>
<tr>
<td class="name"><code>speed</code></td>
<td class="type">
<span class="param-type">number</span>
|
<span class="param-type">Array.&lt;number></span>
</td>
<td class="attributes">
&lt;optional><br>
</td>
<td class="default">
100
</td>
<td class="description last"><p>A value between 1 - 100 should be set (Direction does not apply when going to absolute position)</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source method-doc-label method-doc-details-label">Source:</dt>
<dd class="tag-source">
<ul class="dummy">
<li>
<a href="puphub.js.html">puphub.js</a>,
<a href="puphub.js.html#sunlight-1-line-189">line 189</a>
</li>
</ul>
</dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p>Resolved upon successful completion of command (ie. once the motor is finished).</p>
</div>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise</span>
</dd>
</dl>
</dd>
@ -2665,7 +2922,7 @@
<ul class="dummy">
<li>
<a href="puphub.js.html">puphub.js</a>,
<a href="puphub.js.html#sunlight-1-line-198">line 198</a>
<a href="puphub.js.html#sunlight-1-line-232">line 232</a>
</li>
</ul>
</dd>

View File

@ -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.&lt;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

View File

@ -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.&lt;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

View File

@ -264,7 +264,7 @@
<tr>
<td>Control+ Hub</td>
<td>22127</td>
<td>RGB LED<br />Button</td>
<td>RGB LED<br />Button<br />Tilt Sensor<br />Accelerometer</td>
<td><a href="https://brickset.com/sets/42099-1/">42099</a><br /><a href="https://brickset.com/sets/42100-1/">42100</a></td>
</tr>
</tbody>

View File

@ -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.&lt;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

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "node-poweredup",
"version": "3.2.0",
"version": "3.3.0",
"description": "A Javascript module to interface with LEGO Powered Up components.",
"homepage": "https://github.com/nathankellenicki/node-poweredup/",
"main": "dist/node/index-node.js",

View File

@ -205,6 +205,43 @@ export class BoostMoveHub extends 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).
*/
public setAbsolutePosition (port: string, pos: number, speed: number = 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

View File

@ -203,6 +203,43 @@ export class ControlPlusHub extends 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).
*/
public setAbsolutePosition (port: string, pos: number, speed: number = 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

View File

@ -197,6 +197,43 @@ export class PUPHub extends 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).
*/
public setAbsolutePosition (port: string, pos: number, speed: number = 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