Mbed library to control a motor via DRV8833 H-bridge motor controller. Uses two Pwmouts.
DRV8833.cpp@0:80e26be59f41, 2013-10-15 (annotated)
- Committer:
- xeta05
- Date:
- Tue Oct 15 12:25:34 2013 +0000
- Revision:
- 0:80e26be59f41
- Child:
- 2:ccc9acaebd38
First revision!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xeta05 | 0:80e26be59f41 | 1 | /* mbed simple DRV8833 H-bridge motor controller |
xeta05 | 0:80e26be59f41 | 2 | * |
xeta05 | 0:80e26be59f41 | 3 | * |
xeta05 | 0:80e26be59f41 | 4 | * PWM a un puente en H(DRV8833) conectado a los motores. |
xeta05 | 0:80e26be59f41 | 5 | * El comportamiento del driver es el siguiente |
xeta05 | 0:80e26be59f41 | 6 | * |
xeta05 | 0:80e26be59f41 | 7 | * x_PWM1 x_PWM2 Mode |
xeta05 | 0:80e26be59f41 | 8 | * 0 0 Coast/Fast decay |
xeta05 | 0:80e26be59f41 | 9 | * 0 1 Reverse |
xeta05 | 0:80e26be59f41 | 10 | * 1 0 Forward |
xeta05 | 0:80e26be59f41 | 11 | * 1 1 Brake/slow decay |
xeta05 | 0:80e26be59f41 | 12 | *15/10/2013 |
xeta05 | 0:80e26be59f41 | 13 | **/ |
xeta05 | 0:80e26be59f41 | 14 | |
xeta05 | 0:80e26be59f41 | 15 | |
xeta05 | 0:80e26be59f41 | 16 | #include "DRV8833.h" |
xeta05 | 0:80e26be59f41 | 17 | |
xeta05 | 0:80e26be59f41 | 18 | DRV8833::DRV8833(PinName pwm1, PinName pwm2): |
xeta05 | 0:80e26be59f41 | 19 | _pwm1(pwm1), _pwm2(pwm2) |
xeta05 | 0:80e26be59f41 | 20 | { |
xeta05 | 0:80e26be59f41 | 21 | |
xeta05 | 0:80e26be59f41 | 22 | // Set initial condition of PWM |
xeta05 | 0:80e26be59f41 | 23 | _pwm1.period(0.001); |
xeta05 | 0:80e26be59f41 | 24 | _pwm1 = 0; |
xeta05 | 0:80e26be59f41 | 25 | _pwm2.period(0.001); |
xeta05 | 0:80e26be59f41 | 26 | _pwm2 = 0; |
xeta05 | 0:80e26be59f41 | 27 | |
xeta05 | 0:80e26be59f41 | 28 | } |
xeta05 | 0:80e26be59f41 | 29 | |
xeta05 | 0:80e26be59f41 | 30 | void DRV8833::speed(float speed) |
xeta05 | 0:80e26be59f41 | 31 | { |
xeta05 | 0:80e26be59f41 | 32 | if (speed > 0.0) |
xeta05 | 0:80e26be59f41 | 33 | { |
xeta05 | 0:80e26be59f41 | 34 | _pwm1 = abs(speed); |
xeta05 | 0:80e26be59f41 | 35 | _pwm2 = 0; |
xeta05 | 0:80e26be59f41 | 36 | } |
xeta05 | 0:80e26be59f41 | 37 | else |
xeta05 | 0:80e26be59f41 | 38 | { |
xeta05 | 0:80e26be59f41 | 39 | _pwm1 = 0; |
xeta05 | 0:80e26be59f41 | 40 | _pwm2 = abs(speed); |
xeta05 | 0:80e26be59f41 | 41 | } |
xeta05 | 0:80e26be59f41 | 42 | } |
xeta05 | 0:80e26be59f41 | 43 | void DRV8833::period(float period){ |
xeta05 | 0:80e26be59f41 | 44 | |
xeta05 | 0:80e26be59f41 | 45 | _pwm1.period(period); |
xeta05 | 0:80e26be59f41 | 46 | _pwm2.period(period); |
xeta05 | 0:80e26be59f41 | 47 | } |
xeta05 | 0:80e26be59f41 | 48 | |
xeta05 | 0:80e26be59f41 | 49 | void DRV8833::brake(int highLow){ |
xeta05 | 0:80e26be59f41 | 50 | |
xeta05 | 0:80e26be59f41 | 51 | if(highLow == BRAKE_FAST) |
xeta05 | 0:80e26be59f41 | 52 | { |
xeta05 | 0:80e26be59f41 | 53 | _pwm1 = 0; |
xeta05 | 0:80e26be59f41 | 54 | _pwm2 = 0; |
xeta05 | 0:80e26be59f41 | 55 | } |
xeta05 | 0:80e26be59f41 | 56 | else if(highLow == BRAKE_SLOW){ |
xeta05 | 0:80e26be59f41 | 57 | _pwm1 = 1; |
xeta05 | 0:80e26be59f41 | 58 | _pwm2 = 1; |
xeta05 | 0:80e26be59f41 | 59 | } |
xeta05 | 0:80e26be59f41 | 60 | |
xeta05 | 0:80e26be59f41 | 61 | } |