Library for driving a motor with a DRV8833 motor driver or similar.
Motor.cpp@0:0d650a332dcc, 2014-10-19 (annotated)
- Committer:
- pclary
- Date:
- Sun Oct 19 07:51:25 2014 +0000
- Revision:
- 0:0d650a332dcc
Code imported from Micromouse 2014
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pclary | 0:0d650a332dcc | 1 | #include "Motor.h" |
pclary | 0:0d650a332dcc | 2 | |
pclary | 0:0d650a332dcc | 3 | |
pclary | 0:0d650a332dcc | 4 | Motor::Motor(PinName pin1, PinName pin2) : pwm1(pin1), pwm2(pin2) |
pclary | 0:0d650a332dcc | 5 | { |
pclary | 0:0d650a332dcc | 6 | const float period = 1/20000.0f; |
pclary | 0:0d650a332dcc | 7 | |
pclary | 0:0d650a332dcc | 8 | pwm1.period(period); |
pclary | 0:0d650a332dcc | 9 | pwm2.period(period); |
pclary | 0:0d650a332dcc | 10 | |
pclary | 0:0d650a332dcc | 11 | pwm1 = 0.0f; |
pclary | 0:0d650a332dcc | 12 | pwm2 = 0.0f; |
pclary | 0:0d650a332dcc | 13 | } |
pclary | 0:0d650a332dcc | 14 | |
pclary | 0:0d650a332dcc | 15 | |
pclary | 0:0d650a332dcc | 16 | void Motor::speed(float value) |
pclary | 0:0d650a332dcc | 17 | { |
pclary | 0:0d650a332dcc | 18 | if (value < 0.0f) |
pclary | 0:0d650a332dcc | 19 | { |
pclary | 0:0d650a332dcc | 20 | pwm1 = value; |
pclary | 0:0d650a332dcc | 21 | pwm2 = 0.0f; |
pclary | 0:0d650a332dcc | 22 | } |
pclary | 0:0d650a332dcc | 23 | else |
pclary | 0:0d650a332dcc | 24 | { |
pclary | 0:0d650a332dcc | 25 | pwm1 = 0.0f; |
pclary | 0:0d650a332dcc | 26 | pwm2 = -value; |
pclary | 0:0d650a332dcc | 27 | } |
pclary | 0:0d650a332dcc | 28 | } |
pclary | 0:0d650a332dcc | 29 | |
pclary | 0:0d650a332dcc | 30 | |
pclary | 0:0d650a332dcc | 31 | Motor& Motor::operator=(float value) |
pclary | 0:0d650a332dcc | 32 | { |
pclary | 0:0d650a332dcc | 33 | speed(value); |
pclary | 0:0d650a332dcc | 34 | return *this; |
pclary | 0:0d650a332dcc | 35 | } |
pclary | 0:0d650a332dcc | 36 | |
pclary | 0:0d650a332dcc | 37 | |
pclary | 0:0d650a332dcc | 38 | void Motor::brake() |
pclary | 0:0d650a332dcc | 39 | { |
pclary | 0:0d650a332dcc | 40 | pwm1 = 1.0f; |
pclary | 0:0d650a332dcc | 41 | pwm2 = 1.0f; |
pclary | 0:0d650a332dcc | 42 | } |