Library for H-Bridge Motor Driver Using Bipolar Transistors

Files at this revision

API Documentation at this revision

Comitter:
prabhuvd
Date:
Sat Jan 26 15:00:23 2013 +0000
Parent:
1:b690c5eb9469
Commit message:
LM293D and PWM based drive support added.

Changed in this revision

motorlib.cpp Show annotated file Show diff for this revision Revisions of this file
motorlib.h Show annotated file Show diff for this revision Revisions of this file
diff -r b690c5eb9469 -r c1f9f9d74f35 motorlib.cpp
--- 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 
diff -r b690c5eb9469 -r c1f9f9d74f35 motorlib.h
--- a/motorlib.h	Tue Jan 15 13:33:21 2013 +0000
+++ b/motorlib.h	Sat Jan 26 15:00:23 2013 +0000
@@ -11,11 +11,20 @@
  
 #include "mbed.h"
  
+#define LM293D_MOTOR_CTRL 
 /** Interface to control a standard DC motor 
 * 
 */
 class Motor {
     public:
+    /** Motor Control Type */
+    enum MCtrlType {
+          L293D_DISCRETE   /**< 0 or 100% duty cycle with  L293D */
+        , L293D_PWM        /**< PWM based voltage control  with L293D */
+        , COMPS_DISCRETE   /**< 0 or 100% duty cycle with transistor circuit*/
+        , COMPS_PWM        /**< PWM based voltage control with transistor circuit*/       
+
+    };    
  
 /** Create a motor control interface    
 *
@@ -23,8 +32,10 @@
 * @param r2 DigitalOut, driving  H-bridge PNP transistor on the TOP right
 * @param r3 DigitalOut, driving  H-bridge NPN transistor on the BOTTOm left.
 * @param r4 DigitalOut, driving  H-bridge PNP transistor on the TOP left
+* @param mctype DigitalOut, driving  H-bridge PNP transistor on the TOP left
 */
-        Motor(PinName r1, PinName r2, PinName r3, PinName r4);
+       Motor(PinName r1, PinName r4,MCtrlType mctype =L293D_PWM);
+       Motor(PinName r1, PinName r2, PinName r3, PinName r4,MCtrlType mctype =L293D_PWM);
   
 /** Set the motor to coast/roll/off
 * 
@@ -57,10 +68,16 @@
         void stop(void);
    
     protected:
+      #ifdef LM293D_MOTOR_CTRL
+        PwmOut _r1;
+        PwmOut _r4;  
+      #else 
         DigitalOut _r1;
         DigitalOut _r2;
         DigitalOut _r3;
-        DigitalOut _r4;
+        DigitalOut _r4;      
+      #endif   
+        MCtrlType _mctype;
  
 };