From 83bd1ed3e161753913f622c27859a9f61a6525fe Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 2 Nov 2016 22:35:00 -0700 Subject: [PATCH] support for motor commands --- libs/core/enums.d.ts | 18 +++++++++++++ libs/core/motors.cpp | 59 ++++++++++++++++++++++++++++++++++++++--- libs/core/shims.d.ts | 22 ++++++++++++--- sim/state/soundmotor.ts | 7 ++++- 4 files changed, 97 insertions(+), 9 deletions(-) diff --git a/libs/core/enums.d.ts b/libs/core/enums.d.ts index eb921b58..c546d312 100644 --- a/libs/core/enums.d.ts +++ b/libs/core/enums.d.ts @@ -240,6 +240,24 @@ declare namespace control { } declare namespace led { } + + + declare enum MotorCommand { + //% block=coast + Coast = 0, + //% block=break + Break = 1, + //% block=sleep + Sleep = 2, + } + + + declare enum Motor { + A = 0, + B = 1, + //% block="A and B" + AB = 2, + } declare namespace motors { } diff --git a/libs/core/motors.cpp b/libs/core/motors.cpp index 94ae562c..d04a6837 100644 --- a/libs/core/motors.cpp +++ b/libs/core/motors.cpp @@ -1,17 +1,68 @@ #include "ksbit.h" +enum MotorCommand { + //% block=coast + Coast, + //% block=break + Break, + //% block=sleep + Sleep +}; + +enum Motor { + A, + B, + //% block="A and B" + AB +}; + /** * Blocks to control the onboard motors */ //% color=#008272 weight=30 namespace motors { /** - * Controls the power sent to a single motor + * Turns on the motor at a certain percent of power. * @param power %percent of power sent to the motor. Negative power goes backward. eg: 50 */ - //% blockId=motor_on block="motor on at %percent|%" - //% parts=dcmotor - void motorOn(int power) { + //% blockId=motor_on block="motor on at %percent" + //% parts=dcmotor weight=90 blockGap=8 + void motorPower(int power) { uBit.soundmotor.Motor_On(power); } + + /** + * Send break, coast or sleep commands to the motor + */ + //% blockId=motor_command block="motor %command" + //% parts=dcmotor weight=85 + void motorCommand(MotorCommand command) { + switch(command) { + case MotorCommand::Coast: uBit.soundmotor.Motor_Coast();break; + case MotorCommand::Break: uBit.soundmotor.Motor_Break();break; + case MotorCommand::Sleep: uBit.soundmotor.Motor_Sleep();break; + } + } + + /** + * Controls two motors attached to the board. + */ + //% blockId=block_dual_motor block="motor %motor|at %percent" + //% weight=80 + void dualMotorPower(Motor motor, int duty_percent) { + switch(motor) { + case Motor::A: if (duty_percent <= 0) uBit.soundmotor.MotorA_Off(); + else uBit.soundmotor.MotorA_On(duty_percent); break; + case Motor::B: if (duty_percent <= 0) uBit.soundmotor.MotorB_Off(); + else uBit.soundmotor.MotorB_On(duty_percent); break; + case Motor::AB: if (duty_percent <= 0) { + uBit.soundmotor.MotorA_Off(); + uBit.soundmotor.MotorB_Off(); + } else { + uBit.soundmotor.MotorA_On(duty_percent); + uBit.soundmotor.MotorB_On(duty_percent); + } + break; + } + } } \ No newline at end of file diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index 310d3cb2..25f3dd11 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -517,12 +517,26 @@ declare namespace led { declare namespace motors { /** - * Controls the power sent to a single motor + * Turns on the motor at a certain percent of power. * @param power %percent of power sent to the motor. Negative power goes backward. eg: 50 */ - //% blockId=motor_on block="motor on at %percent|%" - //% parts=dcmotor shim=motors::motorOn - function motorOn(power: number): void; + //% blockId=motor_on block="motor on at %percent" + //% parts=dcmotor weight=90 blockGap=8 shim=motors::motorPower + function motorPower(power: number): void; + + /** + * Send break, coast or sleep commands to the motor + */ + //% blockId=motor_command block="motor %command" + //% parts=dcmotor weight=85 shim=motors::motorCommand + function motorCommand(command: MotorCommand): void; + + /** + * Controls two motors attached to the board. + */ + //% blockId=block_dual_motor block="motor %motor|at %percent" + //% weight=80 shim=motors::dualMotorPower + function dualMotorPower(motor: Motor, duty_percent: number): void; } declare namespace music { diff --git a/sim/state/soundmotor.ts b/sim/state/soundmotor.ts index 71f4386b..b6f23099 100644 --- a/sim/state/soundmotor.ts +++ b/sim/state/soundmotor.ts @@ -1,5 +1,10 @@ namespace pxsim.motors { - export function motorOn(power: number) { + export function motorPower(power: number) { // TODO } + export function motorCommand(command: MotorCommand) { + + } + export function dualMotorPower(motor: Motor, percent: number) { + } }