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

MbedLogger/MbedLogger.cpp

Committer:
tnhnrl
Date:
2017-12-20
Revision:
34:9b66c5188051
Parent:
32:f2f8ae34aadc
Child:
35:2f66ea4863d5

File content as of revision 34:9b66c5188051:

#include "MbedLogger.hpp"
#include "StaticDefs.hpp"

MbedLogger::MbedLogger() {
    _f = 0;
    _file_number = 0;
}

//creates a new file each time it's called
void MbedLogger::createNewFiles() {
    
    int filenum = 0;
    char filename[128];
    
    while(true) {
        //create a file named Log001.csv through Log999.csv
        sprintf(filename, "/local/Log%03d.csv", filenum);
        
        //try to read a file
        _fp = fopen(filename, "r");
       
        //check if this file does not exist, create it, and write to it
        if (_fp == NULL) {            
            //you want to do stuff here!
            _fp = fopen(filename, "a");
            
            //write the header
            fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n");
            
            //DO NOT CLOSE THE FILE, keep it open
            break;
        }
        
        //if the file already exists, keep upping the counter
        else {
            fclose(_fp); //close the old file you tried to read
            filenum++;  //file name number
        }
        wait_us(100);
    }
    
    //set this variable to print the current file
    _file_number = filenum;
}

void MbedLogger::saveArrayToFile(string string_state, int input_state, float *input) {    
    //open single log file
    FILE *fp = fopen("/local/Log000.csv", "r");

    //write to the file (header and data)
    fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n");
    fprintf(fp, "%s,%d,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",string_state.c_str(),input_state,input[0],input[1],input[2],input[3],input[4],input[5],input[6],input[7],input[8]);
    
    //close file
    fclose(fp);
}

//void MbedLogger::saveArrayToFile(string string_state, int input_state, float *input) {    
//    //write to the file
//    fprintf(_fp, "%s,%d,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",string_state.c_str(),input_state,input[0],input[1],input[2],input[3],input[4],input[5],input[6],input[7],input[8]);
//}

void MbedLogger::saveSequenceStringToFile(string str_input) {        
    //write to the file
    fprintf(_fp, "%s\n",str_input.c_str());
}

void MbedLogger::printMbedDirectory() {
    DirectoryList dir( "/local" );
 
    if ( dir.error_check() )
        error( "directory could not be opened\r\n" );
 
    for ( int i = 0; i < dir.size(); i++ )
        printf( "%s\r\n", dir[ i ].c_str() );
}

void MbedLogger::printFromLogFile() {
    char filename[128];
    sprintf(filename, "/local/Log%03d.csv", _file_number);
    
    //open the file
    FILE *fp = fopen(filename, "r");
    
    // http://people.cs.uchicago.edu/~dmfranklin/tutorials/fgets.txt
   
    //while not end of file, read through line by line???
    char buffer[500];
    
    //read the file line by line (and print each line)
    pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\r",_file_number);
    while (!feof(fp)) {
        // read in the line and make sure it was successful
        if (fgets(buffer,500,fp) != NULL) {
            pc().printf("%s\r",buffer);
            //pc().printf("%d: %s\n\r",lineno++,buffer);
        }
    }
    
    //close the file
    fclose(fp);
    pc().printf("\n\rLog file closed.\n\r");
}

//ONLY CLOSE THE FILE WITH THIS
//always close the file when you're done using it
void MbedLogger::closeFile() {
    fclose(_fp);
}