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@0:9afc272fa65f, 2016-06-24 (annotated)
- Committer:
- Electrotiger
- Date:
- Fri Jun 24 21:03:20 2016 +0000
- Revision:
- 0:9afc272fa65f
- Child:
- 4:2d38ad348e0d
First Commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Electrotiger | 0:9afc272fa65f | 1 | /* |
Electrotiger | 0:9afc272fa65f | 2 | * HBridge.cpp |
Electrotiger | 0:9afc272fa65f | 3 | * |
Electrotiger | 0:9afc272fa65f | 4 | * Created on: Jun 4, 2016 |
Electrotiger | 0:9afc272fa65f | 5 | * Author: Developer |
Electrotiger | 0:9afc272fa65f | 6 | */ |
Electrotiger | 0:9afc272fa65f | 7 | |
Electrotiger | 0:9afc272fa65f | 8 | #include <HBridge.hpp> |
Electrotiger | 0:9afc272fa65f | 9 | #include <cmath> |
Electrotiger | 0:9afc272fa65f | 10 | |
Electrotiger | 0:9afc272fa65f | 11 | HBridge::HBridge(PinName PWMPin, PinName DirectionPin) |
Electrotiger | 0:9afc272fa65f | 12 | : PWMOut(PWMPin), DirectionOut(DirectionPin) { |
Electrotiger | 0:9afc272fa65f | 13 | // 10 ms period == 100 Hz works well. |
Electrotiger | 0:9afc272fa65f | 14 | PWMOut.period_ms(10); |
Electrotiger | 0:9afc272fa65f | 15 | } |
Electrotiger | 0:9afc272fa65f | 16 | |
Electrotiger | 0:9afc272fa65f | 17 | HBridge::~HBridge() { |
Electrotiger | 0:9afc272fa65f | 18 | // TODO Auto-generated destructor stub |
Electrotiger | 0:9afc272fa65f | 19 | } |
Electrotiger | 0:9afc272fa65f | 20 | |
Electrotiger | 0:9afc272fa65f | 21 | void HBridge::write(float percent) { |
Electrotiger | 0:9afc272fa65f | 22 | // Write the percent value as a PWM out. |
Electrotiger | 0:9afc272fa65f | 23 | PWMOut.write(std::abs(percent)); |
Electrotiger | 0:9afc272fa65f | 24 | // Write the direction as the sign of percent. |
Electrotiger | 0:9afc272fa65f | 25 | DirectionOut.write(percent >= 0); |
Electrotiger | 0:9afc272fa65f | 26 | // Set the output has changed flag to 1. |
Electrotiger | 0:9afc272fa65f | 27 | outputHasChanged = true; |
Electrotiger | 0:9afc272fa65f | 28 | } |
Electrotiger | 0:9afc272fa65f | 29 | |
Electrotiger | 0:9afc272fa65f | 30 | float HBridge::read() { |
Electrotiger | 0:9afc272fa65f | 31 | static float prevValue; |
Electrotiger | 0:9afc272fa65f | 32 | if (outputHasChanged) { |
Electrotiger | 0:9afc272fa65f | 33 | outputHasChanged = false; |
Electrotiger | 0:9afc272fa65f | 34 | prevValue = (DirectionOut.read()) ? PWMOut.read() : -PWMOut.read(); |
Electrotiger | 0:9afc272fa65f | 35 | } |
Electrotiger | 0:9afc272fa65f | 36 | return prevValue; |
Electrotiger | 0:9afc272fa65f | 37 | } |