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