zhouhang shao
/
test
test
Diff: motor.cpp
- Revision:
- 2:b78dfa2afe92
- Child:
- 6:9f698d1b2996
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/motor.cpp Sat May 06 23:10:06 2017 +0000 @@ -0,0 +1,31 @@ +#include "motor.h" +const int FORWARD = 1; +const int BACKWARD = 0; +const int ONE_MILLISECOND = 0.001; + +Motor::Motor(PinName _pwm_pin, PinName _dir): + pwm_pin(_pwm_pin), dir(_dir){ + pwm_pin.period(ONE_MILLISECOND); + pwm_pin = 0; + dir = 0; + curr_speed = 0; +} + +//Sets motor speed +void Motor::speed(float speed) { + if (speed < 0.0f){ //Backwards + if (speed < -1.0f){ + speed = -1.0f; + } + dir = FORWARD; + pwm_pin = curr_speed = speed + 1.0f; // Inverts it so 1 is off and 0 is on + } else { //Forwards + dir = BACKWARD; + pwm_pin = curr_speed = speed; + } +} + +//Sets motor speed to 0 +void Motor::stop() { + speed(0); +} \ No newline at end of file