Fixed parts of absolute angle
This commit is contained in:
parent
74be8a752b
commit
56eaa7ca49
@ -3,7 +3,7 @@ import { TachoMotor } from "./tachomotor";
|
|||||||
import { IDeviceInterface } from "../interfaces";
|
import { IDeviceInterface } from "../interfaces";
|
||||||
|
|
||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
import { mapSpeed } from "../utils";
|
import { mapSpeed, normalizeAngle } from "../utils";
|
||||||
|
|
||||||
export class AbsoluteMotor extends TachoMotor {
|
export class AbsoluteMotor extends TachoMotor {
|
||||||
|
|
||||||
@ -16,13 +16,13 @@ export class AbsoluteMotor extends TachoMotor {
|
|||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case Mode.ABSOLUTE:
|
case Mode.ABSOLUTE:
|
||||||
// const absolute = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4);
|
const angle = normalizeAngle(message.readInt16LE(this.isWeDo2SmartHub ? 2 : 4));
|
||||||
// /**
|
/**
|
||||||
// * Emits when a the motors absolute position is changed.
|
* Emits when a the motors absolute position is changed.
|
||||||
// * @event AbsoluteMotor#absolute
|
* @event AbsoluteMotor#absolute
|
||||||
// * @param {number} absolute
|
* @param {number} absolute
|
||||||
// */
|
*/
|
||||||
// this.emitGlobal("absolute", { absolute });
|
this.emitGlobal("absolute", { angle });
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
super.receive(message);
|
super.receive(message);
|
||||||
@ -33,12 +33,12 @@ export class AbsoluteMotor extends TachoMotor {
|
|||||||
/**
|
/**
|
||||||
* Rotate a motor by a given angle.
|
* Rotate a motor by a given angle.
|
||||||
* @method AbsoluteMotor#gotoAbsolutePosition
|
* @method AbsoluteMotor#gotoAbsolutePosition
|
||||||
* @param {number} position Absolute position the motor should go to (degrees from 0).
|
* @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.
|
* @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 gotoAbsolutePosition (position: [number, number] | number, speed: number = 100) {
|
public gotoAbsoluteAngle (angle: [number, number] | number, speed: number = 100) {
|
||||||
if (!this.isVirtualPort && position instanceof Array) {
|
if (!this.isVirtualPort && angle instanceof Array) {
|
||||||
throw new Error("Only virtual ports can accept multiple positions");
|
throw new Error("Only virtual ports can accept multiple positions");
|
||||||
}
|
}
|
||||||
if (this.isWeDo2SmartHub) {
|
if (this.isWeDo2SmartHub) {
|
||||||
@ -50,16 +50,17 @@ export class AbsoluteMotor extends TachoMotor {
|
|||||||
speed = 100;
|
speed = 100;
|
||||||
}
|
}
|
||||||
let message;
|
let message;
|
||||||
if (position instanceof Array) {
|
if (angle instanceof Array) {
|
||||||
message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||||
message.writeUInt32LE(position[0], 4);
|
message.writeInt32LE(normalizeAngle(angle[0]), 4);
|
||||||
message.writeUInt32LE(position[1], 8);
|
message.writeInt32LE(normalizeAngle(angle[1]), 8);
|
||||||
} else {
|
} else {
|
||||||
message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||||
message.writeUInt32LE(position, 4);
|
message.writeInt32LE(normalizeAngle(angle), 4);
|
||||||
}
|
}
|
||||||
this.send(message);
|
this.send(message);
|
||||||
this._finished = () => {
|
this._finished = () => {
|
||||||
|
console.log("RESOLVE");
|
||||||
return resolve();
|
return resolve();
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@ -68,11 +69,11 @@ export class AbsoluteMotor extends TachoMotor {
|
|||||||
/**
|
/**
|
||||||
* (Re)set the knowledge of the absolute position to the current position.
|
* (Re)set the knowledge of the absolute position to the current position.
|
||||||
* @method AbsoluteMotor#resetAbsolutePosition
|
* @method AbsoluteMotor#resetAbsolutePosition
|
||||||
* @param {number} position Position to set (degrees from 0).
|
* @param {number} angle Position to set (degrees from 0).
|
||||||
* @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 resetAbsolutePosition (position: [number, number] | number) {
|
public resetAbsoluteAngle (angle: [number, number] | number) {
|
||||||
if (!this.isVirtualPort && position instanceof Array) {
|
if (!this.isVirtualPort && angle instanceof Array) {
|
||||||
throw new Error("Only virtual ports can accept multiple positions");
|
throw new Error("Only virtual ports can accept multiple positions");
|
||||||
}
|
}
|
||||||
if (this.isWeDo2SmartHub) {
|
if (this.isWeDo2SmartHub) {
|
||||||
@ -81,13 +82,13 @@ export class AbsoluteMotor extends TachoMotor {
|
|||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
this._busy = true;
|
this._busy = true;
|
||||||
let message;
|
let message;
|
||||||
if (position instanceof Array) {
|
if (angle instanceof Array) {
|
||||||
message = Buffer.from([0x81, this.portId, 0x11, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
|
message = Buffer.from([0x81, this.portId, 0x11, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
|
||||||
message.writeUInt32LE(position[0], 4);
|
message.writeInt32LE(normalizeAngle(angle[0]), 4);
|
||||||
message.writeUInt32LE(position[0], 8);
|
message.writeInt32LE(normalizeAngle(angle[0]), 8);
|
||||||
} else {
|
} else {
|
||||||
message = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]);
|
message = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]);
|
||||||
message.writeUInt32LE(position, 5);
|
message.writeInt32LE(normalizeAngle(angle), 5);
|
||||||
}
|
}
|
||||||
this.send(message);
|
this.send(message);
|
||||||
this._finished = () => {
|
this._finished = () => {
|
||||||
|
@ -36,6 +36,7 @@ export class Device extends EventEmitter {
|
|||||||
}
|
}
|
||||||
if (this.autoSubscribe) {
|
if (this.autoSubscribe) {
|
||||||
if (this._modeMap[event] !== undefined) {
|
if (this._modeMap[event] !== undefined) {
|
||||||
|
console.log(this._modeMap[event]);
|
||||||
this.subscribe(this._modeMap[event]);
|
this.subscribe(this._modeMap[event]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,13 @@ export class TachoMotor extends BasicMotor {
|
|||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case Mode.ROTATION:
|
case Mode.ROTATION:
|
||||||
const rotation = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4);
|
const degrees = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4);
|
||||||
/**
|
/**
|
||||||
* Emits when a rotation sensor is activated.
|
* Emits when a rotation sensor is activated.
|
||||||
* @event TachoMotor#rotate
|
* @event TachoMotor#rotate
|
||||||
* @param {number} rotation
|
* @param {number} rotation
|
||||||
*/
|
*/
|
||||||
this.emitGlobal("rotate", { rotation });
|
this.emitGlobal("rotate", { degrees });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,18 +68,18 @@ export class TachoMotor extends BasicMotor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotate a motor by a given angle.
|
* Rotate a motor by a given amount of degrees.
|
||||||
* @method TachoMotor#rotateByAngle
|
* @method TachoMotor#rotateByDegrees
|
||||||
* @param {number} angle How much the motor should be rotated (in degrees).
|
* @param {number} degrees How much the motor should be rotated (in degrees).
|
||||||
* @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, number] | number) {
|
public rotateByDegrees (degrees: number, speed: [number, number] | number) {
|
||||||
if (!this.isVirtualPort && speed instanceof Array) {
|
if (!this.isVirtualPort && speed instanceof Array) {
|
||||||
throw new Error("Only virtual ports can accept multiple speeds");
|
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("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;
|
||||||
@ -92,7 +92,7 @@ export class TachoMotor extends BasicMotor {
|
|||||||
} else {
|
} else {
|
||||||
message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||||
}
|
}
|
||||||
message.writeUInt32LE(angle, 4);
|
message.writeUInt32LE(degrees, 4);
|
||||||
this.send(message);
|
this.send(message);
|
||||||
this._finished = () => {
|
this._finished = () => {
|
||||||
return resolve();
|
return resolve();
|
||||||
|
@ -29,3 +29,12 @@ export const decodeVersion = (version: number) => {
|
|||||||
export const decodeMACAddress = (address: Uint8Array) => {
|
export const decodeMACAddress = (address: Uint8Array) => {
|
||||||
return Array.from(address).map((part) => toHex(part, 2)).join(":");
|
return Array.from(address).map((part) => toHex(part, 2)).join(":");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const normalizeAngle = (angle: number) => {
|
||||||
|
if (angle >= 180) {
|
||||||
|
return angle - (360 * ((angle + 180) / 360));
|
||||||
|
} else if (angle < -180) {
|
||||||
|
return angle + (360 * ((180 - angle) / 360));
|
||||||
|
}
|
||||||
|
return angle;
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user