Class library for a L298 H-Bridge to be used for motor control.
L298HBridge.h@1:6d242bb216d6, 2017-01-19 (annotated)
- Committer:
- RiaanEhlers
- Date:
- Thu Jan 19 12:34:48 2017 +0000
- Revision:
- 1:6d242bb216d6
- Parent:
- 0:39561fe6e4ff
- Child:
- 2:1c000b6cf863
Testing
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 | 1:6d242bb216d6 | 30 | /** Class library for a L298 H-Bridge. |
RiaanEhlers | 0:39561fe6e4ff | 31 | * |
RiaanEhlers | 0:39561fe6e4ff | 32 | * Example: |
RiaanEhlers | 0:39561fe6e4ff | 33 | * @code |
RiaanEhlers | 1:6d242bb216d6 | 34 | * |
RiaanEhlers | 0:39561fe6e4ff | 35 | * @endcode |
RiaanEhlers | 0:39561fe6e4ff | 36 | */ |
RiaanEhlers | 0:39561fe6e4ff | 37 | |
RiaanEhlers | 0:39561fe6e4ff | 38 | class L298HBridge { |
RiaanEhlers | 0:39561fe6e4ff | 39 | public: |
RiaanEhlers | 1:6d242bb216d6 | 40 | /** Create a L298HBridge object connected to the specified pins. |
RiaanEhlers | 1:6d242bb216d6 | 41 | * Once created, the motor speed will be set to 0 (PWM signal will be 0%) and |
RiaanEhlers | 1:6d242bb216d6 | 42 | * the motor will be in the "stop" direction (neither forward or reverse). |
RiaanEhlers | 1:6d242bb216d6 | 43 | * @param ENPin PwmOut compatible pin used to connect to L298's En pin associated with enabling the H-Bridge. |
RiaanEhlers | 1:6d242bb216d6 | 44 | * @param FWDPin GPIO pin used to connect to L298's In pin associated with forward direction. |
RiaanEhlers | 1:6d242bb216d6 | 45 | * @param REVPin GPIO pin used to connect to L298's In pin associated with reverse direction. |
RiaanEhlers | 0:39561fe6e4ff | 46 | */ |
RiaanEhlers | 0:39561fe6e4ff | 47 | L298HBridge(PinName ENPin, PinName FWDPin, PinName REVPin); |
RiaanEhlers | 0:39561fe6e4ff | 48 | |
RiaanEhlers | 1:6d242bb216d6 | 49 | /** Switch the H-Bridge to run the motor in the forward direction. |
RiaanEhlers | 0:39561fe6e4ff | 50 | * @param None |
RiaanEhlers | 0:39561fe6e4ff | 51 | */ |
RiaanEhlers | 0:39561fe6e4ff | 52 | void Fwd(); |
RiaanEhlers | 0:39561fe6e4ff | 53 | |
RiaanEhlers | 1:6d242bb216d6 | 54 | /** Switch the H-Bridge to run the motor in the reverse direction. |
RiaanEhlers | 0:39561fe6e4ff | 55 | * @param None |
RiaanEhlers | 0:39561fe6e4ff | 56 | */ |
RiaanEhlers | 0:39561fe6e4ff | 57 | void Rev(); |
RiaanEhlers | 0:39561fe6e4ff | 58 | |
RiaanEhlers | 1:6d242bb216d6 | 59 | /** Switch the H-Bridge off. The H-Bridge is not set to forward or reverse. |
RiaanEhlers | 0:39561fe6e4ff | 60 | * @param None |
RiaanEhlers | 0:39561fe6e4ff | 61 | */ |
RiaanEhlers | 0:39561fe6e4ff | 62 | void Stop(); |
RiaanEhlers | 0:39561fe6e4ff | 63 | |
RiaanEhlers | 1:6d242bb216d6 | 64 | /** Change the motor's speed by adjusting the PWM signal. |
RiaanEhlers | 1:6d242bb216d6 | 65 | * @param int DutyPercent |
RiaanEhlers | 0:39561fe6e4ff | 66 | */ |
RiaanEhlers | 0:39561fe6e4ff | 67 | void Speed(int DutyPercent); |
RiaanEhlers | 0:39561fe6e4ff | 68 | |
RiaanEhlers | 0:39561fe6e4ff | 69 | private: |
RiaanEhlers | 0:39561fe6e4ff | 70 | PwmOut _ENPin; |
RiaanEhlers | 0:39561fe6e4ff | 71 | DigitalOut _FWDPin, _REVPin; |
RiaanEhlers | 0:39561fe6e4ff | 72 | }; |
RiaanEhlers | 0:39561fe6e4ff | 73 | |
RiaanEhlers | 0:39561fe6e4ff | 74 | #endif |