Library for H-Bridge Motor Driver Using Bipolar Transistors
motorlib.cpp@2:c1f9f9d74f35, 2013-01-26 (annotated)
- Committer:
- prabhuvd
- Date:
- Sat Jan 26 15:00:23 2013 +0000
- Revision:
- 2:c1f9f9d74f35
- Parent:
- 0:155daae8a9fa
LM293D and PWM based drive support added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
prabhuvd | 0:155daae8a9fa | 1 | /* mbed motorlib Library for H-Bridge Motor Driver Using Bipolar Transistors |
prabhuvd | 0:155daae8a9fa | 2 | * Copyright (c) 2007-2012, Prabhu Desai http://mbed.org |
prabhuvd | 0:155daae8a9fa | 3 | * |
prabhuvd | 0:155daae8a9fa | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
prabhuvd | 0:155daae8a9fa | 5 | * of this software and associated documentation files (the "Software"), to deal |
prabhuvd | 0:155daae8a9fa | 6 | * in the Software without restriction, including without limitation the rights |
prabhuvd | 0:155daae8a9fa | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
prabhuvd | 0:155daae8a9fa | 8 | * copies of the Software, and to permit persons to whom the Software is |
prabhuvd | 0:155daae8a9fa | 9 | * furnished to do so, subject to the following conditions: |
prabhuvd | 0:155daae8a9fa | 10 | * |
prabhuvd | 0:155daae8a9fa | 11 | * The above copyright notice and this permission notice shall be included in |
prabhuvd | 0:155daae8a9fa | 12 | * all copies or substantial portions of the Software. |
prabhuvd | 0:155daae8a9fa | 13 | * |
prabhuvd | 0:155daae8a9fa | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
prabhuvd | 0:155daae8a9fa | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
prabhuvd | 0:155daae8a9fa | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
prabhuvd | 0:155daae8a9fa | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
prabhuvd | 0:155daae8a9fa | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
prabhuvd | 0:155daae8a9fa | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
prabhuvd | 0:155daae8a9fa | 20 | * THE SOFTWARE. |
prabhuvd | 0:155daae8a9fa | 21 | */ |
prabhuvd | 0:155daae8a9fa | 22 | |
prabhuvd | 0:155daae8a9fa | 23 | #include "motorlib.h" |
prabhuvd | 0:155daae8a9fa | 24 | #include "mbed.h" |
prabhuvd | 0:155daae8a9fa | 25 | |
prabhuvd | 2:c1f9f9d74f35 | 26 | #ifdef LM293D_MOTOR_CTRL |
prabhuvd | 2:c1f9f9d74f35 | 27 | Motor::Motor(PinName r1, PinName r4, MCtrlType mctype): |
prabhuvd | 2:c1f9f9d74f35 | 28 | _r1(r1), _r4(r4),_mctype(mctype) { |
prabhuvd | 2:c1f9f9d74f35 | 29 | |
prabhuvd | 2:c1f9f9d74f35 | 30 | _r1=0 ;_r4=0; |
prabhuvd | 2:c1f9f9d74f35 | 31 | |
prabhuvd | 2:c1f9f9d74f35 | 32 | } |
prabhuvd | 0:155daae8a9fa | 33 | |
prabhuvd | 0:155daae8a9fa | 34 | void Motor::coast(void) |
prabhuvd | 0:155daae8a9fa | 35 | { |
prabhuvd | 2:c1f9f9d74f35 | 36 | if((_mctype == L293D_DISCRETE) || (_mctype == L293D_PWM)){ |
prabhuvd | 2:c1f9f9d74f35 | 37 | _r1=1 ;_r4=1; |
prabhuvd | 2:c1f9f9d74f35 | 38 | } |
prabhuvd | 0:155daae8a9fa | 39 | } |
prabhuvd | 2:c1f9f9d74f35 | 40 | |
prabhuvd | 0:155daae8a9fa | 41 | void Motor::forward(void) |
prabhuvd | 0:155daae8a9fa | 42 | { |
prabhuvd | 2:c1f9f9d74f35 | 43 | if(_mctype == L293D_PWM){ |
prabhuvd | 2:c1f9f9d74f35 | 44 | _r1=0 ; |
prabhuvd | 2:c1f9f9d74f35 | 45 | for(float p = 0.0f; p < 1.0f; p += 0.1f) { |
prabhuvd | 2:c1f9f9d74f35 | 46 | _r4 = p; |
prabhuvd | 2:c1f9f9d74f35 | 47 | wait(0.1); |
prabhuvd | 2:c1f9f9d74f35 | 48 | } |
prabhuvd | 2:c1f9f9d74f35 | 49 | }else if (_mctype == L293D_DISCRETE) { |
prabhuvd | 2:c1f9f9d74f35 | 50 | _r1=0 ;_r4 = 1; |
prabhuvd | 2:c1f9f9d74f35 | 51 | } |
prabhuvd | 2:c1f9f9d74f35 | 52 | |
prabhuvd | 2:c1f9f9d74f35 | 53 | } |
prabhuvd | 2:c1f9f9d74f35 | 54 | void Motor::backward(void) |
prabhuvd | 2:c1f9f9d74f35 | 55 | { |
prabhuvd | 2:c1f9f9d74f35 | 56 | if (_mctype == L293D_PWM){ |
prabhuvd | 2:c1f9f9d74f35 | 57 | _r4=0 ; |
prabhuvd | 2:c1f9f9d74f35 | 58 | for(float p = 0.0f; p < 1.0f; p += 0.1f) { |
prabhuvd | 2:c1f9f9d74f35 | 59 | _r1 = p; |
prabhuvd | 2:c1f9f9d74f35 | 60 | wait(0.1); |
prabhuvd | 2:c1f9f9d74f35 | 61 | } |
prabhuvd | 2:c1f9f9d74f35 | 62 | }else if (_mctype == L293D_DISCRETE){ |
prabhuvd | 2:c1f9f9d74f35 | 63 | _r4=0 ;_r1 =1; |
prabhuvd | 2:c1f9f9d74f35 | 64 | } |
prabhuvd | 2:c1f9f9d74f35 | 65 | } |
prabhuvd | 2:c1f9f9d74f35 | 66 | |
prabhuvd | 2:c1f9f9d74f35 | 67 | void Motor::stop(void) |
prabhuvd | 2:c1f9f9d74f35 | 68 | { |
prabhuvd | 2:c1f9f9d74f35 | 69 | _r1=1; _r4=1; |
prabhuvd | 2:c1f9f9d74f35 | 70 | } |
prabhuvd | 2:c1f9f9d74f35 | 71 | |
prabhuvd | 2:c1f9f9d74f35 | 72 | #else |
prabhuvd | 2:c1f9f9d74f35 | 73 | Motor::Motor(PinName r1, PinName r2, PinName r3, PinName r4, MCtrlType mctype): |
prabhuvd | 2:c1f9f9d74f35 | 74 | _r1(r1), _r2(r2), _r3(r3), _r4(r4),_mctype(mctype) { |
prabhuvd | 2:c1f9f9d74f35 | 75 | _r1=1 ; _r2=1 ;_r3=1 ;_r4=1; |
prabhuvd | 2:c1f9f9d74f35 | 76 | } |
prabhuvd | 2:c1f9f9d74f35 | 77 | |
prabhuvd | 2:c1f9f9d74f35 | 78 | void Motor::coast(void) |
prabhuvd | 2:c1f9f9d74f35 | 79 | { |
prabhuvd | 2:c1f9f9d74f35 | 80 | _r1=0 ; _r2=1 ;_r3=0;_r4=1; |
prabhuvd | 2:c1f9f9d74f35 | 81 | } |
prabhuvd | 2:c1f9f9d74f35 | 82 | |
prabhuvd | 2:c1f9f9d74f35 | 83 | void Motor::forward(void) |
prabhuvd | 2:c1f9f9d74f35 | 84 | { |
prabhuvd | 2:c1f9f9d74f35 | 85 | _r1=0 ; _r2=0 ;_r3=1 ;_r4=1; |
prabhuvd | 0:155daae8a9fa | 86 | |
prabhuvd | 0:155daae8a9fa | 87 | } |
prabhuvd | 2:c1f9f9d74f35 | 88 | |
prabhuvd | 0:155daae8a9fa | 89 | void Motor::backward(void) |
prabhuvd | 2:c1f9f9d74f35 | 90 | { |
prabhuvd | 0:155daae8a9fa | 91 | _r1=1 ; _r2=1 ;_r3=0 ;_r4=0; |
prabhuvd | 0:155daae8a9fa | 92 | } |
prabhuvd | 0:155daae8a9fa | 93 | |
prabhuvd | 0:155daae8a9fa | 94 | void Motor::stop(void) |
prabhuvd | 0:155daae8a9fa | 95 | { |
prabhuvd | 0:155daae8a9fa | 96 | _r1=1 ; _r2=1 ;_r3=1 ;_r4=1; |
prabhuvd | 2:c1f9f9d74f35 | 97 | |
prabhuvd | 0:155daae8a9fa | 98 | } |
prabhuvd | 0:155daae8a9fa | 99 | |
prabhuvd | 2:c1f9f9d74f35 | 100 | #endif |
prabhuvd | 2:c1f9f9d74f35 | 101 | |
prabhuvd | 0:155daae8a9fa | 102 | /* |
prabhuvd | 0:155daae8a9fa | 103 | //Test code for driving h-Bridge |
prabhuvd | 0:155daae8a9fa | 104 | // Assume there are two motors connected to a chasis |
prabhuvd | 0:155daae8a9fa | 105 | // a right and a left motor. |
prabhuvd | 0:155daae8a9fa | 106 | |
prabhuvd | 0:155daae8a9fa | 107 | |
prabhuvd | 0:155daae8a9fa | 108 | Motor left_motor(LED1,LED2,LED3,LED4); |
prabhuvd | 0:155daae8a9fa | 109 | Motor right_motor(LED1,LED2,LED3,LED4); |
prabhuvd | 0:155daae8a9fa | 110 | |
prabhuvd | 0:155daae8a9fa | 111 | void main(){ |
prabhuvd | 0:155daae8a9fa | 112 | //To drive the robot forward |
prabhuvd | 0:155daae8a9fa | 113 | left_motor.forward(); |
prabhuvd | 0:155daae8a9fa | 114 | right_motor.backward(); |
prabhuvd | 0:155daae8a9fa | 115 | |
prabhuvd | 0:155daae8a9fa | 116 | // To drive the robot backward |
prabhuvd | 0:155daae8a9fa | 117 | left_motor.backward(); |
prabhuvd | 0:155daae8a9fa | 118 | right_motor.forward(); |
prabhuvd | 0:155daae8a9fa | 119 | |
prabhuvd | 0:155daae8a9fa | 120 | // To turn the robot LEFT |
prabhuvd | 0:155daae8a9fa | 121 | left_motor.stop(); |
prabhuvd | 0:155daae8a9fa | 122 | right_motor.forward(); |
prabhuvd | 0:155daae8a9fa | 123 | |
prabhuvd | 0:155daae8a9fa | 124 | // To turn the robot RIGHT |
prabhuvd | 0:155daae8a9fa | 125 | left_motor.forward(); |
prabhuvd | 0:155daae8a9fa | 126 | right_motor.stop(); |
prabhuvd | 0:155daae8a9fa | 127 | |
prabhuvd | 0:155daae8a9fa | 128 | } |
prabhuvd | 0:155daae8a9fa | 129 | |
prabhuvd | 0:155daae8a9fa | 130 | */ |