Robot project.

Dependencies:   HCSR04 PID QEI mbed

Committer:
srsmitherman
Date:
Thu Dec 12 17:30:49 2013 +0000
Revision:
1:82463f25fd3c
Initial publish.

Who changed what in which revision?

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