Thomas Morris / Mbed OS PROJ324_Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SERIAL_COMMANDS.cpp Source File

SERIAL_COMMANDS.cpp

00001 #include "SERIAL_COMMANDS.hpp"
00002 int NetworkWaitTime;            //Waiting time to update the network
00003 char input[100] = {};           //Character array initialised to NULL
00004 int steps = 0;
00005 int direction = 1;
00006 int Motor_To_Select = 1;
00007 float angle = 0;
00008 bool move_loop = 0;
00009 bool List_move_loop = 0;
00010 
00011 
00012 //Mutex Locks
00013 
00014 void Serial_Commands_Output()                                       //Used for getting input from the user to determine the opperations to perform
00015 {
00016     if(Log_Value == 4){pc.printf("In Serial_Commands\n");}      //If logging is enabled, print debug statement
00017         
00018     for (int x = 0; x < 100; x++){input[x] = ' ';};             //Fill input with spaces
00019         
00020     pc.printf("Please type in a command\n");                    //Request command in the terminal
00021     cin.getline(input,sizeof(input),'\r');                      //Scan into input from the start of the line to the return character       
00022 
00023 
00024     //LOGGING
00025     if(input[0] == 'L' & input[1] == 'O' & input[2] == 'G' & input[3] == 'G' & input[4] == 'I' & input[5] == 'N' & input[6] == 'G' & input[7] == ' ')
00026     {
00027         int NumberOfChars = 0; int ArrayAddress = 0; string LoggingNumber; int NumberToLogging;         //Declare required variables
00028         while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}                            //Count the number of characters entered into the console
00029         for(int x=8; x < NumberOfChars; x++){LoggingNumber += input[x];}                                //Concatenate the characters between the space and the end
00030         stringstream Number(LoggingNumber);                                                             //Convert string to stringstream
00031         Number >> NumberToLogging;                                                                      //Convert stringstream to integer
00032         if      (NumberToLogging == 0){pc.printf("NOT LOGGING\n");}                                     //Not Logging
00033         else if (NumberToLogging == 1){pc.printf("LOGGING SPI\n");}                                     //Logging SPI
00034 
00035         if (NumberToLogging <= 5){Log_Value = NumberToLogging;}                                         //If inputted value is within bounds equate it to the log state
00036     } 
00037 
00038     //Motor Control
00039     else if(input[0] == 'M' & input[1] == 'o' & input[2] == 't' & input[3] == 'o' & input[4] == 'r' & input[5] == ' ')
00040     {
00041         int NumberOfChars = 0; int ArrayAddress = 0; string MotorNumber; int Motor_Number;         //Declare required variables
00042         while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}                            //Count the number of characters entered into the console
00043         for(int x=6; x < NumberOfChars; x++){MotorNumber += input[x];}                                //Concatenate the characters between the space and the end
00044         stringstream Number(MotorNumber);                                                             //Convert string to stringstream
00045         Number >> Motor_Number;                                                                      //Convert stringstream to integer
00046         if(Motor_Number < 1 || Motor_Number > 6)
00047         {
00048             Motor_Number =0;
00049             pc.printf("Please Select a motor from 1-6\n");
00050         }
00051         Motor_To_Select = Motor_Number;
00052         //Step Control
00053         pc.printf("Enter the angle to rotate\n");                    //Request command in the terminal
00054         scanf("%f",&angle);
00055         //cin.getline(input,sizeof(input),'\r');                      //Scan into input from the start of the line to the return character
00056            //if(input[0] == 'S' & input[1] == 't' & input[2] == 'e' & input[3] == 'p' & input[4] == 's' & input[5] == ' ')
00057         {
00058             //angle = input;
00059             if(angle < 0)
00060             {
00061                 direction = 0;//Anti Clockwise   
00062             }
00063             else
00064             {
00065                 direction = 1;//Clockwise   
00066             }
00067             float new_angle = abs(angle/1.8);//Converts input to steps                                                     
00068             steps = new_angle;
00069             pc.printf("The Motor is: %d\n The Number to step is: %d\n", Motor_Number, steps);
00070         }
00071        
00072     }
00073     //MOVE
00074     else if(input[0] == 'M' & input[1] == 'O' & input[2] == 'V' & input[3] == 'E')
00075     { 
00076         move_loop  = 1;
00077         pc.printf("Enter the Move\n");                    //Request command in the terminal
00078         while(move_loop == 1)
00079         {            
00080             cin.getline(input,sizeof(input),'\r');
00081             
00082             if(input[0] == 'A' & input[1] == 'F'){STEPPER_MOTOR_1.Rotate_Steps(50 ,1);}
00083             else if(input[0] == 'C' & input[1] == 'F'){STEPPER_MOTOR_1.Rotate_Steps(50 ,0);}
00084             else if(input[0] == 'A' & input[1] == 'B'){STEPPER_MOTOR_2.Rotate_Steps(50 ,1);}
00085             else if(input[0] == 'C' & input[1] == 'B'){STEPPER_MOTOR_2.Rotate_Steps(50 ,0);}
00086             else if(input[0] == 'A' & input[1] == 'L'){STEPPER_MOTOR_3.Rotate_Steps(50 ,1);}
00087             else if(input[0] == 'C' & input[1] == 'L'){STEPPER_MOTOR_3.Rotate_Steps(50 ,0);}
00088             else if(input[0] == 'A' & input[1] == 'R'){STEPPER_MOTOR_4.Rotate_Steps(50 ,1);}
00089             else if(input[0] == 'C' & input[1] == 'R'){STEPPER_MOTOR_4.Rotate_Steps(50 ,0);}
00090             else if(input[0] == 'A' & input[1] == 'U'){STEPPER_MOTOR_5.Rotate_Steps(50 ,1);}
00091             else if(input[0] == 'C' & input[1] == 'U'){STEPPER_MOTOR_5.Rotate_Steps(50 ,0);}
00092             else if(input[0] == 'A' & input[1] == 'D'){STEPPER_MOTOR_6.Rotate_Steps(50 ,1);}
00093             else if(input[0] == 'C' & input[1] == 'D'){STEPPER_MOTOR_6.Rotate_Steps(50 ,0);}
00094             else if(input[0] == 'M' & input[1] == 'O' & input[2] == 'V' & input[3] == 'E')
00095             {
00096                 move_loop = 0;
00097                 pc.printf("Exiting Move loop\n");
00098                 //Exit moves function   
00099             }
00100             else
00101             {   
00102                 pc.printf("Error please enter a allowed move\n");
00103                 //Error case   
00104             }
00105             
00106         }
00107     }
00108     //LIST 
00109     else if(input[0] == 'L' & input[1] == 'I' & input[2] == 'S' & input[3] == 'T')//This is where the move list shall be added to
00110     { 
00111         List_move_loop  = 1;
00112         pc.printf("Entered the Move_List Section\n");                    //Request command in the terminal
00113         while(List_move_loop == 1)
00114         {            
00115             cin.getline(input,sizeof(input),'\r');
00116             
00117             if(input[0] == 'A' & input[1] == 'F'){Move_list[Move_list_pointer] = 1;      Move_list_pointer = Move_list_pointer + 1;}
00118             else if(input[0] == 'C' & input[1] == 'F'){Move_list[Move_list_pointer] = 2; Move_list_pointer = Move_list_pointer + 1;}
00119             else if(input[0] == 'A' & input[1] == 'B'){Move_list[Move_list_pointer] = 3; Move_list_pointer = Move_list_pointer + 1;}
00120             else if(input[0] == 'C' & input[1] == 'B'){Move_list[Move_list_pointer] = 4; Move_list_pointer = Move_list_pointer + 1;}
00121             else if(input[0] == 'A' & input[1] == 'L'){Move_list[Move_list_pointer] = 5; Move_list_pointer = Move_list_pointer + 1;}
00122             else if(input[0] == 'C' & input[1] == 'L'){Move_list[Move_list_pointer] = 6; Move_list_pointer = Move_list_pointer + 1;}
00123             else if(input[0] == 'A' & input[1] == 'R'){Move_list[Move_list_pointer] = 7; Move_list_pointer = Move_list_pointer + 1;}
00124             else if(input[0] == 'C' & input[1] == 'R'){Move_list[Move_list_pointer] = 8; Move_list_pointer = Move_list_pointer + 1;}
00125             else if(input[0] == 'A' & input[1] == 'U'){Move_list[Move_list_pointer] = 9; Move_list_pointer = Move_list_pointer + 1;}
00126             else if(input[0] == 'C' & input[1] == 'U'){Move_list[Move_list_pointer] = 10;Move_list_pointer = Move_list_pointer + 1;}
00127             else if(input[0] == 'A' & input[1] == 'D'){Move_list[Move_list_pointer] = 11;Move_list_pointer = Move_list_pointer + 1;}
00128             else if(input[0] == 'C' & input[1] == 'D'){Move_list[Move_list_pointer] = 12;Move_list_pointer = Move_list_pointer + 1;}
00129             
00130             
00131             else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] =='T' & input[5] == 'E')
00132             {   
00133                 pc.printf("Delete Clear Test\n");
00134                 memset(Move_list, 0, sizeof(Move_list));
00135                 Move_list_pointer = 0;
00136                 pc.printf("Move List array and pointer reset\n");
00137                 List_move_loop = 0;
00138                 pc.printf("Exiting Move List loop\n");
00139             }
00140             else if(input[0] == 'L' & input[1] == 'I' & input[2] == 'S' & input[3] == 'T')
00141             {
00142                 List_move_loop = 0;
00143                 pc.printf("Exiting Move List loop\n");
00144                 //Exit moves function   
00145             }
00146             else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D')
00147             {
00148                 pc.printf("Printing out the move list\n");
00149                 for(int i=0;i <= Move_list_pointer; i+=1)
00150                 {
00151                     pc.printf("Value %d is %d\n",i,Move_list[i]);
00152                 }
00153                 pc.printf("Move List test passed\n");
00154                 List_move_loop = 0;
00155                 pc.printf("Exiting Move List loop\n");
00156                 //Exit moves function   
00157             }
00158             else if(input[0] == 'R' & input[1] == 'U' & input[2] == 'N')
00159             {
00160                 List_move_loop = 0;
00161                 {
00162                 pc.printf("Move point value is : %d\n",Move_list_pointer);
00163                 for(int i = 0; i<= Move_list_pointer; i += 1)
00164                 {
00165                     if(Move_list[i] == 0){pc.printf("Move List Finished\n");}
00166                     else if (Move_list[i] == 1){STEPPER_MOTOR_1.Rotate_Steps(50 ,1);}
00167                     else if (Move_list[i] == 2){STEPPER_MOTOR_1.Rotate_Steps(50 ,0);}  
00168                     else if (Move_list[i] == 3){STEPPER_MOTOR_2.Rotate_Steps(50 ,1);}  
00169                     else if (Move_list[i] == 4){STEPPER_MOTOR_2.Rotate_Steps(50 ,0);}  
00170                     else if (Move_list[i] == 5){STEPPER_MOTOR_3.Rotate_Steps(50 ,1);}  
00171                     else if (Move_list[i] == 6){STEPPER_MOTOR_3.Rotate_Steps(50 ,0);}  
00172                     else if (Move_list[i] == 7){STEPPER_MOTOR_4.Rotate_Steps(50 ,1);}  
00173                     else if (Move_list[i] == 8){STEPPER_MOTOR_4.Rotate_Steps(50 ,0);}  
00174                     else if (Move_list[i] == 9){STEPPER_MOTOR_5.Rotate_Steps(50 ,1);}  
00175                     else if (Move_list[i] == 10){STEPPER_MOTOR_5.Rotate_Steps(50 ,0);}  
00176                     else if (Move_list[i] == 11){STEPPER_MOTOR_6.Rotate_Steps(50 ,1);}  
00177                     else if (Move_list[i] == 12){STEPPER_MOTOR_6.Rotate_Steps(50 ,0);}  
00178                     else pc.printf("Finished\n");   
00179                 }
00180                  pc.printf("List Run sucessful\n");
00181             }
00182                 pc.printf("List execution finished\n");
00183             }
00184             //TEST RUN
00185             else if(input[0] == 'T' & input[1] == 'E' & input[2] == 'S' & input[3] == 'T' & input[4] == '_' & input[5] == 'R' & input[6] == 'U' & input[7] == 'N')//This is the test run code
00186             {
00187                 memset(Move_list, 0, sizeof(Move_list));
00188                 Move_list_pointer = 0;
00189                 Move_list[Move_list_pointer] = 1 ;Move_list_pointer = Move_list_pointer + 1;
00190                 Move_list[Move_list_pointer] = 2 ;Move_list_pointer = Move_list_pointer + 1;
00191                 Move_list[Move_list_pointer] = 3 ;Move_list_pointer = Move_list_pointer + 1;
00192                 Move_list[Move_list_pointer] = 4 ;Move_list_pointer = Move_list_pointer + 1;
00193                 Move_list[Move_list_pointer] = 5 ;Move_list_pointer = Move_list_pointer + 1;
00194                 Move_list[Move_list_pointer] = 6 ;Move_list_pointer = Move_list_pointer + 1;
00195                 Move_list[Move_list_pointer] = 7 ;Move_list_pointer = Move_list_pointer + 1;
00196                 Move_list[Move_list_pointer] = 8 ;Move_list_pointer = Move_list_pointer + 1;
00197                 Move_list[Move_list_pointer] = 9 ;Move_list_pointer = Move_list_pointer + 1;
00198                 Move_list[Move_list_pointer] = 10 ;Move_list_pointer = Move_list_pointer + 1;
00199                 Move_list[Move_list_pointer] = 11 ;Move_list_pointer = Move_list_pointer + 1;
00200                 Move_list[Move_list_pointer] = 12 ;Move_list_pointer = Move_list_pointer + 1;
00201                 
00202                 pc.printf("Array filled for test\n");
00203                 List_move_loop = 0;
00204                 pc.printf("Exiting Move List loop\n");
00205             }
00206             else
00207             {   
00208                 pc.printf("Error please enter a allowed move\n");
00209                 //Error case   
00210             }
00211             
00212         }
00213     }
00214     //Colours
00215     if(input[0] == 'C' & input[1] == 'O' & input[2] == 'L' & input[3] == 'O' & input[4] == 'U' & input[5] == 'R' & input[6] == 'S' & input[7] == ' ')
00216     {
00217         int NumberOfChars = 0; int ArrayAddress = 0; string COLOURSNUBMER; int NUMBERTOCOLOUR;         //Declare required variables
00218         while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}                            //Count the number of characters entered into the console
00219         for(int x=8; x < NumberOfChars; x++){COLOURSNUBMER += input[x];}                                //Concatenate the characters between the space and the end
00220         stringstream Number(COLOURSNUBMER);                                                             //Convert string to stringstream
00221         Number >> NUMBERTOCOLOUR;
00222         int n = NUMBERTOCOLOUR -1 ;                                                                      //Convert stringstream to integer
00223         pc.printf("Showing side %D\n",NUMBERTOCOLOUR);
00224         
00225         
00226         pc.printf("%c,%c,%c\n%c,%c,%c\n%c,%c,%c\n",
00227         Value_convert(CubeMap[n][0][0]),Value_convert(CubeMap[n][0][1]),Value_convert(CubeMap[n][0][2])
00228         ,Value_convert(CubeMap[n][1][0]),Value_convert(CubeMap[n][1][1]),Value_convert(CubeMap[n][1][2])
00229         ,Value_convert(CubeMap[n][2][0]),Value_convert(CubeMap[n][2][1]),Value_convert(CubeMap[n][2][2]));;//Side n
00230         
00231 
00232     } 
00233     //SIDE_ADD
00234     if(input[0] == 'S' & input[1] == 'I' & input[2] == 'D' & input[3] == 'E' & input[4] == '_' & input[5] == 'A' & input[6] == 'D' & input[7] == 'D' & input[8] == ' ')
00235     {
00236         int NumberOfChars = 0; int ArrayAddress = 0; string SIDENUMBER; int NUMBERTOSIDE;         //Declare required variables
00237         while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}                            //Count the number of characters entered into the console
00238         for(int x=9; x < NumberOfChars; x++){SIDENUMBER += input[x];}                                //Concatenate the characters between the space and the end
00239         stringstream Number(SIDENUMBER);                                                             //Convert string to stringstream
00240         Number >> NUMBERTOSIDE;
00241         pc.printf("Please enter in the colour of segment 1:\n");
00242         int Colour_value;
00243         cin >>Colour_value;
00244         NUMBERTOSIDE = NUMBERTOSIDE - 1;
00245         
00246         CubeMap[NUMBERTOSIDE][0][0] = convert(Colour_value);
00247         pc.printf("Please enter in the colour of segment 2:\n");
00248         cin >>Colour_value;
00249         CubeMap[NUMBERTOSIDE][0][1] = convert(Colour_value);
00250         pc.printf("Please enter in the colour of segment 3:\n");
00251         cin >>Colour_value;
00252         CubeMap[NUMBERTOSIDE][0][2] = convert(Colour_value);
00253         pc.printf("Please enter in the colour of segment 4:\n");
00254         cin >>Colour_value;
00255         CubeMap[NUMBERTOSIDE][1][0] = convert(Colour_value);
00256         pc.printf("Please enter in the colour of segment 5:\n");
00257         cin >>Colour_value;
00258         CubeMap[NUMBERTOSIDE][1][1] = convert(Colour_value);
00259         pc.printf("Please enter in the colour of segment 6:\n");
00260         cin >>Colour_value;
00261         CubeMap[NUMBERTOSIDE][1][2] = convert(Colour_value);
00262         pc.printf("Please enter in the colour of segment 7:\n");
00263         cin >>Colour_value;
00264         CubeMap[NUMBERTOSIDE][2][0] = convert(Colour_value);
00265         pc.printf("Please enter in the colour of segment 8:\n");
00266         cin >>Colour_value;
00267         CubeMap[NUMBERTOSIDE][2][1] = convert(Colour_value);
00268         pc.printf("Please enter in the colour of segment 9:\n");
00269         cin >>Colour_value;
00270         CubeMap[NUMBERTOSIDE][2][2] = convert(Colour_value);
00271         pc.printf("Side filled\n");
00272         cin.getline(input,sizeof(input),'\r');
00273         }
00274     //SIDE_CLEAR
00275     if(input[0] == 'S' & input[1] == 'I' & input[2] == 'D' & input[3] == 'E' & input[4] == '_' & input[5] == 'C' & input[6] == 'L' & input[7] == 'E' & input[8] == 'A' & input[9] == 'R')
00276     {
00277     
00278         for(int i =0; i<=6;i++)
00279         {
00280             CubeMap[i][0][0] = convert(0);
00281             CubeMap[i][0][1] = convert(0);
00282             CubeMap[i][0][2] = convert(0);
00283             CubeMap[i][1][0] = convert(0);
00284             CubeMap[i][1][1] = convert(0);
00285             CubeMap[i][1][2] = convert(0);
00286             CubeMap[i][2][0] = convert(0);
00287             CubeMap[i][2][1] = convert(0);
00288             CubeMap[i][2][2] = convert(0);
00289         }
00290         pc.printf("All sides cleared of data\n");
00291     }
00292     //HELP
00293     else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P')
00294     { 
00295         pc.printf("Please specify the function you would like help in\nMOTOR , LOGGING , MOVE \n");  //Print introduction line
00296         cin.getline(input,sizeof(input),'\r');
00297         if(input[0] == 'M' & input[1] == 'O' & input[2] == 'T' & input[3] == 'O' & input[4] == 'R'){pc.printf("Motor x selects a motor in the range of 1-6\nThen you issue the angle which is a number either positive or negative\n");}
00298         else if(input[0] == 'L' & input[1] == 'O' & input[2] == 'G' & input[3] == 'G' & input[4] == 'I' & input[5] == 'N' & input[6] == 'G'){pc.printf("Logging enables to user to get more information from the specific area which is logged\n");}
00299         else if(input[0] == 'M' & input[1] == 'O' & input[2] == 'V' & input[3] == 'E'){pc.printf("Allows the user to enter in a movement\n");} 
00300         else if(input[0] == 'L' & input[1] == 'I' & input[2] == 'S' & input[3] == 'T'){pc.printf("Allows the user to to view / set a list of movements that can be performed\n");} 
00301         else if(input[0] == 'C' & input[1] == 'O' & input[2] == 'L' & input[3] == 'O' & input[4] == 'U' &  input[5] == 'R' & input[6] == 'S'){pc.printf("Allows the user to view and set the information upon the cubemap\n");} 
00302         else{pc.printf("Enter the specific help area for more information\n");}
00303     }
00304     else 
00305     {
00306         pc.printf("Please enter an acceptable command\n");
00307     }
00308 }