Library for H-Bridge Motor Driver Using Bipolar Transistors
Diff: motorlib.cpp
- Revision:
- 2:c1f9f9d74f35
- Parent:
- 0:155daae8a9fa
--- a/motorlib.cpp Tue Jan 15 13:33:21 2013 +0000 +++ b/motorlib.cpp Sat Jan 26 15:00:23 2013 +0000 @@ -23,35 +23,82 @@ #include "motorlib.h" #include "mbed.h" -Motor::Motor(PinName r1, PinName r2, PinName r3, PinName r4): - _r1(r1), _r2(r2), _r3(r3), _r4(r4) { +#ifdef LM293D_MOTOR_CTRL +Motor::Motor(PinName r1, PinName r4, MCtrlType mctype): + _r1(r1), _r4(r4),_mctype(mctype) { + + _r1=0 ;_r4=0; + +} -// Set initial condition for port outputs - _r1=1 ; _r2=1 ;_r3=1 ;_r4=1; - -} void Motor::coast(void) { - _r1=0 ; _r2=1 ;_r3=0;_r4=1; - - + if((_mctype == L293D_DISCRETE) || (_mctype == L293D_PWM)){ + _r1=1 ;_r4=1; + } } + void Motor::forward(void) { - _r1=0 ; _r2=0 ;_r3=1 ;_r4=1; + if(_mctype == L293D_PWM){ + _r1=0 ; + for(float p = 0.0f; p < 1.0f; p += 0.1f) { + _r4 = p; + wait(0.1); + } + }else if (_mctype == L293D_DISCRETE) { + _r1=0 ;_r4 = 1; + } + +} +void Motor::backward(void) +{ + if (_mctype == L293D_PWM){ + _r4=0 ; + for(float p = 0.0f; p < 1.0f; p += 0.1f) { + _r1 = p; + wait(0.1); + } + }else if (_mctype == L293D_DISCRETE){ + _r4=0 ;_r1 =1; + } +} + +void Motor::stop(void) +{ + _r1=1; _r4=1; +} + +#else +Motor::Motor(PinName r1, PinName r2, PinName r3, PinName r4, MCtrlType mctype): + _r1(r1), _r2(r2), _r3(r3), _r4(r4),_mctype(mctype) { + _r1=1 ; _r2=1 ;_r3=1 ;_r4=1; +} + +void Motor::coast(void) +{ + _r1=0 ; _r2=1 ;_r3=0;_r4=1; +} + +void Motor::forward(void) +{ + _r1=0 ; _r2=0 ;_r3=1 ;_r4=1; } -// (additions) + void Motor::backward(void) -{ +{ _r1=1 ; _r2=1 ;_r3=0 ;_r4=0; } void Motor::stop(void) { _r1=1 ; _r2=1 ;_r3=1 ;_r4=1; + } +#endif + /* //Test code for driving h-Bridge // Assume there are two motors connected to a chasis