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.cpp@11:6902db5e7978, 2017-12-07 (annotated)
- Committer:
- mbedoguz
- Date:
- Thu Dec 07 15:32:22 2017 +0000
- Revision:
- 11:6902db5e7978
- Parent:
- 10:829d36e2de02
ENDIAG is disabled for compatibility
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ianmcc | 1:5e8d9ed18f0f | 1 | #include "VNH5019.h" |
ianmcc | 0:5d3ab0ea7f27 | 2 | |
ianmcc | 1:5e8d9ed18f0f | 3 | VNH5019::VNH5019(PinName INA_, PinName INB_, PinName ENDIAG_, PinName CS_, PinName PWM_) |
ianmcc | 1:5e8d9ed18f0f | 4 | : INA(INA_), |
ianmcc | 1:5e8d9ed18f0f | 5 | INB(INB_), |
ianmcc | 1:5e8d9ed18f0f | 6 | CS(CS_), |
ianmcc | 1:5e8d9ed18f0f | 7 | PWM(PWM_) |
ianmcc | 0:5d3ab0ea7f27 | 8 | { |
ianmcc | 0:5d3ab0ea7f27 | 9 | this->init(); |
ianmcc | 0:5d3ab0ea7f27 | 10 | } |
ianmcc | 0:5d3ab0ea7f27 | 11 | |
ianmcc | 1:5e8d9ed18f0f | 12 | void VNH5019::init() |
ianmcc | 0:5d3ab0ea7f27 | 13 | { |
mbedoguz | 11:6902db5e7978 | 14 | // ENDIAG.input(); |
mbedoguz | 11:6902db5e7978 | 15 | // ENDIAG.mode(PullUp); |
mbedoguz | 8:1458b4da7e56 | 16 | PWM.period_us(50); // 4 kHz (valid 0 - 20 kHz) |
ianmcc | 1:5e8d9ed18f0f | 17 | PWM.write(0); |
ianmcc | 1:5e8d9ed18f0f | 18 | INA = 0; |
ianmcc | 1:5e8d9ed18f0f | 19 | INB = 0; |
ianmcc | 0:5d3ab0ea7f27 | 20 | } |
ianmcc | 0:5d3ab0ea7f27 | 21 | |
mbedoguz | 8:1458b4da7e56 | 22 | void VNH5019::speed(double Speed) |
ianmcc | 0:5d3ab0ea7f27 | 23 | { |
ianmcc | 0:5d3ab0ea7f27 | 24 | bool Reverse = 0; |
ianmcc | 0:5d3ab0ea7f27 | 25 | |
ianmcc | 0:5d3ab0ea7f27 | 26 | if (Speed < 0) |
ianmcc | 0:5d3ab0ea7f27 | 27 | { |
ianmcc | 0:5d3ab0ea7f27 | 28 | Speed = -Speed; // Make speed a positive quantity |
ianmcc | 0:5d3ab0ea7f27 | 29 | Reverse = 1; // Preserve the direction |
ianmcc | 0:5d3ab0ea7f27 | 30 | } |
ianmcc | 0:5d3ab0ea7f27 | 31 | |
ianmcc | 0:5d3ab0ea7f27 | 32 | // clamp the speed at maximum |
ianmcc | 0:5d3ab0ea7f27 | 33 | if (Speed > 1.0) |
ianmcc | 0:5d3ab0ea7f27 | 34 | Speed = 1.0; |
ianmcc | 0:5d3ab0ea7f27 | 35 | |
ianmcc | 0:5d3ab0ea7f27 | 36 | if (Speed == 0.0) |
ianmcc | 0:5d3ab0ea7f27 | 37 | { |
ianmcc | 1:5e8d9ed18f0f | 38 | INA = 0; |
ianmcc | 1:5e8d9ed18f0f | 39 | INB = 0; |
ianmcc | 4:3802325cf6e1 | 40 | PWM = 0; |
ianmcc | 0:5d3ab0ea7f27 | 41 | } |
ianmcc | 0:5d3ab0ea7f27 | 42 | else |
ianmcc | 0:5d3ab0ea7f27 | 43 | { |
ianmcc | 1:5e8d9ed18f0f | 44 | INA = !Reverse; |
ianmcc | 1:5e8d9ed18f0f | 45 | INB = Reverse; |
ianmcc | 1:5e8d9ed18f0f | 46 | PWM = Speed; |
ianmcc | 0:5d3ab0ea7f27 | 47 | } |
ianmcc | 0:5d3ab0ea7f27 | 48 | } |
ianmcc | 0:5d3ab0ea7f27 | 49 | |
ianmcc | 1:5e8d9ed18f0f | 50 | void VNH5019::clear_fault() |
ianmcc | 0:5d3ab0ea7f27 | 51 | { |
ianmcc | 0:5d3ab0ea7f27 | 52 | // if ENDIAG is high, then there is no fault |
mbedoguz | 11:6902db5e7978 | 53 | /* if (ENDIAG.read()) |
mbedoguz | 11:6902db5e7978 | 54 | return; */ |
ianmcc | 0:5d3ab0ea7f27 | 55 | |
ianmcc | 0:5d3ab0ea7f27 | 56 | // toggle the inputs |
ianmcc | 1:5e8d9ed18f0f | 57 | INA = 0; |
ianmcc | 1:5e8d9ed18f0f | 58 | INB = 0; |
ianmcc | 0:5d3ab0ea7f27 | 59 | wait_us(250); |
ianmcc | 1:5e8d9ed18f0f | 60 | INA = 1; |
ianmcc | 1:5e8d9ed18f0f | 61 | INB = 1; |
ianmcc | 0:5d3ab0ea7f27 | 62 | wait_us(250); |
ianmcc | 0:5d3ab0ea7f27 | 63 | |
ianmcc | 0:5d3ab0ea7f27 | 64 | // pull low all inputs and wait 1600us for t_DEL |
ianmcc | 1:5e8d9ed18f0f | 65 | INA = 0; |
ianmcc | 1:5e8d9ed18f0f | 66 | INB = 0; |
ianmcc | 1:5e8d9ed18f0f | 67 | PWM = 0; |
mbedoguz | 11:6902db5e7978 | 68 | /* ENDIAG.output(); |
mbedoguz | 11:6902db5e7978 | 69 | ENDIAG = 0; */ |
ianmcc | 0:5d3ab0ea7f27 | 70 | wait_us(1600); |
ianmcc | 0:5d3ab0ea7f27 | 71 | |
ianmcc | 0:5d3ab0ea7f27 | 72 | // and finally re-enable the motor |
mbedoguz | 11:6902db5e7978 | 73 | // ENDIAG.input(); |
ianmcc | 0:5d3ab0ea7f27 | 74 | } |