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:
Thu Feb 15 23:07:25 2018 +0000
Revision:
49:47ffa4feb6db
Parent:
48:20e681885161
Working pool-tested code. Rudder disabled (some weird coupling issue).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tnhnrl 46:030be9f5c793 1 #include "MbedLogger.hpp"
tnhnrl 46:030be9f5c793 2 #include "StaticDefs.hpp"
tnhnrl 46:030be9f5c793 3
tnhnrl 46:030be9f5c793 4 MbedLogger::MbedLogger(string file_system_input_string) {
tnhnrl 46:030be9f5c793 5 _file_system_string = file_system_input_string;
tnhnrl 46:030be9f5c793 6
tnhnrl 49:47ffa4feb6db 7 _full_file_path_string = _file_system_string + "LOG000.csv"; //use multiple logs in the future? (after file size is too large)
tnhnrl 46:030be9f5c793 8
tnhnrl 46:030be9f5c793 9 _f = 0;
tnhnrl 46:030be9f5c793 10 _file_number = 0;
tnhnrl 46:030be9f5c793 11
tnhnrl 46:030be9f5c793 12 _file_transmission = true;
tnhnrl 46:030be9f5c793 13
tnhnrl 46:030be9f5c793 14 _confirmed_packet_number = 0; //must set this to zero
tnhnrl 46:030be9f5c793 15
tnhnrl 46:030be9f5c793 16 _transmit_counter = 0;
tnhnrl 46:030be9f5c793 17
tnhnrl 46:030be9f5c793 18 _file_transmission_state = -1;
tnhnrl 46:030be9f5c793 19
tnhnrl 46:030be9f5c793 20 _total_number_of_packets = 0;
tnhnrl 46:030be9f5c793 21
tnhnrl 46:030be9f5c793 22 _mbed_transmit_loop = false;
tnhnrl 46:030be9f5c793 23
tnhnrl 46:030be9f5c793 24 _file_transmission_complete = false;
tnhnrl 46:030be9f5c793 25 }
tnhnrl 46:030be9f5c793 26
tnhnrl 46:030be9f5c793 27 //this function has to be called for the time to function correctly
tnhnrl 46:030be9f5c793 28 void MbedLogger::setLogTime() {
tnhnrl 47:fb3c7929d3f3 29 pc().printf("\n%s log time set.\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 30 set_time(1518467832); // Set RTC time to Mon, 12 FEB 2018 15:37
tnhnrl 46:030be9f5c793 31 }
tnhnrl 46:030be9f5c793 32
tnhnrl 46:030be9f5c793 33 //in the future create the ability to set the start time
tnhnrl 46:030be9f5c793 34 int MbedLogger::getSystemTime() {
tnhnrl 46:030be9f5c793 35 time_t seconds = time(NULL); // Time as seconds since January 1, 1970
tnhnrl 46:030be9f5c793 36
tnhnrl 46:030be9f5c793 37 return seconds;
tnhnrl 46:030be9f5c793 38 }
tnhnrl 46:030be9f5c793 39
tnhnrl 46:030be9f5c793 40 void MbedLogger::recordData(int current_state) {
tnhnrl 46:030be9f5c793 41 int data_log_int = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp
tnhnrl 46:030be9f5c793 42 //_data_log[0] = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp
tnhnrl 46:030be9f5c793 43 _data_log[1] = depthLoop().getCommand(); //depth command
tnhnrl 46:030be9f5c793 44 _data_log[2] = depthLoop().getPosition(); //depth reading
tnhnrl 46:030be9f5c793 45 _data_log[3] = pitchLoop().getCommand(); //pitch command
tnhnrl 46:030be9f5c793 46 _data_log[4] = pitchLoop().getPosition(); //pitch reading
tnhnrl 46:030be9f5c793 47 _data_log[5] = bce().getSetPosition_mm(); //BCE command
tnhnrl 46:030be9f5c793 48 _data_log[6] = bce().getPosition_mm(); //BCE reading
tnhnrl 46:030be9f5c793 49 _data_log[7] = batt().getSetPosition_mm(); //Batt command
tnhnrl 46:030be9f5c793 50 _data_log[8] = batt().getPosition_mm(); //Batt reading
tnhnrl 46:030be9f5c793 51 _data_log[9] = pitchLoop().getVelocity(); // pitchRate_degs (degrees per second)
tnhnrl 46:030be9f5c793 52 _data_log[10] = depthLoop().getVelocity(); // depthRate_fps (feet per second)
tnhnrl 46:030be9f5c793 53
tnhnrl 46:030be9f5c793 54 //check what the current state is and create that string
tnhnrl 46:030be9f5c793 55 string string_state;
tnhnrl 46:030be9f5c793 56 if (current_state == SIT_IDLE)
tnhnrl 46:030be9f5c793 57 string_state = "SIT_IDLE";
tnhnrl 46:030be9f5c793 58 else if (current_state == FIND_NEUTRAL)
tnhnrl 46:030be9f5c793 59 string_state = "FIND_NEUTRAL";
tnhnrl 46:030be9f5c793 60 else if (current_state == DIVE)
tnhnrl 46:030be9f5c793 61 string_state = "DIVE";
tnhnrl 46:030be9f5c793 62 else if (current_state == RISE)
tnhnrl 46:030be9f5c793 63 string_state = "RISE";
tnhnrl 46:030be9f5c793 64 else if (current_state == FLOAT_LEVEL)
tnhnrl 46:030be9f5c793 65 string_state = "FLOAT_LEVEL";
tnhnrl 46:030be9f5c793 66 else if (current_state == FLOAT_BROADCAST)
tnhnrl 46:030be9f5c793 67 string_state = "FLOAT_BROADCAST";
tnhnrl 46:030be9f5c793 68 else if (current_state == EMERGENCY_CLIMB)
tnhnrl 46:030be9f5c793 69 string_state = "EMERGENCY_CLIMB";
tnhnrl 46:030be9f5c793 70 else if (current_state == MULTI_DIVE)
tnhnrl 46:030be9f5c793 71 string_state = "MULTI_DIVE";
tnhnrl 46:030be9f5c793 72 else if (current_state == MULTI_RISE)
tnhnrl 46:030be9f5c793 73 string_state = "MULTI_RISE";
tnhnrl 46:030be9f5c793 74 else if (current_state == KEYBOARD)
tnhnrl 49:47ffa4feb6db 75 string_state = "KEYBOARD";
tnhnrl 49:47ffa4feb6db 76 else if (current_state == CHECK_TUNING)
tnhnrl 49:47ffa4feb6db 77 string_state = "CHECK_TUNING";
tnhnrl 46:030be9f5c793 78
tnhnrl 46:030be9f5c793 79 //record the string state, integer state, and then the data
tnhnrl 46:030be9f5c793 80 fprintf(_fp, "%s,%d,",string_state.c_str(),current_state);
tnhnrl 46:030be9f5c793 81 fprintf(_fp, "%d,",data_log_int);
tnhnrl 46:030be9f5c793 82 fprintf(_fp, "%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",_data_log[1],
tnhnrl 46:030be9f5c793 83 _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]);
tnhnrl 46:030be9f5c793 84 }
tnhnrl 46:030be9f5c793 85
tnhnrl 46:030be9f5c793 86 void MbedLogger::printMbedDirectory() {
tnhnrl 46:030be9f5c793 87 DIR *dir;
tnhnrl 46:030be9f5c793 88 struct dirent *dp; //dirent.h is the format of directory entries
tnhnrl 46:030be9f5c793 89 int log_found =0,loop=0;
tnhnrl 46:030be9f5c793 90 long int temp=0;
tnhnrl 46:030be9f5c793 91
tnhnrl 46:030be9f5c793 92 //char * char_pointer;
tnhnrl 46:030be9f5c793 93 char * numstart;
tnhnrl 46:030be9f5c793 94 //char *numstop;
tnhnrl 46:030be9f5c793 95
tnhnrl 46:030be9f5c793 96 pc().printf("\n\rPrinting out the directory of device %s\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 97
tnhnrl 46:030be9f5c793 98 if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) {
tnhnrl 46:030be9f5c793 99 pc().printf("MBED directory could not be opened\r\n");
tnhnrl 46:030be9f5c793 100 }
tnhnrl 46:030be9f5c793 101 else
tnhnrl 46:030be9f5c793 102 {
tnhnrl 46:030be9f5c793 103 while ( NULL != (dp = readdir( dir )) )
tnhnrl 46:030be9f5c793 104 {
tnhnrl 46:030be9f5c793 105 if(strncmp(dp->d_name,"LOG",3)==0)
tnhnrl 46:030be9f5c793 106 {
tnhnrl 46:030be9f5c793 107 log_found=1;
tnhnrl 46:030be9f5c793 108 /*numstart = dp->d_name; //Look for third character (do safety)
tnhnrl 46:030be9f5c793 109 numstop = strchr(dp->d_name,'.');
tnhnrl 46:030be9f5c793 110 if(numstart!=NULL&&numstop!=NULL)
tnhnrl 46:030be9f5c793 111 {
tnhnrl 46:030be9f5c793 112 temp=numstop-numstart;
tnhnrl 46:030be9f5c793 113 }
tnhnrl 46:030be9f5c793 114 else
tnhnrl 46:030be9f5c793 115 log_found=0; //Something is not right. Ignore
tnhnrl 46:030be9f5c793 116 */
tnhnrl 46:030be9f5c793 117 numstart=dp->d_name+3;
tnhnrl 46:030be9f5c793 118 temp=strtol(numstart,NULL,10); //add in check to see if this is null (start logs at one)
tnhnrl 46:030be9f5c793 119 }
tnhnrl 46:030be9f5c793 120 else
tnhnrl 46:030be9f5c793 121 log_found=0;
tnhnrl 46:030be9f5c793 122 pc().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp);
tnhnrl 46:030be9f5c793 123
tnhnrl 46:030be9f5c793 124 loop++;
tnhnrl 46:030be9f5c793 125 }
tnhnrl 46:030be9f5c793 126 }
tnhnrl 46:030be9f5c793 127 }
tnhnrl 46:030be9f5c793 128
tnhnrl 46:030be9f5c793 129 void MbedLogger::printCurrentLogFile() {
tnhnrl 46:030be9f5c793 130 //open the file for reading
tnhnrl 46:030be9f5c793 131 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 132
tnhnrl 46:030be9f5c793 133 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 46:030be9f5c793 134
tnhnrl 46:030be9f5c793 135 // http://people.cs.uchicago.edu/~dmfranklin/tutorials/fgets.txt
tnhnrl 46:030be9f5c793 136
tnhnrl 46:030be9f5c793 137 //while not end of file, read through line by line???
tnhnrl 46:030be9f5c793 138 char buffer[500];
tnhnrl 46:030be9f5c793 139
tnhnrl 46:030be9f5c793 140 //read the file line by line (and print each line)
tnhnrl 46:030be9f5c793 141 pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\n\r",_file_number);
tnhnrl 46:030be9f5c793 142 while (!feof(_fp)) {
tnhnrl 46:030be9f5c793 143 // read in the line and make sure it was successful
tnhnrl 46:030be9f5c793 144 if (fgets(buffer,500,_fp) != NULL) {
tnhnrl 46:030be9f5c793 145 pc().printf("%s\r",buffer);
tnhnrl 46:030be9f5c793 146 }
tnhnrl 46:030be9f5c793 147 }
tnhnrl 46:030be9f5c793 148
tnhnrl 46:030be9f5c793 149 //close the file
tnhnrl 46:030be9f5c793 150 closeLogFile();
tnhnrl 46:030be9f5c793 151 pc().printf("\n\rLog file closed.\n\r");
tnhnrl 46:030be9f5c793 152 }
tnhnrl 46:030be9f5c793 153
tnhnrl 46:030be9f5c793 154 void MbedLogger::openFileForTransmit() {
tnhnrl 46:030be9f5c793 155 pc().printf("\n\ropenFileForTransmit\n\r");
tnhnrl 46:030be9f5c793 156 //open the current file to read the contents
tnhnrl 46:030be9f5c793 157 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 158
tnhnrl 46:030be9f5c793 159 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 46:030be9f5c793 160
tnhnrl 46:030be9f5c793 161 _file_transmission = true;
tnhnrl 46:030be9f5c793 162 }
tnhnrl 46:030be9f5c793 163
tnhnrl 46:030be9f5c793 164 void MbedLogger::transmitCurrentLogFileLine(bool next_line) {
tnhnrl 46:030be9f5c793 165 vector <int> current_packet;
tnhnrl 46:030be9f5c793 166
tnhnrl 46:030be9f5c793 167 pc().printf("\n\rtransmitCurrentLogFile\n\r");
tnhnrl 46:030be9f5c793 168
tnhnrl 46:030be9f5c793 169 //while not end of file, read through line by line???
tnhnrl 46:030be9f5c793 170
tnhnrl 46:030be9f5c793 171 //transmit the file line by line (reads one line)
tnhnrl 46:030be9f5c793 172 //if you get the command to send a new line, go to the next line
tnhnrl 46:030be9f5c793 173 if (next_line) {
tnhnrl 46:030be9f5c793 174 if (fgets(_line_buffer,256,_fp) != NULL) {
tnhnrl 46:030be9f5c793 175 pc().printf("%s\r",_line_buffer);
tnhnrl 46:030be9f5c793 176 }
tnhnrl 46:030be9f5c793 177 }
tnhnrl 46:030be9f5c793 178
tnhnrl 46:030be9f5c793 179 // GET BUFFER LENGTH
tnhnrl 46:030be9f5c793 180 _current_line_length = strlen(_line_buffer);
tnhnrl 46:030be9f5c793 181
tnhnrl 46:030be9f5c793 182 //DEBUG PRINT TO SCREEN
tnhnrl 46:030be9f5c793 183 pc().printf("line_buffer size: %d %<%s>\r",_current_line_length,_line_buffer);
tnhnrl 46:030be9f5c793 184 }
tnhnrl 46:030be9f5c793 185
tnhnrl 46:030be9f5c793 186 void MbedLogger::createDataPacket() {
tnhnrl 46:030be9f5c793 187 //pc().printf("createDataPacket\n\r");
tnhnrl 46:030be9f5c793 188
tnhnrl 46:030be9f5c793 189 // packet is 7565 0001 FFFF DATA DATA DATA ... CRC1 CRC2
tnhnrl 46:030be9f5c793 190
tnhnrl 46:030be9f5c793 191 //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
tnhnrl 46:030be9f5c793 192 _data_packet.clear();
tnhnrl 46:030be9f5c793 193
tnhnrl 46:030be9f5c793 194 //DATA PACKET HEADER
tnhnrl 46:030be9f5c793 195 _data_packet.push_back(117); //0x75
tnhnrl 46:030be9f5c793 196 _data_packet.push_back(101); //0x65
tnhnrl 46:030be9f5c793 197
tnhnrl 46:030be9f5c793 198 _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form
tnhnrl 46:030be9f5c793 199 _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form
tnhnrl 46:030be9f5c793 200
tnhnrl 46:030be9f5c793 201 _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form
tnhnrl 46:030be9f5c793 202 _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form
tnhnrl 46:030be9f5c793 203
tnhnrl 46:030be9f5c793 204 _data_packet.push_back(_current_line_length);
tnhnrl 46:030be9f5c793 205
tnhnrl 46:030be9f5c793 206 //DATA FROM LINE READ (read string character by chracter)
tnhnrl 46:030be9f5c793 207 for (int i = 0; i < _current_line_length; i++) {
tnhnrl 46:030be9f5c793 208 _data_packet.push_back(_line_buffer[i]);
tnhnrl 46:030be9f5c793 209 }
tnhnrl 46:030be9f5c793 210
tnhnrl 46:030be9f5c793 211 //CRC CALCULATIONS BELOW
tnhnrl 46:030be9f5c793 212 //character version of calculation screws up on null character 0x00, scrapped and using vector of integers
tnhnrl 46:030be9f5c793 213
tnhnrl 46:030be9f5c793 214 int crc_one = calcCrcOne();
tnhnrl 46:030be9f5c793 215 int crc_two = calcCrcTwo();
tnhnrl 46:030be9f5c793 216
tnhnrl 46:030be9f5c793 217 //place the crc bytes into the data packet that is transmitted
tnhnrl 46:030be9f5c793 218 _data_packet.push_back(crc_one);
tnhnrl 46:030be9f5c793 219 _data_packet.push_back(crc_two);
tnhnrl 46:030be9f5c793 220 }
tnhnrl 46:030be9f5c793 221
tnhnrl 46:030be9f5c793 222 //should i save it as a string and transmit the string? next iteration maybe
tnhnrl 46:030be9f5c793 223
tnhnrl 46:030be9f5c793 224 void MbedLogger::transmitDataPacket() {
tnhnrl 46:030be9f5c793 225 //WRITE the data (in bytes) to the serial port
tnhnrl 46:030be9f5c793 226 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) {
tnhnrl 46:030be9f5c793 227 pc().putc(*_it); //send integers over serial port one byte at a time
tnhnrl 46:030be9f5c793 228 }
tnhnrl 46:030be9f5c793 229 }
tnhnrl 46:030be9f5c793 230
tnhnrl 46:030be9f5c793 231 // REACH ONE CHAR AT A TIME (EACH ITERATION OF STATE MACHINE...)
tnhnrl 46:030be9f5c793 232
tnhnrl 46:030be9f5c793 233 void MbedLogger::readTransmitPacketOneChar() {
tnhnrl 46:030be9f5c793 234 _file_transmission = true;
tnhnrl 46:030be9f5c793 235 _file_transmission_state = 0;
tnhnrl 46:030be9f5c793 236
tnhnrl 46:030be9f5c793 237 //first check if you're receiving data, then read four bytes
tnhnrl 46:030be9f5c793 238
tnhnrl 46:030be9f5c793 239 static int transmit_state = HEADER_117; //for state machine
tnhnrl 46:030be9f5c793 240
tnhnrl 46:030be9f5c793 241 int incoming_byte = -1;
tnhnrl 46:030be9f5c793 242
tnhnrl 46:030be9f5c793 243 int requested_packet_number = -1;
tnhnrl 46:030be9f5c793 244
tnhnrl 46:030be9f5c793 245 static int inside_while_loop = 1;
tnhnrl 46:030be9f5c793 246
tnhnrl 46:030be9f5c793 247 static char input_packet[4];
tnhnrl 46:030be9f5c793 248
tnhnrl 46:030be9f5c793 249 static int transmit_crc_one = 0;
tnhnrl 46:030be9f5c793 250 static int transmit_crc_two = 0;
tnhnrl 46:030be9f5c793 251
tnhnrl 46:030be9f5c793 252 static int random_packet_counter = 0;
tnhnrl 46:030be9f5c793 253
tnhnrl 46:030be9f5c793 254 if (pc().readable()) { //don't rely on pc readable being open all the time
tnhnrl 46:030be9f5c793 255 incoming_byte = pc().getc();
tnhnrl 46:030be9f5c793 256 led2() = !led2();
tnhnrl 46:030be9f5c793 257
tnhnrl 46:030be9f5c793 258 }
tnhnrl 46:030be9f5c793 259
tnhnrl 46:030be9f5c793 260 switch(transmit_state) {
tnhnrl 46:030be9f5c793 261
tnhnrl 46:030be9f5c793 262 case (HEADER_117):
tnhnrl 46:030be9f5c793 263 random_packet_counter = 0;
tnhnrl 46:030be9f5c793 264 //pc().printf("SC %x (%d)\n\r", incoming_byte, transmit_state);
tnhnrl 46:030be9f5c793 265 //continue processing
tnhnrl 46:030be9f5c793 266 if (incoming_byte == 30){ //1E
tnhnrl 46:030be9f5c793 267 //pc().printf("byte one \n\r");
tnhnrl 46:030be9f5c793 268 transmit_state = HEADER_101;
tnhnrl 46:030be9f5c793 269 led3() = !led3();
tnhnrl 46:030be9f5c793 270
tnhnrl 46:030be9f5c793 271 random_packet_counter = 0; //successful read
tnhnrl 46:030be9f5c793 272 }
tnhnrl 46:030be9f5c793 273 //end transmission
tnhnrl 46:030be9f5c793 274 else if (incoming_byte == 16) {
tnhnrl 46:030be9f5c793 275 transmit_state = END_TRANSMISSION;
tnhnrl 46:030be9f5c793 276 random_packet_counter = 0; //successful read
tnhnrl 46:030be9f5c793 277 }
tnhnrl 46:030be9f5c793 278 else {
tnhnrl 46:030be9f5c793 279 transmit_state = HEADER_117; //go back to checking the first packet
tnhnrl 46:030be9f5c793 280
tnhnrl 46:030be9f5c793 281 random_packet_counter++; //UNSUCCESSFUL READ
tnhnrl 46:030be9f5c793 282
tnhnrl 46:030be9f5c793 283 if (random_packet_counter > 10) {
tnhnrl 46:030be9f5c793 284 transmit_state = END_TRANSMISSION;
tnhnrl 46:030be9f5c793 285 }
tnhnrl 46:030be9f5c793 286 }
tnhnrl 46:030be9f5c793 287 break;
tnhnrl 46:030be9f5c793 288
tnhnrl 46:030be9f5c793 289 case (HEADER_101):
tnhnrl 46:030be9f5c793 290 if(incoming_byte == 31) { //1F
tnhnrl 46:030be9f5c793 291 //pc().printf("byte two \n\r");
tnhnrl 46:030be9f5c793 292 transmit_state = TRANSMIT_PACKET_1;
tnhnrl 46:030be9f5c793 293 led4() = !led4();
tnhnrl 46:030be9f5c793 294 }
tnhnrl 46:030be9f5c793 295 break;
tnhnrl 46:030be9f5c793 296
tnhnrl 46:030be9f5c793 297 case (TRANSMIT_PACKET_1):
tnhnrl 46:030be9f5c793 298 if (incoming_byte >= 0) {
tnhnrl 46:030be9f5c793 299 input_packet[0] = 30;
tnhnrl 46:030be9f5c793 300 input_packet[1] = 31;
tnhnrl 46:030be9f5c793 301 input_packet[2] = incoming_byte;
tnhnrl 46:030be9f5c793 302
tnhnrl 46:030be9f5c793 303 transmit_state = TRANSMIT_PACKET_2;
tnhnrl 46:030be9f5c793 304 _reply_byte3 = incoming_byte;
tnhnrl 46:030be9f5c793 305 //pc().printf("byte three \n\r");
tnhnrl 46:030be9f5c793 306 }
tnhnrl 46:030be9f5c793 307 break;
tnhnrl 46:030be9f5c793 308
tnhnrl 46:030be9f5c793 309 case (TRANSMIT_PACKET_2):
tnhnrl 46:030be9f5c793 310 if (incoming_byte >= 0) {
tnhnrl 46:030be9f5c793 311 input_packet[3] = incoming_byte;
tnhnrl 46:030be9f5c793 312 _reply_byte4 = incoming_byte;
tnhnrl 46:030be9f5c793 313 //pc().printf("byte four \n\r");
tnhnrl 46:030be9f5c793 314 }
tnhnrl 46:030be9f5c793 315 transmit_state = PACKET_CRC_ONE;
tnhnrl 46:030be9f5c793 316
tnhnrl 46:030be9f5c793 317 break;
tnhnrl 46:030be9f5c793 318
tnhnrl 46:030be9f5c793 319 case (PACKET_CRC_ONE):
tnhnrl 46:030be9f5c793 320 transmit_crc_one = calcCrcOneString(input_packet);
tnhnrl 46:030be9f5c793 321 transmit_state = PACKET_CRC_TWO;
tnhnrl 46:030be9f5c793 322 break;
tnhnrl 46:030be9f5c793 323
tnhnrl 46:030be9f5c793 324 case (PACKET_CRC_TWO):
tnhnrl 46:030be9f5c793 325 transmit_crc_two = calcCrcTwoString(input_packet);
tnhnrl 46:030be9f5c793 326 transmit_state = HEADER_117;
tnhnrl 46:030be9f5c793 327
tnhnrl 46:030be9f5c793 328
tnhnrl 46:030be9f5c793 329
tnhnrl 46:030be9f5c793 330 requested_packet_number = _reply_byte3 * 256 + _reply_byte4; //compute the numbers 0 through 65535 with the two bytes
tnhnrl 46:030be9f5c793 331
tnhnrl 46:030be9f5c793 332 //pc().printf("req packet number is %d\n\r", requested_packet_number);
tnhnrl 46:030be9f5c793 333
tnhnrl 46:030be9f5c793 334 if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE!
tnhnrl 46:030be9f5c793 335 //MUST SET THE PACKET NUMBER
tnhnrl 46:030be9f5c793 336 _packet_number = requested_packet_number;
tnhnrl 46:030be9f5c793 337
tnhnrl 46:030be9f5c793 338 // DO NOT READ IF THE CURRENT REQUEST IS PAST THE TOTAL # OF PACKETS (lines of the file)
tnhnrl 46:030be9f5c793 339 if (_packet_number <= _total_number_of_packets) {
tnhnrl 46:030be9f5c793 340 readPacketInSeries();
tnhnrl 46:030be9f5c793 341 createDataPacket();
tnhnrl 46:030be9f5c793 342 }
tnhnrl 46:030be9f5c793 343
tnhnrl 46:030be9f5c793 344 _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created
tnhnrl 46:030be9f5c793 345
tnhnrl 46:030be9f5c793 346 //pc().printf("DEBUG IF transmitDataPacket\n\r");
tnhnrl 46:030be9f5c793 347 transmitDataPacket(); //continuously transmit current packet until it tells you to do the next packet (response from Python program)
tnhnrl 46:030be9f5c793 348 }
tnhnrl 46:030be9f5c793 349
tnhnrl 46:030be9f5c793 350 else {
tnhnrl 46:030be9f5c793 351 //inside_while_loopc().printf("DEBUG: TRANSMIT SAME PACKET #%d\n\r", _packet_number);
tnhnrl 46:030be9f5c793 352 transmitDataPacket();
tnhnrl 46:030be9f5c793 353 //pc().printf("DEBUG ELSE transmitDataPacket\n\r");
tnhnrl 46:030be9f5c793 354 }
tnhnrl 46:030be9f5c793 355
tnhnrl 46:030be9f5c793 356 break;
tnhnrl 46:030be9f5c793 357
tnhnrl 46:030be9f5c793 358 case END_TRANSMISSION:
tnhnrl 46:030be9f5c793 359 _file_transmission_complete = true; //this triggers an exit on the state machine
tnhnrl 46:030be9f5c793 360 break;
tnhnrl 46:030be9f5c793 361 } //end of switch statement
tnhnrl 46:030be9f5c793 362
tnhnrl 46:030be9f5c793 363 //once you're outside of the while loop
tnhnrl 46:030be9f5c793 364 inside_while_loop = true; //for next iteration
tnhnrl 46:030be9f5c793 365
tnhnrl 46:030be9f5c793 366 /* pc().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r"); */
tnhnrl 46:030be9f5c793 367 }
tnhnrl 46:030be9f5c793 368
tnhnrl 46:030be9f5c793 369 int MbedLogger::readTransmitPacket() {
tnhnrl 46:030be9f5c793 370 _file_transmission = true;
tnhnrl 46:030be9f5c793 371 _file_transmission_state = 0;
tnhnrl 46:030be9f5c793 372
tnhnrl 46:030be9f5c793 373 //first check if you're receiving data, then read four bytes
tnhnrl 46:030be9f5c793 374
tnhnrl 46:030be9f5c793 375 int transmit_state = 0; //for state machine
tnhnrl 46:030be9f5c793 376
tnhnrl 46:030be9f5c793 377 int incoming_byte = -1;
tnhnrl 46:030be9f5c793 378
tnhnrl 46:030be9f5c793 379 int requested_packet_number = -1;
tnhnrl 46:030be9f5c793 380
tnhnrl 46:030be9f5c793 381 static int inside_while_loop = 1;
tnhnrl 46:030be9f5c793 382
tnhnrl 46:030be9f5c793 383 while (inside_while_loop) {
tnhnrl 46:030be9f5c793 384 if (pc().readable()) { //don't rely on pc readable being open all the time
tnhnrl 46:030be9f5c793 385
tnhnrl 46:030be9f5c793 386 incoming_byte = pc().getc();
tnhnrl 46:030be9f5c793 387
tnhnrl 46:030be9f5c793 388 switch(transmit_state) {
tnhnrl 46:030be9f5c793 389
tnhnrl 46:030be9f5c793 390 case (HEADER_117):
tnhnrl 46:030be9f5c793 391 //continue processing
tnhnrl 46:030be9f5c793 392 if (incoming_byte == 117){
tnhnrl 46:030be9f5c793 393 transmit_state = HEADER_101;
tnhnrl 46:030be9f5c793 394 }
tnhnrl 46:030be9f5c793 395 //end transmission
tnhnrl 46:030be9f5c793 396 else if (incoming_byte == 16) {
tnhnrl 46:030be9f5c793 397 transmit_state = END_TRANSMISSION;
tnhnrl 46:030be9f5c793 398 }
tnhnrl 46:030be9f5c793 399 else {
tnhnrl 46:030be9f5c793 400 transmit_state = HEADER_117;
tnhnrl 46:030be9f5c793 401 }
tnhnrl 46:030be9f5c793 402 break;
tnhnrl 46:030be9f5c793 403
tnhnrl 46:030be9f5c793 404 case (HEADER_101):
tnhnrl 46:030be9f5c793 405 if(incoming_byte == 101) {
tnhnrl 46:030be9f5c793 406 transmit_state = TRANSMIT_PACKET_1;
tnhnrl 46:030be9f5c793 407 }
tnhnrl 46:030be9f5c793 408 break;
tnhnrl 46:030be9f5c793 409
tnhnrl 46:030be9f5c793 410 case (TRANSMIT_PACKET_1):
tnhnrl 46:030be9f5c793 411 if (incoming_byte >= 0) {
tnhnrl 46:030be9f5c793 412 transmit_state = TRANSMIT_PACKET_2;
tnhnrl 46:030be9f5c793 413 _reply_byte3 = incoming_byte;
tnhnrl 46:030be9f5c793 414 }
tnhnrl 46:030be9f5c793 415 break;
tnhnrl 46:030be9f5c793 416
tnhnrl 46:030be9f5c793 417 case (TRANSMIT_PACKET_2):
tnhnrl 46:030be9f5c793 418 if (incoming_byte >= 0) {
tnhnrl 46:030be9f5c793 419 _reply_byte4 = incoming_byte;
tnhnrl 46:030be9f5c793 420 }
tnhnrl 46:030be9f5c793 421
tnhnrl 46:030be9f5c793 422 requested_packet_number = _reply_byte3 * 256 + _reply_byte4; //compute the numbers 0 through 65535 with the two bytes
tnhnrl 46:030be9f5c793 423
tnhnrl 46:030be9f5c793 424 if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE!
tnhnrl 46:030be9f5c793 425 //MUST SET THE PACKET NUMBER
tnhnrl 46:030be9f5c793 426 _packet_number = requested_packet_number;
tnhnrl 46:030be9f5c793 427
tnhnrl 46:030be9f5c793 428 readPacketInSeries();
tnhnrl 46:030be9f5c793 429 createDataPacket();
tnhnrl 46:030be9f5c793 430 _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created
tnhnrl 46:030be9f5c793 431
tnhnrl 46:030be9f5c793 432 transmitDataPacket(); //continuously transmit current packet until it tells you to do the next packet (response from Python program)
tnhnrl 46:030be9f5c793 433 }
tnhnrl 46:030be9f5c793 434
tnhnrl 46:030be9f5c793 435 else {
tnhnrl 46:030be9f5c793 436 //inside_while_loopc().printf("DEBUG: TRANSMIT SAME PACKET #%d\n\r", _packet_number);
tnhnrl 46:030be9f5c793 437 transmitDataPacket();
tnhnrl 46:030be9f5c793 438 }
tnhnrl 46:030be9f5c793 439
tnhnrl 46:030be9f5c793 440 //TRANSMIT_PACKET_2
tnhnrl 46:030be9f5c793 441 inside_while_loop = 0; //exit the while loop with this
tnhnrl 46:030be9f5c793 442 pc().printf("(TRANSMIT_PACKET_2)reached inside_while_loop = 0\n\r"); //DEBUG
tnhnrl 46:030be9f5c793 443 break;
tnhnrl 46:030be9f5c793 444 } //end of switch statement
tnhnrl 46:030be9f5c793 445 } //end of while loop
tnhnrl 46:030be9f5c793 446
tnhnrl 46:030be9f5c793 447 // else {
tnhnrl 46:030be9f5c793 448 // //pc().printf("pc not readable \n\r");
tnhnrl 46:030be9f5c793 449 // }
tnhnrl 46:030be9f5c793 450 //
tnhnrl 46:030be9f5c793 451 }
tnhnrl 46:030be9f5c793 452
tnhnrl 46:030be9f5c793 453 //once you're outside of the while loop
tnhnrl 46:030be9f5c793 454 inside_while_loop = true; //for next iteration
tnhnrl 46:030be9f5c793 455
tnhnrl 46:030be9f5c793 456 pc().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r");
tnhnrl 46:030be9f5c793 457 return false;
tnhnrl 46:030be9f5c793 458 }
tnhnrl 46:030be9f5c793 459
tnhnrl 46:030be9f5c793 460 void MbedLogger::reOpenLineReader() {
tnhnrl 46:030be9f5c793 461 //open a new one
tnhnrl 46:030be9f5c793 462 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 463
tnhnrl 46:030be9f5c793 464 _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read
tnhnrl 46:030be9f5c793 465
tnhnrl 46:030be9f5c793 466 //check if this actually worked...
tnhnrl 46:030be9f5c793 467 if (!_fp) {
tnhnrl 46:030be9f5c793 468 pc().printf("ERROR: Log file could not be opened\n\r");
tnhnrl 46:030be9f5c793 469 }
tnhnrl 46:030be9f5c793 470 else {
tnhnrl 46:030be9f5c793 471 pc().printf("Current Log file (LOG000.csv) was opened.\n\r");
tnhnrl 46:030be9f5c793 472 }
tnhnrl 46:030be9f5c793 473 }
tnhnrl 46:030be9f5c793 474
tnhnrl 46:030be9f5c793 475 bool MbedLogger::openLineReader() {
tnhnrl 46:030be9f5c793 476 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 477
tnhnrl 46:030be9f5c793 478 _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read
tnhnrl 46:030be9f5c793 479
tnhnrl 46:030be9f5c793 480 //check if this actually worked...
tnhnrl 46:030be9f5c793 481 if (!_fp) {
tnhnrl 46:030be9f5c793 482 //pc().printf("ERROR: Log file could not be opened\n\r");
tnhnrl 46:030be9f5c793 483 return false;
tnhnrl 46:030be9f5c793 484 }
tnhnrl 46:030be9f5c793 485 else {
tnhnrl 46:030be9f5c793 486 //pc().printf("Current Log file (LOG000.csv) was opened.\n\r");
tnhnrl 46:030be9f5c793 487 return true;
tnhnrl 46:030be9f5c793 488 }
tnhnrl 46:030be9f5c793 489 }
tnhnrl 46:030be9f5c793 490
tnhnrl 46:030be9f5c793 491 //VERIFIED THIS IS WORKING
tnhnrl 46:030be9f5c793 492 void MbedLogger::readPacketInSeries(){
tnhnrl 46:030be9f5c793 493 memset(&_line_buffer[0], 0, sizeof(_line_buffer)); //clear buffer each time, start at the address, fill with zeroes, go to the end of the char array
tnhnrl 46:030be9f5c793 494
tnhnrl 46:030be9f5c793 495 //pc().printf("readPacketInSeries pointer: %p\n\r", _fp);
tnhnrl 46:030be9f5c793 496
tnhnrl 46:030be9f5c793 497 //read the current line in the file
tnhnrl 46:030be9f5c793 498 fgets(_line_buffer, 256, _fp); //reads the line of characters until you reach a newline character
tnhnrl 46:030be9f5c793 499
tnhnrl 46:030be9f5c793 500 //RECORD THE STRING LENGTH
tnhnrl 46:030be9f5c793 501 _current_line_length = strlen(_line_buffer);
tnhnrl 46:030be9f5c793 502 //pc().printf("_current_line_length: %D\n\r", _current_line_length);
tnhnrl 46:030be9f5c793 503 }
tnhnrl 46:030be9f5c793 504
tnhnrl 46:030be9f5c793 505 void MbedLogger::getNumberOfPacketsInCurrentLog() {
tnhnrl 46:030be9f5c793 506 _file_transmission_complete = false; //not sure this is working correctly
tnhnrl 46:030be9f5c793 507 _packet_number = 0;
tnhnrl 46:030be9f5c793 508
tnhnrl 46:030be9f5c793 509 int ch;
tnhnrl 46:030be9f5c793 510
tnhnrl 46:030be9f5c793 511 _total_number_of_packets = 0; //clear this each time you read the file
tnhnrl 46:030be9f5c793 512
tnhnrl 46:030be9f5c793 513 //if this is null, use the default, else use the new file
tnhnrl 46:030be9f5c793 514
tnhnrl 46:030be9f5c793 515 if (_fp == NULL) {
tnhnrl 46:030be9f5c793 516 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 517
tnhnrl 46:030be9f5c793 518 _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read
tnhnrl 46:030be9f5c793 519 }
tnhnrl 46:030be9f5c793 520 else { //else, use the file that is already open...
tnhnrl 46:030be9f5c793 521 fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file
tnhnrl 46:030be9f5c793 522 }
tnhnrl 46:030be9f5c793 523
tnhnrl 46:030be9f5c793 524 while (EOF != (ch=getc(_fp))) {
tnhnrl 46:030be9f5c793 525 if ('\n' == ch)
tnhnrl 46:030be9f5c793 526 _total_number_of_packets++; // records the number of new lines to determine how many packets to send
tnhnrl 46:030be9f5c793 527 }
tnhnrl 46:030be9f5c793 528
tnhnrl 46:030be9f5c793 529 pc().printf("There are %d lines/packets in the file.\n\r", _total_number_of_packets);
tnhnrl 46:030be9f5c793 530
tnhnrl 46:030be9f5c793 531
tnhnrl 46:030be9f5c793 532 //move the FILE pointer back to the start
tnhnrl 46:030be9f5c793 533 fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file
tnhnrl 46:030be9f5c793 534
tnhnrl 46:030be9f5c793 535 _file_transmission = true; //preparing to transmit files from MBED to Python
tnhnrl 46:030be9f5c793 536 }
tnhnrl 46:030be9f5c793 537
tnhnrl 46:030be9f5c793 538 void MbedLogger::endTransmissionCloseFile() {
tnhnrl 46:030be9f5c793 539 // if the file pointer is null, the file was not opened in the first place
tnhnrl 46:030be9f5c793 540 if (!_fp) {
tnhnrl 46:030be9f5c793 541 pc().printf("\n endTransmissionCloseFile: FILE WAS NOT OPENED!\n\r");
tnhnrl 46:030be9f5c793 542 }
tnhnrl 46:030be9f5c793 543 else {
tnhnrl 46:030be9f5c793 544 pc().printf("\n endTransmissionCloseFile: FILE FOUND AND CLOSED!\n\r");
tnhnrl 46:030be9f5c793 545 closeLogFile();
tnhnrl 46:030be9f5c793 546 }
tnhnrl 46:030be9f5c793 547
tnhnrl 46:030be9f5c793 548 _file_transmission = false;
tnhnrl 46:030be9f5c793 549 }
tnhnrl 46:030be9f5c793 550
tnhnrl 46:030be9f5c793 551 void MbedLogger::openWriteFile() {
tnhnrl 46:030be9f5c793 552 pc().printf("Opening file for reception.\n\r");
tnhnrl 46:030be9f5c793 553
tnhnrl 46:030be9f5c793 554 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 555
tnhnrl 46:030be9f5c793 556 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 557 }
tnhnrl 46:030be9f5c793 558
tnhnrl 46:030be9f5c793 559 // function checks for incoming data (receiver function) from a Python program that transmits a file
tnhnrl 46:030be9f5c793 560 bool MbedLogger::checkForIncomingData() {
tnhnrl 46:030be9f5c793 561 int receive_packet_number;
tnhnrl 46:030be9f5c793 562 int receive_total_number_packets;
tnhnrl 46:030be9f5c793 563 int receive_packet_size;
tnhnrl 46:030be9f5c793 564
tnhnrl 46:030be9f5c793 565 bool data_transmission_complete = false;
tnhnrl 46:030be9f5c793 566
tnhnrl 46:030be9f5c793 567 int incoming_byte;
tnhnrl 46:030be9f5c793 568
tnhnrl 46:030be9f5c793 569 char char_buffer[256] = {}; //create empty buffer
tnhnrl 46:030be9f5c793 570
tnhnrl 46:030be9f5c793 571 //starting state
tnhnrl 46:030be9f5c793 572 int process_state = HEADER_117;
tnhnrl 46:030be9f5c793 573
tnhnrl 46:030be9f5c793 574 //variables for processing data below
tnhnrl 46:030be9f5c793 575 int checksum_one = -1;
tnhnrl 46:030be9f5c793 576 int checksum_two = -1;
tnhnrl 46:030be9f5c793 577
tnhnrl 46:030be9f5c793 578 int calculated_crc_one = -1 ;
tnhnrl 46:030be9f5c793 579 int calculated_crc_two = -1;
tnhnrl 46:030be9f5c793 580
tnhnrl 46:030be9f5c793 581 int i = 5;
tnhnrl 46:030be9f5c793 582 int serial_timeout = 0;
tnhnrl 46:030be9f5c793 583
tnhnrl 46:030be9f5c793 584 while (pc().readable() && !data_transmission_complete) {
tnhnrl 46:030be9f5c793 585 incoming_byte = pc().getc(); //getc returns an unsigned char cast to an int
tnhnrl 46:030be9f5c793 586 //pc().printf("DEBUG: State 0\n\r");
tnhnrl 46:030be9f5c793 587
tnhnrl 46:030be9f5c793 588 switch(process_state) {
tnhnrl 46:030be9f5c793 589 case HEADER_117:
tnhnrl 46:030be9f5c793 590 //continue processing
tnhnrl 46:030be9f5c793 591 if (incoming_byte == 117){
tnhnrl 46:030be9f5c793 592 process_state = HEADER_101;
tnhnrl 46:030be9f5c793 593 //pc().printf("DEBUG: Case 117\n\r");
tnhnrl 46:030be9f5c793 594 }
tnhnrl 46:030be9f5c793 595 //end transmission
tnhnrl 46:030be9f5c793 596 else if (incoming_byte == 16) {
tnhnrl 46:030be9f5c793 597 process_state = END_TRANSMISSION;
tnhnrl 46:030be9f5c793 598 pc().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r");
tnhnrl 46:030be9f5c793 599 }
tnhnrl 46:030be9f5c793 600 else {
tnhnrl 46:030be9f5c793 601 process_state = HEADER_117; // ???
tnhnrl 46:030be9f5c793 602 //pc().printf("DEBUG: State Header 117\n\r");
tnhnrl 46:030be9f5c793 603 }
tnhnrl 46:030be9f5c793 604 break;
tnhnrl 46:030be9f5c793 605
tnhnrl 46:030be9f5c793 606 case HEADER_101:
tnhnrl 46:030be9f5c793 607 if(incoming_byte == 101) {
tnhnrl 46:030be9f5c793 608 process_state = PACKET_NUM;
tnhnrl 46:030be9f5c793 609 //pc().printf("DEBUG: Case 101\n\r");
tnhnrl 46:030be9f5c793 610 }
tnhnrl 46:030be9f5c793 611 break;
tnhnrl 46:030be9f5c793 612
tnhnrl 46:030be9f5c793 613 case PACKET_NUM:
tnhnrl 46:030be9f5c793 614 receive_packet_number = incoming_byte;
tnhnrl 46:030be9f5c793 615 process_state = TOTAL_NUM_PACKETS;
tnhnrl 46:030be9f5c793 616 //pc().printf("DEBUG: Case PACKET_NUM\n\r");
tnhnrl 46:030be9f5c793 617 break;
tnhnrl 46:030be9f5c793 618
tnhnrl 46:030be9f5c793 619 case TOTAL_NUM_PACKETS:
tnhnrl 46:030be9f5c793 620 receive_total_number_packets = incoming_byte;
tnhnrl 46:030be9f5c793 621 process_state = PACKET_SIZE;
tnhnrl 46:030be9f5c793 622 //pc().printf("DEBUG: Case TOTAL_NUM_PACKETS\n\r");
tnhnrl 46:030be9f5c793 623 break;
tnhnrl 46:030be9f5c793 624
tnhnrl 46:030be9f5c793 625 case PACKET_SIZE:
tnhnrl 46:030be9f5c793 626 receive_packet_size = incoming_byte;
tnhnrl 46:030be9f5c793 627 //pc().printf("DEBUG: Case PACKET_SIZE\n\r");
tnhnrl 46:030be9f5c793 628
tnhnrl 46:030be9f5c793 629 //write the header stuff to it
tnhnrl 46:030be9f5c793 630 char_buffer[0] = 117;
tnhnrl 46:030be9f5c793 631 char_buffer[1] = 101;
tnhnrl 46:030be9f5c793 632 char_buffer[2] = receive_packet_number;
tnhnrl 46:030be9f5c793 633 char_buffer[3] = receive_total_number_packets;
tnhnrl 46:030be9f5c793 634 char_buffer[4] = receive_packet_size;
tnhnrl 46:030be9f5c793 635
tnhnrl 46:030be9f5c793 636 //pc().printf("DEBUG: after header\n\r");
tnhnrl 46:030be9f5c793 637
tnhnrl 46:030be9f5c793 638 //char temp_char = NULL; //TEST IDEA 1/12/2018
tnhnrl 46:030be9f5c793 639
tnhnrl 46:030be9f5c793 640 //process packet data
tnhnrl 46:030be9f5c793 641 //pc().printf("DEBUG: receive_packet_size %d\n\r", receive_packet_size);
tnhnrl 46:030be9f5c793 642 //append for larger data sizes, FF FF
tnhnrl 46:030be9f5c793 643
tnhnrl 46:030be9f5c793 644 // IF YOU GET AN INTERRUPTED DATA STREAM YOU NEED TO BREAK OUT OF THE LOOP
tnhnrl 46:030be9f5c793 645 i = 5;
tnhnrl 46:030be9f5c793 646 serial_timeout = 0;
tnhnrl 46:030be9f5c793 647
tnhnrl 46:030be9f5c793 648 while (true) {
tnhnrl 46:030be9f5c793 649 if (pc().readable()) {
tnhnrl 46:030be9f5c793 650 char_buffer[i] = pc().getc();
tnhnrl 46:030be9f5c793 651 i++;
tnhnrl 46:030be9f5c793 652
tnhnrl 46:030be9f5c793 653 serial_timeout = 0; //reset the timeout
tnhnrl 46:030be9f5c793 654
tnhnrl 46:030be9f5c793 655 //pc().printf("pc readable?\n\r");
tnhnrl 46:030be9f5c793 656 }
tnhnrl 46:030be9f5c793 657 else {
tnhnrl 46:030be9f5c793 658 serial_timeout++;
tnhnrl 46:030be9f5c793 659 //pc().printf("serial_timeout %d\n\r", serial_timeout);
tnhnrl 46:030be9f5c793 660 }
tnhnrl 46:030be9f5c793 661
tnhnrl 46:030be9f5c793 662 // FULL PACKET RECEIVED
tnhnrl 46:030be9f5c793 663 if (i >= receive_packet_size+5) { //cannot do this properly with a for loop
tnhnrl 46:030be9f5c793 664 //pc().printf("i is %d\n\r", i);
tnhnrl 46:030be9f5c793 665
tnhnrl 46:030be9f5c793 666 checksum_one = pc().getc();
tnhnrl 46:030be9f5c793 667 checksum_two = pc().getc();
tnhnrl 46:030be9f5c793 668
tnhnrl 46:030be9f5c793 669 calculated_crc_one = calcCrcOneString(char_buffer);
tnhnrl 46:030be9f5c793 670 calculated_crc_two = calcCrcTwoString(char_buffer);
tnhnrl 46:030be9f5c793 671
tnhnrl 46:030be9f5c793 672 //if both checksums work...
tnhnrl 46:030be9f5c793 673 if ((calculated_crc_one == checksum_one) and (calculated_crc_two == checksum_two)) {
tnhnrl 46:030be9f5c793 674 //print a subset of the character array by starting at 5 (after header)
tnhnrl 46:030be9f5c793 675 fprintf(_fp, "%s", char_buffer+5);
tnhnrl 46:030be9f5c793 676
tnhnrl 46:030be9f5c793 677 _confirmed_packet_number = receive_packet_number;
tnhnrl 46:030be9f5c793 678 }
tnhnrl 46:030be9f5c793 679
tnhnrl 46:030be9f5c793 680 process_state = HEADER_117;
tnhnrl 46:030be9f5c793 681
tnhnrl 46:030be9f5c793 682 break;
tnhnrl 46:030be9f5c793 683 }
tnhnrl 46:030be9f5c793 684
tnhnrl 46:030be9f5c793 685 if (serial_timeout >= 10000) {
tnhnrl 46:030be9f5c793 686 //pc().printf("break serial_timeout %d\n\r", serial_timeout);
tnhnrl 46:030be9f5c793 687 break;
tnhnrl 46:030be9f5c793 688 }
tnhnrl 46:030be9f5c793 689 }
tnhnrl 46:030be9f5c793 690 break;
tnhnrl 46:030be9f5c793 691
tnhnrl 46:030be9f5c793 692 case END_TRANSMISSION:
tnhnrl 46:030be9f5c793 693 if (pc().getc() == 16) {
tnhnrl 46:030be9f5c793 694 pc().printf("TROY, you're here. 1. \n\r");
tnhnrl 46:030be9f5c793 695
tnhnrl 46:030be9f5c793 696 if (pc().getc() == 16) {
tnhnrl 46:030be9f5c793 697 pc().printf("TROY, you're here. 2. \n\r");
tnhnrl 46:030be9f5c793 698
tnhnrl 46:030be9f5c793 699 endReceiveData();
tnhnrl 46:030be9f5c793 700 }
tnhnrl 46:030be9f5c793 701 }
tnhnrl 46:030be9f5c793 702
tnhnrl 46:030be9f5c793 703 pc().printf("TROY, you're here. 5. \n\r");
tnhnrl 46:030be9f5c793 704
tnhnrl 46:030be9f5c793 705 //process_state = HEADER_117; //don't do this unless the check is wrong
tnhnrl 46:030be9f5c793 706 pc().printf("END_TRANSMISSION process_state is %d\n\r", process_state); //should be 5 (debug) 02/06/2018
tnhnrl 46:030be9f5c793 707 data_transmission_complete = true;
tnhnrl 46:030be9f5c793 708 break;
tnhnrl 46:030be9f5c793 709 }//END OF SWITCH
tnhnrl 46:030be9f5c793 710
tnhnrl 46:030be9f5c793 711 if (data_transmission_complete) {
tnhnrl 46:030be9f5c793 712 pc().printf("if statement data_transmission_complete ??? \n\r");
tnhnrl 46:030be9f5c793 713 break; //out of while loop
tnhnrl 46:030be9f5c793 714 }
tnhnrl 46:030be9f5c793 715 } // while loop
tnhnrl 46:030be9f5c793 716
tnhnrl 46:030be9f5c793 717 //pc().printf("checkForIncomingData done???\n\r"); //debug
tnhnrl 46:030be9f5c793 718
tnhnrl 46:030be9f5c793 719 if (data_transmission_complete)
tnhnrl 46:030be9f5c793 720 return false; //tell state machine class that this is done (not transmitting, false)
tnhnrl 46:030be9f5c793 721 else {
tnhnrl 46:030be9f5c793 722 return true;
tnhnrl 46:030be9f5c793 723 }
tnhnrl 46:030be9f5c793 724 }
tnhnrl 46:030be9f5c793 725
tnhnrl 46:030be9f5c793 726 int MbedLogger::sendReply() {
tnhnrl 46:030be9f5c793 727 //being explicit in what's being transmitted
tnhnrl 46:030be9f5c793 728
tnhnrl 46:030be9f5c793 729 //change this method to be more explicit later
tnhnrl 46:030be9f5c793 730
tnhnrl 46:030be9f5c793 731 //integer vector _data_packet is used here, cleared fist just in case
tnhnrl 46:030be9f5c793 732
tnhnrl 46:030be9f5c793 733 _data_packet.clear(); //same data packet for transmission
tnhnrl 46:030be9f5c793 734 _data_packet.push_back(117);
tnhnrl 46:030be9f5c793 735 _data_packet.push_back(101);
tnhnrl 46:030be9f5c793 736
tnhnrl 46:030be9f5c793 737 //_confirmed_packet_number comes from the packet number that is sent from the Python program
tnhnrl 46:030be9f5c793 738 _data_packet.push_back(_confirmed_packet_number / 256); //packet number only changed when confirmed
tnhnrl 46:030be9f5c793 739 _data_packet.push_back(_confirmed_packet_number % 256); //split into first and second byte
tnhnrl 46:030be9f5c793 740
tnhnrl 46:030be9f5c793 741 //compute checksums
tnhnrl 46:030be9f5c793 742
tnhnrl 46:030be9f5c793 743 int receiver_crc_one = calcCrcOne();
tnhnrl 46:030be9f5c793 744 int receiver_crc_two = calcCrcTwo();
tnhnrl 46:030be9f5c793 745
tnhnrl 46:030be9f5c793 746 _data_packet.push_back(receiver_crc_one);
tnhnrl 46:030be9f5c793 747 _data_packet.push_back(receiver_crc_two);
tnhnrl 46:030be9f5c793 748
tnhnrl 46:030be9f5c793 749 //transmit this packet
tnhnrl 46:030be9f5c793 750 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) {
tnhnrl 46:030be9f5c793 751 pc().putc(*_it); //send integers over serial port one byte at a time
tnhnrl 46:030be9f5c793 752 }
tnhnrl 46:030be9f5c793 753
tnhnrl 46:030be9f5c793 754 //change process methodology later...
tnhnrl 46:030be9f5c793 755
tnhnrl 46:030be9f5c793 756 return _confirmed_packet_number;
tnhnrl 46:030be9f5c793 757 }
tnhnrl 46:030be9f5c793 758
tnhnrl 46:030be9f5c793 759 //void MbedLogger::sendReplyPacketNumber(int packet_number) {
tnhnrl 46:030be9f5c793 760 // //being explicit in what's being transmitted
tnhnrl 46:030be9f5c793 761 // pc().putc(117);
tnhnrl 46:030be9f5c793 762 // pc().putc(101);
tnhnrl 46:030be9f5c793 763 // pc().putc(packet_number / 256); //this removes the trailing byte
tnhnrl 46:030be9f5c793 764 // pc().putc(packet_number % 256); //remainder gives you second byte (removes first byte)
tnhnrl 46:030be9f5c793 765 // pc().putc(packet_number);
tnhnrl 46:030be9f5c793 766 //
tnhnrl 46:030be9f5c793 767 // //not sure a checksum is needed here, I'll test behavior
tnhnrl 46:030be9f5c793 768 // //example is a reply of 75 65 03 03 03 (telling Python program you received packet # 3)
tnhnrl 46:030be9f5c793 769 //}
tnhnrl 46:030be9f5c793 770
tnhnrl 46:030be9f5c793 771 //DLE character * 4 ==> 10 10 10 10
tnhnrl 46:030be9f5c793 772 void MbedLogger::endReceiveData() {
tnhnrl 46:030be9f5c793 773 closeLogFile(); //close the file here
tnhnrl 46:030be9f5c793 774 pc().printf("endReceiveData closed the file and ended transmission\n\r");
tnhnrl 46:030be9f5c793 775 }
tnhnrl 46:030be9f5c793 776
tnhnrl 46:030be9f5c793 777 int MbedLogger::calcCrcOne() {
tnhnrl 46:030be9f5c793 778 //can't initialize the table in the constructor in c++
tnhnrl 46:030be9f5c793 779 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 46:030be9f5c793 780
tnhnrl 46:030be9f5c793 781 int crc = 0;
tnhnrl 46:030be9f5c793 782 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++)
tnhnrl 46:030be9f5c793 783 crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 46:030be9f5c793 784
tnhnrl 46:030be9f5c793 785 return crc / 256; //second-to-last byte
tnhnrl 46:030be9f5c793 786 }
tnhnrl 46:030be9f5c793 787
tnhnrl 46:030be9f5c793 788 int MbedLogger::calcCrcTwo() {
tnhnrl 46:030be9f5c793 789 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 46:030be9f5c793 790
tnhnrl 46:030be9f5c793 791 int crc = 0;
tnhnrl 46:030be9f5c793 792 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++)
tnhnrl 46:030be9f5c793 793 crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 46:030be9f5c793 794
tnhnrl 46:030be9f5c793 795 //pc().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_array.length(), crc % 256);
tnhnrl 46:030be9f5c793 796
tnhnrl 46:030be9f5c793 797 return crc % 256; //last byte
tnhnrl 46:030be9f5c793 798 }
tnhnrl 46:030be9f5c793 799
tnhnrl 46:030be9f5c793 800 void MbedLogger::resetReplyPacket() {
tnhnrl 46:030be9f5c793 801 _confirmed_packet_number = 0;
tnhnrl 46:030be9f5c793 802 }
tnhnrl 46:030be9f5c793 803
tnhnrl 46:030be9f5c793 804 void MbedLogger::openNewMissionFile() {
tnhnrl 46:030be9f5c793 805 pc().printf("Opening Mission file (sequence.txt) for reception.\n\r");
tnhnrl 46:030be9f5c793 806 string file_name_string = _file_system_string + "sequence.txt";
tnhnrl 46:030be9f5c793 807
tnhnrl 46:030be9f5c793 808 pc().printf("openNewMissionFile: %s\n\r", file_name_string.c_str());
tnhnrl 46:030be9f5c793 809
tnhnrl 46:030be9f5c793 810 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 811 }
tnhnrl 46:030be9f5c793 812
tnhnrl 46:030be9f5c793 813 void MbedLogger::setDataCounter(int input_counter) {
tnhnrl 46:030be9f5c793 814 _transmit_counter = input_counter;
tnhnrl 46:030be9f5c793 815 }
tnhnrl 46:030be9f5c793 816
tnhnrl 46:030be9f5c793 817 void MbedLogger::closeIncompleteFile() {
tnhnrl 46:030be9f5c793 818 fprintf(_fp, "TRANSMISSION INTERRUPTED!"); //write this warning to the file
tnhnrl 46:030be9f5c793 819 closeLogFile(); //close file
tnhnrl 46:030be9f5c793 820 }
tnhnrl 46:030be9f5c793 821
tnhnrl 46:030be9f5c793 822 void MbedLogger::appendLogFile(int current_state, int option) {
tnhnrl 46:030be9f5c793 823 //option one means write to file
tnhnrl 46:030be9f5c793 824
tnhnrl 46:030be9f5c793 825 if (option == 1) {
tnhnrl 46:030be9f5c793 826 if (!_fp) { //if not present
tnhnrl 46:030be9f5c793 827 _fp = fopen(_full_file_path_string.c_str(), "a");
tnhnrl 46:030be9f5c793 828 }
tnhnrl 46:030be9f5c793 829
tnhnrl 46:030be9f5c793 830 //record data using the recordData function (takes in the state integer)
tnhnrl 46:030be9f5c793 831 recordData(current_state);
tnhnrl 46:030be9f5c793 832 }
tnhnrl 46:030be9f5c793 833
tnhnrl 46:030be9f5c793 834 else {
tnhnrl 46:030be9f5c793 835 closeLogFile();
tnhnrl 46:030be9f5c793 836 }
tnhnrl 46:030be9f5c793 837 }
tnhnrl 46:030be9f5c793 838
tnhnrl 47:fb3c7929d3f3 839 // initialize and close the file
tnhnrl 48:20e681885161 840 // log file freezes at 0x0000006c
tnhnrl 46:030be9f5c793 841 void MbedLogger::initializeLogFile() {
tnhnrl 46:030be9f5c793 842 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 48:20e681885161 843 pc().printf("%s file system init\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 844
tnhnrl 46:030be9f5c793 845 //try to open this file...
tnhnrl 46:030be9f5c793 846 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 46:030be9f5c793 847
tnhnrl 46:030be9f5c793 848 //if the file is empty, create this.
tnhnrl 46:030be9f5c793 849 if (!_fp) {
tnhnrl 46:030be9f5c793 850 _fp = fopen(file_name_string.c_str(), "w"); //write,print,close
tnhnrl 46:030be9f5c793 851 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");
tnhnrl 46:030be9f5c793 852 closeLogFile();
tnhnrl 46:030be9f5c793 853 }
tnhnrl 46:030be9f5c793 854 else
tnhnrl 46:030be9f5c793 855 closeLogFile(); //close the opened read file
tnhnrl 46:030be9f5c793 856 }
tnhnrl 46:030be9f5c793 857
tnhnrl 46:030be9f5c793 858 int MbedLogger::fileTransmitState() {
tnhnrl 46:030be9f5c793 859 return _file_transmission_state;
tnhnrl 46:030be9f5c793 860 }
tnhnrl 46:030be9f5c793 861
tnhnrl 46:030be9f5c793 862 int MbedLogger::calcCrcOneString (string input_string) {
tnhnrl 46:030be9f5c793 863 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 46:030be9f5c793 864
tnhnrl 46:030be9f5c793 865 int crc = 0;
tnhnrl 46:030be9f5c793 866 for (unsigned int i = 0; i < input_string.length(); i++) {
tnhnrl 46:030be9f5c793 867 //convert each character to an integer
tnhnrl 46:030be9f5c793 868 int character_to_integer = input_string[i]; //correct
tnhnrl 46:030be9f5c793 869
tnhnrl 46:030be9f5c793 870 crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 46:030be9f5c793 871 }
tnhnrl 46:030be9f5c793 872
tnhnrl 46:030be9f5c793 873 //pc().printf("DEBUG: calcCrcOne string length: %d crc: %d\n\r", input_string.length(), crc/256);
tnhnrl 46:030be9f5c793 874
tnhnrl 46:030be9f5c793 875 return crc / 256; //second-to-last byte
tnhnrl 46:030be9f5c793 876 }
tnhnrl 46:030be9f5c793 877
tnhnrl 46:030be9f5c793 878 int MbedLogger::calcCrcTwoString (string input_string) {
tnhnrl 46:030be9f5c793 879 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 46:030be9f5c793 880
tnhnrl 46:030be9f5c793 881 int crc = 0;
tnhnrl 46:030be9f5c793 882 for (unsigned int i = 0; i < input_string.length(); i++) {
tnhnrl 46:030be9f5c793 883 //convert each character to an integer
tnhnrl 46:030be9f5c793 884 int character_to_integer = input_string[i]; //correct
tnhnrl 46:030be9f5c793 885
tnhnrl 46:030be9f5c793 886 crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 46:030be9f5c793 887 }
tnhnrl 46:030be9f5c793 888
tnhnrl 46:030be9f5c793 889 //pc().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_string.length(), crc % 256);
tnhnrl 46:030be9f5c793 890
tnhnrl 46:030be9f5c793 891 return crc % 256; //last byte
tnhnrl 46:030be9f5c793 892 }
tnhnrl 46:030be9f5c793 893
tnhnrl 46:030be9f5c793 894 int MbedLogger::testGetFileSize() {
tnhnrl 46:030be9f5c793 895 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 896
tnhnrl 46:030be9f5c793 897 _fp = fopen(file_name_string.c_str(), "rb"); //open the file for reading as a binary file
tnhnrl 46:030be9f5c793 898
tnhnrl 46:030be9f5c793 899 fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file)
tnhnrl 46:030be9f5c793 900 unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file.
tnhnrl 46:030be9f5c793 901 fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary
tnhnrl 46:030be9f5c793 902
tnhnrl 46:030be9f5c793 903 closeLogFile(); //can probably just close the file pointer and not worry about position
tnhnrl 46:030be9f5c793 904
tnhnrl 46:030be9f5c793 905 pc().printf("(%s) LOG000.csv file size is %d\n\r", _file_system_string.c_str(), file_size);
tnhnrl 46:030be9f5c793 906
tnhnrl 46:030be9f5c793 907 if (file_size == 0)
tnhnrl 46:030be9f5c793 908 createEmptyLog(); //this is not actually a completely empty log
tnhnrl 46:030be9f5c793 909
tnhnrl 46:030be9f5c793 910 return file_size;
tnhnrl 46:030be9f5c793 911
tnhnrl 46:030be9f5c793 912 // http://www.cplusplus.com/reference/cstdio/ftell/
tnhnrl 46:030be9f5c793 913 // https://os.mbed.com/questions/1260/How-to-read-the-LocalFileSystem-filesize/
tnhnrl 46:030be9f5c793 914 }
tnhnrl 46:030be9f5c793 915
tnhnrl 46:030be9f5c793 916 void MbedLogger::createEmptyLog() {
tnhnrl 46:030be9f5c793 917 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 918
tnhnrl 46:030be9f5c793 919 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 920 fprintf(_fp, "EMPTY LOG\n"); //just write this string to the log (processing needs a file size that is not zero)
tnhnrl 46:030be9f5c793 921 closeLogFile();
tnhnrl 46:030be9f5c793 922 }
tnhnrl 46:030be9f5c793 923
tnhnrl 46:030be9f5c793 924 int MbedLogger::getFileSize(string filename) {
tnhnrl 46:030be9f5c793 925 // fixed the const char * errror:
tnhnrl 46:030be9f5c793 926 // https://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char
tnhnrl 46:030be9f5c793 927 const char * char_filename = filename.c_str(); // Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
tnhnrl 46:030be9f5c793 928 //http://www.cplusplus.com/reference/string/string/c_str/
tnhnrl 46:030be9f5c793 929
tnhnrl 46:030be9f5c793 930 _fp = fopen(filename.c_str(), "rb"); //open the file for reading as a binary file
tnhnrl 46:030be9f5c793 931
tnhnrl 46:030be9f5c793 932 fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file)
tnhnrl 46:030be9f5c793 933 unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file.
tnhnrl 46:030be9f5c793 934 fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary
tnhnrl 46:030be9f5c793 935
tnhnrl 46:030be9f5c793 936 closeLogFile(); //can probably just close the file pointer and not worry about position
tnhnrl 46:030be9f5c793 937
tnhnrl 46:030be9f5c793 938 pc().printf("%s file size is %d\n\r", filename.c_str(), file_size);
tnhnrl 46:030be9f5c793 939
tnhnrl 46:030be9f5c793 940 return file_size;
tnhnrl 46:030be9f5c793 941 }
tnhnrl 46:030be9f5c793 942
tnhnrl 46:030be9f5c793 943 int MbedLogger::debugFileState() {
tnhnrl 46:030be9f5c793 944 pc().printf("What is _fp right now? %p\n\r", _fp); //pointer notation
tnhnrl 46:030be9f5c793 945
tnhnrl 46:030be9f5c793 946 if (_fp)
tnhnrl 46:030be9f5c793 947 return 1; //file pointer does exist
tnhnrl 46:030be9f5c793 948 else
tnhnrl 46:030be9f5c793 949 return 0; //file pointer does not exist
tnhnrl 46:030be9f5c793 950 }
tnhnrl 46:030be9f5c793 951
tnhnrl 46:030be9f5c793 952 void MbedLogger::activateTransmitPacket() {
tnhnrl 46:030be9f5c793 953 _mbed_transmit_loop = true;
tnhnrl 46:030be9f5c793 954 }
tnhnrl 46:030be9f5c793 955
tnhnrl 46:030be9f5c793 956 void MbedLogger::transmitDataWithTicker() {
tnhnrl 46:030be9f5c793 957 getNumberOfPacketsInCurrentLog();
tnhnrl 46:030be9f5c793 958
tnhnrl 46:030be9f5c793 959 //activate ticker
tnhnrl 46:030be9f5c793 960 _mbed_transmit_ticker.attach(callback(this, &MbedLogger::activateTransmitPacket), 0.1); // 10 hz ticker
tnhnrl 46:030be9f5c793 961
tnhnrl 46:030be9f5c793 962 int transmit_data_counter = 0; //used for timeout
tnhnrl 46:030be9f5c793 963
tnhnrl 46:030be9f5c793 964 //transmit data with ticker until complete or times out
tnhnrl 46:030be9f5c793 965 while(1) {
tnhnrl 46:030be9f5c793 966 //runs at 10 hz with ticker
tnhnrl 46:030be9f5c793 967 if (_mbed_transmit_loop) {
tnhnrl 46:030be9f5c793 968 led4() = !led4(); //flash LED 4
tnhnrl 46:030be9f5c793 969
tnhnrl 46:030be9f5c793 970 // transmit_data_counter = checkForTransmitPacket(); // run this until it finishes (function returns an integer)
tnhnrl 46:030be9f5c793 971 _mbed_transmit_loop = false; // wait until the loop rate timer fires again (TICKER)
tnhnrl 46:030be9f5c793 972
tnhnrl 46:030be9f5c793 973 if (transmit_data_counter >= 20) { //was 50
tnhnrl 46:030be9f5c793 974 _file_transmission = false; //file transmission complete
tnhnrl 46:030be9f5c793 975 }
tnhnrl 46:030be9f5c793 976
tnhnrl 46:030be9f5c793 977 if (!_file_transmission) {
tnhnrl 46:030be9f5c793 978
tnhnrl 46:030be9f5c793 979 pc().printf("\n\r(_file_transmission == false) Stopped transmitting?\n\r");
tnhnrl 46:030be9f5c793 980
tnhnrl 46:030be9f5c793 981 setDataCounter(0); //reset the counter in the mbed logger
tnhnrl 46:030be9f5c793 982
tnhnrl 46:030be9f5c793 983 _mbed_transmit_ticker.detach();
tnhnrl 46:030be9f5c793 984
tnhnrl 46:030be9f5c793 985 break; //break out of the while loop when transmission over
tnhnrl 46:030be9f5c793 986 }
tnhnrl 46:030be9f5c793 987 }
tnhnrl 46:030be9f5c793 988 } //end of while loop
tnhnrl 46:030be9f5c793 989 }
tnhnrl 46:030be9f5c793 990
tnhnrl 46:030be9f5c793 991 void MbedLogger::specifyFileForTransmit(string input_string) {
tnhnrl 46:030be9f5c793 992 pc().printf("specifyFileForTransmit\n\r");
tnhnrl 46:030be9f5c793 993
tnhnrl 46:030be9f5c793 994 string file_string = _file_system_string + input_string;
tnhnrl 46:030be9f5c793 995
tnhnrl 46:030be9f5c793 996 pc().printf("file_string is <%s>\n\r", file_string.c_str());
tnhnrl 46:030be9f5c793 997
tnhnrl 46:030be9f5c793 998 //open this file to read
tnhnrl 46:030be9f5c793 999 _fp = fopen(file_string.c_str(), "r");
tnhnrl 46:030be9f5c793 1000
tnhnrl 46:030be9f5c793 1001 //transmit that file
tnhnrl 46:030be9f5c793 1002 transmitDataWithTicker();
tnhnrl 46:030be9f5c793 1003
tnhnrl 46:030be9f5c793 1004 _file_transmission = true;
tnhnrl 46:030be9f5c793 1005 }
tnhnrl 46:030be9f5c793 1006
tnhnrl 46:030be9f5c793 1007 void MbedLogger::transmitFileFromDirectory( int file_number ) {
tnhnrl 46:030be9f5c793 1008 file_number = file_number + 1; //to get the correct number
tnhnrl 46:030be9f5c793 1009
tnhnrl 46:030be9f5c793 1010 DIR *dir;
tnhnrl 46:030be9f5c793 1011 struct dirent *dp; //dirent.h is the format of directory entries
tnhnrl 46:030be9f5c793 1012 int log_found =0, loop=1; //start file numbers at 1
tnhnrl 46:030be9f5c793 1013 long int temp=0;
tnhnrl 46:030be9f5c793 1014
tnhnrl 46:030be9f5c793 1015 // char * char_pointer;
tnhnrl 46:030be9f5c793 1016 // char * numstart, *numstop;
tnhnrl 46:030be9f5c793 1017
tnhnrl 46:030be9f5c793 1018 if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) {
tnhnrl 46:030be9f5c793 1019 pc().printf("MBED directory could not be opened\r\n");
tnhnrl 46:030be9f5c793 1020 }
tnhnrl 46:030be9f5c793 1021 else
tnhnrl 46:030be9f5c793 1022 {
tnhnrl 46:030be9f5c793 1023 while ( NULL != (dp = readdir( dir )) )
tnhnrl 46:030be9f5c793 1024 {
tnhnrl 46:030be9f5c793 1025 pc().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp);
tnhnrl 46:030be9f5c793 1026
tnhnrl 46:030be9f5c793 1027 //process current file if it matches the file number
tnhnrl 46:030be9f5c793 1028 if (file_number == loop) {
tnhnrl 46:030be9f5c793 1029 char * current_file_name = dp->d_name; //pointer to this char array
tnhnrl 46:030be9f5c793 1030
tnhnrl 46:030be9f5c793 1031 specifyFileForTransmit(current_file_name);
tnhnrl 46:030be9f5c793 1032
tnhnrl 46:030be9f5c793 1033 break;
tnhnrl 46:030be9f5c793 1034 }
tnhnrl 46:030be9f5c793 1035
tnhnrl 46:030be9f5c793 1036 loop++;
tnhnrl 46:030be9f5c793 1037 }
tnhnrl 46:030be9f5c793 1038 }
tnhnrl 46:030be9f5c793 1039 }
tnhnrl 46:030be9f5c793 1040
tnhnrl 46:030be9f5c793 1041 void MbedLogger::accessMbedDirectory() {
tnhnrl 46:030be9f5c793 1042 printMbedDirectory();
tnhnrl 46:030be9f5c793 1043
tnhnrl 46:030be9f5c793 1044 pc().printf("Type in the number of the file you want to transmit.\n\r");
tnhnrl 46:030be9f5c793 1045
tnhnrl 46:030be9f5c793 1046 char message[42];
tnhnrl 46:030be9f5c793 1047
tnhnrl 46:030be9f5c793 1048 pc().scanf("%41s", message);
tnhnrl 46:030be9f5c793 1049
tnhnrl 46:030be9f5c793 1050 pc().printf("Input received!\n\r");
tnhnrl 46:030be9f5c793 1051
tnhnrl 46:030be9f5c793 1052 //check if char array is an integer
tnhnrl 46:030be9f5c793 1053 char* conversion_pointer;
tnhnrl 46:030be9f5c793 1054 long converted = strtol(message, &conversion_pointer, 10);
tnhnrl 46:030be9f5c793 1055
tnhnrl 46:030be9f5c793 1056 if (*conversion_pointer) {
tnhnrl 46:030be9f5c793 1057 //conversion failed because the input was not a number
tnhnrl 46:030be9f5c793 1058 pc().printf("NOT A VALID FILE NUMBER!\n\r");
tnhnrl 46:030be9f5c793 1059 }
tnhnrl 46:030be9f5c793 1060 else {
tnhnrl 46:030be9f5c793 1061 //conversion worked!
tnhnrl 46:030be9f5c793 1062 pc().printf("You chose file number: %d\n\r", converted);
tnhnrl 46:030be9f5c793 1063
tnhnrl 46:030be9f5c793 1064 // transmit the file
tnhnrl 46:030be9f5c793 1065 transmitFileFromDirectory(converted);
tnhnrl 46:030be9f5c793 1066 }
tnhnrl 46:030be9f5c793 1067 }
tnhnrl 46:030be9f5c793 1068
tnhnrl 46:030be9f5c793 1069 void MbedLogger::closeLogFile() {
tnhnrl 46:030be9f5c793 1070 if (_fp == NULL){
tnhnrl 46:030be9f5c793 1071 pc().printf("MbedLogger: (%s) LOG FILE WAS ALREADY CLOSED!\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 1072 }
tnhnrl 46:030be9f5c793 1073
tnhnrl 46:030be9f5c793 1074 else {
tnhnrl 46:030be9f5c793 1075 pc().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 1076
tnhnrl 46:030be9f5c793 1077 //close file
tnhnrl 46:030be9f5c793 1078 fclose(_fp);
tnhnrl 46:030be9f5c793 1079
tnhnrl 46:030be9f5c793 1080 _fp = NULL; //set pointer to zero
tnhnrl 46:030be9f5c793 1081 }
tnhnrl 46:030be9f5c793 1082 }
tnhnrl 46:030be9f5c793 1083
tnhnrl 46:030be9f5c793 1084 void MbedLogger::activateReceivePacket() {
tnhnrl 46:030be9f5c793 1085 _mbed_receive_loop = true;
tnhnrl 46:030be9f5c793 1086 }
tnhnrl 46:030be9f5c793 1087
tnhnrl 46:030be9f5c793 1088 void MbedLogger::receiveMissionDataWithTicker() {
tnhnrl 46:030be9f5c793 1089 openNewMissionFile(); //sequence.txt file opened
tnhnrl 46:030be9f5c793 1090
tnhnrl 46:030be9f5c793 1091 _mbed_receive_ticker.attach(callback(this, &MbedLogger::activateReceivePacket), 0.1);
tnhnrl 46:030be9f5c793 1092
tnhnrl 46:030be9f5c793 1093 pc().printf("\n\r02/09/2018 MbedLogger receiveMissionData Beginning to receive sequence data...\n\r");
tnhnrl 46:030be9f5c793 1094
tnhnrl 46:030be9f5c793 1095 resetReplyPacket(); //reset the reply packet
tnhnrl 46:030be9f5c793 1096
tnhnrl 46:030be9f5c793 1097 //idea for stopping this if data not being received
tnhnrl 46:030be9f5c793 1098 int current_packet_number = 0;
tnhnrl 46:030be9f5c793 1099 int last_packet_number = -1;
tnhnrl 46:030be9f5c793 1100 int break_transmission = 0;
tnhnrl 46:030be9f5c793 1101
tnhnrl 46:030be9f5c793 1102 while(1) {
tnhnrl 46:030be9f5c793 1103 //runs at 10 hz
tnhnrl 46:030be9f5c793 1104 if (_mbed_receive_loop) {
tnhnrl 46:030be9f5c793 1105 if (!checkForIncomingData()) { // run this until it finishes
tnhnrl 46:030be9f5c793 1106 //when you complete data reception, this will become false
tnhnrl 46:030be9f5c793 1107 pc().printf("\n\rMbedLogger: Data RECEPTION complete.\n\r");
tnhnrl 46:030be9f5c793 1108 _mbed_receive_ticker.detach();
tnhnrl 46:030be9f5c793 1109 break;
tnhnrl 46:030be9f5c793 1110 }
tnhnrl 46:030be9f5c793 1111 else {
tnhnrl 46:030be9f5c793 1112 //check if you keep getting the same thing
tnhnrl 46:030be9f5c793 1113 current_packet_number = sendReply();
tnhnrl 46:030be9f5c793 1114
tnhnrl 46:030be9f5c793 1115 //pc().printf("DEBUG: current packet number %d (last packet number %d) \n\r", current_packet_number, last_packet_number); //debug
tnhnrl 46:030be9f5c793 1116
tnhnrl 46:030be9f5c793 1117 //let this count up a few times before it exits
tnhnrl 46:030be9f5c793 1118 if (current_packet_number == last_packet_number) {
tnhnrl 46:030be9f5c793 1119 break_transmission++;
tnhnrl 46:030be9f5c793 1120
tnhnrl 46:030be9f5c793 1121 //break transmission after 50 failed attempts (was 100)
tnhnrl 46:030be9f5c793 1122 if (break_transmission >= 50) {
tnhnrl 46:030be9f5c793 1123 closeIncompleteFile(); //close the file
tnhnrl 46:030be9f5c793 1124 _mbed_receive_ticker.detach();
tnhnrl 46:030be9f5c793 1125 pc().printf("MbedLogger: TRANSMISSION INTERRUPTED!\n\r");
tnhnrl 46:030be9f5c793 1126 break;
tnhnrl 46:030be9f5c793 1127 }
tnhnrl 46:030be9f5c793 1128 }
tnhnrl 46:030be9f5c793 1129 else
tnhnrl 46:030be9f5c793 1130 last_packet_number = current_packet_number;
tnhnrl 46:030be9f5c793 1131 }
tnhnrl 46:030be9f5c793 1132 _mbed_receive_loop = false; // wait until the loop rate timer fires again
tnhnrl 46:030be9f5c793 1133 }
tnhnrl 46:030be9f5c793 1134 }
tnhnrl 46:030be9f5c793 1135 }
tnhnrl 46:030be9f5c793 1136
tnhnrl 46:030be9f5c793 1137 int MbedLogger::getFilePointerState() {
tnhnrl 46:030be9f5c793 1138 if (_fp)
tnhnrl 46:030be9f5c793 1139 return 1; //file is not null (has an address), open
tnhnrl 46:030be9f5c793 1140 else
tnhnrl 46:030be9f5c793 1141 return 0; //file is null, closed
tnhnrl 46:030be9f5c793 1142 }
tnhnrl 46:030be9f5c793 1143
tnhnrl 46:030be9f5c793 1144 void MbedLogger::testToggleFilePointer() {
tnhnrl 46:030be9f5c793 1145 pc().printf("testToggleFilePointer\n\r");
tnhnrl 46:030be9f5c793 1146
tnhnrl 46:030be9f5c793 1147 string file_name_string = _file_system_string + "example99.txt";
tnhnrl 46:030be9f5c793 1148
tnhnrl 46:030be9f5c793 1149 if (_fp)
tnhnrl 46:030be9f5c793 1150 _fp = NULL;
tnhnrl 46:030be9f5c793 1151 else
tnhnrl 46:030be9f5c793 1152 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 1153 }
tnhnrl 46:030be9f5c793 1154
tnhnrl 46:030be9f5c793 1155 int MbedLogger::getNumberOfPackets() {
tnhnrl 46:030be9f5c793 1156 return _total_number_of_packets;
tnhnrl 46:030be9f5c793 1157 }
tnhnrl 46:030be9f5c793 1158
tnhnrl 46:030be9f5c793 1159 bool MbedLogger::isTransmissionComplete() {
tnhnrl 46:030be9f5c793 1160 return _file_transmission_complete;
tnhnrl 46:030be9f5c793 1161 }
tnhnrl 46:030be9f5c793 1162
tnhnrl 46:030be9f5c793 1163 int MbedLogger::currentPacketNumber() {
tnhnrl 46:030be9f5c793 1164 return _packet_number;
tnhnrl 49:47ffa4feb6db 1165 }
tnhnrl 49:47ffa4feb6db 1166
tnhnrl 49:47ffa4feb6db 1167 //only do this for the MBED because of the limited file size
tnhnrl 49:47ffa4feb6db 1168 //write one line to the file (open to write, this will erase all other data) and close it.
tnhnrl 49:47ffa4feb6db 1169 void MbedLogger::eraseFile() {
tnhnrl 49:47ffa4feb6db 1170 _fp = fopen(_full_file_path_string.c_str(), "w"); // LOG000.csv
tnhnrl 49:47ffa4feb6db 1171
tnhnrl 49:47ffa4feb6db 1172 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\n(file erased)\n");
tnhnrl 49:47ffa4feb6db 1173
tnhnrl 49:47ffa4feb6db 1174 closeLogFile();
tnhnrl 46:030be9f5c793 1175 }