simplify motor API

This commit is contained in:
Peli de Halleux 2017-10-27 01:47:25 -07:00
parent 12cdad72c8
commit 712c2178d2
4 changed files with 42 additions and 79 deletions

View File

@ -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)
})
```

View File

@ -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.",

View File

@ -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",

View File

@ -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