simplify motor API
This commit is contained in:
parent
12cdad72c8
commit
712c2178d2
@ -4,8 +4,14 @@ Use this program with the Programmable Brick and Large Motor.
|
|||||||
|
|
||||||
```blocks
|
```blocks
|
||||||
loops.forever(function () {
|
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))
|
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)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -56,17 +56,9 @@
|
|||||||
"input.remoteButtonCenter": "Remote beacon (center) button.",
|
"input.remoteButtonCenter": "Remote beacon (center) button.",
|
||||||
"input.remoteButtonTopLeft": "Remote top-left button.",
|
"input.remoteButtonTopLeft": "Remote top-left button.",
|
||||||
"input.remoteButtonTopRight": "Remote top-right button.",
|
"input.remoteButtonTopRight": "Remote top-right button.",
|
||||||
"output.Motor.off": "Power off the motor.",
|
"output.Motor.on": "Power on or off the motor.",
|
||||||
"output.Motor.on": "Power on the motor.",
|
"output.Motor.setBrake": "Sets the automatic brake on or off when the motor is off",
|
||||||
"output.Motor.onForAngle": "Powers on the motor for a specified number of milliseconds.",
|
"output.Motor.setBrake|param|brake": "a value indicating if the motor should break when off",
|
||||||
"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.setPower": "Sets the motor power level from ``-100`` to ``100``.",
|
"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.setPower|param|power": "the desired speed to use. eg: 50",
|
||||||
"output.Motor.speed": "Gets motor actual speed.",
|
"output.Motor.speed": "Gets motor actual speed.",
|
||||||
|
@ -82,10 +82,8 @@
|
|||||||
"input.ultrasonic3|block": "ultrasonic sensor 3",
|
"input.ultrasonic3|block": "ultrasonic sensor 3",
|
||||||
"input.ultrasonic4|block": "ultrasonic sensor 4",
|
"input.ultrasonic4|block": "ultrasonic sensor 4",
|
||||||
"input|block": "input",
|
"input|block": "input",
|
||||||
"output.Motor.off|block": "%motor|OFF then brake %brake",
|
"output.Motor.on|block": "%motor|%onOrOff",
|
||||||
"output.Motor.onForAngle|block": "%motor|ON at power %power|for %degrees|deg then brake %brake",
|
"output.Motor.setBrake|block": "%motor|set 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.setPower|block": "%motor|set power to %speed",
|
"output.Motor.setPower|block": "%motor|set power to %speed",
|
||||||
"output.Motor.speed|block": "%motor|speed",
|
"output.Motor.speed|block": "%motor|speed",
|
||||||
"output.largeMotorA|block": "large motor A",
|
"output.largeMotorA|block": "large motor A",
|
||||||
|
@ -65,78 +65,34 @@ namespace output {
|
|||||||
|
|
||||||
//% fixedInstances
|
//% fixedInstances
|
||||||
export class Motor extends control.Component {
|
export class Motor extends control.Component {
|
||||||
port: Output;
|
private port: Output;
|
||||||
large: boolean;
|
private large: boolean;
|
||||||
|
private brake: boolean;
|
||||||
|
|
||||||
constructor(port: Output, large: boolean) {
|
constructor(port: Output, large: boolean) {
|
||||||
super();
|
super();
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.large = large;
|
this.large = large;
|
||||||
|
this.brake = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Power off the motor.
|
* Power on or 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.
|
|
||||||
* @param motor the motor to turn on
|
* @param motor the motor to turn on
|
||||||
* @param power the motor power level from ``-100`` to ``100``, eg: 50
|
* @param power the motor power level from ``-100`` to ``100``, eg: 50
|
||||||
*/
|
*/
|
||||||
//% blockId=outputMotorOn block="%motor|ON at power %power"
|
//% blockId=outputMotorOn block="%motor|%onOrOff"
|
||||||
//% power.min=-100 power.max=100
|
|
||||||
//% weight=99 group="Motors" blockGap=8
|
//% weight=99 group="Motors" blockGap=8
|
||||||
on(power: number = 50) {
|
//% onOrOff.fieldEditor=toggleonoff
|
||||||
this.setPower(power);
|
on(onOrOff: boolean = true) {
|
||||||
const b = mkCmd(this.port, DAL.opOutputStart, 0)
|
if (onOrOff) {
|
||||||
writePWM(b);
|
const b = mkCmd(this.port, DAL.opOutputStart, 0)
|
||||||
}
|
writePWM(b);
|
||||||
|
} else {
|
||||||
/**
|
const b = mkCmd(this.port, DAL.opOutputStop, 1)
|
||||||
* Powers on the motor for a specified number of milliseconds.
|
b.setNumber(NumberFormat.UInt8LE, 2, this.brake ? 1 : 0)
|
||||||
* @param motor the motor to turn on
|
writePWM(b)
|
||||||
* @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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -145,7 +101,7 @@ namespace output {
|
|||||||
* @param power the desired speed to use. eg: 50
|
* @param power the desired speed to use. eg: 50
|
||||||
*/
|
*/
|
||||||
//% blockId=motorSetPower block="%motor|set power to %speed"
|
//% blockId=motorSetPower block="%motor|set power to %speed"
|
||||||
//% weight=60 group="Motors"
|
//% weight=60 group="Motors" blockGap=8
|
||||||
//% speed.min=-100 speed.max=100
|
//% speed.min=-100 speed.max=100
|
||||||
setPower(power: number) {
|
setPower(power: number) {
|
||||||
const b = mkCmd(this.port, DAL.opOutputPower, 1)
|
const b = mkCmd(this.port, DAL.opOutputPower, 1)
|
||||||
@ -153,6 +109,17 @@ namespace output {
|
|||||||
writePWM(b)
|
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.
|
* Gets motor actual speed.
|
||||||
* @param motor the port which connects to the motor
|
* @param motor the port which connects to the motor
|
||||||
|
Loading…
Reference in New Issue
Block a user