Library for driving a motor with a DRV8833 motor driver or similar.
Revision 0:0d650a332dcc, committed 2014-10-19
- Comitter:
- pclary
- Date:
- Sun Oct 19 07:51:25 2014 +0000
- Commit message:
- Code imported from Micromouse 2014
Changed in this revision
Motor.cpp | Show annotated file Show diff for this revision Revisions of this file |
Motor.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 0d650a332dcc Motor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.cpp Sun Oct 19 07:51:25 2014 +0000 @@ -0,0 +1,42 @@ +#include "Motor.h" + + +Motor::Motor(PinName pin1, PinName pin2) : pwm1(pin1), pwm2(pin2) +{ + const float period = 1/20000.0f; + + pwm1.period(period); + pwm2.period(period); + + pwm1 = 0.0f; + pwm2 = 0.0f; +} + + +void Motor::speed(float value) +{ + if (value < 0.0f) + { + pwm1 = value; + pwm2 = 0.0f; + } + else + { + pwm1 = 0.0f; + pwm2 = -value; + } +} + + +Motor& Motor::operator=(float value) +{ + speed(value); + return *this; +} + + +void Motor::brake() +{ + pwm1 = 1.0f; + pwm2 = 1.0f; +}
diff -r 000000000000 -r 0d650a332dcc Motor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.h Sun Oct 19 07:51:25 2014 +0000 @@ -0,0 +1,20 @@ +#ifndef MOTOR_H +#define MOTOR_H + +#include "mbed.h" + + +class Motor +{ +public: + Motor(PinName pin1, PinName pin2); + void speed(float value); + Motor& operator=(float value); + void brake(); + +private: + PwmOut pwm1; + PwmOut pwm2; +}; + +#endif // MOTOR_H \ No newline at end of file