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
Diff: wag.cpp
- Revision:
- 72:aec869655869
- Parent:
- 71:6ced854bebb2
- Child:
- 73:e8f7ee59d3e5
--- a/wag.cpp Mon Apr 23 20:16:41 2018 +0000 +++ b/wag.cpp Mon Apr 23 21:21:29 2018 +0000 @@ -2,8 +2,8 @@ * EECS 397 * * Assignment Name: Lab 7: WaG -* -* Authors: Sam Morrison and Phong Nguyen +* +* Authors: Sam Morrison and Phong Nguyen * File name: wag.cpp * Purpose: Wag functions * @@ -28,14 +28,15 @@ int led_values[8] = {1, 2, 4, 8, 16, 32, 64, 128}; -void gnoll(int sensor_no, float * sensor_values) { +void gnoll(int sensor_no, float * sensor_values) +{ int hit, miss = 0; int a_num; int led_command; int msec; int whacker_no = 0; - + if (sensor_no == 0) whacker_no = 8; @@ -47,49 +48,54 @@ turn_to_target(stp_sensor_pos[a_num]); // turn motor to random target lzr_on(); // turn laser on wait(LASER_DELAY); // wait for laser to activate - + ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors + wait(LASER_DELAY); if (sensor_values[sensor_no + a_num] * 3.3 > PTTHRESH) { // confirm that the sensor is high led_command = 0x0500 + led_values[a_num]; // create SPI command for LED + pc.printf("led_command: %d\n", led_command); spi_send(as1107, led_command); // light up LED - } - else { + pc.printf("done sent"); + // NOTE: must put a wait() here or else the led won't light up + } else { pc.printf("sensor value: %f\n" ,sensor_values[sensor_no + a_num] * 3.3); pc.printf("Error: sensor not activated. Shutting down.\n"); while(1); } - + clock_t start = clock(); pc.printf("Waiting for whacker...\n"); - + do { ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors clock_t difference = clock() - start; msec = difference * 1000 / CLOCKS_PER_SEC; //pc.printf("Time elapsed: %d. Time limit: %d\n", msec, VOLLEY_DELAY); } while (msec < VOLLEY_DELAY and sensor_values[whacker_no + a_num] < PTTHRESH); // check if timer expired or if sensor hit - + if (sensor_values[whacker_no + a_num] * 3.3 > PTTHRESH) { hit++; // increment hit count pc.printf("Hit\n"); while(sensor_values[whacker_no + a_num] * 3.3 > PTTHRESH) ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off } - + else { for (int j = 0 + whacker_no; j < 8 + whacker_no; j++) { // go through all whacker sensors if (sensor_values[i + whacker_no] * 3.3 > PTTHRESH and j != a_num) { // check if wrong sensor hit pc.printf("Wrong sensor hit.\n"); i = 15; // ends volley - ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors + ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors while(sensor_values[i + whacker_no] * 3.3 > PTTHRESH) ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // waits for whacker laser to turn off } } pc.printf("Miss\n"); - miss++; // increment miss count + miss++; // increment miss count } + lzr_off(); + spi_send(as1107, 0x0500); update_score(hit, miss); } } @@ -98,39 +104,40 @@ * void whack(int sensor_no, float * sensor_values) * Description: function for whacker * - * Inputs: + * Inputs: * Parameters: - * int sensor_no: the + * int sensor_no: the * float * sensor_values: an float array that stores 16 sensor values * Globals: - * + * * Outputs: * Returns: void */ -void whack(int sensor_no, float * sensor_values) { +void whack(int sensor_no, float * sensor_values) +{ bool sensor_registered = false; int gnoll_sensor_indicator = 0; // the order of sensor that gnoller's laser points to int whack_sensor_indicator = 0; // the order of sensor that whacker's laser points to int gnoll_no = 0; int led_command = 0; // led command to display the indicator LED in whacker - + if (sensor_no == 0) gnoll_no = 8; if (sensor_no == 8) gnoll_no = 0; - + // start timer for sensor reading t.start(); - + // keep reading until one of the sensor get laser pointed in while (!sensor_registered) { // scan all 16 sensors into sensor_values array ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); - - // scan all gnoll sensors + + // scan all gnoll sensors for (int i = 0; i < TGT_SENSOR_QUAN; i++) { // detect which gnoll sensor get lasered on if (sensor_values[i + gnoll_no] * 3.3f > PTTHRESH) sensor_registered = true; - gnoll_sensor_indicator = i; + gnoll_sensor_indicator = i; } // if (reading sensor timer expired) display error and freeze @@ -138,30 +145,30 @@ if (t.read() > WHACK_EXPIRED_TIMER) { pc.printf("Error: Reading timer expired. See whack() function.\n"); while (1); - } + } } // point whack’s laser to the corresponding sensor on whack target array turn_to_target(stp_sensor_pos[sensor_no]); - + // turn on whacker's laser lzr_on(); - + // scan all 16 sensors into sensor_values array ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); - - t.start(); + + t.start(); sensor_registered = false; - + while (!sensor_registered) { - // scan all whack sensors + // scan all whack sensors for (int i = 0; i < TGT_SENSOR_QUAN; i++) { // detect which gnoll sensor get lasered on if (sensor_values[i + sensor_no] * 3.3f > PTTHRESH) sensor_registered = true; - whack_sensor_indicator = i; + whack_sensor_indicator = i; } - + // if timer expire display error and freeze t.stop(); if (t.read() > WHACK_EXPIRED_TIMER) { @@ -179,12 +186,13 @@ // scan all 16 sensors into sensor_values array ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); } while (sensor_values[gnoll_sensor_indicator + gnoll_no] * 3.3f > PTTHRESH); - + // turn off laer gnoll lzr_off(); } -void update_score(int hit, int miss) { +void update_score(int hit, int miss) +{ int d1, d2, d3, d4 = 0; if ( hit < 10) d1 = hit;