Class library for a L298 H-Bridge to be used for motor control.
L298HBridge.h@0:39561fe6e4ff, 2017-01-19 (annotated)
- Committer:
- RiaanEhlers
- Date:
- Thu Jan 19 11:33:01 2017 +0000
- Revision:
- 0:39561fe6e4ff
- Child:
- 1:6d242bb216d6
v1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RiaanEhlers | 0:39561fe6e4ff | 1 | /* L298HBridge Library v1.0 |
RiaanEhlers | 0:39561fe6e4ff | 2 | * Copyright (c) 2017 Riaan Ehlers |
RiaanEhlers | 0:39561fe6e4ff | 3 | * riaan.ehlers@nmmu.ac.za |
RiaanEhlers | 0:39561fe6e4ff | 4 | * |
RiaanEhlers | 0:39561fe6e4ff | 5 | * |
RiaanEhlers | 0:39561fe6e4ff | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
RiaanEhlers | 0:39561fe6e4ff | 7 | * of this software and associated documentation files (the "Software"), to deal |
RiaanEhlers | 0:39561fe6e4ff | 8 | * in the Software without restriction, including without limitation the rights |
RiaanEhlers | 0:39561fe6e4ff | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
RiaanEhlers | 0:39561fe6e4ff | 10 | * copies of the Software, and to permit persons to whom the Software is |
RiaanEhlers | 0:39561fe6e4ff | 11 | * furnished to do so, subject to the following conditions: |
RiaanEhlers | 0:39561fe6e4ff | 12 | * |
RiaanEhlers | 0:39561fe6e4ff | 13 | * The above copyright notice and this permission notice shall be included in |
RiaanEhlers | 0:39561fe6e4ff | 14 | * all copies or substantial portions of the Software. |
RiaanEhlers | 0:39561fe6e4ff | 15 | * |
RiaanEhlers | 0:39561fe6e4ff | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
RiaanEhlers | 0:39561fe6e4ff | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
RiaanEhlers | 0:39561fe6e4ff | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
RiaanEhlers | 0:39561fe6e4ff | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
RiaanEhlers | 0:39561fe6e4ff | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
RiaanEhlers | 0:39561fe6e4ff | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
RiaanEhlers | 0:39561fe6e4ff | 22 | * THE SOFTWARE. |
RiaanEhlers | 0:39561fe6e4ff | 23 | */ |
RiaanEhlers | 0:39561fe6e4ff | 24 | |
RiaanEhlers | 0:39561fe6e4ff | 25 | #ifndef L298HBridge_H |
RiaanEhlers | 0:39561fe6e4ff | 26 | #define L298HBridge_H |
RiaanEhlers | 0:39561fe6e4ff | 27 | |
RiaanEhlers | 0:39561fe6e4ff | 28 | #include "mbed.h" |
RiaanEhlers | 0:39561fe6e4ff | 29 | |
RiaanEhlers | 0:39561fe6e4ff | 30 | /** Class library for a HC-SR04 Distance Sensor based on PwmOut (Trig) and InterruptIn (Echo). |
RiaanEhlers | 0:39561fe6e4ff | 31 | * |
RiaanEhlers | 0:39561fe6e4ff | 32 | * Example: |
RiaanEhlers | 0:39561fe6e4ff | 33 | * @code |
RiaanEhlers | 0:39561fe6e4ff | 34 | * #include "mbed.h" |
RiaanEhlers | 0:39561fe6e4ff | 35 | * #include "HCSR04.h" |
RiaanEhlers | 0:39561fe6e4ff | 36 | * |
RiaanEhlers | 0:39561fe6e4ff | 37 | * HCSR04 distance(PB_8, PA_1); |
RiaanEhlers | 0:39561fe6e4ff | 38 | * |
RiaanEhlers | 0:39561fe6e4ff | 39 | * int main() { |
RiaanEhlers | 0:39561fe6e4ff | 40 | * while(1){ |
RiaanEhlers | 0:39561fe6e4ff | 41 | * SWO.printf("Distance = %d (us) %f (cm)\n", distance.read_us(), distance.read_cm()); |
RiaanEhlers | 0:39561fe6e4ff | 42 | * } |
RiaanEhlers | 0:39561fe6e4ff | 43 | * } |
RiaanEhlers | 0:39561fe6e4ff | 44 | * @endcode |
RiaanEhlers | 0:39561fe6e4ff | 45 | */ |
RiaanEhlers | 0:39561fe6e4ff | 46 | |
RiaanEhlers | 0:39561fe6e4ff | 47 | class L298HBridge { |
RiaanEhlers | 0:39561fe6e4ff | 48 | public: |
RiaanEhlers | 0:39561fe6e4ff | 49 | /** Create a HCSR04 object connected to the specified pins. Once created, the PWM signal |
RiaanEhlers | 0:39561fe6e4ff | 50 | * on the Trig pin will start immediately and measurements will start in the background. |
RiaanEhlers | 0:39561fe6e4ff | 51 | * @param ENPin PwmOut compatible pin used to connect to HC-SR04's Trig pin |
RiaanEhlers | 0:39561fe6e4ff | 52 | * @param EchoPin InterruptIn compatible pin used to connect to HC-SR04's Echo pin |
RiaanEhlers | 0:39561fe6e4ff | 53 | */ |
RiaanEhlers | 0:39561fe6e4ff | 54 | L298HBridge(PinName ENPin, PinName FWDPin, PinName REVPin); |
RiaanEhlers | 0:39561fe6e4ff | 55 | |
RiaanEhlers | 0:39561fe6e4ff | 56 | /** Create a HCSR04 object connected to the specified pins. Once created, the PWM signal |
RiaanEhlers | 0:39561fe6e4ff | 57 | * on the Trig pin will start immediately and measurements will start in the background. |
RiaanEhlers | 0:39561fe6e4ff | 58 | * @param None |
RiaanEhlers | 0:39561fe6e4ff | 59 | */ |
RiaanEhlers | 0:39561fe6e4ff | 60 | void Fwd(); |
RiaanEhlers | 0:39561fe6e4ff | 61 | |
RiaanEhlers | 0:39561fe6e4ff | 62 | /** Create a HCSR04 object connected to the specified pins. Once created, the PWM signal |
RiaanEhlers | 0:39561fe6e4ff | 63 | * on the Trig pin will start immediately and measurements will start in the background. |
RiaanEhlers | 0:39561fe6e4ff | 64 | * @param None |
RiaanEhlers | 0:39561fe6e4ff | 65 | */ |
RiaanEhlers | 0:39561fe6e4ff | 66 | void Rev(); |
RiaanEhlers | 0:39561fe6e4ff | 67 | |
RiaanEhlers | 0:39561fe6e4ff | 68 | /** Create a HCSR04 object connected to the specified pins. Once created, the PWM signal |
RiaanEhlers | 0:39561fe6e4ff | 69 | * on the Trig pin will start immediately and measurements will start in the background. |
RiaanEhlers | 0:39561fe6e4ff | 70 | * @param None |
RiaanEhlers | 0:39561fe6e4ff | 71 | */ |
RiaanEhlers | 0:39561fe6e4ff | 72 | void Stop(); |
RiaanEhlers | 0:39561fe6e4ff | 73 | |
RiaanEhlers | 0:39561fe6e4ff | 74 | /** Create a HCSR04 object connected to the specified pins. Once created, the PWM signal |
RiaanEhlers | 0:39561fe6e4ff | 75 | * on the Trig pin will start immediately and measurements will start in the background. |
RiaanEhlers | 0:39561fe6e4ff | 76 | * @param None |
RiaanEhlers | 0:39561fe6e4ff | 77 | */ |
RiaanEhlers | 0:39561fe6e4ff | 78 | void Speed(int DutyPercent); |
RiaanEhlers | 0:39561fe6e4ff | 79 | |
RiaanEhlers | 0:39561fe6e4ff | 80 | private: |
RiaanEhlers | 0:39561fe6e4ff | 81 | PwmOut _ENPin; |
RiaanEhlers | 0:39561fe6e4ff | 82 | DigitalOut _FWDPin, _REVPin; |
RiaanEhlers | 0:39561fe6e4ff | 83 | }; |
RiaanEhlers | 0:39561fe6e4ff | 84 | |
RiaanEhlers | 0:39561fe6e4ff | 85 | #endif |