pxt-calliope/libs/core/motors.cpp

68 lines
2.0 KiB
C++
Raw Permalink Normal View History

2016-11-30 06:55:37 +01:00
#include "pxt.h"
2016-10-12 07:58:53 +02:00
2016-11-03 06:35:00 +01:00
enum MotorCommand {
//% block=coast
Coast,
//% block=break
Break,
//% block=sleep
Sleep
};
enum Motor {
A,
B,
//% block="A and B"
AB
};
2016-10-12 07:58:53 +02:00
/**
* Blocks to control the onboard motors
*/
2017-01-20 05:55:31 +01:00
//% color=#008272 weight=30 icon="\uf1b9"
2016-10-12 07:58:53 +02:00
namespace motors {
/**
2016-11-10 13:48:20 +01:00
* Turns on the motor at a certain percent of power. Switches to single motor mode!
2016-10-12 07:58:53 +02:00
* @param power %percent of power sent to the motor. Negative power goes backward. eg: 50
*/
2016-11-03 06:35:00 +01:00
//% blockId=motor_on block="motor on at %percent"
//% parts=dcmotor weight=90 blockGap=8
void motorPower(int power) {
2016-11-10 13:48:20 +01:00
uBit.soundmotor.motorOn(power);
2016-10-12 07:58:53 +02:00
}
2016-11-03 06:35:00 +01:00
/**
2016-11-10 13:48:20 +01:00
* Send break, coast or sleep commands to the motor. Has no effect in dual-motor mode.
2016-11-03 06:35:00 +01:00
*/
//% blockId=motor_command block="motor %command"
//% parts=dcmotor weight=85
void motorCommand(MotorCommand command) {
switch(command) {
2016-11-10 13:48:20 +01:00
case MotorCommand::Coast: uBit.soundmotor.motorCoast();break;
case MotorCommand::Break: uBit.soundmotor.motorBreak();break;
case MotorCommand::Sleep: uBit.soundmotor.motorSleep();break;
2016-11-03 06:35:00 +01:00
}
}
/**
2016-11-10 13:48:20 +01:00
* Controls two motors attached to the board. Switches to dual-motor mode!
2016-11-03 06:35:00 +01:00
*/
//% blockId=block_dual_motor block="motor %motor|at %percent"
//% weight=80
void dualMotorPower(Motor motor, int duty_percent) {
switch(motor) {
2016-11-10 13:48:20 +01:00
case Motor::A: if (duty_percent <= 0) uBit.soundmotor.motorAOff();
else uBit.soundmotor.motorAOn(duty_percent); break;
case Motor::B: if (duty_percent <= 0) uBit.soundmotor.motorBOff();
else uBit.soundmotor.motorBOn(duty_percent); break;
2016-11-03 06:35:00 +01:00
case Motor::AB: if (duty_percent <= 0) {
2016-11-10 13:48:20 +01:00
uBit.soundmotor.motorAOff();
uBit.soundmotor.motorBOff();
2016-11-03 06:35:00 +01:00
} else {
2016-11-10 13:48:20 +01:00
uBit.soundmotor.motorAOn(duty_percent);
uBit.soundmotor.motorBOn(duty_percent);
}
2016-11-03 06:35:00 +01:00
break;
}
}
2016-10-12 07:58:53 +02:00
}