GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Revision:
62:b73067127fd6
Parent:
60:604c10531f58
Child:
63:8fcb1cc55e18
diff -r 604c10531f58 -r b73067127fd6 wag.cpp
--- a/wag.cpp	Tue Apr 17 12:47:32 2018 +0000
+++ b/wag.cpp	Tue Apr 17 16:14:30 2018 +0000
@@ -21,31 +21,65 @@
 #include <timer.h>
 
 extern Timer t;
+int led_values[8] = {1, 2, 4, 8, 16, 32, 64, 128};
 
-void gnoll() {
-    int hit, miss;
+void gnoll(int sensor_no, float sensor_values[]) {
+    int hit, miss = 0;
     int a_num;
     int led_command;
-    int sensor_no = 0 //change to match station
+    int whacker_no = 0;
+    
+    if (sensor_no == 0)
+        whacker_no = 8;
 
     for (int i = 0; i < 15; i++) {
-        a_num = rand() % 8;
-        turn_to_target(stp_sensor_pos[a_num]);
-        lzr_on();
-        wait(LASER_DELAY);
-        if (sensor_values[sensor_no + a_num] > PTHRESH) {
-            led_command = 0x0500 + a_num;
-            spi_send(as1107, led_command);
-
+        pc.printf("Round %d\n", i + 1);
+        a_num = rand() % 8; // create random number 0-7
+        turn_to_target(stp_sensor_pos[a_num]); // turn motor to random target
+        lzr_on(); // turn laser on
+        wait(LASER_DELAY); // wait for laser to activate
+        
+        ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
+        
+        if (sensor_values[sensor_no + a_num] > PTHRESH) { // confirm that the sensor is high
+            led_command = 0x0500 + led_values[a_num]; // create SPI command for LED
+            spi_send(as1107, led_command); // light up LED
+        }
+        else {
+            pc.printf("Error: sensor not activated. Shutting down.\n");
+            while(1);
         }
         
         clock_t start = clock();
         clock_t finish = clock();
+        pc.printf("Waiting for whacker...\n");
         
         do {
-            // check for whacker sensors
-            finish = clock();
-        } while (finish - start < VOLLEY_DELAY)
+            finish = clock(); // update timer
+            ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
+        } while (finish - start < VOLLEY_DELAY or sensor_values[whacker_no + a_num] < PTHRESH); // check if timer expired or if sensor hit
+        
+        if (sensor_values[whacker_no + a_num] > PTHRESH) {
+            hit++; // increment hit count
+            pc.printf("Hit\n");
+            while(sensor_values[whacker_no + a_num] > PTHRESH)
+                ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
+        }
+        
+        else {
+            for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { // go through all whacker sensors
+                if (sensor_values[i + whacker_no] > PTHRESH and j != a_num) { // check if wrong sensor hit
+                    pc.printf("Wrong sensor hit.\n");
+                    i = 15; // ends volley
+                    ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors 
+                    while(sensor_values[i + whacker_no] > PTHRESH)
+                        ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
+                }
+            }
+            pc.printf("Miss\n");
+            miss++; // increment miss count 
+        }
+        update_score(hit, miss);
     }
 }
 
@@ -92,4 +126,24 @@
     while (gnoll’s laser turned on) {}
     // turn off laer gnoll
     lzr_off();
+}
+
+void update_score(int hit, int miss) {
+    int d1, d2, d3, d4 = 0;
+    if ( hit < 10)
+        d1 = hit;
+    else {
+        d1 = hit % 10;
+        d2 = (hit - d1)/10;
+    }
+    if ( miss < 10)
+        d3 = miss;
+    else {
+        d3 = miss % 10;
+        d4 = (miss - d3)/10;
+    }
+    spi_send(as1107, 0x0100 + d1);
+    spi_send(as1107, 0x0200 + d2);
+    spi_send(as1107, 0x0300 + d3);
+    spi_send(as1107, 0x0400 + d4);
 }
\ No newline at end of file