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

Committer:
tnhnrl
Date:
Wed Dec 20 20:24:15 2017 +0000
Revision:
34:9b66c5188051
Parent:
32:f2f8ae34aadc
Child:
35:2f66ea4863d5
Problem with log file on mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tnhnrl 32:f2f8ae34aadc 1 #include "MbedLogger.hpp"
tnhnrl 32:f2f8ae34aadc 2 #include "StaticDefs.hpp"
tnhnrl 32:f2f8ae34aadc 3
tnhnrl 32:f2f8ae34aadc 4 MbedLogger::MbedLogger() {
tnhnrl 32:f2f8ae34aadc 5 _f = 0;
tnhnrl 32:f2f8ae34aadc 6 _file_number = 0;
tnhnrl 32:f2f8ae34aadc 7 }
tnhnrl 32:f2f8ae34aadc 8
tnhnrl 32:f2f8ae34aadc 9 //creates a new file each time it's called
tnhnrl 34:9b66c5188051 10 void MbedLogger::createNewFiles() {
tnhnrl 32:f2f8ae34aadc 11
tnhnrl 32:f2f8ae34aadc 12 int filenum = 0;
tnhnrl 32:f2f8ae34aadc 13 char filename[128];
tnhnrl 32:f2f8ae34aadc 14
tnhnrl 32:f2f8ae34aadc 15 while(true) {
tnhnrl 32:f2f8ae34aadc 16 //create a file named Log001.csv through Log999.csv
tnhnrl 32:f2f8ae34aadc 17 sprintf(filename, "/local/Log%03d.csv", filenum);
tnhnrl 32:f2f8ae34aadc 18
tnhnrl 32:f2f8ae34aadc 19 //try to read a file
tnhnrl 32:f2f8ae34aadc 20 _fp = fopen(filename, "r");
tnhnrl 32:f2f8ae34aadc 21
tnhnrl 32:f2f8ae34aadc 22 //check if this file does not exist, create it, and write to it
tnhnrl 32:f2f8ae34aadc 23 if (_fp == NULL) {
tnhnrl 32:f2f8ae34aadc 24 //you want to do stuff here!
tnhnrl 32:f2f8ae34aadc 25 _fp = fopen(filename, "a");
tnhnrl 32:f2f8ae34aadc 26
tnhnrl 32:f2f8ae34aadc 27 //write the header
tnhnrl 34:9b66c5188051 28 fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n");
tnhnrl 32:f2f8ae34aadc 29
tnhnrl 32:f2f8ae34aadc 30 //DO NOT CLOSE THE FILE, keep it open
tnhnrl 32:f2f8ae34aadc 31 break;
tnhnrl 32:f2f8ae34aadc 32 }
tnhnrl 32:f2f8ae34aadc 33
tnhnrl 32:f2f8ae34aadc 34 //if the file already exists, keep upping the counter
tnhnrl 32:f2f8ae34aadc 35 else {
tnhnrl 32:f2f8ae34aadc 36 fclose(_fp); //close the old file you tried to read
tnhnrl 32:f2f8ae34aadc 37 filenum++; //file name number
tnhnrl 32:f2f8ae34aadc 38 }
tnhnrl 32:f2f8ae34aadc 39 wait_us(100);
tnhnrl 32:f2f8ae34aadc 40 }
tnhnrl 32:f2f8ae34aadc 41
tnhnrl 32:f2f8ae34aadc 42 //set this variable to print the current file
tnhnrl 32:f2f8ae34aadc 43 _file_number = filenum;
tnhnrl 32:f2f8ae34aadc 44 }
tnhnrl 32:f2f8ae34aadc 45
tnhnrl 34:9b66c5188051 46 void MbedLogger::saveArrayToFile(string string_state, int input_state, float *input) {
tnhnrl 34:9b66c5188051 47 //open single log file
tnhnrl 34:9b66c5188051 48 FILE *fp = fopen("/local/Log000.csv", "r");
tnhnrl 34:9b66c5188051 49
tnhnrl 34:9b66c5188051 50 //write to the file (header and data)
tnhnrl 34:9b66c5188051 51 fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n");
tnhnrl 34:9b66c5188051 52 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]);
tnhnrl 34:9b66c5188051 53
tnhnrl 34:9b66c5188051 54 //close file
tnhnrl 34:9b66c5188051 55 fclose(fp);
tnhnrl 32:f2f8ae34aadc 56 }
tnhnrl 32:f2f8ae34aadc 57
tnhnrl 34:9b66c5188051 58 //void MbedLogger::saveArrayToFile(string string_state, int input_state, float *input) {
tnhnrl 34:9b66c5188051 59 // //write to the file
tnhnrl 34:9b66c5188051 60 // 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]);
tnhnrl 34:9b66c5188051 61 //}
tnhnrl 34:9b66c5188051 62
tnhnrl 32:f2f8ae34aadc 63 void MbedLogger::saveSequenceStringToFile(string str_input) {
tnhnrl 32:f2f8ae34aadc 64 //write to the file
tnhnrl 34:9b66c5188051 65 fprintf(_fp, "%s\n",str_input.c_str());
tnhnrl 32:f2f8ae34aadc 66 }
tnhnrl 32:f2f8ae34aadc 67
tnhnrl 32:f2f8ae34aadc 68 void MbedLogger::printMbedDirectory() {
tnhnrl 32:f2f8ae34aadc 69 DirectoryList dir( "/local" );
tnhnrl 32:f2f8ae34aadc 70
tnhnrl 32:f2f8ae34aadc 71 if ( dir.error_check() )
tnhnrl 32:f2f8ae34aadc 72 error( "directory could not be opened\r\n" );
tnhnrl 32:f2f8ae34aadc 73
tnhnrl 32:f2f8ae34aadc 74 for ( int i = 0; i < dir.size(); i++ )
tnhnrl 32:f2f8ae34aadc 75 printf( "%s\r\n", dir[ i ].c_str() );
tnhnrl 32:f2f8ae34aadc 76 }
tnhnrl 32:f2f8ae34aadc 77
tnhnrl 32:f2f8ae34aadc 78 void MbedLogger::printFromLogFile() {
tnhnrl 32:f2f8ae34aadc 79 char filename[128];
tnhnrl 32:f2f8ae34aadc 80 sprintf(filename, "/local/Log%03d.csv", _file_number);
tnhnrl 32:f2f8ae34aadc 81
tnhnrl 32:f2f8ae34aadc 82 //open the file
tnhnrl 32:f2f8ae34aadc 83 FILE *fp = fopen(filename, "r");
tnhnrl 32:f2f8ae34aadc 84
tnhnrl 32:f2f8ae34aadc 85 // http://people.cs.uchicago.edu/~dmfranklin/tutorials/fgets.txt
tnhnrl 32:f2f8ae34aadc 86
tnhnrl 32:f2f8ae34aadc 87 //while not end of file, read through line by line???
tnhnrl 32:f2f8ae34aadc 88 char buffer[500];
tnhnrl 32:f2f8ae34aadc 89
tnhnrl 32:f2f8ae34aadc 90 //read the file line by line (and print each line)
tnhnrl 34:9b66c5188051 91 pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\r",_file_number);
tnhnrl 32:f2f8ae34aadc 92 while (!feof(fp)) {
tnhnrl 32:f2f8ae34aadc 93 // read in the line and make sure it was successful
tnhnrl 32:f2f8ae34aadc 94 if (fgets(buffer,500,fp) != NULL) {
tnhnrl 32:f2f8ae34aadc 95 pc().printf("%s\r",buffer);
tnhnrl 32:f2f8ae34aadc 96 //pc().printf("%d: %s\n\r",lineno++,buffer);
tnhnrl 32:f2f8ae34aadc 97 }
tnhnrl 32:f2f8ae34aadc 98 }
tnhnrl 32:f2f8ae34aadc 99
tnhnrl 32:f2f8ae34aadc 100 //close the file
tnhnrl 32:f2f8ae34aadc 101 fclose(fp);
tnhnrl 32:f2f8ae34aadc 102 pc().printf("\n\rLog file closed.\n\r");
tnhnrl 32:f2f8ae34aadc 103 }
tnhnrl 32:f2f8ae34aadc 104
tnhnrl 32:f2f8ae34aadc 105 //ONLY CLOSE THE FILE WITH THIS
tnhnrl 32:f2f8ae34aadc 106 //always close the file when you're done using it
tnhnrl 32:f2f8ae34aadc 107 void MbedLogger::closeFile() {
tnhnrl 32:f2f8ae34aadc 108 fclose(_fp);
tnhnrl 32:f2f8ae34aadc 109 }