Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Lab_6_WaG by
wag.cpp@66:70efa618c378, 2018-04-19 (annotated)
- Committer:
- spm71
- Date:
- Thu Apr 19 15:26:19 2018 +0000
- Revision:
- 66:70efa618c378
- Parent:
- 65:b8f3339b9268
- Child:
- 67:6dffd5c82eb6
Bug hunting
Who changed what in which revision?
User | Revision | Line number | New 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 | |
phn10 | 59:6b3a52d87465 | 23 | extern 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; |
phn10 | 65:b8f3339b9268 | 34 | |
spm71 | 62:b73067127fd6 | 35 | int whacker_no = 0; |
spm71 | 62:b73067127fd6 | 36 | |
spm71 | 62:b73067127fd6 | 37 | if (sensor_no == 0) |
spm71 | 62:b73067127fd6 | 38 | whacker_no = 8; |
phn10 | 60:604c10531f58 | 39 | |
phn10 | 60:604c10531f58 | 40 | |
spm71 | 58:69f9a4607a16 | 41 | for (int i = 0; i < 15; i++) { |
spm71 | 62:b73067127fd6 | 42 | pc.printf("Round %d\n", i + 1); |
spm71 | 62:b73067127fd6 | 43 | a_num = rand() % 8; // create random number 0-7 |
spm71 | 62:b73067127fd6 | 44 | turn_to_target(stp_sensor_pos[a_num]); // turn motor to random target |
spm71 | 62:b73067127fd6 | 45 | lzr_on(); // turn laser on |
spm71 | 62:b73067127fd6 | 46 | wait(LASER_DELAY); // wait for laser to activate |
spm71 | 62:b73067127fd6 | 47 | |
spm71 | 62:b73067127fd6 | 48 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors |
spm71 | 62:b73067127fd6 | 49 | |
spm71 | 66:70efa618c378 | 50 | if (sensor_values[sensor_no + a_num] > PTTHRESH) { // confirm that the sensor is high |
spm71 | 62:b73067127fd6 | 51 | led_command = 0x0500 + led_values[a_num]; // create SPI command for LED |
spm71 | 62:b73067127fd6 | 52 | spi_send(as1107, led_command); // light up LED |
spm71 | 62:b73067127fd6 | 53 | } |
spm71 | 62:b73067127fd6 | 54 | else { |
spm71 | 62:b73067127fd6 | 55 | pc.printf("Error: sensor not activated. Shutting down.\n"); |
spm71 | 62:b73067127fd6 | 56 | while(1); |
spm71 | 58:69f9a4607a16 | 57 | } |
spm71 | 58:69f9a4607a16 | 58 | |
spm71 | 58:69f9a4607a16 | 59 | clock_t start = clock(); |
spm71 | 58:69f9a4607a16 | 60 | clock_t finish = clock(); |
spm71 | 62:b73067127fd6 | 61 | pc.printf("Waiting for whacker...\n"); |
phn10 | 60:604c10531f58 | 62 | |
spm71 | 58:69f9a4607a16 | 63 | do { |
spm71 | 62:b73067127fd6 | 64 | finish = clock(); // update timer |
spm71 | 62:b73067127fd6 | 65 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors |
spm71 | 66:70efa618c378 | 66 | } while (finish - start < VOLLEY_DELAY or sensor_values[whacker_no + a_num] < PTTHRESH); // check if timer expired or if sensor hit |
spm71 | 62:b73067127fd6 | 67 | |
spm71 | 66:70efa618c378 | 68 | if (sensor_values[whacker_no + a_num] > PTTHRESH) { |
spm71 | 62:b73067127fd6 | 69 | hit++; // increment hit count |
spm71 | 62:b73067127fd6 | 70 | pc.printf("Hit\n"); |
spm71 | 66:70efa618c378 | 71 | while(sensor_values[whacker_no + a_num] > PTTHRESH) |
spm71 | 62:b73067127fd6 | 72 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off |
spm71 | 62:b73067127fd6 | 73 | } |
spm71 | 62:b73067127fd6 | 74 | |
spm71 | 62:b73067127fd6 | 75 | else { |
spm71 | 62:b73067127fd6 | 76 | for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { // go through all whacker sensors |
spm71 | 66:70efa618c378 | 77 | if (sensor_values[i + whacker_no] > PTTHRESH and j != a_num) { // check if wrong sensor hit |
spm71 | 62:b73067127fd6 | 78 | pc.printf("Wrong sensor hit.\n"); |
spm71 | 62:b73067127fd6 | 79 | i = 15; // ends volley |
spm71 | 62:b73067127fd6 | 80 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors |
spm71 | 66:70efa618c378 | 81 | while(sensor_values[i + whacker_no] > PTTHRESH) |
spm71 | 62:b73067127fd6 | 82 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off |
spm71 | 62:b73067127fd6 | 83 | } |
spm71 | 62:b73067127fd6 | 84 | } |
spm71 | 62:b73067127fd6 | 85 | pc.printf("Miss\n"); |
spm71 | 62:b73067127fd6 | 86 | miss++; // increment miss count |
spm71 | 62:b73067127fd6 | 87 | } |
spm71 | 62:b73067127fd6 | 88 | update_score(hit, miss); |
spm71 | 58:69f9a4607a16 | 89 | } |
phn10 | 60:604c10531f58 | 90 | } |
phn10 | 60:604c10531f58 | 91 | |
phn10 | 64:72f7f5757f0d | 92 | /* |
phn10 | 64:72f7f5757f0d | 93 | * void whack(int sensor_no, float * sensor_values) |
phn10 | 64:72f7f5757f0d | 94 | * Description: function for whacker |
phn10 | 64:72f7f5757f0d | 95 | * |
phn10 | 64:72f7f5757f0d | 96 | * Inputs: |
phn10 | 64:72f7f5757f0d | 97 | * Parameters: |
phn10 | 64:72f7f5757f0d | 98 | * int sensor_no: the |
phn10 | 64:72f7f5757f0d | 99 | * float * sensor_values: an float array that stores 16 sensor values |
phn10 | 64:72f7f5757f0d | 100 | * Globals: |
phn10 | 64:72f7f5757f0d | 101 | * |
phn10 | 64:72f7f5757f0d | 102 | * Outputs: |
phn10 | 64:72f7f5757f0d | 103 | * Returns: void |
phn10 | 64:72f7f5757f0d | 104 | */ |
phn10 | 59:6b3a52d87465 | 105 | void whack(int sensor_no, float * sensor_values) { |
phn10 | 59:6b3a52d87465 | 106 | bool sensor_registered = false; |
phn10 | 61:778e7e30e370 | 107 | int gnoll_sensor_indicator = 0; // the order of sensor that gnoller's laser points to |
phn10 | 64:72f7f5757f0d | 108 | int whack_sensor_indicator = 0; // the order of sensor that whacker's laser points to |
phn10 | 61:778e7e30e370 | 109 | int gnoll_no = 0; |
phn10 | 61:778e7e30e370 | 110 | int led_command = 0; // led command to display the indicator LED in whacker |
phn10 | 61:778e7e30e370 | 111 | |
phn10 | 64:72f7f5757f0d | 112 | if (sensor_no == 0) gnoll_no = 8; |
phn10 | 64:72f7f5757f0d | 113 | if (sensor_no == 8) gnoll_no = 0; |
phn10 | 61:778e7e30e370 | 114 | |
phn10 | 59:6b3a52d87465 | 115 | // start timer for sensor reading |
phn10 | 59:6b3a52d87465 | 116 | t.start(); |
phn10 | 59:6b3a52d87465 | 117 | |
phn10 | 59:6b3a52d87465 | 118 | // keep reading until one of the sensor get laser pointed in |
phn10 | 59:6b3a52d87465 | 119 | while (!sensor_registered) { |
phn10 | 61:778e7e30e370 | 120 | // scan all 16 sensors into sensor_values array |
phn10 | 61:778e7e30e370 | 121 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 61:778e7e30e370 | 122 | |
phn10 | 61:778e7e30e370 | 123 | // scan all gnoll sensors |
phn10 | 59:6b3a52d87465 | 124 | for (int i = 0; i < TGT_SENSOR_QUAN; i++) { |
phn10 | 61:778e7e30e370 | 125 | // detect which gnoll sensor get lasered on |
phn10 | 64:72f7f5757f0d | 126 | if (sensor_values[i + gnoll_no] * 3.3f > PTTHRESH) |
phn10 | 59:6b3a52d87465 | 127 | sensor_registered = true; |
phn10 | 61:778e7e30e370 | 128 | gnoll_sensor_indicator = i; |
phn10 | 59:6b3a52d87465 | 129 | } |
phn10 | 59:6b3a52d87465 | 130 | |
phn10 | 59:6b3a52d87465 | 131 | // if (reading sensor timer expired) display error and freeze |
phn10 | 59:6b3a52d87465 | 132 | t.stop(); |
phn10 | 61:778e7e30e370 | 133 | if (t.read() > WHACK_EXPIRED_TIMER) { |
phn10 | 59:6b3a52d87465 | 134 | pc.printf("Error: Reading timer expired. See whack() function.\n"); |
phn10 | 59:6b3a52d87465 | 135 | while (1); |
phn10 | 59:6b3a52d87465 | 136 | } |
phn10 | 59:6b3a52d87465 | 137 | } |
phn10 | 59:6b3a52d87465 | 138 | |
phn10 | 61:778e7e30e370 | 139 | // point whack’s laser to the corresponding sensor on whack target array |
phn10 | 61:778e7e30e370 | 140 | turn_to_target(stp_sensor_pos[sensor_no]); |
phn10 | 61:778e7e30e370 | 141 | |
phn10 | 61:778e7e30e370 | 142 | // turn on whacker's laser |
phn10 | 61:778e7e30e370 | 143 | lzr_on(); |
phn10 | 61:778e7e30e370 | 144 | |
phn10 | 61:778e7e30e370 | 145 | // scan all 16 sensors into sensor_values array |
phn10 | 61:778e7e30e370 | 146 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 61:778e7e30e370 | 147 | |
phn10 | 61:778e7e30e370 | 148 | t.start(); |
phn10 | 61:778e7e30e370 | 149 | sensor_registered = false; |
phn10 | 59:6b3a52d87465 | 150 | |
phn10 | 61:778e7e30e370 | 151 | while (!sensor_registered) { |
phn10 | 61:778e7e30e370 | 152 | // scan all whack sensors |
phn10 | 61:778e7e30e370 | 153 | for (int i = 0; i < TGT_SENSOR_QUAN; i++) { |
phn10 | 61:778e7e30e370 | 154 | // detect which gnoll sensor get lasered on |
phn10 | 64:72f7f5757f0d | 155 | if (sensor_values[i + sensor_no] * 3.3f > PTTHRESH) |
phn10 | 61:778e7e30e370 | 156 | sensor_registered = true; |
phn10 | 61:778e7e30e370 | 157 | whack_sensor_indicator = i; |
phn10 | 61:778e7e30e370 | 158 | } |
phn10 | 61:778e7e30e370 | 159 | |
phn10 | 61:778e7e30e370 | 160 | // if timer expire display error and freeze |
phn10 | 61:778e7e30e370 | 161 | t.stop(); |
phn10 | 61:778e7e30e370 | 162 | if (t.read() > WHACK_EXPIRED_TIMER) { |
phn10 | 61:778e7e30e370 | 163 | pc.printf("Error: Reading timer expired. See whack() function.\n"); |
phn10 | 61:778e7e30e370 | 164 | while(1); |
phn10 | 61:778e7e30e370 | 165 | } |
phn10 | 59:6b3a52d87465 | 166 | } |
phn10 | 59:6b3a52d87465 | 167 | |
phn10 | 61:778e7e30e370 | 168 | // activate corresponding indicator in whack LED row |
phn10 | 65:b8f3339b9268 | 169 | led_command = 0x0500 + led_values[whack_sensor_indicator]; |
phn10 | 61:778e7e30e370 | 170 | spi_send(as1107, led_command); |
phn10 | 59:6b3a52d87465 | 171 | |
phn10 | 59:6b3a52d87465 | 172 | // waiting for gnoll laser to turn off to turn off |
phn10 | 61:778e7e30e370 | 173 | do { |
phn10 | 61:778e7e30e370 | 174 | // scan all 16 sensors into sensor_values array |
phn10 | 61:778e7e30e370 | 175 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 64:72f7f5757f0d | 176 | } while (sensor_values[gnoll_sensor_indicator + gnoll_no] * 3.3f > PTTHRESH); |
phn10 | 61:778e7e30e370 | 177 | |
phn10 | 59:6b3a52d87465 | 178 | // turn off laer gnoll |
phn10 | 59:6b3a52d87465 | 179 | lzr_off(); |
spm71 | 62:b73067127fd6 | 180 | } |
spm71 | 62:b73067127fd6 | 181 | |
spm71 | 62:b73067127fd6 | 182 | void update_score(int hit, int miss) { |
spm71 | 62:b73067127fd6 | 183 | int d1, d2, d3, d4 = 0; |
spm71 | 62:b73067127fd6 | 184 | if ( hit < 10) |
spm71 | 62:b73067127fd6 | 185 | d1 = hit; |
spm71 | 62:b73067127fd6 | 186 | else { |
spm71 | 62:b73067127fd6 | 187 | d1 = hit % 10; |
spm71 | 62:b73067127fd6 | 188 | d2 = (hit - d1)/10; |
spm71 | 62:b73067127fd6 | 189 | } |
spm71 | 62:b73067127fd6 | 190 | if ( miss < 10) |
spm71 | 62:b73067127fd6 | 191 | d3 = miss; |
spm71 | 62:b73067127fd6 | 192 | else { |
spm71 | 62:b73067127fd6 | 193 | d3 = miss % 10; |
spm71 | 62:b73067127fd6 | 194 | d4 = (miss - d3)/10; |
spm71 | 62:b73067127fd6 | 195 | } |
spm71 | 62:b73067127fd6 | 196 | spi_send(as1107, 0x0100 + d1); |
spm71 | 62:b73067127fd6 | 197 | spi_send(as1107, 0x0200 + d2); |
spm71 | 62:b73067127fd6 | 198 | spi_send(as1107, 0x0300 + d3); |
spm71 | 62:b73067127fd6 | 199 | spi_send(as1107, 0x0400 + d4); |
phn10 | 59:6b3a52d87465 | 200 | } |