GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Committer:
spm71
Date:
Thu Apr 19 15:49:06 2018 +0000
Revision:
68:9245d6ce176e
Parent:
67:6dffd5c82eb6
Child:
69:1b7271bd4a75
Gnoll timer update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
spm71 57:aef01bd9b3be 1 /******************************************************************************
spm71 57:aef01bd9b3be 2 * EECS 397
spm71 57:aef01bd9b3be 3 *
spm71 57:aef01bd9b3be 4 * Assignment Name: Lab 7: WaG
spm71 57:aef01bd9b3be 5 *
spm71 57:aef01bd9b3be 6 * Authors: Sam Morrison and Phong Nguyen
spm71 57:aef01bd9b3be 7 * File name: wag.cpp
spm71 57:aef01bd9b3be 8 * Purpose: Wag functions
spm71 57:aef01bd9b3be 9 *
spm71 57:aef01bd9b3be 10 * Created: 04/12/2018
spm71 57:aef01bd9b3be 11 * Last Modified: 04/12/2018
spm71 57:aef01bd9b3be 12 *
spm71 57:aef01bd9b3be 13 ******************************************************************************/
spm71 57:aef01bd9b3be 14
spm71 57:aef01bd9b3be 15 #include "mbed.h"
spm71 57:aef01bd9b3be 16 #include "io_pins.h"
spm71 57:aef01bd9b3be 17 #include "wag.h"
phn10 65:b8f3339b9268 18 #include "spi.h"
spm71 57:aef01bd9b3be 19 #include <stdlib.h>
spm71 57:aef01bd9b3be 20 #include <stdio.h>
phn10 59:6b3a52d87465 21 #include <string.h>
phn10 59:6b3a52d87465 22
spm71 67:6dffd5c82eb6 23 Timer t;
phn10 65:b8f3339b9268 24 extern spi_cfg as1107;
phn10 65:b8f3339b9268 25 extern int stp_sensor_pos[TGT_SENSOR_QUAN];
phn10 59:6b3a52d87465 26
spm71 62:b73067127fd6 27 int led_values[8] = {1, 2, 4, 8, 16, 32, 64, 128};
phn10 59:6b3a52d87465 28
phn10 65:b8f3339b9268 29
spm71 66:70efa618c378 30 void gnoll(int sensor_no, float * sensor_values) {
spm71 62:b73067127fd6 31 int hit, miss = 0;
spm71 58:69f9a4607a16 32 int a_num;
spm71 58:69f9a4607a16 33 int led_command;
spm71 68:9245d6ce176e 34 int msec;
phn10 65:b8f3339b9268 35
spm71 62:b73067127fd6 36 int whacker_no = 0;
spm71 62:b73067127fd6 37
spm71 62:b73067127fd6 38 if (sensor_no == 0)
spm71 62:b73067127fd6 39 whacker_no = 8;
phn10 60:604c10531f58 40
phn10 60:604c10531f58 41
spm71 58:69f9a4607a16 42 for (int i = 0; i < 15; i++) {
spm71 62:b73067127fd6 43 pc.printf("Round %d\n", i + 1);
spm71 62:b73067127fd6 44 a_num = rand() % 8; // create random number 0-7
spm71 62:b73067127fd6 45 turn_to_target(stp_sensor_pos[a_num]); // turn motor to random target
spm71 62:b73067127fd6 46 lzr_on(); // turn laser on
spm71 62:b73067127fd6 47 wait(LASER_DELAY); // wait for laser to activate
spm71 62:b73067127fd6 48
spm71 62:b73067127fd6 49 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 62:b73067127fd6 50
spm71 66:70efa618c378 51 if (sensor_values[sensor_no + a_num] > PTTHRESH) { // confirm that the sensor is high
spm71 62:b73067127fd6 52 led_command = 0x0500 + led_values[a_num]; // create SPI command for LED
spm71 62:b73067127fd6 53 spi_send(as1107, led_command); // light up LED
spm71 62:b73067127fd6 54 }
spm71 62:b73067127fd6 55 else {
spm71 62:b73067127fd6 56 pc.printf("Error: sensor not activated. Shutting down.\n");
spm71 62:b73067127fd6 57 while(1);
spm71 58:69f9a4607a16 58 }
spm71 58:69f9a4607a16 59
spm71 58:69f9a4607a16 60 clock_t start = clock();
spm71 62:b73067127fd6 61 pc.printf("Waiting for whacker...\n");
phn10 60:604c10531f58 62
spm71 58:69f9a4607a16 63 do {
spm71 62:b73067127fd6 64 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 68:9245d6ce176e 65 clock_t difference = clock() - start;
spm71 68:9245d6ce176e 66 msec = difference * 1000 / CLOCKS_PER_SEC;
spm71 68:9245d6ce176e 67 } while (msec < VOLLEY_DELAY or sensor_values[whacker_no + a_num] < PTTHRESH); // check if timer expired or if sensor hit
spm71 62:b73067127fd6 68
spm71 66:70efa618c378 69 if (sensor_values[whacker_no + a_num] > PTTHRESH) {
spm71 62:b73067127fd6 70 hit++; // increment hit count
spm71 62:b73067127fd6 71 pc.printf("Hit\n");
spm71 66:70efa618c378 72 while(sensor_values[whacker_no + a_num] > PTTHRESH)
spm71 62:b73067127fd6 73 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
spm71 62:b73067127fd6 74 }
spm71 62:b73067127fd6 75
spm71 62:b73067127fd6 76 else {
spm71 62:b73067127fd6 77 for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { // go through all whacker sensors
spm71 66:70efa618c378 78 if (sensor_values[i + whacker_no] > PTTHRESH and j != a_num) { // check if wrong sensor hit
spm71 62:b73067127fd6 79 pc.printf("Wrong sensor hit.\n");
spm71 62:b73067127fd6 80 i = 15; // ends volley
spm71 62:b73067127fd6 81 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 66:70efa618c378 82 while(sensor_values[i + whacker_no] > PTTHRESH)
spm71 62:b73067127fd6 83 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
spm71 62:b73067127fd6 84 }
spm71 62:b73067127fd6 85 }
spm71 62:b73067127fd6 86 pc.printf("Miss\n");
spm71 62:b73067127fd6 87 miss++; // increment miss count
spm71 62:b73067127fd6 88 }
spm71 62:b73067127fd6 89 update_score(hit, miss);
spm71 58:69f9a4607a16 90 }
phn10 60:604c10531f58 91 }
phn10 60:604c10531f58 92
phn10 64:72f7f5757f0d 93 /*
phn10 64:72f7f5757f0d 94 * void whack(int sensor_no, float * sensor_values)
phn10 64:72f7f5757f0d 95 * Description: function for whacker
phn10 64:72f7f5757f0d 96 *
phn10 64:72f7f5757f0d 97 * Inputs:
phn10 64:72f7f5757f0d 98 * Parameters:
phn10 64:72f7f5757f0d 99 * int sensor_no: the
phn10 64:72f7f5757f0d 100 * float * sensor_values: an float array that stores 16 sensor values
phn10 64:72f7f5757f0d 101 * Globals:
phn10 64:72f7f5757f0d 102 *
phn10 64:72f7f5757f0d 103 * Outputs:
phn10 64:72f7f5757f0d 104 * Returns: void
phn10 64:72f7f5757f0d 105 */
phn10 59:6b3a52d87465 106 void whack(int sensor_no, float * sensor_values) {
phn10 59:6b3a52d87465 107 bool sensor_registered = false;
phn10 61:778e7e30e370 108 int gnoll_sensor_indicator = 0; // the order of sensor that gnoller's laser points to
phn10 64:72f7f5757f0d 109 int whack_sensor_indicator = 0; // the order of sensor that whacker's laser points to
phn10 61:778e7e30e370 110 int gnoll_no = 0;
phn10 61:778e7e30e370 111 int led_command = 0; // led command to display the indicator LED in whacker
phn10 61:778e7e30e370 112
phn10 64:72f7f5757f0d 113 if (sensor_no == 0) gnoll_no = 8;
phn10 64:72f7f5757f0d 114 if (sensor_no == 8) gnoll_no = 0;
phn10 61:778e7e30e370 115
phn10 59:6b3a52d87465 116 // start timer for sensor reading
phn10 59:6b3a52d87465 117 t.start();
phn10 59:6b3a52d87465 118
phn10 59:6b3a52d87465 119 // keep reading until one of the sensor get laser pointed in
phn10 59:6b3a52d87465 120 while (!sensor_registered) {
phn10 61:778e7e30e370 121 // scan all 16 sensors into sensor_values array
phn10 61:778e7e30e370 122 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
phn10 61:778e7e30e370 123
phn10 61:778e7e30e370 124 // scan all gnoll sensors
phn10 59:6b3a52d87465 125 for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
phn10 61:778e7e30e370 126 // detect which gnoll sensor get lasered on
phn10 64:72f7f5757f0d 127 if (sensor_values[i + gnoll_no] * 3.3f > PTTHRESH)
phn10 59:6b3a52d87465 128 sensor_registered = true;
phn10 61:778e7e30e370 129 gnoll_sensor_indicator = i;
phn10 59:6b3a52d87465 130 }
phn10 59:6b3a52d87465 131
phn10 59:6b3a52d87465 132 // if (reading sensor timer expired) display error and freeze
phn10 59:6b3a52d87465 133 t.stop();
phn10 61:778e7e30e370 134 if (t.read() > WHACK_EXPIRED_TIMER) {
phn10 59:6b3a52d87465 135 pc.printf("Error: Reading timer expired. See whack() function.\n");
phn10 59:6b3a52d87465 136 while (1);
phn10 59:6b3a52d87465 137 }
phn10 59:6b3a52d87465 138 }
phn10 59:6b3a52d87465 139
phn10 61:778e7e30e370 140 // point whack’s laser to the corresponding sensor on whack target array
phn10 61:778e7e30e370 141 turn_to_target(stp_sensor_pos[sensor_no]);
phn10 61:778e7e30e370 142
phn10 61:778e7e30e370 143 // turn on whacker's laser
phn10 61:778e7e30e370 144 lzr_on();
phn10 61:778e7e30e370 145
phn10 61:778e7e30e370 146 // scan all 16 sensors into sensor_values array
phn10 61:778e7e30e370 147 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
phn10 61:778e7e30e370 148
phn10 61:778e7e30e370 149 t.start();
phn10 61:778e7e30e370 150 sensor_registered = false;
phn10 59:6b3a52d87465 151
phn10 61:778e7e30e370 152 while (!sensor_registered) {
phn10 61:778e7e30e370 153 // scan all whack sensors
phn10 61:778e7e30e370 154 for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
phn10 61:778e7e30e370 155 // detect which gnoll sensor get lasered on
phn10 64:72f7f5757f0d 156 if (sensor_values[i + sensor_no] * 3.3f > PTTHRESH)
phn10 61:778e7e30e370 157 sensor_registered = true;
phn10 61:778e7e30e370 158 whack_sensor_indicator = i;
phn10 61:778e7e30e370 159 }
phn10 61:778e7e30e370 160
phn10 61:778e7e30e370 161 // if timer expire display error and freeze
phn10 61:778e7e30e370 162 t.stop();
phn10 61:778e7e30e370 163 if (t.read() > WHACK_EXPIRED_TIMER) {
phn10 61:778e7e30e370 164 pc.printf("Error: Reading timer expired. See whack() function.\n");
phn10 61:778e7e30e370 165 while(1);
phn10 61:778e7e30e370 166 }
phn10 59:6b3a52d87465 167 }
phn10 59:6b3a52d87465 168
phn10 61:778e7e30e370 169 // activate corresponding indicator in whack LED row
phn10 65:b8f3339b9268 170 led_command = 0x0500 + led_values[whack_sensor_indicator];
phn10 61:778e7e30e370 171 spi_send(as1107, led_command);
phn10 59:6b3a52d87465 172
phn10 59:6b3a52d87465 173 // waiting for gnoll laser to turn off to turn off
phn10 61:778e7e30e370 174 do {
phn10 61:778e7e30e370 175 // scan all 16 sensors into sensor_values array
phn10 61:778e7e30e370 176 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
phn10 64:72f7f5757f0d 177 } while (sensor_values[gnoll_sensor_indicator + gnoll_no] * 3.3f > PTTHRESH);
phn10 61:778e7e30e370 178
phn10 59:6b3a52d87465 179 // turn off laer gnoll
phn10 59:6b3a52d87465 180 lzr_off();
spm71 62:b73067127fd6 181 }
spm71 62:b73067127fd6 182
spm71 62:b73067127fd6 183 void update_score(int hit, int miss) {
spm71 62:b73067127fd6 184 int d1, d2, d3, d4 = 0;
spm71 62:b73067127fd6 185 if ( hit < 10)
spm71 62:b73067127fd6 186 d1 = hit;
spm71 62:b73067127fd6 187 else {
spm71 62:b73067127fd6 188 d1 = hit % 10;
spm71 62:b73067127fd6 189 d2 = (hit - d1)/10;
spm71 62:b73067127fd6 190 }
spm71 62:b73067127fd6 191 if ( miss < 10)
spm71 62:b73067127fd6 192 d3 = miss;
spm71 62:b73067127fd6 193 else {
spm71 62:b73067127fd6 194 d3 = miss % 10;
spm71 62:b73067127fd6 195 d4 = (miss - d3)/10;
spm71 62:b73067127fd6 196 }
spm71 62:b73067127fd6 197 spi_send(as1107, 0x0100 + d1);
spm71 62:b73067127fd6 198 spi_send(as1107, 0x0200 + d2);
spm71 62:b73067127fd6 199 spi_send(as1107, 0x0300 + d3);
spm71 62:b73067127fd6 200 spi_send(as1107, 0x0400 + d4);
phn10 59:6b3a52d87465 201 }