VNH5019 motor driver library
Diff: vnh5019.cpp
- Revision:
- 0:666c49565378
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vnh5019.cpp Thu Feb 03 08:30:44 2022 +0000 @@ -0,0 +1,44 @@ +#include "vnh5019.h" +#include "mbed.h" + +VNH5019::VNH5019(PinName _dirH,PinName _dirL,PinName _pwm): + dirH(_dirH),dirL(_dirL), pwm(_pwm) { + started = false; + pwm.period(1/20000.0); + dirL = 0; + dirH = 0; + pwm=0; + braking=false; +} + +int VNH5019::setFreq(float freq) +{ + if(freq <= 0) + return 1; + pwm.period(1/freq); + return 0; +} + +int VNH5019::setMotorSpeed(float speed) +{ + started = true; + if(speed >=1.0) + speed = 1.0; + if(speed <= -1.0) + speed = -1.0; + if(fabs(speed) > 0.02){ + if(speed > 0){ + dirH = 0; + dirL = 1; + }else{ + dirH = 1; + dirL = 0; + } + pwm = fabs(speed); + }else{ + dirH = 0; + dirL = 0; + pwm = 0; + } + return 0; +} \ No newline at end of file