Library for the VNH5019 Motor Driver, with a helper class for the Pololu Dual VNH5019 Dual Motor Driver Shield http://www.pololu.com/product/2502

Dependents:   VNH5019_second VNH5019_second1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers VNH5019.h Source File

VNH5019.h

00001 #ifndef DualVNH5019MotorShield_h
00002 #define DualVNH5019MotorShield_h
00003 
00004 #include <mbed.h>
00005 
00006 class VNH5019
00007 {
00008     public:
00009         VNH5019(PinName INA_, PinName INB_, PinName ENDIAG_, PinName CS_, PinName PWM_);
00010         
00011         // set motor speed from -1.0 to +1.0
00012         void speed(float Speed);
00013 
00014         // stop (no current to the motors)
00015         void stop();        
00016     
00017         // Brake, with strength 0..1
00018         void brake(float Brake);
00019 
00020         // returns the current through the motor, in mA
00021         float get_current_mA();
00022 
00023         // returns true if there has been a fault
00024         bool is_fault();
00025 
00026         // Clears the fault condition
00027         // PRECONDITION: is_fault()
00028         void clear_fault();
00029 
00030         // disable the motor, and set outputs to zero.  This is a low power mode.
00031         void disable();
00032 
00033         // enable the motor.        
00034         void enable();
00035         
00036         // set the PWM period of oscillation in seconds
00037         void set_pwm_period(float p)
00038         { PWM.period(p); }
00039 
00040     private:
00041         void init();
00042 
00043         DigitalOut   INA;
00044         DigitalOut   INB;
00045         DigitalInOut ENDIAG;
00046         AnalogIn     CS;
00047         PwmOut       PWM;
00048 };        
00049 
00050 // Helper class for the Pololu dual VNH5019 motor shield.
00051 // The default constructor uses the default arduino pins.
00052 // The motors can be accessed either by .m1 or .m2, or by operator()(i) where i is 1 or 2.
00053 class DualVNH5019MotorShield
00054 {
00055    public:
00056       // default pin selection
00057       DualVNH5019MotorShield(); // Default pin selection.
00058 
00059                               // User-defined pin selection. 
00060       DualVNH5019MotorShield(PinName INA1_, PinName INB1_, PinName ENDIAG1_, PinName CS1_, PinName PWM1_,
00061                              PinName INA2_, PinName INB2_, PinName ENDIAG2_, PinName CS2_, PinName PWM2_);
00062     
00063       // returns the given motor object, 1 or 2.
00064       VNH5019& operator()(int m);
00065 
00066       VNH5019 m1;
00067       VNH5019 m2;
00068 };
00069 
00070 inline
00071 void VNH5019::stop()
00072 {
00073     INA = 0;
00074     INB = 0;
00075     PWM = 0.0;
00076 }
00077 
00078 inline
00079 void VNH5019::brake(float Brake)
00080 {
00081    // normalize Brake to 0..1
00082    if (Brake < 0)
00083       Brake = -Brake;
00084    if (Brake > 1.0)
00085       Brake = 1.0;
00086 
00087    INA = 0;
00088    INB = 0;
00089    PWM = Brake;
00090 }
00091 
00092 inline
00093 float VNH5019::get_current_mA()
00094 {
00095    // Scale is 144mV per A
00096    // Scale factor is 3.3 / 0.144 = 22.916667
00097    return CS.read() * 22.916667;
00098 }
00099 
00100 inline
00101 bool VNH5019::is_fault()
00102 {
00103   return !ENDIAG;
00104 }
00105 
00106 inline
00107 void VNH5019::disable()
00108 {
00109     ENDIAG.output();
00110     ENDIAG.write(0);
00111 }
00112 
00113 inline    
00114 void VNH5019::enable()
00115 {
00116     ENDIAG.input();
00117 }
00118 
00119 inline
00120 DualVNH5019MotorShield::DualVNH5019MotorShield()
00121 : m1(PTD4, PTA4, PTC8, PTB0, PTD5),
00122   m2(PTC9, PTA13, PTD3, PTB1, PTD0)
00123 {
00124 }
00125 
00126 inline
00127 DualVNH5019MotorShield::DualVNH5019MotorShield(PinName INA1_, PinName INB1_, PinName ENDIAG1_, PinName CS1_, PinName PWM1_,
00128                                                PinName INA2_, PinName INB2_, PinName ENDIAG2_, PinName CS2_, PinName PWM2_)
00129  : m1(INA1_, INB1_, ENDIAG1_, CS1_, PWM1_),
00130    m2(INA2_, INB2_, ENDIAG2_, CS2_, PWM2_)
00131 {
00132 }
00133 
00134 inline
00135 VNH5019& 
00136 DualVNH5019MotorShield::operator()(int m)
00137 {
00138     return m == 1 ? m1 : m2;
00139 }
00140 
00141 
00142 #endif