ECE4180 Final Project

Dependencies:   LSM9DS1_Library Motor mbed-rtos mbed HC_SR04_Ultrasonic_Library

Fork of IMURoomba4_ThrowSumMo by James Plager

Revision:
6:7123768ea0c9
Parent:
5:ab5fd9a37d7a
--- a/main.cpp	Thu May 04 20:23:10 2017 +0000
+++ b/main.cpp	Thu May 04 23:54:17 2017 +0000
@@ -31,9 +31,6 @@
 // Speaker out
 //AnalogOut DACout(p18);      //must(?) be p18
 //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
-ultrasonic mu(p29, p30, .1, 1);    //Set the trigger pin to D8 (p29) and the echo pin to D9 (p30)
-                                        //have updates every .1 seconds and a timeout after 1
-                                        //second, and call dist when the distance changes
 
 Thread thread1;
 //Thread thread2;
@@ -49,6 +46,18 @@
 //float origHeading;
 //float heading;
 
+void dist(int distance)
+{
+    //put code here to execute when the distance has changed
+    if(distance*0.00328084 < 40) {
+    //printf("Distance %f ft\r\n", distance*0.00328084);
+    }
+}
+
+ultrasonic mu(p29, p30, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
+                                        //have updates every .1 seconds and a timeout after 1
+                                        //second, and call dist when the distance changes
+
 // Calculate pitch, roll, and heading.
 // Pitch/roll calculations taken from this app note:
 // http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1
@@ -194,11 +203,11 @@
     bool objOnRight = true;
     while(objOnRight) {
         mutex.lock();
-        dev.printf("Avoiding Obstacles...\n\r");
+        //pc.printf("Avoiding Obstacles...\n\r");
         //currIR1 = IR1; //must keep scanning IR readers to know when object is cleared
         sonar = mu.getCurrentDistance()*0.00328084;
         currIR2 = IR2;
-        dev.printf("    IR1 Reading         IR2 Reading\n\r         %f          %f\n\r", sonar, currIR2);
+        //pc.printf("    IR1 Reading         IR2 Reading\n\r         %f          %f\n\r", sonar, currIR2);
         mutex.unlock();
         if(currIR2 < 0.7) {
             objOnRight = false;   //if IR2 drops below threshold, obstacle passed. Break out of loop
@@ -311,6 +320,7 @@
     //bluetooth setup
     pc.baud(9600);
     dev.baud(9600);
+
     mu.startUpdates();//start measuring the distance from Sonar
     //wait to recv start command or time delay
     for(int i=0; i<3; i++) {        //temp delay for a few sec
@@ -345,12 +355,13 @@
             //update current IR readings
             //mutex.lock();//IR readings included in mutex since they are shared global variables
             //currIR1 = IR1; //replaced with sonar
+            mu.checkDistance();
             sonar = mu.getCurrentDistance()*0.00328084;
             currIR2 = IR2;
             mutex.lock();         //prevent race conditions in BT dataoutput //changed from BTmutex
-            dev.puts("      Front IR reading    Right IR reading\n\r");     // print IR readings over BT
+            //pc.puts("      Front Sonar reading    Right IR reading\n\r");     // print IR readings over BT
             //dev.printf("        %2f             %2f\n\r", currIR1, currIR2);
-            dev.printf("        %2f             %2f\n\r", sonar, currIR2); //changed
+            //pc.printf("        %2f             %2f\n\r", sonar, currIR2); //changed
             //pc.puts("      Front IR reading    Right IR reading\n\r");     // print IR readings over serial
             //pc.printf("        %2f             %2f\n\r", currIR1, currIR2);
             mutex.unlock(); // changed from BTmutex
@@ -369,6 +380,7 @@
             if (dev.readable()) {
                 mutex.lock();
                 temp = dev.getc();
+                pc.putc(temp);
                 mutex.unlock();
             }
             if(temp == 'M') {
@@ -394,6 +406,7 @@
             if (dev.readable()){
                 mutex.lock();
                 temp = dev.getc();
+                pc.putc(temp);
                 mutex.unlock();
             }
             if(temp == 'A') { // reset command