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@76:c802e1da4179, 2018-08-16 (annotated)
- Committer:
- tnhnrl
- Date:
- Thu Aug 16 21:47:40 2018 +0000
- Revision:
- 76:c802e1da4179
- Parent:
- 74:d281aaef9766
- Child:
- 80:4e5d306d695b
Stable version of code with PID zero offset save fixed!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tnhnrl | 62:d502889e74f1 | 1 | #include "MbedLogger.hpp" |
tnhnrl | 62:d502889e74f1 | 2 | #include "StaticDefs.hpp" |
tnhnrl | 62:d502889e74f1 | 3 | |
tnhnrl | 62:d502889e74f1 | 4 | //Timer t; //used to test time to create packet //timing debug |
tnhnrl | 62:d502889e74f1 | 5 | |
tnhnrl | 62:d502889e74f1 | 6 | MbedLogger::MbedLogger(string file_system_input_string) { |
tnhnrl | 62:d502889e74f1 | 7 | _file_system_string = file_system_input_string; |
tnhnrl | 62:d502889e74f1 | 8 | _full_file_path_string = _file_system_string + "LOG000.csv"; //use multiple logs in the future? (after file size is too large) |
tnhnrl | 62:d502889e74f1 | 9 | _file_number = 0; |
tnhnrl | 62:d502889e74f1 | 10 | _file_transmission = true; |
tnhnrl | 62:d502889e74f1 | 11 | _confirmed_packet_number = 0; //must set this to zero |
tnhnrl | 62:d502889e74f1 | 12 | _transmit_counter = 0; |
tnhnrl | 62:d502889e74f1 | 13 | _file_transmission_state = -1; |
tnhnrl | 62:d502889e74f1 | 14 | _total_number_of_packets = 0; |
tnhnrl | 62:d502889e74f1 | 15 | _mbed_transmit_loop = false; |
tnhnrl | 62:d502889e74f1 | 16 | _received_filename = ""; |
tnhnrl | 63:6cb0405fc6e6 | 17 | |
tnhnrl | 63:6cb0405fc6e6 | 18 | _log_file_line_counter = 0; //used to set timer in finite state machine based on size of log |
tnhnrl | 67:c86a4b464682 | 19 | |
tnhnrl | 74:d281aaef9766 | 20 | //heading string is 254 bytes long, FIXED LENGTH |
tnhnrl | 74:d281aaef9766 | 21 | _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"; |
tnhnrl | 68:8f549749b8ce | 22 | _transmit_packet_num = 0; |
tnhnrl | 69:919ac8d7e023 | 23 | |
tnhnrl | 69:919ac8d7e023 | 24 | _fsm_transmit_complete = false; |
tnhnrl | 73:f6f378311c8d | 25 | |
tnhnrl | 73:f6f378311c8d | 26 | _end_transmit_packet = false; |
tnhnrl | 74:d281aaef9766 | 27 | |
tnhnrl | 74:d281aaef9766 | 28 | _end_sequence_transmission = false; |
tnhnrl | 62:d502889e74f1 | 29 | } |
tnhnrl | 62:d502889e74f1 | 30 | |
tnhnrl | 62:d502889e74f1 | 31 | //this function has to be called for the time to function correctly |
tnhnrl | 62:d502889e74f1 | 32 | void MbedLogger::setLogTime() { |
tnhnrl | 74:d281aaef9766 | 33 | xbee().printf("\n%s log time set.\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 34 | set_time(1518467832); // Set RTC time to Mon, 12 FEB 2018 15:37 |
tnhnrl | 62:d502889e74f1 | 35 | } |
tnhnrl | 62:d502889e74f1 | 36 | |
tnhnrl | 62:d502889e74f1 | 37 | //in the future create the ability to set the start time |
tnhnrl | 62:d502889e74f1 | 38 | int MbedLogger::getSystemTime() { |
tnhnrl | 62:d502889e74f1 | 39 | time_t seconds = time(NULL); // Time as seconds since January 1, 1970 |
tnhnrl | 62:d502889e74f1 | 40 | |
tnhnrl | 62:d502889e74f1 | 41 | return seconds; |
tnhnrl | 62:d502889e74f1 | 42 | } |
tnhnrl | 62:d502889e74f1 | 43 | |
tnhnrl | 62:d502889e74f1 | 44 | void MbedLogger::recordData(int current_state) { |
tnhnrl | 74:d281aaef9766 | 45 | int data_log_time = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp |
tnhnrl | 73:f6f378311c8d | 46 | |
tnhnrl | 74:d281aaef9766 | 47 | _data_log[0] = depthLoop().getCommand(); //depth command |
tnhnrl | 74:d281aaef9766 | 48 | _data_log[1] = depthLoop().getPosition(); //depth reading (filtered depth) |
tnhnrl | 74:d281aaef9766 | 49 | _data_log[2] = pitchLoop().getCommand(); //pitch command |
tnhnrl | 74:d281aaef9766 | 50 | _data_log[3] = pitchLoop().getPosition(); //pitch reading (filtered pitch) |
tnhnrl | 74:d281aaef9766 | 51 | _data_log[4] = rudder().getSetPosition_pwm(); //rudder command PWM |
tnhnrl | 74:d281aaef9766 | 52 | _data_log[5] = rudder().getSetPosition_deg(); //rudder command DEG |
tnhnrl | 73:f6f378311c8d | 53 | _data_log[6] = headingLoop().getPosition(); //heading reading (filtered heading) |
tnhnrl | 73:f6f378311c8d | 54 | |
tnhnrl | 73:f6f378311c8d | 55 | _data_log[7] = bce().getSetPosition_mm(); //BCE command |
tnhnrl | 73:f6f378311c8d | 56 | _data_log[8] = bce().getPosition_mm(); //BCE reading |
tnhnrl | 73:f6f378311c8d | 57 | _data_log[9] = batt().getSetPosition_mm(); //Batt command |
tnhnrl | 73:f6f378311c8d | 58 | _data_log[10] = batt().getPosition_mm(); //Batt reading |
tnhnrl | 73:f6f378311c8d | 59 | _data_log[11] = pitchLoop().getVelocity(); // pitchRate_degs (degrees per second) |
tnhnrl | 73:f6f378311c8d | 60 | _data_log[12] = depthLoop().getVelocity(); // depthRate_fps (feet per second) |
tnhnrl | 62:d502889e74f1 | 61 | |
tnhnrl | 73:f6f378311c8d | 62 | _data_log[13] = sensors().getCurrentInput(); // i_in |
tnhnrl | 73:f6f378311c8d | 63 | _data_log[14] = sensors().getVoltageInput(); // v_in |
tnhnrl | 74:d281aaef9766 | 64 | _data_log[15] = sensors().getAltimeterChannelReadings(); // Altimeter Channel Readings |
tnhnrl | 74:d281aaef9766 | 65 | _data_log[16] = sensors().getInternalPressurePSI(); // int_press_PSI |
tnhnrl | 73:f6f378311c8d | 66 | |
tnhnrl | 74:d281aaef9766 | 67 | //BCE_p,i,d,freq,deadband |
tnhnrl | 74:d281aaef9766 | 68 | _data_log[17] = bce().getControllerP(); |
tnhnrl | 74:d281aaef9766 | 69 | _data_log[18] = bce().getControllerI(); |
tnhnrl | 74:d281aaef9766 | 70 | _data_log[19] = bce().getControllerD(); |
tnhnrl | 74:d281aaef9766 | 71 | |
tnhnrl | 74:d281aaef9766 | 72 | _data_log[20] = batt().getControllerP(); |
tnhnrl | 74:d281aaef9766 | 73 | _data_log[21] = batt().getControllerI(); |
tnhnrl | 74:d281aaef9766 | 74 | _data_log[22] = batt().getControllerD(); |
tnhnrl | 67:c86a4b464682 | 75 | |
tnhnrl | 74:d281aaef9766 | 76 | _data_log[23] = depthLoop().getControllerP(); |
tnhnrl | 74:d281aaef9766 | 77 | _data_log[24] = depthLoop().getControllerI(); |
tnhnrl | 74:d281aaef9766 | 78 | _data_log[25] = depthLoop().getControllerD(); |
tnhnrl | 74:d281aaef9766 | 79 | _data_log[26] = depthLoop().getFilterFrequency(); |
tnhnrl | 74:d281aaef9766 | 80 | _data_log[27] = depthLoop().getDeadband(); |
tnhnrl | 67:c86a4b464682 | 81 | |
tnhnrl | 74:d281aaef9766 | 82 | _data_log[28] = pitchLoop().getControllerP(); |
tnhnrl | 74:d281aaef9766 | 83 | _data_log[29] = pitchLoop().getControllerI(); |
tnhnrl | 74:d281aaef9766 | 84 | _data_log[30] = pitchLoop().getControllerD(); |
tnhnrl | 67:c86a4b464682 | 85 | |
tnhnrl | 74:d281aaef9766 | 86 | _data_log[31] = headingLoop().getControllerP(); |
tnhnrl | 74:d281aaef9766 | 87 | _data_log[32] = headingLoop().getControllerI(); |
tnhnrl | 74:d281aaef9766 | 88 | _data_log[33] = headingLoop().getControllerD(); |
tnhnrl | 74:d281aaef9766 | 89 | _data_log[34] = headingLoop().getFilterFrequency(); |
tnhnrl | 74:d281aaef9766 | 90 | _data_log[35] = headingLoop().getDeadband(); |
tnhnrl | 74:d281aaef9766 | 91 | |
tnhnrl | 62:d502889e74f1 | 92 | string string_state; |
tnhnrl | 62:d502889e74f1 | 93 | if (current_state == SIT_IDLE) |
tnhnrl | 62:d502889e74f1 | 94 | string_state = "SIT_IDLE"; |
tnhnrl | 62:d502889e74f1 | 95 | else if (current_state == FIND_NEUTRAL) |
tnhnrl | 62:d502889e74f1 | 96 | string_state = "FIND_NEUTRAL"; |
tnhnrl | 62:d502889e74f1 | 97 | else if (current_state == DIVE) |
tnhnrl | 62:d502889e74f1 | 98 | string_state = "DIVE"; |
tnhnrl | 62:d502889e74f1 | 99 | else if (current_state == RISE) |
tnhnrl | 62:d502889e74f1 | 100 | string_state = "RISE"; |
tnhnrl | 62:d502889e74f1 | 101 | else if (current_state == FLOAT_LEVEL) |
tnhnrl | 62:d502889e74f1 | 102 | string_state = "FLOAT_LEVEL"; |
tnhnrl | 62:d502889e74f1 | 103 | else if (current_state == FLOAT_BROADCAST) |
tnhnrl | 62:d502889e74f1 | 104 | string_state = "FLOAT_BROADCAST"; |
tnhnrl | 62:d502889e74f1 | 105 | else if (current_state == EMERGENCY_CLIMB) |
tnhnrl | 62:d502889e74f1 | 106 | string_state = "EMERGENCY_CLIMB"; |
tnhnrl | 62:d502889e74f1 | 107 | else if (current_state == MULTI_DIVE) |
tnhnrl | 62:d502889e74f1 | 108 | string_state = "MULTI_DIVE"; |
tnhnrl | 62:d502889e74f1 | 109 | else if (current_state == MULTI_RISE) |
tnhnrl | 62:d502889e74f1 | 110 | string_state = "MULTI_RISE"; |
tnhnrl | 62:d502889e74f1 | 111 | else if (current_state == KEYBOARD) |
tnhnrl | 62:d502889e74f1 | 112 | string_state = "KEYBOARD"; |
tnhnrl | 62:d502889e74f1 | 113 | else if (current_state == CHECK_TUNING) |
tnhnrl | 63:6cb0405fc6e6 | 114 | string_state = "CHECK_TUNING"; |
tnhnrl | 63:6cb0405fc6e6 | 115 | else if (current_state == POSITION_DIVE) |
tnhnrl | 63:6cb0405fc6e6 | 116 | string_state = "POSITION_DIVE"; |
tnhnrl | 63:6cb0405fc6e6 | 117 | else if (current_state == POSITION_RISE) |
tnhnrl | 69:919ac8d7e023 | 118 | string_state = "POSITION_RISE"; |
tnhnrl | 71:939d179478c4 | 119 | else if (current_state == TX_MBED_LOG) |
tnhnrl | 71:939d179478c4 | 120 | string_state = "TX_MBED_LOG"; |
tnhnrl | 74:d281aaef9766 | 121 | else if (current_state == RX_SEQUENCE) |
tnhnrl | 69:919ac8d7e023 | 122 | string_state = "RECEIVE_SEQUENCE"; |
tnhnrl | 69:919ac8d7e023 | 123 | |
tnhnrl | 74:d281aaef9766 | 124 | string blank_space = ""; //to get consistent spacing in the file (had a nonsense char w/o this) |
tnhnrl | 62:d502889e74f1 | 125 | |
tnhnrl | 76:c802e1da4179 | 126 | //below this format is used for data transmission, each packet needs to be 254 characters long (not counting newline char) |
tnhnrl | 68:8f549749b8ce | 127 | |
tnhnrl | 76:c802e1da4179 | 128 | //verified that this generates the correct line length of 254 using SOLELY an mbed 08/16/2018 |
tnhnrl | 76:c802e1da4179 | 129 | 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", |
tnhnrl | 76:c802e1da4179 | 130 | string_state.c_str(),current_state,data_log_time, |
tnhnrl | 74:d281aaef9766 | 131 | _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], |
tnhnrl | 74:d281aaef9766 | 132 | _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], |
tnhnrl | 74:d281aaef9766 | 133 | _data_log[31],_data_log[32],_data_log[33],_data_log[34],_data_log[35]); |
tnhnrl | 74:d281aaef9766 | 134 | |
tnhnrl | 67:c86a4b464682 | 135 | //each line in the file is 160 characters long text-wise, check this with a file read |
tnhnrl | 62:d502889e74f1 | 136 | } |
tnhnrl | 62:d502889e74f1 | 137 | |
tnhnrl | 62:d502889e74f1 | 138 | void MbedLogger::printMbedDirectory() { |
tnhnrl | 62:d502889e74f1 | 139 | DIR *dir; |
tnhnrl | 62:d502889e74f1 | 140 | struct dirent *dp; //dirent.h is the format of directory entries |
tnhnrl | 62:d502889e74f1 | 141 | int log_found =0,loop=0; |
tnhnrl | 62:d502889e74f1 | 142 | long int temp=0; |
tnhnrl | 62:d502889e74f1 | 143 | |
tnhnrl | 62:d502889e74f1 | 144 | //char * char_pointer; |
tnhnrl | 62:d502889e74f1 | 145 | char * numstart; |
tnhnrl | 62:d502889e74f1 | 146 | //char *numstop; |
tnhnrl | 62:d502889e74f1 | 147 | |
tnhnrl | 74:d281aaef9766 | 148 | xbee().printf("\n\rPrinting out the directory of device %s\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 149 | |
tnhnrl | 62:d502889e74f1 | 150 | if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) { |
tnhnrl | 74:d281aaef9766 | 151 | xbee().printf("MBED directory could not be opened\r\n"); |
tnhnrl | 62:d502889e74f1 | 152 | } |
tnhnrl | 62:d502889e74f1 | 153 | else |
tnhnrl | 62:d502889e74f1 | 154 | { |
tnhnrl | 62:d502889e74f1 | 155 | while ( NULL != (dp = readdir( dir )) ) |
tnhnrl | 62:d502889e74f1 | 156 | { |
tnhnrl | 62:d502889e74f1 | 157 | if(strncmp(dp->d_name,"LOG",3)==0) |
tnhnrl | 62:d502889e74f1 | 158 | { |
tnhnrl | 62:d502889e74f1 | 159 | log_found=1; |
tnhnrl | 62:d502889e74f1 | 160 | /*numstart = dp->d_name; //Look for third character (do safety) |
tnhnrl | 62:d502889e74f1 | 161 | numstop = strchr(dp->d_name,'.'); |
tnhnrl | 62:d502889e74f1 | 162 | if(numstart!=NULL&&numstop!=NULL) |
tnhnrl | 62:d502889e74f1 | 163 | { |
tnhnrl | 62:d502889e74f1 | 164 | temp=numstop-numstart; |
tnhnrl | 62:d502889e74f1 | 165 | } |
tnhnrl | 62:d502889e74f1 | 166 | else |
tnhnrl | 62:d502889e74f1 | 167 | log_found=0; //Something is not right. Ignore |
tnhnrl | 62:d502889e74f1 | 168 | */ |
tnhnrl | 62:d502889e74f1 | 169 | numstart=dp->d_name+3; |
tnhnrl | 62:d502889e74f1 | 170 | temp=strtol(numstart,NULL,10); //add in check to see if this is null (start logs at one) |
tnhnrl | 62:d502889e74f1 | 171 | } |
tnhnrl | 62:d502889e74f1 | 172 | else |
tnhnrl | 62:d502889e74f1 | 173 | log_found=0; |
tnhnrl | 74:d281aaef9766 | 174 | xbee().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp); |
tnhnrl | 62:d502889e74f1 | 175 | |
tnhnrl | 62:d502889e74f1 | 176 | loop++; |
tnhnrl | 62:d502889e74f1 | 177 | } |
tnhnrl | 62:d502889e74f1 | 178 | } |
tnhnrl | 62:d502889e74f1 | 179 | } |
tnhnrl | 62:d502889e74f1 | 180 | |
tnhnrl | 68:8f549749b8ce | 181 | //prints current log file to the screen (terminal) |
tnhnrl | 62:d502889e74f1 | 182 | void MbedLogger::printCurrentLogFile() { |
tnhnrl | 62:d502889e74f1 | 183 | //open the file for reading |
tnhnrl | 62:d502889e74f1 | 184 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 185 | |
tnhnrl | 63:6cb0405fc6e6 | 186 | _log_file_line_counter = 0; |
tnhnrl | 62:d502889e74f1 | 187 | |
tnhnrl | 62:d502889e74f1 | 188 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 62:d502889e74f1 | 189 | |
tnhnrl | 62:d502889e74f1 | 190 | char buffer[500]; |
tnhnrl | 62:d502889e74f1 | 191 | |
tnhnrl | 62:d502889e74f1 | 192 | //read the file line-by-line and print that to the screen |
tnhnrl | 74:d281aaef9766 | 193 | xbee().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\n\r",_file_number); |
tnhnrl | 62:d502889e74f1 | 194 | while (!feof(_fp)) { |
tnhnrl | 62:d502889e74f1 | 195 | // read in the line and make sure it was successful |
tnhnrl | 62:d502889e74f1 | 196 | if (fgets(buffer,500,_fp) != NULL) { //stops at new line |
tnhnrl | 74:d281aaef9766 | 197 | xbee().printf("%s\r",buffer); |
tnhnrl | 63:6cb0405fc6e6 | 198 | _log_file_line_counter++; |
tnhnrl | 62:d502889e74f1 | 199 | } |
tnhnrl | 62:d502889e74f1 | 200 | } |
tnhnrl | 62:d502889e74f1 | 201 | |
tnhnrl | 62:d502889e74f1 | 202 | //fgets stops when either (n-1) characters are read, the newline character is read, |
tnhnrl | 62:d502889e74f1 | 203 | // or the end-of-file is reached |
tnhnrl | 62:d502889e74f1 | 204 | |
tnhnrl | 62:d502889e74f1 | 205 | //close the file |
tnhnrl | 62:d502889e74f1 | 206 | closeLogFile(); |
tnhnrl | 74:d281aaef9766 | 207 | xbee().printf("\n\rLog file closed. Lines in log file: %d.\n\r", _log_file_line_counter); |
tnhnrl | 62:d502889e74f1 | 208 | } |
tnhnrl | 62:d502889e74f1 | 209 | |
tnhnrl | 73:f6f378311c8d | 210 | void MbedLogger::blastData() { |
tnhnrl | 74:d281aaef9766 | 211 | xbee().printf("blastData FUNCTION\n\r"); |
tnhnrl | 73:f6f378311c8d | 212 | |
tnhnrl | 73:f6f378311c8d | 213 | //OPEN FILE FOR READING |
tnhnrl | 73:f6f378311c8d | 214 | |
tnhnrl | 73:f6f378311c8d | 215 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 73:f6f378311c8d | 216 | |
tnhnrl | 73:f6f378311c8d | 217 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 73:f6f378311c8d | 218 | |
tnhnrl | 73:f6f378311c8d | 219 | /******************************/ |
tnhnrl | 73:f6f378311c8d | 220 | while(1) { |
tnhnrl | 73:f6f378311c8d | 221 | wait(0.05); |
tnhnrl | 73:f6f378311c8d | 222 | if (!feof(_fp)) { //check for end of file |
tnhnrl | 73:f6f378311c8d | 223 | //based on the internal packet number of the class //createDataPacket //transmit data packet |
tnhnrl | 73:f6f378311c8d | 224 | transmitPacketNumber(_packet_number); |
tnhnrl | 74:d281aaef9766 | 225 | xbee().printf("\r"); // for proper spacing |
tnhnrl | 73:f6f378311c8d | 226 | |
tnhnrl | 73:f6f378311c8d | 227 | _packet_number++; |
tnhnrl | 73:f6f378311c8d | 228 | } |
tnhnrl | 73:f6f378311c8d | 229 | else { |
tnhnrl | 73:f6f378311c8d | 230 | _packet_number = 0; //reset packet number |
tnhnrl | 73:f6f378311c8d | 231 | break; |
tnhnrl | 73:f6f378311c8d | 232 | } |
tnhnrl | 73:f6f378311c8d | 233 | } |
tnhnrl | 73:f6f378311c8d | 234 | /******************************/ |
tnhnrl | 73:f6f378311c8d | 235 | |
tnhnrl | 73:f6f378311c8d | 236 | //CLOSE THE FILE |
tnhnrl | 73:f6f378311c8d | 237 | closeLogFile(); |
tnhnrl | 74:d281aaef9766 | 238 | xbee().printf("\n\rblastData: Log file closed. %d.\n\r"); |
tnhnrl | 62:d502889e74f1 | 239 | } |
tnhnrl | 62:d502889e74f1 | 240 | |
tnhnrl | 62:d502889e74f1 | 241 | void MbedLogger::createDataPacket() { |
tnhnrl | 62:d502889e74f1 | 242 | // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2 |
tnhnrl | 62:d502889e74f1 | 243 | |
tnhnrl | 62:d502889e74f1 | 244 | //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0. |
tnhnrl | 62:d502889e74f1 | 245 | _data_packet.clear(); |
tnhnrl | 62:d502889e74f1 | 246 | |
tnhnrl | 62:d502889e74f1 | 247 | //DATA PACKET HEADER |
tnhnrl | 62:d502889e74f1 | 248 | _data_packet.push_back(117); //0x75 |
tnhnrl | 62:d502889e74f1 | 249 | _data_packet.push_back(101); //0x65 |
tnhnrl | 62:d502889e74f1 | 250 | |
tnhnrl | 62:d502889e74f1 | 251 | _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form |
tnhnrl | 62:d502889e74f1 | 252 | _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form |
tnhnrl | 62:d502889e74f1 | 253 | |
tnhnrl | 62:d502889e74f1 | 254 | _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form |
tnhnrl | 62:d502889e74f1 | 255 | _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form |
tnhnrl | 62:d502889e74f1 | 256 | |
tnhnrl | 62:d502889e74f1 | 257 | _data_packet.push_back(_current_line_length); |
tnhnrl | 62:d502889e74f1 | 258 | |
tnhnrl | 74:d281aaef9766 | 259 | //xbee().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer); //debug |
tnhnrl | 62:d502889e74f1 | 260 | |
tnhnrl | 62:d502889e74f1 | 261 | //DATA FROM LINE READ (read string character by chracter) |
tnhnrl | 62:d502889e74f1 | 262 | for (int i = 0; i < _current_line_length; i++) { |
tnhnrl | 62:d502889e74f1 | 263 | _data_packet.push_back(_line_buffer[i]); |
tnhnrl | 62:d502889e74f1 | 264 | } |
tnhnrl | 62:d502889e74f1 | 265 | |
tnhnrl | 62:d502889e74f1 | 266 | //CRC CALCULATIONS BELOW |
tnhnrl | 62:d502889e74f1 | 267 | //character version of calculation screws up on null character 0x00, scrapped and using vector of integers |
tnhnrl | 62:d502889e74f1 | 268 | |
tnhnrl | 62:d502889e74f1 | 269 | int crc_one = calcCrcOne(); |
tnhnrl | 62:d502889e74f1 | 270 | int crc_two = calcCrcTwo(); |
tnhnrl | 62:d502889e74f1 | 271 | |
tnhnrl | 62:d502889e74f1 | 272 | //place the crc bytes into the data packet that is transmitted |
tnhnrl | 62:d502889e74f1 | 273 | _data_packet.push_back(crc_one); |
tnhnrl | 62:d502889e74f1 | 274 | _data_packet.push_back(crc_two); |
tnhnrl | 62:d502889e74f1 | 275 | } |
tnhnrl | 62:d502889e74f1 | 276 | |
tnhnrl | 69:919ac8d7e023 | 277 | //new 6/27/2018 |
tnhnrl | 69:919ac8d7e023 | 278 | void MbedLogger::createDataPacket(char line_buffer_sent[], int line_length_sent) { |
tnhnrl | 69:919ac8d7e023 | 279 | // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2 |
tnhnrl | 69:919ac8d7e023 | 280 | |
tnhnrl | 69:919ac8d7e023 | 281 | //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0. |
tnhnrl | 69:919ac8d7e023 | 282 | _data_packet.clear(); |
tnhnrl | 69:919ac8d7e023 | 283 | |
tnhnrl | 69:919ac8d7e023 | 284 | //DATA PACKET HEADER |
tnhnrl | 69:919ac8d7e023 | 285 | _data_packet.push_back(117); //0x75 |
tnhnrl | 69:919ac8d7e023 | 286 | _data_packet.push_back(101); //0x65 |
tnhnrl | 69:919ac8d7e023 | 287 | |
tnhnrl | 69:919ac8d7e023 | 288 | _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form |
tnhnrl | 69:919ac8d7e023 | 289 | _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form |
tnhnrl | 69:919ac8d7e023 | 290 | |
tnhnrl | 69:919ac8d7e023 | 291 | _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form |
tnhnrl | 69:919ac8d7e023 | 292 | _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form |
tnhnrl | 69:919ac8d7e023 | 293 | |
tnhnrl | 69:919ac8d7e023 | 294 | _data_packet.push_back(line_length_sent); |
tnhnrl | 69:919ac8d7e023 | 295 | |
tnhnrl | 74:d281aaef9766 | 296 | //xbee().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer); //debug |
tnhnrl | 69:919ac8d7e023 | 297 | |
tnhnrl | 69:919ac8d7e023 | 298 | //DATA FROM LINE READ (read string character by chracter) |
tnhnrl | 69:919ac8d7e023 | 299 | for (int i = 0; i < line_length_sent; i++) { |
tnhnrl | 69:919ac8d7e023 | 300 | _data_packet.push_back(line_buffer_sent[i]); |
tnhnrl | 69:919ac8d7e023 | 301 | } |
tnhnrl | 69:919ac8d7e023 | 302 | |
tnhnrl | 69:919ac8d7e023 | 303 | //CRC CALCULATIONS BELOW |
tnhnrl | 69:919ac8d7e023 | 304 | //character version of calculation screws up on null character 0x00, scrapped and using vector of integers |
tnhnrl | 69:919ac8d7e023 | 305 | |
tnhnrl | 69:919ac8d7e023 | 306 | int crc_one = calcCrcOne(); |
tnhnrl | 69:919ac8d7e023 | 307 | int crc_two = calcCrcTwo(); |
tnhnrl | 69:919ac8d7e023 | 308 | |
tnhnrl | 69:919ac8d7e023 | 309 | //place the crc bytes into the data packet that is transmitted |
tnhnrl | 69:919ac8d7e023 | 310 | _data_packet.push_back(crc_one); |
tnhnrl | 69:919ac8d7e023 | 311 | _data_packet.push_back(crc_two); |
tnhnrl | 69:919ac8d7e023 | 312 | |
tnhnrl | 74:d281aaef9766 | 313 | //xbee().printf("debug createDataPacket(char line_buffer_sent[], int line_length_sent)\n\r"); |
tnhnrl | 69:919ac8d7e023 | 314 | } |
tnhnrl | 69:919ac8d7e023 | 315 | |
tnhnrl | 62:d502889e74f1 | 316 | //should i save it as a string and transmit the string? next iteration maybe |
tnhnrl | 62:d502889e74f1 | 317 | |
tnhnrl | 62:d502889e74f1 | 318 | void MbedLogger::transmitDataPacket() { |
tnhnrl | 62:d502889e74f1 | 319 | //WRITE the data (in bytes) to the serial port |
tnhnrl | 62:d502889e74f1 | 320 | for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) { |
tnhnrl | 74:d281aaef9766 | 321 | xbee().putc(*_it); //send integers over serial port one byte at a time |
tnhnrl | 62:d502889e74f1 | 322 | } |
tnhnrl | 62:d502889e74f1 | 323 | } |
tnhnrl | 62:d502889e74f1 | 324 | |
tnhnrl | 62:d502889e74f1 | 325 | // test to see if transmitDataPacket is working slower than you think. |
tnhnrl | 62:d502889e74f1 | 326 | |
tnhnrl | 62:d502889e74f1 | 327 | // REACH ONE CHAR AT A TIME (EACH ITERATION OF STATE MACHINE...) |
tnhnrl | 62:d502889e74f1 | 328 | |
tnhnrl | 68:8f549749b8ce | 329 | |
tnhnrl | 68:8f549749b8ce | 330 | void MbedLogger::continuouslyTransmitDataNoTimer() { |
tnhnrl | 68:8f549749b8ce | 331 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 68:8f549749b8ce | 332 | |
tnhnrl | 68:8f549749b8ce | 333 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 68:8f549749b8ce | 334 | |
tnhnrl | 68:8f549749b8ce | 335 | static int packet_num = 0; |
tnhnrl | 68:8f549749b8ce | 336 | |
tnhnrl | 68:8f549749b8ce | 337 | while(!feof(_fp)) { |
tnhnrl | 68:8f549749b8ce | 338 | |
tnhnrl | 68:8f549749b8ce | 339 | _packet_number = packet_num; |
tnhnrl | 68:8f549749b8ce | 340 | |
tnhnrl | 68:8f549749b8ce | 341 | readPacketInSeries(); //not using this but for testing purposes... |
tnhnrl | 68:8f549749b8ce | 342 | |
tnhnrl | 68:8f549749b8ce | 343 | // // createDataPacket requires _packet_number, _total_number_of_packets, _current_line_length (data packet size) |
tnhnrl | 68:8f549749b8ce | 344 | // // uses char _line_buffer[256] variable to hold characters read from the file |
tnhnrl | 69:919ac8d7e023 | 345 | // // packs this into a vector for transmission |
tnhnrl | 69:919ac8d7e023 | 346 | |
tnhnrl | 68:8f549749b8ce | 347 | createDataPacket(); |
tnhnrl | 68:8f549749b8ce | 348 | |
tnhnrl | 68:8f549749b8ce | 349 | transmitDataPacket(); |
tnhnrl | 68:8f549749b8ce | 350 | |
tnhnrl | 68:8f549749b8ce | 351 | packet_num++; |
tnhnrl | 68:8f549749b8ce | 352 | |
tnhnrl | 68:8f549749b8ce | 353 | led3() = !led3(); |
tnhnrl | 68:8f549749b8ce | 354 | |
tnhnrl | 68:8f549749b8ce | 355 | //check for incoming request |
tnhnrl | 68:8f549749b8ce | 356 | } |
tnhnrl | 68:8f549749b8ce | 357 | |
tnhnrl | 68:8f549749b8ce | 358 | closeLogFile(); //close log file if open |
tnhnrl | 68:8f549749b8ce | 359 | led4() = !led4(); |
tnhnrl | 68:8f549749b8ce | 360 | } |
tnhnrl | 68:8f549749b8ce | 361 | |
tnhnrl | 73:f6f378311c8d | 362 | void MbedLogger::fsmTransmitData() { //using the finite state machine |
tnhnrl | 73:f6f378311c8d | 363 | if ( !feof(_fp) and (!_fsm_transmit_complete) ) { //check for end of file |
tnhnrl | 73:f6f378311c8d | 364 | //based on the internal packet number of the class //createDataPacket //transmit data packet |
tnhnrl | 69:919ac8d7e023 | 365 | transmitPacketNumber(_packet_number); |
tnhnrl | 69:919ac8d7e023 | 366 | |
tnhnrl | 74:d281aaef9766 | 367 | xbee().printf("\r"); // for proper spacing |
tnhnrl | 73:f6f378311c8d | 368 | |
tnhnrl | 69:919ac8d7e023 | 369 | _packet_number++; |
tnhnrl | 68:8f549749b8ce | 370 | |
tnhnrl | 73:f6f378311c8d | 371 | led2() = !led2(); |
tnhnrl | 68:8f549749b8ce | 372 | } |
tnhnrl | 69:919ac8d7e023 | 373 | else { |
tnhnrl | 73:f6f378311c8d | 374 | _packet_number = 0; //reset packet number |
tnhnrl | 73:f6f378311c8d | 375 | |
tnhnrl | 69:919ac8d7e023 | 376 | _fsm_transmit_complete = true; |
tnhnrl | 73:f6f378311c8d | 377 | |
tnhnrl | 73:f6f378311c8d | 378 | led3() = !led3(); |
tnhnrl | 69:919ac8d7e023 | 379 | } |
tnhnrl | 68:8f549749b8ce | 380 | } |
tnhnrl | 68:8f549749b8ce | 381 | |
tnhnrl | 71:939d179478c4 | 382 | //transmitting log file with fixed length of characters to receiver program |
tnhnrl | 68:8f549749b8ce | 383 | void MbedLogger::transmitPacketNumber(int line_number) { |
tnhnrl | 73:f6f378311c8d | 384 | int line_size = 254; //length of lines in the log file, EVERY LINE MUST BE THE SAME LENGTH |
tnhnrl | 69:919ac8d7e023 | 385 | |
tnhnrl | 68:8f549749b8ce | 386 | fseek(_fp,(line_size+1)*line_number,SEEK_SET); //fseek must use the +1 to get the newline character |
tnhnrl | 69:919ac8d7e023 | 387 | |
tnhnrl | 69:919ac8d7e023 | 388 | //write over the internal _line_buffer //start from the beginning and go to this position |
tnhnrl | 69:919ac8d7e023 | 389 | fread(_line_buffer, 1, line_size, _fp); //read the line that is exactly 160 characters long |
tnhnrl | 68:8f549749b8ce | 390 | |
tnhnrl | 74:d281aaef9766 | 391 | //xbee().printf("Debug (transmitPacketNumber): line_buffer <<%s>> (line size: %d)\n\r", line_buffer,line_size); |
tnhnrl | 68:8f549749b8ce | 392 | |
tnhnrl | 68:8f549749b8ce | 393 | // createDataPacket requires _packet_number, _total_number_of_packets, _current_line_length (data packet size) |
tnhnrl | 68:8f549749b8ce | 394 | // uses char _line_buffer[256] variable to hold characters read from the file |
tnhnrl | 68:8f549749b8ce | 395 | // packs this into a vector for transmission |
tnhnrl | 68:8f549749b8ce | 396 | |
tnhnrl | 71:939d179478c4 | 397 | //change the internal member variable for packet number, reorg this later |
tnhnrl | 71:939d179478c4 | 398 | _packet_number = line_number; |
tnhnrl | 71:939d179478c4 | 399 | |
tnhnrl | 69:919ac8d7e023 | 400 | createDataPacket(_line_buffer, line_size); //create the data packet from the _line_buffer (char array) |
tnhnrl | 69:919ac8d7e023 | 401 | |
tnhnrl | 68:8f549749b8ce | 402 | transmitDataPacket(); //transmit the assembled packet |
tnhnrl | 68:8f549749b8ce | 403 | } |
tnhnrl | 68:8f549749b8ce | 404 | |
tnhnrl | 69:919ac8d7e023 | 405 | |
tnhnrl | 69:919ac8d7e023 | 406 | //receive correct data packet from python, immediately send a transmit packet from the MBED |
tnhnrl | 73:f6f378311c8d | 407 | void MbedLogger::transmitOnePacket() { |
tnhnrl | 74:d281aaef9766 | 408 | xbee().printf("transmitOnePacket\n"); |
tnhnrl | 73:f6f378311c8d | 409 | |
tnhnrl | 68:8f549749b8ce | 410 | static int transmit_state = HEADER_117; //state in switch statement |
tnhnrl | 73:f6f378311c8d | 411 | //int incoming_byte = -1; //reset each time a character is read |
tnhnrl | 68:8f549749b8ce | 412 | int requested_packet_number = -1; //reset each time a character is read |
tnhnrl | 68:8f549749b8ce | 413 | static int input_packet[4]; //changed from char in previous iteration 03/28/2018 |
tnhnrl | 68:8f549749b8ce | 414 | static int transmit_crc_one = 0; //hold crc values until they're reset with calculations |
tnhnrl | 68:8f549749b8ce | 415 | static int transmit_crc_two = 0; |
tnhnrl | 73:f6f378311c8d | 416 | |
tnhnrl | 73:f6f378311c8d | 417 | int incoming_byte[6]; |
tnhnrl | 73:f6f378311c8d | 418 | |
tnhnrl | 73:f6f378311c8d | 419 | static int bytes_received = 0; |
tnhnrl | 73:f6f378311c8d | 420 | |
tnhnrl | 73:f6f378311c8d | 421 | int req_packet_number = -1; |
tnhnrl | 68:8f549749b8ce | 422 | |
tnhnrl | 73:f6f378311c8d | 423 | while (1) { |
tnhnrl | 74:d281aaef9766 | 424 | incoming_byte[bytes_received] = xbee().getc(); |
tnhnrl | 74:d281aaef9766 | 425 | xbee().printf("<%d> ", incoming_byte[bytes_received]); |
tnhnrl | 68:8f549749b8ce | 426 | |
tnhnrl | 73:f6f378311c8d | 427 | bytes_received++; |
tnhnrl | 73:f6f378311c8d | 428 | |
tnhnrl | 73:f6f378311c8d | 429 | if (bytes_received > 5) { |
tnhnrl | 68:8f549749b8ce | 430 | |
tnhnrl | 73:f6f378311c8d | 431 | req_packet_number = incoming_byte[2] * 256 + incoming_byte[3]; |
tnhnrl | 68:8f549749b8ce | 432 | |
tnhnrl | 74:d281aaef9766 | 433 | xbee().printf("req_packet_number = %d\n\r", req_packet_number); |
tnhnrl | 68:8f549749b8ce | 434 | |
tnhnrl | 73:f6f378311c8d | 435 | bytes_received = 0; |
tnhnrl | 68:8f549749b8ce | 436 | break; |
tnhnrl | 73:f6f378311c8d | 437 | } |
tnhnrl | 73:f6f378311c8d | 438 | } |
tnhnrl | 73:f6f378311c8d | 439 | |
tnhnrl | 73:f6f378311c8d | 440 | setTransmitPacketNumber(0); |
tnhnrl | 73:f6f378311c8d | 441 | |
tnhnrl | 73:f6f378311c8d | 442 | //open the file |
tnhnrl | 73:f6f378311c8d | 443 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 73:f6f378311c8d | 444 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 73:f6f378311c8d | 445 | |
tnhnrl | 73:f6f378311c8d | 446 | //receive correct checksum, immediately send this packet |
tnhnrl | 73:f6f378311c8d | 447 | transmitPacketNumber(req_packet_number); |
tnhnrl | 73:f6f378311c8d | 448 | |
tnhnrl | 74:d281aaef9766 | 449 | //fclose(_fp); |
tnhnrl | 74:d281aaef9766 | 450 | closeLogFile(); |
tnhnrl | 68:8f549749b8ce | 451 | } |
tnhnrl | 68:8f549749b8ce | 452 | |
tnhnrl | 73:f6f378311c8d | 453 | void MbedLogger::transmitMultiplePackets() { |
tnhnrl | 74:d281aaef9766 | 454 | xbee().printf("transmitMultiplePackets\n"); |
tnhnrl | 73:f6f378311c8d | 455 | |
tnhnrl | 74:d281aaef9766 | 456 | //static int transmit_state = HEADER_117; //state in switch statement |
tnhnrl | 73:f6f378311c8d | 457 | //int incoming_byte = -1; //reset each time a character is read |
tnhnrl | 74:d281aaef9766 | 458 | //int requested_packet_number = -1; //reset each time a character is read |
tnhnrl | 74:d281aaef9766 | 459 | static int input_packet; //changed from char in previous iteration 03/28/2018 |
tnhnrl | 62:d502889e74f1 | 460 | static int transmit_crc_one = 0; //hold crc values until they're reset with calculations |
tnhnrl | 62:d502889e74f1 | 461 | static int transmit_crc_two = 0; |
tnhnrl | 74:d281aaef9766 | 462 | |
tnhnrl | 74:d281aaef9766 | 463 | int current_byte = -1; |
tnhnrl | 62:d502889e74f1 | 464 | |
tnhnrl | 73:f6f378311c8d | 465 | static int bytes_received = 0; |
tnhnrl | 73:f6f378311c8d | 466 | |
tnhnrl | 73:f6f378311c8d | 467 | int req_packet_number = -1; |
tnhnrl | 73:f6f378311c8d | 468 | |
tnhnrl | 74:d281aaef9766 | 469 | //GET TOTAL NUMBER OF PACKETS! |
tnhnrl | 74:d281aaef9766 | 470 | getNumberOfPacketsInCurrentLog(); |
tnhnrl | 74:d281aaef9766 | 471 | //GET TOTAL NUMBER OF PACKETS! |
tnhnrl | 74:d281aaef9766 | 472 | |
tnhnrl | 73:f6f378311c8d | 473 | //open the file |
tnhnrl | 73:f6f378311c8d | 474 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 73:f6f378311c8d | 475 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 74:d281aaef9766 | 476 | |
tnhnrl | 74:d281aaef9766 | 477 | //DEFAULT STATE |
tnhnrl | 74:d281aaef9766 | 478 | static int current_state = HEADER_117; |
tnhnrl | 74:d281aaef9766 | 479 | |
tnhnrl | 74:d281aaef9766 | 480 | bool active_loop = true; |
tnhnrl | 74:d281aaef9766 | 481 | |
tnhnrl | 74:d281aaef9766 | 482 | while (active_loop) { |
tnhnrl | 73:f6f378311c8d | 483 | //INCOMING BYTE |
tnhnrl | 74:d281aaef9766 | 484 | current_byte = xbee().getc(); |
tnhnrl | 74:d281aaef9766 | 485 | |
tnhnrl | 74:d281aaef9766 | 486 | //provide the next byte / state |
tnhnrl | 73:f6f378311c8d | 487 | |
tnhnrl | 74:d281aaef9766 | 488 | switch (current_state) { |
tnhnrl | 74:d281aaef9766 | 489 | case HEADER_117: |
tnhnrl | 74:d281aaef9766 | 490 | //xbee().printf("HEADING 117\n\r"); |
tnhnrl | 74:d281aaef9766 | 491 | if (current_byte == 0x75) { |
tnhnrl | 74:d281aaef9766 | 492 | current_state = HEADER_101; |
tnhnrl | 74:d281aaef9766 | 493 | } |
tnhnrl | 74:d281aaef9766 | 494 | |
tnhnrl | 74:d281aaef9766 | 495 | |
tnhnrl | 74:d281aaef9766 | 496 | else if (current_byte == 0x10) { |
tnhnrl | 74:d281aaef9766 | 497 | current_state = END_TX_1; |
tnhnrl | 74:d281aaef9766 | 498 | } |
tnhnrl | 74:d281aaef9766 | 499 | break; |
tnhnrl | 74:d281aaef9766 | 500 | case HEADER_101: |
tnhnrl | 74:d281aaef9766 | 501 | //xbee().printf("HEADING 101\n\r"); |
tnhnrl | 74:d281aaef9766 | 502 | if (current_byte == 0x65) { |
tnhnrl | 74:d281aaef9766 | 503 | current_state = PACKET_NO_1; |
tnhnrl | 73:f6f378311c8d | 504 | } |
tnhnrl | 74:d281aaef9766 | 505 | break; |
tnhnrl | 74:d281aaef9766 | 506 | case PACKET_NO_1: |
tnhnrl | 74:d281aaef9766 | 507 | //xbee().printf("PACKET_NO_1\n\r"); |
tnhnrl | 74:d281aaef9766 | 508 | input_packet = current_byte * 256; |
tnhnrl | 74:d281aaef9766 | 509 | |
tnhnrl | 74:d281aaef9766 | 510 | current_state = PACKET_NO_2; |
tnhnrl | 74:d281aaef9766 | 511 | //xbee().printf("PACKET # 1 current byte %d\n\r", current_byte); |
tnhnrl | 74:d281aaef9766 | 512 | break; |
tnhnrl | 73:f6f378311c8d | 513 | |
tnhnrl | 74:d281aaef9766 | 514 | case PACKET_NO_2: |
tnhnrl | 74:d281aaef9766 | 515 | //xbee().printf("PACKET_NO_2\n\r"); |
tnhnrl | 74:d281aaef9766 | 516 | input_packet = input_packet + current_byte; |
tnhnrl | 74:d281aaef9766 | 517 | |
tnhnrl | 74:d281aaef9766 | 518 | current_state = PACKET_CRC_ONE; |
tnhnrl | 74:d281aaef9766 | 519 | //xbee().printf("PACKET # 2 current byte %d (req packet num: %d)\n\r", current_byte, input_packet); |
tnhnrl | 74:d281aaef9766 | 520 | break; |
tnhnrl | 74:d281aaef9766 | 521 | |
tnhnrl | 74:d281aaef9766 | 522 | case PACKET_CRC_ONE: |
tnhnrl | 74:d281aaef9766 | 523 | //xbee().printf("PACKET_CRC_ONE\n\r"); |
tnhnrl | 74:d281aaef9766 | 524 | current_state = PACKET_CRC_TWO; |
tnhnrl | 74:d281aaef9766 | 525 | break; |
tnhnrl | 73:f6f378311c8d | 526 | |
tnhnrl | 74:d281aaef9766 | 527 | case PACKET_CRC_TWO: |
tnhnrl | 74:d281aaef9766 | 528 | //xbee().printf("PACKET_CRC_TWO\n\r"); |
tnhnrl | 74:d281aaef9766 | 529 | current_state = HEADER_117; |
tnhnrl | 74:d281aaef9766 | 530 | transmitPacketNumber(input_packet); |
tnhnrl | 74:d281aaef9766 | 531 | break; |
tnhnrl | 74:d281aaef9766 | 532 | |
tnhnrl | 74:d281aaef9766 | 533 | case END_TX_1: |
tnhnrl | 74:d281aaef9766 | 534 | //xbee().printf("END_TX_1\n\r"); |
tnhnrl | 74:d281aaef9766 | 535 | current_state = END_TX_2; |
tnhnrl | 74:d281aaef9766 | 536 | break; |
tnhnrl | 74:d281aaef9766 | 537 | case END_TX_2: |
tnhnrl | 74:d281aaef9766 | 538 | //xbee().printf("END_TX_2\n\r"); |
tnhnrl | 74:d281aaef9766 | 539 | current_state = HEADER_117; |
tnhnrl | 74:d281aaef9766 | 540 | active_loop = false; |
tnhnrl | 74:d281aaef9766 | 541 | break; |
tnhnrl | 73:f6f378311c8d | 542 | |
tnhnrl | 74:d281aaef9766 | 543 | default: |
tnhnrl | 74:d281aaef9766 | 544 | //reset state |
tnhnrl | 74:d281aaef9766 | 545 | //xbee().printf("DEFAULT. HEADER_117\n\r"); |
tnhnrl | 74:d281aaef9766 | 546 | current_state = HEADER_117; //reset here |
tnhnrl | 74:d281aaef9766 | 547 | break; |
tnhnrl | 73:f6f378311c8d | 548 | |
tnhnrl | 73:f6f378311c8d | 549 | } |
tnhnrl | 73:f6f378311c8d | 550 | } |
tnhnrl | 73:f6f378311c8d | 551 | |
tnhnrl | 73:f6f378311c8d | 552 | //CLOSE THE FILE |
tnhnrl | 74:d281aaef9766 | 553 | closeLogFile(); |
tnhnrl | 74:d281aaef9766 | 554 | xbee().printf("08/05/2018 CLOSE THE LOG FILE\n\r"); |
tnhnrl | 74:d281aaef9766 | 555 | |
tnhnrl | 74:d281aaef9766 | 556 | //RESET THE STATE |
tnhnrl | 74:d281aaef9766 | 557 | //current_state = HEADER_117; |
tnhnrl | 73:f6f378311c8d | 558 | } |
tnhnrl | 73:f6f378311c8d | 559 | |
tnhnrl | 73:f6f378311c8d | 560 | void MbedLogger::checkForPythonTransmitRequest() { |
tnhnrl | 74:d281aaef9766 | 561 | //xbee().printf("checkForPythonTransmitRequest\n"); |
tnhnrl | 73:f6f378311c8d | 562 | |
tnhnrl | 74:d281aaef9766 | 563 | if ( xbee().readable() ) { |
tnhnrl | 73:f6f378311c8d | 564 | |
tnhnrl | 73:f6f378311c8d | 565 | //PC READABLE DOES NOT WORK HERE?! |
tnhnrl | 73:f6f378311c8d | 566 | |
tnhnrl | 73:f6f378311c8d | 567 | led2() = !led2(); |
tnhnrl | 63:6cb0405fc6e6 | 568 | led3() = !led3(); |
tnhnrl | 62:d502889e74f1 | 569 | |
tnhnrl | 74:d281aaef9766 | 570 | xbee().printf("########################\n"); |
tnhnrl | 74:d281aaef9766 | 571 | //xbee().printf("%d", xbee().getc()); |
tnhnrl | 73:f6f378311c8d | 572 | } |
tnhnrl | 73:f6f378311c8d | 573 | |
tnhnrl | 73:f6f378311c8d | 574 | // static int transmit_state = HEADER_117; //state in switch statement |
tnhnrl | 73:f6f378311c8d | 575 | // //int incoming_byte = -1; //reset each time a character is read |
tnhnrl | 73:f6f378311c8d | 576 | // int requested_packet_number = -1; //reset each time a character is read |
tnhnrl | 73:f6f378311c8d | 577 | // static int input_packet[4]; //changed from char in previous iteration 03/28/2018 |
tnhnrl | 73:f6f378311c8d | 578 | // static int transmit_crc_one = 0; //hold crc values until they're reset with calculations |
tnhnrl | 73:f6f378311c8d | 579 | // static int transmit_crc_two = 0; |
tnhnrl | 73:f6f378311c8d | 580 | // |
tnhnrl | 73:f6f378311c8d | 581 | // static int incoming_byte[6]; |
tnhnrl | 73:f6f378311c8d | 582 | // |
tnhnrl | 73:f6f378311c8d | 583 | // static int bytes_received = 0; |
tnhnrl | 73:f6f378311c8d | 584 | // |
tnhnrl | 73:f6f378311c8d | 585 | // int req_packet_number = -1; |
tnhnrl | 73:f6f378311c8d | 586 | // |
tnhnrl | 74:d281aaef9766 | 587 | // if (xbee().readable()) { |
tnhnrl | 74:d281aaef9766 | 588 | // incoming_byte[bytes_received] = xbee().getc(); |
tnhnrl | 74:d281aaef9766 | 589 | // xbee().printf("<%d> ", incoming_byte[bytes_received]); |
tnhnrl | 73:f6f378311c8d | 590 | // |
tnhnrl | 73:f6f378311c8d | 591 | // bytes_received++; |
tnhnrl | 73:f6f378311c8d | 592 | // |
tnhnrl | 73:f6f378311c8d | 593 | // if (bytes_received > 5) { |
tnhnrl | 73:f6f378311c8d | 594 | // |
tnhnrl | 73:f6f378311c8d | 595 | // req_packet_number = incoming_byte[2] * 256 + incoming_byte[3]; |
tnhnrl | 73:f6f378311c8d | 596 | // |
tnhnrl | 74:d281aaef9766 | 597 | // xbee().printf("req_packet_number = %d\n\r", req_packet_number); |
tnhnrl | 73:f6f378311c8d | 598 | // |
tnhnrl | 73:f6f378311c8d | 599 | // //reset |
tnhnrl | 73:f6f378311c8d | 600 | // bytes_received = 0; |
tnhnrl | 73:f6f378311c8d | 601 | // } |
tnhnrl | 73:f6f378311c8d | 602 | // } |
tnhnrl | 73:f6f378311c8d | 603 | // |
tnhnrl | 73:f6f378311c8d | 604 | // //setTransmitPacketNumber(0); |
tnhnrl | 73:f6f378311c8d | 605 | // |
tnhnrl | 73:f6f378311c8d | 606 | // /* OPEN THE FILE */ |
tnhnrl | 73:f6f378311c8d | 607 | // string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 73:f6f378311c8d | 608 | //_fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 73:f6f378311c8d | 609 | |
tnhnrl | 73:f6f378311c8d | 610 | /* RECEIVE CORRECT CHECKSUM, SEND PACKET */ |
tnhnrl | 73:f6f378311c8d | 611 | //transmitPacketNumber(req_packet_number); |
tnhnrl | 73:f6f378311c8d | 612 | |
tnhnrl | 73:f6f378311c8d | 613 | //fclose(_fp); |
tnhnrl | 73:f6f378311c8d | 614 | |
tnhnrl | 73:f6f378311c8d | 615 | // switch(transmit_state) { |
tnhnrl | 73:f6f378311c8d | 616 | // case HEADER_117: |
tnhnrl | 73:f6f378311c8d | 617 | // led3() = !led3(); |
tnhnrl | 73:f6f378311c8d | 618 | // |
tnhnrl | 73:f6f378311c8d | 619 | // //continue processing |
tnhnrl | 73:f6f378311c8d | 620 | // if (incoming_byte == 117){ //"u" |
tnhnrl | 73:f6f378311c8d | 621 | // transmit_state = HEADER_101; |
tnhnrl | 73:f6f378311c8d | 622 | // } |
tnhnrl | 73:f6f378311c8d | 623 | // //did not receive byte 1 |
tnhnrl | 73:f6f378311c8d | 624 | // else { |
tnhnrl | 73:f6f378311c8d | 625 | // transmit_state = HEADER_117; //go back to checking the first packet |
tnhnrl | 73:f6f378311c8d | 626 | // } |
tnhnrl | 73:f6f378311c8d | 627 | // break; |
tnhnrl | 73:f6f378311c8d | 628 | // |
tnhnrl | 73:f6f378311c8d | 629 | // case HEADER_101: |
tnhnrl | 73:f6f378311c8d | 630 | // if(incoming_byte == 101) { //"e" |
tnhnrl | 73:f6f378311c8d | 631 | // transmit_state = TRANSMIT_PACKET_1; |
tnhnrl | 74:d281aaef9766 | 632 | // //xbee().printf(" U E \n\r"); |
tnhnrl | 73:f6f378311c8d | 633 | // } |
tnhnrl | 73:f6f378311c8d | 634 | // else { |
tnhnrl | 73:f6f378311c8d | 635 | // transmit_state = HEADER_117; |
tnhnrl | 73:f6f378311c8d | 636 | // } |
tnhnrl | 73:f6f378311c8d | 637 | // break; |
tnhnrl | 73:f6f378311c8d | 638 | // |
tnhnrl | 73:f6f378311c8d | 639 | // case TRANSMIT_PACKET_1: |
tnhnrl | 73:f6f378311c8d | 640 | // if (incoming_byte >= 0) { |
tnhnrl | 73:f6f378311c8d | 641 | // input_packet[0] = 117; |
tnhnrl | 73:f6f378311c8d | 642 | // input_packet[1] = 101; |
tnhnrl | 73:f6f378311c8d | 643 | // input_packet[2] = incoming_byte; |
tnhnrl | 73:f6f378311c8d | 644 | // |
tnhnrl | 73:f6f378311c8d | 645 | // transmit_state = TRANSMIT_PACKET_2; |
tnhnrl | 73:f6f378311c8d | 646 | // //_reply_byte3 = incoming_byte; |
tnhnrl | 73:f6f378311c8d | 647 | // |
tnhnrl | 73:f6f378311c8d | 648 | // led1() = !led1(); |
tnhnrl | 74:d281aaef9766 | 649 | // //xbee().printf(" T P \n\r"); |
tnhnrl | 74:d281aaef9766 | 650 | // //xbee().printf("DEBUG: Transmit Packet %d\n\r", incoming_byte); |
tnhnrl | 73:f6f378311c8d | 651 | // } |
tnhnrl | 73:f6f378311c8d | 652 | // break; |
tnhnrl | 73:f6f378311c8d | 653 | // |
tnhnrl | 73:f6f378311c8d | 654 | // case TRANSMIT_PACKET_2: |
tnhnrl | 73:f6f378311c8d | 655 | // |
tnhnrl | 73:f6f378311c8d | 656 | // if (incoming_byte >= 0) { |
tnhnrl | 73:f6f378311c8d | 657 | // input_packet[3] = incoming_byte; |
tnhnrl | 73:f6f378311c8d | 658 | // //_reply_byte4 = incoming_byte; |
tnhnrl | 73:f6f378311c8d | 659 | // } |
tnhnrl | 73:f6f378311c8d | 660 | // transmit_state = PACKET_CRC_ONE; |
tnhnrl | 73:f6f378311c8d | 661 | // |
tnhnrl | 73:f6f378311c8d | 662 | // break; |
tnhnrl | 73:f6f378311c8d | 663 | // |
tnhnrl | 73:f6f378311c8d | 664 | // case (PACKET_CRC_ONE): |
tnhnrl | 73:f6f378311c8d | 665 | // transmit_crc_one = calcCrcOneArray(input_packet, 4); //calc CRC 1 from the input packet (size 4) |
tnhnrl | 73:f6f378311c8d | 666 | // |
tnhnrl | 73:f6f378311c8d | 667 | // if (incoming_byte == transmit_crc_one) { |
tnhnrl | 73:f6f378311c8d | 668 | // transmit_state = PACKET_CRC_TWO; |
tnhnrl | 73:f6f378311c8d | 669 | // } |
tnhnrl | 73:f6f378311c8d | 670 | // |
tnhnrl | 73:f6f378311c8d | 671 | // else |
tnhnrl | 73:f6f378311c8d | 672 | // transmit_state = HEADER_117; |
tnhnrl | 73:f6f378311c8d | 673 | // //or state remains the same? |
tnhnrl | 73:f6f378311c8d | 674 | // |
tnhnrl | 73:f6f378311c8d | 675 | // break; |
tnhnrl | 73:f6f378311c8d | 676 | // |
tnhnrl | 73:f6f378311c8d | 677 | // case (PACKET_CRC_TWO): |
tnhnrl | 73:f6f378311c8d | 678 | // transmit_state = HEADER_117; |
tnhnrl | 73:f6f378311c8d | 679 | // transmit_crc_two = calcCrcTwoArray(input_packet, 4); //calc CRC 2 from the input packet (size 4) |
tnhnrl | 73:f6f378311c8d | 680 | // |
tnhnrl | 73:f6f378311c8d | 681 | // //check if CRC TWO is correct (then send full packet) |
tnhnrl | 73:f6f378311c8d | 682 | // if (incoming_byte == transmit_crc_two) { |
tnhnrl | 73:f6f378311c8d | 683 | // requested_packet_number = input_packet[2] * 256 + input_packet[3]; //compute the numbers 0 through 65535 with the two bytes |
tnhnrl | 73:f6f378311c8d | 684 | // |
tnhnrl | 73:f6f378311c8d | 685 | // //receive correct checksum, immediately send this packet |
tnhnrl | 73:f6f378311c8d | 686 | // transmitPacketNumber(requested_packet_number); |
tnhnrl | 73:f6f378311c8d | 687 | // |
tnhnrl | 73:f6f378311c8d | 688 | // //fseek(_fp, 0, SEEK_END); //reset _fp (this was causing errors with the other function) |
tnhnrl | 73:f6f378311c8d | 689 | // led3() = !led3(); |
tnhnrl | 73:f6f378311c8d | 690 | // |
tnhnrl | 73:f6f378311c8d | 691 | // } //end of checksum (incoming_byte) if statement |
tnhnrl | 73:f6f378311c8d | 692 | // |
tnhnrl | 73:f6f378311c8d | 693 | // break; |
tnhnrl | 73:f6f378311c8d | 694 | // } //switch statement complete |
tnhnrl | 73:f6f378311c8d | 695 | // } //while statement complete |
tnhnrl | 62:d502889e74f1 | 696 | } |
tnhnrl | 62:d502889e74f1 | 697 | |
tnhnrl | 62:d502889e74f1 | 698 | int MbedLogger::readTransmitPacket() { |
tnhnrl | 62:d502889e74f1 | 699 | _file_transmission = true; |
tnhnrl | 62:d502889e74f1 | 700 | _file_transmission_state = 0; |
tnhnrl | 62:d502889e74f1 | 701 | |
tnhnrl | 62:d502889e74f1 | 702 | //first check if you're receiving data, then read four bytes |
tnhnrl | 62:d502889e74f1 | 703 | |
tnhnrl | 62:d502889e74f1 | 704 | int transmit_state = 0; //for state machine |
tnhnrl | 62:d502889e74f1 | 705 | |
tnhnrl | 62:d502889e74f1 | 706 | int incoming_byte = -1; |
tnhnrl | 62:d502889e74f1 | 707 | |
tnhnrl | 62:d502889e74f1 | 708 | int requested_packet_number = -1; |
tnhnrl | 62:d502889e74f1 | 709 | |
tnhnrl | 62:d502889e74f1 | 710 | static int inside_while_loop = 1; |
tnhnrl | 62:d502889e74f1 | 711 | |
tnhnrl | 62:d502889e74f1 | 712 | while (inside_while_loop) { |
tnhnrl | 74:d281aaef9766 | 713 | if (xbee().readable()) { //don't rely on pc readable being open all the time |
tnhnrl | 62:d502889e74f1 | 714 | |
tnhnrl | 74:d281aaef9766 | 715 | incoming_byte = xbee().getc(); |
tnhnrl | 62:d502889e74f1 | 716 | |
tnhnrl | 62:d502889e74f1 | 717 | switch(transmit_state) { |
tnhnrl | 62:d502889e74f1 | 718 | |
tnhnrl | 62:d502889e74f1 | 719 | case (HEADER_117): |
tnhnrl | 62:d502889e74f1 | 720 | //continue processing |
tnhnrl | 62:d502889e74f1 | 721 | if (incoming_byte == 117){ |
tnhnrl | 62:d502889e74f1 | 722 | transmit_state = HEADER_101; |
tnhnrl | 62:d502889e74f1 | 723 | } |
tnhnrl | 62:d502889e74f1 | 724 | //end transmission |
tnhnrl | 62:d502889e74f1 | 725 | else if (incoming_byte == 16) { |
tnhnrl | 62:d502889e74f1 | 726 | transmit_state = END_TRANSMISSION; |
tnhnrl | 62:d502889e74f1 | 727 | } |
tnhnrl | 62:d502889e74f1 | 728 | else { |
tnhnrl | 62:d502889e74f1 | 729 | transmit_state = HEADER_117; |
tnhnrl | 62:d502889e74f1 | 730 | } |
tnhnrl | 62:d502889e74f1 | 731 | break; |
tnhnrl | 62:d502889e74f1 | 732 | |
tnhnrl | 62:d502889e74f1 | 733 | case (HEADER_101): |
tnhnrl | 62:d502889e74f1 | 734 | if(incoming_byte == 101) { |
tnhnrl | 62:d502889e74f1 | 735 | transmit_state = TRANSMIT_PACKET_1; |
tnhnrl | 62:d502889e74f1 | 736 | } |
tnhnrl | 62:d502889e74f1 | 737 | break; |
tnhnrl | 62:d502889e74f1 | 738 | |
tnhnrl | 62:d502889e74f1 | 739 | case (TRANSMIT_PACKET_1): |
tnhnrl | 62:d502889e74f1 | 740 | if (incoming_byte >= 0) { |
tnhnrl | 62:d502889e74f1 | 741 | transmit_state = TRANSMIT_PACKET_2; |
tnhnrl | 62:d502889e74f1 | 742 | _reply_byte3 = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 743 | } |
tnhnrl | 62:d502889e74f1 | 744 | break; |
tnhnrl | 62:d502889e74f1 | 745 | |
tnhnrl | 62:d502889e74f1 | 746 | case (TRANSMIT_PACKET_2): |
tnhnrl | 62:d502889e74f1 | 747 | if (incoming_byte >= 0) { |
tnhnrl | 62:d502889e74f1 | 748 | _reply_byte4 = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 749 | } |
tnhnrl | 62:d502889e74f1 | 750 | |
tnhnrl | 62:d502889e74f1 | 751 | requested_packet_number = _reply_byte3 * 256 + _reply_byte4; //compute the numbers 0 through 65535 with the two bytes |
tnhnrl | 62:d502889e74f1 | 752 | |
tnhnrl | 62:d502889e74f1 | 753 | if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE! |
tnhnrl | 62:d502889e74f1 | 754 | //MUST SET THE PACKET NUMBER |
tnhnrl | 62:d502889e74f1 | 755 | _packet_number = requested_packet_number; |
tnhnrl | 62:d502889e74f1 | 756 | |
tnhnrl | 62:d502889e74f1 | 757 | readPacketInSeries(); |
tnhnrl | 62:d502889e74f1 | 758 | createDataPacket(); |
tnhnrl | 62:d502889e74f1 | 759 | _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created |
tnhnrl | 62:d502889e74f1 | 760 | } |
tnhnrl | 62:d502889e74f1 | 761 | |
tnhnrl | 62:d502889e74f1 | 762 | //continuously transmit current packet until it tells you to do the next packet (response from Python program) |
tnhnrl | 62:d502889e74f1 | 763 | transmitDataPacket(); |
tnhnrl | 62:d502889e74f1 | 764 | |
tnhnrl | 62:d502889e74f1 | 765 | //TRANSMIT_PACKET_2 |
tnhnrl | 62:d502889e74f1 | 766 | inside_while_loop = 0; //exit the while loop with this |
tnhnrl | 74:d281aaef9766 | 767 | xbee().printf("(TRANSMIT_PACKET_2)reached inside_while_loop = 0\n\r"); //DEBUG |
tnhnrl | 62:d502889e74f1 | 768 | break; |
tnhnrl | 62:d502889e74f1 | 769 | } //end of switch statement |
tnhnrl | 62:d502889e74f1 | 770 | } //end of while loop |
tnhnrl | 62:d502889e74f1 | 771 | |
tnhnrl | 62:d502889e74f1 | 772 | // else { |
tnhnrl | 74:d281aaef9766 | 773 | // //xbee().printf("pc not readable \n\r"); |
tnhnrl | 62:d502889e74f1 | 774 | // } |
tnhnrl | 62:d502889e74f1 | 775 | // |
tnhnrl | 62:d502889e74f1 | 776 | } |
tnhnrl | 62:d502889e74f1 | 777 | |
tnhnrl | 62:d502889e74f1 | 778 | //once you're outside of the while loop |
tnhnrl | 62:d502889e74f1 | 779 | inside_while_loop = true; //for next iteration |
tnhnrl | 62:d502889e74f1 | 780 | |
tnhnrl | 74:d281aaef9766 | 781 | xbee().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r"); |
tnhnrl | 62:d502889e74f1 | 782 | return false; |
tnhnrl | 62:d502889e74f1 | 783 | } |
tnhnrl | 62:d502889e74f1 | 784 | |
tnhnrl | 73:f6f378311c8d | 785 | bool MbedLogger::endTransmitPacket() { |
tnhnrl | 73:f6f378311c8d | 786 | |
tnhnrl | 73:f6f378311c8d | 787 | int incoming_byte; |
tnhnrl | 73:f6f378311c8d | 788 | |
tnhnrl | 74:d281aaef9766 | 789 | while (xbee().readable()) { |
tnhnrl | 74:d281aaef9766 | 790 | incoming_byte = xbee().getc(); //get first byte |
tnhnrl | 73:f6f378311c8d | 791 | |
tnhnrl | 73:f6f378311c8d | 792 | if (incoming_byte == 16) { |
tnhnrl | 74:d281aaef9766 | 793 | incoming_byte = xbee().getc(); //get second byte |
tnhnrl | 73:f6f378311c8d | 794 | return true; |
tnhnrl | 73:f6f378311c8d | 795 | } |
tnhnrl | 73:f6f378311c8d | 796 | //quick and dirty |
tnhnrl | 73:f6f378311c8d | 797 | |
tnhnrl | 73:f6f378311c8d | 798 | } |
tnhnrl | 73:f6f378311c8d | 799 | |
tnhnrl | 73:f6f378311c8d | 800 | return false; |
tnhnrl | 73:f6f378311c8d | 801 | } |
tnhnrl | 73:f6f378311c8d | 802 | |
tnhnrl | 62:d502889e74f1 | 803 | void MbedLogger::reOpenLineReader() { |
tnhnrl | 62:d502889e74f1 | 804 | //open a new one |
tnhnrl | 62:d502889e74f1 | 805 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 806 | |
tnhnrl | 62:d502889e74f1 | 807 | _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read |
tnhnrl | 62:d502889e74f1 | 808 | |
tnhnrl | 62:d502889e74f1 | 809 | //check if this actually worked... |
tnhnrl | 62:d502889e74f1 | 810 | if (!_fp) { |
tnhnrl | 74:d281aaef9766 | 811 | xbee().printf("ERROR: Log file could not be opened\n\r"); |
tnhnrl | 62:d502889e74f1 | 812 | } |
tnhnrl | 62:d502889e74f1 | 813 | else { |
tnhnrl | 74:d281aaef9766 | 814 | xbee().printf("Current Log file (LOG000.csv) was opened.\n\r"); |
tnhnrl | 62:d502889e74f1 | 815 | } |
tnhnrl | 62:d502889e74f1 | 816 | } |
tnhnrl | 62:d502889e74f1 | 817 | |
tnhnrl | 62:d502889e74f1 | 818 | bool MbedLogger::openLineReader() { |
tnhnrl | 62:d502889e74f1 | 819 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 820 | |
tnhnrl | 62:d502889e74f1 | 821 | _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read |
tnhnrl | 62:d502889e74f1 | 822 | |
tnhnrl | 62:d502889e74f1 | 823 | //check if this actually worked... |
tnhnrl | 62:d502889e74f1 | 824 | if (!_fp) { |
tnhnrl | 74:d281aaef9766 | 825 | //xbee().printf("ERROR: Log file could not be opened\n\r"); |
tnhnrl | 62:d502889e74f1 | 826 | return false; |
tnhnrl | 62:d502889e74f1 | 827 | } |
tnhnrl | 62:d502889e74f1 | 828 | else { |
tnhnrl | 74:d281aaef9766 | 829 | //xbee().printf("Current Log file (LOG000.csv) was opened.\n\r"); |
tnhnrl | 62:d502889e74f1 | 830 | return true; |
tnhnrl | 62:d502889e74f1 | 831 | } |
tnhnrl | 62:d502889e74f1 | 832 | } |
tnhnrl | 62:d502889e74f1 | 833 | |
tnhnrl | 62:d502889e74f1 | 834 | //VERIFIED THIS IS WORKING |
tnhnrl | 62:d502889e74f1 | 835 | void MbedLogger::readPacketInSeries(){ |
tnhnrl | 62:d502889e74f1 | 836 | 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 | 62:d502889e74f1 | 837 | |
tnhnrl | 62:d502889e74f1 | 838 | //read the current line in the file |
tnhnrl | 62:d502889e74f1 | 839 | fgets(_line_buffer, 256, _fp); //reads the line of characters until you reach a newline character |
tnhnrl | 62:d502889e74f1 | 840 | |
tnhnrl | 62:d502889e74f1 | 841 | //RECORD THE STRING LENGTH |
tnhnrl | 62:d502889e74f1 | 842 | _current_line_length = strlen(_line_buffer); |
tnhnrl | 62:d502889e74f1 | 843 | } |
tnhnrl | 62:d502889e74f1 | 844 | |
tnhnrl | 69:919ac8d7e023 | 845 | int MbedLogger::getNumberOfPacketsInCurrentLog() { |
tnhnrl | 73:f6f378311c8d | 846 | //takes less than a second to complete, verified 7/24/2018 |
tnhnrl | 73:f6f378311c8d | 847 | |
tnhnrl | 73:f6f378311c8d | 848 | //open the file |
tnhnrl | 73:f6f378311c8d | 849 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 73:f6f378311c8d | 850 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 62:d502889e74f1 | 851 | |
tnhnrl | 73:f6f378311c8d | 852 | fseek(_fp, 0L, SEEK_END); |
tnhnrl | 73:f6f378311c8d | 853 | |
tnhnrl | 73:f6f378311c8d | 854 | size_t size = ftell(_fp); |
tnhnrl | 62:d502889e74f1 | 855 | |
tnhnrl | 62:d502889e74f1 | 856 | //move the FILE pointer back to the start |
tnhnrl | 62:d502889e74f1 | 857 | fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file |
tnhnrl | 62:d502889e74f1 | 858 | |
tnhnrl | 73:f6f378311c8d | 859 | _total_number_of_packets = size/254; |
tnhnrl | 69:919ac8d7e023 | 860 | |
tnhnrl | 74:d281aaef9766 | 861 | //CLOSE THE FILE |
tnhnrl | 74:d281aaef9766 | 862 | closeLogFile(); |
tnhnrl | 74:d281aaef9766 | 863 | |
tnhnrl | 69:919ac8d7e023 | 864 | return _total_number_of_packets; |
tnhnrl | 62:d502889e74f1 | 865 | } |
tnhnrl | 62:d502889e74f1 | 866 | |
tnhnrl | 62:d502889e74f1 | 867 | void MbedLogger::endTransmissionCloseFile() { |
tnhnrl | 62:d502889e74f1 | 868 | // if the file pointer is null, the file was not opened in the first place |
tnhnrl | 62:d502889e74f1 | 869 | if (!_fp) { |
tnhnrl | 74:d281aaef9766 | 870 | xbee().printf("\n endTransmissionCloseFile: FILE WAS NOT OPENED!\n\r"); |
tnhnrl | 62:d502889e74f1 | 871 | } |
tnhnrl | 62:d502889e74f1 | 872 | else { |
tnhnrl | 74:d281aaef9766 | 873 | xbee().printf("\n endTransmissionCloseFile: FILE FOUND AND CLOSED!\n\r"); |
tnhnrl | 62:d502889e74f1 | 874 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 875 | } |
tnhnrl | 62:d502889e74f1 | 876 | |
tnhnrl | 62:d502889e74f1 | 877 | _file_transmission = false; |
tnhnrl | 62:d502889e74f1 | 878 | } |
tnhnrl | 62:d502889e74f1 | 879 | |
tnhnrl | 62:d502889e74f1 | 880 | void MbedLogger::openWriteFile() { |
tnhnrl | 74:d281aaef9766 | 881 | xbee().printf("Opening file for reception.\n\r"); |
tnhnrl | 62:d502889e74f1 | 882 | |
tnhnrl | 62:d502889e74f1 | 883 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 884 | |
tnhnrl | 62:d502889e74f1 | 885 | _fp = fopen(file_name_string.c_str(), "w"); |
tnhnrl | 62:d502889e74f1 | 886 | } |
tnhnrl | 62:d502889e74f1 | 887 | |
tnhnrl | 62:d502889e74f1 | 888 | |
tnhnrl | 62:d502889e74f1 | 889 | //weird bug noticed on 5/25/2018 where if you're not sending data the function is not completing |
tnhnrl | 62:d502889e74f1 | 890 | |
tnhnrl | 62:d502889e74f1 | 891 | |
tnhnrl | 62:d502889e74f1 | 892 | |
tnhnrl | 62:d502889e74f1 | 893 | |
tnhnrl | 62:d502889e74f1 | 894 | // function checks for incoming data (receiver function) from a Python program that transmits a file |
tnhnrl | 62:d502889e74f1 | 895 | // current limit is a file that has 255 lines of data |
tnhnrl | 68:8f549749b8ce | 896 | bool MbedLogger::checkForIncomingData() { |
tnhnrl | 62:d502889e74f1 | 897 | int receive_packet_number; |
tnhnrl | 62:d502889e74f1 | 898 | int receive_total_number_packets; |
tnhnrl | 62:d502889e74f1 | 899 | int receive_packet_size; |
tnhnrl | 62:d502889e74f1 | 900 | |
tnhnrl | 62:d502889e74f1 | 901 | bool data_transmission_complete = false; |
tnhnrl | 62:d502889e74f1 | 902 | |
tnhnrl | 62:d502889e74f1 | 903 | int incoming_byte; |
tnhnrl | 62:d502889e74f1 | 904 | |
tnhnrl | 62:d502889e74f1 | 905 | char char_buffer[256] = {}; //create empty buffer |
tnhnrl | 62:d502889e74f1 | 906 | |
tnhnrl | 62:d502889e74f1 | 907 | //starting state |
tnhnrl | 62:d502889e74f1 | 908 | int process_state = HEADER_117; |
tnhnrl | 62:d502889e74f1 | 909 | |
tnhnrl | 62:d502889e74f1 | 910 | //variables for processing data below |
tnhnrl | 62:d502889e74f1 | 911 | int checksum_one = -1; |
tnhnrl | 62:d502889e74f1 | 912 | int checksum_two = -1; |
tnhnrl | 62:d502889e74f1 | 913 | |
tnhnrl | 62:d502889e74f1 | 914 | int i = 5; |
tnhnrl | 62:d502889e74f1 | 915 | int serial_timeout = 0; |
tnhnrl | 62:d502889e74f1 | 916 | |
tnhnrl | 74:d281aaef9766 | 917 | while (xbee().readable() && !data_transmission_complete) { |
tnhnrl | 74:d281aaef9766 | 918 | incoming_byte = xbee().getc(); //getc returns an unsigned char cast to an int |
tnhnrl | 74:d281aaef9766 | 919 | //xbee().printf("DEBUG: State 0\n\r"); |
tnhnrl | 62:d502889e74f1 | 920 | |
tnhnrl | 62:d502889e74f1 | 921 | switch(process_state) { |
tnhnrl | 62:d502889e74f1 | 922 | case HEADER_117: |
tnhnrl | 62:d502889e74f1 | 923 | //continue processing |
tnhnrl | 62:d502889e74f1 | 924 | if (incoming_byte == 117){ |
tnhnrl | 62:d502889e74f1 | 925 | process_state = HEADER_101; |
tnhnrl | 74:d281aaef9766 | 926 | //xbee().printf("DEBUG: Case 117\n\r"); |
tnhnrl | 62:d502889e74f1 | 927 | } |
tnhnrl | 62:d502889e74f1 | 928 | //end transmission |
tnhnrl | 62:d502889e74f1 | 929 | else if (incoming_byte == 16) { |
tnhnrl | 62:d502889e74f1 | 930 | process_state = END_TRANSMISSION; |
tnhnrl | 74:d281aaef9766 | 931 | //xbee().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r"); |
tnhnrl | 62:d502889e74f1 | 932 | } |
tnhnrl | 62:d502889e74f1 | 933 | else { |
tnhnrl | 62:d502889e74f1 | 934 | process_state = HEADER_117; // ??? |
tnhnrl | 74:d281aaef9766 | 935 | //xbee().printf("DEBUG: State Header 117\n\r"); |
tnhnrl | 62:d502889e74f1 | 936 | } |
tnhnrl | 62:d502889e74f1 | 937 | break; |
tnhnrl | 62:d502889e74f1 | 938 | |
tnhnrl | 62:d502889e74f1 | 939 | case HEADER_101: |
tnhnrl | 62:d502889e74f1 | 940 | if(incoming_byte == 101) { |
tnhnrl | 62:d502889e74f1 | 941 | process_state = PACKET_NUM; |
tnhnrl | 74:d281aaef9766 | 942 | //xbee().printf("DEBUG: Case 101\n\r"); |
tnhnrl | 62:d502889e74f1 | 943 | } |
tnhnrl | 62:d502889e74f1 | 944 | break; |
tnhnrl | 62:d502889e74f1 | 945 | |
tnhnrl | 62:d502889e74f1 | 946 | case PACKET_NUM: |
tnhnrl | 62:d502889e74f1 | 947 | receive_packet_number = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 948 | process_state = TOTAL_NUM_PACKETS; |
tnhnrl | 74:d281aaef9766 | 949 | //xbee().printf("DEBUG: Case PACKET_NUM\n\r"); |
tnhnrl | 62:d502889e74f1 | 950 | break; |
tnhnrl | 62:d502889e74f1 | 951 | |
tnhnrl | 62:d502889e74f1 | 952 | case TOTAL_NUM_PACKETS: |
tnhnrl | 62:d502889e74f1 | 953 | receive_total_number_packets = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 954 | process_state = PACKET_SIZE; |
tnhnrl | 74:d281aaef9766 | 955 | //xbee().printf("DEBUG: Case TOTAL_NUM_PACKETS\n\r"); |
tnhnrl | 62:d502889e74f1 | 956 | break; |
tnhnrl | 62:d502889e74f1 | 957 | |
tnhnrl | 62:d502889e74f1 | 958 | case PACKET_SIZE: |
tnhnrl | 62:d502889e74f1 | 959 | receive_packet_size = incoming_byte; |
tnhnrl | 74:d281aaef9766 | 960 | //xbee().printf("DEBUG: Case PACKET_SIZE\n\r"); |
tnhnrl | 62:d502889e74f1 | 961 | |
tnhnrl | 62:d502889e74f1 | 962 | //write the header stuff to it |
tnhnrl | 62:d502889e74f1 | 963 | char_buffer[0] = 117; |
tnhnrl | 62:d502889e74f1 | 964 | char_buffer[1] = 101; |
tnhnrl | 62:d502889e74f1 | 965 | char_buffer[2] = receive_packet_number; |
tnhnrl | 62:d502889e74f1 | 966 | char_buffer[3] = receive_total_number_packets; |
tnhnrl | 62:d502889e74f1 | 967 | char_buffer[4] = receive_packet_size; |
tnhnrl | 62:d502889e74f1 | 968 | |
tnhnrl | 62:d502889e74f1 | 969 | // tests confirmed that packet number is zero, number of packets is 12, packet size is 12 |
tnhnrl | 74:d281aaef9766 | 970 | //xbee().printf("char_buffer 2/3/4: %d %d %d\n\r", receive_packet_number,receive_total_number_packets,receive_packet_size); |
tnhnrl | 68:8f549749b8ce | 971 | |
tnhnrl | 62:d502889e74f1 | 972 | //process packet data, future version will append for larger data sizes, 0xFFFF |
tnhnrl | 62:d502889e74f1 | 973 | |
tnhnrl | 62:d502889e74f1 | 974 | // IF YOU GET AN INTERRUPTED DATA STREAM YOU NEED TO BREAK OUT OF THE LOOP |
tnhnrl | 62:d502889e74f1 | 975 | i = 5; |
tnhnrl | 62:d502889e74f1 | 976 | serial_timeout = 0; |
tnhnrl | 62:d502889e74f1 | 977 | |
tnhnrl | 62:d502889e74f1 | 978 | while (true) { |
tnhnrl | 74:d281aaef9766 | 979 | if (xbee().readable()) { |
tnhnrl | 74:d281aaef9766 | 980 | char_buffer[i] = xbee().getc(); //read all of the data packets |
tnhnrl | 62:d502889e74f1 | 981 | i++; |
tnhnrl | 62:d502889e74f1 | 982 | |
tnhnrl | 62:d502889e74f1 | 983 | serial_timeout = 0; //reset the timeout |
tnhnrl | 62:d502889e74f1 | 984 | } |
tnhnrl | 62:d502889e74f1 | 985 | else { |
tnhnrl | 62:d502889e74f1 | 986 | serial_timeout++; |
tnhnrl | 62:d502889e74f1 | 987 | } |
tnhnrl | 62:d502889e74f1 | 988 | |
tnhnrl | 62:d502889e74f1 | 989 | // When full data packet is received... |
tnhnrl | 62:d502889e74f1 | 990 | if (i >= receive_packet_size+5) { //cannot do this properly with a for loop |
tnhnrl | 62:d502889e74f1 | 991 | //get checksum bytes |
tnhnrl | 74:d281aaef9766 | 992 | checksum_one = xbee().getc(); |
tnhnrl | 74:d281aaef9766 | 993 | checksum_two = xbee().getc(); |
tnhnrl | 62:d502889e74f1 | 994 | |
tnhnrl | 62:d502889e74f1 | 995 | //calculate the CRC from the header and data bytes using _data_packet (vector) |
tnhnrl | 62:d502889e74f1 | 996 | //found out calculating crc with string was dropping empty or null spaces |
tnhnrl | 62:d502889e74f1 | 997 | _data_packet.clear(); //clear it just in case |
tnhnrl | 62:d502889e74f1 | 998 | |
tnhnrl | 62:d502889e74f1 | 999 | for (int a = 0; a < receive_packet_size+5; a++) { |
tnhnrl | 62:d502889e74f1 | 1000 | _data_packet.push_back(char_buffer[a]); //push the character array into the buffer |
tnhnrl | 62:d502889e74f1 | 1001 | } |
tnhnrl | 62:d502889e74f1 | 1002 | |
tnhnrl | 62:d502889e74f1 | 1003 | //calculate the CRC using the vector (strings will cut off null characters) |
tnhnrl | 62:d502889e74f1 | 1004 | int calc_crc_one = calcCrcOne(); |
tnhnrl | 62:d502889e74f1 | 1005 | int calc_crc_two = calcCrcTwo(); |
tnhnrl | 62:d502889e74f1 | 1006 | |
tnhnrl | 74:d281aaef9766 | 1007 | //xbee().printf("DEBUG: calc crc 1: %d, crc 2: %d\n\r", calc_crc_one, calc_crc_two); |
tnhnrl | 62:d502889e74f1 | 1008 | |
tnhnrl | 62:d502889e74f1 | 1009 | // first confirm that the checksum is correct |
tnhnrl | 74:d281aaef9766 | 1010 | if ((calc_crc_one == checksum_one) and (calc_crc_two == checksum_two)) { |
tnhnrl | 74:d281aaef9766 | 1011 | |
tnhnrl | 74:d281aaef9766 | 1012 | //xbee().printf("DEBUG: checksums are good!\n\r"); |
tnhnrl | 74:d281aaef9766 | 1013 | |
tnhnrl | 74:d281aaef9766 | 1014 | //xbee().printf("receive_packet_number %d and _confirmed_packet_number %d\n\r", receive_packet_number, _confirmed_packet_number); //debug |
tnhnrl | 74:d281aaef9766 | 1015 | |
tnhnrl | 74:d281aaef9766 | 1016 | // // CHECKSUM CORRECT & get the filename from the first packet (check that you receive first packet) |
tnhnrl | 74:d281aaef9766 | 1017 | // if (receive_packet_number == 0) { |
tnhnrl | 74:d281aaef9766 | 1018 | // char temp_char[receive_packet_size+1]; //temp array for memcpy |
tnhnrl | 74:d281aaef9766 | 1019 | // memset(&temp_char[0], 0, sizeof(temp_char)); //clear full array (or get random characters) |
tnhnrl | 74:d281aaef9766 | 1020 | // strncpy(temp_char, char_buffer + 5 /* Offset */, receive_packet_size*sizeof(char) /* Length */); //memcpy introduced random characters |
tnhnrl | 74:d281aaef9766 | 1021 | // |
tnhnrl | 74:d281aaef9766 | 1022 | // //xbee().printf("SEQUENCE: <<%s>>\n\r", temp_char]) // ERASE |
tnhnrl | 74:d281aaef9766 | 1023 | // |
tnhnrl | 74:d281aaef9766 | 1024 | // //have to terminate the string with '\0' |
tnhnrl | 74:d281aaef9766 | 1025 | // temp_char[receive_packet_size] = '\0'; |
tnhnrl | 74:d281aaef9766 | 1026 | // |
tnhnrl | 74:d281aaef9766 | 1027 | // //xbee().printf("\n\rDEBUG: ------ Filename? <<%s>>\n\r", temp_char); |
tnhnrl | 74:d281aaef9766 | 1028 | // //_received_filename = temp_char; |
tnhnrl | 74:d281aaef9766 | 1029 | // |
tnhnrl | 74:d281aaef9766 | 1030 | // //xbee().printf("\n\rDEBUG: _received_filename <<%s>>\n\r", _received_filename); |
tnhnrl | 74:d281aaef9766 | 1031 | // |
tnhnrl | 74:d281aaef9766 | 1032 | // //open a file for writing |
tnhnrl | 74:d281aaef9766 | 1033 | // //openReceiveFile(_received_filename); |
tnhnrl | 74:d281aaef9766 | 1034 | // |
tnhnrl | 74:d281aaef9766 | 1035 | // //send a reply to Python transmit program |
tnhnrl | 74:d281aaef9766 | 1036 | // sendReply(); |
tnhnrl | 74:d281aaef9766 | 1037 | // |
tnhnrl | 74:d281aaef9766 | 1038 | // led3() = 1; |
tnhnrl | 74:d281aaef9766 | 1039 | // |
tnhnrl | 74:d281aaef9766 | 1040 | // // even if correct CRC, counter prevents the program from writing the same packet twice |
tnhnrl | 74:d281aaef9766 | 1041 | // _confirmed_packet_number++; |
tnhnrl | 74:d281aaef9766 | 1042 | // } |
tnhnrl | 62:d502889e74f1 | 1043 | |
tnhnrl | 62:d502889e74f1 | 1044 | // check if the packet that you're receiving (receive_packet_number) has been received already... |
tnhnrl | 62:d502889e74f1 | 1045 | |
tnhnrl | 62:d502889e74f1 | 1046 | //CHECKSUM CORRECT & packet numbers that are 1 through N packets |
tnhnrl | 74:d281aaef9766 | 1047 | if (receive_packet_number == _confirmed_packet_number){ |
tnhnrl | 62:d502889e74f1 | 1048 | //save the data (char buffer) to the file if both checksums work... |
tnhnrl | 62:d502889e74f1 | 1049 | |
tnhnrl | 62:d502889e74f1 | 1050 | // when a packet is received (successfully) send a reply |
tnhnrl | 74:d281aaef9766 | 1051 | //sendReply(); |
tnhnrl | 62:d502889e74f1 | 1052 | |
tnhnrl | 62:d502889e74f1 | 1053 | // write correct data to file |
tnhnrl | 62:d502889e74f1 | 1054 | fprintf(_fp, "%s", char_buffer+5); |
tnhnrl | 62:d502889e74f1 | 1055 | |
tnhnrl | 62:d502889e74f1 | 1056 | // even if correct CRC, counter prevents the program from writing the same packet twice |
tnhnrl | 62:d502889e74f1 | 1057 | _confirmed_packet_number++; |
tnhnrl | 62:d502889e74f1 | 1058 | } |
tnhnrl | 62:d502889e74f1 | 1059 | |
tnhnrl | 62:d502889e74f1 | 1060 | //clear the variables |
tnhnrl | 62:d502889e74f1 | 1061 | checksum_one = -1; |
tnhnrl | 62:d502889e74f1 | 1062 | checksum_two = -1; |
tnhnrl | 62:d502889e74f1 | 1063 | } |
tnhnrl | 62:d502889e74f1 | 1064 | |
tnhnrl | 62:d502889e74f1 | 1065 | process_state = HEADER_117; |
tnhnrl | 62:d502889e74f1 | 1066 | |
tnhnrl | 62:d502889e74f1 | 1067 | break; |
tnhnrl | 62:d502889e74f1 | 1068 | } |
tnhnrl | 62:d502889e74f1 | 1069 | |
tnhnrl | 74:d281aaef9766 | 1070 | // //counter breaks out of the loop if no data received |
tnhnrl | 74:d281aaef9766 | 1071 | // if (serial_timeout >= 10000) { |
tnhnrl | 74:d281aaef9766 | 1072 | // //xbee().printf("break serial_timeout %d\n\r", serial_timeout); |
tnhnrl | 74:d281aaef9766 | 1073 | // break; |
tnhnrl | 74:d281aaef9766 | 1074 | // } |
tnhnrl | 62:d502889e74f1 | 1075 | } |
tnhnrl | 62:d502889e74f1 | 1076 | break; |
tnhnrl | 62:d502889e74f1 | 1077 | |
tnhnrl | 62:d502889e74f1 | 1078 | case END_TRANSMISSION: |
tnhnrl | 74:d281aaef9766 | 1079 | if (xbee().getc() == 16) { |
tnhnrl | 74:d281aaef9766 | 1080 | //xbee().printf("DEBUG: END_TRANSMISSION REACHED: 1. \n\r"); |
tnhnrl | 62:d502889e74f1 | 1081 | |
tnhnrl | 74:d281aaef9766 | 1082 | if (xbee().getc() == 16) { |
tnhnrl | 74:d281aaef9766 | 1083 | //xbee().printf("DEBUG: END_TRANSMISSION REACHED: 2. \n\r"); |
tnhnrl | 62:d502889e74f1 | 1084 | |
tnhnrl | 74:d281aaef9766 | 1085 | _end_sequence_transmission = true; |
tnhnrl | 74:d281aaef9766 | 1086 | //endReceiveData(); |
tnhnrl | 62:d502889e74f1 | 1087 | } |
tnhnrl | 62:d502889e74f1 | 1088 | } |
tnhnrl | 62:d502889e74f1 | 1089 | |
tnhnrl | 74:d281aaef9766 | 1090 | //xbee().printf("DEBUG: END_TRANSMISSION REACHED: 5. \n\r"); |
tnhnrl | 62:d502889e74f1 | 1091 | |
tnhnrl | 62:d502889e74f1 | 1092 | //process_state = HEADER_117; //don't do this unless the check is wrong |
tnhnrl | 74:d281aaef9766 | 1093 | //xbee().printf("END_TRANSMISSION process_state is %d\n\r", process_state); //should be 5 (debug) 02/06/2018 |
tnhnrl | 62:d502889e74f1 | 1094 | data_transmission_complete = true; |
tnhnrl | 62:d502889e74f1 | 1095 | break; |
tnhnrl | 62:d502889e74f1 | 1096 | }//END OF SWITCH |
tnhnrl | 62:d502889e74f1 | 1097 | |
tnhnrl | 62:d502889e74f1 | 1098 | if (data_transmission_complete) { |
tnhnrl | 74:d281aaef9766 | 1099 | //xbee().printf("DEBUG: checkForIncomingData data_transmission_complete \n\r"); |
tnhnrl | 62:d502889e74f1 | 1100 | break; //out of while loop |
tnhnrl | 62:d502889e74f1 | 1101 | } |
tnhnrl | 62:d502889e74f1 | 1102 | } // while loop |
tnhnrl | 62:d502889e74f1 | 1103 | |
tnhnrl | 62:d502889e74f1 | 1104 | led3() = !led3(); |
tnhnrl | 62:d502889e74f1 | 1105 | |
tnhnrl | 62:d502889e74f1 | 1106 | if (data_transmission_complete) |
tnhnrl | 62:d502889e74f1 | 1107 | return false; //tell state machine class that this is done (not transmitting, false) |
tnhnrl | 62:d502889e74f1 | 1108 | else { |
tnhnrl | 62:d502889e74f1 | 1109 | return true; |
tnhnrl | 62:d502889e74f1 | 1110 | } |
tnhnrl | 62:d502889e74f1 | 1111 | } |
tnhnrl | 62:d502889e74f1 | 1112 | |
tnhnrl | 62:d502889e74f1 | 1113 | int MbedLogger::sendReply() { |
tnhnrl | 62:d502889e74f1 | 1114 | //being explicit in what's being transmitted |
tnhnrl | 62:d502889e74f1 | 1115 | |
tnhnrl | 62:d502889e74f1 | 1116 | //change this method to be more explicit later |
tnhnrl | 62:d502889e74f1 | 1117 | |
tnhnrl | 62:d502889e74f1 | 1118 | //integer vector _data_packet is used here, cleared fist just in case |
tnhnrl | 62:d502889e74f1 | 1119 | |
tnhnrl | 62:d502889e74f1 | 1120 | _data_packet.clear(); //same data packet for transmission |
tnhnrl | 62:d502889e74f1 | 1121 | _data_packet.push_back(117); |
tnhnrl | 62:d502889e74f1 | 1122 | _data_packet.push_back(101); |
tnhnrl | 62:d502889e74f1 | 1123 | |
tnhnrl | 62:d502889e74f1 | 1124 | //_confirmed_packet_number comes from the packet number that is sent from the Python program |
tnhnrl | 62:d502889e74f1 | 1125 | _data_packet.push_back(_confirmed_packet_number / 256); //packet number only changed when confirmed |
tnhnrl | 62:d502889e74f1 | 1126 | _data_packet.push_back(_confirmed_packet_number % 256); //split into first and second byte |
tnhnrl | 62:d502889e74f1 | 1127 | |
tnhnrl | 62:d502889e74f1 | 1128 | //compute checksums |
tnhnrl | 62:d502889e74f1 | 1129 | |
tnhnrl | 62:d502889e74f1 | 1130 | int receiver_crc_one = calcCrcOne(); |
tnhnrl | 62:d502889e74f1 | 1131 | int receiver_crc_two = calcCrcTwo(); |
tnhnrl | 62:d502889e74f1 | 1132 | |
tnhnrl | 62:d502889e74f1 | 1133 | _data_packet.push_back(receiver_crc_one); |
tnhnrl | 62:d502889e74f1 | 1134 | _data_packet.push_back(receiver_crc_two); |
tnhnrl | 62:d502889e74f1 | 1135 | |
tnhnrl | 62:d502889e74f1 | 1136 | //transmit this packet |
tnhnrl | 62:d502889e74f1 | 1137 | for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) { |
tnhnrl | 74:d281aaef9766 | 1138 | xbee().putc(*_it); //send integers over serial port one byte at a time |
tnhnrl | 62:d502889e74f1 | 1139 | } |
tnhnrl | 62:d502889e74f1 | 1140 | |
tnhnrl | 62:d502889e74f1 | 1141 | //change process methodology later... |
tnhnrl | 62:d502889e74f1 | 1142 | |
tnhnrl | 62:d502889e74f1 | 1143 | return _confirmed_packet_number; |
tnhnrl | 62:d502889e74f1 | 1144 | } |
tnhnrl | 62:d502889e74f1 | 1145 | |
tnhnrl | 62:d502889e74f1 | 1146 | void MbedLogger::endReceiveData() { //DLE character * 4 ==> 10 10 10 10 |
tnhnrl | 62:d502889e74f1 | 1147 | closeLogFile(); //close the file here |
tnhnrl | 74:d281aaef9766 | 1148 | xbee().printf("endReceiveData closed the file and ended transmission\n\r"); |
tnhnrl | 62:d502889e74f1 | 1149 | } |
tnhnrl | 62:d502889e74f1 | 1150 | |
tnhnrl | 62:d502889e74f1 | 1151 | //calculate the crc with an integer array |
tnhnrl | 62:d502889e74f1 | 1152 | int MbedLogger::calcCrcOneArray(int *input_array, int array_length) { |
tnhnrl | 62:d502889e74f1 | 1153 | //can't initialize the table in the constructor in c++ |
tnhnrl | 62:d502889e74f1 | 1154 | 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 | 62:d502889e74f1 | 1155 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 1156 | for (int z = 0; z < array_length; z++) { |
tnhnrl | 62:d502889e74f1 | 1157 | crc = (crc_table[(input_array[z] ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 1158 | } |
tnhnrl | 62:d502889e74f1 | 1159 | return crc / 256; //second-to-last byte |
tnhnrl | 62:d502889e74f1 | 1160 | } |
tnhnrl | 62:d502889e74f1 | 1161 | |
tnhnrl | 62:d502889e74f1 | 1162 | int MbedLogger::calcCrcTwoArray(int *input_array, int array_length) { |
tnhnrl | 62:d502889e74f1 | 1163 | //can't initialize the table in the constructor in c++ |
tnhnrl | 62:d502889e74f1 | 1164 | 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 | 62:d502889e74f1 | 1165 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 1166 | for (int z = 0; z < array_length; z++) { |
tnhnrl | 62:d502889e74f1 | 1167 | crc = (crc_table[(input_array[z] ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 1168 | } |
tnhnrl | 62:d502889e74f1 | 1169 | return crc % 256; //second-to-last byte |
tnhnrl | 62:d502889e74f1 | 1170 | } |
tnhnrl | 62:d502889e74f1 | 1171 | |
tnhnrl | 62:d502889e74f1 | 1172 | int MbedLogger::calcCrcOne() { |
tnhnrl | 62:d502889e74f1 | 1173 | //can't initialize the table in the constructor in c++ |
tnhnrl | 62:d502889e74f1 | 1174 | 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 | 62:d502889e74f1 | 1175 | |
tnhnrl | 62:d502889e74f1 | 1176 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 1177 | for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) |
tnhnrl | 62:d502889e74f1 | 1178 | crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 1179 | |
tnhnrl | 62:d502889e74f1 | 1180 | return crc / 256; //second-to-last byte |
tnhnrl | 62:d502889e74f1 | 1181 | } |
tnhnrl | 62:d502889e74f1 | 1182 | |
tnhnrl | 62:d502889e74f1 | 1183 | int MbedLogger::calcCrcTwo() { |
tnhnrl | 62:d502889e74f1 | 1184 | 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 | 62:d502889e74f1 | 1185 | |
tnhnrl | 62:d502889e74f1 | 1186 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 1187 | for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) |
tnhnrl | 62:d502889e74f1 | 1188 | crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 1189 | |
tnhnrl | 74:d281aaef9766 | 1190 | //xbee().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_array.length(), crc % 256); |
tnhnrl | 62:d502889e74f1 | 1191 | |
tnhnrl | 62:d502889e74f1 | 1192 | return crc % 256; //last byte |
tnhnrl | 62:d502889e74f1 | 1193 | } |
tnhnrl | 62:d502889e74f1 | 1194 | |
tnhnrl | 62:d502889e74f1 | 1195 | void MbedLogger::resetReplyPacket() { |
tnhnrl | 62:d502889e74f1 | 1196 | _confirmed_packet_number = 0; |
tnhnrl | 62:d502889e74f1 | 1197 | } |
tnhnrl | 62:d502889e74f1 | 1198 | |
tnhnrl | 62:d502889e74f1 | 1199 | void MbedLogger::openNewMissionFile() { |
tnhnrl | 74:d281aaef9766 | 1200 | xbee().printf("Opening Mission file (sequence.txt) for reception.\n\r"); |
tnhnrl | 62:d502889e74f1 | 1201 | string filename_string = _file_system_string + "sequence.txt"; |
tnhnrl | 62:d502889e74f1 | 1202 | |
tnhnrl | 74:d281aaef9766 | 1203 | xbee().printf("openNewMissionFile: %s\n\r", filename_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1204 | |
tnhnrl | 62:d502889e74f1 | 1205 | _fp = fopen(filename_string.c_str(), "w"); |
tnhnrl | 62:d502889e74f1 | 1206 | } |
tnhnrl | 62:d502889e74f1 | 1207 | |
tnhnrl | 62:d502889e74f1 | 1208 | void MbedLogger::openReceiveFile(string filename) { |
tnhnrl | 62:d502889e74f1 | 1209 | string filename_string = _file_system_string + filename; //example "sequence.txt" |
tnhnrl | 62:d502889e74f1 | 1210 | |
tnhnrl | 62:d502889e74f1 | 1211 | _fp = fopen(filename_string.c_str(), "w"); //open a file for writing |
tnhnrl | 62:d502889e74f1 | 1212 | } |
tnhnrl | 62:d502889e74f1 | 1213 | |
tnhnrl | 62:d502889e74f1 | 1214 | void MbedLogger::setDataCounter(int input_counter) { |
tnhnrl | 62:d502889e74f1 | 1215 | _transmit_counter = input_counter; |
tnhnrl | 62:d502889e74f1 | 1216 | } |
tnhnrl | 62:d502889e74f1 | 1217 | |
tnhnrl | 62:d502889e74f1 | 1218 | void MbedLogger::closeIncompleteFile() { |
tnhnrl | 62:d502889e74f1 | 1219 | fprintf(_fp, "TRANSMISSION INTERRUPTED!"); //write this warning to the file |
tnhnrl | 62:d502889e74f1 | 1220 | closeLogFile(); //close file |
tnhnrl | 62:d502889e74f1 | 1221 | } |
tnhnrl | 62:d502889e74f1 | 1222 | |
tnhnrl | 62:d502889e74f1 | 1223 | void MbedLogger::appendLogFile(int current_state, int option) { |
tnhnrl | 62:d502889e74f1 | 1224 | //option one means write to file |
tnhnrl | 62:d502889e74f1 | 1225 | |
tnhnrl | 62:d502889e74f1 | 1226 | if (option == 1) { |
tnhnrl | 62:d502889e74f1 | 1227 | if (!_fp) { //if not present |
tnhnrl | 62:d502889e74f1 | 1228 | _fp = fopen(_full_file_path_string.c_str(), "a"); |
tnhnrl | 62:d502889e74f1 | 1229 | } |
tnhnrl | 62:d502889e74f1 | 1230 | |
tnhnrl | 62:d502889e74f1 | 1231 | //record data using the recordData function (takes in the state integer) |
tnhnrl | 62:d502889e74f1 | 1232 | recordData(current_state); |
tnhnrl | 62:d502889e74f1 | 1233 | } |
tnhnrl | 62:d502889e74f1 | 1234 | |
tnhnrl | 62:d502889e74f1 | 1235 | else { |
tnhnrl | 62:d502889e74f1 | 1236 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 1237 | } |
tnhnrl | 62:d502889e74f1 | 1238 | } |
tnhnrl | 62:d502889e74f1 | 1239 | |
tnhnrl | 62:d502889e74f1 | 1240 | // initialize and close the file |
tnhnrl | 62:d502889e74f1 | 1241 | // log file freezes at 0x0000006c |
tnhnrl | 62:d502889e74f1 | 1242 | void MbedLogger::initializeLogFile() { |
tnhnrl | 62:d502889e74f1 | 1243 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 74:d281aaef9766 | 1244 | xbee().printf("%s file system init\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1245 | |
tnhnrl | 62:d502889e74f1 | 1246 | //try to open this file... |
tnhnrl | 62:d502889e74f1 | 1247 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 62:d502889e74f1 | 1248 | |
tnhnrl | 62:d502889e74f1 | 1249 | //if the file is empty, create this. |
tnhnrl | 62:d502889e74f1 | 1250 | if (!_fp) { |
tnhnrl | 62:d502889e74f1 | 1251 | _fp = fopen(file_name_string.c_str(), "w"); //write,print,close |
tnhnrl | 67:c86a4b464682 | 1252 | //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 | 67:c86a4b464682 | 1253 | fprintf(_fp,_heading_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1254 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 1255 | } |
tnhnrl | 62:d502889e74f1 | 1256 | else |
tnhnrl | 62:d502889e74f1 | 1257 | closeLogFile(); //close the opened read file |
tnhnrl | 62:d502889e74f1 | 1258 | } |
tnhnrl | 62:d502889e74f1 | 1259 | |
tnhnrl | 62:d502889e74f1 | 1260 | int MbedLogger::fileTransmitState() { |
tnhnrl | 62:d502889e74f1 | 1261 | return _file_transmission_state; |
tnhnrl | 62:d502889e74f1 | 1262 | } |
tnhnrl | 62:d502889e74f1 | 1263 | |
tnhnrl | 62:d502889e74f1 | 1264 | int MbedLogger::calcCrcOneString (string input_string) { |
tnhnrl | 62:d502889e74f1 | 1265 | 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 | 62:d502889e74f1 | 1266 | |
tnhnrl | 62:d502889e74f1 | 1267 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 1268 | for (unsigned int i = 0; i < input_string.length(); i++) { |
tnhnrl | 62:d502889e74f1 | 1269 | //convert each character to an integer |
tnhnrl | 62:d502889e74f1 | 1270 | int character_to_integer = input_string[i]; //correct |
tnhnrl | 62:d502889e74f1 | 1271 | |
tnhnrl | 62:d502889e74f1 | 1272 | crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 1273 | } |
tnhnrl | 62:d502889e74f1 | 1274 | |
tnhnrl | 74:d281aaef9766 | 1275 | //xbee().printf("DEBUG: calcCrcOne string length: %d crc: %d\n\r", input_string.length(), crc/256); |
tnhnrl | 62:d502889e74f1 | 1276 | |
tnhnrl | 62:d502889e74f1 | 1277 | return crc / 256; //second-to-last byte |
tnhnrl | 62:d502889e74f1 | 1278 | } |
tnhnrl | 62:d502889e74f1 | 1279 | |
tnhnrl | 62:d502889e74f1 | 1280 | int MbedLogger::calcCrcTwoString (string input_string) { |
tnhnrl | 62:d502889e74f1 | 1281 | 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 | 62:d502889e74f1 | 1282 | |
tnhnrl | 62:d502889e74f1 | 1283 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 1284 | for (unsigned int i = 0; i < input_string.length(); i++) { |
tnhnrl | 62:d502889e74f1 | 1285 | //convert each character to an integer |
tnhnrl | 62:d502889e74f1 | 1286 | int character_to_integer = input_string[i]; //correct |
tnhnrl | 62:d502889e74f1 | 1287 | |
tnhnrl | 62:d502889e74f1 | 1288 | crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 1289 | } |
tnhnrl | 62:d502889e74f1 | 1290 | |
tnhnrl | 74:d281aaef9766 | 1291 | //xbee().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_string.length(), crc % 256); |
tnhnrl | 62:d502889e74f1 | 1292 | |
tnhnrl | 62:d502889e74f1 | 1293 | return crc % 256; //last byte |
tnhnrl | 62:d502889e74f1 | 1294 | } |
tnhnrl | 62:d502889e74f1 | 1295 | |
tnhnrl | 62:d502889e74f1 | 1296 | void MbedLogger::createEmptyLog() { |
tnhnrl | 62:d502889e74f1 | 1297 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 67:c86a4b464682 | 1298 | string empty_log = "EMPTY LOG"; |
tnhnrl | 62:d502889e74f1 | 1299 | |
tnhnrl | 62:d502889e74f1 | 1300 | _fp = fopen(file_name_string.c_str(), "w"); |
tnhnrl | 67:c86a4b464682 | 1301 | |
tnhnrl | 67:c86a4b464682 | 1302 | fprintf(_fp, "%.25s\n",empty_log.c_str()); //just write this string to the log (processing needs a file size that is not zero) |
tnhnrl | 62:d502889e74f1 | 1303 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 1304 | } |
tnhnrl | 62:d502889e74f1 | 1305 | |
tnhnrl | 62:d502889e74f1 | 1306 | int MbedLogger::getFileSize(string filename) { |
tnhnrl | 62:d502889e74f1 | 1307 | // fixed the const char * errror: |
tnhnrl | 62:d502889e74f1 | 1308 | // https://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char |
tnhnrl | 62:d502889e74f1 | 1309 | 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 | 62:d502889e74f1 | 1310 | //http://www.cplusplus.com/reference/string/string/c_str/ |
tnhnrl | 62:d502889e74f1 | 1311 | |
tnhnrl | 62:d502889e74f1 | 1312 | _fp = fopen(filename.c_str(), "rb"); //open the file for reading as a binary file |
tnhnrl | 62:d502889e74f1 | 1313 | |
tnhnrl | 62:d502889e74f1 | 1314 | fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file) |
tnhnrl | 62:d502889e74f1 | 1315 | unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file. |
tnhnrl | 62:d502889e74f1 | 1316 | fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary |
tnhnrl | 62:d502889e74f1 | 1317 | |
tnhnrl | 73:f6f378311c8d | 1318 | //closeLogFile(); //can probably just close the file pointer and not worry about position |
tnhnrl | 62:d502889e74f1 | 1319 | |
tnhnrl | 74:d281aaef9766 | 1320 | xbee().printf("%s file size is %d\n\r", filename.c_str(), file_size); |
tnhnrl | 62:d502889e74f1 | 1321 | |
tnhnrl | 62:d502889e74f1 | 1322 | return file_size; |
tnhnrl | 62:d502889e74f1 | 1323 | } |
tnhnrl | 62:d502889e74f1 | 1324 | |
tnhnrl | 62:d502889e74f1 | 1325 | int MbedLogger::debugFileState() { |
tnhnrl | 74:d281aaef9766 | 1326 | xbee().printf("What is _fp right now? %p\n\r", _fp); //pointer notation |
tnhnrl | 62:d502889e74f1 | 1327 | |
tnhnrl | 62:d502889e74f1 | 1328 | if (_fp) |
tnhnrl | 62:d502889e74f1 | 1329 | return 1; //file pointer does exist |
tnhnrl | 62:d502889e74f1 | 1330 | else |
tnhnrl | 62:d502889e74f1 | 1331 | return 0; //file pointer does not exist |
tnhnrl | 62:d502889e74f1 | 1332 | } |
tnhnrl | 62:d502889e74f1 | 1333 | |
tnhnrl | 62:d502889e74f1 | 1334 | void MbedLogger::specifyFileForTransmit(string input_string) { |
tnhnrl | 74:d281aaef9766 | 1335 | xbee().printf("specifyFileForTransmit\n\r"); |
tnhnrl | 62:d502889e74f1 | 1336 | |
tnhnrl | 62:d502889e74f1 | 1337 | string file_string = _file_system_string + input_string; |
tnhnrl | 62:d502889e74f1 | 1338 | |
tnhnrl | 74:d281aaef9766 | 1339 | xbee().printf("file_string is <%s>\n\r", file_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1340 | |
tnhnrl | 62:d502889e74f1 | 1341 | //open this file to read |
tnhnrl | 62:d502889e74f1 | 1342 | _fp = fopen(file_string.c_str(), "r"); |
tnhnrl | 62:d502889e74f1 | 1343 | |
tnhnrl | 62:d502889e74f1 | 1344 | //transmit that file |
tnhnrl | 62:d502889e74f1 | 1345 | //transmitDataWithTicker(); //replaced ticker |
tnhnrl | 62:d502889e74f1 | 1346 | |
tnhnrl | 62:d502889e74f1 | 1347 | _file_transmission = true; |
tnhnrl | 62:d502889e74f1 | 1348 | } |
tnhnrl | 62:d502889e74f1 | 1349 | |
tnhnrl | 62:d502889e74f1 | 1350 | void MbedLogger::transmitFileFromDirectory( int file_number ) { |
tnhnrl | 62:d502889e74f1 | 1351 | file_number = file_number + 1; //to get the correct number |
tnhnrl | 62:d502889e74f1 | 1352 | |
tnhnrl | 62:d502889e74f1 | 1353 | DIR *dir; |
tnhnrl | 62:d502889e74f1 | 1354 | struct dirent *dp; //dirent.h is the format of directory entries |
tnhnrl | 62:d502889e74f1 | 1355 | int log_found =0, loop=1; //start file numbers at 1 |
tnhnrl | 62:d502889e74f1 | 1356 | long int temp=0; |
tnhnrl | 62:d502889e74f1 | 1357 | |
tnhnrl | 62:d502889e74f1 | 1358 | // char * char_pointer; |
tnhnrl | 62:d502889e74f1 | 1359 | // char * numstart, *numstop; |
tnhnrl | 62:d502889e74f1 | 1360 | |
tnhnrl | 62:d502889e74f1 | 1361 | if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) { |
tnhnrl | 74:d281aaef9766 | 1362 | xbee().printf("MBED directory could not be opened\r\n"); |
tnhnrl | 62:d502889e74f1 | 1363 | } |
tnhnrl | 62:d502889e74f1 | 1364 | else |
tnhnrl | 62:d502889e74f1 | 1365 | { |
tnhnrl | 62:d502889e74f1 | 1366 | while ( NULL != (dp = readdir( dir )) ) |
tnhnrl | 62:d502889e74f1 | 1367 | { |
tnhnrl | 74:d281aaef9766 | 1368 | xbee().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp); |
tnhnrl | 62:d502889e74f1 | 1369 | |
tnhnrl | 62:d502889e74f1 | 1370 | //process current file if it matches the file number |
tnhnrl | 62:d502889e74f1 | 1371 | if (file_number == loop) { |
tnhnrl | 62:d502889e74f1 | 1372 | char * current_file_name = dp->d_name; //pointer to this char array |
tnhnrl | 62:d502889e74f1 | 1373 | |
tnhnrl | 62:d502889e74f1 | 1374 | specifyFileForTransmit(current_file_name); |
tnhnrl | 62:d502889e74f1 | 1375 | |
tnhnrl | 62:d502889e74f1 | 1376 | break; |
tnhnrl | 62:d502889e74f1 | 1377 | } |
tnhnrl | 62:d502889e74f1 | 1378 | |
tnhnrl | 62:d502889e74f1 | 1379 | loop++; |
tnhnrl | 62:d502889e74f1 | 1380 | } |
tnhnrl | 62:d502889e74f1 | 1381 | } |
tnhnrl | 62:d502889e74f1 | 1382 | } |
tnhnrl | 62:d502889e74f1 | 1383 | |
tnhnrl | 62:d502889e74f1 | 1384 | void MbedLogger::accessMbedDirectory() { |
tnhnrl | 62:d502889e74f1 | 1385 | printMbedDirectory(); |
tnhnrl | 62:d502889e74f1 | 1386 | |
tnhnrl | 74:d281aaef9766 | 1387 | xbee().printf("Type in the number of the file you want to transmit.\n\r"); |
tnhnrl | 62:d502889e74f1 | 1388 | |
tnhnrl | 62:d502889e74f1 | 1389 | char message[42]; |
tnhnrl | 62:d502889e74f1 | 1390 | |
tnhnrl | 74:d281aaef9766 | 1391 | xbee().scanf("%41s", message); |
tnhnrl | 62:d502889e74f1 | 1392 | |
tnhnrl | 74:d281aaef9766 | 1393 | xbee().printf("Input received!\n\r"); |
tnhnrl | 62:d502889e74f1 | 1394 | |
tnhnrl | 62:d502889e74f1 | 1395 | //check if char array is an integer |
tnhnrl | 62:d502889e74f1 | 1396 | char* conversion_pointer; |
tnhnrl | 62:d502889e74f1 | 1397 | long converted = strtol(message, &conversion_pointer, 10); |
tnhnrl | 62:d502889e74f1 | 1398 | |
tnhnrl | 62:d502889e74f1 | 1399 | if (*conversion_pointer) { |
tnhnrl | 62:d502889e74f1 | 1400 | //conversion failed because the input was not a number |
tnhnrl | 74:d281aaef9766 | 1401 | xbee().printf("NOT A VALID FILE NUMBER!\n\r"); |
tnhnrl | 62:d502889e74f1 | 1402 | } |
tnhnrl | 62:d502889e74f1 | 1403 | else { |
tnhnrl | 62:d502889e74f1 | 1404 | //conversion worked! |
tnhnrl | 74:d281aaef9766 | 1405 | xbee().printf("You chose file number: %d\n\r", converted); |
tnhnrl | 62:d502889e74f1 | 1406 | |
tnhnrl | 62:d502889e74f1 | 1407 | // transmit the file |
tnhnrl | 62:d502889e74f1 | 1408 | transmitFileFromDirectory(converted); |
tnhnrl | 62:d502889e74f1 | 1409 | } |
tnhnrl | 62:d502889e74f1 | 1410 | } |
tnhnrl | 62:d502889e74f1 | 1411 | |
tnhnrl | 62:d502889e74f1 | 1412 | void MbedLogger::closeLogFile() { |
tnhnrl | 68:8f549749b8ce | 1413 | led4() = 1; |
tnhnrl | 68:8f549749b8ce | 1414 | |
tnhnrl | 62:d502889e74f1 | 1415 | if (_fp == NULL){ |
tnhnrl | 74:d281aaef9766 | 1416 | xbee().printf("MbedLogger: (%s) LOG FILE WAS ALREADY CLOSED!\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1417 | } |
tnhnrl | 62:d502889e74f1 | 1418 | |
tnhnrl | 62:d502889e74f1 | 1419 | else { |
tnhnrl | 74:d281aaef9766 | 1420 | xbee().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1421 | |
tnhnrl | 62:d502889e74f1 | 1422 | //close file |
tnhnrl | 62:d502889e74f1 | 1423 | fclose(_fp); |
tnhnrl | 62:d502889e74f1 | 1424 | |
tnhnrl | 62:d502889e74f1 | 1425 | _fp = NULL; //set pointer to zero |
tnhnrl | 62:d502889e74f1 | 1426 | } |
tnhnrl | 62:d502889e74f1 | 1427 | } |
tnhnrl | 62:d502889e74f1 | 1428 | |
tnhnrl | 62:d502889e74f1 | 1429 | void MbedLogger::activateReceivePacket() { |
tnhnrl | 62:d502889e74f1 | 1430 | _mbed_receive_loop = true; |
tnhnrl | 62:d502889e74f1 | 1431 | } |
tnhnrl | 62:d502889e74f1 | 1432 | |
tnhnrl | 74:d281aaef9766 | 1433 | void MbedLogger::receiveSequenceFile() { |
tnhnrl | 74:d281aaef9766 | 1434 | //restart each time |
tnhnrl | 74:d281aaef9766 | 1435 | _end_sequence_transmission = false; |
tnhnrl | 74:d281aaef9766 | 1436 | |
tnhnrl | 74:d281aaef9766 | 1437 | openNewMissionFile(); |
tnhnrl | 74:d281aaef9766 | 1438 | |
tnhnrl | 74:d281aaef9766 | 1439 | //zero will be reserved for the file name, future |
tnhnrl | 74:d281aaef9766 | 1440 | _confirmed_packet_number = 1; //in sendReply() function that transmits a reply for incoming data |
tnhnrl | 74:d281aaef9766 | 1441 | |
tnhnrl | 74:d281aaef9766 | 1442 | // int current_packet_number = 1; |
tnhnrl | 74:d281aaef9766 | 1443 | // int last_packet_number = -1; |
tnhnrl | 74:d281aaef9766 | 1444 | // int break_transmission = 0; |
tnhnrl | 74:d281aaef9766 | 1445 | |
tnhnrl | 74:d281aaef9766 | 1446 | int counter = 0; |
tnhnrl | 74:d281aaef9766 | 1447 | |
tnhnrl | 74:d281aaef9766 | 1448 | while(1) { |
tnhnrl | 74:d281aaef9766 | 1449 | wait(0.25); //runs at 4 Hz |
tnhnrl | 74:d281aaef9766 | 1450 | |
tnhnrl | 74:d281aaef9766 | 1451 | checkForIncomingData(); |
tnhnrl | 74:d281aaef9766 | 1452 | |
tnhnrl | 74:d281aaef9766 | 1453 | //xbee().printf("\n\rDEBUG: _confirmed_packet_number%d\n\r", _confirmed_packet_number); |
tnhnrl | 74:d281aaef9766 | 1454 | |
tnhnrl | 74:d281aaef9766 | 1455 | counter++; |
tnhnrl | 74:d281aaef9766 | 1456 | |
tnhnrl | 74:d281aaef9766 | 1457 | sendReply(); //bad name, should call it send request or something |
tnhnrl | 74:d281aaef9766 | 1458 | |
tnhnrl | 74:d281aaef9766 | 1459 | if (_end_sequence_transmission) |
tnhnrl | 74:d281aaef9766 | 1460 | break; |
tnhnrl | 74:d281aaef9766 | 1461 | } |
tnhnrl | 74:d281aaef9766 | 1462 | |
tnhnrl | 74:d281aaef9766 | 1463 | closeLogFile(); |
tnhnrl | 74:d281aaef9766 | 1464 | } |
tnhnrl | 74:d281aaef9766 | 1465 | |
tnhnrl | 62:d502889e74f1 | 1466 | void MbedLogger::receiveMissionDataWithFSM() { |
tnhnrl | 62:d502889e74f1 | 1467 | checkForIncomingData(); |
tnhnrl | 62:d502889e74f1 | 1468 | |
tnhnrl | 62:d502889e74f1 | 1469 | // Example: send reply 75 65 00 00 |
tnhnrl | 62:d502889e74f1 | 1470 | // Example: send reply 75 65 00 01 |
tnhnrl | 62:d502889e74f1 | 1471 | } |
tnhnrl | 62:d502889e74f1 | 1472 | |
tnhnrl | 62:d502889e74f1 | 1473 | void MbedLogger::receiveMissionDataWithTicker() { |
tnhnrl | 62:d502889e74f1 | 1474 | openNewMissionFile(); //sequence.txt file opened |
tnhnrl | 62:d502889e74f1 | 1475 | |
tnhnrl | 62:d502889e74f1 | 1476 | _mbed_receive_ticker.attach(callback(this, &MbedLogger::activateReceivePacket), 0.5); |
tnhnrl | 62:d502889e74f1 | 1477 | |
tnhnrl | 74:d281aaef9766 | 1478 | xbee().printf("\n\r02/09/2018 MbedLogger receiveMissionData Beginning to receive sequence data...\n\r"); |
tnhnrl | 62:d502889e74f1 | 1479 | |
tnhnrl | 62:d502889e74f1 | 1480 | resetReplyPacket(); //reset the reply packet |
tnhnrl | 62:d502889e74f1 | 1481 | |
tnhnrl | 62:d502889e74f1 | 1482 | //idea for stopping this if data not being received |
tnhnrl | 62:d502889e74f1 | 1483 | int current_packet_number = 0; |
tnhnrl | 62:d502889e74f1 | 1484 | int last_packet_number = -1; |
tnhnrl | 62:d502889e74f1 | 1485 | int break_transmission = 0; |
tnhnrl | 62:d502889e74f1 | 1486 | |
tnhnrl | 62:d502889e74f1 | 1487 | while(1) { |
tnhnrl | 62:d502889e74f1 | 1488 | //runs at 10 hz |
tnhnrl | 62:d502889e74f1 | 1489 | if (_mbed_receive_loop) { |
tnhnrl | 62:d502889e74f1 | 1490 | if (!checkForIncomingData()) { // run this until it finishes |
tnhnrl | 62:d502889e74f1 | 1491 | //when you complete data reception, this will become false |
tnhnrl | 74:d281aaef9766 | 1492 | xbee().printf("\n\rMbedLogger: Data RECEPTION complete.\n\r"); |
tnhnrl | 62:d502889e74f1 | 1493 | _mbed_receive_ticker.detach(); |
tnhnrl | 62:d502889e74f1 | 1494 | break; |
tnhnrl | 62:d502889e74f1 | 1495 | } |
tnhnrl | 62:d502889e74f1 | 1496 | else { |
tnhnrl | 62:d502889e74f1 | 1497 | |
tnhnrl | 62:d502889e74f1 | 1498 | // SEND REPLY 75 65 00 00 |
tnhnrl | 62:d502889e74f1 | 1499 | // SEND REPLY 75 65 00 01 |
tnhnrl | 62:d502889e74f1 | 1500 | |
tnhnrl | 62:d502889e74f1 | 1501 | //check if you keep getting the same thing |
tnhnrl | 62:d502889e74f1 | 1502 | current_packet_number = sendReply(); |
tnhnrl | 62:d502889e74f1 | 1503 | |
tnhnrl | 74:d281aaef9766 | 1504 | //xbee().printf("DEBUG: current packet number %d (last packet number %d) \n\r", current_packet_number, last_packet_number); //debug |
tnhnrl | 62:d502889e74f1 | 1505 | |
tnhnrl | 62:d502889e74f1 | 1506 | //let this count up a few times before it exits |
tnhnrl | 62:d502889e74f1 | 1507 | if (current_packet_number == last_packet_number) { |
tnhnrl | 62:d502889e74f1 | 1508 | break_transmission++; |
tnhnrl | 62:d502889e74f1 | 1509 | |
tnhnrl | 62:d502889e74f1 | 1510 | //break transmission after 50 failed attempts (was 100) |
tnhnrl | 62:d502889e74f1 | 1511 | if (break_transmission >= 50) { |
tnhnrl | 62:d502889e74f1 | 1512 | closeIncompleteFile(); //close the file |
tnhnrl | 62:d502889e74f1 | 1513 | _mbed_receive_ticker.detach(); |
tnhnrl | 74:d281aaef9766 | 1514 | xbee().printf("MbedLogger: TRANSMISSION INTERRUPTED!\n\r"); |
tnhnrl | 62:d502889e74f1 | 1515 | break; |
tnhnrl | 62:d502889e74f1 | 1516 | } |
tnhnrl | 62:d502889e74f1 | 1517 | } |
tnhnrl | 62:d502889e74f1 | 1518 | else |
tnhnrl | 62:d502889e74f1 | 1519 | last_packet_number = current_packet_number; |
tnhnrl | 62:d502889e74f1 | 1520 | } |
tnhnrl | 62:d502889e74f1 | 1521 | _mbed_receive_loop = false; // wait until the loop rate timer fires again |
tnhnrl | 62:d502889e74f1 | 1522 | } |
tnhnrl | 62:d502889e74f1 | 1523 | } |
tnhnrl | 62:d502889e74f1 | 1524 | } |
tnhnrl | 62:d502889e74f1 | 1525 | |
tnhnrl | 62:d502889e74f1 | 1526 | //only do this for the MBED because of the limited file size |
tnhnrl | 62:d502889e74f1 | 1527 | //write one line to the file (open to write, this will erase all other data) and close it. |
tnhnrl | 62:d502889e74f1 | 1528 | void MbedLogger::eraseFile() { |
tnhnrl | 62:d502889e74f1 | 1529 | _fp = fopen(_full_file_path_string.c_str(), "w"); // LOG000.csv |
tnhnrl | 62:d502889e74f1 | 1530 | |
tnhnrl | 67:c86a4b464682 | 1531 | fprintf(_fp,_heading_string.c_str()); |
tnhnrl | 74:d281aaef9766 | 1532 | |
tnhnrl | 62:d502889e74f1 | 1533 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 1534 | } |
tnhnrl | 62:d502889e74f1 | 1535 | |
tnhnrl | 62:d502889e74f1 | 1536 | void MbedLogger::intCreateDataPacket(int data_buffer[],int payload_length) { |
tnhnrl | 62:d502889e74f1 | 1537 | // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2 |
tnhnrl | 62:d502889e74f1 | 1538 | |
tnhnrl | 62:d502889e74f1 | 1539 | //check here https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=01c0 |
tnhnrl | 62:d502889e74f1 | 1540 | // ieee 754: http://www6.uniovi.es/~antonio/uned/ieee754/IEEE-754hex32.html |
tnhnrl | 62:d502889e74f1 | 1541 | |
tnhnrl | 62:d502889e74f1 | 1542 | //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0. |
tnhnrl | 62:d502889e74f1 | 1543 | _data_packet.clear(); |
tnhnrl | 62:d502889e74f1 | 1544 | |
tnhnrl | 62:d502889e74f1 | 1545 | //DATA PACKET HEADER |
tnhnrl | 62:d502889e74f1 | 1546 | _data_packet.push_back(117); //0x75 |
tnhnrl | 62:d502889e74f1 | 1547 | _data_packet.push_back(101); //0x65 |
tnhnrl | 62:d502889e74f1 | 1548 | |
tnhnrl | 62:d502889e74f1 | 1549 | _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form |
tnhnrl | 62:d502889e74f1 | 1550 | _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form |
tnhnrl | 62:d502889e74f1 | 1551 | |
tnhnrl | 62:d502889e74f1 | 1552 | _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form |
tnhnrl | 62:d502889e74f1 | 1553 | _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form |
tnhnrl | 62:d502889e74f1 | 1554 | |
tnhnrl | 62:d502889e74f1 | 1555 | _data_packet.push_back(_current_line_length); |
tnhnrl | 62:d502889e74f1 | 1556 | |
tnhnrl | 62:d502889e74f1 | 1557 | //DATA FROM INTEGER ARRAY (read the array) |
tnhnrl | 62:d502889e74f1 | 1558 | for (int i = 0; i < payload_length; i++) { |
tnhnrl | 62:d502889e74f1 | 1559 | _data_packet.push_back(data_buffer[i]); |
tnhnrl | 62:d502889e74f1 | 1560 | } |
tnhnrl | 62:d502889e74f1 | 1561 | |
tnhnrl | 62:d502889e74f1 | 1562 | //CRC CALCULATIONS BELOW, character version of calculation screws up on null character 0x00, scrapped and using vector of integers |
tnhnrl | 62:d502889e74f1 | 1563 | int crc_one = calcCrcOne(); |
tnhnrl | 62:d502889e74f1 | 1564 | int crc_two = calcCrcTwo(); |
tnhnrl | 62:d502889e74f1 | 1565 | |
tnhnrl | 62:d502889e74f1 | 1566 | //place the crc bytes into the data packet that is transmitted |
tnhnrl | 62:d502889e74f1 | 1567 | _data_packet.push_back(crc_one); |
tnhnrl | 62:d502889e74f1 | 1568 | _data_packet.push_back(crc_two); |
tnhnrl | 62:d502889e74f1 | 1569 | } |
tnhnrl | 62:d502889e74f1 | 1570 | |
tnhnrl | 68:8f549749b8ce | 1571 | void MbedLogger::setTransmitPacketNumber(int packet_number) { |
tnhnrl | 68:8f549749b8ce | 1572 | _transmit_packet_num = packet_number; |
tnhnrl | 69:919ac8d7e023 | 1573 | |
tnhnrl | 69:919ac8d7e023 | 1574 | //also needed to reset a boolean flag on the transmit |
tnhnrl | 69:919ac8d7e023 | 1575 | _fsm_transmit_complete = false; |
tnhnrl | 73:f6f378311c8d | 1576 | |
tnhnrl | 73:f6f378311c8d | 1577 | _end_transmit_packet = false; |
tnhnrl | 72:250b2665755c | 1578 | } |
tnhnrl | 72:250b2665755c | 1579 | |
tnhnrl | 72:250b2665755c | 1580 | int MbedLogger::currentPacketNumber() { |
tnhnrl | 72:250b2665755c | 1581 | return _packet_number; |
tnhnrl | 74:d281aaef9766 | 1582 | } |