GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Committer:
phn10
Date:
Mon Apr 23 21:21:29 2018 +0000
Revision:
72:aec869655869
Parent:
71:6ced854bebb2
Child:
73:e8f7ee59d3e5
fixed the LED not light up in the gnoll() function. 2 issues still need to be improved:; 1. add a wait() after spi_send which light up the LED, or else the led won't light up.; 2. there is still some lagging between when laser light up and LED light up

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