Drivers for the mini robot designed for Princeton's MAE 433 course.
Dependencies: mbed-dsp mbed-rtos mbed
Dependents: MAE433_Library_Tester RobotBalancerv2
HBridge.hpp@0:9afc272fa65f, 2016-06-24 (annotated)
- Committer:
- Electrotiger
- Date:
- Fri Jun 24 21:03:20 2016 +0000
- Revision:
- 0:9afc272fa65f
First Commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Electrotiger | 0:9afc272fa65f | 1 | /** |
Electrotiger | 0:9afc272fa65f | 2 | * @file HBridge.hpp |
Electrotiger | 0:9afc272fa65f | 3 | * @author Weimen Li |
Electrotiger | 0:9afc272fa65f | 4 | * @date June 4th, 2016 |
Electrotiger | 0:9afc272fa65f | 5 | * @brief HBridge Class Header |
Electrotiger | 0:9afc272fa65f | 6 | * @class HBridge |
Electrotiger | 0:9afc272fa65f | 7 | * @brief The HBridge class encapsulates a physical H-Bridge from which the direction |
Electrotiger | 0:9afc272fa65f | 8 | * and PWM output can be controlled with separate pins. |
Electrotiger | 0:9afc272fa65f | 9 | * Example Usage: |
Electrotiger | 0:9afc272fa65f | 10 | * @code |
Electrotiger | 0:9afc272fa65f | 11 | // First, you must ensure that HBridge is included. |
Electrotiger | 0:9afc272fa65f | 12 | #include "HBridge.hpp" |
Electrotiger | 0:9afc272fa65f | 13 | |
Electrotiger | 0:9afc272fa65f | 14 | // Create a new H-Bridge object - |
Electrotiger | 0:9afc272fa65f | 15 | // If many functions need access to it, do it out here, in a global scope. |
Electrotiger | 0:9afc272fa65f | 16 | // Otherwise, only create it in the function that you need it in. |
Electrotiger | 0:9afc272fa65f | 17 | |
Electrotiger | 0:9afc272fa65f | 18 | // With a global scope: |
Electrotiger | 0:9afc272fa65f | 19 | HBridge HBridgeGlobal(PTA12, PTA13); |
Electrotiger | 0:9afc272fa65f | 20 | |
Electrotiger | 0:9afc272fa65f | 21 | void someFunction() { |
Electrotiger | 0:9afc272fa65f | 22 | // With a local scope: (May need to be static to persist across calls to the function.) |
Electrotiger | 0:9afc272fa65f | 23 | static HBridge HBridgeLocal(PTA14, PTA15); |
Electrotiger | 0:9afc272fa65f | 24 | HBridgeLocal.write(0.75); |
Electrotiger | 0:9afc272fa65f | 25 | } |
Electrotiger | 0:9afc272fa65f | 26 | |
Electrotiger | 0:9afc272fa65f | 27 | int main() { |
Electrotiger | 0:9afc272fa65f | 28 | |
Electrotiger | 0:9afc272fa65f | 29 | for(;;) { |
Electrotiger | 0:9afc272fa65f | 30 | // Write to an HBridge by calling its write method: |
Electrotiger | 0:9afc272fa65f | 31 | HBridgeGlobal.write(0.50); |
Electrotiger | 0:9afc272fa65f | 32 | someFunction(); |
Electrotiger | 0:9afc272fa65f | 33 | } |
Electrotiger | 0:9afc272fa65f | 34 | } |
Electrotiger | 0:9afc272fa65f | 35 | * @endcode |
Electrotiger | 0:9afc272fa65f | 36 | * |
Electrotiger | 0:9afc272fa65f | 37 | */ |
Electrotiger | 0:9afc272fa65f | 38 | |
Electrotiger | 0:9afc272fa65f | 39 | #ifndef HBRIDGE_H_ |
Electrotiger | 0:9afc272fa65f | 40 | #define HBRIDGE_H_ |
Electrotiger | 0:9afc272fa65f | 41 | #include "mbed.h" |
Electrotiger | 0:9afc272fa65f | 42 | |
Electrotiger | 0:9afc272fa65f | 43 | class HBridge { |
Electrotiger | 0:9afc272fa65f | 44 | public: |
Electrotiger | 0:9afc272fa65f | 45 | /** |
Electrotiger | 0:9afc272fa65f | 46 | * @brief Constructor for the H-bridge. |
Electrotiger | 0:9afc272fa65f | 47 | * @param PWMPin The Pin that the motor PWM is attached to. |
Electrotiger | 0:9afc272fa65f | 48 | * @param DirectionPin The Pin that the direction control is attached to. |
Electrotiger | 0:9afc272fa65f | 49 | */ |
Electrotiger | 0:9afc272fa65f | 50 | HBridge(PinName PWMPin, PinName DirectionPin); |
Electrotiger | 0:9afc272fa65f | 51 | virtual ~HBridge(); |
Electrotiger | 0:9afc272fa65f | 52 | /** |
Electrotiger | 0:9afc272fa65f | 53 | * @brief Write an output to the H bridge. |
Electrotiger | 0:9afc272fa65f | 54 | * @param percent a float between -1 and 1, where the magnitude indicates the |
Electrotiger | 0:9afc272fa65f | 55 | * duty cycle, and the sign indicates the direction. Values exceeding the range are automatically |
Electrotiger | 0:9afc272fa65f | 56 | * set to 1. |
Electrotiger | 0:9afc272fa65f | 57 | */ |
Electrotiger | 0:9afc272fa65f | 58 | void write(float percent); |
Electrotiger | 0:9afc272fa65f | 59 | /** |
Electrotiger | 0:9afc272fa65f | 60 | * @brief Read the current output from the H-bridge. |
Electrotiger | 0:9afc272fa65f | 61 | * @returns A float between -1 and 1, where the magnitude |
Electrotiger | 0:9afc272fa65f | 62 | * indicates the duty cycle, and the sign indicates the direction. |
Electrotiger | 0:9afc272fa65f | 63 | */ |
Electrotiger | 0:9afc272fa65f | 64 | float read(); |
Electrotiger | 0:9afc272fa65f | 65 | private: |
Electrotiger | 0:9afc272fa65f | 66 | PwmOut PWMOut; |
Electrotiger | 0:9afc272fa65f | 67 | DigitalOut DirectionOut; |
Electrotiger | 0:9afc272fa65f | 68 | bool outputHasChanged; |
Electrotiger | 0:9afc272fa65f | 69 | }; |
Electrotiger | 0:9afc272fa65f | 70 | |
Electrotiger | 0:9afc272fa65f | 71 | #endif /* HBRIDGE_H_ */ |