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:
82:0981b9ada820
Parent:
80:4e5d306d695b
Child:
84:eccd8e837134
--- a/MbedLogger/MbedLogger.cpp	Thu Nov 08 22:30:32 2018 +0000
+++ b/MbedLogger/MbedLogger.cpp	Fri Feb 15 16:00:17 2019 +0000
@@ -1,11 +1,13 @@
 #include "MbedLogger.hpp"
 #include "StaticDefs.hpp"
+#include <stdarg.h>
 
   //Timer t;    //used to test time to create packet    //timing debug
 
 MbedLogger::MbedLogger(string file_system_input_string) {
-    _file_system_string = file_system_input_string;
+    _file_system_string = file_system_input_string;                  // how to make the LOG000.csv a variable string?
     _full_file_path_string = _file_system_string + "LOG000.csv";    //use multiple logs in the future? (after file size is too large)  
+    _full_diagfile_path_string = _file_system_string + "DIAG000.txt"; 
     _file_number = 0;
     _file_transmission = true;
     _confirmed_packet_number = 0;   //must set this to zero
@@ -19,7 +21,7 @@
     
     //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;
+    _diag_heading_string =  "Diagnostics file header \n";
     
     _fsm_transmit_complete = false;
     
@@ -43,7 +45,7 @@
 
 void MbedLogger::recordData(int current_state) {
     int data_log_time = mbedLogger().getSystemTime();                 //read the system timer to get unix timestamp
-    
+    int start_time = 1518467832;
     _data_log[0] = depthLoop().getCommand();        //depth command
     _data_log[1] = depthLoop().getPosition();       //depth reading (filtered depth)
     _data_log[2] = pitchLoop().getCommand();        //pitch command
@@ -119,7 +121,13 @@
     else if (current_state == TX_MBED_LOG) 
         string_state = "TX_MBED_LOG";
     else if (current_state == RX_SEQUENCE) 
-        string_state = "RECEIVE_SEQUENCE";    
+        string_state = "RECEIVE_SEQUENCE";
+    else if (current_state == LEG_POSITION_DIVE) 
+        string_state = "LEG_POS_DIVE";
+    else if (current_state == LEG_POSITION_RISE) 
+        string_state = "LEG_POS_RISE";
+    else if (current_state == FB_EXIT) 
+        string_state = "FB_EXIT";        
         
     string blank_space = ""; //to get consistent spacing in the file (had a nonsense char w/o this)
     
@@ -127,7 +135,7 @@
     
     //verified that this generates the correct line length of 254 using SOLELY an mbed 08/16/2018
     fprintf(_fp, "%17s,%.2d,%10d,%5.1f,%5.1f,%6.1f,%6.1f,%4.0f,%4.0f,%6.1f,%5.1f,%6.1f,%5.1f,%6.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,%6.2f,%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",
-    string_state.c_str(),current_state,data_log_time,
+    string_state.c_str(),current_state,data_log_time-start_time,
     _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]);
@@ -1236,13 +1244,34 @@
         closeLogFile();
     }
 }
+void MbedLogger::appendDiagFile(char *printf_string, int flushclose) {
+    //
+    
+ int start_time = 1518467832;
+ //in the future create the ability to set the start time
+
+    int time_now = mbedLogger().getSystemTime(); 
+     if (!_fp2) {     //if not present
+            _fp2 = fopen(_full_diagfile_path_string.c_str(), "a");
+        }
+        int del_time = time_now - start_time;
+        //record data using the recordData function (takes in the state integer)
+        fprintf(_fp2, "time=%d  seconds ", del_time);
+        fprintf(_fp2, printf_string);  
+        if(flushclose == 1) { fflush(_fp2); }
+        if(flushclose == 0) {fclose(_fp2); _fp2 = NULL; }
+}
 
 // initialize and close the file
 // log file freezes at 0x0000006c
 void MbedLogger::initializeLogFile() {
-    string file_name_string = _file_system_string + "LOG000.csv";
+    string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName;   //  "DIAG000.txt";
+    //string file_name_string = _file_system_string + "LOG000.csv";  // how to pass in log file name as a string?
+    _full_file_path_string = file_name_string;
+    char buf[256];
     xbee().printf("%s file system init\n\r", _file_system_string.c_str());
-    
+    sprintf(buf, "%s mbedlogger():initializelogfile:  file system init\n\r", _file_system_string.c_str());
+          appendDiagFile(buf,1);
     //try to open this file...
     _fp = fopen(file_name_string.c_str(), "r");
     
@@ -1256,6 +1285,27 @@
     else
         closeLogFile();   //close the opened read file
 }
+void MbedLogger::initializeDiagFile(int print_diag) {
+    //string file_name_string = _file_system_string + "DIAG000.txt";  // how to pass in log file name as a string?
+    string file_name_string = _file_system_string + configFileIO().logFilesStruct.diagFileName;   //  "DIAG000.txt";
+    _full_diagfile_path_string = file_name_string;
+    char buf[256];
+    xbee().printf("%s file system init\n\r", _file_system_string.c_str());
+    sprintf(buf, "mbedlogger():Initializediagfile  input variable diagfilename:  %s file system init\n\r", file_name_string.c_str());
+    if(print_diag == 1)  {appendDiagFile(buf,3);}          //configFileIO().logFilesStruct.diagFileName
+    //try to open this file...
+    if( _fp2 == NULL) { _fp2 = fopen(file_name_string.c_str(), "r"); }
+    
+    //if the file is empty, create this.
+    if (!_fp2) {
+        _fp2 = 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(_fp2,_diag_heading_string.c_str());
+        closeDiagFile(); 
+    }
+    else
+        closeDiagFile();   //close the opened read file
+}
 
 int MbedLogger::fileTransmitState() {
     return _file_transmission_state;
@@ -1425,6 +1475,22 @@
         _fp = NULL;     //set pointer to zero
     }    
 }
+void MbedLogger::closeDiagFile() { // closes he present diag file on _fp2
+    led4() = 1;
+    
+    if (_fp2 == NULL){
+        xbee().printf("MbedLogger: (%s) DIAG FILE WAS ALREADY CLOSED!\n\r", _full_diagfile_path_string.c_str());
+    }
+    
+    else {
+        xbee().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _full_diagfile_path_string.c_str());
+        
+        //close file
+        fclose(_fp2);
+                
+        _fp2 = NULL;     //set pointer to zero
+    }    
+}
 
 void MbedLogger::activateReceivePacket() {
     _mbed_receive_loop = true;