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:
68:8f549749b8ce
Parent:
67:c86a4b464682
Child:
69:919ac8d7e023
--- a/MbedLogger/MbedLogger.cpp	Mon Jun 25 15:44:00 2018 +0000
+++ b/MbedLogger/MbedLogger.cpp	Wed Jun 27 23:01:53 2018 +0000
@@ -17,7 +17,9 @@
     
     _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";
+    _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;
 }
 
 //this function has to be called for the time to function correctly
@@ -47,17 +49,17 @@
     _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)
-    
+    // extrapolated from graph if V_s = 5.0
+    // https://www.nxp.com/docs/en/data-sheet/MPXA6115A.pdf 
+    float press_xducer_PSI = (22.029*(5.0*adc().readCh5()/4095.0) + 10.884) * 0.145038; // Press_Xducer (on-board)
+    float voltage_input = (adc().readCh6()/4095.0) * 5.0 * 7.8;  //Vin_Mon on PCB schematic (system input voltage from power supply)
+    float current_input = adc().readCh7()/4095.0; // I_mon (appears to be one-to-one from the schematic)
     // presssure onboard
     
     
-    _data_log[11] = current_input;       // i_in
+    _data_log[11] = current_input;      // i_in
     _data_log[12] = voltage_input;      // v_in
-    _data_log[13] = internal_pressure;      // vac_per
+    _data_log[13] = press_xducer_PSI;   // int_press_PSI
     
     //check what the current state is and create that string
     string string_state;
@@ -91,9 +93,13 @@
     string blank_space = ""; //to get consistent spacing in the file
     
     //record the string state, integer state, and then the data
+    //_heading_string is 160 characters long
+    
+    //make function that checks length of header versus string below, add padding
+    
     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],
+    fprintf(_fp, "%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%38s\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
@@ -142,6 +148,7 @@
     }
 }
 
+//prints current log file to the screen (terminal)
 void MbedLogger::printCurrentLogFile() {
     //open the file for reading
     string file_name_string = _file_system_string + "LOG000.csv";
@@ -220,7 +227,7 @@
     
     _data_packet.push_back(_current_line_length);
 
-    pc().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer);        //debug
+    //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 < _current_line_length; i++) {
@@ -251,9 +258,200 @@
 
 // REACH ONE CHAR AT A TIME (EACH ITERATION OF STATE MACHINE...)
 
+
+void MbedLogger::continuouslyTransmitDataNoTimer() {
+    string file_name_string = _file_system_string + "LOG000.csv";
+    
+    _fp = fopen(file_name_string.c_str(), "r");
+    
+    static int packet_num = 0;
+    
+    while(!feof(_fp)) {
+    
+        _packet_number = packet_num;
+        
+        //fseek
+            
+        readPacketInSeries();   //not using this but for testing purposes...
+    
+    //    // 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();                                                    
+            
+        transmitDataPacket(); 
+            
+        packet_num++;
+        
+        led3() = !led3();
+        
+        //check for incoming request
+    }
+    
+    closeLogFile(); //close log file if open
+    led4() = !led4();
+}
+
+bool MbedLogger::fsmTransmitData() {    //using the finite state machine   
+    pc().printf("debug fsmTransmitData (%d) (tpn: %d)\n\r",_fp,_transmit_packet_num);
+     
+    if (!feof(_fp)) {   //check for end of file
+        wait(0.1);
+
+        //based on the internal packet number of the class    
+        transmitPacketNumber(_transmit_packet_num);
+        
+        //check for incoming transmission
+        pythonTransmitRequest();
+    
+        _transmit_packet_num++;
+    
+        led3() = !led3();
+        
+        return true;
+    }
+    
+    else 
+        return false;
+}
+
+void MbedLogger::continuouslyTransmitData() {
+    static int packet_num = 0;
+    
+    while(!feof(_fp)) {
+        wait(0.1);
+        
+        _packet_number = packet_num;
+
+        transmitPacketNumber(packet_num);
+        
+        //check for incoming transmission
+        pythonTransmitRequest();
+    
+        packet_num++;
+    
+        led3() = !led3();
+    }
+}
+
+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
+    
+    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)
+    transmitDataPacket();   //transmit the assembled packet
+}
+
+void MbedLogger::pythonTransmitRequest() {
+    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
+            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;
+            
+        case TRANSMIT_PACKET_2:
+            
+            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;
+                
+                    // 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
+}
+
 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
@@ -509,6 +707,8 @@
     fseek(_fp, 0, SEEK_SET);        // SEEK_SET is the beginning of file
     
     _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);
 }
 
 void MbedLogger::endTransmissionCloseFile() {
@@ -540,9 +740,7 @@
 
 // function checks for incoming data (receiver function) from a Python program that transmits a file
 // current limit is a file that has 255 lines of data
-bool MbedLogger::checkForIncomingData() {
-    led1() = !led1();
-        
+bool MbedLogger::checkForIncomingData() {        
     int receive_packet_number;
     int receive_total_number_packets;
     int receive_packet_size;
@@ -617,9 +815,7 @@
                 
                 // 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);
-                
-                led4() = !led4();        
-                
+                       
                 //process packet data, future version will append for larger data sizes, 0xFFFF
                 
                 // IF YOU GET AN INTERRUPTED DATA STREAM YOU NEED TO BREAK OUT OF THE LOOP
@@ -1077,6 +1273,8 @@
 }
 
 void MbedLogger::closeLogFile() {
+    led4() = 1;
+    
     if (_fp == NULL){
         pc().printf("MbedLogger: (%s) LOG FILE WAS ALREADY CLOSED!\n\r", _file_system_string.c_str());
     }
@@ -1096,8 +1294,6 @@
 }
 
 void MbedLogger::receiveMissionDataWithFSM() {   
-    led4() = !led4();
- 
     checkForIncomingData();  
             
 // Example: send reply 75 65 00 00             
@@ -1157,32 +1353,10 @@
     }
 }
 
-int MbedLogger::getFilePointerState() {
-    if (_fp)
-        return 1;   //file is not null (has an address), open
-    else
-        return 0;   //file is null, closed
-}
-
-void MbedLogger::testToggleFilePointer() {
-    pc().printf("testToggleFilePointer\n\r");
-    
-    string file_name_string = _file_system_string + "example99.txt";
-    
-    if (_fp)
-        _fp = NULL;
-    else
-        _fp = fopen(file_name_string.c_str(), "w");
-}
-
 int MbedLogger::getNumberOfPackets() {
     return _total_number_of_packets;
 }
 
-int MbedLogger::currentPacketNumber() {
-    return _packet_number;
-}
-
 //only do this for the MBED because of the limited file size
 //write one line to the file (open to write, this will erase all other data) and close it.
 void MbedLogger::eraseFile() {    
@@ -1373,4 +1547,8 @@
 
 int MbedLogger::getLogSize() {
     return _log_file_line_counter;      //number accurate after running getNumberOfPacketsInCurrentLog
+}
+
+void MbedLogger::setTransmitPacketNumber(int packet_number) {
+    _transmit_packet_num = packet_number;
 }
\ No newline at end of file