GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Revision:
63:8fcb1cc55e18
Parent:
62:b73067127fd6
Parent:
61:778e7e30e370
Child:
65:b8f3339b9268
--- a/wag.cpp	Tue Apr 17 16:14:30 2018 +0000
+++ b/wag.cpp	Tue Apr 17 16:15:21 2018 +0000
@@ -85,45 +85,77 @@
 
 void whack(int sensor_no, float * sensor_values) {
     bool sensor_registered = false;
-    int sensor_no = // --> need to specify sensor_no
-
+    int gnoll_sensor_indicator = 0;   // the order of sensor that gnoller's laser points to
+    int whack_sensor_indicaotr = 0;   // the order of sensor that whacker's laser points to
+    int gnoll_no = 0;
+    int led_command = 0;              // led command to display the indicator LED in whacker
+    
+    if (sensor_no == 0) whack_no = 8;
+    if (sensor_no == 8) whack_no = 0;
+    
     // start timer for sensor reading
     t.start();
     
     // keep reading until one of the sensor get laser pointed in
     while (!sensor_registered) {
-        // read value of all gnoll sensor
+        // scan all 16 sensors into sensor_values array
+        ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
+                    
+        // scan all gnoll sensors 
         for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
-            // scan all 16 sensors into sensor_values array
-            ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
-        }
-                    
-        // detect which sensor get lasered on
-        for (int i = 0; i < TGT_SENSOR_QUAN + sensor_no; i++) {
-            if (sensor_values[i] > THRESHOLD_SIGNAL)
+            // detect which gnoll sensor get lasered on
+            if (sensor_values[i + gnoll_no] > THRESHOLD_SIGNAL)
                 sensor_registered = true;
+                gnoll_sensor_indicator = i;
         }
 
         // if (reading sensor timer expired) display error and freeze
         t.stop();
-        if (t.read() > EXPIRED_TIMER) {
+        if (t.read() > WHACK_EXPIRED_TIMER) {
             pc.printf("Error: Reading timer expired. See whack() function.\n");
             while (1);
         } 
     }
 
-    // point whack’s laser to the corresponding sensor on it’s target array
+    // point whack’s laser to the corresponding sensor on whack target array
+    turn_to_target(stp_sensor_pos[sensor_no]);
+    
+    // turn on whacker's laser
+    lzr_on();
+    
+    // scan all 16 sensors into sensor_values array
+    ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
+    
+    t.start(); 
+    sensor_registered = false;
     
-    // activate whacker’s laser
-
-    while (signal from corresponding sensor value > THRESHOLD_SIGNAL) {
-        if (finding sensor timer expired) display error and freeze
+    while (!sensor_registered) {
+        // scan all whack sensors 
+        for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
+            // detect which gnoll sensor get lasered on
+            if (sensor_values[i + sensor_no] > THRESHOLD_SIGNAL)
+                sensor_registered = true;
+                whack_sensor_indicator = i;
+        }
+        
+        // if timer expire display error and freeze
+        t.stop();
+        if (t.read() > WHACK_EXPIRED_TIMER) {
+            pc.printf("Error: Reading timer expired. See whack() function.\n");
+            while(1);
+        }
     }
 
-    activate corresponding indicator
+    // activate corresponding indicator in whack LED row
+    led_command = 0x0500 + led_values[whack_sensor_indicator]);
+    spi_send(as1107, led_command);
 
     // waiting for gnoll laser to turn off to turn off
-    while (gnoll’s laser turned on) {}
+    do {
+        // scan all 16 sensors into sensor_values array
+        ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
+    } while (sensor_values[gnoll_sensor_indicator + gnoll_no] > PT_THRESHOLD);
+    
     // turn off laer gnoll
     lzr_off();
 }