From 712c2178d23b95a7d2ddb6c8ba44acc0bbdd20e0 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 27 Oct 2017 01:47:25 -0700 Subject: [PATCH] simplify motor API --- docs/maker/puppet.md | 10 ++- libs/core/_locales/core-jsdoc-strings.json | 14 +--- libs/core/_locales/core-strings.json | 6 +- libs/core/output.ts | 91 +++++++--------------- 4 files changed, 42 insertions(+), 79 deletions(-) diff --git a/docs/maker/puppet.md b/docs/maker/puppet.md index b0556e59..231c2e95 100644 --- a/docs/maker/puppet.md +++ b/docs/maker/puppet.md @@ -4,8 +4,14 @@ Use this program with the Programmable Brick and Large Motor. ```blocks loops.forever(function () { - output.largeMotorA.onForAngle(30, 30, false) + output.largeMotorA.setPower(30) + output.largeMotorA.on(true) + loops.pause(100) + output.largeMotorA.on(false) music.playSoundUntilDone(music.sounds(Sounds.PowerUp)) - output.largeMotorA.onForAngle(40, -30, false) + output.largeMotorA.setPower(-30) + output.largeMotorA.on(true) + loops.pause(100) + output.largeMotorA.on(false) }) ``` diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 6bda4c69..2c3d986c 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -56,17 +56,9 @@ "input.remoteButtonCenter": "Remote beacon (center) button.", "input.remoteButtonTopLeft": "Remote top-left button.", "input.remoteButtonTopRight": "Remote top-right button.", - "output.Motor.off": "Power off the motor.", - "output.Motor.on": "Power on the motor.", - "output.Motor.onForAngle": "Powers on the motor for a specified number of milliseconds.", - "output.Motor.onForAngle|param|brake": "whether or not to use the brake", - "output.Motor.onForAngle|param|degrees": "the number of degrees to turn, eg: 90", - "output.Motor.onForAngle|param|power": "the motor power level from ``-100`` to ``100``, eg: 50", - "output.Motor.onForTime": "Powers on the motor for a specified number of milliseconds.", - "output.Motor.onForTime|param|brake": "whether or not to use the brake", - "output.Motor.onForTime|param|milliseconds": "the number of milliseconds to turn the motor on, eg: 500", - "output.Motor.onForTime|param|power": "the motor power level from ``-100`` to ``100``, eg: 50", - "output.Motor.on|param|power": "the motor power level from ``-100`` to ``100``, eg: 50", + "output.Motor.on": "Power on or off the motor.", + "output.Motor.setBrake": "Sets the automatic brake on or off when the motor is off", + "output.Motor.setBrake|param|brake": "a value indicating if the motor should break when off", "output.Motor.setPower": "Sets the motor power level from ``-100`` to ``100``.", "output.Motor.setPower|param|power": "the desired speed to use. eg: 50", "output.Motor.speed": "Gets motor actual speed.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 6b4d90ae..db12c922 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -82,10 +82,8 @@ "input.ultrasonic3|block": "ultrasonic sensor 3", "input.ultrasonic4|block": "ultrasonic sensor 4", "input|block": "input", - "output.Motor.off|block": "%motor|OFF then brake %brake", - "output.Motor.onForAngle|block": "%motor|ON at power %power|for %degrees|deg then brake %brake", - "output.Motor.onForTime|block": "%motor|ON at power %power|for %milliseconds=timePicker|ms then brake %brake", - "output.Motor.on|block": "%motor|ON at power %power", + "output.Motor.on|block": "%motor|%onOrOff", + "output.Motor.setBrake|block": "%motor|set brake %brake", "output.Motor.setPower|block": "%motor|set power to %speed", "output.Motor.speed|block": "%motor|speed", "output.largeMotorA|block": "large motor A", diff --git a/libs/core/output.ts b/libs/core/output.ts index 04ca603f..a44cbcdf 100644 --- a/libs/core/output.ts +++ b/libs/core/output.ts @@ -65,78 +65,34 @@ namespace output { //% fixedInstances export class Motor extends control.Component { - port: Output; - large: boolean; + private port: Output; + private large: boolean; + private brake: boolean; + constructor(port: Output, large: boolean) { super(); this.port = port; this.large = large; + this.brake = false; } /** - * Power off the motor. - * @param motor the motor to turn off - */ - //% blockId=outputMotorOf block="%motor|OFF then brake %brake" - //% brake.fieldEditor=toggleonoff - //% weight=100 group="Motors" blockGap=8 - off(brake = false) { - const b = mkCmd(this.port, DAL.opOutputStop, 1) - b.setNumber(NumberFormat.UInt8LE, 2, brake ? 1 : 0) - writePWM(b) - } - - /** - * Power on the motor. + * Power on or off the motor. * @param motor the motor to turn on * @param power the motor power level from ``-100`` to ``100``, eg: 50 */ - //% blockId=outputMotorOn block="%motor|ON at power %power" - //% power.min=-100 power.max=100 + //% blockId=outputMotorOn block="%motor|%onOrOff" //% weight=99 group="Motors" blockGap=8 - on(power: number = 50) { - this.setPower(power); - const b = mkCmd(this.port, DAL.opOutputStart, 0) - writePWM(b); - } - - /** - * Powers on the motor for a specified number of milliseconds. - * @param motor the motor to turn on - * @param power the motor power level from ``-100`` to ``100``, eg: 50 - * @param milliseconds the number of milliseconds to turn the motor on, eg: 500 - * @param brake whether or not to use the brake - */ - //% blockId=outputMotorOnForTime block="%motor|ON at power %power|for %milliseconds=timePicker|ms then brake %brake" - //% power.min=-100 power.max=100 - //% brake.fieldEditor=toggleonoff - //% weight=98 group="Motors" blockGap=8 - onForTime(power: number, milliseconds: number, brake = false) { - step(this.port, { - power, - step1: 0, - step2: milliseconds, - step3: 0, - useSteps: false, - useBrake: brake - }) - loops.pause(milliseconds); - } - - /** - * Powers on the motor for a specified number of milliseconds. - * @param motor the motor to turn on - * @param power the motor power level from ``-100`` to ``100``, eg: 50 - * @param degrees the number of degrees to turn, eg: 90 - * @param brake whether or not to use the brake - */ - //% blockId=outputMotorOnForAngle block="%motor|ON at power %power|for %degrees|deg then brake %brake" - //% power.min=-100 power.max=100 - //% degrees.min=-360 degrees.max=360 - //% brake.fieldEditor=toggleonoff - //% weight=97 group="Motors" blockGap=8 - onForAngle(power: number, degrees: number, brake = false) { - // TODO + //% onOrOff.fieldEditor=toggleonoff + on(onOrOff: boolean = true) { + if (onOrOff) { + const b = mkCmd(this.port, DAL.opOutputStart, 0) + writePWM(b); + } else { + const b = mkCmd(this.port, DAL.opOutputStop, 1) + b.setNumber(NumberFormat.UInt8LE, 2, this.brake ? 1 : 0) + writePWM(b) + } } /** @@ -145,7 +101,7 @@ namespace output { * @param power the desired speed to use. eg: 50 */ //% blockId=motorSetPower block="%motor|set power to %speed" - //% weight=60 group="Motors" + //% weight=60 group="Motors" blockGap=8 //% speed.min=-100 speed.max=100 setPower(power: number) { const b = mkCmd(this.port, DAL.opOutputPower, 1) @@ -153,6 +109,17 @@ namespace output { writePWM(b) } + /** + * 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="%motor|set brake %brake" + //% brake.fieldEditor=toggleonoff + //% weight=60 group="Motors" + setBrake(brake: boolean) { + this.brake = brake; + } + /** * Gets motor actual speed. * @param motor the port which connects to the motor