Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MODSERIAL FATFileSystem
Diff: MbedLogger/MbedLogger.cpp
- Revision:
- 67:c86a4b464682
- Parent:
- 63:6cb0405fc6e6
- Child:
- 68:8f549749b8ce
--- a/MbedLogger/MbedLogger.cpp Tue Jun 19 20:14:23 2018 +0000 +++ b/MbedLogger/MbedLogger.cpp Mon Jun 25 15:44:00 2018 +0000 @@ -16,6 +16,8 @@ _received_filename = ""; _log_file_line_counter = 0; //used to set timer in finite state machine based on size of log + + _heading_string = "state_string,state_ID,timer,depth_cmd,depth_ft,pitch_cmd,pitch_deg,bce_cmd,bce_mm,batt_cmd,batt_mm,pitchRate_degs,depthRate_fps,sys_amps,sys_volts,int_press\n"; } //this function has to be called for the time to function correctly @@ -45,6 +47,18 @@ _data_log[9] = pitchLoop().getVelocity(); // pitchRate_degs (degrees per second) _data_log[10] = depthLoop().getVelocity(); // depthRate_fps (feet per second) + float internal_pressure = 5.0 * ((0.009 * adc().readCh5()) - 0.095); // Press_Xducer (on-board) + float voltage_input = 7.8 * adc().readCh6(); //Vin_Mon on PCB schematic (system input voltage from power supply) + + float current_input = adc().readCh7(); // I_mon (appears to be one-to-one from the schematic) + + // presssure onboard + + + _data_log[11] = current_input; // i_in + _data_log[12] = voltage_input; // v_in + _data_log[13] = internal_pressure; // vac_per + //check what the current state is and create that string string string_state; if (current_state == SIT_IDLE) @@ -73,19 +87,16 @@ string_state = "POSITION_DIVE"; else if (current_state == POSITION_RISE) string_state = "POSITION_RISE"; - - - -// else if (current_state == PITCH_TUNER_DEPTH) -// string_state = "PITCH_TUNER_DEPTH"; -// else if (current_state == PITCH_TUNER_RUN) -// string_state = "PITCH_TUNER_RUN"; + + string blank_space = ""; //to get consistent spacing in the file //record the string state, integer state, and then the data - fprintf(_fp, "%s,%d,",string_state.c_str(),current_state); - fprintf(_fp, "%d,",data_log_int); - fprintf(_fp, "%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",_data_log[1], - _data_log[2],_data_log[3],_data_log[4],_data_log[5],_data_log[6],_data_log[7],_data_log[8],_data_log[9],_data_log[10]); + fprintf(_fp, "%16s,%.2d,",string_state.c_str(),current_state); + fprintf(_fp, "%11d,",data_log_int); //length 10 + fprintf(_fp, "%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f%34s\n",_data_log[1], + _data_log[2],_data_log[3],_data_log[4],_data_log[5],_data_log[6],_data_log[7],_data_log[8],_data_log[9],_data_log[10],_data_log[11],_data_log[12],_data_log[13],blank_space.c_str()); + + //each line in the file is 160 characters long text-wise, check this with a file read } void MbedLogger::printMbedDirectory() { @@ -488,6 +499,7 @@ fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file } + //counts the number of newline characters to generate the packet size while (EOF != (ch=getc(_fp))) { if ('\n' == ch) _total_number_of_packets++; // records the number of new lines to determine how many packets to send @@ -741,154 +753,6 @@ } } -void MbedLogger::checkForPythonCommand() { - // STARTING STATE - static int process_state = HEADER_117; - - static string command_string = ""; - - int incoming_byte; - - if ( pc().readable() ) { - led1() = !led1(); - incoming_byte = pc().getc(); //getc returns an unsigned char cast to an int - - switch(process_state) { - case HEADER_117: - //continue processing - if (incoming_byte == 117){ - //had led two here - process_state = HEADER_101; - pc().printf("DEBUG: Case 117\n\r"); - } - //end transmission - else if (incoming_byte == 16) { - process_state = END_TRANSMISSION; - pc().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r"); - } - else { - process_state = HEADER_117; // ??? - //pc().printf("DEBUG: State Header 117\n\r"); - } - break; - - case HEADER_101: - if(incoming_byte == 101) { - led3() = !led3(); - process_state = COMMAND_ONE; - pc().printf("DEBUG: Case 101\n\r"); - } - break; - case COMMAND_ONE: - command_string += (char)incoming_byte; - process_state = COMMAND_TWO; - pc().printf("DEBUG: COMMAND: [%s]\n\r", command_string.c_str()); - break; - case COMMAND_TWO: - command_string += (char)incoming_byte; - process_state = HEADER_117; - pc().printf("DEBUG: COMMAND: [%s]\n\r", command_string.c_str()); - -// COMMAND -// if ( command_string.find("CT") == 0 ) { //find returns the position of the string -// stateMachine().setFSMCommand(CHECK_TUNING); -// } -// else if ( command_string.find("FN") == 0 ) { //find returns the position of the string -// stateMachine().setFSMCommand(FIND_NEUTRAL); -// } -// else if ( command_string.find("DV") == 0 ) { //find returns the position of the string -// stateMachine().setFSMCommand(DIVE); -// } - - command_string = ""; - -// COMMAND - - break; - } -// -// case PACKET_NUM: -// receive_packet_number = incoming_byte; -// process_state = TOTAL_NUM_PACKETS; -// //pc().printf("DEBUG: Case PACKET_NUM\n\r"); -// break; - } - //else { -// return false; -// } -// -// switch(process_state) { -// case HEADER_117: -// //continue processing -// if (incoming_byte == 101){ -// process_state = HEADER_101; -// pc().printf("DEBUG: Case 101\n\r"); -// } -// //end transmission -// else if (incoming_byte == 16) { -// process_state = END_TRANSMISSION; -// pc().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r"); -// } -// else { -// process_state = HEADER_117; // ??? -// //pc().printf("DEBUG: State Header 117\n\r"); -// } -// break; -// -// case HEADER_101: -// if(incoming_byte == 117) { -// process_state = PACKET_NUM; -// pc().printf("DEBUG: Case 117\n\r"); -// } -// break; -// -// case PACKET_NUM: -// receive_packet_number = incoming_byte; -// process_state = TOTAL_NUM_PACKETS; -// //pc().printf("DEBUG: Case PACKET_NUM\n\r"); -// break; -// -// case TOTAL_NUM_PACKETS: -// receive_total_number_packets = incoming_byte; -// process_state = PACKET_SIZE; -// //pc().printf("DEBUG: Case TOTAL_NUM_PACKETS\n\r"); -// break; -// -// case PACKET_SIZE: -// receive_packet_size = incoming_byte; -// //pc().printf("DEBUG: Case PACKET_SIZE\n\r"); -// -// //write the header stuff to it -// char_buffer[0] = 117; -// char_buffer[1] = 101; -// char_buffer[2] = receive_packet_number; -// char_buffer[3] = receive_total_number_packets; -// char_buffer[4] = receive_packet_size; -// -// break; -// -// case END_TRANSMISSION: -// if (pc().getc() == 16) { -// pc().printf("DEBUG: END_TRANSMISSION REACHED: 1. \n\r"); -// -// if (pc().getc() == 16) { -// pc().printf("DEBUG: END_TRANSMISSION REACHED: 2. \n\r"); -// -// endReceiveData(); -// } -// } -// -// pc().printf("DEBUG: END_TRANSMISSION REACHED: 5. \n\r"); -// -// //process_state = HEADER_117; //don't do this unless the check is wrong -// pc().printf("END_TRANSMISSION process_state is %d\n\r", process_state); //should be 5 (debug) 02/06/2018 -// data_transmission_complete = true; -// break; -// }//END OF SWITCH STATEMENT -// -// return true; -} - int MbedLogger::sendReply() { //being explicit in what's being transmitted @@ -1028,7 +892,8 @@ //if the file is empty, create this. if (!_fp) { _fp = fopen(file_name_string.c_str(), "w"); //write,print,close - fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth_ft,pitch_cmd,pitch_deg,bce_cmd,bce_mm,batt_cmd,batt_mm,pitchRate_degs,depthRate_fps\nempty log file!\n"); + //fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth_ft,pitch_cmd,pitch_deg,bce_cmd,bce_mm,batt_cmd,batt_mm,pitchRate_degs,depthRate_fps\nempty log file!\n"); + fprintf(_fp,_heading_string.c_str()); closeLogFile(); } else @@ -1071,33 +936,37 @@ return crc % 256; //last byte } -int MbedLogger::testGetFileSize() { - string file_name_string = _file_system_string + "LOG000.csv"; - - _fp = fopen(file_name_string.c_str(), "rb"); //open the file for reading as a binary file - - fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file) - unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file. - fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary - - closeLogFile(); //can probably just close the file pointer and not worry about position - - pc().printf("(%s) LOG000.csv file size is %d\n\r", _file_system_string.c_str(), file_size); - - if (file_size == 0) - createEmptyLog(); //this is not actually a completely empty log - - return file_size; - - // http://www.cplusplus.com/reference/cstdio/ftell/ - // https://os.mbed.com/questions/1260/How-to-read-the-LocalFileSystem-filesize/ -} +//delete +//int MbedLogger::testGetFileSize() { +// string file_name_string = _file_system_string + "LOG000.csv"; +// +// _fp = fopen(file_name_string.c_str(), "rb"); //open the file for reading as a binary file +// +// fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file) +// unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file. +// fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary +// +// closeLogFile(); //can probably just close the file pointer and not worry about position +// +// pc().printf("(%s) LOG000.csv file size is %d\n\r", _file_system_string.c_str(), file_size); +// +// if (file_size == 0) +// createEmptyLog(); //this is not actually a completely empty log +// +// return file_size; +// +// // http://www.cplusplus.com/reference/cstdio/ftell/ +// // https://os.mbed.com/questions/1260/How-to-read-the-LocalFileSystem-filesize/ +//} +//delete void MbedLogger::createEmptyLog() { string file_name_string = _file_system_string + "LOG000.csv"; + string empty_log = "EMPTY LOG"; _fp = fopen(file_name_string.c_str(), "w"); - fprintf(_fp, "EMPTY LOG\n"); //just write this string to the log (processing needs a file size that is not zero) + + fprintf(_fp, "%.25s\n",empty_log.c_str()); //just write this string to the log (processing needs a file size that is not zero) closeLogFile(); } @@ -1319,7 +1188,9 @@ void MbedLogger::eraseFile() { _fp = fopen(_full_file_path_string.c_str(), "w"); // LOG000.csv - fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth_ft,pitch_cmd,pitch_deg,bce_cmd,bce_mm,batt_cmd,batt_mm,pitchRate_degs,depthRate_fps\n(file erased)\n"); + fprintf(_fp,_heading_string.c_str()); + + // _heading_string = "state_string,state_ID,timer,depth_cmd,depth_ft,pitch_cmd,pitch_deg,bce_cmd,bce_mm,batt_cmd,batt_mm,pitchRate_degs,depthRate_fps,sys_amps,sys_volts\n(file erased)" closeLogFile(); } @@ -1501,5 +1372,5 @@ } int MbedLogger::getLogSize() { - return _log_file_line_counter; + return _log_file_line_counter; //number accurate after running getNumberOfPacketsInCurrentLog } \ No newline at end of file