Mbed library to control a motor via DRV8833 H-bridge motor controller. Uses two Pwmouts.
DRV8833.cpp@3:0aa2cdeb2e2b, 2013-12-10 (annotated)
- Committer:
- xeta05
- Date:
- Tue Dec 10 11:17:13 2013 +0000
- Revision:
- 3:0aa2cdeb2e2b
- Parent:
- 2:ccc9acaebd38
docs updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xeta05 | 3:0aa2cdeb2e2b | 1 | /* |
xeta05 | 3:0aa2cdeb2e2b | 2 | * @file DRV8833.h |
xeta05 | 3:0aa2cdeb2e2b | 3 | * @author Oskar Lopez de Gamboa |
xeta05 | 3:0aa2cdeb2e2b | 4 | * |
xeta05 | 3:0aa2cdeb2e2b | 5 | * @section LICENSE |
xeta05 | 3:0aa2cdeb2e2b | 6 | * |
xeta05 | 3:0aa2cdeb2e2b | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
xeta05 | 3:0aa2cdeb2e2b | 8 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
xeta05 | 3:0aa2cdeb2e2b | 9 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
xeta05 | 3:0aa2cdeb2e2b | 10 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
xeta05 | 3:0aa2cdeb2e2b | 11 | * furnished to do so, subject to the following conditions: |
xeta05 | 3:0aa2cdeb2e2b | 12 | * |
xeta05 | 3:0aa2cdeb2e2b | 13 | * The above copyright notice and this permission notice shall be included in all copies or |
xeta05 | 3:0aa2cdeb2e2b | 14 | * substantial portions of the Software. |
xeta05 | 3:0aa2cdeb2e2b | 15 | * |
xeta05 | 3:0aa2cdeb2e2b | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
xeta05 | 3:0aa2cdeb2e2b | 17 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
xeta05 | 3:0aa2cdeb2e2b | 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
xeta05 | 3:0aa2cdeb2e2b | 19 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
xeta05 | 3:0aa2cdeb2e2b | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
xeta05 | 3:0aa2cdeb2e2b | 21 | * |
xeta05 | 3:0aa2cdeb2e2b | 22 | * @section DESCRIPTION |
xeta05 | 3:0aa2cdeb2e2b | 23 | * |
xeta05 | 3:0aa2cdeb2e2b | 24 | * mbed simple DRV8833 H-bridge motor controller |
xeta05 | 0:80e26be59f41 | 25 | * |
xeta05 | 0:80e26be59f41 | 26 | * |
xeta05 | 0:80e26be59f41 | 27 | * PWM a un puente en H(DRV8833) conectado a los motores. |
xeta05 | 3:0aa2cdeb2e2b | 28 | * El comportamiento del driver es el siguiente: |
xeta05 | 0:80e26be59f41 | 29 | * |
xeta05 | 0:80e26be59f41 | 30 | * x_PWM1 x_PWM2 Mode |
xeta05 | 0:80e26be59f41 | 31 | * 0 0 Coast/Fast decay |
xeta05 | 0:80e26be59f41 | 32 | * 0 1 Reverse |
xeta05 | 0:80e26be59f41 | 33 | * 1 0 Forward |
xeta05 | 0:80e26be59f41 | 34 | * 1 1 Brake/slow decay |
xeta05 | 3:0aa2cdeb2e2b | 35 | *10/12/2013 |
xeta05 | 3:0aa2cdeb2e2b | 36 | */ |
xeta05 | 0:80e26be59f41 | 37 | |
xeta05 | 0:80e26be59f41 | 38 | |
xeta05 | 0:80e26be59f41 | 39 | #include "DRV8833.h" |
xeta05 | 0:80e26be59f41 | 40 | |
xeta05 | 0:80e26be59f41 | 41 | DRV8833::DRV8833(PinName pwm1, PinName pwm2): |
xeta05 | 0:80e26be59f41 | 42 | _pwm1(pwm1), _pwm2(pwm2) |
xeta05 | 0:80e26be59f41 | 43 | { |
xeta05 | 0:80e26be59f41 | 44 | |
xeta05 | 0:80e26be59f41 | 45 | // Set initial condition of PWM |
xeta05 | 0:80e26be59f41 | 46 | _pwm1.period(0.001); |
xeta05 | 0:80e26be59f41 | 47 | _pwm1 = 0; |
xeta05 | 0:80e26be59f41 | 48 | _pwm2.period(0.001); |
xeta05 | 0:80e26be59f41 | 49 | _pwm2 = 0; |
xeta05 | 0:80e26be59f41 | 50 | |
xeta05 | 0:80e26be59f41 | 51 | } |
xeta05 | 0:80e26be59f41 | 52 | |
xeta05 | 0:80e26be59f41 | 53 | void DRV8833::speed(float speed) |
xeta05 | 0:80e26be59f41 | 54 | { |
xeta05 | 0:80e26be59f41 | 55 | if (speed > 0.0) |
xeta05 | 0:80e26be59f41 | 56 | { |
xeta05 | 0:80e26be59f41 | 57 | _pwm1 = abs(speed); |
xeta05 | 0:80e26be59f41 | 58 | _pwm2 = 0; |
xeta05 | 0:80e26be59f41 | 59 | } |
xeta05 | 0:80e26be59f41 | 60 | else |
xeta05 | 0:80e26be59f41 | 61 | { |
xeta05 | 0:80e26be59f41 | 62 | _pwm1 = 0; |
xeta05 | 0:80e26be59f41 | 63 | _pwm2 = abs(speed); |
xeta05 | 0:80e26be59f41 | 64 | } |
xeta05 | 0:80e26be59f41 | 65 | } |
xeta05 | 0:80e26be59f41 | 66 | void DRV8833::period(float period){ |
xeta05 | 0:80e26be59f41 | 67 | |
xeta05 | 0:80e26be59f41 | 68 | _pwm1.period(period); |
xeta05 | 0:80e26be59f41 | 69 | _pwm2.period(period); |
xeta05 | 0:80e26be59f41 | 70 | } |
xeta05 | 0:80e26be59f41 | 71 | |
xeta05 | 2:ccc9acaebd38 | 72 | void DRV8833::brake(int mode){ |
xeta05 | 0:80e26be59f41 | 73 | |
xeta05 | 2:ccc9acaebd38 | 74 | if(mode == COAST) |
xeta05 | 0:80e26be59f41 | 75 | { |
xeta05 | 0:80e26be59f41 | 76 | _pwm1 = 0; |
xeta05 | 0:80e26be59f41 | 77 | _pwm2 = 0; |
xeta05 | 0:80e26be59f41 | 78 | } |
xeta05 | 2:ccc9acaebd38 | 79 | else if(mode == BRAKE) |
xeta05 | 2:ccc9acaebd38 | 80 | { |
xeta05 | 0:80e26be59f41 | 81 | _pwm1 = 1; |
xeta05 | 0:80e26be59f41 | 82 | _pwm2 = 1; |
xeta05 | 0:80e26be59f41 | 83 | } |
xeta05 | 0:80e26be59f41 | 84 | |
xeta05 | 0:80e26be59f41 | 85 | } |