Basic Library for Pololu DRV8835

Dependents:   ME503_VehicleAssembly

DRV8835.cpp

Committer:
DrCoyle
Date:
2017-09-13
Revision:
2:6e781e956958
Parent:
1:edc8af84b968

File content as of revision 2:6e781e956958:

/* File: DRV8835.cpp
 * Adopted from work by Cameron Isbell
 *
 * Description: library for DRV8835 Motor Driver
 * Assumptions: A is left and B is right
 */

#include "mbed.h"
#include "DRV8835.h"


DRV8835::DRV8835( PinName pinPwmL, PinName pinLin,
                      PinName pinPwmR, PinName pinRin) :
pwmL(pinPwmL),
Lin(pinLin),
pwmR(pinPwmR),
Rin(pinRin)
{
    Lin = 0;
    Rin = 0;
    pwmL.period(DRV8835_PWM_PERIOD_DEFAULT);
    pwmL = DRV8835_PWM_PULSEWIDTH_DEFAULT;
    pwmR.period(DRV8835_PWM_PERIOD_DEFAULT);
    pwmR = DRV8835_PWM_PULSEWIDTH_DEFAULT;
    motorL_stop();
    motorR_stop();
}

void DRV8835::stop()
{
    motorL_stop();
    motorR_stop();
}

void DRV8835::motorL_stop(void)
{
    pwmL = 0;
}

void DRV8835::motorR_stop(void)
{
    pwmR = 0;
}

void DRV8835::setSpeeds(float Right,float Left)
{
    //Set Right Speed and Direction
    if(Right<0)
    {
        motorR_rev(Right*-1);
    } else {
        motorR_fwd(Right);
    }
    
    //Set Left Speed and Direction
    if(Left<0)
    {
        motorL_rev(Left*-1);
    } else {
        motorL_fwd(Left);
    }
}

void DRV8835::motorL_fwd(float fPulsewidth)
{
    Lin = 0;
    pwmL = fPulsewidth;
}
void DRV8835::motorL_rev(float fPulsewidth)
{
    Lin = 1;
    pwmL = fPulsewidth;
}

void DRV8835::motorR_fwd(float fPulsewidth)
{
    Rin = 1;
    pwmR = fPulsewidth;
}
void DRV8835::motorR_rev(float fPulsewidth)
{
    Rin = 0;
    pwmR = fPulsewidth;
}