zhaw_st16b_pes2_10 / Mbed 2 deprecated PES_Yanick

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
beacon
Date:
Tue Mar 21 13:12:54 2017 +0000
Parent:
1:388c915756f5
Commit message:
l

Changed in this revision

Headers/Robot.h Show annotated file Show diff for this revision Revisions of this file
Sources/Robot.cpp Show annotated file Show diff for this revision Revisions of this file
Sources/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Headers/Robot.h	Sun Mar 19 12:20:26 2017 +0000
+++ b/Headers/Robot.h	Tue Mar 21 13:12:54 2017 +0000
@@ -20,6 +20,9 @@
 #define NOBLOCK 0
 #define RED -1
 
+//Misc:
+#define TIMEOUT 20
+
 
 /* Declarations for the Motors in the Robot in order to drive -------- */
 
@@ -76,7 +79,7 @@
         void turnAround(int left);
         void stop();
         
-        void search(int* counter);
+        void search(int* counter, Timer* t);
         
         //DistanceSensors related:
         DistanceSensors sensors[6];
--- a/Sources/Robot.cpp	Sun Mar 19 12:20:26 2017 +0000
+++ b/Sources/Robot.cpp	Tue Mar 21 13:12:54 2017 +0000
@@ -62,21 +62,33 @@
     *right= 0.5f;
 }
 
-void Robot::search(int* counter){
+void Robot::search(int* counter, Timer* t){
     int rando;
     
     if (*counter >= 5) {
+        t->reset();
+        
         rando = rand() % 2;
         while (this->sensors[FWD] < NEAR){
+            if ( t->read() > TIMEOUT ){
+                break;
+            }
+            
             this->leds[FWD] = 1;
             this->turnAround(rando);
         }
-     this->leds[FWD] = 0;   
+        this->leds[FWD] = 0;   
     }
     
     else if (this->sensors[RIGHT] < NEAR){
+        t-> reset();
+        
         *counter += 1;
         while (this->sensors[RIGHT] < NEAR){
+            if ( t->read() > TIMEOUT ){
+                break;
+            }
+            
             this->turnLeft();
             this->leds[RIGHT] = 1;
         }
@@ -84,8 +96,14 @@
     }
     
     else if (this->sensors[LEFT] < NEAR){
+        t-> reset();
+        
         *counter += 1;
         while (this->sensors[LEFT] < NEAR){
+            if ( t->read() > TIMEOUT ){
+                break;
+            }
+            
             this->turnRight();
             this->leds[LEFT] = 1;
         }
@@ -93,8 +111,14 @@
     }
     
     else if (this->sensors[FWD] < NEAR) {
+        t-> reset();
+        
         rando = rand() % 2;
         while (this->sensors[FWD] < NEAR){
+            if ( t->read() > TIMEOUT ){
+                break;
+            }
+            
             this->leds[FWD] = 1;
             this->turnAround(rando);
         }
--- a/Sources/main.cpp	Sun Mar 19 12:20:26 2017 +0000
+++ b/Sources/main.cpp	Tue Mar 21 13:12:54 2017 +0000
@@ -38,12 +38,18 @@
 
 /* */
 int main(){
+    Timer t;
+    t.start();
     
     initializeDistanceSensors();
-    int counter = 0;        //counts how many times the robot has turned, before driving
+    int counter = 0;                //counts how many times the robot has turned, before driving
         
     while( 1 ){
-        sam.search(&counter);
+        if ( t.read() > TIMEOUT ){
+            NVIC_SystemReset();     //Resets Sam.
+        }
+            
+        sam.search(&counter, &t);
         wait( 0.1f );
     }
     return 0;