Drivers for the mini robot designed for Princeton's MAE 433 course.
Dependencies: mbed-dsp mbed-rtos mbed
Dependents: MAE433_Library_Tester RobotBalancerv2
Diff: HBridge.cpp
- Revision:
- 0:9afc272fa65f
- Child:
- 4:2d38ad348e0d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HBridge.cpp Fri Jun 24 21:03:20 2016 +0000 @@ -0,0 +1,37 @@ +/* + * 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; +}