GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Revision:
76:7514ca07b45d
Parent:
75:49e69031799c
Child:
78:84ce90a76594
--- a/wag.cpp	Thu Apr 26 15:33:35 2018 +0000
+++ b/wag.cpp	Thu Apr 26 15:44:46 2018 +0000
@@ -27,7 +27,19 @@
 
 int led_values[8] = {1, 2, 4, 8, 16, 32, 64, 128};
 
-
+/*
+ * void gnoll(int sensor_no, float * sensor_values)
+ * Description: function for gnoller
+ *
+ * Inputs:
+ *      Parameters:
+ *          int sensor_no: station indicator 
+ *          float * sensor_values: a float array that stores 16 sensor values
+ *      Globals:
+ *
+ * Outputs:
+ *      Returns: void
+*/
 void gnoll(int sensor_no, float * sensor_values)
 {
     int hit = 0;
@@ -43,6 +55,7 @@
     
     
     srand(time(NULL));
+    // run through 15 volleys 
     for (int i = 0; i < 15; i++) {
         pc.printf("Round %d\n", i + 1);
         a_num = rand() % 8; // create random number 0-7
@@ -54,22 +67,27 @@
         ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
         wait(LED_DELAY);
         
-        if (sensor_values[sensor_no + a_num] * 3.3 > PTTHRESH) { // confirm that the sensor is high
+        // confirm that the sensor is high
+        if (sensor_values[sensor_no + a_num] * 3.3 > PTTHRESH) { 
             led_command = 0x0500 + led_values[a_num]; // create SPI command for LED
             //pc.printf("led_command: %d\n", led_command);
             spi_send(as1107, led_command); // light up LED
             //pc.printf("done sent\n");
             wait(LED_DELAY);
             // NOTE: must put a wait() here or else the led won't light up
-        } else {
+        } 
+        // laser failed to hit sensor
+        else {
             pc.printf("sensor value: %f\n" ,sensor_values[sensor_no + a_num] * 3.3);
             pc.printf("Error: sensor not activated. Shutting down.\n");
             while(1);
         }
 
+        // start a timer to wait for whacker to hit target
         clock_t start = clock();
         pc.printf("Waiting for whacker...\n");
 
+        // scan sensors for timer duration or until whacker hits
         do {
             ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
             clock_t difference = clock() - start;
@@ -77,6 +95,7 @@
             //pc.printf("Time elapsed: %d. Time limit: %d\n", msec, VOLLEY_DELAY);
         } while (msec < VOLLEY_DELAY and sensor_values[whacker_no + a_num] * 3.3 < PTTHRESH); // check if timer expired or if sensor hit
 
+        // whacker hit target within time
         if (sensor_values[whacker_no + a_num] * 3.3 > PTTHRESH) {
             hit++; // increment hit count
             pc.printf("Hit\n");
@@ -85,8 +104,10 @@
         }
 
         else {
-            for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { // go through all whacker sensors
-                if (sensor_values[i + whacker_no] * 3.3 > PTTHRESH and j != a_num) { // check if wrong sensor hit
+            // go through all whacker sensors
+            for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { 
+                // check if wrong sensor hit
+                if (sensor_values[i + whacker_no] * 3.3 > PTTHRESH and j != a_num) { 
                     pc.printf("Sensor: %d. Sensor value:%f\n", j, sensor_values[i + whacker_no] * 3.3);
                     pc.printf("Wrong sensor hit.\n");
                     i = 15; // ends volley
@@ -95,9 +116,11 @@
                         ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
                 }
             }
+            // log miss regardless
             pc.printf("Miss\n");
             miss++; // increment miss count
         }
+        // turn laser off and update scoreboard
         lzr_off();
         spi_send(as1107, 0x0500);
         pc.printf("Hit: %d. Miss: %d.", hit, miss);
@@ -111,8 +134,8 @@
  *
  * Inputs:
  *      Parameters:
- *          int sensor_no: the
- *          float * sensor_values: an float array that stores 16 sensor values
+ *          int sensor_no: station indicator 
+ *          float * sensor_values: a float array that stores 16 sensor values
  *      Globals:
  *
  * Outputs:
@@ -213,6 +236,19 @@
     }
 }
 
+/*
+ * void update_score(int hit, int miss)
+ * Description: updates gnoller scoreboard
+ *
+ * Inputs:
+ *      Parameters:
+ *          int hit: whacker hit count
+ *          int miss: whacker miss count
+ *      Globals:
+ *
+ * Outputs:
+ *      Returns: void
+*/
 void update_score(int hit, int miss)
 {
     int d1, d2, d3, d4 = 0;