Missing ramp speed working properly

This commit is contained in:
Nathan Kellenicki 2018-08-19 12:33:56 +01:00
parent 7a23697685
commit 97e48a8c6a
5 changed files with 46 additions and 18 deletions

View File

@ -61,6 +61,7 @@ export class BoostMoveHub extends LPF2Hub {
if (portObj.id !== "AB" && speed instanceof Array) { if (portObj.id !== "AB" && speed instanceof Array) {
throw new Error(`Port ${portObj.id} can only accept a single speed`); throw new Error(`Port ${portObj.id} can only accept a single speed`);
} }
portObj.cancelEventTimer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (time) { if (time) {
@ -82,11 +83,12 @@ export class BoostMoveHub extends LPF2Hub {
// @ts-ignore: The type of speed is properly checked at the start // @ts-ignore: The type of speed is properly checked at the start
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
setTimeout(() => { const timeout = setTimeout(() => {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
}, time); }, time);
portObj.setEventTimer(timeout);
} }
} else { } else {
@ -125,8 +127,10 @@ export class BoostMoveHub extends LPF2Hub {
* @returns {Promise} Resolved upon successful completion of command. * @returns {Promise} Resolved upon successful completion of command.
*/ */
public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) {
const portObj = this._portLookup(port);
portObj.cancelEventTimer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time) this._calculateRamp(fromSpeed, toSpeed, time, portObj)
.on("changeSpeed", (speed) => { .on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed); this.setMotorSpeed(port, speed);
}) })
@ -151,6 +155,7 @@ export class BoostMoveHub extends LPF2Hub {
if (portObj.id !== "AB" && speed instanceof Array) { if (portObj.id !== "AB" && speed instanceof Array) {
throw new Error(`Port ${portObj.id} can only accept a single speed`); throw new Error(`Port ${portObj.id} can only accept a single speed`);
} }
portObj.cancelEventTimer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
portObj.busy = true; portObj.busy = true;
let data = null; let data = null;
@ -183,11 +188,12 @@ export class BoostMoveHub extends LPF2Hub {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
if (time) { if (time) {
setTimeout(() => { const timeout = setTimeout(() => {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
}, time); }, time);
portObj.setEventTimer(timeout);
} else { } else {
return resolve(); return resolve();
} }

3
hub.ts
View File

@ -304,7 +304,7 @@ export class Hub extends EventEmitter {
} }
protected _calculateRamp (fromSpeed: number, toSpeed: number, time: number) { protected _calculateRamp (fromSpeed: number, toSpeed: number, time: number, port: Port) {
const emitter = new EventEmitter(); const emitter = new EventEmitter();
const steps = Math.abs(toSpeed - fromSpeed); const steps = Math.abs(toSpeed - fromSpeed);
let delay = time / steps; let delay = time / steps;
@ -330,6 +330,7 @@ export class Hub extends EventEmitter {
emitter.emit("finished"); emitter.emit("finished");
} }
}, delay); }, delay);
port.setEventTimer(interval);
return emitter; return emitter;
} }

21
port.ts
View File

@ -6,19 +6,28 @@ export class Port {
public id: string; public id: string;
public value: number; public value: number;
public connected: boolean;
public type: Consts.Devices; public type: Consts.Devices;
public busy: boolean; public connected: boolean = false;
public finished: (() => void) | null; public busy: boolean = false;
public finished: (() => void) | null = null;
private _eventTimer: NodeJS.Timer | null = null;
constructor (id: string, value: number) { constructor (id: string, value: number) {
this.id = id; this.id = id;
this.value = value; this.value = value;
this.connected = false;
this.busy = false;
this.finished = null;
this.type = Consts.Devices.UNKNOWN; this.type = Consts.Devices.UNKNOWN;
} }
public cancelEventTimer () {
if (this._eventTimer) {
clearTimeout(this._eventTimer);
this._eventTimer = null;
}
}
public setEventTimer (timer: NodeJS.Timer) {
this._eventTimer = timer;
}
} }

View File

@ -74,6 +74,7 @@ export class PUPHub extends LPF2Hub {
throw new Error(`Port ${portObj.id} requires both motors be of the same type`); throw new Error(`Port ${portObj.id} requires both motors be of the same type`);
} }
} }
portObj.cancelEventTimer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (time) { if (time) {
let data = null; let data = null;
@ -84,7 +85,7 @@ export class PUPHub extends LPF2Hub {
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]); data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
} }
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
setTimeout(() => { const timeout = setTimeout(() => {
let data = null; let data = null;
if (portObj.id === "AB") { if (portObj.id === "AB") {
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00, 0x00]); data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00, 0x00]);
@ -94,6 +95,7 @@ export class PUPHub extends LPF2Hub {
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
}, time); }, time);
portObj.setEventTimer(timeout);
} else { } else {
let data = null; let data = null;
if (portObj.id === "AB") { if (portObj.id === "AB") {
@ -119,8 +121,10 @@ export class PUPHub extends LPF2Hub {
* @returns {Promise} Resolved upon successful completion of command. * @returns {Promise} Resolved upon successful completion of command.
*/ */
public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) {
const portObj = this._portLookup(port);
portObj.cancelEventTimer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time) this._calculateRamp(fromSpeed, toSpeed, time, portObj)
.on("changeSpeed", (speed) => { .on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed); this.setMotorSpeed(port, speed);
}) })
@ -139,15 +143,17 @@ export class PUPHub extends LPF2Hub {
*/ */
public setLightBrightness (port: string, brightness: number, time?: number) { public setLightBrightness (port: string, brightness: number, time?: number) {
const portObj = this._portLookup(port); const portObj = this._portLookup(port);
portObj.cancelEventTimer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
if (time) { if (time) {
setTimeout(() => { const timeout = setTimeout(() => {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
}, time); }, time);
portObj.setEventTimer(timeout);
} else { } else {
return resolve(); return resolve();
} }

View File

@ -125,13 +125,16 @@ export class WeDo2SmartHub extends Hub {
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
*/ */
public setMotorSpeed (port: string, speed: number, time?: number) { public setMotorSpeed (port: string, speed: number, time?: number) {
const portObj = this._portLookup(port);
portObj.cancelEventTimer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._portLookup(port).value, 0x01, 0x02, this._mapSpeed(speed)])); this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)]));
if (time) { if (time) {
setTimeout(() => { const timeout = setTimeout(() => {
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._portLookup(port).value, 0x01, 0x02, 0x00])); this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00]));
return resolve(); return resolve();
}, time); }, time);
portObj.setEventTimer(timeout);
} else { } else {
return resolve(); return resolve();
} }
@ -149,8 +152,10 @@ export class WeDo2SmartHub extends Hub {
* @returns {Promise} Resolved upon successful completion of command. * @returns {Promise} Resolved upon successful completion of command.
*/ */
public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) {
const portObj = this._portLookup(port);
portObj.cancelEventTimer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time) this._calculateRamp(fromSpeed, toSpeed, time, portObj)
.on("changeSpeed", (speed) => { .on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed); this.setMotorSpeed(port, speed);
}) })
@ -191,11 +196,12 @@ export class WeDo2SmartHub extends Hub {
const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]); const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
if (time) { if (time) {
setTimeout(() => { const timeout = setTimeout(() => {
const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]); const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
return resolve(); return resolve();
}, time); }, time);
portObj.setEventTimer(timeout);
} else { } else {
return resolve(); return resolve();
} }