Plymouth ELEC351 Group T
/
ELEC351_Group_T
FINAL PROJECT isn't it
Fork of ELEC351 by
Diff: SERIAL_COMMANDS.cpp
- Revision:
- 53:71f59e195f06
- Parent:
- 52:99915f5240b2
- Child:
- 55:e0e684531825
--- a/SERIAL_COMMANDS.cpp Tue Jan 09 22:27:49 2018 +0000 +++ b/SERIAL_COMMANDS.cpp Mon May 07 15:44:34 2018 +0000 @@ -1,8 +1,11 @@ #include "SERIAL_COMMANDS.hpp" int NetworkWaitTime; //Waiting time to update the network char input[100] = {}; //Character array initialised to NULL +int steps = 0; +int direction = 1; +int Motor_To_Select = 1; float Sample_Rate = 15.0; //Initial sampling rate - +float angle = 0; Ticker Sampling_Timer; //Controls Sampling Rate //Mutex Locks @@ -15,159 +18,14 @@ pc.printf("Please type in a command\n"); //Request command in the terminal cin.getline(input,sizeof(input),'\r'); //Scan into input from the start of the line to the return character + //cout << "Input is : " << input << endl; //READ ALL if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L') { if(Log_Value == 4){pc.printf("READ ALL Confirmed\n");} //If logging is enabled, print debug statement - - SERIAL_COMMANDS_Lock.lock(); //Apply MUTEX lock - int Start_Address_Read_All = Write_Pointer; //Store Write_Pointer in temporary variable - SERIAL_COMMANDS_Lock.unlock(); //Release MUTEX lock - - for(int x_Read_All = (Start_Address_Read_All - mailsize); x_Read_All != Start_Address_Read_All; x_Read_All = x_Read_All ) //From the oldest sample to the newest sample - { - if(x_Read_All < 0){x_Read_All = x_Read_All + mailsize;} //If the address has gone below 0 add mailsize to it - else {x_Read_All = x_Read_All;} //If the adress hasn't gone below 0 keep it the same - if(Log_Value == 4){pc.printf("Address Reading From: %d\n",x_Read_All);} //If logging is enabled, print debug statement - - DATA Serial_Read_Data = Read_Data(x_Read_All); //Read from target address into temporary variable - - time_t Time = Serial_Read_Data.get_time(); //Assign the sampled time to Time - tm* Time_Pointer = localtime(&Time); //Create pointer to Time - - pc.printf("Date = %02d %02d %d ,\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900)); //Print Date String - pc.printf("Time = %02d:%02d:%02d ,\t", Time_Pointer->tm_hour, Time_Pointer->tm_min, Time_Pointer->tm_sec); //Print Time sTRING - pc.printf("Temperature = %f ,\t", Serial_Read_Data.get_temperature()); //Print Temperature - pc.printf("Pressure = %f ,\t", Serial_Read_Data.get_pressure()); //Print Pressure - pc.printf("Light = %f ,\n", Serial_Read_Data.get_light()); //Print Light - - if(x_Read_All == mailsize - 1){x_Read_All = 0;} //If read address is mailsize - 1 set the read adress to 0 - else {x_Read_All = x_Read_All + 1;} //If read address is not mailsize increment the read address - } - } - - //DELETE ALL - else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ' & input[7] == 'A' & input[8] == 'L' & input[9] == 'L') - { - Sampling_Timer.detach(); //Detach the sampling interrupt - if(Log_Value == 4){pc.printf("DELETE ALL Confirmed\n");} //If logging is enabled, print debug statement - - SERIAL_COMMANDS_Lock.lock(); //Apply MUTEX lock - int Start_Address = Write_Pointer; //Store Write_Pointer in a temporary variable - SERIAL_COMMANDS_Lock.unlock(); //Release MUTEX lock - - for(int x_Delete_All = (Start_Address - mailsize); x_Delete_All != Start_Address; x_Delete_All = x_Delete_All) //From the oldest sample to the newest sample - { - if(x_Delete_All < 0){x_Delete_All = x_Delete_All + mailsize;} //If the target is less than 0 add mailsize to the target - else {x_Delete_All = x_Delete_All;} //If the target is not less than 0 keep it the same - if(Log_Value == 4){pc.printf("Address Deleting From: %d\n",x_Delete_All);} //If logging is enabled, print debug statement - - Delete_Data(x_Delete_All); //Delete DATA in the target address - - if(x_Delete_All == mailsize - 1){x_Delete_All = 0;} //If the target is mailsize - 1 then set the new target to 0 - else {x_Delete_All = x_Delete_All + 1;} //If the target is not mailsize - 1 then increment the target - - } - pc.printf("DELETED %d RECORDS\n",mailsize); //Print number of records which have been deleted - Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); //Reattach the sampling interrupt - } - - //READ - else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ') - { - if(Log_Value == 4){pc.printf("READ n Confirmed\n");} //If logging is enabled, print the debug statement - int NumberOfChars = 0; int ArrayAddress = 0; string ReadNumber; int NumberToRead; //Declare required variables - while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters typed in the console - for(int x=5; x < NumberOfChars; x++){ReadNumber += input[x];} //Concatenate characters from console into a string - stringstream Number(ReadNumber); //Convert string into a stringstream - Number >> NumberToRead; //Convert stringstream into an integer - - if(Log_Value == 4){pc.printf("Number of samples to Read %d\n",NumberToRead);} //If logging is enabled, print the debug statement - - if(NumberToRead > mailsize){NumberToRead = mailsize;} //If more records are chosen than exist in the buffer limit the selection to the buffer size - - SERIAL_COMMANDS_Lock.lock(); //Apply MUTEX lock - int Start_Address = Write_Pointer; //Copy Write_Pointer into temporary variable - SERIAL_COMMANDS_Lock.unlock(); //Release MUTEX lock - - for(int x = (Start_Address - NumberToRead); x != Start_Address; x = x ) //From newest - input number to newest - { - if(Log_Value == 4){pc.printf("Address Reading From: %d\n",x);} //If logging is enabled, print the debug statement - if(x < 0){x = x + mailsize;} //If target is less than 0 add mailsize - else {x = x;} //If target is not less than 0 keep it the same - - DATA Serial_Read_Data = Read_Data(x); //Read DATA from target - - time_t Time = Serial_Read_Data.get_time(); //Assign sampled time to Time - tm* Time_Pointer = localtime(&Time); //Create a pointer to Time - - pc.printf("Date = %02d %02d %d ,\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900)); //Print the Date - pc.printf("Time = %02d:%02d:%02d ,\t", Time_Pointer->tm_hour, Time_Pointer->tm_min, Time_Pointer->tm_sec); //Print the Time - pc.printf("Temperature = %f ,\t", Serial_Read_Data.get_temperature()); //Print the Temperature - pc.printf("Pressure = %f ,\t", Serial_Read_Data.get_pressure()); //Print the Pressure - pc.printf("Light = %f ,\n", Serial_Read_Data.get_light()); //Print the Light - - if(x == mailsize - 1){x = 0;} //If target is mailsize - 1 set the new target to 0 - else {x = x + 1;} //If target is not mailsize - 1 increment the target - } - if(Log_Value == 4){pc.printf("Read %d samples\n",NumberToRead);} //If logging is enabled, print the debug statement - } - - //DELETE - else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ') - { - Sampling_Timer.detach(); //Detach sampling interrupt - if(Log_Value == 4){pc.printf("DELETE N Confirmed\n");} //If logging is enabled, print debug statement - int NumberOfChars = 0; int ArrayAddress = 0; string DeleteNumber; int NumberToDelete; //Declare required variables - while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters typed in the console - for(int x=7; x < NumberOfChars; x++){DeleteNumber += input[x];} //Concatenate characters from console into a string - stringstream Number(DeleteNumber); //Convert string into a stringstream - Number >> NumberToDelete; //Convert stringstream into an integer - - if(NumberToDelete > mailsize){NumberToDelete = mailsize;} //If more records are chosen than exist in the buffer limit the selection to the buffer size - - if(Log_Value == 4){pc.printf("Deleted %d samples\n",NumberToDelete);} //If logging is enabled, print debug statement - - SERIAL_COMMANDS_Lock.lock(); //Apply MUTEX lock - int Start_Address = Write_Pointer; //Copy Write_Pointer into temporary variable - SERIAL_COMMANDS_Lock.unlock(); //Release MUTEX lock - - for(int x_Delete = (Start_Address - NumberToDelete); x_Delete != Start_Address; x_Delete = x_Delete ) //From newest - input number to newest - { - if(Log_Value == 4){pc.printf("Address Deleting From: %d\n",x_Delete);} //If logging is enabled, print debug statement - if(x_Delete < 0){x_Delete = x_Delete + mailsize;} //If target is less than 0 add mailsize - else {x_Delete = x_Delete;} //If target is not less than 0 keep it the same - - Delete_Data(x_Delete); //Delete DATA at specified address - - if(x_Delete == mailsize - 1){x_Delete = 0;} //If target is mailsize - 1 set target to 0 - else {x_Delete = x_Delete + 1;} //If target is not mailsize - 1 increment the target - - } - pc.printf("DELETED %d RECORDS\n",NumberToDelete); //Print number of records which have been deleted - if(Log_Value == 4){pc.printf("Deleted %d samples\n",NumberToDelete);} //If logging is enabled, print the debug statement - Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); //Attach sampling interrupt - } - - //STATE - else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'A' & input[3] == 'T' & input[4] == 'E' & input[5] == ' ') - { - if(Log_Value == 4){pc.printf("STATE Confirmed\n");} //If logging is enabled, print debug statement - int NumberOfChars = 0; int ArrayAddress = 0; string StateNumber; int NumberToState; //Declare required variables - while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters typed in the console - for(int x=6; x < NumberOfChars; x++){StateNumber += input[x];} //Concatenate characters from console into a string - stringstream Number(StateNumber); //Convert string into a stringstream - Number >> NumberToState; //Convert stringstream into an integer - - if(Log_Value == 4){pc.printf("STATE Set to %d\n",NumberToState);} //If logging is enabled, print debug statement - - if(NumberToState==0) {Sampling_Timer.detach(); pc.printf("SAMPLING 0\n");} //If input is 0 stop sampling - else if(NumberToState==1) {Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);pc.printf("SAMPLING 1\n");} //If input is 1 start sampling - else {pc.printf("Invalid State\n");} //If other input received return error message - - } - + + } //SETDATE else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'D' & input[4] == 'A' & input[5] == 'T' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ') { @@ -217,59 +75,6 @@ set_new_time(NumberToHour,NumberToMinute,NumberToSecond); //Set New Time pc.printf("TIME UPDATED TO %02d %02d %02d\n",NumberToHour,NumberToMinute,NumberToSecond); //Print Updated Time } - - //SETT - else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == ' ') - { - if(Log_Value == 4){pc.printf("SETT Confirmed\n");} //If logging is enabled, print debug statement - int NumberOfChars = 0; int ArrayAddress = 0; //Declare required variables - string SettNumber; double NumberToSett; //Declare required variables - int Decimal_Divider = 10; //Declare required variables - float Sample_Rate_Integer = 0; string Sample_Rate_Integer_String; //Declare required variables - float Sample_Rate_Decimal = 0; string Sample_Rate_Decimal_String; //Declare required variables - - while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console - - if (Decimal_Check(input) == true) //If a decimal is present - { - for(int x = 5; x < Decimal_Position(input); x++){Sample_Rate_Integer_String += input[x];} //Concatenate characters from the space to the decimal - stringstream Number_1(Sample_Rate_Integer_String); //Convert string to a stringstream - Number_1 >> Sample_Rate_Integer; //Convert stringstream to a integer - - for(int x = Decimal_Position(input) + 1; x < NumberOfChars; x++){Sample_Rate_Decimal_String += input[x];} //Concatenate characters from the decimal to the end - stringstream Number_2(Sample_Rate_Decimal_String); //Convert string to a stringstream - Number_2 >> Sample_Rate_Decimal; //Convert stringstream to a integer - - for(int tens_power = 1; tens_power != NumberOfChars - Decimal_Position(input) - 1; tens_power++) //For each position after the decimal place - { - Decimal_Divider = Decimal_Divider * 10; //Multiply the divider by another power of 10 - } - NumberToSett = Sample_Rate_Integer + (Sample_Rate_Decimal / float(Decimal_Divider)); //Divide the number after the decimal by the number of characters after the decimal - } - else //If a decimal is not present - { - for(int x=5; x < NumberOfChars; x++){SettNumber += input[x];} //Concatenate characters from the space to the end - stringstream Number_3(SettNumber); //Convert string to a stringstream - Number_3 >> NumberToSett; //Convert stringstream to an integer - } - - if(Log_Value == 4){pc.printf("Sample Rate Input %d\n",NumberToSett);} //If logging is enabled, print debug statement - if(Log_Value == 4){pc.printf("Decimal Rate Input %d\n",Sample_Rate_Decimal);} //If logging is enabled, print debug statement - if(Log_Value == 4){pc.printf("Integer Rate Input %d\n",Sample_Rate_Integer);} //If logging is enabled, print debug statement - - if(NumberToSett >= 0.1 & NumberToSett <= 60) //If inputted number is between 0.1 and 60.0 - { - Sample_Rate = NumberToSett; //Set the sample rate to that input number - pc.printf("T UPDATED TO %1.1f \n",NumberToSett); //Print updated sample rate - } - else //If inputted number is not within bounds - { - Sample_Rate = Sample_Rate; //Don't change the sample rate - pc.printf("OUT OF RANGE\n"); //Print out of range error warning - } - Sampling_Timer.detach(); //Detach the sampling interrupt - Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); //Reattach the sampling interrupt with the new rate - } //LOGGING else if(input[0] == 'L' & input[1] == 'O' & input[2] == 'G' & input[3] == 'G' & input[4] == 'I' & input[5] == 'N' & input[6] == 'G' & input[7] == ' ') { @@ -288,7 +93,74 @@ if (NumberToLogging <= 5){Log_Value = NumberToLogging;} //If inputted value is within bounds equate it to the log state } - + //Steps + /* + else if(input[0] == 'S' & input[1] == 't' & input[2] == 'e' & input[3] == 'p' & input[4] == 's' & input[5] == ' ') + { + int NumberOfChars = 0; int ArrayAddress = 0; string SteppingNumber; int NumberToStep; //Declare required variables + while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console + for(int x=6; x < NumberOfChars; x++){SteppingNumber += input[x];} //Concatenate the characters between the space and the end + stringstream Number(SteppingNumber); //Convert string to stringstream + Number >> NumberToStep; //Convert stringstream to integer + if(NumberToStep <0) + { + NumberToStep =0; + } + steps = NumberToStep; + pc.printf("Number to step is %d\n", steps); + } + */ + //Motor Control + else if(input[0] == 'M' & input[1] == 'o' & input[2] == 't' & input[3] == 'o' & input[4] == 'r' & input[5] == ' ') + { + int NumberOfChars = 0; int ArrayAddress = 0; string MotorNumber; int Motor_Number; //Declare required variables + while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console + for(int x=6; x < NumberOfChars; x++){MotorNumber += input[x];} //Concatenate the characters between the space and the end + stringstream Number(MotorNumber); //Convert string to stringstream + Number >> Motor_Number; //Convert stringstream to integer + if(Motor_Number < 1 || Motor_Number > 6) + { + Motor_Number =0; + pc.printf("Please Select a motor from 1-6\n"); + } + Motor_To_Select = Motor_Number; + //Step Control + pc.printf("Enter the angle to rotate\n"); //Request command in the terminal + scanf("%f",&angle); + //cin.getline(input,sizeof(input),'\r'); //Scan into input from the start of the line to the return character + //if(input[0] == 'S' & input[1] == 't' & input[2] == 'e' & input[3] == 'p' & input[4] == 's' & input[5] == ' ') + { + //angle = input; + if(angle < 0) + { + direction = 0; + } + else + { + direction = 1; + } + float new_angle = abs(angle/1.8); + steps = new_angle; + pc.printf("The Motor is: %d\n The Number to step is: %d\n", Motor_Number, steps); + } + + } + //Motor Direction + else if(strcmp(input,"Motor") == 0) + { + motor_direction = 1; + pc.printf("Motor direction set to clockwise\n"); + } + else if(strcmp(input,"AntiClockwise") == 0) + { + motor_direction = 0; + pc.printf("Motor direction set to Anti clockwise\n"); + } + //Test + else if(strcmp(input,"Test") == 0) + { + pc.printf("Test Passed\n"); + } //HELP else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P') { @@ -297,31 +169,8 @@ pc.printf("SETT <T>\nSTATE <x>\nLOGGING <x>\t 1:LCD\t 2:Networking\t 3:Sampling\t 4:Serial Commands\t 5:SD Card\nNETSET <n>\nHELP\n"); //Print list of commands } - - //NETSET - else if(input[0] == 'N' & input[1] == 'E' & input[2] == 'T' & input[3] == 'S' & input[4] == 'E' & input[5] == 'T' & input[6] == ' ')// Use this to display all of the availble commands - { - if(Log_Value == 4){pc.printf("NETSET confirmed\n");} - int NumberOfChars = 0; int ArrayAddress = 0; string NetSetNumber; int NumberToNetSet; - while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} - for(int x=7; x < NumberOfChars; x++){NetSetNumber += input[x];} - stringstream Number(NetSetNumber); - Number >> NumberToNetSet; - if(Log_Value == 4){pc.printf("NETSET Number %d",NumberToNetSet);} - if(NumberToNetSet < 1) - { - NumberToNetSet = 1; - } - else - { - NumberToNetSet = NumberToNetSet; - } - NetworkWaitTime = (NumberToNetSet * 1000); - } - else { pc.printf("Please enter an acceptable command\n"); } - -} +} \ No newline at end of file