support for unregulated motors (#227)

This commit is contained in:
Peli de Halleux 2018-01-10 23:34:27 -08:00 committed by GitHub
parent a4164470d8
commit c3419c0b74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -61,6 +61,8 @@
"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.angle": "Gets motor angle.", "motors.Motor.angle": "Gets motor angle.",
"motors.Motor.clearCounts": "Clears the motor count", "motors.Motor.clearCounts": "Clears the motor count",
"motors.Motor.setRegulated": "Indicates if the motor speed should be regulated. Default is true.",
"motors.Motor.setRegulated|param|value": "true for regulated motor",
"motors.Motor.speed": "Gets motor actual speed.", "motors.Motor.speed": "Gets motor actual speed.",
"motors.Motor.tacho": "Gets motor tachometer count.", "motors.Motor.tacho": "Gets motor tachometer count.",
"motors.Motor.toString": "Returns the status of the motor", "motors.Motor.toString": "Returns the status of the motor",

View File

@ -52,6 +52,7 @@
"control|block": "control", "control|block": "control",
"motors.Motor.angle|block": "%motor|angle", "motors.Motor.angle|block": "%motor|angle",
"motors.Motor.clearCounts|block": "%motor|clear counts", "motors.Motor.clearCounts|block": "%motor|clear counts",
"motors.Motor.setRegulated|block": "set %motor|regulated %value",
"motors.Motor.speed|block": "%motor|speed", "motors.Motor.speed|block": "%motor|speed",
"motors.Motor.tacho|block": "%motor|tacho", "motors.Motor.tacho|block": "%motor|tacho",
"motors.MotorBase.pauseUntilReady|block": "%motor|pause until ready", "motors.MotorBase.pauseUntilReady|block": "%motor|pause until ready",

View File

@ -175,7 +175,7 @@ namespace motors {
*/ */
//% blockId=motorSetReversed block="set %motor|reversed %reversed" //% blockId=motorSetReversed block="set %motor|reversed %reversed"
//% reversed.fieldEditor=toggleonoff //% reversed.fieldEditor=toggleonoff
//% weight=59 //% weight=59 blockGap=8
//% group="Move" //% group="Move"
setReversed(reversed: boolean) { setReversed(reversed: boolean) {
this.init(); this.init();
@ -282,10 +282,12 @@ namespace motors {
//% fixedInstances //% fixedInstances
export class Motor extends MotorBase { export class Motor extends MotorBase {
private _large: boolean; private _large: boolean;
private _regulated: boolean;
constructor(port: Output, large: boolean) { constructor(port: Output, large: boolean) {
super(port, () => this.__init(), (speed) => this.__setSpeed(speed), (steps, stepsOrTime, speed) => this.__move(steps, stepsOrTime, speed)); super(port, () => this.__init(), (speed) => this.__setSpeed(speed), (steps, stepsOrTime, speed) => this.__move(steps, stepsOrTime, speed));
this._large = large; this._large = large;
this._regulated = true;
this.markUsed(); this.markUsed();
} }
@ -301,7 +303,7 @@ namespace motors {
} }
private __setSpeed(speed: number) { private __setSpeed(speed: number) {
const b = mkCmd(this._port, DAL.opOutputSpeed, 1) const b = mkCmd(this._port, this._regulated ? DAL.opOutputPower : DAL.opOutputSpeed, 1)
b.setNumber(NumberFormat.Int8LE, 2, speed) b.setNumber(NumberFormat.Int8LE, 2, speed)
writePWM(b) writePWM(b)
if (speed) { if (speed) {
@ -315,11 +317,24 @@ namespace motors {
step1: 0, step1: 0,
step2: stepsOrTime, step2: stepsOrTime,
step3: 0, step3: 0,
speed: speed, speed: this._regulated ? speed : undefined,
power: this._regulated ? undefined : speed,
useBrake: this._brake useBrake: this._brake
}) })
} }
/**
* Indicates if the motor speed should be regulated. Default is true.
* @param value true for regulated motor
*/
//% blockId=outputMotorSetRegulated block="set %motor|regulated %value"
//% value.fieldEditor=toggleonoff
//% weight=58
//% group="Move"
setRegulated(value: boolean) {
this._regulated = value;
}
/** /**
* 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