Buggy bois / Mbed 2 deprecated HEATS_1

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
mazdo25
Date:
Mon Feb 18 13:13:34 2019 +0000
Child:
1:813f4b17ae65
Commit message:
initial commit

Changed in this revision

Encoder.h Show annotated file Show diff for this revision Revisions of this file
PID.lib Show annotated file Show diff for this revision Revisions of this file
QEI.lib Show annotated file Show diff for this revision Revisions of this file
Robot.h Show annotated file Show diff for this revision Revisions of this file
Wheel.h Show annotated file Show diff for this revision Revisions of this file
main.cpp 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/Encoder.h	Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,49 @@
+//works only in x2 encoding (512 counts per second)
+class Encoder {  
+    private:
+    InterruptIn ChannelA; //connect this to channel A, 
+    DigitalIn ChannelB; //connect this to channel B,
+    Timer dT; //to calculate rate of change of encoder ticks.
+    int PET;
+    int CET;
+    
+    public:
+    
+    
+    Encoder(PinName A, PinName B) : ChannelA(A),ChannelB(B){
+        dT.reset();
+        ChannelA.rise(callback(this, &Encoder::riseEncode)); //
+        ChannelA.fall(callback(this, &Encoder::fallEncode));
+        CET = 0;
+        PET = 0;
+        dT.start();
+        };
+        
+    void riseEncode(){ //logic for what happens when you get a pulse in channel A
+        if (ChannelB.read() != 0) //essentially if the rise happens while B is 1 (high) means its going in reverse
+            {
+            CET --;
+            } else
+            {
+            CET ++;   
+            }
+        };
+        
+    void fallEncode(){ //logic, same as above but a fall in channel A
+        if (ChannelB.read() != 0){ //essentially if fall pulse in channel A happens while b ia high, means going forward
+            CET ++; 
+            } else
+            {
+            CET --;
+            }
+        };  
+        //refer to technical handbook quadrature encoder section.
+        
+    float encoderTickRate() {
+        int Temp = PET;
+        PET = CET;
+        dT.reset();
+        return (CET-Temp)/(int)dT.read();
+        };
+
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PID.lib	Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/aberk/code/PID/#6e12a3e5af19
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QEI.lib	Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/aberk/code/QEI/#8ac891a6882f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Robot.h	Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,33 @@
+class Robot {
+    private:
+    float robotAngularVelocity;
+    float robotTranslationalVeocity;
+    Wheel* leftWheel;
+    Wheel* rightWheel;
+    float static const distanceBetweenWheels = 0.19;
+    
+    public:
+    Robot(Wheel* LW, Wheel* RW) {
+        leftWheel = LW;
+        rightWheel= RW;
+        
+        robotTranslationalVeocity = (lTemp + rTemp)/2;
+        };
+    
+    void turn(float angularVelocity){//positive value is right, negative value is left
+        
+        };
+        
+    void calculateTranslationalVelocity(){
+        robotTranslationalVeocity = (LW->calculateAngularVelocity() + RW->calculateAngularVelocity();)/2;
+        }
+        
+    void calculateAngularVelocity(){
+        robotAngularVelocity = (LW->calculateAngularVelocity() - RW->calculateAngularVelocity())/distanceBetweenWheels;
+        }
+    
+    void Turn(float degrees){
+        
+        }
+    
+    };
\ No newline at end of file
--- /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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,21 @@
+#include "mbed.h"
+#include "Encoder.h"
+#include "PID.h"
+#include "Wheel.h"
+#include "QEI.h"
+
+//blue D8
+//Green D9
+//Red D5
+
+int main() {
+    DigitalOut enable(PC_12);
+    enable.write(1);
+    Encoder* LE = new Encoder(A5,A4);
+    Encoder* RE = new Encoder(A2,A3);
+    Wheel* leftWheel = new Wheel(LE,PB_7,PB_14);
+    Wheel* rightWheel = new Wheel(RE,PA_15,PB_13);
+    
+    
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file