-
Notifications
You must be signed in to change notification settings - Fork 62
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
base: master
Are you sure you want to change the base?
Dynamic modes #97
Changes from all commits
ea6b23b
ec37e6d
d4ea7cb
299963b
b4f1880
57c7baa
45b0c20
4edd5eb
8c451f2
8feffd6
4c429e2
c830cb9
df8ebd2
405b5f2
351bc90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
{ | ||
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add handle for |
||
} | ||
|
||
|
||
/** | ||
* Rotate a motor by a given angle. | ||
* @method AbsoluteMotor#gotoAngle | ||
|
@@ -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 | ||
}; |
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: IMode[] = [ | ||
{ | ||
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 } | ||
} | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mode definitions are exported to be used by |
||
|
||
/** | ||
* @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); | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
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