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

Revision:
63:6cb0405fc6e6
Parent:
62:d502889e74f1
Child:
67:c86a4b464682
--- a/MbedLogger/MbedLogger.cpp	Mon Jun 18 14:45:37 2018 +0000
+++ b/MbedLogger/MbedLogger.cpp	Mon Jun 18 21:02:55 2018 +0000
@@ -14,6 +14,8 @@
     _total_number_of_packets = 0;
     _mbed_transmit_loop = false;
     _received_filename = "";
+    
+    _log_file_line_counter = 0;     //used to set timer in finite state machine based on size of log
 }
 
 //this function has to be called for the time to function correctly
@@ -66,11 +68,18 @@
     else if (current_state == KEYBOARD)
         string_state = "KEYBOARD";
     else if (current_state == CHECK_TUNING)
-        string_state = "CHECK_TUNING";   
-    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_state = "CHECK_TUNING";
+    else if (current_state == POSITION_DIVE)
+        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";
     
     //record the string state, integer state, and then the data
     fprintf(_fp, "%s,%d,",string_state.c_str(),current_state);
@@ -126,7 +135,7 @@
     //open the file for reading
     string file_name_string = _file_system_string + "LOG000.csv";
     
-    int log_file_line_counter = 0;
+    _log_file_line_counter = 0;
 
     _fp = fopen(file_name_string.c_str(), "r");
        
@@ -138,7 +147,7 @@
         // 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);
-            log_file_line_counter++;
+            _log_file_line_counter++;
         }
     }
     
@@ -147,7 +156,7 @@
     
     //close the file
     closeLogFile();
-    pc().printf("\n\rLog file closed. Lines in log file: %d.\n\r", log_file_line_counter);
+    pc().printf("\n\rLog file closed. Lines in log file: %d.\n\r", _log_file_line_counter);
 }
 
 void MbedLogger::openFileForTransmit() { 
@@ -231,7 +240,9 @@
 
 // REACH ONE CHAR AT A TIME (EACH ITERATION OF STATE MACHINE...)
 
-void MbedLogger::readTransmitPacketOneChar() {           
+void MbedLogger::readTransmitPacketOneChar() {
+    led2() = !led2();
+             
     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
@@ -239,92 +250,95 @@
     static int transmit_crc_one = 0;            //hold crc values until they're reset with calculations
     static int transmit_crc_two = 0;
     
-    incoming_byte = pc().getc();
     
-    switch(transmit_state) {
-    case HEADER_117:
-        //continue processing
-        if (incoming_byte == 30){   //1E
-            transmit_state = HEADER_101;
-        }
-        //did not receive byte 1
-        else {
-            transmit_state = HEADER_117;    //go back to checking the first packet
-        }
-        break;
-                    
-    case HEADER_101:
-        if(incoming_byte == 31) {   //1F
-            transmit_state = TRANSMIT_PACKET_1;
-        }
-        break;
-    
-    case TRANSMIT_PACKET_1:
-        if (incoming_byte >= 0) {
-            input_packet[0] = 30;
-            input_packet[1] = 31;
-            input_packet[2] = incoming_byte;
-            
-            transmit_state = TRANSMIT_PACKET_2;
-            //_reply_byte3 = incoming_byte;
-        }
-        break;
+    while (pc().readable()) {
+        incoming_byte = pc().getc();
+        
+        led3() = !led3();
         
-    case TRANSMIT_PACKET_2:
+        switch(transmit_state) {
+        case HEADER_117:
+            //continue processing
+            if (incoming_byte == 30){   //1E
+                transmit_state = HEADER_101;
+            }
+            //did not receive byte 1
+            else {
+                transmit_state = HEADER_117;    //go back to checking the first packet
+            }
+            break;
+                        
+        case HEADER_101:
+            if(incoming_byte == 31) {   //1F
+                transmit_state = TRANSMIT_PACKET_1;
+            }
+            break;
         
-        if (incoming_byte >= 0) {                    
-            input_packet[3] = incoming_byte;
-            //_reply_byte4 = incoming_byte;
-        }
-        transmit_state = PACKET_CRC_ONE;
-        
-        break;
-        
-    case (PACKET_CRC_ONE):
-        transmit_crc_one = calcCrcOneArray(input_packet, 4);        //calc CRC 1 from the input packet (size 4)
-        
-        if (incoming_byte == transmit_crc_one) {
-            transmit_state = PACKET_CRC_TWO;
-        }
-        
-        else
-            transmit_state = HEADER_117;
-        //or state remains the same?
-        
-        break;
-    
-    case (PACKET_CRC_TWO):
-        transmit_state = HEADER_117;
-        transmit_crc_two = calcCrcTwoArray(input_packet, 4);        //calc CRC 2 from the input packet (size 4)
-               
-        //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;
+        case TRANSMIT_PACKET_1:
+            if (incoming_byte >= 0) {
+                input_packet[0] = 30;
+                input_packet[1] = 31;
+                input_packet[2] = incoming_byte;
+                
+                transmit_state = TRANSMIT_PACKET_2;
+                //_reply_byte3 = incoming_byte;
+            }
+            break;
+            
+        case TRANSMIT_PACKET_2:
             
-                // 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();                                                    
-                }
+            if (incoming_byte >= 0) {                    
+                input_packet[3] = incoming_byte;
+                //_reply_byte4 = incoming_byte;
+            }
+            transmit_state = PACKET_CRC_ONE;
             
-                _previous_reply_byte = requested_packet_number;                 //RECORD THIS BYTE to prevent new packets being created
+            break;
             
-                transmitDataPacket();                                           //continuously transmit current packet until it tells you to do the next packet (response from Python program)
+        case (PACKET_CRC_ONE):
+            transmit_crc_one = calcCrcOneArray(input_packet, 4);        //calc CRC 1 from the input packet (size 4)
+            
+            if (incoming_byte == transmit_crc_one) {
+                transmit_state = PACKET_CRC_TWO;
             }
             
             else
-                transmitDataPacket();   //if you get the same packet request, send the same packet
+                transmit_state = HEADER_117;
+            //or state remains the same?
             
-        }   //end of checksum (incoming_byte) if statement
+            break;
         
-        break;
-    }   //switch statement complete
-    
-    //led1() = !led1();
+        case (PACKET_CRC_TWO):
+            transmit_state = HEADER_117;
+            transmit_crc_two = calcCrcTwoArray(input_packet, 4);        //calc CRC 2 from the input packet (size 4)
+                   
+            //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();                                                    
+                    }
+                
+                    _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
+                
+            }   //end of checksum (incoming_byte) if statement
+            
+            break;
+        }   //switch statement complete
+    } //while statement complete
 }
 
 int MbedLogger::readTransmitPacket() {      
@@ -633,7 +647,6 @@
                         
                         // first confirm that the checksum is correct
                         if ((calc_crc_one == checksum_one) and (calc_crc_two == checksum_two)) {                            
-                            led2() = !led2();
                             // 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
@@ -744,7 +757,7 @@
         case HEADER_117:
             //continue processing
             if (incoming_byte == 117){
-                led2() = !led2();
+                //had led two here
                 process_state = HEADER_101;
                 pc().printf("DEBUG: Case 117\n\r");
             }
@@ -777,15 +790,15 @@
             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);
-            }
+//            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 = "";
             
@@ -1364,7 +1377,8 @@
     float depth_value = depthLoop().getPosition();
     float pitch_value = imu().getPitch();
     float heading_value = imu().getHeading();
-    float timer_value = stateMachine().getTimer();
+    //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);
@@ -1484,4 +1498,8 @@
 
 
     transmitDataPacket();
+}
+
+int MbedLogger::getLogSize() {
+    return _log_file_line_counter;
 }
\ No newline at end of file