a dc_motor drive lib use two PWM IO to drive the dc_motor

Dependents:   Nucleo_F411RE_OS_Robot_Tank

Files at this revision

API Documentation at this revision

Comitter:
adaphoto
Date:
Tue Jun 19 08:50:21 2018 +0000
Child:
1:d31307e787fd
Commit message:
motor driver lib ; use two PWM IO to drive the motor

Changed in this revision

motodriver.cpp Show annotated file Show diff for this revision Revisions of this file
motodriver.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motodriver.cpp	Tue Jun 19 08:50:21 2018 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "motodriver.h"
+
+Motor::Motor(PinName pwm1pin, PinName pwm2pin) : _pwm1(pwm1pin) , _pwm2(pwm2pin)
+{
+    init();    
+}
+
+void Motor::init()
+{
+    // Set initial condition of PWM
+    _pwm1.period(0.001);
+    _pwm1 = 0;
+    
+    // Set initial condition of PWM
+    _pwm2.period(0.001);
+    _pwm2 = 0;
+}
+
+float Motor::Speed(float speed)
+{
+    float temp;
+    temp = abs(speed);
+    
+    if (speed > 0.0)
+    {
+        _pwm1 = temp;
+        _pwm2 = 0;
+    }
+    else
+    {
+        _pwm1 = 0;
+        _pwm2 = temp;
+    }
+    
+    return temp;
+}
+
+void Motor::Stop()
+{
+    _pwm1 = 0;
+    _pwm2 = 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motodriver.h	Tue Jun 19 08:50:21 2018 +0000
@@ -0,0 +1,21 @@
+#ifndef MOTORDRIVER_H_ADA
+#define MOTORDRIVER_H_ADA
+
+class Motor
+{
+    public:
+    Motor(PinName pwm1pin, PinName pwm2pin);
+    
+    float Speed(float speed);
+    
+    void Stop();
+    
+    private:
+    PwmOut _pwm1;
+    PwmOut _pwm2;
+    
+    /** Initialization. */
+    void init();
+};
+
+#endif
\ No newline at end of file