Dependents:   YMotor

YMotorDriverBase.h

Committer:
inst
Date:
2015-08-23
Revision:
0:a2bbf76ca734
Child:
1:c8ed08beefb9

File content as of revision 0:a2bbf76ca734:

#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