refactoring motors wiht base class (#104)

This commit is contained in:
Peli de Halleux 2017-12-17 22:47:13 -08:00 committed by GitHub
parent 944098b9f9
commit 6836852122
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 156 additions and 148 deletions

View File

@ -51,34 +51,31 @@
"control.raiseEvent": "Announce that an event happened to registered handlers.", "control.raiseEvent": "Announce that an event happened to registered handlers.",
"control.raiseEvent|param|src": "ID of the Component that generated the event", "control.raiseEvent|param|src": "ID of the Component that generated the event",
"control.raiseEvent|param|value": "Component specific code indicating the cause of the event.", "control.raiseEvent|param|value": "Component specific code indicating the cause of the event.",
"motors.Motor.clearCount": "Clears the motor count",
"motors.Motor.count": "Gets motor step count.",
"motors.Motor.move": "Moves the motor by a number of rotations, degress or seconds", "motors.Motor.move": "Moves the motor by a number of rotations, degress or seconds",
"motors.Motor.move|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50", "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|unit": "the meaning of the value",
"motors.Motor.move|param|value": "the move quantity, eg: 2", "motors.Motor.move|param|value": "the move quantity, eg: 2",
"motors.Motor.port": "Gets the port where this motor is connected", "motors.Motor.port": "Gets the port where this motor is connected",
"motors.Motor.reset": "Resets the motor.", "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": "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", "motors.Motor.setBrake|param|brake": "a value indicating if the motor should break when off",
"motors.Motor.setReversed": "Reverses the motor polarity", "motors.Motor.setReversed": "Reverses the motor polarity",
"motors.Motor.setSpeed": "Sets the speed of the motor.", "motors.Motor.setSpeed": "Sets the speed of the motor.",
"motors.Motor.setSpeed|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50", "motors.Motor.setSpeed|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.Motor.speed": "Gets motor actual speed.", "motors.Motor.stop": "Stops the motor(s).",
"motors.Motor.tachoCount": "Gets motor tacho count.", "motors.SingleMotor.clearCount": "Clears the motor count",
"motors.SynchedMotorPair.moveSteering": "Turns the motor and the follower motor by a number of rotations", "motors.SingleMotor.count": "Gets motor step count.",
"motors.SynchedMotorPair.moveSteering|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50", "motors.SingleMotor.speed": "Gets motor actual speed.",
"motors.SynchedMotorPair.moveSteering|param|steering": "the ratio of power sent to the follower motor, from ``-100`` to ``100``", "motors.SingleMotor.tachoCount": "Gets motor tacho count.",
"motors.SynchedMotorPair.moveSteering|param|unit": "the meaning of the value", "motors.SynchedMotorPair.steer": "Turns the motor and the follower motor by a number of rotations",
"motors.SynchedMotorPair.moveSteering|param|value": "the move quantity, eg: 2", "motors.SynchedMotorPair.steer|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.SynchedMotorPair.moveTank": "The Move Tank block can make a robot drive forward, backward, turn, or stop. \nUse the Move Tank block for robot vehicles that have two Large Motors, \nwith one motor driving the left side of the vehicle and the other the right side. \nYou can make the two motors go at different speeds or in different directions \nto make your robot turn.", "motors.SynchedMotorPair.steer|param|steering": "the ratio of power sent to the follower motor, from ``-100`` to ``100``",
"motors.SynchedMotorPair.moveTank|param|speedRight": "the speed on the right motor, eg: 50", "motors.SynchedMotorPair.steer|param|unit": "the meaning of the value",
"motors.SynchedMotorPair.moveTank|param|unit": "@param speedLeft the speed on the left motor, eg: 50", "motors.SynchedMotorPair.steer|param|value": "the move quantity, eg: 2",
"motors.SynchedMotorPair.moveTank|param|value": "the amount of movement, eg: 2", "motors.SynchedMotorPair.tank": "The Move Tank block can make a robot drive forward, backward, turn, or stop. \nUse the Move Tank block for robot vehicles that have two Large Motors, \nwith one motor driving the left side of the vehicle and the other the right side. \nYou can make the two motors go at different speeds or in different directions \nto make your robot turn.",
"motors.SynchedMotorPair.setBrake": "Sets the automatic brake on or off when the motor is off", "motors.SynchedMotorPair.tank|param|speedRight": "the speed on the right motor, eg: 50",
"motors.SynchedMotorPair.setBrake|param|brake": "a value indicating if the motor should break when off", "motors.SynchedMotorPair.tank|param|unit": "@param speedLeft the speed on the left motor, eg: 50",
"motors.SynchedMotorPair.setSpeed": "Sets the speed of the motor.", "motors.SynchedMotorPair.tank|param|value": "the amount of movement, eg: 2",
"motors.SynchedMotorPair.setSpeed|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.stopAllMotors": "Stops all motors", "motors.stopAllMotors": "Stops all motors",
"output.createBuffer": "Create a new zero-initialized buffer.", "output.createBuffer": "Create a new zero-initialized buffer.",
"output.createBuffer|param|size": "number of bytes in the buffer", "output.createBuffer|param|size": "number of bytes in the buffer",

View File

@ -43,18 +43,15 @@
"brick|block": "brick", "brick|block": "brick",
"control.raiseEvent|block": "raise event|from %src|with value %value", "control.raiseEvent|block": "raise event|from %src|with value %value",
"control|block": "control", "control|block": "control",
"motors.Motor.count|block": "`icons.motorLarge` %motor|count",
"motors.Motor.move|block": "move `icons.motorLarge` %motor|for %value|%unit|at %speed|%", "motors.Motor.move|block": "move `icons.motorLarge` %motor|for %value|%unit|at %speed|%",
"motors.Motor.reset|block": "reset `icons.motorLarge` %motor",
"motors.Motor.setBrake|block": "set `icons.motorLarge` %motor|brake %brake", "motors.Motor.setBrake|block": "set `icons.motorLarge` %motor|brake %brake",
"motors.Motor.setReversed|block": "set `icons.motorLarge` %motor|reversed %reversed", "motors.Motor.setReversed|block": "set `icons.motorLarge` %motor|reversed %reversed",
"motors.Motor.setSpeed|block": "set speed of `icons.motorLarge` %motor|to %speed|%", "motors.Motor.setSpeed|block": "set speed of `icons.motorLarge` %motor|to %speed|%",
"motors.Motor.speed|block": "`icons.motorLarge` %motor|speed", "motors.SingleMotor.count|block": "`icons.motorLarge` %motor|count",
"motors.Motor.tachoCount|block": "`icons.motorLarge` %motor|tacho count", "motors.SingleMotor.speed|block": "`icons.motorLarge` %motor|speed",
"motors.SynchedMotorPair.moveSteering|block": "move steering %chassis|at %speed|%|steer %turnRadio|%|by %value|%unit", "motors.SingleMotor.tachoCount|block": "`icons.motorLarge` %motor|tacho count",
"motors.SynchedMotorPair.moveTank|block": "move tank %chassis|left %speedLeft|%|right %speedRight|%|by %value|%unit", "motors.SynchedMotorPair.steer|block": "steer %chassis|%steering|%|at speed %speed|%|by %value|%unit",
"motors.SynchedMotorPair.setBrake|block": "set `icons.motorLarge` %chassis|brake %brake", "motors.SynchedMotorPair.tank|block": "tank %chassis|left %speedLeft|%|right %speedRight|%|by %value|%unit",
"motors.SynchedMotorPair.setSpeed|block": "set speed of `icons.motorLarge` %motor|to %speed|%",
"motors.largeAB|block": "large A+B", "motors.largeAB|block": "large A+B",
"motors.largeAD|block": "large A+D", "motors.largeAD|block": "large A+D",
"motors.largeA|block": "large A", "motors.largeA|block": "large A",

View File

@ -94,26 +94,20 @@ namespace motors {
//% fixedInstances //% fixedInstances
export class Motor extends control.Component { export class Motor extends control.Component {
private _port: Output; protected _port: Output;
private _large: boolean; protected _brake: boolean;
private _initialized: boolean; constructor(port: Output) {
private _brake: boolean;
constructor(port: Output, large: boolean) {
super(); super();
this._port = port; this._port = port;
this._large = large;
this._brake = false; this._brake = false;
} }
private __init() { /**
if (!this._initialized) { * Lazy initialization code
// specify motor size on this port */
const b = mkCmd(this._port, DAL.opOutputSetType, 1) protected __init() {
b.setNumber(NumberFormat.Int8LE, 2, this._large ? 0x07 : 0x08)
writePWM(b)
}
} }
/** /**
@ -122,9 +116,56 @@ namespace motors {
//% //%
//% group="Motion" //% group="Motion"
port(): Output { port(): Output {
this.__init();
return this._port; 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
*/
//% blockId=outputMotorSetBrakeMode block="set `icons.motorLarge` %motor|brake %brake"
//% brake.fieldEditor=toggleonoff
//% weight=60 blockGap=8
//% group="Motion"
setBrake(brake: boolean) {
this.__init();
this._brake = brake;
}
/**
* Reverses the motor polarity
*/
//% blockId=motorSetReversed block="set `icons.motorLarge` %motor|reversed %reversed"
//% reversed.fieldEditor=toggleonoff
//% weight=59
//% group="Motion"
setReversed(reversed: boolean) {
this.__init();
const b = mkCmd(this._port, DAL.opOutputPolarity, 1)
b.setNumber(NumberFormat.Int8LE, 2, reversed ? -1 : 1);
writePWM(b)
}
/**
* Stops the motor(s).
*/
//%
stop() {
this.__init();
stop(this._port, this._brake);
}
/**
* Resets the motor(s).
*/
//%
reset() {
this.__init();
reset(this._port);
}
/** /**
* Sets the speed of the motor. * Sets the speed of the motor.
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50 * @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50
@ -137,15 +178,15 @@ namespace motors {
setSpeed(speed: number) { setSpeed(speed: number) {
this.__init(); this.__init();
speed = Math.clamp(-100, 100, speed >> 0); speed = Math.clamp(-100, 100, speed >> 0);
if (!speed) { // always stop if (!speed) // always stop
this.stop(); this.stop();
} else { else
const b = mkCmd(this._port, DAL.opOutputSpeed, 1) this.__setSpeed(speed);
b.setNumber(NumberFormat.Int8LE, 2, speed)
writePWM(b)
}
} }
protected __setSpeed(speed: number) {
}
/** /**
* Moves the motor by a number of rotations, degress or seconds * Moves the motor by a number of rotations, degress or seconds
* @param value the move quantity, eg: 2 * @param value the move quantity, eg: 2
@ -157,17 +198,12 @@ namespace motors {
//% speed.min=-100 speed.max=100 //% speed.min=-100 speed.max=100
//% group="Motion" //% group="Motion"
move(value: number, unit: MoveUnit, speed: number) { move(value: number, unit: MoveUnit, speed: number) {
this.output(value, unit, speed, 0);
}
private output(value: number, unit: MoveUnit, speed: number, turnRatio: number) {
this.__init(); this.__init();
speed = Math.clamp(-100, 100, speed >> 0); speed = Math.clamp(-100, 100, speed >> 0);
if (!speed) { if (!speed) {
this.stop(); this.stop();
return; return;
} }
turnRatio = Math.clamp(-200, 200, turnRatio >> 0);
let useSteps: boolean; let useSteps: boolean;
let stepsOrTime: number; let stepsOrTime: number;
switch (unit) { switch (unit) {
@ -185,8 +221,42 @@ namespace motors {
break; break;
} }
this.__move(useSteps, stepsOrTime, speed);
}
protected __move(steps: boolean, stepsOrTime: number, speed: number) {
}
}
//% fixedInstances
export class SingleMotor extends Motor {
private _large: boolean;
private _initialized: boolean;
constructor(port: Output, large: boolean) {
super(port);
this._large = large;
}
protected __init() {
if (!this._initialized) {
this._initialized = true;
// specify motor size on this port
const b = mkCmd(this._port, DAL.opOutputSetType, 1)
b.setNumber(NumberFormat.Int8LE, 2, this._large ? 0x07 : 0x08)
writePWM(b)
}
}
protected __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) {
step(this._port, { step(this._port, {
useSteps: useSteps, useSteps: steps,
step1: 0, step1: 0,
step2: stepsOrTime, step2: stepsOrTime,
step3: 0, step3: 0,
@ -195,41 +265,6 @@ namespace motors {
}) })
} }
/**
* Stops the motor
*/
private stop() {
this.__init();
stop(this._port, this._brake);
}
/**
* Sets the automatic brake on or off when the motor is off
* @param brake a value indicating if the motor should break when off
*/
//% blockId=outputMotorSetBrakeMode block="set `icons.motorLarge` %motor|brake %brake"
//% brake.fieldEditor=toggleonoff
//% weight=60 blockGap=8
//% group="Motion"
setBrake(brake: boolean) {
this.__init();
this._brake = brake;
}
/**
* Reverses the motor polarity
*/
//% blockId=motorSetReversed block="set `icons.motorLarge` %motor|reversed %reversed"
//% reversed.fieldEditor=toggleonoff
//% weight=59
//% group="Motion"
setReversed(reversed: boolean) {
this.__init();
const b = mkCmd(this._port, DAL.opOutputPolarity, 1)
b.setNumber(NumberFormat.Int8LE, 2, reversed ? -1 : 1);
writePWM(b)
}
/** /**
* Gets motor actual speed. * Gets motor actual speed.
* @param motor the port which connects to the motor * @param motor the port which connects to the motor
@ -280,104 +315,83 @@ namespace motors {
} }
} }
} }
/**
* Resets the motor.
*/
//% blockId=motorReset block="reset `icons.motorLarge` %motor"
//% weight=20
//% group="Motion"
reset() {
this.__init();
reset(this._port);
}
} }
//% whenUsed fixedInstance block="large A" //% whenUsed fixedInstance block="large A"
export const largeA = new Motor(Output.A, true); export const largeA = new SingleMotor(Output.A, true);
//% whenUsed fixedInstance block="large B" //% whenUsed fixedInstance block="large B"
export const largeB = new Motor(Output.B, true); export const largeB = new SingleMotor(Output.B, true);
//% whenUsed fixedInstance block="large C" //% whenUsed fixedInstance block="large C"
export const largeC = new Motor(Output.C, true); export const largeC = new SingleMotor(Output.C, true);
//% whenUsed fixedInstance block="large D" //% whenUsed fixedInstance block="large D"
export const largeD = new Motor(Output.D, true); export const largeD = new SingleMotor(Output.D, true);
//% whenUsed fixedInstance block="medium A" //% whenUsed fixedInstance block="medium A"
export const mediumA = new Motor(Output.A, false); export const mediumA = new SingleMotor(Output.A, false);
//% whenUsed fixedInstance block="medium B" //% whenUsed fixedInstance block="medium B"
export const mediumB = new Motor(Output.B, false); export const mediumB = new SingleMotor(Output.B, false);
//% whenUsed fixedInstance block="medium C" //% whenUsed fixedInstance block="medium C"
export const mediumC = new Motor(Output.C, false); export const mediumC = new SingleMotor(Output.C, false);
//% whenUsed fixedInstance block="medium D" //% whenUsed fixedInstance block="medium D"
export const mediumD = new Motor(Output.D, false); export const mediumD = new SingleMotor(Output.D, false);
//% fixedInstances //% fixedInstances
export class SynchedMotorPair extends control.Component { export class SynchedMotorPair extends Motor {
private _ports: Output; private _initialized: boolean;
private _brake: boolean;
constructor(ports: Output) { constructor(ports: Output) {
super(); super(ports);
this._ports = ports;
this._brake = false;
} }
/** protected __init() {
* Sets the speed of the motor. if (!this._initialized) {
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50 this._initialized = true;
*/ const b = mkCmd(this._port, DAL.opOutputSetType, 1)
//% blockId=motorPairSetSpeed block="set speed of `icons.motorLarge` %motor|to %speed|%" b.setNumber(NumberFormat.Int8LE, 2, 0x07) // large motor
//% on.fieldEditor=toggleonoff writePWM(b)
//% weight=99 blockGap=8
//% speed.min=-100 speed.max=100
//% group="Chassis"
setSpeed(speed: number) {
speed = Math.clamp(speed >> 0, -100, 100);
if (!speed) {
stop(this._ports, this._brake);
return;
} }
syncMotors(this._ports, { }
protected __setSpeed(speed: number) {
syncMotors(this._port, {
speed: speed, speed: speed,
turnRatio: 0, turnRatio: 0,
useBrake: !!this._brake useBrake: !!this._brake
}) })
} }
/** protected __move(steps: boolean, stepsOrTime: number, speed: number) {
* Sets the automatic brake on or off when the motor is off syncMotors(this._port, {
* @param brake a value indicating if the motor should break when off useSteps: steps,
*/ speed: speed,
//% blockId=motorPairSetBrakeMode block="set `icons.motorLarge` %chassis|brake %brake" turnRatio: 100, // same speed
//% brake.fieldEditor=toggleonoff stepsOrTime: stepsOrTime,
//% weight=60 blockGap=8 useBrake: this._brake
//% group="Chassis" });
setBrake(brake: boolean) { }
this._brake = brake;
}
/** /**
* Turns the motor and the follower motor by a number of rotations * Turns the motor and the follower motor by a number of rotations
* @param value the move quantity, eg: 2 * @param value the move quantity, eg: 2
* @param unit the meaning of the value * @param unit the meaning of the value
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50
* @param steering the ratio of power sent to the follower motor, from ``-100`` to ``100`` * @param steering the ratio of power sent to the follower motor, from ``-100`` to ``100``
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50
*/ */
//% blockId=motorPairTurn block="move steering %chassis|at %speed|%|steer %turnRadio|%|by %value|%unit" //% blockId=motorPairTurn block="steer %chassis|%steering|%|at speed %speed|%|by %value|%unit"
//% weight=9 blockGap=8 //% weight=9 blockGap=8
//% steering.min=-100 steering=100 //% steering.min=-100 steering=100
//% inlineInputMode=inline //% inlineInputMode=inline
//% group="Chassis" //% group="Chassis"
moveSteering(steering: number, speed: number, value: number, unit: MoveUnit) { steer(steering: number, speed: number, value: number, unit: MoveUnit) {
speed = Math.clamp(-100, 100, speed >> 0); speed = Math.clamp(-100, 100, speed >> 0);
if (!speed) { if (!speed) {
stop(this._ports, this._brake); stop(this._port, this._brake);
return; return;
} }
@ -399,7 +413,7 @@ namespace motors {
break; break;
} }
syncMotors(this._ports, { syncMotors(this._port, {
useSteps: useSteps, useSteps: useSteps,
speed: speed, speed: speed,
turnRatio: turnRatio, turnRatio: turnRatio,
@ -419,17 +433,17 @@ namespace motors {
* @param speedLeft the speed on the left motor, eg: 50 * @param speedLeft the speed on the left motor, eg: 50
* @param speedRight the speed on the right motor, eg: 50 * @param speedRight the speed on the right motor, eg: 50
*/ */
//% blockId=motorPairTank block="move tank %chassis|left %speedLeft|%|right %speedRight|%|by %value|%unit" //% blockId=motorPairTank block="tank %chassis|left %speedLeft|%|right %speedRight|%|by %value|%unit"
//% weight=9 blockGap=8 //% weight=9 blockGap=8
//% speedLeft.min=-100 speedLeft=100 //% speedLeft.min=-100 speedLeft=100
//% speedRight.min=-100 speedRight=100 //% speedRight.min=-100 speedRight=100
//% inlineInputMode=inline //% inlineInputMode=inline
//% group="Chassis" //% group="Chassis"
moveTank(speedLeft: number, speedRight: number, value: number, unit: MoveUnit) { tank(speedLeft: number, speedRight: number, value: number, unit: MoveUnit) {
speedLeft = Math.clamp(speedLeft >> 0, -100, 100); speedLeft = Math.clamp(speedLeft >> 0, -100, 100);
speedRight = Math.clamp(speedRight >> 0, -100, 100); speedRight = Math.clamp(speedRight >> 0, -100, 100);
const steering = (speedRight * 100 / speedLeft) >> 0; const steering = (speedRight * 100 / speedLeft) >> 0;
this.moveSteering(speedLeft, steering, value, unit); this.steer(speedLeft, steering, value, unit);
} }
} }