A simple Hbridge driver library (tested with the L293D). It uses a PWM pin on the enable and two DIOs on the control pins. Versatile.

Dependents:   RSALB_hbridge_helloworld RSALB_lobster uva_nc

Committer:
p07gbar
Date:
Thu Sep 20 11:36:17 2012 +0000
Revision:
0:16208393c7ae
Working, Documented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p07gbar 0:16208393c7ae 1 /**
p07gbar 0:16208393c7ae 2 * @author Giles Barton-Owen
p07gbar 0:16208393c7ae 3 *
p07gbar 0:16208393c7ae 4 * @section LICENSE
p07gbar 0:16208393c7ae 5 *
p07gbar 0:16208393c7ae 6 * Copyright (c) 2012 mbed
p07gbar 0:16208393c7ae 7 *
p07gbar 0:16208393c7ae 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
p07gbar 0:16208393c7ae 9 * of this software and associated documentation files (the "Software"), to deal
p07gbar 0:16208393c7ae 10 * in the Software without restriction, including without limitation the rights
p07gbar 0:16208393c7ae 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
p07gbar 0:16208393c7ae 12 * copies of the Software, and to permit persons to whom the Software is
p07gbar 0:16208393c7ae 13 * furnished to do so, subject to the following conditions:
p07gbar 0:16208393c7ae 14 *
p07gbar 0:16208393c7ae 15 * The above copyright notice and this permission notice shall be included in
p07gbar 0:16208393c7ae 16 * all copies or substantial portions of the Software.
p07gbar 0:16208393c7ae 17 *
p07gbar 0:16208393c7ae 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
p07gbar 0:16208393c7ae 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
p07gbar 0:16208393c7ae 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
p07gbar 0:16208393c7ae 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
p07gbar 0:16208393c7ae 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
p07gbar 0:16208393c7ae 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
p07gbar 0:16208393c7ae 24 * THE SOFTWARE.
p07gbar 0:16208393c7ae 25 *
p07gbar 0:16208393c7ae 26 * @section DESCRIPTION
p07gbar 0:16208393c7ae 27 * A simple library to control an H-Bridge driver using two DIO and a PWM pin
p07gbar 0:16208393c7ae 28 *
p07gbar 0:16208393c7ae 29 */
p07gbar 0:16208393c7ae 30
p07gbar 0:16208393c7ae 31
p07gbar 0:16208393c7ae 32 #ifndef HBRIDGERAW_H
p07gbar 0:16208393c7ae 33 #define HBRIDGERAW_H
p07gbar 0:16208393c7ae 34
p07gbar 0:16208393c7ae 35 #include "mbed.h"
p07gbar 0:16208393c7ae 36
p07gbar 0:16208393c7ae 37 /** A simple HBridge controller library
p07gbar 0:16208393c7ae 38 */
p07gbar 0:16208393c7ae 39 class HBridge
p07gbar 0:16208393c7ae 40 {
p07gbar 0:16208393c7ae 41 public:
p07gbar 0:16208393c7ae 42 /** Creates an instance of the HBridge class
p07gbar 0:16208393c7ae 43 *
p07gbar 0:16208393c7ae 44 * @param A The pin the "A" line is connected to (naming taken from the L293D).
p07gbar 0:16208393c7ae 45 * @param B The pin the "B" line is connected to (naming taken from the L293D).
p07gbar 0:16208393c7ae 46 * @param en The pin the enable line is connected to.]
p07gbar 0:16208393c7ae 47 */
p07gbar 0:16208393c7ae 48 HBridge(PinName A, PinName B, PinName en);
p07gbar 0:16208393c7ae 49
p07gbar 0:16208393c7ae 50 /** Hard stops the motor
p07gbar 0:16208393c7ae 51 */
p07gbar 0:16208393c7ae 52 void stop();
p07gbar 0:16208393c7ae 53
p07gbar 0:16208393c7ae 54 /** Starts the motor
p07gbar 0:16208393c7ae 55 */
p07gbar 0:16208393c7ae 56 void start();
p07gbar 0:16208393c7ae 57
p07gbar 0:16208393c7ae 58 /** Sets the PWM output of the enable pin and therefore the speed of the motor
p07gbar 0:16208393c7ae 59 *
p07gbar 0:16208393c7ae 60 * @param speed_in The float value of the desired speed between -1 and 1
p07gbar 0:16208393c7ae 61 */
p07gbar 0:16208393c7ae 62 void speed(float speed_in);
p07gbar 0:16208393c7ae 63
p07gbar 0:16208393c7ae 64 /** Turn off and on the enable pin
p07gbar 0:16208393c7ae 65 *
p07gbar 0:16208393c7ae 66 * @param onoff Enable the motor or not (on(true))
p07gbar 0:16208393c7ae 67 */
p07gbar 0:16208393c7ae 68 void power(bool onoff);
p07gbar 0:16208393c7ae 69
p07gbar 0:16208393c7ae 70 /** Set the pwm value of the enable pin
p07gbar 0:16208393c7ae 71 *
p07gbar 0:16208393c7ae 72 * @param power_in The value of the pwm enable pins
p07gbar 0:16208393c7ae 73 */
p07gbar 0:16208393c7ae 74 void power(float power_in);
p07gbar 0:16208393c7ae 75
p07gbar 0:16208393c7ae 76 /** Soft stop the motor
p07gbar 0:16208393c7ae 77 */
p07gbar 0:16208393c7ae 78 void soft_stop();
p07gbar 0:16208393c7ae 79
p07gbar 0:16208393c7ae 80 /** Go forward, starts the motor
p07gbar 0:16208393c7ae 81 */
p07gbar 0:16208393c7ae 82 void forward();
p07gbar 0:16208393c7ae 83
p07gbar 0:16208393c7ae 84 /** Go backward, starts the motor
p07gbar 0:16208393c7ae 85 */
p07gbar 0:16208393c7ae 86 void backward();
p07gbar 0:16208393c7ae 87
p07gbar 0:16208393c7ae 88 /** Go forward, starts the motor
p07gbar 0:16208393c7ae 89 *
p07gbar 0:16208393c7ae 90 * @param speed_in The float value of the desired speed between 0 and 1
p07gbar 0:16208393c7ae 91 */
p07gbar 0:16208393c7ae 92 void forward(float speed_in);
p07gbar 0:16208393c7ae 93
p07gbar 0:16208393c7ae 94 /** Go backward, starts the motor
p07gbar 0:16208393c7ae 95 *
p07gbar 0:16208393c7ae 96 * @param speed_in The float value of the desired speed between 0 and 1
p07gbar 0:16208393c7ae 97 */
p07gbar 0:16208393c7ae 98 void backward(float speed_in);
p07gbar 0:16208393c7ae 99
p07gbar 0:16208393c7ae 100 /** Set A high or low
p07gbar 0:16208393c7ae 101 *
p07gbar 0:16208393c7ae 102 * @param highlow Set the high/low state of the A half-bridge (high(true))
p07gbar 0:16208393c7ae 103 */
p07gbar 0:16208393c7ae 104 void A(bool highlow);
p07gbar 0:16208393c7ae 105
p07gbar 0:16208393c7ae 106 /** Set B high or low
p07gbar 0:16208393c7ae 107 *
p07gbar 0:16208393c7ae 108 * @param highlow Set the high/low state of the B half-bridge (high(true))
p07gbar 0:16208393c7ae 109 */
p07gbar 0:16208393c7ae 110 void B(bool highlow);
p07gbar 0:16208393c7ae 111
p07gbar 0:16208393c7ae 112 /** Set the direction of travel
p07gbar 0:16208393c7ae 113 *
p07gbar 0:16208393c7ae 114 * @param direction_in Forward = true
p07gbar 0:16208393c7ae 115 */
p07gbar 0:16208393c7ae 116 void direction(bool direction_in);
p07gbar 0:16208393c7ae 117
p07gbar 0:16208393c7ae 118
p07gbar 0:16208393c7ae 119 private:
p07gbar 0:16208393c7ae 120 PwmOut enable;
p07gbar 0:16208393c7ae 121 DigitalOut Adrive;
p07gbar 0:16208393c7ae 122 DigitalOut Bdrive;
p07gbar 0:16208393c7ae 123
p07gbar 0:16208393c7ae 124 bool power_status;
p07gbar 0:16208393c7ae 125 float speed_value;
p07gbar 0:16208393c7ae 126 bool A_value;
p07gbar 0:16208393c7ae 127 bool B_value;
p07gbar 0:16208393c7ae 128 bool stored_direction;
p07gbar 0:16208393c7ae 129
p07gbar 0:16208393c7ae 130 void set();
p07gbar 0:16208393c7ae 131
p07gbar 0:16208393c7ae 132
p07gbar 0:16208393c7ae 133 };
p07gbar 0:16208393c7ae 134
p07gbar 0:16208393c7ae 135
p07gbar 0:16208393c7ae 136
p07gbar 0:16208393c7ae 137 #endif