Andres Rodriguez / Mbed 2 deprecated Collision_Avoidance_Robot-Controller

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE

Files at this revision

API Documentation at this revision

Comitter:
Andres013
Date:
Fri Apr 26 14:53:26 2019 +0000
Parent:
0:a0902c8e1f7b
Commit message:
(ACTUAL) FINAL COMMIT

Changed in this revision

Speaker.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Fri Apr 26 14:53:26 2019 +0000
@@ -0,0 +1,54 @@
+#include "rtos.h"
+
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+// precompute 32 sample points on one sine wave cycle
+// used for continuous sine wave output later
+        for(int k=0; k<32; k++) {
+            Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0));
+            // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg
+        }
+
+    }
+// class method to play a note based on AnalogOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        // scale samples using current volume level arg
+        for(int k=0; k<32; k++) {
+            Analog_scaled_data[k] = Analog_out_data[k] * volume;
+        }
+        // reset to start of sample array
+        i=0;
+        // turn on timer interrupts to start sine wave output
+        Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0));
+        // play note for specified time
+        Thread::wait(duration);
+        // turns off timer interrupts
+        Sample_Period.detach();
+        // sets output to mid range - analog zero
+        this->_pin.write_u16(32768);
+
+    }
+private:
+// sets up specified pin for analog using AnalogOut class
+    AnalogOut _pin;
+    // set up a timer to be used for sample rate interrupts
+    Ticker Sample_Period;
+
+    //variables used by interrupt routine and PlayNote
+    volatile int i;
+    short unsigned Analog_out_data[32];
+    short unsigned Analog_scaled_data[32];
+
+// Interrupt routine
+// used to output next analog sample whenever a timer interrupt occurs
+    void Sample_timer_interrupt(void) {
+        // send next analog sample out to D to A
+        this->_pin.write_u16(Analog_scaled_data[i]);
+        // increment pointer and wrap around back to 0 at 32
+        i = (i+1) & 0x01F;
+    }
+};
+
--- a/main.cpp	Fri Apr 26 14:24:41 2019 +0000
+++ b/main.cpp	Fri Apr 26 14:53:26 2019 +0000
@@ -1,25 +1,20 @@
 #include "mbed.h"
 #include "uLCD_4DGL.h"
-#include "rtos.h"
 #include "stdio.h"
 #include <cstdlib>
 #include "math.h"
-
-//#define MIN_DIST = ?????
-
-Mutex uLCD_mutex;
+#include "Speaker.h"
 
 DigitalOut myled(LED1);
 
-Serial hc05(p9, p10); //master HC05 bluetooth, *CHECK PINS*
+Serial hc05(p9, p10); //master HC05 bluetooth
 Serial pc(USBTX, USBRX); //serial pc connection for testing, *ONLY USE WHEN TESTING*
 
-uLCD_4DGL uLCD(p28, p27, p11); //uLCD for lidar imaging, *CHECK PINS*
-AnalogOut speaker(p18); //speaker for collision detect noise
+uLCD_4DGL uLCD(p28, p27, p11); //uLCD for lidar imaging
+Speaker mySpeaker(p18); //speaker for collision detect noise
 
-AnalogIn horz(p16); //left/right input from joystick, *CHECK PINS*
-AnalogIn forward(p15); //forward input from joystick, *CHECK PINS*
-//DigitalIn stop(p17); //stop input from joystick, *CHECK PINS* 
+AnalogIn horz(p16); //left/right input from joystick
+AnalogIn forward(p15); //forward input from joystick
 
 volatile int mode = 0; //drive mode = 0 or collision detect mode = 1
 
@@ -125,11 +120,12 @@
             if(good_buff){ mode = 1; }                              /* SET MODE */
                             
 
-            if (mode) {
+            if (mode) {                         /* COLLISION AVOIDANCE PROTOCOL */
                 /* pc.printf("Collision Avoidance Engaged\r\n"); */
                 uLCD.cls();                                /* INIT RADAR SCREEN */
                 uLCD.filled_circle(64, 64, 5, (RED+GREEN)); 
-                Thread::wait(2*1000);
+                mySpeaker.PlayNote(440, 1000, 0.8);                /* PLAY TONE */
+                Thread::wait(1000);                     /* WAIT AN EXTRA SECOND */
                 ack_timer.start();
                 int i = 0;
                 while(i < 60) {
@@ -178,9 +174,7 @@
                         int y = (int) (64.0f + (collision_dist * 60 * sin(collision_angle)));
     
                         if (x >= 0 && y >= 0) {
-                            uLCD_mutex.lock();
                             uLCD.filled_circle(x, y, 2, GREEN);
-                            uLCD_mutex.unlock();
                         }
                     } else if(ack_timer.read_ms() > 2000) {
                         hc05.printf("_ack_%02d", (i-1));         /* ACKNOWLEDGE */