geonwoo park / Mbed 2 deprecated L298LIB

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
kennethdhdl
Date:
Sat Jun 04 05:41:18 2022 +0000
Commit message:
L298N motor driver library

Changed in this revision

L298LIB.cpp Show annotated file Show diff for this revision Revisions of this file
L298LIB.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/L298LIB.cpp	Sat Jun 04 05:41:18 2022 +0000
@@ -0,0 +1,54 @@
+#include "L298LIB.h"
+L298LIB::L298LIB(PinName pinEnable, PinName pinIN1, PinName pinIN2, PinName pinIN3, PinName pinIN4):
+    _pwm(pinEnable),
+    _pinIN1 (pinIN1),
+    _pinIN2 (pinIN2),
+    _pinIN3 (pinIN3),
+    _pinIN4 (pinIN4)
+{
+    _pwm.period_us(25); // 40kHz
+    _pwm = 0.;
+    _pinIN1 = 0;
+    _pinIN2 = 0;
+    _pinIN3 = 0;
+    _pinIN4 = 0;
+}
+void L298LIB::setSpeed(float pwmVal)
+{
+    _pwm = pwmVal;
+}
+float L298LIB::getSpeed()
+{
+    return _pwm;
+}
+void L298LIB::forward()
+{
+    _pinIN1=1;
+    _pinIN2=0;
+    _pinIN3=1;
+    _pinIN4=0;
+}
+void L298LIB::backward()
+{
+    _pinIN1=0;
+    _pinIN2=1;
+    _pinIN3=0;
+    _pinIN4=1;
+}
+void L298LIB::stop()
+{
+    _pinIN1=0;
+    _pinIN2=0;
+    _pinIN3=0;
+    _pinIN4=0;
+}
+void L298LIB::run(float vel)
+{
+    if (vel>=0) {
+        setSpeed((vel>1.0f) ? 1.0f:vel);
+        forward();
+    } else {
+        setSpeed((vel<-1.0f) ? 1.0f:-vel);
+        backward();
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/L298LIB.h	Sat Jun 04 05:41:18 2022 +0000
@@ -0,0 +1,21 @@
+#ifndef L298LIB_h
+#define L298LIB_h
+#include "mbed.h"
+class L298LIB
+{
+    public:
+        L298LIB(PinName pinEnable, PinName pinIN1, PinName pinIN2, PinName pinIN3, PinName pinIN4);
+        void setSpeed(float pwmVal);
+        float getSpeed();
+        void forward();
+        void backward();
+        void stop();
+        void run(float velocity);
+    private:
+    PwmOut _pwm;
+    DigitalOut _pinIN1;
+    DigitalOut _pinIN2;
+    DigitalOut _pinIN3;
+    DigitalOut _pinIN4;
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Jun 04 05:41:18 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file