Drivers for the mini robot designed for Princeton's MAE 433 course.

Dependencies:   mbed-dsp mbed-rtos mbed

Dependents:   MAE433_Library_Tester RobotBalancerv2

HBridge.cpp

Committer:
Electrotiger
Date:
2016-06-24
Revision:
0:9afc272fa65f
Child:
4:2d38ad348e0d

File content as of revision 0:9afc272fa65f:

/*
 * HBridge.cpp
 *
 *  Created on: Jun 4, 2016
 *      Author: Developer
 */

#include <HBridge.hpp>
#include <cmath>

HBridge::HBridge(PinName PWMPin, PinName DirectionPin)
: PWMOut(PWMPin), DirectionOut(DirectionPin) {
    // 10 ms period == 100 Hz works well.
    PWMOut.period_ms(10);
}

HBridge::~HBridge() {
    // TODO Auto-generated destructor stub
}

void HBridge::write(float percent) {
    // Write the percent value as a PWM out.
    PWMOut.write(std::abs(percent));
    // Write the direction as the sign of percent.
    DirectionOut.write(percent >= 0);
    // Set the output has changed flag to 1.
    outputHasChanged = true;
}

float HBridge::read() {
    static float prevValue;
    if (outputHasChanged) {
        outputHasChanged = false;
        prevValue = (DirectionOut.read()) ? PWMOut.read() : -PWMOut.read();
    }
    return prevValue;
}