Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework port output command handling #159

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,27 @@ export enum MarioColor {
BROWN = 0x6a00,
CYAN = 0x4201,
}

/**
* @typedef CommandFeedback
* @param {number} TRANSMISSION_PENDING 0x00 waiting for previous commands to complete transmission or execution
* @param {number} TRANSMISSION_BUSY 0x10 waiting for device to acknowledge reception
* @param {number} TRANSMISSION_DISCARDED 0x44 interrupt command has been recieved or device disconnected
* @param {number} EXECUTION_PENDING 0x20 device is waiting for previous command to complete
* @param {number} EXECUTION_BUSY 0x21 device is executing the command
* @param {number} EXECUTION_DISCARDED 0x24 device discarded the command e.g. due to interrupt
* @param {number} EXECUTION_COMPLETED 0x22 device reported successful completion of command
* @param {number} FEEDBACK_MISSING 0x66 device disconnected or failed to report feedback
* @param {number} FEEDBACK_DISABLED 0x26 feedback not implemented for this command
*/
export enum CommandFeedback {
TRANSMISSION_PENDING = 0x00,
TRANSMISSION_BUSY = 0x10,
TRANSMISSION_DISCARDED = 0x44,
EXECUTION_PENDING = 0x20,
EXECUTION_BUSY = 0x21,
EXECUTION_DISCARDED = 0x24,
EXECUTION_COMPLETED = 0x22,
FEEDBACK_MISSING = 0x66,
FEEDBACK_DISABLED = 0x26,
}
55 changes: 24 additions & 31 deletions src/devices/absolutemotor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,29 @@ export class AbsoluteMotor extends TachoMotor {
* @method AbsoluteMotor#gotoAngle
* @param {number} angle Absolute position the motor should go to (degrees from 0).
* @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).
* @param {boolean} [interrupt=false] If true, previous commands are discarded.
* @returns {Promise<CommandFeedback>} Resolved upon completion of command (i.e. once the motor is finished).
*/
public gotoAngle (angle: [number, number] | number, speed: number = 100) {
public gotoAngle (angle: [number, number] | number, speed: number = 100, interrupt: boolean = false) {
if (!this.isVirtualPort && angle instanceof Array) {
throw new Error("Only virtual ports can accept multiple positions");
}
if (this.isWeDo2SmartHub) {
throw new Error("Absolute positioning is not available on the WeDo 2.0 Smart Hub");
}
this.cancelEventTimer();
return new Promise<void>((resolve) => {
if (speed === undefined || speed === null) {
speed = 100;
}
let message;
if (angle instanceof Array) {
message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
message.writeInt32LE(normalizeAngle(angle[0]), 4);
message.writeInt32LE(normalizeAngle(angle[1]), 8);
} else {
message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
message.writeInt32LE(normalizeAngle(angle), 4);
}
this.send(message);
this._finishedCallbacks.push(() => {
return resolve();
});
});
if (speed === undefined || speed === null) {
speed = 100;
}
let message;
if (angle instanceof Array) {
message = Buffer.from([0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
message.writeInt32LE(normalizeAngle(angle[0]), 1);
message.writeInt32LE(normalizeAngle(angle[1]), 5);
} else {
message = Buffer.from([0x0d, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
message.writeInt32LE(normalizeAngle(angle), 1);
}
return this.sendPortOutputCommand(message, interrupt);
}


Expand All @@ -77,10 +72,10 @@ export class AbsoluteMotor extends TachoMotor {
* Real zero is marked on Technic angular motors (SPIKE Prime). It is also available on Technic linear motors (Control+) but is unmarked.
* @method AbsoluteMotor#gotoRealZero
* @param {number} [speed=100] Speed between 1 - 100. Note that this will always take the shortest path to zero.
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
* @returns {Promise<CommandFeedback>} Resolved upon completion of command (i.e. once the motor is finished).
*/
public gotoRealZero (speed: number = 100) {
return new Promise<void>((resolve) => {
return new Promise<Consts.CommandFeedback>((resolve) => {
const oldMode = this.mode;
let calibrated = false;
this.on("absolute", async ({ angle }) => {
Expand All @@ -95,7 +90,7 @@ export class AbsoluteMotor extends TachoMotor {
if (oldMode) {
this.subscribe(oldMode);
}
return resolve();
return resolve(Consts.CommandFeedback.FEEDBACK_DISABLED);
}
});
this.requestUpdate();
Expand All @@ -106,14 +101,12 @@ export class AbsoluteMotor extends TachoMotor {
/**
* Reset zero to current position
* @method AbsoluteMotor#resetZero
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
* @param {boolean} [interrupt=false] If true, previous commands are discarded.
* @returns {Promise<CommandFeedback>} Resolved upon completion of command (i.e. once the motor is finished).
*/
public resetZero () {
return new Promise<void>((resolve) => {
const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]);
this.send(data);
return resolve();
});
public resetZero (interrupt: boolean = false) {
const data = Buffer.from([0x51, 0x02, 0x00, 0x00, 0x00, 0x00]);
return this.sendPortOutputCommand(data, interrupt);
}


Expand Down
41 changes: 16 additions & 25 deletions src/devices/basicmotor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Device } from "./device";

import { IDeviceInterface } from "../interfaces";

import * as Consts from "../consts";

import { calculateRamp, mapSpeed } from "../utils";

/**
Expand All @@ -22,13 +19,11 @@ export class BasicMotor extends Device {
* Set the motor 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.
* @returns {Promise} Resolved upon successful issuance of the command.
* @param {boolean} [interrupt=false] If true, previous commands are discarded.
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
*/
public setPower (power: number, interrupt: boolean = true) {
if (interrupt) {
this.cancelEventTimer();
}
return this.writeDirect(0x00, Buffer.from([mapSpeed(power)]));
public setPower (power: number, interrupt: boolean = false) {
return this.writeDirect(0x00, Buffer.from([mapSpeed(power)]), interrupt);
}


Expand All @@ -38,39 +33,35 @@ export class BasicMotor extends Device {
* @param {number} fromPower For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number} toPower For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number} time How long the ramp should last (in milliseconds).
* @returns {Promise} Resolved upon successful completion of command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
*/
public rampPower (fromPower: number, toPower: number, time: number) {
this.cancelEventTimer();
return new Promise((resolve) => {
calculateRamp(this, fromPower, toPower, time)
.on("changePower", (power) => {
this.setPower(power, false);
})
.on("finished", resolve);
const powerValues = calculateRamp(fromPower, toPower, time);
powerValues.forEach(value => {
this.setPower(value);
this.addPortOutputSleep(Math.round(time/powerValues.length));
});
return this.setPower(toPower);
}


/**
* Stop the motor.
* Stop the motor. Previous commands that have not completed are discarded.
* @method BasicMotor#stop
* @returns {Promise} Resolved upon successful issuance of the command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
*/
public stop () {
this.cancelEventTimer();
return this.setPower(0);
return this.setPower(0, true);
}


/**
* Brake the motor.
* Brake the motor. Previous commands that have not completed are discarded.
* @method BasicMotor#brake
* @returns {Promise} Resolved upon successful issuance of the command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
*/
public brake () {
this.cancelEventTimer();
return this.setPower(Consts.BrakingStyle.BRAKE);
return this.setPower(Consts.BrakingStyle.BRAKE, true);
}


Expand Down
52 changes: 23 additions & 29 deletions src/devices/colordistancesensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class ColorDistanceSensor extends Device {
* NOTE: Calling this with channel 5-8 with switch off extended channel mode for this receiver.
* @method ColorDistanceSensor#setPFExtendedChannel
* @param {number} channel Channel number, between 1-8
* @returns {Promise} Resolved upon successful issuance of the command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of the command.
*/
public setPFExtendedChannel (channel: number) {
let address = 0;
Expand All @@ -181,7 +181,7 @@ export class ColorDistanceSensor extends Device {
* @param {number} channel Channel number, between 1-4
* @param {string} output Outport port, "RED" (A) or "BLUE" (B)
* @param {number} power -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
* @returns {Promise} Resolved upon successful issuance of the command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of the command.
*/
public setPFPower (channel: number, output: Output, power: number) {
let address = 0;
Expand All @@ -205,7 +205,7 @@ export class ColorDistanceSensor extends Device {
* @param {Buffer} channel Channel number, between 1-4
* @param {Buffer} powerA -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
* @param {Buffer} powerB -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
* @returns {Promise} Resolved upon successful issuance of the command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of the command.
*/
public startPFMotors (channel: number, powerBlue: number, powerRed: number) {
let address = 0;
Expand All @@ -225,7 +225,7 @@ export class ColorDistanceSensor extends Device {
* Send a raw Power Functions IR command
* @method ColorDistanceSensor#sendPFIRMessage
* @param {Buffer} message 2 byte payload making up a Power Functions protocol command. NOTE: Only specify nibbles 1-3, nibble 4 should be zeroed.
* @returns {Promise} Resolved upon successful issuance of the command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of the command.
*/
public sendPFIRMessage (message: Buffer) {
if (this.isWeDo2SmartHub) {
Expand All @@ -244,41 +244,35 @@ export class ColorDistanceSensor extends Device {
* Set the color of the LED on the sensor via a color value.
* @method ColorDistanceSensor#setColor
* @param {Color} color
* @returns {Promise} Resolved upon successful issuance of the command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of the command.
*/
public setColor (color: number | boolean) {
return new Promise<void>((resolve) => {
if (color === false) {
color = 0;
}
if (this.isWeDo2SmartHub) {
throw new Error("Setting LED color is not available on the WeDo 2.0 Smart Hub");
} else {
this.subscribe(Mode.LED);
this.writeDirect(0x05, Buffer.from([color as number]));
}
return resolve();
});
if (color === false) {
color = 0;
}
if (this.isWeDo2SmartHub) {
throw new Error("Setting LED color is not available on the WeDo 2.0 Smart Hub");
} else {
this.subscribe(Mode.LED);
return this.writeDirect(0x05, Buffer.from([color as number]));
}
}

/**
* Set the distance count value.
* @method ColorDistanceSensor#setDistanceCount
* @param {count} distance count between 0 and 2^32
* @returns {Promise} Resolved upon successful issuance of the command.
* @returns {Promise<CommandFeedback>} Resolved upon completion of the command.
*/
public setDistanceCount (count: number) {
return new Promise<void>((resolve) => {
if (this.isWeDo2SmartHub) {
throw new Error("Setting distance count is not available on the WeDo 2.0 Smart Hub");
} else {
const payload = Buffer.alloc(4);
payload.writeUInt32LE(count % 2**32);
// no need to subscribe, can be set in different mode
this.writeDirect(0x02, payload);
}
return resolve();
});
if (this.isWeDo2SmartHub) {
throw new Error("Setting distance count is not available on the WeDo 2.0 Smart Hub");
} else {
const payload = Buffer.alloc(4);
payload.writeUInt32LE(count % 2**32);
// no need to subscribe, can be set in different mode
return this.writeDirect(0x02, payload);
}
}

private _pfPowerToPWM (power: number) {
Expand Down
Loading