Shuttle Test Client

Dependencies:   mbed

Committer:
tkanavathy
Date:
Fri Jan 13 13:14:07 2017 +0000
Revision:
0:447b25550052
T Kanavathy RTES Final Project Client

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tkanavathy 0:447b25550052 1 /*-- CLIENT INCLUDES ---------------------------------------------------------*/
tkanavathy 0:447b25550052 2 #include "mbed.h"
tkanavathy 0:447b25550052 3 #include "EthernetInterface.h"
tkanavathy 0:447b25550052 4 /*-- GLOBAL CONSTANTS --------------------------------------------------------*/
tkanavathy 0:447b25550052 5 const int PORT = 7; //arbitrary port
tkanavathy 0:447b25550052 6 /*-- IP CONSTANT CONFIGURATION --*/
tkanavathy 0:447b25550052 7 static const char* SERVER_IP = "192.168.1.101"; //IP of server board
tkanavathy 0:447b25550052 8 static const char* CLIENT_IP = "192.168.1.102"; //IP of client board
tkanavathy 0:447b25550052 9 static const char* MASK = "255.255.255.0"; //mask
tkanavathy 0:447b25550052 10 static const char* GATEWAY = "192.168.1.1"; //gateway
tkanavathy 0:447b25550052 11 /*-- INITIALISATION ----------------------------------------------------------*/
tkanavathy 0:447b25550052 12 Serial pc(USBTX, USBRX); //create PC interface
tkanavathy 0:447b25550052 13 EthernetInterface eth; //create ethernet
tkanavathy 0:447b25550052 14 UDPSocket sock; //creat Ethernet socket
tkanavathy 0:447b25550052 15 Endpoint server; //create endpoint
tkanavathy 0:447b25550052 16 /* -- DECLARE LEDs for STATUS DISPLAY --*/
tkanavathy 0:447b25550052 17 DigitalOut red(LED_RED);
tkanavathy 0:447b25550052 18 DigitalOut green(LED_GREEN);
tkanavathy 0:447b25550052 19 DigitalOut blue(LED_BLUE);
tkanavathy 0:447b25550052 20 /*-- DECLARE INTERRUPTS --*/
tkanavathy 0:447b25550052 21 InterruptIn touch(SW2);
tkanavathy 0:447b25550052 22 InterruptIn butt3(SW3);
tkanavathy 0:447b25550052 23 Timer t_level, t_shuttle; //define timers to measure each level and shuttle
tkanavathy 0:447b25550052 24 /*--VARIABLES------------------------------------------------------------*/
tkanavathy 0:447b25550052 25 int i; //Loop iteration counter
tkanavathy 0:447b25550052 26 int n; //size of received message
tkanavathy 0:447b25550052 27 char message[6] = {0,01,1,1,'\0'}; //MESSAGE status[1],level[2],blue[1],line[1]
tkanavathy 0:447b25550052 28 char c_status[1], c_level[2], c_blue[1], c_line[1]; //receival buffers
tkanavathy 0:447b25550052 29
tkanavathy 0:447b25550052 30 /*---- Status of program? ----*/
tkanavathy 0:447b25550052 31 /*------ 0 = Loaded --*/
tkanavathy 0:447b25550052 32 /*------ 1 = Start --*/
tkanavathy 0:447b25550052 33 /*------ 2 = In Progress --*/
tkanavathy 0:447b25550052 34 /*------ 3 = Stop --*/
tkanavathy 0:447b25550052 35 int status, level; //Keep track of the level of the Beep Test
tkanavathy 0:447b25550052 36 bool line; //Has the runner crossed the line?
tkanavathy 0:447b25550052 37
tkanavathy 0:447b25550052 38 /*-- FUNCTION DECLARATIONS ---------------------------------------------------*/
tkanavathy 0:447b25550052 39 void init_usb(void); //initializes pc.printf if required
tkanavathy 0:447b25550052 40 void init_eth(void); //initializes Ethernet
tkanavathy 0:447b25550052 41 void init_load(void); //Initialise program
tkanavathy 0:447b25550052 42 void send(void); //Send packets
tkanavathy 0:447b25550052 43 void receive(void); //Recv packets
tkanavathy 0:447b25550052 44 int main(void); //main
tkanavathy 0:447b25550052 45 void end_eth(void); //closes Ethernet socket
tkanavathy 0:447b25550052 46
tkanavathy 0:447b25550052 47 /*-- INTERRUPT DEFINITIONS ---------------------------------------------------*/
tkanavathy 0:447b25550052 48 void ISR1()
tkanavathy 0:447b25550052 49 {
tkanavathy 0:447b25550052 50 //Did the runner cross line in time, before showing RED?
tkanavathy 0:447b25550052 51 if (red == 0) line = false; //Crossed line late
tkanavathy 0:447b25550052 52 else line = true; // Crossed line in time
tkanavathy 0:447b25550052 53 }
tkanavathy 0:447b25550052 54 void ISR2()
tkanavathy 0:447b25550052 55 {
tkanavathy 0:447b25550052 56 //Reset the program
tkanavathy 0:447b25550052 57 status =0;
tkanavathy 0:447b25550052 58 }
tkanavathy 0:447b25550052 59
tkanavathy 0:447b25550052 60 /*-- FUNCTION DEFINITIONS ---------------------------------------------------*/
tkanavathy 0:447b25550052 61 /****INIT_USB BAUD RATE***/
tkanavathy 0:447b25550052 62 void init_usb(void)
tkanavathy 0:447b25550052 63 {
tkanavathy 0:447b25550052 64 pc.baud(9600); //baud rate
tkanavathy 0:447b25550052 65 }
tkanavathy 0:447b25550052 66
tkanavathy 0:447b25550052 67 /****OPEN_ETHERNET CONNECTION***/
tkanavathy 0:447b25550052 68 void init_eth(void)
tkanavathy 0:447b25550052 69 {
tkanavathy 0:447b25550052 70 eth.init(CLIENT_IP, MASK, GATEWAY); //set up IP
tkanavathy 0:447b25550052 71 eth.connect(); //connect ethernet
tkanavathy 0:447b25550052 72 pc.printf("\nCLIENT - Client IP Address is %s\r\n", eth.getIPAddress()); //get client IP address
tkanavathy 0:447b25550052 73 sock.init(); //initialize socket
tkanavathy 0:447b25550052 74 server.set_address(SERVER_IP, PORT); //set address of server
tkanavathy 0:447b25550052 75 }
tkanavathy 0:447b25550052 76
tkanavathy 0:447b25550052 77 /**** INITIALISE CLIENT PARAMETERS***/
tkanavathy 0:447b25550052 78 void init_load(void)
tkanavathy 0:447b25550052 79 {
tkanavathy 0:447b25550052 80 //Initialise flags and reset timers
tkanavathy 0:447b25550052 81 level = 1; //Reset to starting level 1
tkanavathy 0:447b25550052 82 line = true;
tkanavathy 0:447b25550052 83 //Client LED is solid BLUE in Loaded Status
tkanavathy 0:447b25550052 84 blue = 0;
tkanavathy 0:447b25550052 85 green = 1;
tkanavathy 0:447b25550052 86 red = 1;
tkanavathy 0:447b25550052 87 //Reset the timers
tkanavathy 0:447b25550052 88 t_level.reset();
tkanavathy 0:447b25550052 89 t_shuttle.reset(); //20*3600/(8000+500*level) = 144/(level+16) sec
tkanavathy 0:447b25550052 90 }
tkanavathy 0:447b25550052 91
tkanavathy 0:447b25550052 92 /**** SEND MESSAGE ***/
tkanavathy 0:447b25550052 93 void send(void)
tkanavathy 0:447b25550052 94 {
tkanavathy 0:447b25550052 95 sprintf(message, "%i %02i %i %i %c", status, level, int(blue), line, '\0');
tkanavathy 0:447b25550052 96 sock.sendTo(server, message, sizeof(message)); //send message
tkanavathy 0:447b25550052 97 pc.printf("CLIENT: Send '%s' to server %s\r\n", message, SERVER_IP);//print message
tkanavathy 0:447b25550052 98 }
tkanavathy 0:447b25550052 99
tkanavathy 0:447b25550052 100 /**** RECEIVE ***/
tkanavathy 0:447b25550052 101 void receive(void)
tkanavathy 0:447b25550052 102 {
tkanavathy 0:447b25550052 103 pc.printf("\nCLIENT: Waiting for UDP packet...\r\n"); //wait for packet
tkanavathy 0:447b25550052 104 n = sock.receiveFrom(server, message, sizeof(message)); //receive message from server
tkanavathy 0:447b25550052 105 message[n] = ' '; //Remove \0 from message
tkanavathy 0:447b25550052 106 pc.printf("CLIENT: Received '%s' from server %s\r\n", message, SERVER_IP); //print message from server
tkanavathy 0:447b25550052 107 sscanf(message, "%s %s %s %s", &c_status, &c_level, &c_blue, &c_line);
tkanavathy 0:447b25550052 108 status = atoi(c_status);
tkanavathy 0:447b25550052 109 level = atoi(c_level);
tkanavathy 0:447b25550052 110 blue = atoi(c_blue);
tkanavathy 0:447b25550052 111 c_blue[0] = (blue == 0) ? '0' : '1';
tkanavathy 0:447b25550052 112 line = atoi(c_line);
tkanavathy 0:447b25550052 113 pc.printf("CLIENT: Converted '%i %i %c %i' from server \r\n", status, level, c_blue[0], line); //print converted message
tkanavathy 0:447b25550052 114 } //end receive()
tkanavathy 0:447b25550052 115
tkanavathy 0:447b25550052 116 /****CLOSE_ETHERNET CONNECTION***/
tkanavathy 0:447b25550052 117 void end_eth(void){
tkanavathy 0:447b25550052 118 sock.close(); //close socket
tkanavathy 0:447b25550052 119 eth.disconnect(); //close Ethernet
tkanavathy 0:447b25550052 120 }
tkanavathy 0:447b25550052 121
tkanavathy 0:447b25550052 122 /************ MAIN ************************************************************/
tkanavathy 0:447b25550052 123 int main(void)
tkanavathy 0:447b25550052 124 {
tkanavathy 0:447b25550052 125 touch.fall(&ISR1); // attach address ISR to interrupt falling edge
tkanavathy 0:447b25550052 126 butt3.rise(&ISR2); // attach address ISR to interrupt rising edge
tkanavathy 0:447b25550052 127
tkanavathy 0:447b25550052 128 init_usb(); //initialize the PC interface
tkanavathy 0:447b25550052 129 init_eth(); //initialize the Ethernet connection
tkanavathy 0:447b25550052 130
tkanavathy 0:447b25550052 131 while(true) { //repeat forever
tkanavathy 0:447b25550052 132 switch (status) {
tkanavathy 0:447b25550052 133 case 0 :
tkanavathy 0:447b25550052 134 init_load(); //Display Program Loaded status -> Flash Blue
tkanavathy 0:447b25550052 135 send(); //Connect to server
tkanavathy 0:447b25550052 136 receive(); //Wait for START trigger from Server
tkanavathy 0:447b25550052 137 break;
tkanavathy 0:447b25550052 138 case 1 : //Start the Beep Test
tkanavathy 0:447b25550052 139 blue = 1; //Make sure Blue is OFF
tkanavathy 0:447b25550052 140 //2 second COUNTDOWN red, purple, blue, GO!
tkanavathy 0:447b25550052 141 red = 0;
tkanavathy 0:447b25550052 142 wait_ms(500);
tkanavathy 0:447b25550052 143 blue = 0;
tkanavathy 0:447b25550052 144 wait_ms(500);
tkanavathy 0:447b25550052 145 red = 1;
tkanavathy 0:447b25550052 146 wait_ms(500);
tkanavathy 0:447b25550052 147 blue = 1;
tkanavathy 0:447b25550052 148 status = 2;
tkanavathy 0:447b25550052 149 line = true;
tkanavathy 0:447b25550052 150 for (i=0; i<4; i++) {
tkanavathy 0:447b25550052 151 green = !green;
tkanavathy 0:447b25550052 152 wait_ms(250);
tkanavathy 0:447b25550052 153 }
tkanavathy 0:447b25550052 154 green = 0;
tkanavathy 0:447b25550052 155 wait(72/(level - 1)); //half(t_shuttle - 1)
tkanavathy 0:447b25550052 156 red = 0;
tkanavathy 0:447b25550052 157 wait(72/(level)); //half(t_shuttle)
tkanavathy 0:447b25550052 158 send();
tkanavathy 0:447b25550052 159 red = 1;
tkanavathy 0:447b25550052 160 break;
tkanavathy 0:447b25550052 161 case 2 : //Wait for message
tkanavathy 0:447b25550052 162 receive();
tkanavathy 0:447b25550052 163 if (line == false) {
tkanavathy 0:447b25550052 164 if (blue == 1) blue = 0;
tkanavathy 0:447b25550052 165 else {
tkanavathy 0:447b25550052 166 status = 3; //Stop the Beep Test
tkanavathy 0:447b25550052 167 break;
tkanavathy 0:447b25550052 168 }
tkanavathy 0:447b25550052 169 } //end if-statement
tkanavathy 0:447b25550052 170 line = true; //reset the line flag
tkanavathy 0:447b25550052 171 if (t_level.read() >= 60) {
tkanavathy 0:447b25550052 172 level++; //New level
tkanavathy 0:447b25550052 173 t_level.reset();
tkanavathy 0:447b25550052 174 t_level.start();//New level - Restart timer
tkanavathy 0:447b25550052 175 for (i=0; i<4; i++) { //Flash green to indicate new level
tkanavathy 0:447b25550052 176 green = !green;
tkanavathy 0:447b25550052 177 wait_ms(250);
tkanavathy 0:447b25550052 178 }
tkanavathy 0:447b25550052 179 } //end if-statement
tkanavathy 0:447b25550052 180 red = 1;
tkanavathy 0:447b25550052 181 green = 0; //Display Solid green
tkanavathy 0:447b25550052 182 wait(144/(level+16)/2); //t_shuttle = 20*3600/(8000+500*level)
tkanavathy 0:447b25550052 183 red = 0; //Display solid Yellow
tkanavathy 0:447b25550052 184 wait(144/(level+16)/2);
tkanavathy 0:447b25550052 185 send(); //Send message to server
tkanavathy 0:447b25550052 186 green = 1;
tkanavathy 0:447b25550052 187 break;
tkanavathy 0:447b25550052 188 case 3 : //Stop the Beep Test, delay, then reload
tkanavathy 0:447b25550052 189 red = blue = 0;
tkanavathy 0:447b25550052 190 send(); //Send message to client
tkanavathy 0:447b25550052 191 //LED FLASHES PURPLE at 1HZ square waves for 2 minutes
tkanavathy 0:447b25550052 192 for (i=0; i<180; i++){
tkanavathy 0:447b25550052 193 red = !red;
tkanavathy 0:447b25550052 194 blue = !blue;
tkanavathy 0:447b25550052 195 wait_ms(500);
tkanavathy 0:447b25550052 196 }
tkanavathy 0:447b25550052 197 status = 0; //Reset the Beep Test
tkanavathy 0:447b25550052 198 break;
tkanavathy 0:447b25550052 199 default :
tkanavathy 0:447b25550052 200 red = 0; green = 1;
tkanavathy 0:447b25550052 201 for (i=0; i<60; i++) { //FLASHING red green at 2Hz - status-error
tkanavathy 0:447b25550052 202 red = !red;
tkanavathy 0:447b25550052 203 green = !green;
tkanavathy 0:447b25550052 204 wait_ms(250);
tkanavathy 0:447b25550052 205 }
tkanavathy 0:447b25550052 206 return(0);
tkanavathy 0:447b25550052 207 } //End switch-statement
tkanavathy 0:447b25550052 208 }//End while-loop
tkanavathy 0:447b25550052 209
tkanavathy 0:447b25550052 210 } //end main()