From 05a839502851b3f8e11f0410bcdfc6928dd0f555 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 27 Oct 2017 02:52:42 -0700 Subject: [PATCH] stop all motors --- libs/core/_locales/core-jsdoc-strings.json | 6 ++ libs/core/_locales/core-strings.json | 4 + libs/core/output.ts | 90 ++++++++++++++++------ 3 files changed, 78 insertions(+), 22 deletions(-) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 2c3d986c..c7c1731e 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -56,18 +56,24 @@ "input.remoteButtonCenter": "Remote beacon (center) button.", "input.remoteButtonTopLeft": "Remote top-left button.", "input.remoteButtonTopRight": "Remote top-right button.", + "output.Motor.clearCount": "Clears the motor count", + "output.Motor.count": "Gets motor step count.", "output.Motor.on": "Power on or off the motor.", + "output.Motor.reset": "Resets 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.setReversed": "Reverses the motor polarity", "output.Motor.speed": "Gets motor actual speed.", + "output.Motor.tachoCount": "Gets motor tacho count.", "output.createBuffer": "Create a new zero-initialized buffer.", "output.createBuffer|param|size": "number of bytes in the buffer", "output.pattern": "Pattern block.", "output.pattern|param|pattern": "the lights pattern to use. eg: LightsPattern.Green", "output.setStatusLight": "Set lights.", "output.setStatusLight|param|pattern": "the lights pattern to use.", + "output.stopAllMotors": "Stops all motors", "screen.clear": "Clear screen and reset font to normal.", "screen.doubleIcon": "Double size of an icon.", "screen.drawIcon": "Draw an icon on the screen.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index db12c922..b706cfb0 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -82,10 +82,13 @@ "input.ultrasonic3|block": "ultrasonic sensor 3", "input.ultrasonic4|block": "ultrasonic sensor 4", "input|block": "input", + "output.Motor.count|block": "%motor|count", "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.setReversed|block": "%motor|set reversed %reversed", "output.Motor.speed|block": "%motor|speed", + "output.Motor.tachoCount|block": "%motor|tacho count", "output.largeMotorA|block": "large motor A", "output.largeMotorB|block": "large motor B", "output.largeMotorC|block": "large motor C", @@ -96,6 +99,7 @@ "output.mediumMotorD|block": "medium motor D", "output.pattern|block": "%pattern", "output.setStatusLight|block": "set status light %pattern=led_pattern", + "output.stopAllMotors|block": "stop all motors", "output|block": "output", "screen.print|block": "print %text| at x: %x| y: %y", "screen.setPixel|block": "set pixel %on| at x: %x| y: %y", diff --git a/libs/core/output.ts b/libs/core/output.ts index a44cbcdf..44ff689d 100644 --- a/libs/core/output.ts +++ b/libs/core/output.ts @@ -63,6 +63,16 @@ namespace output { reset(Output.ALL) } + /** + * Stops all motors + */ + //% blockId=motorStopAll block="stop all motors" + //% weight=10 group="Motors" blockGap=8 + export function stopAllMotors() { + const b = mkCmd(Output.ALL, DAL.opOutputStop, 0) + writePWM(b) + } + //% fixedInstances export class Motor extends control.Component { private port: Output; @@ -82,16 +92,16 @@ namespace output { * @param power the motor power level from ``-100`` to ``100``, eg: 50 */ //% blockId=outputMotorOn block="%motor|%onOrOff" - //% weight=99 group="Motors" blockGap=8 //% onOrOff.fieldEditor=toggleonoff + //% weight=99 group="Motors" blockGap=8 on(onOrOff: boolean = true) { if (onOrOff) { const b = mkCmd(this.port, DAL.opOutputStart, 0) - writePWM(b); + writePWM(b); } else { const b = mkCmd(this.port, DAL.opOutputStop, 1) b.setNumber(NumberFormat.UInt8LE, 2, this.brake ? 1 : 0) - writePWM(b) + writePWM(b) } } @@ -101,7 +111,7 @@ namespace output { * @param power the desired speed to use. eg: 50 */ //% blockId=motorSetPower block="%motor|set power to %speed" - //% weight=60 group="Motors" blockGap=8 + //% weight=62 group="Motors" blockGap=8 //% speed.min=-100 speed.max=100 setPower(power: number) { const b = mkCmd(this.port, DAL.opOutputPower, 1) @@ -115,20 +125,72 @@ namespace output { */ //% blockId=outputMotorSetBrakeMode block="%motor|set brake %brake" //% brake.fieldEditor=toggleonoff - //% weight=60 group="Motors" + //% weight=60 group="Motors" blockGap=8 setBrake(brake: boolean) { this.brake = brake; } + /** + * Reverses the motor polarity + */ + //% blockId=motorSetReversed block="%motor|set reversed %reversed" + //% reversed.fieldEditor=toggleonoff + //% weight=59 group="Motors" + setReversed(reversed: boolean) { + const b = mkCmd(this.port, DAL.opOutputPolarity, 1) + b.setNumber(NumberFormat.Int8LE, 2, reversed ? -1 : 1); + writePWM(b) + } + /** * Gets motor actual speed. * @param motor the port which connects to the motor */ //% blockId=motorSpeed block="%motor|speed" //% weight=50 group="Motors" blockGap=8 - speed() { + speed(): number { return getMotorData(this.port).actualSpeed; } + + /** + * Gets motor step count. + * @param motor the port which connects to the motor + */ + //% blockId=motorCount block="%motor|count" + //% weight=49 group="Motors" blockGap=8 + count(): number { + return getMotorData(this.port).count; + } + + /** + * Gets motor tacho count. + * @param motor the port which connects to the motor + */ + //% blockId=motorTachoCount block="%motor|tacho count" + //% weight=48 group="Motors" + tachoCount(): number { + return getMotorData(this.port).tachoCount; + } + + /** + * Clears the motor count + */ + clearCount() { + const b = mkCmd(this.port, DAL.opOutputClearCount, 0) + writePWM(b) + for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) { + if (this.port & (1 << i)) { + motorMM.setNumber(NumberFormat.Int32LE, i * MotorDataOff.Size + MotorDataOff.TachoSensor, 0) + } + } + } + + /** + * Resets the motor. + */ + reset() { + reset(this.port); + } } //% whenUsed fixedInstance block="large motor A" @@ -160,16 +222,6 @@ namespace output { writePWM(b) } - function clearCount(out: Output) { - let b = mkCmd(out, DAL.opOutputClearCount, 0) - writePWM(b) - for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) { - if (out & (1 << i)) { - motorMM.setNumber(NumberFormat.Int32LE, i * MotorDataOff.Size + MotorDataOff.TachoSensor, 0) - } - } - } - function outOffset(out: Output) { for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) { if (out & (1 << i)) @@ -194,12 +246,6 @@ namespace output { } } - function setPolarity(out: Output, polarity: number) { - let b = mkCmd(out, DAL.opOutputPolarity, 1) - b.setNumber(NumberFormat.Int8LE, 2, Math.clamp(-1, 1, polarity)) - writePWM(b) - } - interface StepOptions { power?: number; speed?: number; // either speed or power has to be present