A library for H-Bridge dual motor control

DualMotorController.cpp

Committer:
Cornelius Bezuidenhout
Date:
2017-10-22
Revision:
1:b85eb4e982a9
Parent:
0:9ac0748f78d4
Child:
2:c0e11aaa32e7

File content as of revision 1:b85eb4e982a9:

#include "DualMotorController.hpp"

DualMotorController::DualMotorController(PinName pwmL, PinName dirL0, PinName dirL1, PinName pwmR, PinName dirR0, PinName dirR1, float frequency) :
    _pwmL(pwmA),
    _dirL0(dirA0,0),
    _dirL1(dirA1,0),
    _pwmR(pwmR),
    _dirR0(dirR0,0),
    _dirR1(dirR1,0)
{
    _pwmL.period(1.0f/frequency);
    _pwmR.period(1.0f/frequency);

    _speedL = 0.0f;
    _speedR = 0.0f;
    _inverted = false;
}

void DualMotorController::SetLeft(float speed, boolean forward) {
    if( forward ) {
        _dirL0.write(1);
        _dirL1.write(0);
    } else {
        _dirL0.write(0);
        _dirL1.write(1);
    }

    _pwmL.write(speed);
}

void DualMotorController::SetRight(float speed, boolean forward) {
    if( forward ) {
        _dirR0.write(1);
        _dirR1.write(0);
    } else {
        _dirR0.write(0);
        _dirR1.write(1);
    }

    _pwmR.write(speed);
}