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@13:414b463160ef, 2018-03-15 (annotated)
- Committer:
- mbedoguz
- Date:
- Thu Mar 15 20:51:20 2018 +0000
- Revision:
- 13:414b463160ef
- Parent:
- 12:fee8aa385836
- Child:
- 14:193c422498bf
a
Who changed what in which revision?
User | Revision | Line number | New 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; |
ianmcc | 1:5e8d9ed18f0f | 43 | AnalogIn CS; |
mbedoguz | 8:1458b4da7e56 | 44 | FastPWM 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 | 13:414b463160ef | 94 | return CS.read();//*15.7142; |
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 | { |
mbedoguz | 11:6902db5e7978 | 100 | return false; |
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 | } |
ianmcc | 5:b5f360a16354 | 107 | |
ianmcc | 5:b5f360a16354 | 108 | inline |
ianmcc | 5:b5f360a16354 | 109 | void VNH5019::enable() |
ianmcc | 5:b5f360a16354 | 110 | { |
ianmcc | 5:b5f360a16354 | 111 | } |