Control an H-Bridge using a PwmOut (enable) and two DigitalOuts (direction select). (forked from simon/Motor and changed according to our needs)

Dependents:   robots

Fork of Motor by Simon Ford

Files at this revision

API Documentation at this revision

Comitter:
narendraj9
Date:
Sun Oct 27 09:15:12 2013 +0000
Parent:
2:f265e441bcd9
Commit message:
initial commit

Changed in this revision

Motor.cpp Show annotated file Show diff for this revision Revisions of this file
Motor.h Show annotated file Show diff for this revision Revisions of this file
diff -r f265e441bcd9 -r 118c5f0675da Motor.cpp
--- a/Motor.cpp	Tue Nov 23 16:16:43 2010 +0000
+++ b/Motor.cpp	Sun Oct 27 09:15:12 2013 +0000
@@ -24,23 +24,46 @@
 
 #include "mbed.h"
 
-Motor::Motor(PinName pwm, PinName fwd, PinName rev):
-        _pwm(pwm), _fwd(fwd), _rev(rev) {
+Motor::Motor(PinName en, PinName fwd, PinName rev):
+        _en(en), _fwd(fwd), _rev(rev) {
 
-    // Set initial condition of PWM
-    _pwm.period(0.001);
-    _pwm = 0;
-
+    // Set initial condition for the enable pin
+    _en = 0;
+    
     // Initial condition of output enables
     _fwd = 0;
     _rev = 0;
 }
 
-void Motor::speed(float speed) {
-    _fwd = (speed > 0.0);
-    _rev = (speed < 0.0);
-    _pwm = abs(speed);
+// direct the motor in a specific direction or stop it
+void Motor::direct(int dir) {
+    _fwd = (dir > 0);
+    _rev = (dir < 0);
+    _en = !(dir == 0);
 }
 
+/* emulate pwm for a specified time interval */
+void Motor::speed(float timeval, float speed) {
+    float wait_time = 1/(speed * 10);
+    for (int i = 0; i < timeval; i += 0.1 ) {
+        _en = 0;
+        wait(wait_time/2);
+        _en = 1;
+        wait(wait_time);
+    }
 
+}
 
+mState Motor::getState()
+{
+    mState temp = { _en, _fwd, _rev };
+    return temp;
+}
+
+void Motor::setState(mState pinState) 
+{
+    _en = pinState.en;
+    _fwd = pinState.fwd;
+    _rev = pinState.rev;
+}
+
diff -r f265e441bcd9 -r 118c5f0675da Motor.h
--- a/Motor.h	Tue Nov 23 16:16:43 2010 +0000
+++ b/Motor.h	Sun Oct 27 09:15:12 2013 +0000
@@ -25,6 +25,14 @@
 
 #include "mbed.h"
 
+
+
+struct mState {
+    bool en;
+    bool fwd;
+    bool rev;
+};
+
 /** Interface to control a standard DC motor 
  *
  * with an H-bridge using a PwmOut and 2 DigitalOuts
@@ -38,16 +46,19 @@
      * @param fwd A DigitalOut, set high when the motor should go forward
      * @param rev A DigitalOut, set high when the motor should go backwards
      */
-    Motor(PinName pwm, PinName fwd, PinName rev);
+    Motor(PinName en, PinName fwd, PinName rev);
     
     /** Set the speed of the motor
      * 
      * @param speed The speed of the motor as a normalised value between -1.0 and 1.0
      */
-    void speed(float speed);
+    void direct(int dir);
+    void speed(float timeval, float speed);
+    mState getState();
+    void setState(mState);
 
 protected:
-    PwmOut _pwm;
+    DigitalOut _en;
     DigitalOut _fwd;
     DigitalOut _rev;