Class library for a L298 H-Bridge to be used for motor control.

Committer:
RiaanEhlers
Date:
Fri Jan 20 09:10:05 2017 +0000
Revision:
2:1c000b6cf863
Parent:
1:6d242bb216d6
V1.0

Who changed what in which revision?

UserRevisionLine numberNew 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 2:1c000b6cf863 30 /** Class library for a L298 H-Bridge. The class is written for one H-Bridge of
RiaanEhlers 2:1c000b6cf863 31 * the L298. Constructing the class twice will enable you to use both H-bridges.
RiaanEhlers 0:39561fe6e4ff 32 *
RiaanEhlers 0:39561fe6e4ff 33 * Example:
RiaanEhlers 0:39561fe6e4ff 34 * @code
RiaanEhlers 2:1c000b6cf863 35 * #include "mbed.h"
RiaanEhlers 2:1c000b6cf863 36 * #include "L298HBridge.h"
RiaanEhlers 2:1c000b6cf863 37
RiaanEhlers 2:1c000b6cf863 38 * L298HBridge Motor(PB_4, PC_4, PC_5);
RiaanEhlers 2:1c000b6cf863 39
RiaanEhlers 2:1c000b6cf863 40 * int main()
RiaanEhlers 2:1c000b6cf863 41 * {
RiaanEhlers 2:1c000b6cf863 42 * float i;
RiaanEhlers 2:1c000b6cf863 43 * while(1)
RiaanEhlers 2:1c000b6cf863 44 * {
RiaanEhlers 2:1c000b6cf863 45 * Motor.Fwd();
RiaanEhlers 2:1c000b6cf863 46 *
RiaanEhlers 2:1c000b6cf863 47 * for(i=0;i<100;i++)
RiaanEhlers 2:1c000b6cf863 48 * {
RiaanEhlers 2:1c000b6cf863 49 * Motor.Speed(i);
RiaanEhlers 2:1c000b6cf863 50 * wait(0.1);
RiaanEhlers 2:1c000b6cf863 51 * }
RiaanEhlers 2:1c000b6cf863 52 *
RiaanEhlers 2:1c000b6cf863 53 * for(i=100;i>25;i--)
RiaanEhlers 2:1c000b6cf863 54 * {
RiaanEhlers 2:1c000b6cf863 55 * Motor.Speed(i);
RiaanEhlers 2:1c000b6cf863 56 * wait(0.1);
RiaanEhlers 2:1c000b6cf863 57 * }
RiaanEhlers 2:1c000b6cf863 58 *
RiaanEhlers 2:1c000b6cf863 59 * Motor.Rev();
RiaanEhlers 2:1c000b6cf863 60 *
RiaanEhlers 2:1c000b6cf863 61 * for(i=0;i<100;i++)
RiaanEhlers 2:1c000b6cf863 62 * {
RiaanEhlers 2:1c000b6cf863 63 * Motor.Speed(i);
RiaanEhlers 2:1c000b6cf863 64 * wait(0.1);
RiaanEhlers 2:1c000b6cf863 65 * }
RiaanEhlers 2:1c000b6cf863 66 *
RiaanEhlers 2:1c000b6cf863 67 * for(i=100;i>25;i--)
RiaanEhlers 2:1c000b6cf863 68 * {
RiaanEhlers 2:1c000b6cf863 69 * Motor.Speed(i);
RiaanEhlers 2:1c000b6cf863 70 * wait(0.1);
RiaanEhlers 2:1c000b6cf863 71 * }
RiaanEhlers 2:1c000b6cf863 72 * }
RiaanEhlers 2:1c000b6cf863 73 * }
RiaanEhlers 0:39561fe6e4ff 74 * @endcode
RiaanEhlers 0:39561fe6e4ff 75 */
RiaanEhlers 0:39561fe6e4ff 76
RiaanEhlers 0:39561fe6e4ff 77 class L298HBridge {
RiaanEhlers 0:39561fe6e4ff 78 public:
RiaanEhlers 1:6d242bb216d6 79 /** Create a L298HBridge object connected to the specified pins.
RiaanEhlers 1:6d242bb216d6 80 * Once created, the motor speed will be set to 0 (PWM signal will be 0%) and
RiaanEhlers 2:1c000b6cf863 81 * the motor will be in the stop mode (neither forward or reverse).
RiaanEhlers 2:1c000b6cf863 82 * @param ENPin PwmOut compatible pin used to connect to L298's En(x) pin associated with enabling the H-Bridge.
RiaanEhlers 2:1c000b6cf863 83 * @param FWDPin GPIO pin used to connect to L298's In(x) pin associated with forward direction.
RiaanEhlers 2:1c000b6cf863 84 * @param REVPin GPIO pin used to connect to L298's In(x) pin associated with reverse direction.
RiaanEhlers 0:39561fe6e4ff 85 */
RiaanEhlers 0:39561fe6e4ff 86 L298HBridge(PinName ENPin, PinName FWDPin, PinName REVPin);
RiaanEhlers 0:39561fe6e4ff 87
RiaanEhlers 2:1c000b6cf863 88 /** Configure the H-Bridge to run the motor in the forward direction.
RiaanEhlers 0:39561fe6e4ff 89 * @param None
RiaanEhlers 0:39561fe6e4ff 90 */
RiaanEhlers 0:39561fe6e4ff 91 void Fwd();
RiaanEhlers 0:39561fe6e4ff 92
RiaanEhlers 2:1c000b6cf863 93 /** Configure the H-Bridge to run the motor in the reverse direction.
RiaanEhlers 0:39561fe6e4ff 94 * @param None
RiaanEhlers 0:39561fe6e4ff 95 */
RiaanEhlers 0:39561fe6e4ff 96 void Rev();
RiaanEhlers 0:39561fe6e4ff 97
RiaanEhlers 1:6d242bb216d6 98 /** Switch the H-Bridge off. The H-Bridge is not set to forward or reverse.
RiaanEhlers 0:39561fe6e4ff 99 * @param None
RiaanEhlers 0:39561fe6e4ff 100 */
RiaanEhlers 0:39561fe6e4ff 101 void Stop();
RiaanEhlers 0:39561fe6e4ff 102
RiaanEhlers 2:1c000b6cf863 103 /** Change the motor's speed by adjusting the PWM signal.
RiaanEhlers 2:1c000b6cf863 104 * The value passed to the function can be any value from 0 to 100.
RiaanEhlers 2:1c000b6cf863 105 * Where 0 = 0% and 100 = 100%.
RiaanEhlers 2:1c000b6cf863 106 * @param DutyPercent
RiaanEhlers 0:39561fe6e4ff 107 */
RiaanEhlers 2:1c000b6cf863 108 void Speed(float DutyPercent);
RiaanEhlers 0:39561fe6e4ff 109
RiaanEhlers 0:39561fe6e4ff 110 private:
RiaanEhlers 0:39561fe6e4ff 111 PwmOut _ENPin;
RiaanEhlers 0:39561fe6e4ff 112 DigitalOut _FWDPin, _REVPin;
RiaanEhlers 0:39561fe6e4ff 113 };
RiaanEhlers 0:39561fe6e4ff 114
RiaanEhlers 0:39561fe6e4ff 115 #endif