Llenard Gonzalo / Mbed 2 deprecated Racing_14050239

Dependencies:   mbed SRF05

Files at this revision

API Documentation at this revision

Comitter:
Llenard
Date:
Sat Oct 30 18:14:38 2021 +0000
Commit message:
Submission

Changed in this revision

SRF05.lib 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
variables.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SRF05.lib	Sat Oct 30 18:14:38 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/simon/code/SRF05/#e758665e072c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Oct 30 18:14:38 2021 +0000
@@ -0,0 +1,171 @@
+#include "mbed.h"
+#include "variables.h"
+#include "SRF05.h"
+
+DigitalOut myled(LED1);
+#include <vector> //Tracker data
+
+
+#define TESTING 1  //for TDD
+
+
+// Forwards and backwards
+void driveWheels(int direction, float pause)
+{
+    
+#if TESTING
+    pc.printf("Driving\n");
+#endif
+    
+    if(direction == forward)
+    {
+        drive   = 1;
+        turnRight     = 0;
+        wait(pause);        
+    }
+    else
+    {
+        drive   = 0;
+        turnRight     = 1;
+        wait(pause);  
+    }
+    drive   = 0;
+    turnRight     = 0;
+    
+#if TESTING
+    wait(0.01);
+#endif
+}
+
+// Steering function
+void turnWheels(int direction)
+{
+#if TESTING
+    pc.printf("Turning\n");
+#endif
+    switch(direction)
+    {
+        case left:
+            turnLeft = 0;
+            turnRight = 1;
+            break;
+        case right:
+            turnLeft = 1;
+            turnRight = 0;
+            break;  
+        case center:
+        defauturnLeft:
+            turnLeft = 0;
+            turnRight = 0;
+            break;
+    }
+}
+
+//Robot location
+std::vector <float> readPos()
+{
+#if TESTING
+    pc.printf("Reading\n");
+#endif
+    std::vector<float> pos;
+    pos.push_back(ir1.read());
+    pos.push_back(ir2.read());
+    pos.push_back(ir3.read());
+    pos.push_back(ir4.read());
+    pos.push_back(ir5.read());
+    return pos;
+}
+
+int wheelDir(std::vector<float> line)
+{
+#if TESTING
+    pc.printf("Locating\n");
+#endif
+    int ret = 100;
+    float low = 1;
+    for (unsigned i=0; i<line.size(); ++i)
+    {
+#if TESTING
+    pc.printf("%.1f", line[i]);
+    pc.printf("\n");
+#endif
+        if(line[i] < low && line[i] < 0.7)
+        {
+            ret = i; 
+            low = line[i];
+        }
+       
+    }
+        
+#if TESTING
+    pc.printf("%u", ret);
+    pc.printf("\n");
+#endif
+    return ret;
+}
+//Collision handling
+float collision(float col){
+    col = srf.read();
+#if TESTING    
+    pc.printf("Distance = %.1f\n", col);
+#endif    
+    return col;
+}
+
+//Main Loop
+int main()
+{
+#if TESTING
+    pc.printf("Set_up_variables\n");
+#endif
+    std::vector <float>line;
+    std::vector <float> pturnRightLine = readPos();
+    int dir;
+    int pos;
+    float col;
+    while(1)
+    {
+#if TESTING
+        pc.printf("Main\n");
+#endif
+        line = readPos();
+        pos = wheelDir(line);
+#if TESTING
+        pc.printf("%u",pos);
+#endif
+        
+        if(pos == 100) 
+        { 
+            driveWheels(backward, fulldrive);
+        }
+        else 
+        {
+            if(pturnRightLine != line)
+            {
+                switch(pos) 
+                {
+                    case 0:
+                    case 1:
+                        dir = left;
+                        break; 
+                    case 2: 
+                        dir = center;
+                        break;
+                    case 3:
+                    case 4:
+                        dir = right;
+                        break;   
+                }
+                pturnRightLine.clear();
+                std::vector<float> pturnRightLine(line);     
+                turnWheels(dir);
+                collision(col);    
+            }
+            driveWheels(forward, fulldrive);
+            //Collision handling
+            if(col <= 200){
+                driveWheels(backward, fulldrive);
+            }      
+        } 
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Oct 30 18:14:38 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/variables.h	Sat Oct 30 18:14:38 2021 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "SRF05.h"
+
+#define fulldrive 0.0115
+#define halfdrive 0.6
+
+
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+AnalogIn ir1(PA_1);
+AnalogIn ir2(PA_4);
+AnalogIn ir3(PB_0);
+AnalogIn ir4(PC_1);
+AnalogIn ir5(PC_0);
+ 
+DigitalOut turnLeft(PA_10); //left turn
+DigitalOut turnRight(PA_8); //right turn
+DigitalOut back(PA_9); //reverse
+DigitalOut drive(PA_6); //forward
+ 
+bool setupFlag = false;
+SRF05 srf(PA_2, PA_3);
+
+ 
+enum direction{forward, backward, left = 77, right = 99, center = 88};
+enum trackersensor{sen1_left, sen2_left, sen3_mid, sen4_right, sen5_right};
\ No newline at end of file