Wearable armour target.
Dependencies: communication mbed
Introduction
This module functions as the wearable armour target. The armour interfaces with a connected gun via serial bus, via another gun with IR communication, and via the scoreboard via wireless communication.
Components
- FRDM - KL25Z mbed board
- nRF24L01 wireless communication module
- tsmp1138 IR receiver module
Functionality
The armour module handles recording hits to the player and sending the data back to the scoreboard.
It also handles game state commands from the scoreboard and relays the appropriate command to the gun.
Revision 1:6db021f1bba2, committed 2015-06-12
- Comitter:
- Huqi
- Date:
- Fri Jun 12 14:00:54 2015 +0000
- Parent:
- 0:e4f628ef776e
- Commit message:
- Final build
Changed in this revision
communication.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r e4f628ef776e -r 6db021f1bba2 communication.lib --- a/communication.lib Thu Jun 11 15:28:37 2015 +0000 +++ b/communication.lib Fri Jun 12 14:00:54 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/Oxford-cwm-laser-tag/code/communication/#7c0f460e2717 +http://developer.mbed.org/teams/Oxford-cwm-laser-tag/code/communication/#4051d24c7906
diff -r e4f628ef776e -r 6db021f1bba2 main.cpp --- a/main.cpp Thu Jun 11 15:28:37 2015 +0000 +++ b/main.cpp Fri Jun 12 14:00:54 2015 +0000 @@ -1,14 +1,36 @@ +/* Armour Program + +Authors: Jamieson Brynes and Qi Hu + +This program is designed for use with the gun and scoreboard program from the Laser Tag - University of Oxford CWM 2015 project. + +A few notes: + +For sending data to the gun +'a' ==> Enable gun +'z' ==> Send back bullet count data +'B' ==> Disable gun + +For data received from the IR +'a' ==> Player 1 +'z' ==> Player 2 +'B' ==> Player 3 +52 ==> Static target ( does not contribute to score) + +*/ + #include "mbed.h" #include "comm.h" DigitalOut myled(LED1); -Serial RX(NC,PTE1); //Set up serial for IR sensor -DigitalOut LEDHigh(PTE30); //Need to define a port for this -DigitalOut LEDLow(PTE29); //Need to define a port for this -Serial gun(PTE22,PTE23); //Need to define a port for this -Serial pc(USBTX, USBRX); //Pc serial for debugging -DigitalOut gunEnableFlag(PTE21); //Flag for gunEnable +Serial RX(NC,PTE1); //Set up serial for IR sensor +DigitalOut LEDHigh(PTE30); //Pin for LED high value +DigitalOut LEDLow(PTE29); //Pin for LED low value +Serial gun(PTE22,PTE23); //Serial communication between the armour and the gun +Serial pc(USBTX, USBRX); //Pc serial for debugging +DigitalOut gunEnableFlag(PTE21); //Flag for gunEnable +//Initialize functions void checkHit(); void checkScoreboard(); void gameStart(); @@ -17,45 +39,59 @@ void gameContinue(); void powerDown(); void sendGunInfo(char code); +void powerUp(); +//Initialize communication buffers char* rxBuffer = new char[4]; char* txBuffer = new char[4]; -int readSize = 0; -int writeSize = 0; char gameOn; //Initialize boolean for game state +Timer t; //Timer for the power down situation + + int main() { - - + + //Initialize txBuffer + for (int i = 0; i < 4; i++) { + txBuffer[i] = 0; + } RX.baud(1000); //Set baud rate for the receiver - + //Set default values for the LEDs and game state LEDLow = 0; - LEDHigh = 1; - - + LEDHigh = 0; + gameOn = 0; - - gameOn = 1; + + commInit(3); //Initialize communications - commInit(1); //Initialize communications - - +while(true) +{ while(gameOn) { + //Game loop checkHit(); - //checkScoreboard(); + checkScoreboard(); } while(!gameOn) { + //Loop to run when game is off checkScoreboard(); + if(t.read() > 5) //Check to see if player has been dead for long enough + { + powerUp(); + t.stop(); + t.reset(); + } + } + } } @@ -63,13 +99,13 @@ void checkHit() { char identifier = 0; - pc.printf("Looking\n\r"); + //Logic to see if we have been hit if(RX.readable()) { - while(identifier != 'a' && identifier != 'z' && identifier != 'B') { + //Read until we find a value we recognize + while(identifier != 'a' && identifier != 'z' && identifier != 'B' && identifier != 52) { identifier = RX.getc(); - pc.putc(identifier); } } @@ -91,6 +127,7 @@ } + //If not hit, continue as normal @@ -99,18 +136,17 @@ void checkScoreboard() { - // comm check, needs to be done regularly if(commRead(rxBuffer)) { //Get gun shots and load it in sendGunInfo('z'); - wait(1); + wait(0.5); if(gun.readable()) { char shots; shots = gun.getc(); - txBuffer[3] = shots; + txBuffer[3] += shots; while(gun.readable()) { @@ -118,33 +154,41 @@ trash = gun.getc(); } } + commSetAck(txBuffer); + pc.printf("Refreshing acknowledge buffer \n\r"); switch(rxBuffer[0]) { - case 0: //code to be executed on start signal e.g. game = true - + case 0: //code to be executed on start signal + pc.printf("Starting\n\r"); gameStart(); + //wait(2); break; - case 1: // code to be executed on a get hits request, probably nothing - - + case 1: // code to be executed on a get hits request + pc.printf("Received get request.\n\r"); + txBuffer[3] = 0; + break; - case 2: // code to be executed on end signal e.g. game = false - + case 2: // code to be executed on end signal + pc.printf("Ending game"); gameEnd(); break; - case 3: // code to be executed on pause signal e.g. pause = true + case 3: // code to be executed on pause signal gamePause(); break; - case 4: // code to be executed on resume signal e.g. pause = false + case 4: // code to be executed on resume signal gameContinue(); break; + default: + commSetAck(txBuffer); + + break; } } @@ -154,31 +198,23 @@ void powerDown() { - sendGunInfo('a'); - - - //Blinking LEDs - //Switch LEDs off - for(int i = 0; i < 5; i++) - { - LEDHigh = 0; - wait(0.1); - LEDHigh = 1; - wait(0.1); - } + sendGunInfo('B'); LEDHigh = 0; + gameOn = 0; //Time delay for the armour to be down - wait(4); + t.start(); +} - +void powerUp() +{ //Switch LEDs on LEDHigh = 1; - //Enable gun - sendGunInfo('a'); + + gameOn = 1; @@ -189,24 +225,27 @@ char trash; trash = RX.getc(); } - - } void gameStart() { + pc.printf("Starting game\n\r"); //Enable gun sendGunInfo('a'); //Switch LEDs on LEDHigh = 1; + //Enable game loop gameOn = 1; + //Reset scoring buffer for (int i = 0; i < 4; i++) { txBuffer[i] = 0; } + pc.printf("Ending start game routine\n\r"); + } @@ -214,33 +253,57 @@ void gameEnd() { + //Set flag and send data to gun gameOn = 0; - sendGunInfo('a'); + sendGunInfo('B'); + //Turn off LED LEDHigh = 0; + + while(RX.readable()) + { + //Loop to wipe buffer of the receiver + //Need to ignore any shots received during the power down phase + char trash; + trash = RX.getc(); + } } void gamePause() { + //Set flag and send data to gun gameOn = 0; - sendGunInfo('a'); + sendGunInfo('B'); + t.stop(); + } void gameContinue() { + //Set flag and send data to gun gameOn = 1; sendGunInfo('a'); + + while(RX.readable()) + { + //Loop to wipe buffer of the receiver + //Need to ignore any shots received during the power down phase + char trash; + trash = RX.getc(); + } + + if(t.read() > 0) + { + t.start(); + } } void sendGunInfo(char code) - { + //Send flag to gun and push a code to them gunEnableFlag = 1; gun.putc(code); gunEnableFlag = 0; -} - - - +} \ No newline at end of file