YMotorDriverBase.h@0:a2bbf76ca734, 2015-08-23 (annotated)
- Committer:
- inst
- Date:
- Sun Aug 23 15:18:04 2015 +0000
- Revision:
- 0:a2bbf76ca734
- Child:
- 1:c8ed08beefb9
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 0:a2bbf76ca734 | 1 | #ifndef INCLUDED_YMOTOR_DRIVER_BASE_H |
inst | 0:a2bbf76ca734 | 2 | #define INCLUDED_YMOTOR_DRIVER_BASE_H |
inst | 0:a2bbf76ca734 | 3 | |
inst | 0:a2bbf76ca734 | 4 | #include "mbed.h" |
inst | 0:a2bbf76ca734 | 5 | #include "PWMOut.h" |
inst | 0:a2bbf76ca734 | 6 | |
inst | 0:a2bbf76ca734 | 7 | class YMotorDriverBase{ |
inst | 0:a2bbf76ca734 | 8 | public: |
inst | 0:a2bbf76ca734 | 9 | enum MotorAction{ |
inst | 0:a2bbf76ca734 | 10 | FORWARD, |
inst | 0:a2bbf76ca734 | 11 | REVERSE, |
inst | 0:a2bbf76ca734 | 12 | BRAKE, |
inst | 0:a2bbf76ca734 | 13 | RELEASE |
inst | 0:a2bbf76ca734 | 14 | }; |
inst | 0:a2bbf76ca734 | 15 | |
inst | 0:a2bbf76ca734 | 16 | YMotorDriverBase( char address ); |
inst | 0:a2bbf76ca734 | 17 | ~YMotorDriverBase(); |
inst | 0:a2bbf76ca734 | 18 | |
inst | 0:a2bbf76ca734 | 19 | virtual void update(); |
inst | 0:a2bbf76ca734 | 20 | |
inst | 0:a2bbf76ca734 | 21 | void setMotorAction( MotorAction action ){ |
inst | 0:a2bbf76ca734 | 22 | mAction = action; |
inst | 0:a2bbf76ca734 | 23 | } |
inst | 0:a2bbf76ca734 | 24 | void setDuty( float d ){ |
inst | 0:a2bbf76ca734 | 25 | mDuty = middle( mMinDuty, d, mMaxDuty ); |
inst | 0:a2bbf76ca734 | 26 | } |
inst | 0:a2bbf76ca734 | 27 | void setPercent( float p ); |
inst | 0:a2bbf76ca734 | 28 | |
inst | 0:a2bbf76ca734 | 29 | float getMinDuty(){ |
inst | 0:a2bbf76ca734 | 30 | return mMinDuty; |
inst | 0:a2bbf76ca734 | 31 | } |
inst | 0:a2bbf76ca734 | 32 | float getMaxDuty(){ |
inst | 0:a2bbf76ca734 | 33 | return mMaxDuty; |
inst | 0:a2bbf76ca734 | 34 | } |
inst | 0:a2bbf76ca734 | 35 | |
inst | 0:a2bbf76ca734 | 36 | protected: |
inst | 0:a2bbf76ca734 | 37 | void write(); |
inst | 0:a2bbf76ca734 | 38 | |
inst | 0:a2bbf76ca734 | 39 | I2CSlave* mI2C; |
inst | 0:a2bbf76ca734 | 40 | DigitalOut* mLED; |
inst | 0:a2bbf76ca734 | 41 | |
inst | 0:a2bbf76ca734 | 42 | private: |
inst | 0:a2bbf76ca734 | 43 | PWMOut* mMotorDrivePwm; |
inst | 0:a2bbf76ca734 | 44 | DigitalOut* mMotorDriveDout[ 4 ]; |
inst | 0:a2bbf76ca734 | 45 | |
inst | 0:a2bbf76ca734 | 46 | MotorAction mAction; |
inst | 0:a2bbf76ca734 | 47 | float mDuty; |
inst | 0:a2bbf76ca734 | 48 | |
inst | 0:a2bbf76ca734 | 49 | const char mAddress; |
inst | 0:a2bbf76ca734 | 50 | |
inst | 0:a2bbf76ca734 | 51 | static const PinName mMotorDriveDoutPinName[ 4 ]; |
inst | 0:a2bbf76ca734 | 52 | static const PinName mMotorDrivePwmPinName; |
inst | 0:a2bbf76ca734 | 53 | static const PinName mLEDPinName; |
inst | 0:a2bbf76ca734 | 54 | static const PinName mSerialPinName[ 2 ]; |
inst | 0:a2bbf76ca734 | 55 | static const PinName mI2CPinName[ 2 ]; |
inst | 0:a2bbf76ca734 | 56 | static const int mPwmCycle_us; |
inst | 0:a2bbf76ca734 | 57 | static const float mMaxDuty; |
inst | 0:a2bbf76ca734 | 58 | static const float mMinDuty; |
inst | 0:a2bbf76ca734 | 59 | |
inst | 0:a2bbf76ca734 | 60 | void updatePwmDuty(); |
inst | 0:a2bbf76ca734 | 61 | |
inst | 0:a2bbf76ca734 | 62 | static float middle( float min, float d, float max ){ |
inst | 0:a2bbf76ca734 | 63 | if ( min > max ){ |
inst | 0:a2bbf76ca734 | 64 | float tmp = min; |
inst | 0:a2bbf76ca734 | 65 | min = max; |
inst | 0:a2bbf76ca734 | 66 | max = tmp; |
inst | 0:a2bbf76ca734 | 67 | } |
inst | 0:a2bbf76ca734 | 68 | |
inst | 0:a2bbf76ca734 | 69 | if ( d < min ){ |
inst | 0:a2bbf76ca734 | 70 | return min; |
inst | 0:a2bbf76ca734 | 71 | } else if ( d > max ){ |
inst | 0:a2bbf76ca734 | 72 | return max; |
inst | 0:a2bbf76ca734 | 73 | } |
inst | 0:a2bbf76ca734 | 74 | |
inst | 0:a2bbf76ca734 | 75 | return d; |
inst | 0:a2bbf76ca734 | 76 | } |
inst | 0:a2bbf76ca734 | 77 | }; |
inst | 0:a2bbf76ca734 | 78 | |
inst | 0:a2bbf76ca734 | 79 | #endif |