Default mbed pwm doesn't have enough resolution at high frequencies, thats why I implemented VNH5019 Motor carrier with FastPWM.
Fork of VNH5019 by
Diff: VNH5019.h
- Revision:
- 1:5e8d9ed18f0f
- Parent:
- 0:5d3ab0ea7f27
- Child:
- 2:d670a4b999ab
diff -r 5d3ab0ea7f27 -r 5e8d9ed18f0f VNH5019.h --- a/VNH5019.h Sat Feb 01 13:48:28 2014 +0000 +++ b/VNH5019.h Sat Feb 01 14:46:45 2014 +0000 @@ -3,6 +3,49 @@ #include <mbed.h> +class VNH5019 +{ + public: + VNH5019(PinName INA_, PinName INB_, PinName ENDIAG_, PinName CS_, PinName PWM_); + + // set motor speed from -1.0 to +1.0 + void speed(float Speed); + + // stop (no current to the motors) + void stop(); + + // Brake, with strength 0..1 + void brake(float Brake); + + // returns the current through the motor, in mA + float get_current_mA(); + + // returns true if there has been a fault + bool is_fault(); + + // Clears the fault condition + // PRECONDITION: is_fault() + void clear_fault(); + + // disable the motor, and set outputs to zero. This is a low power mode. + void disable(); + + // enable the motor. + void enable(); + + private: + void init(); // Initialize TIMER 1, set the PWM to 20kHZ. + + DigitalOut INA; + DigitalOut INB; + DigitalInOut ENDIAG; + AnalogIn CS; + PwmOut PWM; +}; + +// Helper class for the Pololu dual VNH5019 motor shield. +// The default constructor uses the default arduino pins. +// The motors can be accessed either by .m1 or .m2, or by operator()(i) where i is 1 or 2. class DualVNH5019MotorShield { public: @@ -13,54 +56,11 @@ DualVNH5019MotorShield(PinName INA1_, PinName INB1_, PinName ENDIAG1_, PinName CS1_, PinName PWM1_, PinName INA2_, PinName INB2_, PinName ENDIAG2_, PinName CS2_, PinName PWM2_); - // Set motor speeds, from -1.0 to +1.0 - void setM1Speed(float Speed); - void setM2Speed(float Speed); - void setSpeeds(float m1Speed, float m2Speed); - - // stop (no current to the motors) - void setM1Stop(); - void setM2Stop(); - - // brake, with strength 0..1 - void setM1Brake(float Brake); - void setM2Brake(float Brake); - void setBrakes(float m1Brake, float m2Brake); - - // return the current supplied to the motors - float getM1CurrentMA(); - float getM2CurrentMA(); - - // fault - bool isM1Fault(); - bool isM2Fault(); + // returns the given motor object, 1 or 2. + VNH5019& operator()(int m); - // If a fault occurs, then we follow the procedure from the data sheet to clear it - void clearM1Fault(); - void clearM2Fault(); - - // disable / enable (isFault() will return true if the motor is disabled). - // Disabling the motor also sets the speed to 0 and brakes to LOW. - void disableM1(); - void disableM2(); - - void enableM1(bool Enable = true); - void enableM2(bool Enable = true); - - private: - void init(); // Initialize TIMER 1, set the PWM to 20kHZ. - - DigitalOut INA1; - DigitalOut INB1; - DigitalInOut ENDIAG1; - AnalogIn CS1; - PwmOut PWM1; - - DigitalOut INA2; - DigitalOut INB2; - DigitalInOut ENDIAG2; - AnalogIn CS2; - PwmOut PWM2; + VNH5019 m1; + VNH5019 m2; }; #endif