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

Revision:
3:118c5f0675da
Parent:
2:f265e441bcd9
--- 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;
+}
+