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