Kiko Ishimoto / Motor2

Dependents:   OneCircleRobot

Fork of Motor by Kiko Ishimoto

Files at this revision

API Documentation at this revision

Comitter:
kikoaac
Date:
Fri Jun 19 06:35:46 2015 +0000
Child:
1:4ab6e9768847
Commit message:
Motor

Changed in this revision

Motor.cpp Show annotated file Show diff for this revision Revisions of this file
Motor.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.cpp	Fri Jun 19 06:35:46 2015 +0000
@@ -0,0 +1,33 @@
+/**
+ * Includes
+ */
+#include "Motor.h"
+
+Motor::Motor(PinName _pin_h1, PinName _pin_g2, PinName _pin_g1, PinName _pin_h2,PinName _pwm,float Max) :
+motor(_pin_h1,_pin_g2,_pin_g1,_pin_h2),PwmPin(_pwm)
+{
+    max=Max;
+    PwmPin.period_ms(10);
+    run(Stop,1);
+}
+void Motor::run(int i,float duty)
+{
+    static int state;
+    Duty = duty/max;
+    //if(state==i)return;
+    PwmPin = Duty;
+    if(state==i)return;
+    Timer t;
+    motor=0;
+    //wait_us(20);
+    t.start();
+    t.reset();
+    while(t.read_us()>=20);
+    if(i==Front) motor=0x01|0x04;
+    else if(i==Back) motor=0x02|0x08;
+    else if(i==Stop) motor=0x01|0x08;
+    else if(i==Free) motor=0x00|0x00;
+    else motor=0;   
+    state=i;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.h	Fri Jun 19 06:35:46 2015 +0000
@@ -0,0 +1,38 @@
+
+#ifndef PID_H
+#define PID_H
+
+#include "mbed.h"
+#define Front 1
+#define Back 2
+#define Stop 3
+#define Free 4
+class Motor {
+
+public:
+
+    Motor(PinName _pin_h1, PinName _pin_g2, PinName _pin_g1, PinName _pin_h2,PinName _pwm,float Max);
+    Motor& operator= (float duty)
+    {
+        if(duty<0)
+        {
+            duty*=-1;
+            run(Front,duty);
+        }
+        else if(duty>0)
+        {
+            run(Back,duty);
+        }
+        else run(Free,duty);
+        return *this;
+    }
+    void run(int i,float duty);
+    float min,max;
+private:
+    
+    float Duty;
+    BusOut motor;
+    PwmOut PwmPin;
+};
+
+#endif /* PID_H */