GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Committer:
spm71
Date:
Mon Apr 23 19:52:06 2018 +0000
Revision:
69:1b7271bd4a75
Parent:
68:9245d6ce176e
Child:
71:6ced854bebb2
Timer fix

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 69:1b7271bd4a75 45 pc.printf("Random number: %d. Stepper position: %d.", a_num, stp_sensor_pos[a_num]);
spm71 62:b73067127fd6 46 turn_to_target(stp_sensor_pos[a_num]); // turn motor to random target
spm71 62:b73067127fd6 47 lzr_on(); // turn laser on
spm71 62:b73067127fd6 48 wait(LASER_DELAY); // wait for laser to activate
spm71 62:b73067127fd6 49
spm71 62:b73067127fd6 50 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 62:b73067127fd6 51
spm71 69:1b7271bd4a75 52 if (sensor_values[sensor_no + a_num] * 3.3 > PTTHRESH) { // confirm that the sensor is high
spm71 62:b73067127fd6 53 led_command = 0x0500 + led_values[a_num]; // create SPI command for LED
spm71 62:b73067127fd6 54 spi_send(as1107, led_command); // light up LED
spm71 62:b73067127fd6 55 }
spm71 62:b73067127fd6 56 else {
spm71 69:1b7271bd4a75 57 pc.printf("sensor value: %f\n" ,sensor_values[sensor_no + a_num] * 3.3);
spm71 62:b73067127fd6 58 pc.printf("Error: sensor not activated. Shutting down.\n");
spm71 62:b73067127fd6 59 while(1);
spm71 58:69f9a4607a16 60 }
spm71 58:69f9a4607a16 61
spm71 58:69f9a4607a16 62 clock_t start = clock();
spm71 62:b73067127fd6 63 pc.printf("Waiting for whacker...\n");
phn10 60:604c10531f58 64
spm71 58:69f9a4607a16 65 do {
spm71 62:b73067127fd6 66 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 68:9245d6ce176e 67 clock_t difference = clock() - start;
spm71 68:9245d6ce176e 68 msec = difference * 1000 / CLOCKS_PER_SEC;
spm71 69:1b7271bd4a75 69 //pc.printf("Time elapsed: %d. Time limit: %d\n", msec, VOLLEY_DELAY);
spm71 69:1b7271bd4a75 70 } while (msec < VOLLEY_DELAY and sensor_values[whacker_no + a_num] < PTTHRESH); // check if timer expired or if sensor hit
spm71 62:b73067127fd6 71
spm71 69:1b7271bd4a75 72 if (sensor_values[whacker_no + a_num] * 3.3 > PTTHRESH) {
spm71 62:b73067127fd6 73 hit++; // increment hit count
spm71 62:b73067127fd6 74 pc.printf("Hit\n");
spm71 69:1b7271bd4a75 75 while(sensor_values[whacker_no + a_num] * 3.3 > PTTHRESH)
spm71 62:b73067127fd6 76 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
spm71 62:b73067127fd6 77 }
spm71 62:b73067127fd6 78
spm71 62:b73067127fd6 79 else {
spm71 62:b73067127fd6 80 for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { // go through all whacker sensors
spm71 69:1b7271bd4a75 81 if (sensor_values[i + whacker_no] * 3.3 > PTTHRESH and j != a_num) { // check if wrong sensor hit
spm71 62:b73067127fd6 82 pc.printf("Wrong sensor hit.\n");
spm71 62:b73067127fd6 83 i = 15; // ends volley
spm71 62:b73067127fd6 84 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 69:1b7271bd4a75 85 while(sensor_values[i + whacker_no] * 3.3 > PTTHRESH)
spm71 62:b73067127fd6 86 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
spm71 62:b73067127fd6 87 }
spm71 62:b73067127fd6 88 }
spm71 62:b73067127fd6 89 pc.printf("Miss\n");
spm71 62:b73067127fd6 90 miss++; // increment miss count
spm71 62:b73067127fd6 91 }
spm71 62:b73067127fd6 92 update_score(hit, miss);
spm71 58:69f9a4607a16 93 }
phn10 60:604c10531f58 94 }
phn10 60:604c10531f58 95
phn10 64:72f7f5757f0d 96 /*
phn10 64:72f7f5757f0d 97 * void whack(int sensor_no, float * sensor_values)
phn10 64:72f7f5757f0d 98 * Description: function for whacker
phn10 64:72f7f5757f0d 99 *
phn10 64:72f7f5757f0d 100 * Inputs:
phn10 64:72f7f5757f0d 101 * Parameters:
phn10 64:72f7f5757f0d 102 * int sensor_no: the
phn10 64:72f7f5757f0d 103 * float * sensor_values: an float array that stores 16 sensor values
phn10 64:72f7f5757f0d 104 * Globals:
phn10 64:72f7f5757f0d 105 *
phn10 64:72f7f5757f0d 106 * Outputs:
phn10 64:72f7f5757f0d 107 * Returns: void
phn10 64:72f7f5757f0d 108 */
phn10 59:6b3a52d87465 109 void whack(int sensor_no, float * sensor_values) {
phn10 59:6b3a52d87465 110 bool sensor_registered = false;
phn10 61:778e7e30e370 111 int gnoll_sensor_indicator = 0; // the order of sensor that gnoller's laser points to
phn10 64:72f7f5757f0d 112 int whack_sensor_indicator = 0; // the order of sensor that whacker's laser points to
phn10 61:778e7e30e370 113 int gnoll_no = 0;
phn10 61:778e7e30e370 114 int led_command = 0; // led command to display the indicator LED in whacker
phn10 61:778e7e30e370 115
phn10 64:72f7f5757f0d 116 if (sensor_no == 0) gnoll_no = 8;
phn10 64:72f7f5757f0d 117 if (sensor_no == 8) gnoll_no = 0;
phn10 61:778e7e30e370 118
phn10 59:6b3a52d87465 119 // start timer for sensor reading
phn10 59:6b3a52d87465 120 t.start();
phn10 59:6b3a52d87465 121
phn10 59:6b3a52d87465 122 // keep reading until one of the sensor get laser pointed in
phn10 59:6b3a52d87465 123 while (!sensor_registered) {
phn10 61:778e7e30e370 124 // scan all 16 sensors into sensor_values array
phn10 61:778e7e30e370 125 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
phn10 61:778e7e30e370 126
phn10 61:778e7e30e370 127 // scan all gnoll sensors
phn10 59:6b3a52d87465 128 for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
phn10 61:778e7e30e370 129 // detect which gnoll sensor get lasered on
phn10 64:72f7f5757f0d 130 if (sensor_values[i + gnoll_no] * 3.3f > PTTHRESH)
phn10 59:6b3a52d87465 131 sensor_registered = true;
phn10 61:778e7e30e370 132 gnoll_sensor_indicator = i;
phn10 59:6b3a52d87465 133 }
phn10 59:6b3a52d87465 134
phn10 59:6b3a52d87465 135 // if (reading sensor timer expired) display error and freeze
phn10 59:6b3a52d87465 136 t.stop();
phn10 61:778e7e30e370 137 if (t.read() > WHACK_EXPIRED_TIMER) {
phn10 59:6b3a52d87465 138 pc.printf("Error: Reading timer expired. See whack() function.\n");
phn10 59:6b3a52d87465 139 while (1);
phn10 59:6b3a52d87465 140 }
phn10 59:6b3a52d87465 141 }
phn10 59:6b3a52d87465 142
phn10 61:778e7e30e370 143 // point whack’s laser to the corresponding sensor on whack target array
phn10 61:778e7e30e370 144 turn_to_target(stp_sensor_pos[sensor_no]);
phn10 61:778e7e30e370 145
phn10 61:778e7e30e370 146 // turn on whacker's laser
phn10 61:778e7e30e370 147 lzr_on();
phn10 61:778e7e30e370 148
phn10 61:778e7e30e370 149 // scan all 16 sensors into sensor_values array
phn10 61:778e7e30e370 150 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
phn10 61:778e7e30e370 151
phn10 61:778e7e30e370 152 t.start();
phn10 61:778e7e30e370 153 sensor_registered = false;
phn10 59:6b3a52d87465 154
phn10 61:778e7e30e370 155 while (!sensor_registered) {
phn10 61:778e7e30e370 156 // scan all whack sensors
phn10 61:778e7e30e370 157 for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
phn10 61:778e7e30e370 158 // detect which gnoll sensor get lasered on
phn10 64:72f7f5757f0d 159 if (sensor_values[i + sensor_no] * 3.3f > PTTHRESH)
phn10 61:778e7e30e370 160 sensor_registered = true;
phn10 61:778e7e30e370 161 whack_sensor_indicator = i;
phn10 61:778e7e30e370 162 }
phn10 61:778e7e30e370 163
phn10 61:778e7e30e370 164 // if timer expire display error and freeze
phn10 61:778e7e30e370 165 t.stop();
phn10 61:778e7e30e370 166 if (t.read() > WHACK_EXPIRED_TIMER) {
phn10 61:778e7e30e370 167 pc.printf("Error: Reading timer expired. See whack() function.\n");
phn10 61:778e7e30e370 168 while(1);
phn10 61:778e7e30e370 169 }
phn10 59:6b3a52d87465 170 }
phn10 59:6b3a52d87465 171
phn10 61:778e7e30e370 172 // activate corresponding indicator in whack LED row
phn10 65:b8f3339b9268 173 led_command = 0x0500 + led_values[whack_sensor_indicator];
phn10 61:778e7e30e370 174 spi_send(as1107, led_command);
phn10 59:6b3a52d87465 175
phn10 59:6b3a52d87465 176 // waiting for gnoll laser to turn off to turn off
phn10 61:778e7e30e370 177 do {
phn10 61:778e7e30e370 178 // scan all 16 sensors into sensor_values array
phn10 61:778e7e30e370 179 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
phn10 64:72f7f5757f0d 180 } while (sensor_values[gnoll_sensor_indicator + gnoll_no] * 3.3f > PTTHRESH);
phn10 61:778e7e30e370 181
phn10 59:6b3a52d87465 182 // turn off laer gnoll
phn10 59:6b3a52d87465 183 lzr_off();
spm71 62:b73067127fd6 184 }
spm71 62:b73067127fd6 185
spm71 62:b73067127fd6 186 void update_score(int hit, int miss) {
spm71 62:b73067127fd6 187 int d1, d2, d3, d4 = 0;
spm71 62:b73067127fd6 188 if ( hit < 10)
spm71 62:b73067127fd6 189 d1 = hit;
spm71 62:b73067127fd6 190 else {
spm71 62:b73067127fd6 191 d1 = hit % 10;
spm71 62:b73067127fd6 192 d2 = (hit - d1)/10;
spm71 62:b73067127fd6 193 }
spm71 62:b73067127fd6 194 if ( miss < 10)
spm71 62:b73067127fd6 195 d3 = miss;
spm71 62:b73067127fd6 196 else {
spm71 62:b73067127fd6 197 d3 = miss % 10;
spm71 62:b73067127fd6 198 d4 = (miss - d3)/10;
spm71 62:b73067127fd6 199 }
spm71 62:b73067127fd6 200 spi_send(as1107, 0x0100 + d1);
spm71 62:b73067127fd6 201 spi_send(as1107, 0x0200 + d2);
spm71 62:b73067127fd6 202 spi_send(as1107, 0x0300 + d3);
spm71 62:b73067127fd6 203 spi_send(as1107, 0x0400 + d4);
phn10 59:6b3a52d87465 204 }