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 14:30:32 2015 +0000
Parent:
0:15664f71b21f
Child:
2:6f15a798993d
Commit message:
Updated functions

Changed in this revision

functions.cpp 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 14:13:09 2015 +0000
+++ b/functions.cpp	Sat May 23 14:30:32 2015 +0000
@@ -12,7 +12,7 @@
 int read_line1()
 {
     // Time to record how long input is active
-    Timer t;
+    Timer temp_t;
     
     // Activate the line sensor and then read
     line1.output();
@@ -21,18 +21,18 @@
     line1.input();
     
     // Start timer
-    t.start();
+    temp_t.start();
 
     // Time how long the input is HIGH, but quit
     // after 1ms as nothing happens after that
-    while (line1 == 1 && t.read_us() < 1000);
-    return t.read_us();
+    while (line1 == 1 && temp_t.read_us() < 1000);
+    return temp_t.read_us();
 }
 
 int read_line2()
 {
     // Time to record how long input is active
-    Timer t;
+    Timer temp_t;
     
     // Activate the line sensor and then read
     line2.output();
@@ -45,7 +45,7 @@
 
     // Time how long the input is HIGH, but quit
     // after 1ms as nothing happens after that
-    while (line2 == 1 && t.read_us() < 1000);
+    while (line2 == 1 && temp_t.read_us() < 1000);
     return t.read_us();
 }
 
@@ -78,7 +78,7 @@
 void reverseandturn()
 {
     printf("Reverse and turn\n");
-    MotorLeft.speed((chargespeed-0.2));
+    MotorLeft.speed((chargespeed-0.3));
     MotorRight.speed(-(chargespeed-0.1));
     wait_ms(2000);
     return;
@@ -89,4 +89,33 @@
     MotorLeft.speed(chargespeed);
     MotorRight.speed(chargespeed);
     return;
+}
+
+int detect_object(int smooth)
+{
+    // Start a timer - finds an object for 10 seconds
+    // if it doesn't find anything returns 0
+    Timer usensor_t;
+    usensor_t.start();
+    
+    // Variable to store sensed value
+    int sense;
+        
+    while (usensor_t.readms < 10000)
+    {
+        // Start the ultrasonic sensor
+        usensor.start();
+        dist = usensor.get_dist_cm();
+        
+        // If an object is detected based on out set range return 1
+        if (dist <= range && dist >= 1) {
+            sense = 1;
+            break;
+        } else {
+            sense = 0;
+            MotorLeft.speed(searchspeed);
+            MotorRight.speed(-(searchspeed));
+        }
+    }
+    return sense;
 }
\ No newline at end of file
--- a/main.cpp	Sat May 23 14:13:09 2015 +0000
+++ b/main.cpp	Sat May 23 14:30:32 2015 +0000
@@ -44,7 +44,7 @@
 
 // Setting pins for ultrasonic sensor, as follows:
 // Example: usensor(Trigger, Echo)
-HCSR04  usensor(p25,p26);
+HCSR04 usensor(p25,p26);
 
 // Set for debugging purpose
 // Example: pc(TX, RX)
@@ -52,7 +52,7 @@
 
 
 // Global parameters
-// Speed at which it charged an object
+// Speed at which it charges an object
 // optimum value: 0.4 to 0.8
 float chargespeed;
 // Speed at which it rotates to find an object
@@ -84,35 +84,6 @@
     t.start();
 
     while(1) { // main loop
-        if (dist <= 35 && dist >= 1) {
-            MotorLeft.speed(-0.4);
-            MotorRight.speed(-0.4);
-            Timer t3;
-
-            t3.start();
-
-            //printf("%d,%d,%d\r\n",line1val,line2val,line1val-line2val);
-
-            while (t3.read_ms() < 100) {
-
-                if (((line1val-line2val) > 50) || ((line1val-line2val) < -50)) {
-                    MotorLeft.speed(0.4);
-                    MotorRight.speed(0.4);
-
-                    wait_ms(500);
-
-                    //return 0;
-                    printf("STOP\r\n");
-                }
-                //t.reset();
-            }
-
-        } else {
-            MotorLeft.speed(motorspeed);
-            MotorRight.speed(-(motorspeed));
-        }
-
-        usensor.start();
         if (t.read_ms() >=100) {
             dist=usensor.get_dist_cm();
             motorspeed = -0.8;