VNH5019 motor driver library

Files at this revision

API Documentation at this revision

Comitter:
LVRhase01
Date:
Thu Feb 03 08:30:44 2022 +0000
Commit message:
first create vnh5019 library

Changed in this revision

vnh5019.cpp Show annotated file Show diff for this revision Revisions of this file
vnh5019.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 666c49565378 vnh5019.cpp
--- /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
diff -r 000000000000 -r 666c49565378 vnh5019.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vnh5019.h	Thu Feb 03 08:30:44 2022 +0000
@@ -0,0 +1,33 @@
+#ifndef VNH5019_H
+#define VNH5019_H
+
+#include "mbed.h"
+/**
+* vnh5019 class
+* class to drive vnh5019
+*/
+class VNH5019
+{
+public:
+    /** creates VNH5019 instance
+    *@param _dirH dirH pin
+    *@param _dirL dirL pin
+    *@param _pwm pwm pin
+    */ 
+    VNH5019(PinName _dirH,PinName _dirL,PinName _pwm);
+    /** set drive Mode won't work after setMotorSpeed() is called
+    */ 
+    int setMotorSpeed(float speed);
+    /** set pwm frequency
+    *@param frequency needs to be bigger than 0
+    */
+    int setFreq(float freq);
+    /** set this variable to 1 if you want to brake when the motorSpeed is set to 0
+    */
+    bool  braking;
+private:
+    bool started;
+    DigitalOut dirH,dirL;
+    PwmOut pwm;
+};
+#endif
\ No newline at end of file