most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown
Dependencies: mbed MODSERIAL FATFileSystem
Diff: MbedLogger/MbedLogger.cpp
- Revision:
- 74:d281aaef9766
- Parent:
- 73:f6f378311c8d
- Child:
- 76:c802e1da4179
--- a/MbedLogger/MbedLogger.cpp Mon Jul 30 16:48:48 2018 +0000 +++ b/MbedLogger/MbedLogger.cpp Tue Aug 14 21:06:48 2018 +0000 @@ -17,23 +17,20 @@ _log_file_line_counter = 0; //used to set timer in finite state machine based on size of log -//NEXT VERSION WILL ELIMINATE LENGTH REQUIREMENT - //_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"; - -//NEW ALTIMETER READINGS! - - _heading_string = "state_string,state_number,timer_sec,depth_cmd,depth_ft,pitch_cmd,pitch_deg,rudder_cmd_deg,heading_deg,bce_cmd,bce_mm,batt_cmd,batt_mm,pitch_rate_degs,depth_rate_fps,system_amps,system_volts,altimeter_or_intPSI,BCE_p,i,d,BATT_p,i,d,DEPTH_p,i,d,PITCH_p,i,d\n"; - + //heading string is 254 bytes long, FIXED LENGTH + _heading_string = "StateStr,St#,TimeSec,DepthCmd,DepthFt,PitchCmd,PitchDeg,RudderPWM,RudderCmdDeg,HeadDeg,bceCmd,bce_mm,battCmd,batt_mm,PitchRateDegSec,depth_rate_fps,SystemAmps,SystemVolts,AltChRd,Int_PSI,BCE_p,i,d,BATT_p,i,d,DEPTH_p,i,d,fq,db,PITCH_p,i,d,HEAD_p,i,d,fq,db\n"; _transmit_packet_num = 0; _fsm_transmit_complete = false; _end_transmit_packet = false; + + _end_sequence_transmission = false; } //this function has to be called for the time to function correctly void MbedLogger::setLogTime() { - pc().printf("\n%s log time set.\n\r", _file_system_string.c_str()); + xbee().printf("\n%s log time set.\n\r", _file_system_string.c_str()); set_time(1518467832); // Set RTC time to Mon, 12 FEB 2018 15:37 } @@ -45,13 +42,14 @@ } void MbedLogger::recordData(int current_state) { - int data_log_int = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp + int data_log_time = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp - _data_log[1] = depthLoop().getCommand(); //depth command - _data_log[2] = depthLoop().getPosition(); //depth reading (filtered depth) - _data_log[3] = pitchLoop().getCommand(); //pitch command - _data_log[4] = pitchLoop().getPosition(); //pitch reading (filtered pitch) - _data_log[5] = rudder().getPosition_deg(); //rudder command + _data_log[0] = depthLoop().getCommand(); //depth command + _data_log[1] = depthLoop().getPosition(); //depth reading (filtered depth) + _data_log[2] = pitchLoop().getCommand(); //pitch command + _data_log[3] = pitchLoop().getPosition(); //pitch reading (filtered pitch) + _data_log[4] = rudder().getSetPosition_pwm(); //rudder command PWM + _data_log[5] = rudder().getSetPosition_deg(); //rudder command DEG _data_log[6] = headingLoop().getPosition(); //heading reading (filtered heading) _data_log[7] = bce().getSetPosition_mm(); //BCE command @@ -63,26 +61,34 @@ _data_log[13] = sensors().getCurrentInput(); // i_in _data_log[14] = sensors().getVoltageInput(); // v_in - _data_log[15] = sensors().getInternalPressurePSI(); // int_press_PSI + _data_log[15] = sensors().getAltimeterChannelReadings(); // Altimeter Channel Readings + _data_log[16] = sensors().getInternalPressurePSI(); // int_press_PSI - //BCE_p,i,d - _data_log[16] = bce().getControllerP(); - _data_log[17] = bce().getControllerI(); - _data_log[18] = bce().getControllerD(); + //BCE_p,i,d,freq,deadband + _data_log[17] = bce().getControllerP(); + _data_log[18] = bce().getControllerI(); + _data_log[19] = bce().getControllerD(); + + _data_log[20] = batt().getControllerP(); + _data_log[21] = batt().getControllerI(); + _data_log[22] = batt().getControllerD(); - _data_log[19] = batt().getControllerP(); - _data_log[20] = batt().getControllerI(); - _data_log[21] = batt().getControllerD(); + _data_log[23] = depthLoop().getControllerP(); + _data_log[24] = depthLoop().getControllerI(); + _data_log[25] = depthLoop().getControllerD(); + _data_log[26] = depthLoop().getFilterFrequency(); + _data_log[27] = depthLoop().getDeadband(); - _data_log[22] = depthLoop().getControllerP(); - _data_log[23] = depthLoop().getControllerI(); - _data_log[24] = depthLoop().getControllerD(); + _data_log[28] = pitchLoop().getControllerP(); + _data_log[29] = pitchLoop().getControllerI(); + _data_log[30] = pitchLoop().getControllerD(); - _data_log[25] = pitchLoop().getControllerP(); - _data_log[26] = pitchLoop().getControllerI(); - _data_log[27] = pitchLoop().getControllerD(); - - //check what the current state is and create that string + _data_log[31] = headingLoop().getControllerP(); + _data_log[32] = headingLoop().getControllerI(); + _data_log[33] = headingLoop().getControllerD(); + _data_log[34] = headingLoop().getFilterFrequency(); + _data_log[35] = headingLoop().getDeadband(); + string string_state; if (current_state == SIT_IDLE) string_state = "SIT_IDLE"; @@ -112,26 +118,22 @@ string_state = "POSITION_RISE"; else if (current_state == TX_MBED_LOG) string_state = "TX_MBED_LOG"; - else if (current_state == RECEIVE_SEQUENCE) + else if (current_state == RX_SEQUENCE) string_state = "RECEIVE_SEQUENCE"; - - string blank_space = ""; //to get consistent spacing in the file + string blank_space = ""; //to get consistent spacing in the file (had a nonsense char w/o this) //record the string state, integer state, and then the data - - //_heading_string is 254 characters long - - //packet size is 254 characters long - - //make function that checks length of header versus string below, add padding + //_heading_string is 254 characters long, packet size is 254 characters long (matches _heading_string) - fprintf(_fp, "%16s,%.2d,",string_state.c_str(),current_state); - fprintf(_fp, "%11d,",data_log_int); //length 10 //right here - 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.3f,%6.2f,%6.1f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f%41s\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],_data_log[14],_data_log[15], - _data_log[16],_data_log[17],_data_log[18],_data_log[19],_data_log[20],_data_log[21],_data_log[22],_data_log[23],_data_log[24],_data_log[25],_data_log[26],_data_log[27],blank_space.c_str()); - + fprintf(_fp, "%16s,%.2d,%10d,",string_state.c_str(),current_state,data_log_time); + //%5.0f altimeter ch reading was for spacing (it's 0 to 4096) + //be careful with the spacing, the int psi was printing out too many decimal places (now it's %6.2f) + fprintf(_fp, "%5.1f,%5.1f,%6.1f,%6.1f,%4.0f,%4.0f,%6.1f,%5.1f,%5.1f,%5.1f,%5.1f,%6.1f,%6.1f,%6.3f,%6.2f,%5.0f,%6.2f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%4.1f,%4.1f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%4.1f,%4.1f\n", + _data_log[0],_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],_data_log[14],_data_log[15], + _data_log[16],_data_log[17],_data_log[18],_data_log[19],_data_log[20],_data_log[21],_data_log[22],_data_log[23],_data_log[24],_data_log[25],_data_log[26],_data_log[27],_data_log[28],_data_log[29],_data_log[30], + _data_log[31],_data_log[32],_data_log[33],_data_log[34],_data_log[35]); + //each line in the file is 160 characters long text-wise, check this with a file read } @@ -145,10 +147,10 @@ char * numstart; //char *numstop; - pc().printf("\n\rPrinting out the directory of device %s\n\r", _file_system_string.c_str()); + xbee().printf("\n\rPrinting out the directory of device %s\n\r", _file_system_string.c_str()); if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) { - pc().printf("MBED directory could not be opened\r\n"); + xbee().printf("MBED directory could not be opened\r\n"); } else { @@ -171,7 +173,7 @@ } else log_found=0; - pc().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp); + xbee().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp); loop++; } @@ -190,11 +192,11 @@ char buffer[500]; //read the file line-by-line and print that to the screen - pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\n\r",_file_number); + xbee().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\n\r",_file_number); while (!feof(_fp)) { // read in the line and make sure it was successful if (fgets(buffer,500,_fp) != NULL) { //stops at new line - pc().printf("%s\r",buffer); + xbee().printf("%s\r",buffer); _log_file_line_counter++; } } @@ -204,11 +206,11 @@ //close the file closeLogFile(); - pc().printf("\n\rLog file closed. Lines in log file: %d.\n\r", _log_file_line_counter); + xbee().printf("\n\rLog file closed. Lines in log file: %d.\n\r", _log_file_line_counter); } void MbedLogger::blastData() { - pc().printf("blastData FUNCTION\n\r"); + xbee().printf("blastData FUNCTION\n\r"); //OPEN FILE FOR READING @@ -222,7 +224,7 @@ if (!feof(_fp)) { //check for end of file //based on the internal packet number of the class //createDataPacket //transmit data packet transmitPacketNumber(_packet_number); - pc().printf("\r"); // for proper spacing + xbee().printf("\r"); // for proper spacing _packet_number++; } @@ -235,17 +237,7 @@ //CLOSE THE FILE closeLogFile(); - pc().printf("\n\rblastData: Log file closed. %d.\n\r"); -} - -void MbedLogger::openFileForTransmit() { - pc().printf("\n\ropenFileForTransmit\n\r"); - //open the current file to read the contents - string file_name_string = _file_system_string + "LOG000.csv"; - - _fp = fopen(file_name_string.c_str(), "r"); - - _file_transmission = true; + xbee().printf("\n\rblastData: Log file closed. %d.\n\r"); } void MbedLogger::createDataPacket() { @@ -266,7 +258,7 @@ _data_packet.push_back(_current_line_length); - //pc().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer); //debug + //xbee().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 < _current_line_length; i++) { @@ -303,7 +295,7 @@ _data_packet.push_back(line_length_sent); - //pc().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer); //debug + //xbee().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++) { @@ -320,7 +312,7 @@ _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"); + //xbee().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 @@ -328,7 +320,7 @@ void MbedLogger::transmitDataPacket() { //WRITE the data (in bytes) to the serial port for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) { - pc().putc(*_it); //send integers over serial port one byte at a time + xbee().putc(*_it); //send integers over serial port one byte at a time } } @@ -374,7 +366,7 @@ //based on the internal packet number of the class //createDataPacket //transmit data packet transmitPacketNumber(_packet_number); - pc().printf("\r"); // for proper spacing + xbee().printf("\r"); // for proper spacing _packet_number++; @@ -398,7 +390,7 @@ //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); + //xbee().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 @@ -415,7 +407,7 @@ //receive correct data packet from python, immediately send a transmit packet from the MBED void MbedLogger::transmitOnePacket() { - pc().printf("transmitOnePacket\n"); + xbee().printf("transmitOnePacket\n"); static int transmit_state = HEADER_117; //state in switch statement //int incoming_byte = -1; //reset each time a character is read @@ -431,8 +423,8 @@ int req_packet_number = -1; while (1) { - incoming_byte[bytes_received] = pc().getc(); - pc().printf("<%d> ", incoming_byte[bytes_received]); + incoming_byte[bytes_received] = xbee().getc(); + xbee().printf("<%d> ", incoming_byte[bytes_received]); bytes_received++; @@ -440,7 +432,7 @@ req_packet_number = incoming_byte[2] * 256 + incoming_byte[3]; - pc().printf("req_packet_number = %d\n\r", req_packet_number); + xbee().printf("req_packet_number = %d\n\r", req_packet_number); bytes_received = 0; break; @@ -456,82 +448,129 @@ //receive correct checksum, immediately send this packet transmitPacketNumber(req_packet_number); - fclose(_fp); + //fclose(_fp); + closeLogFile(); } void MbedLogger::transmitMultiplePackets() { - pc().printf("transmitMultiplePackets\n"); + xbee().printf("transmitMultiplePackets\n"); - static int transmit_state = HEADER_117; //state in switch statement + //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 + //int requested_packet_number = -1; //reset each time a character is read + static int input_packet; //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; - - int incoming_byte[6]; + + int current_byte = -1; static int bytes_received = 0; int req_packet_number = -1; - +//GET TOTAL NUMBER OF PACKETS! + getNumberOfPacketsInCurrentLog(); +//GET TOTAL NUMBER OF PACKETS! + //open the file string file_name_string = _file_system_string + "LOG000.csv"; _fp = fopen(file_name_string.c_str(), "r"); - - while (1) { + + //DEFAULT STATE + static int current_state = HEADER_117; + + bool active_loop = true; + + while (active_loop) { //INCOMING BYTE - incoming_byte[bytes_received] = pc().getc(); - pc().printf("<%d> ", incoming_byte[bytes_received]); + current_byte = xbee().getc(); + + //provide the next byte / state - //EXIT IF YOU RECEIVE... - if (incoming_byte[bytes_received] == 16) { - //quick and dirty - if (pc().getc() == 17) { - if (pc().getc() == 18) { - if (pc().getc() == 19) { - //exit if you get all of these packets hex 10 11 12 13, dec 16 17 18 19 - pc().printf("END TRANSMISSION REQUEST RECEIVED!\n\r"); - break; - } + switch (current_state) { + case HEADER_117: + //xbee().printf("HEADING 117\n\r"); + if (current_byte == 0x75) { + current_state = HEADER_101; + } + + + else if (current_byte == 0x10) { + current_state = END_TX_1; + } + break; + case HEADER_101: + //xbee().printf("HEADING 101\n\r"); + if (current_byte == 0x65) { + current_state = PACKET_NO_1; } - } - } - - bytes_received++; - - if (bytes_received > 5) { + break; + case PACKET_NO_1: + //xbee().printf("PACKET_NO_1\n\r"); + input_packet = current_byte * 256; + + current_state = PACKET_NO_2; + //xbee().printf("PACKET # 1 current byte %d\n\r", current_byte); + break; - req_packet_number = incoming_byte[2] * 256 + incoming_byte[3]; + case PACKET_NO_2: + //xbee().printf("PACKET_NO_2\n\r"); + input_packet = input_packet + current_byte; + + current_state = PACKET_CRC_ONE; + //xbee().printf("PACKET # 2 current byte %d (req packet num: %d)\n\r", current_byte, input_packet); + break; + + case PACKET_CRC_ONE: + //xbee().printf("PACKET_CRC_ONE\n\r"); + current_state = PACKET_CRC_TWO; + break; - pc().printf("req_packet_number = %d\n\r", req_packet_number); + case PACKET_CRC_TWO: + //xbee().printf("PACKET_CRC_TWO\n\r"); + current_state = HEADER_117; + transmitPacketNumber(input_packet); + break; + + case END_TX_1: + //xbee().printf("END_TX_1\n\r"); + current_state = END_TX_2; + break; + case END_TX_2: + //xbee().printf("END_TX_2\n\r"); + current_state = HEADER_117; + active_loop = false; + break; - //receive correct checksum, immediately send this packet - transmitPacketNumber(req_packet_number); + default: + //reset state + //xbee().printf("DEFAULT. HEADER_117\n\r"); + current_state = HEADER_117; //reset here + break; - - //reset bytes_received - bytes_received = 0; } } //CLOSE THE FILE - fclose(_fp); + closeLogFile(); + xbee().printf("08/05/2018 CLOSE THE LOG FILE\n\r"); + + //RESET THE STATE + //current_state = HEADER_117; } void MbedLogger::checkForPythonTransmitRequest() { - //pc().printf("checkForPythonTransmitRequest\n"); + //xbee().printf("checkForPythonTransmitRequest\n"); - if ( pc().readable() ) { + if ( xbee().readable() ) { //PC READABLE DOES NOT WORK HERE?! led2() = !led2(); led3() = !led3(); - pc().printf("########################\n"); - //pc().printf("%d", pc().getc()); + xbee().printf("########################\n"); + //xbee().printf("%d", xbee().getc()); } // static int transmit_state = HEADER_117; //state in switch statement @@ -547,9 +586,9 @@ // // int req_packet_number = -1; // -// if (pc().readable()) { -// incoming_byte[bytes_received] = pc().getc(); -// pc().printf("<%d> ", incoming_byte[bytes_received]); +// if (xbee().readable()) { +// incoming_byte[bytes_received] = xbee().getc(); +// xbee().printf("<%d> ", incoming_byte[bytes_received]); // // bytes_received++; // @@ -557,7 +596,7 @@ // // req_packet_number = incoming_byte[2] * 256 + incoming_byte[3]; // -// pc().printf("req_packet_number = %d\n\r", req_packet_number); +// xbee().printf("req_packet_number = %d\n\r", req_packet_number); // // //reset // bytes_received = 0; @@ -592,7 +631,7 @@ // case HEADER_101: // if(incoming_byte == 101) { //"e" // transmit_state = TRANSMIT_PACKET_1; -// //pc().printf(" U E \n\r"); +// //xbee().printf(" U E \n\r"); // } // else { // transmit_state = HEADER_117; @@ -609,8 +648,8 @@ // //_reply_byte3 = incoming_byte; // // led1() = !led1(); -// //pc().printf(" T P \n\r"); -// //pc().printf("DEBUG: Transmit Packet %d\n\r", incoming_byte); +// //xbee().printf(" T P \n\r"); +// //xbee().printf("DEBUG: Transmit Packet %d\n\r", incoming_byte); // } // break; // @@ -673,9 +712,9 @@ static int inside_while_loop = 1; while (inside_while_loop) { - if (pc().readable()) { //don't rely on pc readable being open all the time + if (xbee().readable()) { //don't rely on pc readable being open all the time - incoming_byte = pc().getc(); + incoming_byte = xbee().getc(); switch(transmit_state) { @@ -727,13 +766,13 @@ //TRANSMIT_PACKET_2 inside_while_loop = 0; //exit the while loop with this - pc().printf("(TRANSMIT_PACKET_2)reached inside_while_loop = 0\n\r"); //DEBUG + xbee().printf("(TRANSMIT_PACKET_2)reached inside_while_loop = 0\n\r"); //DEBUG break; } //end of switch statement } //end of while loop // else { -// //pc().printf("pc not readable \n\r"); +// //xbee().printf("pc not readable \n\r"); // } // } @@ -741,7 +780,7 @@ //once you're outside of the while loop inside_while_loop = true; //for next iteration - pc().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r"); + xbee().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r"); return false; } @@ -749,11 +788,11 @@ int incoming_byte; - while (pc().readable()) { - incoming_byte = pc().getc(); //get first byte + while (xbee().readable()) { + incoming_byte = xbee().getc(); //get first byte if (incoming_byte == 16) { - incoming_byte = pc().getc(); //get second byte + incoming_byte = xbee().getc(); //get second byte return true; } //quick and dirty @@ -771,10 +810,10 @@ //check if this actually worked... if (!_fp) { - pc().printf("ERROR: Log file could not be opened\n\r"); + xbee().printf("ERROR: Log file could not be opened\n\r"); } else { - pc().printf("Current Log file (LOG000.csv) was opened.\n\r"); + xbee().printf("Current Log file (LOG000.csv) was opened.\n\r"); } } @@ -785,11 +824,11 @@ //check if this actually worked... if (!_fp) { - //pc().printf("ERROR: Log file could not be opened\n\r"); + //xbee().printf("ERROR: Log file could not be opened\n\r"); return false; } else { - //pc().printf("Current Log file (LOG000.csv) was opened.\n\r"); + //xbee().printf("Current Log file (LOG000.csv) was opened.\n\r"); return true; } } @@ -821,16 +860,19 @@ _total_number_of_packets = size/254; + //CLOSE THE FILE + closeLogFile(); + return _total_number_of_packets; } void MbedLogger::endTransmissionCloseFile() { // if the file pointer is null, the file was not opened in the first place if (!_fp) { - pc().printf("\n endTransmissionCloseFile: FILE WAS NOT OPENED!\n\r"); + xbee().printf("\n endTransmissionCloseFile: FILE WAS NOT OPENED!\n\r"); } else { - pc().printf("\n endTransmissionCloseFile: FILE FOUND AND CLOSED!\n\r"); + xbee().printf("\n endTransmissionCloseFile: FILE FOUND AND CLOSED!\n\r"); closeLogFile(); } @@ -838,7 +880,7 @@ } void MbedLogger::openWriteFile() { - pc().printf("Opening file for reception.\n\r"); + xbee().printf("Opening file for reception.\n\r"); string file_name_string = _file_system_string + "LOG000.csv"; @@ -874,50 +916,50 @@ int i = 5; int serial_timeout = 0; - while (pc().readable() && !data_transmission_complete) { - incoming_byte = pc().getc(); //getc returns an unsigned char cast to an int - //pc().printf("DEBUG: State 0\n\r"); + while (xbee().readable() && !data_transmission_complete) { + incoming_byte = xbee().getc(); //getc returns an unsigned char cast to an int + //xbee().printf("DEBUG: State 0\n\r"); switch(process_state) { case HEADER_117: //continue processing if (incoming_byte == 117){ process_state = HEADER_101; - //pc().printf("DEBUG: Case 117\n\r"); + //xbee().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"); + //xbee().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r"); } else { process_state = HEADER_117; // ??? - //pc().printf("DEBUG: State Header 117\n\r"); + //xbee().printf("DEBUG: State Header 117\n\r"); } break; case HEADER_101: if(incoming_byte == 101) { process_state = PACKET_NUM; - //pc().printf("DEBUG: Case 101\n\r"); + //xbee().printf("DEBUG: Case 101\n\r"); } break; case PACKET_NUM: receive_packet_number = incoming_byte; process_state = TOTAL_NUM_PACKETS; - //pc().printf("DEBUG: Case PACKET_NUM\n\r"); + //xbee().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"); + //xbee().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"); + //xbee().printf("DEBUG: Case PACKET_SIZE\n\r"); //write the header stuff to it char_buffer[0] = 117; @@ -927,7 +969,7 @@ char_buffer[4] = receive_packet_size; // tests confirmed that packet number is zero, number of packets is 12, packet size is 12 - //pc().printf("char_buffer 2/3/4: %d %d %d\n\r", receive_packet_number,receive_total_number_packets,receive_packet_size); + //xbee().printf("char_buffer 2/3/4: %d %d %d\n\r", receive_packet_number,receive_total_number_packets,receive_packet_size); //process packet data, future version will append for larger data sizes, 0xFFFF @@ -936,8 +978,8 @@ serial_timeout = 0; while (true) { - if (pc().readable()) { - char_buffer[i] = pc().getc(); //read all of the data packets + if (xbee().readable()) { + char_buffer[i] = xbee().getc(); //read all of the data packets i++; serial_timeout = 0; //reset the timeout @@ -949,8 +991,8 @@ // When full data packet is received... if (i >= receive_packet_size+5) { //cannot do this properly with a for loop //get checksum bytes - checksum_one = pc().getc(); - checksum_two = pc().getc(); + checksum_one = xbee().getc(); + checksum_two = xbee().getc(); //calculate the CRC from the header and data bytes using _data_packet (vector) //found out calculating crc with string was dropping empty or null spaces @@ -964,44 +1006,51 @@ int calc_crc_one = calcCrcOne(); int calc_crc_two = calcCrcTwo(); - //pc().printf("DEBUG: calc crc 1: %d, crc 2: %d\n\r", calc_crc_one, calc_crc_two); + //xbee().printf("DEBUG: calc crc 1: %d, crc 2: %d\n\r", calc_crc_one, calc_crc_two); // first confirm that the checksum is correct - if ((calc_crc_one == checksum_one) and (calc_crc_two == checksum_two)) { - // CHECKSUM CORRECT & get the filename from the first packet (check that you receive first packet) - if (receive_packet_number == 0) { - char temp_char[receive_packet_size+1]; //temp array for memcpy - memset(&temp_char[0], 0, sizeof(temp_char)); //clear full array (or get random characters) - strncpy(temp_char, char_buffer + 5 /* Offset */, receive_packet_size*sizeof(char) /* Length */); //memcpy introduced random characters - - //have to terminate the string with '\0' - temp_char[receive_packet_size] = '\0'; - - //pc().printf("\n\rDEBUG: ------ Filename? <<%s>>\n\r", temp_char); - _received_filename = temp_char; - - //pc().printf("\n\rDEBUG: _received_filename <<%s>>\n\r", _received_filename); - - //open a file for writing - openReceiveFile(_received_filename); - - //send a reply to Python transmit program - sendReply(); - - led3() = 1; - - // even if correct CRC, counter prevents the program from writing the same packet twice - _confirmed_packet_number++; - } + if ((calc_crc_one == checksum_one) and (calc_crc_two == checksum_two)) { + + //xbee().printf("DEBUG: checksums are good!\n\r"); + + //xbee().printf("receive_packet_number %d and _confirmed_packet_number %d\n\r", receive_packet_number, _confirmed_packet_number); //debug + +// // CHECKSUM CORRECT & get the filename from the first packet (check that you receive first packet) +// if (receive_packet_number == 0) { +// char temp_char[receive_packet_size+1]; //temp array for memcpy +// memset(&temp_char[0], 0, sizeof(temp_char)); //clear full array (or get random characters) +// strncpy(temp_char, char_buffer + 5 /* Offset */, receive_packet_size*sizeof(char) /* Length */); //memcpy introduced random characters +// +// //xbee().printf("SEQUENCE: <<%s>>\n\r", temp_char]) // ERASE +// +// //have to terminate the string with '\0' +// temp_char[receive_packet_size] = '\0'; +// +// //xbee().printf("\n\rDEBUG: ------ Filename? <<%s>>\n\r", temp_char); +// //_received_filename = temp_char; +// +// //xbee().printf("\n\rDEBUG: _received_filename <<%s>>\n\r", _received_filename); +// +// //open a file for writing +// //openReceiveFile(_received_filename); +// +// //send a reply to Python transmit program +// sendReply(); +// +// led3() = 1; +// +// // even if correct CRC, counter prevents the program from writing the same packet twice +// _confirmed_packet_number++; +// } // check if the packet that you're receiving (receive_packet_number) has been received already... //CHECKSUM CORRECT & packet numbers that are 1 through N packets - else if (receive_packet_number == _confirmed_packet_number){ + if (receive_packet_number == _confirmed_packet_number){ //save the data (char buffer) to the file if both checksums work... // when a packet is received (successfully) send a reply - sendReply(); + //sendReply(); // write correct data to file fprintf(_fp, "%s", char_buffer+5); @@ -1020,35 +1069,36 @@ break; } - //counter breaks out of the loop if no data received - if (serial_timeout >= 10000) { - //pc().printf("break serial_timeout %d\n\r", serial_timeout); - break; - } +// //counter breaks out of the loop if no data received +// if (serial_timeout >= 10000) { +// //xbee().printf("break serial_timeout %d\n\r", serial_timeout); +// break; +// } } break; case END_TRANSMISSION: - if (pc().getc() == 16) { - pc().printf("DEBUG: END_TRANSMISSION REACHED: 1. \n\r"); + if (xbee().getc() == 16) { + //xbee().printf("DEBUG: END_TRANSMISSION REACHED: 1. \n\r"); - if (pc().getc() == 16) { - pc().printf("DEBUG: END_TRANSMISSION REACHED: 2. \n\r"); + if (xbee().getc() == 16) { + //xbee().printf("DEBUG: END_TRANSMISSION REACHED: 2. \n\r"); - endReceiveData(); + _end_sequence_transmission = true; + //endReceiveData(); } } - pc().printf("DEBUG: END_TRANSMISSION REACHED: 5. \n\r"); + //xbee().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 + //xbee().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 if (data_transmission_complete) { - pc().printf("DEBUG: checkForIncomingData data_transmission_complete \n\r"); + //xbee().printf("DEBUG: checkForIncomingData data_transmission_complete \n\r"); break; //out of while loop } } // while loop @@ -1087,7 +1137,7 @@ //transmit this packet for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) { - pc().putc(*_it); //send integers over serial port one byte at a time + xbee().putc(*_it); //send integers over serial port one byte at a time } //change process methodology later... @@ -1097,7 +1147,7 @@ void MbedLogger::endReceiveData() { //DLE character * 4 ==> 10 10 10 10 closeLogFile(); //close the file here - pc().printf("endReceiveData closed the file and ended transmission\n\r"); + xbee().printf("endReceiveData closed the file and ended transmission\n\r"); } //calculate the crc with an integer array @@ -1139,7 +1189,7 @@ for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; - //pc().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_array.length(), crc % 256); + //xbee().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_array.length(), crc % 256); return crc % 256; //last byte } @@ -1149,10 +1199,10 @@ } void MbedLogger::openNewMissionFile() { - pc().printf("Opening Mission file (sequence.txt) for reception.\n\r"); + xbee().printf("Opening Mission file (sequence.txt) for reception.\n\r"); string filename_string = _file_system_string + "sequence.txt"; - pc().printf("openNewMissionFile: %s\n\r", filename_string.c_str()); + xbee().printf("openNewMissionFile: %s\n\r", filename_string.c_str()); _fp = fopen(filename_string.c_str(), "w"); } @@ -1193,7 +1243,7 @@ // log file freezes at 0x0000006c void MbedLogger::initializeLogFile() { string file_name_string = _file_system_string + "LOG000.csv"; - pc().printf("%s file system init\n\r", _file_system_string.c_str()); + xbee().printf("%s file system init\n\r", _file_system_string.c_str()); //try to open this file... _fp = fopen(file_name_string.c_str(), "r"); @@ -1224,7 +1274,7 @@ crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; } - //pc().printf("DEBUG: calcCrcOne string length: %d crc: %d\n\r", input_string.length(), crc/256); + //xbee().printf("DEBUG: calcCrcOne string length: %d crc: %d\n\r", input_string.length(), crc/256); return crc / 256; //second-to-last byte } @@ -1240,7 +1290,7 @@ crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; } - //pc().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_string.length(), crc % 256); + //xbee().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_string.length(), crc % 256); return crc % 256; //last byte } @@ -1269,13 +1319,13 @@ //closeLogFile(); //can probably just close the file pointer and not worry about position - pc().printf("%s file size is %d\n\r", filename.c_str(), file_size); + xbee().printf("%s file size is %d\n\r", filename.c_str(), file_size); return file_size; } int MbedLogger::debugFileState() { - pc().printf("What is _fp right now? %p\n\r", _fp); //pointer notation + xbee().printf("What is _fp right now? %p\n\r", _fp); //pointer notation if (_fp) return 1; //file pointer does exist @@ -1284,11 +1334,11 @@ } void MbedLogger::specifyFileForTransmit(string input_string) { - pc().printf("specifyFileForTransmit\n\r"); + xbee().printf("specifyFileForTransmit\n\r"); string file_string = _file_system_string + input_string; - pc().printf("file_string is <%s>\n\r", file_string.c_str()); + xbee().printf("file_string is <%s>\n\r", file_string.c_str()); //open this file to read _fp = fopen(file_string.c_str(), "r"); @@ -1311,13 +1361,13 @@ // char * numstart, *numstop; if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) { - pc().printf("MBED directory could not be opened\r\n"); + xbee().printf("MBED directory could not be opened\r\n"); } else { while ( NULL != (dp = readdir( dir )) ) { - pc().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp); + xbee().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp); //process current file if it matches the file number if (file_number == loop) { @@ -1336,13 +1386,13 @@ void MbedLogger::accessMbedDirectory() { printMbedDirectory(); - pc().printf("Type in the number of the file you want to transmit.\n\r"); + xbee().printf("Type in the number of the file you want to transmit.\n\r"); char message[42]; - pc().scanf("%41s", message); + xbee().scanf("%41s", message); - pc().printf("Input received!\n\r"); + xbee().printf("Input received!\n\r"); //check if char array is an integer char* conversion_pointer; @@ -1350,11 +1400,11 @@ if (*conversion_pointer) { //conversion failed because the input was not a number - pc().printf("NOT A VALID FILE NUMBER!\n\r"); + xbee().printf("NOT A VALID FILE NUMBER!\n\r"); } else { //conversion worked! - pc().printf("You chose file number: %d\n\r", converted); + xbee().printf("You chose file number: %d\n\r", converted); // transmit the file transmitFileFromDirectory(converted); @@ -1365,11 +1415,11 @@ led4() = 1; if (_fp == NULL){ - pc().printf("MbedLogger: (%s) LOG FILE WAS ALREADY CLOSED!\n\r", _file_system_string.c_str()); + xbee().printf("MbedLogger: (%s) LOG FILE WAS ALREADY CLOSED!\n\r", _file_system_string.c_str()); } else { - pc().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _file_system_string.c_str()); + xbee().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _file_system_string.c_str()); //close file fclose(_fp); @@ -1382,6 +1432,39 @@ _mbed_receive_loop = true; } +void MbedLogger::receiveSequenceFile() { + //restart each time + _end_sequence_transmission = false; + + openNewMissionFile(); + + //zero will be reserved for the file name, future + _confirmed_packet_number = 1; //in sendReply() function that transmits a reply for incoming data + +// int current_packet_number = 1; +// int last_packet_number = -1; +// int break_transmission = 0; + + int counter = 0; + + while(1) { + wait(0.25); //runs at 4 Hz + + checkForIncomingData(); + + //xbee().printf("\n\rDEBUG: _confirmed_packet_number%d\n\r", _confirmed_packet_number); + + counter++; + + sendReply(); //bad name, should call it send request or something + + if (_end_sequence_transmission) + break; + } + + closeLogFile(); +} + void MbedLogger::receiveMissionDataWithFSM() { checkForIncomingData(); @@ -1394,7 +1477,7 @@ _mbed_receive_ticker.attach(callback(this, &MbedLogger::activateReceivePacket), 0.5); - pc().printf("\n\r02/09/2018 MbedLogger receiveMissionData Beginning to receive sequence data...\n\r"); + xbee().printf("\n\r02/09/2018 MbedLogger receiveMissionData Beginning to receive sequence data...\n\r"); resetReplyPacket(); //reset the reply packet @@ -1408,7 +1491,7 @@ if (_mbed_receive_loop) { if (!checkForIncomingData()) { // run this until it finishes //when you complete data reception, this will become false - pc().printf("\n\rMbedLogger: Data RECEPTION complete.\n\r"); + xbee().printf("\n\rMbedLogger: Data RECEPTION complete.\n\r"); _mbed_receive_ticker.detach(); break; } @@ -1420,7 +1503,7 @@ //check if you keep getting the same thing current_packet_number = sendReply(); - //pc().printf("DEBUG: current packet number %d (last packet number %d) \n\r", current_packet_number, last_packet_number); //debug + //xbee().printf("DEBUG: current packet number %d (last packet number %d) \n\r", current_packet_number, last_packet_number); //debug //let this count up a few times before it exits if (current_packet_number == last_packet_number) { @@ -1430,7 +1513,7 @@ if (break_transmission >= 50) { closeIncompleteFile(); //close the file _mbed_receive_ticker.detach(); - pc().printf("MbedLogger: TRANSMISSION INTERRUPTED!\n\r"); + xbee().printf("MbedLogger: TRANSMISSION INTERRUPTED!\n\r"); break; } } @@ -1448,9 +1531,7 @@ _fp = fopen(_full_file_path_string.c_str(), "w"); // LOG000.csv 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(); } @@ -1489,147 +1570,6 @@ _data_packet.push_back(crc_two); } -void MbedLogger::sendStatus() { - led3() = !led3(); - //readPacketInSeries() //uses this functionality - - //get data depth, pitch, heading, timer - - //store the data as a hex char array - char hex_char_depth[256]; - char hex_char_pitch[256]; - char hex_char_heading[256]; - char hex_char_timer[256]; - - string string_hex_buffer = ""; //buffer to store the hex data as a string - string string_temp; // ELIMINATE THIS ASAP - - float depth_value = depthLoop().getPosition(); - float pitch_value = imu().getPitch(); - float heading_value = imu().getHeading(); - //float timer_value = stateMachine().getTimer(); - float timer_value = 123.123; - - sprintf(hex_char_depth, "%08X" , *(unsigned int*)&depth_value); - sprintf(hex_char_pitch, "%08X" , *(unsigned int*)&pitch_value); - sprintf(hex_char_heading, "%08X" , *(unsigned int*)&heading_value); - sprintf(hex_char_timer, "%08X" , *(unsigned int*)&timer_value); - - //store the integer values - int hex_to_int[128]; - - //read through hex string and break apart into chunks and save the decimal values - int hex_counter = 0; - -////////////////////////// - string_hex_buffer = hex_char_depth; - - for (int i = 0; i < 8; i = i+2) { - //substring starts at x substr(x, length of substring) - string_temp = string_hex_buffer.substr(i,2); // get substring from string (converted from char array) - - //convoluted, fix this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - // copying the contents of the string to char array - char temp_char_array[256]; - strcpy(temp_char_array, string_temp.c_str()); - - //store char array hex value as decimal value - int decimal_value; - sscanf(temp_char_array,"%x",&decimal_value); - - hex_to_int[hex_counter] = decimal_value; - hex_counter++; //iterate to keep up with for loop - - //clear the string each time - //string_temp = ""; - //cout << "string_temp CLEARED? is " << string_temp << endl; - //cout << "string_temp is " << string_temp << " and the value is " << decimal_value << endl; - } - - string_hex_buffer = hex_char_pitch; - - for (int i = 0; i < 8; i = i+2) { - //substring starts at x substr(x, length of substring) - string_temp = string_hex_buffer.substr(i,2); - - //convoluted, fix this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - // copying the contents of the string to char array - char temp_char_array[256]; - strcpy(temp_char_array, string_temp.c_str()); - - //store char array hex value as decimal value - int decimal_value; - sscanf(temp_char_array,"%x",&decimal_value); - - hex_to_int[hex_counter] = decimal_value; - hex_counter++; //iterate to keep up with for loop - - //clear the string each time - //string_temp = ""; - //cout << "string_temp CLEARED? is " << string_temp << endl; - //cout << "string_temp is " << string_temp << " and the value is " << decimal_value << endl; - } - - string_hex_buffer = hex_char_heading; - - for (int i = 0; i < 8; i = i+2) { - //substring starts at x substr(x, length of substring) - string_temp = string_hex_buffer.substr(i,2); - - //convoluted, fix this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - // copying the contents of the string to char array - char temp_char_array[256]; - strcpy(temp_char_array, string_temp.c_str()); - - //store char array hex value as decimal value - int decimal_value; - sscanf(temp_char_array,"%x",&decimal_value); - - hex_to_int[hex_counter] = decimal_value; - hex_counter++; //iterate to keep up with for loop - - //clear the string each time - //string_temp = ""; - //cout << "string_temp CLEARED? is " << string_temp << endl; - //cout << "string_temp is " << string_temp << " and the value is " << decimal_value << endl; - } - - string_hex_buffer = hex_char_timer; - - for (int i = 0; i < 8; i = i+2) { - //substring starts at x substr(x, length of substring) - string_temp = string_hex_buffer.substr(i,2); - - //convoluted, fix this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - // copying the contents of the string to char array - char temp_char_array[256]; - strcpy(temp_char_array, string_temp.c_str()); - - //store char array hex value as decimal value - int decimal_value; - sscanf(temp_char_array,"%x",&decimal_value); - - hex_to_int[hex_counter] = decimal_value; - hex_counter++; //iterate to keep up with for loop - - //clear the string each time - //string_temp = ""; - //cout << "string_temp CLEARED? is " << string_temp << endl; - //cout << "string_temp is " << string_temp << " and the value is " << decimal_value << endl; - } -////////////////////////// - - //create new packet based on int array - _packet_number = 1; // TEST, FIX THIS - _total_number_of_packets = 1; - _current_line_length = 16; - - intCreateDataPacket(hex_to_int,16); - - - transmitDataPacket(); -} - void MbedLogger::setTransmitPacketNumber(int packet_number) { _transmit_packet_num = packet_number; @@ -1641,4 +1581,4 @@ int MbedLogger::currentPacketNumber() { return _packet_number; -} \ No newline at end of file +} \ No newline at end of file