Buggy bois / Mbed 2 deprecated HEATS_1

Dependencies:   mbed

Revision:
0:f45212966fb1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wheel.h	Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,63 @@
+class Wheel {
+    private:
+    float angularVelocity; //angular speed of the wheel
+    float linearVelocity;
+    PwmOut Mtr; //connect this pin to the motor driveboard pwm
+    DigitalOut direction; //connected to the direction pin of motor drive board
+    float static const wheelDiameter = 0.18;
+    Encoder* enc;
+    float const static MAV = 510;
+    
+    public:
+    Wheel (Encoder* E, PinName M, PinName C) : Mtr(M), direction(C) {
+        enc = E;
+        Mtr.write(1.0f); //duty cycle of 100%
+        Mtr.period_us(100000); //frequency of 1KHz determine this constant value based on switching losses+frequency losses
+        //higher freq -> more switching losses lower freq -> more "capacitive losses" need to find a balance
+        direction = 0;
+        Mtr.write(0.1f); //duty cycle of 0.1 -> turn it off
+        }
+        
+    
+    float calculateAngularVelocity(){ //returns a float value which is the angular velocity of the WHEEL
+        angularVelocity = ((enc->encoderTickRate())/512)*2*3.141593f;
+        return ((enc->encoderTickRate())/512)*2*3.141593f;
+        };
+    
+    void setDutyCycle(float f)
+    {
+        if (f > 1.0f)
+        {
+        Mtr.write(1);
+        }
+        else if (f < 0.0f)
+        {
+         Mtr.write (0);   
+        }else
+        {
+         Mtr.write(f);
+        }
+    };
+        
+    void setFrequency(int freq){
+        Mtr.period(1/freq);
+        };
+        
+    void adjustAngularVelocity(float W) // W = angular velocity you want, obviously putting a |w| value that is > max angular velocity will set dutcy cycle to 1
+    {
+        if (abs(W) >= MAV)
+        {
+            Mtr.write(1);
+        }else 
+            {
+            Mtr.write(abs(W)/MAV); //assuming linear relationship between dutcy cycle and speed.
+            }
+        if (W<0) 
+        { 
+        direction = 0; 
+        } else  { 
+        direction = 1; 
+                }
+    };
+    
+    };
\ No newline at end of file