Default mbed pwm doesn't have enough resolution at high frequencies, thats why I implemented VNH5019 Motor carrier with FastPWM.

Dependencies:   FastPWM

Fork of VNH5019 by IEEE RAS METU

Committer:
mbedoguz
Date:
Thu Nov 30 17:42:16 2017 +0000
Revision:
10:829d36e2de02
Parent:
9:1709d9b034f7
Child:
11:6902db5e7978
Compatibility changes are made.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ianmcc 0:5d3ab0ea7f27 1 #include <mbed.h>
mbedoguz 8:1458b4da7e56 2 #include <FastPWM.h>
ianmcc 0:5d3ab0ea7f27 3
ianmcc 1:5e8d9ed18f0f 4 class VNH5019
ianmcc 1:5e8d9ed18f0f 5 {
ianmcc 1:5e8d9ed18f0f 6 public:
ianmcc 1:5e8d9ed18f0f 7 VNH5019(PinName INA_, PinName INB_, PinName ENDIAG_, PinName CS_, PinName PWM_);
ianmcc 1:5e8d9ed18f0f 8
ianmcc 1:5e8d9ed18f0f 9 // set motor speed from -1.0 to +1.0
mbedoguz 8:1458b4da7e56 10 void speed(double Speed);
ianmcc 1:5e8d9ed18f0f 11
ianmcc 1:5e8d9ed18f0f 12 // stop (no current to the motors)
ianmcc 1:5e8d9ed18f0f 13 void stop();
ianmcc 1:5e8d9ed18f0f 14
ianmcc 1:5e8d9ed18f0f 15 // Brake, with strength 0..1
ianmcc 1:5e8d9ed18f0f 16 void brake(float Brake);
ianmcc 1:5e8d9ed18f0f 17
ianmcc 1:5e8d9ed18f0f 18 // returns the current through the motor, in mA
ianmcc 1:5e8d9ed18f0f 19 float get_current_mA();
ianmcc 1:5e8d9ed18f0f 20
ianmcc 1:5e8d9ed18f0f 21 // returns true if there has been a fault
ianmcc 1:5e8d9ed18f0f 22 bool is_fault();
ianmcc 1:5e8d9ed18f0f 23
ianmcc 1:5e8d9ed18f0f 24 // Clears the fault condition
ianmcc 1:5e8d9ed18f0f 25 // PRECONDITION: is_fault()
ianmcc 1:5e8d9ed18f0f 26 void clear_fault();
ianmcc 1:5e8d9ed18f0f 27
ianmcc 1:5e8d9ed18f0f 28 // disable the motor, and set outputs to zero. This is a low power mode.
ianmcc 1:5e8d9ed18f0f 29 void disable();
ianmcc 1:5e8d9ed18f0f 30
ianmcc 1:5e8d9ed18f0f 31 // enable the motor.
ianmcc 1:5e8d9ed18f0f 32 void enable();
ianmcc 2:d670a4b999ab 33
ianmcc 2:d670a4b999ab 34 // set the PWM period of oscillation in seconds
mbedoguz 8:1458b4da7e56 35 void set_pwm_period(int p)
mbedoguz 8:1458b4da7e56 36 { PWM.period_us(p); }
ianmcc 1:5e8d9ed18f0f 37
ianmcc 1:5e8d9ed18f0f 38 private:
ianmcc 2:d670a4b999ab 39 void init();
ianmcc 1:5e8d9ed18f0f 40
ianmcc 1:5e8d9ed18f0f 41 DigitalOut INA;
ianmcc 1:5e8d9ed18f0f 42 DigitalOut INB;
mbedoguz 10:829d36e2de02 43 DigitalInOut ENDIAG;
ianmcc 1:5e8d9ed18f0f 44 AnalogIn CS;
mbedoguz 8:1458b4da7e56 45 FastPWM PWM;
ianmcc 1:5e8d9ed18f0f 46 };
ianmcc 1:5e8d9ed18f0f 47
ianmcc 1:5e8d9ed18f0f 48 // Helper class for the Pololu dual VNH5019 motor shield.
ianmcc 1:5e8d9ed18f0f 49 // The default constructor uses the default arduino pins.
ianmcc 1:5e8d9ed18f0f 50 // The motors can be accessed either by .m1 or .m2, or by operator()(i) where i is 1 or 2.
ianmcc 0:5d3ab0ea7f27 51 class DualVNH5019MotorShield
ianmcc 0:5d3ab0ea7f27 52 {
ianmcc 0:5d3ab0ea7f27 53 public:
ianmcc 0:5d3ab0ea7f27 54 // default pin selection
ianmcc 0:5d3ab0ea7f27 55 DualVNH5019MotorShield(); // Default pin selection.
ianmcc 0:5d3ab0ea7f27 56
ianmcc 0:5d3ab0ea7f27 57 // User-defined pin selection.
ianmcc 0:5d3ab0ea7f27 58 DualVNH5019MotorShield(PinName INA1_, PinName INB1_, PinName ENDIAG1_, PinName CS1_, PinName PWM1_,
ianmcc 0:5d3ab0ea7f27 59 PinName INA2_, PinName INB2_, PinName ENDIAG2_, PinName CS2_, PinName PWM2_);
ianmcc 0:5d3ab0ea7f27 60
ianmcc 1:5e8d9ed18f0f 61 // returns the given motor object, 1 or 2.
ianmcc 1:5e8d9ed18f0f 62 VNH5019& operator()(int m);
ianmcc 0:5d3ab0ea7f27 63
ianmcc 1:5e8d9ed18f0f 64 VNH5019 m1;
ianmcc 1:5e8d9ed18f0f 65 VNH5019 m2;
ianmcc 0:5d3ab0ea7f27 66 };
ianmcc 0:5d3ab0ea7f27 67
ianmcc 5:b5f360a16354 68 inline
ianmcc 5:b5f360a16354 69 void VNH5019::stop()
ianmcc 5:b5f360a16354 70 {
ianmcc 5:b5f360a16354 71 INA = 0;
ianmcc 5:b5f360a16354 72 INB = 0;
ianmcc 5:b5f360a16354 73 PWM = 0.0;
ianmcc 5:b5f360a16354 74 }
ianmcc 5:b5f360a16354 75
ianmcc 5:b5f360a16354 76 inline
ianmcc 5:b5f360a16354 77 void VNH5019::brake(float Brake)
ianmcc 5:b5f360a16354 78 {
ianmcc 5:b5f360a16354 79 // normalize Brake to 0..1
ianmcc 5:b5f360a16354 80 if (Brake < 0)
ianmcc 5:b5f360a16354 81 Brake = -Brake;
ianmcc 5:b5f360a16354 82 if (Brake > 1.0)
ianmcc 5:b5f360a16354 83 Brake = 1.0;
ianmcc 5:b5f360a16354 84
ianmcc 5:b5f360a16354 85 INA = 0;
ianmcc 5:b5f360a16354 86 INB = 0;
ianmcc 5:b5f360a16354 87 PWM = Brake;
ianmcc 5:b5f360a16354 88 }
ianmcc 5:b5f360a16354 89
ianmcc 5:b5f360a16354 90 inline
ianmcc 5:b5f360a16354 91 float VNH5019::get_current_mA()
ianmcc 5:b5f360a16354 92 {
mbedoguz 7:76af111bfff3 93 // Scale is 210mV per A
mbedoguz 7:76af111bfff3 94 // Scale factor is 3.3 / 0.210 = 15.7142
mbedoguz 7:76af111bfff3 95 return CS.read() * 15714;
ianmcc 5:b5f360a16354 96 }
ianmcc 5:b5f360a16354 97
ianmcc 5:b5f360a16354 98 inline
ianmcc 5:b5f360a16354 99 bool VNH5019::is_fault()
ianmcc 5:b5f360a16354 100 {
mbedoguz 10:829d36e2de02 101 return !ENDIAG;
ianmcc 5:b5f360a16354 102 }
ianmcc 5:b5f360a16354 103
ianmcc 5:b5f360a16354 104 inline
ianmcc 5:b5f360a16354 105 void VNH5019::disable()
ianmcc 5:b5f360a16354 106 {
mbedoguz 10:829d36e2de02 107 ENDIAG.output();
mbedoguz 10:829d36e2de02 108 ENDIAG.write(0);
ianmcc 5:b5f360a16354 109 }
ianmcc 5:b5f360a16354 110
ianmcc 5:b5f360a16354 111 inline
ianmcc 5:b5f360a16354 112 void VNH5019::enable()
ianmcc 5:b5f360a16354 113 {
mbedoguz 10:829d36e2de02 114 ENDIAG.input();
ianmcc 5:b5f360a16354 115 }