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@64:72f7f5757f0d, 2018-04-19 (annotated)
- Committer:
- phn10
- Date:
- Thu Apr 19 00:04:48 2018 +0000
- Revision:
- 64:72f7f5757f0d
- Parent:
- 61:778e7e30e370
- Child:
- 65:b8f3339b9268
keep working on whack() function, not complied yet;
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" |
spm71 | 57:aef01bd9b3be | 18 | #include <stdlib.h> |
spm71 | 57:aef01bd9b3be | 19 | #include <stdio.h> |
phn10 | 59:6b3a52d87465 | 20 | #include <string.h> |
phn10 | 59:6b3a52d87465 | 21 | |
phn10 | 59:6b3a52d87465 | 22 | extern Timer t; |
phn10 | 59:6b3a52d87465 | 23 | |
spm71 | 58:69f9a4607a16 | 24 | void gnoll() { |
spm71 | 58:69f9a4607a16 | 25 | int hit, miss; |
spm71 | 58:69f9a4607a16 | 26 | int a_num; |
spm71 | 58:69f9a4607a16 | 27 | int led_command; |
phn10 | 64:72f7f5757f0d | 28 | int sensor_no = 0; //change to match station |
phn10 | 60:604c10531f58 | 29 | |
spm71 | 58:69f9a4607a16 | 30 | for (int i = 0; i < 15; i++) { |
spm71 | 58:69f9a4607a16 | 31 | a_num = rand() % 8; |
spm71 | 58:69f9a4607a16 | 32 | turn_to_target(stp_sensor_pos[a_num]); |
spm71 | 58:69f9a4607a16 | 33 | lzr_on(); |
spm71 | 58:69f9a4607a16 | 34 | wait(LASER_DELAY); |
spm71 | 58:69f9a4607a16 | 35 | if (sensor_values[sensor_no + a_num] > PTHRESH) { |
spm71 | 58:69f9a4607a16 | 36 | led_command = 0x0500 + a_num; |
spm71 | 58:69f9a4607a16 | 37 | spi_send(as1107, led_command); |
phn10 | 60:604c10531f58 | 38 | |
spm71 | 58:69f9a4607a16 | 39 | } |
spm71 | 58:69f9a4607a16 | 40 | |
spm71 | 58:69f9a4607a16 | 41 | clock_t start = clock(); |
spm71 | 58:69f9a4607a16 | 42 | clock_t finish = clock(); |
phn10 | 60:604c10531f58 | 43 | |
spm71 | 58:69f9a4607a16 | 44 | do { |
spm71 | 58:69f9a4607a16 | 45 | // check for whacker sensors |
spm71 | 58:69f9a4607a16 | 46 | finish = clock(); |
spm71 | 58:69f9a4607a16 | 47 | } while (finish - start < VOLLEY_DELAY) |
spm71 | 58:69f9a4607a16 | 48 | } |
phn10 | 60:604c10531f58 | 49 | } |
phn10 | 60:604c10531f58 | 50 | |
phn10 | 64:72f7f5757f0d | 51 | /* |
phn10 | 64:72f7f5757f0d | 52 | * void whack(int sensor_no, float * sensor_values) |
phn10 | 64:72f7f5757f0d | 53 | * Description: function for whacker |
phn10 | 64:72f7f5757f0d | 54 | * |
phn10 | 64:72f7f5757f0d | 55 | * Inputs: |
phn10 | 64:72f7f5757f0d | 56 | * Parameters: |
phn10 | 64:72f7f5757f0d | 57 | * int sensor_no: the |
phn10 | 64:72f7f5757f0d | 58 | * float * sensor_values: an float array that stores 16 sensor values |
phn10 | 64:72f7f5757f0d | 59 | * Globals: |
phn10 | 64:72f7f5757f0d | 60 | * |
phn10 | 64:72f7f5757f0d | 61 | * Outputs: |
phn10 | 64:72f7f5757f0d | 62 | * Returns: void |
phn10 | 64:72f7f5757f0d | 63 | */ |
phn10 | 59:6b3a52d87465 | 64 | void whack(int sensor_no, float * sensor_values) { |
phn10 | 59:6b3a52d87465 | 65 | bool sensor_registered = false; |
phn10 | 61:778e7e30e370 | 66 | int gnoll_sensor_indicator = 0; // the order of sensor that gnoller's laser points to |
phn10 | 64:72f7f5757f0d | 67 | int whack_sensor_indicator = 0; // the order of sensor that whacker's laser points to |
phn10 | 61:778e7e30e370 | 68 | int gnoll_no = 0; |
phn10 | 61:778e7e30e370 | 69 | int led_command = 0; // led command to display the indicator LED in whacker |
phn10 | 61:778e7e30e370 | 70 | |
phn10 | 64:72f7f5757f0d | 71 | if (sensor_no == 0) gnoll_no = 8; |
phn10 | 64:72f7f5757f0d | 72 | if (sensor_no == 8) gnoll_no = 0; |
phn10 | 61:778e7e30e370 | 73 | |
phn10 | 59:6b3a52d87465 | 74 | // start timer for sensor reading |
phn10 | 59:6b3a52d87465 | 75 | t.start(); |
phn10 | 59:6b3a52d87465 | 76 | |
phn10 | 59:6b3a52d87465 | 77 | // keep reading until one of the sensor get laser pointed in |
phn10 | 59:6b3a52d87465 | 78 | while (!sensor_registered) { |
phn10 | 61:778e7e30e370 | 79 | // scan all 16 sensors into sensor_values array |
phn10 | 61:778e7e30e370 | 80 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 61:778e7e30e370 | 81 | |
phn10 | 61:778e7e30e370 | 82 | // scan all gnoll sensors |
phn10 | 59:6b3a52d87465 | 83 | for (int i = 0; i < TGT_SENSOR_QUAN; i++) { |
phn10 | 61:778e7e30e370 | 84 | // detect which gnoll sensor get lasered on |
phn10 | 64:72f7f5757f0d | 85 | if (sensor_values[i + gnoll_no] * 3.3f > PTTHRESH) |
phn10 | 59:6b3a52d87465 | 86 | sensor_registered = true; |
phn10 | 61:778e7e30e370 | 87 | gnoll_sensor_indicator = i; |
phn10 | 59:6b3a52d87465 | 88 | } |
phn10 | 59:6b3a52d87465 | 89 | |
phn10 | 59:6b3a52d87465 | 90 | // if (reading sensor timer expired) display error and freeze |
phn10 | 59:6b3a52d87465 | 91 | t.stop(); |
phn10 | 61:778e7e30e370 | 92 | if (t.read() > WHACK_EXPIRED_TIMER) { |
phn10 | 59:6b3a52d87465 | 93 | pc.printf("Error: Reading timer expired. See whack() function.\n"); |
phn10 | 59:6b3a52d87465 | 94 | while (1); |
phn10 | 59:6b3a52d87465 | 95 | } |
phn10 | 59:6b3a52d87465 | 96 | } |
phn10 | 59:6b3a52d87465 | 97 | |
phn10 | 61:778e7e30e370 | 98 | // point whack’s laser to the corresponding sensor on whack target array |
phn10 | 61:778e7e30e370 | 99 | turn_to_target(stp_sensor_pos[sensor_no]); |
phn10 | 61:778e7e30e370 | 100 | |
phn10 | 61:778e7e30e370 | 101 | // turn on whacker's laser |
phn10 | 61:778e7e30e370 | 102 | lzr_on(); |
phn10 | 61:778e7e30e370 | 103 | |
phn10 | 61:778e7e30e370 | 104 | // scan all 16 sensors into sensor_values array |
phn10 | 61:778e7e30e370 | 105 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 61:778e7e30e370 | 106 | |
phn10 | 61:778e7e30e370 | 107 | t.start(); |
phn10 | 61:778e7e30e370 | 108 | sensor_registered = false; |
phn10 | 59:6b3a52d87465 | 109 | |
phn10 | 61:778e7e30e370 | 110 | while (!sensor_registered) { |
phn10 | 61:778e7e30e370 | 111 | // scan all whack sensors |
phn10 | 61:778e7e30e370 | 112 | for (int i = 0; i < TGT_SENSOR_QUAN; i++) { |
phn10 | 61:778e7e30e370 | 113 | // detect which gnoll sensor get lasered on |
phn10 | 64:72f7f5757f0d | 114 | if (sensor_values[i + sensor_no] * 3.3f > PTTHRESH) |
phn10 | 61:778e7e30e370 | 115 | sensor_registered = true; |
phn10 | 61:778e7e30e370 | 116 | whack_sensor_indicator = i; |
phn10 | 61:778e7e30e370 | 117 | } |
phn10 | 61:778e7e30e370 | 118 | |
phn10 | 61:778e7e30e370 | 119 | // if timer expire display error and freeze |
phn10 | 61:778e7e30e370 | 120 | t.stop(); |
phn10 | 61:778e7e30e370 | 121 | if (t.read() > WHACK_EXPIRED_TIMER) { |
phn10 | 61:778e7e30e370 | 122 | pc.printf("Error: Reading timer expired. See whack() function.\n"); |
phn10 | 61:778e7e30e370 | 123 | while(1); |
phn10 | 61:778e7e30e370 | 124 | } |
phn10 | 59:6b3a52d87465 | 125 | } |
phn10 | 59:6b3a52d87465 | 126 | |
phn10 | 61:778e7e30e370 | 127 | // activate corresponding indicator in whack LED row |
phn10 | 61:778e7e30e370 | 128 | led_command = 0x0500 + led_values[whack_sensor_indicator]); |
phn10 | 61:778e7e30e370 | 129 | spi_send(as1107, led_command); |
phn10 | 59:6b3a52d87465 | 130 | |
phn10 | 59:6b3a52d87465 | 131 | // waiting for gnoll laser to turn off to turn off |
phn10 | 61:778e7e30e370 | 132 | do { |
phn10 | 61:778e7e30e370 | 133 | // scan all 16 sensors into sensor_values array |
phn10 | 61:778e7e30e370 | 134 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 64:72f7f5757f0d | 135 | } while (sensor_values[gnoll_sensor_indicator + gnoll_no] * 3.3f > PTTHRESH); |
phn10 | 61:778e7e30e370 | 136 | |
phn10 | 59:6b3a52d87465 | 137 | // turn off laer gnoll |
phn10 | 59:6b3a52d87465 | 138 | lzr_off(); |
phn10 | 59:6b3a52d87465 | 139 | } |