GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wag.cpp Source File

wag.cpp

00001 /******************************************************************************
00002 * EECS 397
00003 *
00004 * Assignment Name: Lab 7: WaG
00005 *
00006 * Authors: Sam Morrison and Phong Nguyen
00007 * File name: wag.cpp
00008 * Purpose: Wag functions
00009 *
00010 * Created: 04/12/2018
00011 * Last Modified: 04/12/2018
00012 *
00013 ******************************************************************************/
00014 
00015 #include "mbed.h"
00016 #include "io_pins.h"
00017 #include "wag.h"
00018 #include "spi.h"
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <time.h>
00023 
00024 Timer t;
00025 extern spi_cfg as1107;
00026 extern int stp_sensor_pos[TGT_SENSOR_QUAN];
00027 
00028 int led_values[8] = {1, 2, 4, 8, 16, 32, 64, 128};
00029 
00030 /*
00031  * void gnoll(int sensor_no, float * sensor_values)
00032  * Description: function for gnoller
00033  *
00034  * Inputs:
00035  *      Parameters:
00036  *          int sensor_no: station indicator 
00037  *          float * sensor_values: a float array that stores 16 sensor values
00038  *      Globals:
00039  *
00040  * Outputs:
00041  *      Returns: void
00042 */
00043 void gnoll(int sensor_no, float * sensor_values)
00044 {
00045     int hit = 0;
00046     int miss = 0;
00047     int a_num;
00048     int led_command;
00049     int msec;
00050 
00051     int whacker_no = 0;
00052 
00053     if (sensor_no == 0)
00054         whacker_no = 8;
00055     
00056     
00057     srand(time(NULL));
00058     // run through 15 volleys 
00059     for (int i = 0; i < 15; i++) {
00060         pc.printf("Round %d\n", i + 1);
00061         a_num = rand() % 8; // create random number 0-7
00062         pc.printf("Random number: %d. Stepper position: %d.", a_num, stp_sensor_pos[a_num]);
00063         turn_to_target(stp_sensor_pos[a_num]); // turn motor to random target
00064         lzr_on(); // turn laser on
00065         wait(LASER_DELAY); // wait for laser to activate
00066 
00067         ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
00068         wait(LED_DELAY);
00069         
00070         // confirm that the sensor is high
00071         if (sensor_values[sensor_no + a_num] * 3.3 > PTTHRESH) { 
00072             led_command = 0x0500 + led_values[a_num]; // create SPI command for LED
00073             //pc.printf("led_command: %d\n", led_command);
00074             spi_send(as1107, led_command); // light up LED
00075             //pc.printf("done sent\n");
00076             pc.printf("led_command: %d\n", led_command);
00077             wait(LED_DELAY);
00078             // NOTE: must put a wait() here or else the led won't light up
00079         } 
00080         // laser failed to hit sensor
00081         else {
00082             pc.printf("sensor value: %f\n" ,sensor_values[sensor_no + a_num] * 3.3);
00083             pc.printf("Error: sensor not activated. Shutting down.\n");
00084             while(1);
00085         }
00086 
00087         // start a timer to wait for whacker to hit target
00088         clock_t start = clock();
00089         pc.printf("Waiting for whacker...\n");
00090 
00091         // scan sensors for timer duration or until whacker hits
00092         do {
00093             ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
00094             clock_t difference = clock() - start;
00095             msec = difference * 1000 / CLOCKS_PER_SEC;
00096             //pc.printf("Time elapsed: %d. Time limit: %d\n", msec, VOLLEY_DELAY);
00097         } while (msec < VOLLEY_DELAY and sensor_values[whacker_no + a_num] * 3.3 < PTTHRESH); // check if timer expired or if sensor hit
00098 
00099         // whacker hit target within time
00100         if (sensor_values[whacker_no + a_num] * 3.3 > PTTHRESH) {
00101             hit++; // increment hit count
00102             pc.printf("Hit\n");
00103             //while(sensor_values[whacker_no + a_num] * 3.3 > PTTHRESH)
00104               //  ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
00105         }
00106 
00107         else {
00108             // go through all whacker sensors
00109             for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { 
00110                 // check if wrong sensor hit
00111                 if (sensor_values[i + whacker_no] * 3.3 > PTTHRESH and j != a_num) { 
00112                     pc.printf("Sensor: %d. Sensor value:%f\n", j, sensor_values[i + whacker_no] * 3.3);
00113                     pc.printf("Wrong sensor hit.\n");
00114                     i = 15; // ends volley
00115                     ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
00116                     while(sensor_values[i + whacker_no] * 3.3 > PTTHRESH)
00117                         ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
00118                 }
00119             }
00120             // log miss regardless
00121             pc.printf("Miss\n");
00122             miss++; // increment miss count
00123         }
00124         // turn laser off and update scoreboard
00125         lzr_off();
00126         spi_send(as1107, 0x0500);
00127         pc.printf("Hit: %d. Miss: %d.", hit, miss);
00128         update_score(hit, miss);
00129     }
00130 }
00131 
00132 /*
00133  * void whack(int sensor_no, float * sensor_values)
00134  * Description: function for whacker
00135  *
00136  * Inputs:
00137  *      Parameters:
00138  *          int sensor_no: station indicator 
00139  *          float * sensor_values: a float array that stores 16 sensor values
00140  *      Globals:
00141  *
00142  * Outputs:
00143  *      Returns: void
00144 */
00145 void whack(int sensor_no, float * sensor_values)
00146 {
00147     bool sensor_registered = false;
00148     int gnoll_sensor_indicator = 0;   // the order of sensor that gnoller's laser points to
00149     int whack_sensor_indicator = 0;   // the order of sensor that whacker's laser points to
00150     int gnoll_no = 0;
00151     int led_command = 0;              // led command to display the indicator LED in whacker
00152     int msec = 0;
00153     clock_t start;
00154     clock_t difference;
00155 
00156     if (sensor_no == 0) gnoll_no = 8;
00157     if (sensor_no == 8) gnoll_no = 0;
00158 
00159     // start timer for sensor reading
00160     start = clock();
00161 
00162     for (int i = 0; i < 15; i++) {
00163         pc.printf("i: %d\n", i);
00164         sensor_registered = false;
00165         
00166         // keep reading until one of the sensor get laser pointed in
00167         while (!sensor_registered) {
00168             // scan all 16 sensors into sensor_values array
00169             ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
00170     
00171             // scan all gnoll sensors
00172             for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
00173                 // detect which gnoll sensor get lasered on
00174                 if (sensor_values[i + gnoll_no] * 3.3f > PTTHRESH) {
00175                     sensor_registered = true;
00176                     gnoll_sensor_indicator = i;
00177                     pc.printf("gnoll_sensor_indicator: %d\n", gnoll_sensor_indicator);
00178                 }
00179             }
00180     
00181             // if (reading sensor timer expired) display error and freeze
00182             difference = clock() - start;
00183             msec = difference * 1000 / CLOCKS_PER_SEC;
00184             if (msec > WHACK_EXPIRED_TIMER) {
00185                 pc.printf("Error: Reading timer expired. See whack() function.\n");
00186                 while (1);
00187             }
00188         }
00189         
00190         pc.printf("stp_sensor_pos: %d\n", stp_sensor_pos[sensor_no + gnoll_sensor_indicator]);
00191         // point whack’s laser to the corresponding sensor on whack target array
00192         turn_to_target(stp_sensor_pos[sensor_no + gnoll_sensor_indicator]);
00193     
00194         // turn on whacker's laser
00195         lzr_on();
00196     
00197         start = clock();
00198         sensor_registered = false;
00199         
00200         // sense which sensor the whack laser point to and turn it on
00201         while (!sensor_registered) {    
00202             // scan all 16 sensors into sensor_values array
00203             ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
00204     
00205             for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
00206                 // detect which gnoll sensor get lasered on
00207                 if (sensor_values[i + sensor_no] * 3.3f > PTTHRESH) {
00208                     sensor_registered = true;
00209                     whack_sensor_indicator = i;
00210                     pc.printf("whack_sensor_indicator: %d\n", whack_sensor_indicator);
00211                 }
00212             }
00213     
00214             // if timer expire display error and freeze
00215             difference = clock() - start;
00216             msec = difference * 1000 / CLOCKS_PER_SEC;
00217             if (msec > WHACK_EXPIRED_TIMER) {
00218                 pc.printf("Error: Reading timer expired. See whack() function.\n");
00219                 while(1);
00220             }
00221         }
00222     
00223         // activate corresponding indicator in whack LED row
00224         led_command = 0x0500 + led_values[whack_sensor_indicator];
00225         pc.printf("led_command: %d\n", led_command);
00226         spi_send(as1107, led_command);
00227         wait(LED_DELAY);
00228     
00229         // waiting for gnoll laser to turn off to turn off
00230         do {
00231             // scan all 16 sensors into sensor_values array
00232             ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
00233         } while (sensor_values[gnoll_sensor_indicator + gnoll_no] * 3.3f > PTTHRESH);
00234     
00235         // turn off laer gnoll
00236         lzr_off();
00237         
00238         // turn off LED display
00239         spi_send(as1107, 0x0500);
00240     }
00241 }
00242 
00243 /*
00244  * void update_score(int hit, int miss)
00245  * Description: updates gnoller scoreboard
00246  *
00247  * Inputs:
00248  *      Parameters:
00249  *          int hit: whacker hit count
00250  *          int miss: whacker miss count
00251  *      Globals:
00252  *
00253  * Outputs:
00254  *      Returns: void
00255 */
00256 void update_score(int hit, int miss)
00257 {
00258     int d1, d2, d3, d4 = 0;
00259     if ( hit < 10)
00260         d1 = hit;
00261     else {
00262         d1 = hit % 10;
00263         d2 = (hit - d1)/10;
00264     }
00265     if ( miss < 10)
00266         d3 = miss;
00267     else {
00268         d3 = miss % 10;
00269         d4 = (miss - d3)/10;
00270     }
00271     pc.printf("d1: %d -- d3: %d\n", d1, d3);
00272     spi_send(as1107, 0x0100 + d1);
00273     spi_send(as1107, 0x0200 + d2);
00274     spi_send(as1107, 0x0300 + d3);
00275     spi_send(as1107, 0x0400 + d4);
00276 }