VNH5019 motor driver library
vnh5019.cpp@0:666c49565378, 2022-02-03 (annotated)
- Committer:
- LVRhase01
- Date:
- Thu Feb 03 08:30:44 2022 +0000
- Revision:
- 0:666c49565378
first create vnh5019 library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LVRhase01 | 0:666c49565378 | 1 | #include "vnh5019.h" |
LVRhase01 | 0:666c49565378 | 2 | #include "mbed.h" |
LVRhase01 | 0:666c49565378 | 3 | |
LVRhase01 | 0:666c49565378 | 4 | VNH5019::VNH5019(PinName _dirH,PinName _dirL,PinName _pwm): |
LVRhase01 | 0:666c49565378 | 5 | dirH(_dirH),dirL(_dirL), pwm(_pwm) { |
LVRhase01 | 0:666c49565378 | 6 | started = false; |
LVRhase01 | 0:666c49565378 | 7 | pwm.period(1/20000.0); |
LVRhase01 | 0:666c49565378 | 8 | dirL = 0; |
LVRhase01 | 0:666c49565378 | 9 | dirH = 0; |
LVRhase01 | 0:666c49565378 | 10 | pwm=0; |
LVRhase01 | 0:666c49565378 | 11 | braking=false; |
LVRhase01 | 0:666c49565378 | 12 | } |
LVRhase01 | 0:666c49565378 | 13 | |
LVRhase01 | 0:666c49565378 | 14 | int VNH5019::setFreq(float freq) |
LVRhase01 | 0:666c49565378 | 15 | { |
LVRhase01 | 0:666c49565378 | 16 | if(freq <= 0) |
LVRhase01 | 0:666c49565378 | 17 | return 1; |
LVRhase01 | 0:666c49565378 | 18 | pwm.period(1/freq); |
LVRhase01 | 0:666c49565378 | 19 | return 0; |
LVRhase01 | 0:666c49565378 | 20 | } |
LVRhase01 | 0:666c49565378 | 21 | |
LVRhase01 | 0:666c49565378 | 22 | int VNH5019::setMotorSpeed(float speed) |
LVRhase01 | 0:666c49565378 | 23 | { |
LVRhase01 | 0:666c49565378 | 24 | started = true; |
LVRhase01 | 0:666c49565378 | 25 | if(speed >=1.0) |
LVRhase01 | 0:666c49565378 | 26 | speed = 1.0; |
LVRhase01 | 0:666c49565378 | 27 | if(speed <= -1.0) |
LVRhase01 | 0:666c49565378 | 28 | speed = -1.0; |
LVRhase01 | 0:666c49565378 | 29 | if(fabs(speed) > 0.02){ |
LVRhase01 | 0:666c49565378 | 30 | if(speed > 0){ |
LVRhase01 | 0:666c49565378 | 31 | dirH = 0; |
LVRhase01 | 0:666c49565378 | 32 | dirL = 1; |
LVRhase01 | 0:666c49565378 | 33 | }else{ |
LVRhase01 | 0:666c49565378 | 34 | dirH = 1; |
LVRhase01 | 0:666c49565378 | 35 | dirL = 0; |
LVRhase01 | 0:666c49565378 | 36 | } |
LVRhase01 | 0:666c49565378 | 37 | pwm = fabs(speed); |
LVRhase01 | 0:666c49565378 | 38 | }else{ |
LVRhase01 | 0:666c49565378 | 39 | dirH = 0; |
LVRhase01 | 0:666c49565378 | 40 | dirL = 0; |
LVRhase01 | 0:666c49565378 | 41 | pwm = 0; |
LVRhase01 | 0:666c49565378 | 42 | } |
LVRhase01 | 0:666c49565378 | 43 | return 0; |
LVRhase01 | 0:666c49565378 | 44 | } |