simplified motor API

This commit is contained in:
Peli de Halleux 2017-10-30 21:29:18 -07:00
parent 8f5c930f76
commit 16a025f3a0
4 changed files with 63 additions and 36 deletions

View File

@ -42,14 +42,17 @@
"control.raiseEvent|param|value": "Component specific code indicating the cause of the event.",
"motors.Motor.clearCount": "Clears the motor count",
"motors.Motor.count": "Gets motor step count.",
"motors.Motor.on": "Power on or off the motor.",
"motors.Motor.move": "Moves the motor by a number of degrees",
"motors.Motor.move|param|angle": "the degrees to rotate, eg: 360",
"motors.Motor.move|param|power": "the power from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.Motor.power": "Sets the motor power level from ``-100`` to ``100``.",
"motors.Motor.power|param|power": "the power from ``100`` full forward to ``-100`` full backward, eg: 50",
"motors.Motor.reset": "Resets the motor.",
"motors.Motor.setBrake": "Sets the automatic brake on or off when the motor is off",
"motors.Motor.setBrake|param|brake": "a value indicating if the motor should break when off",
"motors.Motor.setPower": "Sets the motor power level from ``-100`` to ``100``.",
"motors.Motor.setPower|param|power": "the desired speed to use. eg: 50",
"motors.Motor.setReversed": "Reverses the motor polarity",
"motors.Motor.speed": "Gets motor actual speed.",
"motors.Motor.stop": "Stops the motor",
"motors.Motor.tachoCount": "Gets motor tacho count.",
"motors.stopAllMotors": "Stops all motors",
"output.createBuffer": "Create a new zero-initialized buffer.",

View File

@ -46,11 +46,12 @@
"control.raiseEvent|block": "raise event|from %src|with value %value",
"control|block": "control",
"motors.Motor.count|block": "%motor|count",
"motors.Motor.on|block": "%motor|%onOrOff",
"motors.Motor.setBrake|block": "%motor|set brake %brake",
"motors.Motor.setPower|block": "%motor|set power to %speed",
"motors.Motor.setReversed|block": "%motor|set reversed %reversed",
"motors.Motor.move|block": "move %motor|by %angle|degrees at %power|%",
"motors.Motor.power|block": "power %motor|to %power|%",
"motors.Motor.setBrake|block": "set %motor|brake %brake",
"motors.Motor.setReversed|block": "set %motor|reversed %reversed",
"motors.Motor.speed|block": "%motor|speed",
"motors.Motor.stop|block": "stop %motor",
"motors.Motor.tachoCount|block": "%motor|tacho count",
"motors.largeMotorA|block": "large motor A",
"motors.largeMotorB|block": "large motor B",

View File

@ -87,43 +87,66 @@ namespace motors {
}
/**
* 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
* Sets the motor power level from ``-100`` to ``100``.
* @param motor the output connection that the motor is connected to
* @param power the power from ``100`` full forward to ``-100`` full backward, eg: 50
*/
//% blockId=outputMotorOn block="%motor|%onOrOff"
//% onOrOff.fieldEditor=toggleonoff
//% blockId=motorSetPower block="power %motor|to %power|%"
//% weight=99 group="Motors" blockGap=8
on(onOrOff: boolean = true) {
if (onOrOff) {
//% power.min=-100 power.max=100
power(power: number) {
power = Math.clamp(-100, 100, power >> 0);
const b = mkCmd(this.port, DAL.opOutputPower, 1)
b.setNumber(NumberFormat.Int8LE, 2, power)
writePWM(b)
if (power) {
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)
this.stop();
}
}
/**
* Sets the motor power level from ``-100`` to ``100``.
* @param motor the output connection that the motor is connected to
* @param power the desired speed to use. eg: 50
* Moves the motor by a number of degrees
* @param degrees the angle to turn the motor
* @param angle the degrees to rotate, eg: 360
* @param power the power from ``100`` full forward to ``-100`` full backward, eg: 50
*/
//% blockId=motorSetPower block="%motor|set power to %speed"
//% weight=62 group="Motors" blockGap=8
//% speed.min=-100 speed.max=100
setPower(power: number) {
const b = mkCmd(this.port, DAL.opOutputPower, 1)
b.setNumber(NumberFormat.Int8LE, 2, Math.clamp(-100, 100, power))
writePWM(b)
//% blockId=motorMove block="move %motor|by %angle|degrees at %power|%"
//% weight=98 group="Motors" blockGap=8
//% power.min=-100 power.max=100
move(angle: number, power: number) {
angle = angle >> 0;
power = Math.clamp(-100, 100, power >> 0);
step(this.port, {
speed: power,
step1: 0,
step2: angle,
step3: 0,
useSteps: true,
useBrake: this.brake
})
}
/**
* Stops the motor
*/
//% blockId=motorStop block="stop %motor"
//% weight=97 group="Motors"
stop() {
const b = mkCmd(this.port, DAL.opOutputStop, 1)
b.setNumber(NumberFormat.UInt8LE, 2, this.brake ? 1 : 0)
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"
//% blockId=outputMotorSetBrakeMode block="set %motor|brake %brake"
//% brake.fieldEditor=toggleonoff
//% weight=60 group="Motors" blockGap=8
setBrake(brake: boolean) {
@ -133,7 +156,7 @@ namespace motors {
/**
* Reverses the motor polarity
*/
//% blockId=motorSetReversed block="%motor|set reversed %reversed"
//% blockId=motorSetReversed block="set %motor|reversed %reversed"
//% reversed.fieldEditor=toggleonoff
//% weight=59 group="Motors"
setReversed(reversed: boolean) {
@ -147,7 +170,7 @@ namespace motors {
* @param motor the port which connects to the motor
*/
//% blockId=motorSpeed block="%motor|speed"
//% weight=50 group="Motors" blockGap=8
//% weight=72 group="Motors" blockGap=8
speed(): number {
return getMotorData(this.port).actualSpeed;
}
@ -157,7 +180,7 @@ namespace motors {
* @param motor the port which connects to the motor
*/
//% blockId=motorCount block="%motor|count"
//% weight=49 group="Motors" blockGap=8
//% weight=71 group="Motors" blockGap=8
count(): number {
return getMotorData(this.port).count;
}
@ -167,7 +190,7 @@ namespace motors {
* @param motor the port which connects to the motor
*/
//% blockId=motorTachoCount block="%motor|tacho count"
//% weight=48 group="Motors"
//% weight=70 group="Motors"
tachoCount(): number {
return getMotorData(this.port).tachoCount;
}

View File

@ -5,12 +5,12 @@ namespace brick {
}
//% color="#8AC044" weight=95 icon="\uf185"
namespace motors {
//% color="#D42878" weight=95
namespace sensors {
}
//% color="#D42878" weight=90
namespace sensors {
//% color="#8AC044" weight=90 icon="\uf185"
namespace motors {
}
//% color="#DF5014" weight=80