Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Files at this revision

API Documentation at this revision

Comitter:
abdsha01
Date:
Sat May 23 17:48:01 2015 +0000
Parent:
8:43a650f9d4af
Parent:
9:7770a84228c0
Child:
11:b45798cc3c10
Child:
12:703f75ae2453
Commit message:
Updated stuff

Changed in this revision

functions.cpp Show annotated file Show diff for this revision Revisions of this file
functions.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
--- a/functions.cpp	Sat May 23 17:08:45 2015 +0000
+++ b/functions.cpp	Sat May 23 17:48:01 2015 +0000
@@ -82,30 +82,31 @@
 
 void reverse(float speed) {
     printf("Reverse\n");
-    MotorLeft.speed((speed));
+    MotorLeft.speed(-(speed));
     MotorRight.speed(-(speed));
-    wait_ms(1000);
-    return;
+}
+
+void turn(float speed) {
+    printf("Turning\n");
+    MotorLeft.speed(speed);
+    MotorRight.speed(-(speed));
 }
 
 void reverseandturn(float speed) {
     printf("Reverse and turn\n");
     MotorLeft.speed((speed-0.3));
     MotorRight.speed(-(speed-0.1));
-    wait_ms(2000);
-    return;
 }
 
 void charge(float speed) {
     MotorLeft.speed(speed);
     MotorRight.speed(speed);
-    return;
 }
 
 void moverandom()
 {
     int counter;
-    int turn;
+    int dir;
     int direction;
     float random_time;
     float random_speed;
@@ -117,7 +118,7 @@
     {
         random_time = rand() % 1000 + 1000;
 
-        turn = rand()/(RAND_MAX);
+        dir = rand()/(RAND_MAX);
         random_speed = (float)rand()/(float)(RAND_MAX);
         if (random_speed < 0.5) {
             random_speed = 0.5;
@@ -125,20 +126,17 @@
             random_speed = 0.8;
         }
         
-        if (turn == 0) {
+        if (dir == 0) {
             direction = rand()/(RAND_MAX);
             if  (direction == 1) { 
-                MotorLeft.speed(random_speed);
-                MotorRight.speed(random_speed);
+                charge(random_speed);
                 wait_ms(random_time);
             } else {
-                MotorLeft.speed(-random_speed);
-                MotorRight.speed(-random_speed);
+                reverse(random_speed);
                 wait_ms(random_time);
             }   
         } else {
-            MotorLeft.speed(-random_speed);
-            MotorRight.speed(random_speed);
+            turn(random_speed);
             wait_ms(random_time);
         }
         return;
@@ -148,7 +146,6 @@
 void stop() {
     MotorLeft.speed(0.0);
     MotorRight.speed(0.0);
-    return;
 }
 
 int detect_object(int range, float speed) {
@@ -175,8 +172,7 @@
             break;
         } else {
             sense = 0;
-            MotorLeft.speed(speed);
-            MotorRight.speed(-(speed));
+            turn(speed);
         }
         
         if (inner_t.read_ms() >=100) {
--- a/functions.h	Sat May 23 17:08:45 2015 +0000
+++ b/functions.h	Sat May 23 17:48:01 2015 +0000
@@ -5,6 +5,7 @@
 int detect_line();
 void moverandom();
 void reverse(float speed);
+void turn(float speed);
 void reverseandturn(float speed);
 void charge(float speed);
 void stop();
--- a/main.cpp	Sat May 23 17:08:45 2015 +0000
+++ b/main.cpp	Sat May 23 17:48:01 2015 +0000
@@ -79,21 +79,33 @@
 
     Timer t;
     t.start();
+    
+    int detect = 0;
 
     while(1) {
-    // Sample code to detect and object and charge at it
-    if (detect_object(range, searchspeed)) {
-         
-        charge(chargespeed);
-        wait(3);
+        // Sample code to detect and object and charge at it
+        if (detect_object(range, searchspeed)) {
+            while (true) {
+                charge(chargespeed);
+
+                detect = detect_line();
+                // If line is detected from front then reverse
+                if(detect == 1) {
+                    reverse(searchspeed);
+                    wait(2);
+                    stop();
+                    break;
+                    // If line is detected from back just keep on moving forward
+                } else if (detect == -1) {
+                    wait(2);
+                    stop();
+                    break;
+                }
+            }
+        } else {
+            moverandom();
+        }
         stop();
-          
-    } else {
-        moverandom();  
-  
-    }
-    
-    stop();
     }
 }