Oluwagbemiga Mabogunje / Mbed 2 deprecated 4180_Lab4_ScaredBot

Dependencies:   HALLFX_ENCODER Motor SDFileSystem mbed-rtos mbed wave_player

Files at this revision

API Documentation at this revision

Comitter:
omabogunje3
Date:
Thu Nov 03 20:50:10 2016 +0000
Commit message:
Code that implements 'ScaredBot' behavior.; Reacts to loud sounds (microphone) by rapidly moving away; Reacts to objects (IR sensor) by turning away; Uses Hall effect motor encoder to achieve more accurate motion

Changed in this revision

HALLFX_ENCODER.lib Show annotated file Show diff for this revision Revisions of this file
Motor.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib 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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
wave_player.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HALLFX_ENCODER.lib	Thu Nov 03 20:50:10 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/electromotivated/code/HALLFX_ENCODER/#f10558519825
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.lib	Thu Nov 03 20:50:10 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/simon/code/Motor/#f265e441bcd9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Thu Nov 03 20:50:10 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#8db0d3b02cec
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 03 20:50:10 2016 +0000
@@ -0,0 +1,173 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "Motor.h"
+#include "HALLFX_ENCODER.h"
+//#include "SDFileSystem.h"
+//#include "wave_player.h"
+
+class microphone
+{
+public :
+    microphone(PinName pin);
+    float read();
+    operator float ();
+private :
+    AnalogIn _pin;
+};
+microphone::microphone (PinName pin):
+    _pin(pin)
+{
+}
+float microphone::read()
+{
+    return _pin.read();
+}
+inline microphone::operator float ()
+{
+    return _pin.read();
+}
+
+
+// Declare Components
+
+//Adafruit MEMs SPW2430 microphone demo - LEDs display audio level
+BusOut myleds(LED1,LED2,LED3,LED4);
+microphone mymicrophone(p16);
+Mutex movement_mutex;
+Motor left_motor(p26,p24,p25); // pwm, fwd, rev  
+Motor right_motor(p21, p23, p22); // pwm, fwd, rev
+HALLFX_ENCODER hall_left(p13);
+HALLFX_ENCODER hall_right(p14);
+AnalogIn IRSound(p17);
+
+
+
+// Robot functions
+/* For debugging IR sensor
+void IRsoundRead() {
+        distance = IRSound.read();
+        if (distance >= 0.0f && distance <= .5f/3.3f) { // Furthest away
+            myleds = 1;
+        } else if (distance >= .5f/3.3f && distance <= .75f/3.3f) {
+            myleds = 3;   
+        } else if (distance >= .75f/3.3f && distance <= 1.25f/3.3f) {
+            myleds = 7; 
+        } else if (distance >= 1.25f/3.3f ) { // closest
+            myleds = 15;
+            tooClose = 1;
+        }
+} 
+*/
+// forward
+void forward( float vol ){
+    long diff = hall_left.read()-hall_right.read();
+    float k = 0.8;
+    if (diff < 0) { // left has moved less than right
+        left_motor.speed(vol);
+        right_motor.speed(k*vol);
+    }else if (diff > 0)  { // right has moved less than left
+        left_motor.speed(k*vol);
+        right_motor.speed(vol); 
+    } else {
+        left_motor.speed(vol);
+        right_motor.speed(vol);
+    }
+}
+
+// spin
+void panicSpin() {
+    //Spin really scaredly
+    left_motor.speed(1);
+    right_motor.speed(-1);
+    Thread::wait(2000);
+    
+    //Try to avoid ojbect
+    left_motor.speed(0.5);
+    right_motor.speed(-0.5);    
+    while(IRSound.read()>= 1.25f/3.3f ) {};
+    Thread::wait(100);
+    forward(0);    
+}
+
+
+// Listen mic Function (Continuous, thread)
+float listen() {
+    //read in sample value using AC coupled input option
+    float sample = mymicrophone;
+    
+    int a = int(abs((sample-0.5)*300.0));
+    if (a < 15 ) {
+          myleds = a;
+    } else {
+         myleds = 12;
+    }
+    //myleds = int(abs((sample-0.5)*300.0)); //scale to around 15 for LEDs
+    return abs((sample-0.5)*300.0/15.0);
+}
+
+// IRsound Function (Continuous, thread)
+// When run everything else supspended
+void ActScared(void)
+{
+    Thread::wait(1000);
+    float loudnessBuffer[10] = {0,0,0,0,0,0,0,0,0,0};
+    float volume = 0, currentVal = 0;
+    int i = 0;
+    //float speed = 0;
+    while(1) {
+        currentVal = listen();
+        volume += (currentVal-loudnessBuffer[i])/10;
+        loudnessBuffer[i] = currentVal;
+        i++;
+        if (i==10) {i=0;}
+        if(2.0*volume>0.1){
+            movement_mutex.lock();
+            forward(1);
+            movement_mutex.unlock();
+        } else {
+            hall_left.reset(); hall_right.reset();
+            movement_mutex.lock();
+            forward(0.1);
+            movement_mutex.unlock();
+        }
+        Thread::wait(100);
+        /**/
+    }
+}
+
+void AvoidObject(void)
+{
+    Thread::wait(500);
+    float closenessBuffer[10] = {0,0,0,0,0,0,0,0,0,0};
+    float closeness = 0, currentVal = 0;
+    int i = 0;
+    while(1) {
+        // average 10 values over 1 seconds.
+        currentVal = IRSound.read();
+        closeness += (currentVal-closenessBuffer[i])/10;
+        closenessBuffer[i] = currentVal;
+        i++;
+        if (i==10) {i=0;}
+        //float distance = IRSound.read();
+        movement_mutex.lock();
+        if (closeness >= 1.0f/3.3f ) { // close object detected
+            for (int j = 0; j < 10; j++) {
+              closenessBuffer[j] = 0;  
+            }
+            closeness = 0;
+            forward(0);
+            Thread::wait(250);            
+            panicSpin();
+        }
+        movement_mutex.unlock();
+        Thread::wait(50);
+    }
+}
+
+int main()
+{
+    Thread t2(ActScared);
+    while(1) {       
+        AvoidObject();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Nov 03 20:50:10 2016 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#3da5f554d8bf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Nov 03 20:50:10 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Thu Nov 03 20:50:10 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad