Added setSpeed, virtual port multiparam options
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
b43b59e526
commit
f7e86d32cf
@ -15,8 +15,8 @@ export class BasicMotor extends Device {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the motor speed.
|
* Set the motor power.
|
||||||
* @method BasicMotor#power
|
* @method BasicMotor#setPower
|
||||||
* @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
|
* @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
|
||||||
* @returns {Promise} Resolved upon successful completion of command.
|
* @returns {Promise} Resolved upon successful completion of command.
|
||||||
*/
|
*/
|
||||||
|
@ -27,6 +27,46 @@ export class TachoMotor extends BasicMotor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the motor speed.
|
||||||
|
* @method TachoMotor#setSpeed
|
||||||
|
* @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
|
||||||
|
* @returns {Promise} Resolved upon successful completion of command.
|
||||||
|
*/
|
||||||
|
public setSpeed (speed: [number, number] | number, time: number | undefined) {
|
||||||
|
if (!this.isVirtualPort && speed instanceof Array) {
|
||||||
|
throw new Error("Only virtual ports can accept multiple speeds");
|
||||||
|
}
|
||||||
|
if (this.isWeDo2SmartHub) {
|
||||||
|
throw new Error("Motor speed is not available on the WeDo 2.0 Smart Hub");
|
||||||
|
}
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
this._busy = true;
|
||||||
|
if (speed === undefined || speed === null) {
|
||||||
|
speed = 100;
|
||||||
|
}
|
||||||
|
let message;
|
||||||
|
if (time !== undefined) {
|
||||||
|
if (speed instanceof Array) {
|
||||||
|
message = Buffer.from([0x81, this.portId, 0x11, 0x0a, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||||
|
} else {
|
||||||
|
message = Buffer.from([0x81, this.portId, 0x11, 0x09, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||||
|
}
|
||||||
|
message.writeUInt16LE(time, 4);
|
||||||
|
} else {
|
||||||
|
if (speed instanceof Array) {
|
||||||
|
message = Buffer.from([0x81, this.portId, 0x11, 0x08, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||||
|
} else {
|
||||||
|
message = Buffer.from([0x81, this.portId, 0x11, 0x07, mapSpeed(speed), 0x64, 0x03, 0x64, 0x7f, 0x03]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.send(message);
|
||||||
|
this._finished = () => {
|
||||||
|
return resolve();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotate a motor by a given angle.
|
* Rotate a motor by a given angle.
|
||||||
* @method TachoMotor#rotateByAngle
|
* @method TachoMotor#rotateByAngle
|
||||||
@ -34,13 +74,24 @@ export class TachoMotor extends BasicMotor {
|
|||||||
* @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.
|
* @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.
|
||||||
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
|
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
|
||||||
*/
|
*/
|
||||||
public rotateByAngle (angle: number, speed: number = 100) {
|
public rotateByAngle (angle: number, speed: [number, number] | number) {
|
||||||
|
if (!this.isVirtualPort && speed instanceof Array) {
|
||||||
|
throw new Error("Only virtual ports can accept multiple speeds");
|
||||||
|
}
|
||||||
if (this.isWeDo2SmartHub) {
|
if (this.isWeDo2SmartHub) {
|
||||||
throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub");
|
throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub");
|
||||||
}
|
}
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
this._busy = true;
|
this._busy = true;
|
||||||
const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
if (speed === undefined || speed === null) {
|
||||||
|
speed = 100;
|
||||||
|
}
|
||||||
|
let message;
|
||||||
|
if (speed instanceof Array) {
|
||||||
|
message = Buffer.from([0x81, this.portId, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||||
|
} else {
|
||||||
|
message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||||
|
}
|
||||||
message.writeUInt32LE(angle, 4);
|
message.writeUInt32LE(angle, 4);
|
||||||
this.send(message);
|
this.send(message);
|
||||||
this._finished = () => {
|
this._finished = () => {
|
||||||
|
@ -106,7 +106,7 @@ export class LPF2Hub extends BaseHub {
|
|||||||
if (firstDevice.type !== secondDevice.type) {
|
if (firstDevice.type !== secondDevice.type) {
|
||||||
throw new Error(`Both devices must be of the same type to create a virtual port`);
|
throw new Error(`Both devices must be of the same type to create a virtual port`);
|
||||||
}
|
}
|
||||||
this.send(Buffer.from([0x61, 0x01, firstPortId, secondPortId]), Consts.BLECharacteristic.LPF2_ALL);
|
this.send(Buffer.from([0x61, 0x01, firstDevice.portId, secondDevice.portId]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user