Kristyn DiGiovanni / Mbed 2 deprecated 4180_FinalProject_DeploymentVersion

Dependencies:   mbed mbed-rtos PinDetect

Files at this revision

API Documentation at this revision

Comitter:
kristyn1230
Date:
Mon Dec 02 17:08:13 2019 +0000
Parent:
0:c18a60fc6636
Commit message:
Deployment Version of the Project Test Drive code which is a driver awareness application that alerts and measures driver attention.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Nov 28 03:53:53 2019 +0000
+++ b/main.cpp	Mon Dec 02 17:08:13 2019 +0000
@@ -2,36 +2,42 @@
 #include "rtos.h"
 #include "PinDetect.h"
 
-
+// Hardware devices
 RawSerial pc(USBTX, USBRX);
 DigitalOut myled(p21);
 DigitalOut myled2(p22);
 PinDetect pb(p26, PullDown);
 PinDetect pb2(p27, PullDown);
 PwmOut speaker(p25);
+
+// Constants and Timers
 Timer t1;
 Timer t2;
-const int TEN_SEC_DEL = 5000;  // Change back to 10 seconds (changed for testing purposes)
+const int FIFTEEN_SEC_DEL = 15000;
+const int MIN_DEL = 3000;
 const int WINDOW_SIZE = 5;
 const float INCORRECT_PENALTY = 3.00;
 const float WEIGHT = 1.25; 
 
+
+// Pushbutton flags and inputs
 volatile float rxn_time = 0;
 volatile int pb1_asserted = 0;
 volatile int pb2_asserted = 0;
 volatile int pb1_chosen = 0;
 volatile int pb2_chosen = 0;
+
+// Reading windows, counters, and accuracy calculations
 volatile int total_count = 0;
 volatile int incorrect_count = 0;
 volatile int interval = 0;
 volatile bool timeout = false;
 volatile float baseline_avg = 0;
 volatile float current_avg = 0;
-volatile float readings[WINDOW_SIZE]; // Change back to 10 later
+volatile float readings[WINDOW_SIZE]; 
 volatile int step = 0;
 volatile bool calc_baseline = false;
 
-
 void clear_timers() {
     t1.stop();
     t1.reset();
@@ -39,6 +45,7 @@
     t2.reset();
 }
 
+// Main LED control thread
 void flash(void const *args) {
     float timeout_time = 0;
     while(1){
@@ -64,25 +71,26 @@
             pb1_chosen = 1;
             myled = 1;
             t1.start();
-            Thread::wait(1000);
+            Thread::wait(2000);
             myled = 0;
         } else {
             pb2_chosen = 1;
             myled2 = 1;
             t2.start();
-            Thread::wait(1000);
+            Thread::wait(2000);
             myled2 = 0;
         }
         
         float weight = rand() / (float) RAND_MAX;
-        timeout_time = (int) (TEN_SEC_DEL * weight);
-        Thread::wait(timeout_time);
+        timeout_time = (int) (FIFTEEN_SEC_DEL * weight);
+        Thread::wait(MIN_DEL + timeout_time);
         
         total_count++;
         interval++;
     }
 }
 
+// Pushbutton interrupts to read in a reaction time reading
 void button_ready(void) {
     pb1_asserted = 1;
     rxn_time = t1.read();
@@ -109,6 +117,7 @@
     t2.reset();
 }
 
+// Alarm and sound control thread
 void sound(void const* args) {
     bool playOnce = true;
     while(1) {
@@ -139,25 +148,29 @@
     
 int main() {
 
+    // Messages to alert the user that the system is starting
     pc.printf("System starting in ...");
     for(int i = 1; i <= 5; i++) {  
         pc.printf("%d...", i);
         wait(1);
     }
     pc.printf("Go!\n\r");
+
+    // Pushbutton setup
     pb.attach_deasserted(&button_ready);
     pb.setSampleFrequency();
     pb2.attach_deasserted(&button_ready2);
     pb2.setSampleFrequency();
 
+    // Start led and sound control threads
     Thread thread1(flash);
     Thread thread2(sound);
 
     float accuracy;
     pc.printf("Starting Training Phase\n\r");
 
+    // Continuously monitor the user's reaction 10x a second
     while(1) {
-        // Change back to 10 later
         if (calc_baseline && baseline_avg == 0) {
             float sum = 0;
             for (int j = 0; j < WINDOW_SIZE; j++)
@@ -209,7 +222,6 @@
                accuracy = accuracy*100;
                pc.printf("--- TotalCount: %d IncorrectCount: %d Current Accuracy: %.2f%% ---\n\r", total_count, incorrect_count, accuracy);
                pc.printf("\n\r");
-               //pc.printf("Current Accuracy: %.2f%%\n\r", accuracy);
                interval = 0;
         }