Therverson Kanavathy / Mbed 2 deprecated RTES-20m_Shuttle_Test-Client

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*-- CLIENT INCLUDES ---------------------------------------------------------*/
00002 #include "mbed.h"
00003 #include "EthernetInterface.h"
00004 /*-- GLOBAL CONSTANTS --------------------------------------------------------*/
00005 const int PORT = 7;                             //arbitrary port
00006 /*-- IP CONSTANT CONFIGURATION --*/
00007 static const char* SERVER_IP = "192.168.1.101"; //IP of server board
00008 static const char* CLIENT_IP = "192.168.1.102"; //IP of client board
00009 static const char* MASK = "255.255.255.0";      //mask          
00010 static const char* GATEWAY = "192.168.1.1";     //gateway
00011 /*-- INITIALISATION ----------------------------------------------------------*/
00012 Serial pc(USBTX, USBRX);        //create PC interface
00013 EthernetInterface eth;          //create ethernet
00014 UDPSocket sock;                 //creat Ethernet socket
00015 Endpoint server;                //create endpoint
00016 /* -- DECLARE LEDs for STATUS DISPLAY --*/
00017 DigitalOut red(LED_RED);    
00018 DigitalOut green(LED_GREEN);
00019 DigitalOut blue(LED_BLUE);
00020 /*-- DECLARE INTERRUPTS --*/
00021 InterruptIn touch(SW2);
00022 InterruptIn butt3(SW3);
00023 Timer t_level, t_shuttle; //define timers to measure each level and shuttle
00024 /*--VARIABLES------------------------------------------------------------*/
00025 int i; //Loop iteration counter
00026 int n;                  //size of received message
00027 char message[6] = {0,01,1,1,'\0'};  //MESSAGE status[1],level[2],blue[1],line[1]
00028 char c_status[1], c_level[2], c_blue[1], c_line[1];  //receival buffers
00029 
00030 /*---- Status of program? ----*/
00031 /*------ 0 = Loaded      --*/ 
00032 /*------ 1 = Start       --*/
00033 /*------ 2 = In Progress --*/
00034 /*------ 3 = Stop        --*/
00035 int status, level;       //Keep track of the level of the Beep Test
00036 bool line;               //Has the runner crossed the line?
00037 
00038 /*-- FUNCTION DECLARATIONS ---------------------------------------------------*/
00039 void init_usb(void);    //initializes pc.printf if required
00040 void init_eth(void);    //initializes Ethernet
00041 void init_load(void);   //Initialise program
00042 void send(void);        //Send packets
00043 void receive(void);     //Recv packets
00044 int main(void);         //main
00045 void end_eth(void);     //closes Ethernet socket
00046 
00047 /*-- INTERRUPT DEFINITIONS ---------------------------------------------------*/
00048 void ISR1()
00049 {
00050     //Did the runner cross line in time, before showing RED?
00051     if (red == 0) line = false; //Crossed line late
00052     else line = true; // Crossed line in time
00053 }
00054 void ISR2()
00055 {
00056     //Reset the program
00057     status =0;
00058 }
00059 
00060 /*-- FUNCTION DEFINITIONS ---------------------------------------------------*/
00061 /****INIT_USB BAUD RATE***/
00062 void init_usb(void)
00063 {
00064     pc.baud(9600);    //baud rate
00065 }
00066 
00067 /****OPEN_ETHERNET CONNECTION***/
00068 void init_eth(void)
00069 {
00070     eth.init(CLIENT_IP, MASK, GATEWAY);                  //set up IP
00071     eth.connect();                                       //connect ethernet
00072     pc.printf("\nCLIENT - Client IP Address is %s\r\n", eth.getIPAddress()); //get client IP address
00073     sock.init();                                         //initialize socket
00074     server.set_address(SERVER_IP, PORT);                 //set address of server
00075 }
00076 
00077 /**** INITIALISE CLIENT PARAMETERS***/
00078 void init_load(void)
00079 {
00080     //Initialise flags and reset timers
00081     level = 1; //Reset to starting level 1
00082     line = true;
00083     //Client LED is solid BLUE in Loaded Status
00084     blue = 0;
00085     green = 1;
00086     red = 1;
00087     //Reset the timers
00088     t_level.reset();
00089     t_shuttle.reset(); //20*3600/(8000+500*level) = 144/(level+16) sec
00090 }
00091 
00092 /**** SEND MESSAGE ***/
00093 void send(void)
00094 {
00095     sprintf(message, "%i %02i %i %i %c", status, level, int(blue), line, '\0');   
00096     sock.sendTo(server, message, sizeof(message));                //send message
00097     pc.printf("CLIENT: Send '%s' to server %s\r\n", message, SERVER_IP);//print message
00098 }
00099 
00100 /**** RECEIVE ***/
00101 void receive(void)
00102 {
00103     pc.printf("\nCLIENT: Waiting for UDP packet...\r\n");      //wait for packet
00104     n = sock.receiveFrom(server, message, sizeof(message)); //receive message from server
00105     message[n] = ' ';                               //Remove \0 from message
00106     pc.printf("CLIENT: Received '%s' from server %s\r\n", message, SERVER_IP);   //print message from server
00107     sscanf(message, "%s %s %s %s", &c_status, &c_level, &c_blue, &c_line);
00108     status = atoi(c_status);
00109     level = atoi(c_level);
00110     blue = atoi(c_blue);
00111     c_blue[0] = (blue == 0) ? '0' : '1';
00112     line = atoi(c_line);
00113     pc.printf("CLIENT: Converted '%i %i %c %i' from server \r\n", status, level, c_blue[0], line);   //print converted message
00114 }   //end receive()
00115 
00116 /****CLOSE_ETHERNET CONNECTION***/
00117 void end_eth(void){
00118     sock.close();       //close socket
00119     eth.disconnect();   //close Ethernet
00120 }
00121 
00122 /************ MAIN ************************************************************/
00123 int main(void)
00124 {
00125    touch.fall(&ISR1); // attach address ISR to interrupt falling edge
00126    butt3.rise(&ISR2); // attach address ISR to interrupt rising edge
00127    
00128     init_usb();     //initialize the PC interface
00129     init_eth();     //initialize the Ethernet connection
00130 
00131     while(true) { //repeat forever
00132         switch (status) {
00133             case 0 :
00134                 init_load(); //Display Program Loaded status -> Flash Blue
00135                 send();    //Connect to server
00136                 receive(); //Wait for START trigger from Server
00137                 break;
00138             case 1 : //Start the Beep Test
00139                 blue = 1; //Make sure Blue is OFF
00140                 //2 second COUNTDOWN red, purple, blue, GO!
00141                 red = 0;
00142                 wait_ms(500);
00143                 blue = 0;
00144                 wait_ms(500);
00145                 red = 1;
00146                 wait_ms(500);
00147                 blue = 1;
00148                 status = 2;
00149                 line = true;
00150                 for (i=0; i<4; i++) {
00151                     green = !green;
00152                     wait_ms(250);
00153                 }
00154                 green = 0;
00155                 wait(72/(level - 1)); //half(t_shuttle - 1)
00156                 red = 0;
00157                 wait(72/(level)); //half(t_shuttle)
00158                 send();
00159                 red = 1;
00160                 break;
00161             case 2 :  //Wait for message
00162                 receive();
00163                 if (line == false) {
00164                     if (blue == 1) blue = 0;
00165                     else {
00166                         status = 3; //Stop the Beep Test
00167                         break;
00168                     }
00169                 } //end if-statement
00170                 line = true; //reset the line flag
00171                 if (t_level.read() >= 60) {
00172                     level++; //New level
00173                     t_level.reset();
00174                     t_level.start();//New level - Restart timer
00175                     for (i=0; i<4; i++) { //Flash green to indicate new level
00176                         green = !green;
00177                         wait_ms(250);
00178                     }
00179                 } //end if-statement
00180                 red = 1;
00181                 green = 0; //Display Solid green
00182                 wait(144/(level+16)/2); //t_shuttle = 20*3600/(8000+500*level)
00183                 red = 0; //Display solid Yellow
00184                 wait(144/(level+16)/2);
00185                 send();               //Send message to server
00186                 green = 1;
00187                 break;
00188             case 3 : //Stop the Beep Test, delay, then reload
00189                 red = blue = 0;
00190                 send();               //Send message to client                
00191                 //LED FLASHES PURPLE at 1HZ square waves for 2 minutes
00192                 for (i=0; i<180; i++){
00193                     red = !red;
00194                     blue = !blue;
00195                     wait_ms(500);
00196                 }
00197                 status = 0; //Reset the Beep Test
00198                 break;
00199             default :
00200                 red = 0; green = 1;
00201                 for (i=0; i<60; i++) { //FLASHING red green at 2Hz - status-error
00202                     red = !red;
00203                     green = !green;
00204                     wait_ms(250);
00205                 }
00206                 return(0);
00207                 } //End switch-statement
00208     }//End while-loop
00209     
00210 }   //end main()