update with altimeter, swimfile.txt endleg.txt, etc see changes_13sep.txt also reset_PI()

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
joel_ssc
Date:
Fri Sep 13 19:15:40 2019 +0000
Revision:
104:426224a55f5f
Parent:
102:0f430de62447
slight change  includes reset_PI

Who changed what in which revision?

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