Dependents:   YMotor

Revision:
0:a2bbf76ca734
Child:
1:c8ed08beefb9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/YMotorDriverBase.h	Sun Aug 23 15:18:04 2015 +0000
@@ -0,0 +1,79 @@
+#ifndef INCLUDED_YMOTOR_DRIVER_BASE_H
+#define INCLUDED_YMOTOR_DRIVER_BASE_H
+
+#include "mbed.h"
+#include "PWMOut.h"
+
+class YMotorDriverBase{
+public:
+    enum MotorAction{
+        FORWARD,
+        REVERSE,
+        BRAKE,
+        RELEASE
+    };
+
+    YMotorDriverBase( char address );
+    ~YMotorDriverBase();
+    
+    virtual void update();
+    
+    void setMotorAction( MotorAction action ){
+        mAction = action;
+    }
+    void setDuty( float d ){
+        mDuty = middle( mMinDuty, d, mMaxDuty );
+    }
+    void setPercent( float p );
+    
+    float getMinDuty(){
+        return mMinDuty;
+    }
+    float getMaxDuty(){
+        return mMaxDuty;
+    }
+    
+protected:
+    void write();
+    
+    I2CSlave* mI2C;
+    DigitalOut* mLED;
+
+private:
+    PWMOut* mMotorDrivePwm;
+    DigitalOut* mMotorDriveDout[ 4 ];
+    
+    MotorAction mAction;
+    float mDuty;
+    
+    const char mAddress;
+    
+    static const PinName mMotorDriveDoutPinName[ 4 ];
+    static const PinName mMotorDrivePwmPinName;
+    static const PinName mLEDPinName;
+    static const PinName mSerialPinName[ 2 ];
+    static const PinName mI2CPinName[ 2 ];
+    static const int mPwmCycle_us;
+    static const float mMaxDuty;
+    static const float mMinDuty;
+    
+    void updatePwmDuty();
+    
+    static float middle( float min, float d, float max ){
+        if ( min > max ){
+            float tmp = min;
+            min = max;
+            max = tmp;
+        }
+        
+        if ( d < min ){
+            return min;
+        } else if ( d > max ){
+            return max;
+        }
+        
+        return d;
+    }
+};
+
+#endif
\ No newline at end of file