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@60:604c10531f58, 2018-04-17 (annotated)
- Committer:
- phn10
- Date:
- Tue Apr 17 12:47:32 2018 +0000
- Revision:
- 60:604c10531f58
- Parent:
- 58:69f9a4607a16
- Parent:
- 59:6b3a52d87465
- Child:
- 61:778e7e30e370
- Child:
- 62:b73067127fd6
merge
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> |
spm71 | 58:69f9a4607a16 | 21 | #include <timer.h> |
phn10 | 59:6b3a52d87465 | 22 | |
phn10 | 59:6b3a52d87465 | 23 | extern Timer t; |
phn10 | 59:6b3a52d87465 | 24 | |
spm71 | 58:69f9a4607a16 | 25 | void gnoll() { |
spm71 | 58:69f9a4607a16 | 26 | int hit, miss; |
spm71 | 58:69f9a4607a16 | 27 | int a_num; |
spm71 | 58:69f9a4607a16 | 28 | int led_command; |
spm71 | 58:69f9a4607a16 | 29 | int sensor_no = 0 //change to match station |
phn10 | 60:604c10531f58 | 30 | |
spm71 | 58:69f9a4607a16 | 31 | for (int i = 0; i < 15; i++) { |
spm71 | 58:69f9a4607a16 | 32 | a_num = rand() % 8; |
spm71 | 58:69f9a4607a16 | 33 | turn_to_target(stp_sensor_pos[a_num]); |
spm71 | 58:69f9a4607a16 | 34 | lzr_on(); |
spm71 | 58:69f9a4607a16 | 35 | wait(LASER_DELAY); |
spm71 | 58:69f9a4607a16 | 36 | if (sensor_values[sensor_no + a_num] > PTHRESH) { |
spm71 | 58:69f9a4607a16 | 37 | led_command = 0x0500 + a_num; |
spm71 | 58:69f9a4607a16 | 38 | spi_send(as1107, led_command); |
phn10 | 60:604c10531f58 | 39 | |
spm71 | 58:69f9a4607a16 | 40 | } |
spm71 | 58:69f9a4607a16 | 41 | |
spm71 | 58:69f9a4607a16 | 42 | clock_t start = clock(); |
spm71 | 58:69f9a4607a16 | 43 | clock_t finish = clock(); |
phn10 | 60:604c10531f58 | 44 | |
spm71 | 58:69f9a4607a16 | 45 | do { |
spm71 | 58:69f9a4607a16 | 46 | // check for whacker sensors |
spm71 | 58:69f9a4607a16 | 47 | finish = clock(); |
spm71 | 58:69f9a4607a16 | 48 | } while (finish - start < VOLLEY_DELAY) |
spm71 | 58:69f9a4607a16 | 49 | } |
phn10 | 60:604c10531f58 | 50 | } |
phn10 | 60:604c10531f58 | 51 | |
phn10 | 59:6b3a52d87465 | 52 | void whack(int sensor_no, float * sensor_values) { |
phn10 | 59:6b3a52d87465 | 53 | bool sensor_registered = false; |
phn10 | 59:6b3a52d87465 | 54 | int sensor_no = // --> need to specify sensor_no |
phn10 | 60:604c10531f58 | 55 | |
phn10 | 59:6b3a52d87465 | 56 | // start timer for sensor reading |
phn10 | 59:6b3a52d87465 | 57 | t.start(); |
phn10 | 59:6b3a52d87465 | 58 | |
phn10 | 59:6b3a52d87465 | 59 | // keep reading until one of the sensor get laser pointed in |
phn10 | 59:6b3a52d87465 | 60 | while (!sensor_registered) { |
phn10 | 59:6b3a52d87465 | 61 | // read value of all gnoll sensor |
phn10 | 59:6b3a52d87465 | 62 | for (int i = 0; i < TGT_SENSOR_QUAN; i++) { |
phn10 | 59:6b3a52d87465 | 63 | // scan all 16 sensors into sensor_values array |
phn10 | 59:6b3a52d87465 | 64 | ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); |
phn10 | 59:6b3a52d87465 | 65 | } |
phn10 | 60:604c10531f58 | 66 | |
phn10 | 59:6b3a52d87465 | 67 | // detect which sensor get lasered on |
phn10 | 59:6b3a52d87465 | 68 | for (int i = 0; i < TGT_SENSOR_QUAN + sensor_no; i++) { |
phn10 | 59:6b3a52d87465 | 69 | if (sensor_values[i] > THRESHOLD_SIGNAL) |
phn10 | 59:6b3a52d87465 | 70 | sensor_registered = true; |
phn10 | 59:6b3a52d87465 | 71 | } |
phn10 | 59:6b3a52d87465 | 72 | |
phn10 | 59:6b3a52d87465 | 73 | // if (reading sensor timer expired) display error and freeze |
phn10 | 59:6b3a52d87465 | 74 | t.stop(); |
phn10 | 59:6b3a52d87465 | 75 | if (t.read() > EXPIRED_TIMER) { |
phn10 | 59:6b3a52d87465 | 76 | pc.printf("Error: Reading timer expired. See whack() function.\n"); |
phn10 | 59:6b3a52d87465 | 77 | while (1); |
phn10 | 59:6b3a52d87465 | 78 | } |
phn10 | 59:6b3a52d87465 | 79 | } |
phn10 | 59:6b3a52d87465 | 80 | |
phn10 | 59:6b3a52d87465 | 81 | // point whack’s laser to the corresponding sensor on it’s target array |
phn10 | 59:6b3a52d87465 | 82 | |
phn10 | 59:6b3a52d87465 | 83 | // activate whacker’s laser |
phn10 | 59:6b3a52d87465 | 84 | |
phn10 | 59:6b3a52d87465 | 85 | while (signal from corresponding sensor value > THRESHOLD_SIGNAL) { |
phn10 | 59:6b3a52d87465 | 86 | if (finding sensor timer expired) display error and freeze |
phn10 | 59:6b3a52d87465 | 87 | } |
phn10 | 59:6b3a52d87465 | 88 | |
phn10 | 59:6b3a52d87465 | 89 | activate corresponding indicator |
phn10 | 59:6b3a52d87465 | 90 | |
phn10 | 59:6b3a52d87465 | 91 | // waiting for gnoll laser to turn off to turn off |
phn10 | 59:6b3a52d87465 | 92 | while (gnoll’s laser turned on) {} |
phn10 | 59:6b3a52d87465 | 93 | // turn off laer gnoll |
phn10 | 59:6b3a52d87465 | 94 | lzr_off(); |
phn10 | 59:6b3a52d87465 | 95 | } |