fixing motor compilation
This commit is contained in:
parent
8bf6f265f7
commit
363e076f36
@ -55,7 +55,6 @@
|
||||
"motors.Motor.move|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
|
||||
"motors.Motor.move|param|unit": "the meaning of the value",
|
||||
"motors.Motor.move|param|value": "the move quantity, eg: 2",
|
||||
"motors.Motor.port": "Gets the port where this motor is connected",
|
||||
"motors.Motor.reset": "Resets the motor(s).",
|
||||
"motors.Motor.setBrake": "Sets the automatic brake on or off when the motor is off",
|
||||
"motors.Motor.setBrake|param|brake": "a value indicating if the motor should break when off",
|
||||
|
@ -96,31 +96,31 @@ namespace motors {
|
||||
export class Motor extends control.Component {
|
||||
protected _port: Output;
|
||||
protected _brake: boolean;
|
||||
private _initialized: boolean;
|
||||
private _init: () => void;
|
||||
private _setSpeed: (speed: number) => void;
|
||||
private _move: (steps: boolean, stepsOrTime: number, speed: number) => void;
|
||||
|
||||
constructor(port: Output) {
|
||||
constructor(port: Output, init: () => void, setSpeed: (speed: number) => void, move: (steps: boolean, stepsOrTime: number, speed: number) => void) {
|
||||
super();
|
||||
this._port = port;
|
||||
this._brake = false;
|
||||
this._initialized = false;
|
||||
this._init = init;
|
||||
this._setSpeed = setSpeed;
|
||||
this._move = move;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazy initialization code
|
||||
*/
|
||||
protected __init() {
|
||||
|
||||
protected init() {
|
||||
if (!this._initialized) {
|
||||
this._initialized = true;
|
||||
this._init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the port where this motor is connected
|
||||
*/
|
||||
//%
|
||||
//% group="Motion"
|
||||
port(): Output {
|
||||
this.__init();
|
||||
return this._port;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the automatic brake on or off when the motor is off
|
||||
* @param brake a value indicating if the motor should break when off
|
||||
@ -130,7 +130,7 @@ namespace motors {
|
||||
//% weight=60 blockGap=8
|
||||
//% group="Motion"
|
||||
setBrake(brake: boolean) {
|
||||
this.__init();
|
||||
this.init();
|
||||
this._brake = brake;
|
||||
}
|
||||
|
||||
@ -142,7 +142,7 @@ namespace motors {
|
||||
//% weight=59
|
||||
//% group="Motion"
|
||||
setReversed(reversed: boolean) {
|
||||
this.__init();
|
||||
this.init();
|
||||
const b = mkCmd(this._port, DAL.opOutputPolarity, 1)
|
||||
b.setNumber(NumberFormat.Int8LE, 2, reversed ? 0 : 1);
|
||||
writePWM(b)
|
||||
@ -153,7 +153,7 @@ namespace motors {
|
||||
*/
|
||||
//%
|
||||
stop() {
|
||||
this.__init();
|
||||
this.init();
|
||||
stop(this._port, this._brake);
|
||||
}
|
||||
|
||||
@ -162,7 +162,7 @@ namespace motors {
|
||||
*/
|
||||
//%
|
||||
reset() {
|
||||
this.__init();
|
||||
this.init();
|
||||
reset(this._port);
|
||||
}
|
||||
|
||||
@ -176,15 +176,12 @@ namespace motors {
|
||||
//% speed.min=-100 speed.max=100
|
||||
//% group="Motion"
|
||||
setSpeed(speed: number) {
|
||||
this.__init();
|
||||
this.init();
|
||||
speed = Math.clamp(-100, 100, speed >> 0);
|
||||
if (!speed) // always stop
|
||||
this.stop();
|
||||
else
|
||||
this.__setSpeed(speed);
|
||||
}
|
||||
|
||||
protected __setSpeed(speed: number) {
|
||||
this._setSpeed(speed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,7 +195,7 @@ namespace motors {
|
||||
//% speed.min=-100 speed.max=100
|
||||
//% group="Motion"
|
||||
move(value: number, unit: MoveUnit, speed: number) {
|
||||
this.__init();
|
||||
this.init();
|
||||
speed = Math.clamp(-100, 100, speed >> 0);
|
||||
if (!speed) {
|
||||
this.stop();
|
||||
@ -221,40 +218,33 @@ namespace motors {
|
||||
break;
|
||||
}
|
||||
|
||||
this.__move(useSteps, stepsOrTime, speed);
|
||||
}
|
||||
|
||||
protected __move(steps: boolean, stepsOrTime: number, speed: number) {
|
||||
this._move(useSteps, stepsOrTime, speed);
|
||||
}
|
||||
}
|
||||
|
||||
//% fixedInstances
|
||||
export class SingleMotor extends Motor {
|
||||
private _large: boolean;
|
||||
private _initialized: boolean;
|
||||
|
||||
constructor(port: Output, large: boolean) {
|
||||
super(port);
|
||||
super(port, () => this.__init(), (speed) => this.__setSpeed(speed), (steps, stepsOrTime, speed) => this.__move(steps, stepsOrTime, speed));
|
||||
this._large = large;
|
||||
}
|
||||
|
||||
protected __init() {
|
||||
if (!this._initialized) {
|
||||
this._initialized = true;
|
||||
private __init() {
|
||||
// specify motor size on this port
|
||||
const b = mkCmd(outOffset(this._port), DAL.opOutputSetType, 1)
|
||||
b.setNumber(NumberFormat.Int8LE, 2, this._large ? 0x07 : 0x08)
|
||||
writePWM(b)
|
||||
}
|
||||
}
|
||||
|
||||
protected __setSpeed(speed: number) {
|
||||
private __setSpeed(speed: number) {
|
||||
const b = mkCmd(this._port, DAL.opOutputSpeed, 1)
|
||||
b.setNumber(NumberFormat.Int8LE, 2, speed)
|
||||
writePWM(b)
|
||||
}
|
||||
|
||||
protected __move(steps: boolean, stepsOrTime: number, speed: number) {
|
||||
private __move(steps: boolean, stepsOrTime: number, speed: number) {
|
||||
step(this._port, {
|
||||
useSteps: steps,
|
||||
step1: 0,
|
||||
@ -273,7 +263,7 @@ namespace motors {
|
||||
//% weight=72 blockGap=8
|
||||
//% group="Sensors"
|
||||
speed(): number {
|
||||
this.__init();
|
||||
this.init();
|
||||
return getMotorData(this._port).actualSpeed;
|
||||
}
|
||||
|
||||
@ -285,7 +275,7 @@ namespace motors {
|
||||
//% weight=71 blockGap=8
|
||||
//% group="Sensors"
|
||||
count(): number {
|
||||
this.__init();
|
||||
this.init();
|
||||
return getMotorData(this._port).count;
|
||||
}
|
||||
|
||||
@ -297,7 +287,7 @@ namespace motors {
|
||||
//% weight=70
|
||||
//% group="Sensors"
|
||||
tachoCount(): number {
|
||||
this.__init();
|
||||
this.init();
|
||||
return getMotorData(this._port).tachoCount;
|
||||
}
|
||||
|
||||
@ -306,7 +296,7 @@ namespace motors {
|
||||
*/
|
||||
//% group="Motion"
|
||||
clearCount() {
|
||||
this.__init();
|
||||
this.init();
|
||||
const b = mkCmd(this._port, DAL.opOutputClearCount, 0)
|
||||
writePWM(b)
|
||||
for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
|
||||
@ -343,15 +333,12 @@ namespace motors {
|
||||
|
||||
//% fixedInstances
|
||||
export class SynchedMotorPair extends Motor {
|
||||
private _initialized: boolean;
|
||||
|
||||
constructor(ports: Output) {
|
||||
super(ports);
|
||||
super(ports, () => this.__init(), (speed) => this.__setSpeed(speed), (steps, stepsOrTime, speed) => this.__move(steps, stepsOrTime, speed));
|
||||
}
|
||||
|
||||
protected __init() {
|
||||
if (!this._initialized) {
|
||||
this._initialized = true;
|
||||
private __init() {
|
||||
for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
|
||||
if (this._port & (1 << i)) {
|
||||
const b = mkCmd(outOffset(1 << i), DAL.opOutputSetType, 1)
|
||||
@ -360,9 +347,8 @@ namespace motors {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected __setSpeed(speed: number) {
|
||||
private __setSpeed(speed: number) {
|
||||
syncMotors(this._port, {
|
||||
speed: speed,
|
||||
turnRatio: 0,
|
||||
@ -370,7 +356,7 @@ namespace motors {
|
||||
})
|
||||
}
|
||||
|
||||
protected __move(steps: boolean, stepsOrTime: number, speed: number) {
|
||||
private __move(steps: boolean, stepsOrTime: number, speed: number) {
|
||||
syncMotors(this._port, {
|
||||
useSteps: steps,
|
||||
speed: speed,
|
||||
@ -393,7 +379,7 @@ namespace motors {
|
||||
//% inlineInputMode=inline
|
||||
//% group="Chassis"
|
||||
steer(steering: number, speed: number, value: number, unit: MoveUnit) {
|
||||
this.__init();
|
||||
this.init();
|
||||
speed = Math.clamp(-100, 100, speed >> 0);
|
||||
if (!speed) {
|
||||
stop(this._port, this._brake);
|
||||
|
Loading…
Reference in New Issue
Block a user