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

Dynamic modes #97

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
19 changes: 19 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,4 +636,23 @@ export enum PortInputFormatSetupSubCommand {
}


/**
* @typedef ValueType
* @param {number} Int8 0x01
* @param {number} Int16 0x02
* @param {number} Int32 0x03
* @param {number} Float 0x04
*/
export enum ValueType {
Int8 = 0x00,
Int16 = 0x01,
Int32 = 0x02,
Float = 0x03,
}

export const ValueTypeSize = {
[ValueType.Int8]: 1,
[ValueType.Int16]: 2,
[ValueType.Int32]: 4,
[ValueType.Float]: 4,
}
82 changes: 48 additions & 34 deletions src/devices/absolutemotor.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@
import { TachoMotor } from "./tachomotor";
import { TachoMotor, modes as TachoMotorModes } from "./tachomotor";

import { IDeviceInterface } from "../interfaces";
import { IDeviceInterface, IMode, IEventData } from "../interfaces";

import * as Consts from "../consts";
import { mapSpeed, normalizeAngle, roundAngleToNearest90 } from "../utils";

export const modes = TachoMotorModes.concat([
// POWER
// SPEED/speed
// POS/rotate
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It add mode to the existing ones in TachoMotor

{
name: "absolute", // APOS
input: true,
output: true,
raw: { min: -360, max: 360 },
pct: { min: -100, max: 100 },
si: { min: -360, max: 360, symbol: "DEG" },
values: { count: 1, type: Consts.ValueType.Int16 }
},
{
name: "LOAD",
input: true,
output: true,
raw: { min: 0, max: 127 },
pct: { min: 0, max: 100 },
si: { min: 0, max: 127, symbol: "PCT" },
values: { count: 1, type: Consts.ValueType.Int8 }
},
{
name: "CALIB",
input: false,
output: false,
raw: { min: 0, max: 512 },
pct: { min: 0, max: 100 },
si: { min: 0, max: 512, symbol: "RAW" },
values: { count: 3, type: Consts.ValueType.Int16 }
}
])

/**
* @class AbsoluteMotor
* @extends TachoMotor
*/
export class AbsoluteMotor extends TachoMotor {

constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) {
super(hub, portId, Object.assign({}, modeMap, ModeMap), type);
}
constructor (hub: IDeviceInterface, portId: number, _modes: IMode[] = [], type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) {
super(hub, portId, _modes.length > 0 ? _modes : modes, type);

public receive (message: Buffer) {
const mode = this._mode;

switch (mode) {
case Mode.ABSOLUTE:
const angle = normalizeAngle(message.readInt16LE(this.isWeDo2SmartHub ? 2 : 4));
/**
* Emits when a the motors absolute position is changed.
* @event AbsoluteMotor#absolute
* @type {object}
* @param {number} absolute
*/
this.notify("absolute", { angle });
break;
default:
super.receive(message);
break;
}
this._eventHandlers.rotate = (data: IEventData) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo - this should be this._eventHandlers.absolute

const [angle] = data.raw;
/**
* Emits when a the motors absolute position is changed.
* @event AbsoluteMotor#absolute
* @type {object}
* @param {number} absolute
*/
this.notify("absolute", { angle });
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add handle for rotate mode. Using raw instead of SI because we know it does not convert very well on angles

}


/**
* Rotate a motor by a given angle.
* @method AbsoluteMotor#gotoAngle
Expand Down Expand Up @@ -117,15 +142,4 @@ export class AbsoluteMotor extends TachoMotor {
});
}


}

export enum Mode {
ROTATION = 0x02,
ABSOLUTE = 0x03
}

export const ModeMap: {[event: string]: number} = {
"rotate": Mode.ROTATION,
"absolute": Mode.ABSOLUTE
};
18 changes: 15 additions & 3 deletions src/devices/basicmotor.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { Device } from "./device";

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

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

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

export const modes = [
{
name: "POWER", // or LPF2-MOTOR
input: false,
output: true,
raw: { min: -100, max: 100 },
pct: { min: -100, max: 100 },
si: { min: -100, max: 100, symbol: "PCT" },
values: { count: 1, type: Consts.ValueType.Int8 }
}
];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mode definitions are exported to be used by TachoMotor


/**
* @class BasicMotor
* @extends Device
*/
export class BasicMotor extends Device {


constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) {
super(hub, portId, modeMap, type);
constructor (hub: IDeviceInterface, portId: number, _modes: IMode[] = [], type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) {
super(hub, portId, _modes.length > 0 ? _modes : modes, type);
}


Expand Down
Loading