Dependencies: mbed MODSERIAL FATFileSystem
Diff: MbedLogger/MbedLogger.cpp
- Revision:
- 69:919ac8d7e023
- Parent:
- 68:8f549749b8ce
- Child:
- 70:0e5180befedd
--- a/MbedLogger/MbedLogger.cpp Wed Jun 27 23:01:53 2018 +0000 +++ b/MbedLogger/MbedLogger.cpp Fri Jun 29 14:21:22 2018 +0000 @@ -20,6 +20,8 @@ _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_PSI\n"; _transmit_packet_num = 0; + + _fsm_transmit_complete = false; } //this function has to be called for the time to function correctly @@ -88,8 +90,13 @@ else if (current_state == POSITION_DIVE) string_state = "POSITION_DIVE"; else if (current_state == POSITION_RISE) - string_state = "POSITION_RISE"; - + string_state = "POSITION_RISE"; + else if (current_state == TRANSMIT_MBED_LOG) + string_state = "TRANSMIT_MBED_LOG"; + else if (current_state == RECEIVE_SEQUENCE) + string_state = "RECEIVE_SEQUENCE"; + + string blank_space = ""; //to get consistent spacing in the file //record the string state, integer state, and then the data @@ -187,28 +194,6 @@ _file_transmission = true; } -void MbedLogger::transmitCurrentLogFileLine(bool next_line) { - vector <int> current_packet; - - pc().printf("\n\rtransmitCurrentLogFile\n\r"); - - //while not end of file, read through line by line??? - - //transmit the file line by line (reads one line) - //if you get the command to send a new line, go to the next line - if (next_line) { - if (fgets(_line_buffer,256,_fp) != NULL) { - pc().printf("%s\r",_line_buffer); - } - } - - // GET BUFFER LENGTH - _current_line_length = strlen(_line_buffer); - - //DEBUG PRINT TO SCREEN - pc().printf("line_buffer size: %d %<%s>\r",_current_line_length,_line_buffer); -} - void MbedLogger::createDataPacket() { // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2 @@ -245,6 +230,45 @@ _data_packet.push_back(crc_two); } +//new 6/27/2018 +void MbedLogger::createDataPacket(char line_buffer_sent[], int line_length_sent) { + // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2 + + //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0. + _data_packet.clear(); + + //DATA PACKET HEADER + _data_packet.push_back(117); //0x75 + _data_packet.push_back(101); //0x65 + + _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form + _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form + + _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form + _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form + + _data_packet.push_back(line_length_sent); + + //pc().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer); //debug + + //DATA FROM LINE READ (read string character by chracter) + for (int i = 0; i < line_length_sent; i++) { + _data_packet.push_back(line_buffer_sent[i]); + } + + //CRC CALCULATIONS BELOW + //character version of calculation screws up on null character 0x00, scrapped and using vector of integers + + int crc_one = calcCrcOne(); + int crc_two = calcCrcTwo(); + + //place the crc bytes into the data packet that is transmitted + _data_packet.push_back(crc_one); + _data_packet.push_back(crc_two); + + //pc().printf("debug createDataPacket(char line_buffer_sent[], int line_length_sent)\n\r"); +} + //should i save it as a string and transmit the string? next iteration maybe void MbedLogger::transmitDataPacket() { @@ -276,7 +300,8 @@ // // createDataPacket requires _packet_number, _total_number_of_packets, _current_line_length (data packet size) // // uses char _line_buffer[256] variable to hold characters read from the file - // // packs this into a vector for transmission + // // packs this into a vector for transmission + createDataPacket(); transmitDataPacket(); @@ -293,26 +318,36 @@ } bool MbedLogger::fsmTransmitData() { //using the finite state machine - pc().printf("debug fsmTransmitData (%d) (tpn: %d)\n\r",_fp,_transmit_packet_num); + //pc().printf("debug fsmTransmitData (%d) (tpn: %d)\n\r",_fp,_transmit_packet_num); - if (!feof(_fp)) { //check for end of file + if (!feof(_fp) and (!_fsm_transmit_complete)) { //check for end of file wait(0.1); - //based on the internal packet number of the class - transmitPacketNumber(_transmit_packet_num); + //based on the internal packet number of the class + //createDataPacket + //transmit data packet + + pc().printf("DEBUG:"); - //check for incoming transmission - pythonTransmitRequest(); + transmitPacketNumber(_packet_number); + + pc().printf("\r"); // for proper spacing - _transmit_packet_num++; - - led3() = !led3(); + _packet_number++; + led2() = !led2(); return true; } - else + else { + //is there a better way to do this? + //this flag was required because the additional requests kept generating two packets through the FSM + // by calling mbedLogger().fsmTransmitData(); + //and mbedLogger().checkForPythonTransmitRequest(); + _fsm_transmit_complete = true; + return false; + } } void MbedLogger::continuouslyTransmitData() { @@ -326,7 +361,7 @@ transmitPacketNumber(packet_num); //check for incoming transmission - pythonTransmitRequest(); + //python? packet_num++; @@ -336,36 +371,39 @@ void MbedLogger::transmitPacketNumber(int line_number) { int line_size = 160; //length of lines in the log file, EVERY LINE MUST BE THE SAME LENGTH - char line_buffer[500] = {}; - + fseek(_fp,(line_size+1)*line_number,SEEK_SET); //fseek must use the +1 to get the newline character - //start from the beginning and go to this position - fread(line_buffer, 1, line_size, _fp); //read the line that is exactly 160 characters long + + //write over the internal _line_buffer //start from the beginning and go to this position + fread(_line_buffer, 1, line_size, _fp); //read the line that is exactly 160 characters long - pc().printf("Debug (transmitPacketNumber): line_buffer <<%s>> (line size: %d)\n\r", line_buffer,line_size); + //pc().printf("Debug (transmitPacketNumber): line_buffer <<%s>> (line size: %d)\n\r", line_buffer,line_size); // createDataPacket requires _packet_number, _total_number_of_packets, _current_line_length (data packet size) // uses char _line_buffer[256] variable to hold characters read from the file // packs this into a vector for transmission - createDataPacket(); //create the data packet from the _line_buffer (char array) + createDataPacket(_line_buffer, line_size); //create the data packet from the _line_buffer (char array) + transmitDataPacket(); //transmit the assembled packet } -void MbedLogger::pythonTransmitRequest() { + +//sample packet +//1E 1F 00 01 2E F6 //packet 1 +//1E 1F 00 02 2F B6 //packet 2 +//receive correct data packet from python, immediately send a transmit packet from the MBED +void MbedLogger::checkForPythonTransmitRequest() { static int transmit_state = HEADER_117; //state in switch statement int incoming_byte = -1; //reset each time a character is read int requested_packet_number = -1; //reset each time a character is read static int input_packet[4]; //changed from char in previous iteration 03/28/2018 static int transmit_crc_one = 0; //hold crc values until they're reset with calculations static int transmit_crc_two = 0; - - + while (pc().readable()) { incoming_byte = pc().getc(); - led3() = !led3(); - switch(transmit_state) { case HEADER_117: //continue processing @@ -425,30 +463,18 @@ //check if CRC TWO is correct (then send full packet) if (incoming_byte == transmit_crc_two) { requested_packet_number = input_packet[2] * 256 + input_packet[3]; //compute the numbers 0 through 65535 with the two bytes - - if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE! - //MUST SET THE PACKET NUMBER - _packet_number = requested_packet_number; - // DO NOT READ IF THE CURRENT REQUEST IS PAST THE TOTAL # OF PACKETS (lines of the file) - if (_packet_number <= _total_number_of_packets) { - readPacketInSeries(); - createDataPacket(); - } + //receive correct checksum, immediately send this packet + transmitPacketNumber(requested_packet_number); - _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created - - transmitDataPacket(); //continuously transmit current packet until it tells you to do the next packet (response from Python program) - } - - else - transmitDataPacket(); //if you get the same packet request, send the same packet + //fseek(_fp, 0, SEEK_END); //reset _fp (this was causing errors with the other function) + led3() = !led3(); } //end of checksum (incoming_byte) if statement break; } //switch statement complete - } //while statement complete + } //while statement complete } void MbedLogger::readTransmitPacketOneChar() { @@ -679,7 +705,7 @@ _current_line_length = strlen(_line_buffer); } -void MbedLogger::getNumberOfPacketsInCurrentLog() { +int MbedLogger::getNumberOfPacketsInCurrentLog() { _packet_number = 0; int ch; @@ -708,7 +734,9 @@ _file_transmission = true; //preparing to transmit files from MBED to Python - pc().printf("debug: _total_number_of_packets: %d\n\r",_total_number_of_packets); + //pc().printf("debug: _total_number_of_packets: %d\n\r",_total_number_of_packets); + + return _total_number_of_packets; } void MbedLogger::endTransmissionCloseFile() { @@ -1132,30 +1160,6 @@ return crc % 256; //last byte } -//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"; @@ -1545,10 +1549,9 @@ transmitDataPacket(); } -int MbedLogger::getLogSize() { - return _log_file_line_counter; //number accurate after running getNumberOfPacketsInCurrentLog -} - void MbedLogger::setTransmitPacketNumber(int packet_number) { _transmit_packet_num = packet_number; + + //also needed to reset a boolean flag on the transmit + _fsm_transmit_complete = false; } \ No newline at end of file