GroupA / Mbed 2 deprecated WaG_final

Dependencies:   mbed

Fork of Lab_6_WaG by GroupA

Committer:
spm71
Date:
Tue Apr 17 16:14:30 2018 +0000
Revision:
62:b73067127fd6
Parent:
60:604c10531f58
Child:
63:8fcb1cc55e18
Added to gnoller function

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"
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;
spm71 62:b73067127fd6 24 int led_values[8] = {1, 2, 4, 8, 16, 32, 64, 128};
phn10 59:6b3a52d87465 25
spm71 62:b73067127fd6 26 void gnoll(int sensor_no, float sensor_values[]) {
spm71 62:b73067127fd6 27 int hit, miss = 0;
spm71 58:69f9a4607a16 28 int a_num;
spm71 58:69f9a4607a16 29 int led_command;
spm71 62:b73067127fd6 30 int whacker_no = 0;
spm71 62:b73067127fd6 31
spm71 62:b73067127fd6 32 if (sensor_no == 0)
spm71 62:b73067127fd6 33 whacker_no = 8;
phn10 60:604c10531f58 34
spm71 58:69f9a4607a16 35 for (int i = 0; i < 15; i++) {
spm71 62:b73067127fd6 36 pc.printf("Round %d\n", i + 1);
spm71 62:b73067127fd6 37 a_num = rand() % 8; // create random number 0-7
spm71 62:b73067127fd6 38 turn_to_target(stp_sensor_pos[a_num]); // turn motor to random target
spm71 62:b73067127fd6 39 lzr_on(); // turn laser on
spm71 62:b73067127fd6 40 wait(LASER_DELAY); // wait for laser to activate
spm71 62:b73067127fd6 41
spm71 62:b73067127fd6 42 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 62:b73067127fd6 43
spm71 62:b73067127fd6 44 if (sensor_values[sensor_no + a_num] > PTHRESH) { // confirm that the sensor is high
spm71 62:b73067127fd6 45 led_command = 0x0500 + led_values[a_num]; // create SPI command for LED
spm71 62:b73067127fd6 46 spi_send(as1107, led_command); // light up LED
spm71 62:b73067127fd6 47 }
spm71 62:b73067127fd6 48 else {
spm71 62:b73067127fd6 49 pc.printf("Error: sensor not activated. Shutting down.\n");
spm71 62:b73067127fd6 50 while(1);
spm71 58:69f9a4607a16 51 }
spm71 58:69f9a4607a16 52
spm71 58:69f9a4607a16 53 clock_t start = clock();
spm71 58:69f9a4607a16 54 clock_t finish = clock();
spm71 62:b73067127fd6 55 pc.printf("Waiting for whacker...\n");
phn10 60:604c10531f58 56
spm71 58:69f9a4607a16 57 do {
spm71 62:b73067127fd6 58 finish = clock(); // update timer
spm71 62:b73067127fd6 59 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 62:b73067127fd6 60 } while (finish - start < VOLLEY_DELAY or sensor_values[whacker_no + a_num] < PTHRESH); // check if timer expired or if sensor hit
spm71 62:b73067127fd6 61
spm71 62:b73067127fd6 62 if (sensor_values[whacker_no + a_num] > PTHRESH) {
spm71 62:b73067127fd6 63 hit++; // increment hit count
spm71 62:b73067127fd6 64 pc.printf("Hit\n");
spm71 62:b73067127fd6 65 while(sensor_values[whacker_no + a_num] > PTHRESH)
spm71 62:b73067127fd6 66 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off
spm71 62:b73067127fd6 67 }
spm71 62:b73067127fd6 68
spm71 62:b73067127fd6 69 else {
spm71 62:b73067127fd6 70 for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { // go through all whacker sensors
spm71 62:b73067127fd6 71 if (sensor_values[i + whacker_no] > PTHRESH and j != a_num) { // check if wrong sensor hit
spm71 62:b73067127fd6 72 pc.printf("Wrong sensor hit.\n");
spm71 62:b73067127fd6 73 i = 15; // ends volley
spm71 62:b73067127fd6 74 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
spm71 62:b73067127fd6 75 while(sensor_values[i + whacker_no] > PTHRESH)
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 pc.printf("Miss\n");
spm71 62:b73067127fd6 80 miss++; // increment miss count
spm71 62:b73067127fd6 81 }
spm71 62:b73067127fd6 82 update_score(hit, miss);
spm71 58:69f9a4607a16 83 }
phn10 60:604c10531f58 84 }
phn10 60:604c10531f58 85
phn10 59:6b3a52d87465 86 void whack(int sensor_no, float * sensor_values) {
phn10 59:6b3a52d87465 87 bool sensor_registered = false;
phn10 59:6b3a52d87465 88 int sensor_no = // --> need to specify sensor_no
phn10 60:604c10531f58 89
phn10 59:6b3a52d87465 90 // start timer for sensor reading
phn10 59:6b3a52d87465 91 t.start();
phn10 59:6b3a52d87465 92
phn10 59:6b3a52d87465 93 // keep reading until one of the sensor get laser pointed in
phn10 59:6b3a52d87465 94 while (!sensor_registered) {
phn10 59:6b3a52d87465 95 // read value of all gnoll sensor
phn10 59:6b3a52d87465 96 for (int i = 0; i < TGT_SENSOR_QUAN; i++) {
phn10 59:6b3a52d87465 97 // scan all 16 sensors into sensor_values array
phn10 59:6b3a52d87465 98 ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2);
phn10 59:6b3a52d87465 99 }
phn10 60:604c10531f58 100
phn10 59:6b3a52d87465 101 // detect which sensor get lasered on
phn10 59:6b3a52d87465 102 for (int i = 0; i < TGT_SENSOR_QUAN + sensor_no; i++) {
phn10 59:6b3a52d87465 103 if (sensor_values[i] > THRESHOLD_SIGNAL)
phn10 59:6b3a52d87465 104 sensor_registered = true;
phn10 59:6b3a52d87465 105 }
phn10 59:6b3a52d87465 106
phn10 59:6b3a52d87465 107 // if (reading sensor timer expired) display error and freeze
phn10 59:6b3a52d87465 108 t.stop();
phn10 59:6b3a52d87465 109 if (t.read() > EXPIRED_TIMER) {
phn10 59:6b3a52d87465 110 pc.printf("Error: Reading timer expired. See whack() function.\n");
phn10 59:6b3a52d87465 111 while (1);
phn10 59:6b3a52d87465 112 }
phn10 59:6b3a52d87465 113 }
phn10 59:6b3a52d87465 114
phn10 59:6b3a52d87465 115 // point whack’s laser to the corresponding sensor on it’s target array
phn10 59:6b3a52d87465 116
phn10 59:6b3a52d87465 117 // activate whacker’s laser
phn10 59:6b3a52d87465 118
phn10 59:6b3a52d87465 119 while (signal from corresponding sensor value > THRESHOLD_SIGNAL) {
phn10 59:6b3a52d87465 120 if (finding sensor timer expired) display error and freeze
phn10 59:6b3a52d87465 121 }
phn10 59:6b3a52d87465 122
phn10 59:6b3a52d87465 123 activate corresponding indicator
phn10 59:6b3a52d87465 124
phn10 59:6b3a52d87465 125 // waiting for gnoll laser to turn off to turn off
phn10 59:6b3a52d87465 126 while (gnoll’s laser turned on) {}
phn10 59:6b3a52d87465 127 // turn off laer gnoll
phn10 59:6b3a52d87465 128 lzr_off();
spm71 62:b73067127fd6 129 }
spm71 62:b73067127fd6 130
spm71 62:b73067127fd6 131 void update_score(int hit, int miss) {
spm71 62:b73067127fd6 132 int d1, d2, d3, d4 = 0;
spm71 62:b73067127fd6 133 if ( hit < 10)
spm71 62:b73067127fd6 134 d1 = hit;
spm71 62:b73067127fd6 135 else {
spm71 62:b73067127fd6 136 d1 = hit % 10;
spm71 62:b73067127fd6 137 d2 = (hit - d1)/10;
spm71 62:b73067127fd6 138 }
spm71 62:b73067127fd6 139 if ( miss < 10)
spm71 62:b73067127fd6 140 d3 = miss;
spm71 62:b73067127fd6 141 else {
spm71 62:b73067127fd6 142 d3 = miss % 10;
spm71 62:b73067127fd6 143 d4 = (miss - d3)/10;
spm71 62:b73067127fd6 144 }
spm71 62:b73067127fd6 145 spi_send(as1107, 0x0100 + d1);
spm71 62:b73067127fd6 146 spi_send(as1107, 0x0200 + d2);
spm71 62:b73067127fd6 147 spi_send(as1107, 0x0300 + d3);
spm71 62:b73067127fd6 148 spi_send(as1107, 0x0400 + d4);
phn10 59:6b3a52d87465 149 }