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
- Committer:
- spm71
- Date:
- 2018-04-19
- Revision:
- 66:70efa618c378
- Parent:
- 65:b8f3339b9268
- Child:
- 67:6dffd5c82eb6
File content as of revision 66:70efa618c378:
/******************************************************************************
* EECS 397
*
* Assignment Name: Lab 7: WaG
*
* Authors: Sam Morrison and Phong Nguyen
* File name: wag.cpp
* Purpose: Wag functions
*
* Created: 04/12/2018
* Last Modified: 04/12/2018
*
******************************************************************************/
#include "mbed.h"
#include "io_pins.h"
#include "wag.h"
#include "spi.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
extern Timer t;
extern spi_cfg as1107;
extern int stp_sensor_pos[TGT_SENSOR_QUAN];
int led_values[8] = {1, 2, 4, 8, 16, 32, 64, 128};
void gnoll(int sensor_no, float * sensor_values) {
int hit, miss = 0;
int a_num;
int led_command;
int whacker_no = 0;
if (sensor_no == 0)
whacker_no = 8;
for (int i = 0; i < 15; i++) {
pc.printf("Round %d\n", i + 1);
a_num = rand() % 8; // create random number 0-7
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
if (sensor_values[sensor_no + a_num] > PTTHRESH) { // confirm that the sensor is high
led_command = 0x0500 + led_values[a_num]; // create SPI command for LED
spi_send(as1107, led_command); // light up LED
}
else {
pc.printf("Error: sensor not activated. Shutting down.\n");
while(1);
}
clock_t start = clock();
clock_t finish = clock();
pc.printf("Waiting for whacker...\n");
do {
finish = clock(); // update timer
ana_scan_mux(sensor_values, TGT_SENSOR_QUAN * 2); // scan all sensors
} while (finish - start < VOLLEY_DELAY or sensor_values[whacker_no + a_num] < PTTHRESH); // check if timer expired or if sensor hit
if (sensor_values[whacker_no + a_num] > PTTHRESH) {
hit++; // increment hit count
pc.printf("Hit\n");
while(sensor_values[whacker_no + a_num] > 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] > 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
while(sensor_values[i + whacker_no] > 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
}
update_score(hit, miss);
}
}
/*
* void whack(int sensor_no, float * sensor_values)
* Description: function for whacker
*
* Inputs:
* Parameters:
* 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) {
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
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;
}
// if (reading sensor timer expired) display error and freeze
t.stop();
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();
sensor_registered = false;
while (!sensor_registered) {
// 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;
}
// if timer expire display error and freeze
t.stop();
if (t.read() > WHACK_EXPIRED_TIMER) {
pc.printf("Error: Reading timer expired. See whack() function.\n");
while(1);
}
}
// activate corresponding indicator in whack LED row
led_command = 0x0500 + led_values[whack_sensor_indicator];
spi_send(as1107, led_command);
// waiting for gnoll laser to turn off to turn off
do {
// 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) {
int d1, d2, d3, d4 = 0;
if ( hit < 10)
d1 = hit;
else {
d1 = hit % 10;
d2 = (hit - d1)/10;
}
if ( miss < 10)
d3 = miss;
else {
d3 = miss % 10;
d4 = (miss - d3)/10;
}
spi_send(as1107, 0x0100 + d1);
spi_send(as1107, 0x0200 + d2);
spi_send(as1107, 0x0300 + d3);
spi_send(as1107, 0x0400 + d4);
}
