Class library for a L298 H-Bridge to be used for motor control.
Embed:
(wiki syntax)
Show/hide line numbers
L298HBridge.h
00001 /* L298HBridge Library v1.0 00002 * Copyright (c) 2017 Riaan Ehlers 00003 * riaan.ehlers@nmmu.ac.za 00004 * 00005 * 00006 * Permission is hereby granted, free of charge, to any person obtaining a copy 00007 * of this software and associated documentation files (the "Software"), to deal 00008 * in the Software without restriction, including without limitation the rights 00009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00010 * copies of the Software, and to permit persons to whom the Software is 00011 * furnished to do so, subject to the following conditions: 00012 * 00013 * The above copyright notice and this permission notice shall be included in 00014 * all copies or substantial portions of the Software. 00015 * 00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00022 * THE SOFTWARE. 00023 */ 00024 00025 #ifndef L298HBridge_H 00026 #define L298HBridge_H 00027 00028 #include "mbed.h" 00029 00030 /** Class library for a L298 H-Bridge. The class is written for one H-Bridge of 00031 * the L298. Constructing the class twice will enable you to use both H-bridges. 00032 * 00033 * Example: 00034 * @code 00035 * #include "mbed.h" 00036 * #include "L298HBridge.h" 00037 00038 * L298HBridge Motor(PB_4, PC_4, PC_5); 00039 00040 * int main() 00041 * { 00042 * float i; 00043 * while(1) 00044 * { 00045 * Motor.Fwd(); 00046 * 00047 * for(i=0;i<100;i++) 00048 * { 00049 * Motor.Speed(i); 00050 * wait(0.1); 00051 * } 00052 * 00053 * for(i=100;i>25;i--) 00054 * { 00055 * Motor.Speed(i); 00056 * wait(0.1); 00057 * } 00058 * 00059 * Motor.Rev(); 00060 * 00061 * for(i=0;i<100;i++) 00062 * { 00063 * Motor.Speed(i); 00064 * wait(0.1); 00065 * } 00066 * 00067 * for(i=100;i>25;i--) 00068 * { 00069 * Motor.Speed(i); 00070 * wait(0.1); 00071 * } 00072 * } 00073 * } 00074 * @endcode 00075 */ 00076 00077 class L298HBridge { 00078 public: 00079 /** Create a L298HBridge object connected to the specified pins. 00080 * Once created, the motor speed will be set to 0 (PWM signal will be 0%) and 00081 * the motor will be in the stop mode (neither forward or reverse). 00082 * @param ENPin PwmOut compatible pin used to connect to L298's En(x) pin associated with enabling the H-Bridge. 00083 * @param FWDPin GPIO pin used to connect to L298's In(x) pin associated with forward direction. 00084 * @param REVPin GPIO pin used to connect to L298's In(x) pin associated with reverse direction. 00085 */ 00086 L298HBridge(PinName ENPin, PinName FWDPin, PinName REVPin); 00087 00088 /** Configure the H-Bridge to run the motor in the forward direction. 00089 * @param None 00090 */ 00091 void Fwd(); 00092 00093 /** Configure the H-Bridge to run the motor in the reverse direction. 00094 * @param None 00095 */ 00096 void Rev(); 00097 00098 /** Switch the H-Bridge off. The H-Bridge is not set to forward or reverse. 00099 * @param None 00100 */ 00101 void Stop(); 00102 00103 /** Change the motor's speed by adjusting the PWM signal. 00104 * The value passed to the function can be any value from 0 to 100. 00105 * Where 0 = 0% and 100 = 100%. 00106 * @param DutyPercent 00107 */ 00108 void Speed(float DutyPercent); 00109 00110 private: 00111 PwmOut _ENPin; 00112 DigitalOut _FWDPin, _REVPin; 00113 }; 00114 00115 #endif
Generated on Wed Jul 20 2022 19:00:01 by
1.7.2