Warehouse Rover main program

Dependencies:   mbed Team2_Library

Files at this revision

API Documentation at this revision

Comitter:
simon9987
Date:
Fri Mar 11 04:13:50 2022 +0000
Parent:
0:c642441cc98b
Commit message:
N

Changed in this revision

Team2_Library.lib Show annotated file Show diff for this revision Revisions of this file
WareHouse.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show diff for this revision Revisions of this file
--- a/Team2_Library.lib	Tue Mar 08 06:37:21 2022 +0000
+++ b/Team2_Library.lib	Fri Mar 11 04:13:50 2022 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/simon9987/code/Team2_Library/#f1b2082f8cb9
+https://os.mbed.com/users/simon9987/code/Team2_Library/#27b38338ec62
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WareHouse.cpp	Fri Mar 11 04:13:50 2022 +0000
@@ -0,0 +1,93 @@
+#include "Common.h"
+
+bool enteredReverse = false;
+
+bool obstacleAvoidance(){
+    getDistance();
+    const unsigned int frontRight = sensorDistance[0], frontLeft = sensorDistance[1], rear = sensorDistance[2];
+
+    if((avoidObstacle(frontRight) && avoidObstacle(frontLeft)) || enteredReverse){ //all sensors are blocked car needs to stop and terminate break out of movement
+        if(avoidObstacle(rear)){
+            buzzerTerminate();
+            sendMessage("Terminating program - car is stuck!\r\n");
+            stop();
+            resetAll();
+            return true;
+        }
+        moveReverse();
+        enteredReverse = true;
+    } else if(avoidObstacle(frontRight)){ //turn right if obstacle detected on right sonar sensor
+        steerLeft();
+        moveForward();
+
+    } else if(avoidObstacle(frontLeft)){ //turn left if obstacle detected on left sonar sensor
+        steerRight();
+        moveForward();
+    } else {
+        resetSteering(); //straighten steering wheel
+        //if previously reversing continue to reverse else move forward
+        if(enteredReverse)
+            moveReverse();
+        else
+            moveForward();
+    }
+    return false;
+}
+
+bool checkColour(){
+    const int waitTime = 2;
+
+    Colour RED = Colour(255,99,71);
+    Colour BLUE = Colour(0,10,255);
+    Colour YELLOW = Colour(255,255,0);
+    Colour GREEN = Colour(0, 128, 0);
+    Colour BLACK = Colour(0, 0, 0);
+
+    Colour currentColour = getColour();
+    currentColour.setColourTolerance(8,8,8); // set room for error for slightly varying versions of the colour
+
+    //notify operator to collect package and wait 60 seconds
+    if(currentColour.equals(BLACK)){
+        stop();
+        sendMessage("Car has arrived. Please collect package\r\n");
+        for(int i = 0; i<30; ++i)
+            buzzerPackage(); //beeps for 60 seconds to also notify operator
+        resetAll();
+        return true;
+    }
+
+    if(currentColour.equals(YELLOW)){
+        steerRight(); //turn right if yellow is detected
+        wait(waitTime);
+    } else if(currentColour.equals(RED)){
+        steerLeft(); //turn left if red is detected
+        wait(waitTime);
+    } else if(currentColour.equals(BLUE) && packageDestination == 1){
+        steerLeft(); // if blue is detected and the package destination is set to one
+        wait(waitTime);
+    } else if(currentColour.equals(GREEN) && packageDestination == 2){
+        steerLeft(); //turn left if green is detected and the package destination is set to two
+        wait(waitTime);
+    } else
+        resetSteering();
+
+    moveForward();
+    return false;
+}
+
+int main() {
+    setUpMovement(0.2, -0.2, -0.7);
+    setUpRGB();
+    packageDestination = getPackageDestination(); //returns int which correlates to package destination
+
+    while (true){
+        if(checkColour()) // break loop if car has reached package destination
+            break;
+
+        if(obstacleAvoidance()){ //break loop if car is unable to move
+            break;
+        }
+    }
+
+    sendMessage("Rc car has terminated!\r\n"); // notify operator that car has finished its operation
+}
--- a/main.cpp	Tue Mar 08 06:37:21 2022 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
-#include "Common.h"
-
-bool enteredReverse = false;
-int packageDestination = 0;
-
-bool obstacleAvoidance(){
-    getDistance();
-    const unsigned int frontRight = sensorDistance[0], frontLeft = sensorDistance[1], rear = sensorDistance[2];
-
-    if(avoidObstacle(frontRight) && avoidObstacle(frontLeft)){ //all sensors are blocked car needs to stop and terminate break out of movement
-        if(avoidObstacle(rear)){
-            buzzerTerminate();
-            stop();
-            resetAll();
-            return true;
-        }
-        moveReverse();
-        enteredReverse = true;
-    } else if(avoidObstacle(frontRight)){ //turn right if obstacle detected on right sonar sensor
-        steerLeft();
-        moveForward();
-
-    } else if(avoidObstacle(frontLeft)){ //turn left if obstacle detected on left sonar sensor
-        steerRight();
-        moveForward();
-    } else {
-        resetSteering(); //straighten steering wheel
-        //if previously reversing continue to reverse else move forward
-        if(enteredReverse)
-            moveReverse();
-        else
-            moveForward();
-    }
-    return false;
-}
-
-bool checkColour(){
-    const int waitTime = 2;
-
-    Colour RED = Colour(255,99,71);
-    Colour BLUE = Colour(0,10,255);
-    Colour YELLOW = Colour(255,255,0);
-    Colour GREEN = Colour(0, 128, 0);
-    Colour BLACK = Colour(0, 0, 0);
-
-    Colour currentColour = getColour();
-    currentColour.setColourTolerance(8,8,8); // set room for error for slightly varying versions of the colour
-
-    //notify operator to collect package and wait 60 seconds
-    if(currentColour.equals(BLACK)){
-        stop();
-        for(int i = 0; i<30; ++i)
-            buzzerPackage(); //beeps for 60 seconds to also notify operator
-        resetAll();
-        bluetooth.print("Car has arrived. Please collect package\r\n"); // notify operator that package has arrived at the destination
-        return true;
-    }
-
-    if(currentColour.equals(YELLOW)){
-        steerRight(); //turn right if yellow is detected
-        wait(waitTime);
-    } else if(currentColour.equals(RED)){
-        steerLeft(); //turn left if red is detected
-        wait(waitTime);
-    } else if(currentColour.equals(BLUE) && packageDestination == 1){
-        steerLeft(); // if blue is detected and the package destination is set to one
-
-        wait(waitTime);
-    } else if(currentColour.equals(GREEN) && packageDestination == 2){
-        steerLeft(); //turn left if green is detected and the package destination is set to two
-        wait(waitTime);
-    } else
-        resetSteering();
-
-    moveForward();
-    return false;
-}
-
-int main() {
-    setUpMovement(0.2, -0.2, -0.7);
-    packageDestination = getLocation(); //returns int which correlates to package destination
-
-    while (true){
-        if(checkColour()) // break loop if car has reached package destination
-            break;
-
-        if(obstacleAvoidance()){ //break loop if car is unable to move
-            bluetooth.print("Terminating program - car is stuck!\r\n"); // notify operator that car is stuck
-            break;
-        }
-    }
-
-    bluetooth.print("Rc car has terminated!\r\n"); // notify operator that car has finished its operation
-}