pxt-ev3/sim/state/motors.ts

88 lines
2.0 KiB
TypeScript
Raw Normal View History

2017-12-18 22:04:17 +01:00
namespace pxsim {
export abstract class MotorNode extends BaseNode {
2017-12-18 22:04:17 +01:00
isOutput = true;
protected angle: number = 0;
2017-12-18 22:04:17 +01:00
2017-12-20 01:03:26 +01:00
private rotationsPerMilliSecond: number;
2017-12-18 22:04:17 +01:00
private speed: number;
private large: boolean;
private rotation: number;
private polarity: boolean;
2017-12-20 01:03:26 +01:00
constructor(port: number, rpm: number) {
2017-12-18 22:04:17 +01:00
super(port);
2017-12-20 01:03:26 +01:00
this.rotationsPerMilliSecond = rpm / 60000;
2017-12-18 22:04:17 +01:00
}
setSpeed(speed: number) {
if (this.speed != speed) {
this.speed = speed;
this.changed = true;
this.setChangedState();
2017-12-18 22:04:17 +01:00
}
}
setLarge(large: boolean) {
this.large = large;
}
getSpeed() {
return this.speed;
}
stepSpeed(speed: number, angle: number, brake: boolean) {
// TODO: implement
}
setPolarity(polarity: number) {
// Either 1 or 255 (reverse)
this.polarity = polarity === 255;
// TODO: implement
}
reset() {
// TODO: implement
}
stop() {
// TODO: implement
this.setSpeed(0);
2017-12-18 22:04:17 +01:00
}
start() {
// TODO: implement
this.setChangedState();
2017-12-18 22:04:17 +01:00
}
public getAngle() {
return this.angle;
}
2017-12-20 01:03:26 +01:00
updateState(elapsed: number) {
const rotations = this.getSpeed() / 100 * this.rotationsPerMilliSecond * elapsed;
const angle = rotations * 360;
if (angle) {
this.angle += angle;
this.setChangedState();
}
}
2017-12-18 22:04:17 +01:00
}
export class MediumMotorNode extends MotorNode {
id = NodeType.MediumMotor;
constructor(port: number) {
2017-12-20 01:03:26 +01:00
super(port, 250);
}
2017-12-18 22:04:17 +01:00
}
export class LargeMotorNode extends MotorNode {
id = NodeType.LargeMotor;
constructor(port: number) {
2017-12-20 01:03:26 +01:00
super(port, 170);
}
2017-12-18 22:04:17 +01:00
}
}