most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown
Dependencies: mbed MODSERIAL FATFileSystem
SequenceController/SequenceController.cpp@92:52a91656458a, 2019-05-13 (annotated)
- Committer:
- joel_ssc
- Date:
- Mon May 13 19:25:26 2019 +0000
- Revision:
- 92:52a91656458a
- Parent:
- 87:6d95f853dab3
version for first flight test, timeouts not yet set correctly
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tnhnrl | 17:7c16b5671d0e | 1 | #include "SequenceController.hpp" |
tnhnrl | 17:7c16b5671d0e | 2 | #include "StaticDefs.hpp" |
tnhnrl | 17:7c16b5671d0e | 3 | |
tnhnrl | 17:7c16b5671d0e | 4 | SequenceController::SequenceController() { |
tnhnrl | 17:7c16b5671d0e | 5 | _sequence_counter = 0; |
tnhnrl | 17:7c16b5671d0e | 6 | } |
tnhnrl | 17:7c16b5671d0e | 7 | |
tnhnrl | 17:7c16b5671d0e | 8 | void SequenceController::loadSequence() { |
tnhnrl | 74:d281aaef9766 | 9 | xbee().printf("\n\rLoading Dive Sequence File:"); |
tnhnrl | 17:7c16b5671d0e | 10 | |
tnhnrl | 17:7c16b5671d0e | 11 | ConfigFile read_sequence_cfg; |
joel_ssc | 86:ba3a118b0080 | 12 | char value[256]; |
joel_ssc | 86:ba3a118b0080 | 13 | char bux2[256]; |
tnhnrl | 17:7c16b5671d0e | 14 | |
tnhnrl | 17:7c16b5671d0e | 15 | //read configuration file stored on MBED |
tnhnrl | 45:16b8162188ca | 16 | if (!read_sequence_cfg.read("/local/sequence.txt")) { |
tnhnrl | 74:d281aaef9766 | 17 | xbee().printf("\n\rERROR:Failure to read sequence.txt file."); |
tnhnrl | 17:7c16b5671d0e | 18 | } |
tnhnrl | 45:16b8162188ca | 19 | else { |
tnhnrl | 17:7c16b5671d0e | 20 | /* Read values from the file until you reach an "exit" character" */ |
tnhnrl | 45:16b8162188ca | 21 | //up to 256 items in the sequence |
tnhnrl | 45:16b8162188ca | 22 | for (int i = 0; i < 256; i++) { //works |
tnhnrl | 17:7c16b5671d0e | 23 | /* convert INT to string */ |
tnhnrl | 17:7c16b5671d0e | 24 | char buf[256]; |
tnhnrl | 17:7c16b5671d0e | 25 | sprintf(buf, "%d", i); //searching for 0,1,2,3... |
tnhnrl | 17:7c16b5671d0e | 26 | /* convert INT to string */ |
tnhnrl | 17:7c16b5671d0e | 27 | |
tnhnrl | 17:7c16b5671d0e | 28 | if (read_sequence_cfg.getValue(buf, &value[0], sizeof(value))) { |
tnhnrl | 74:d281aaef9766 | 29 | xbee().printf("\n\rsequence %d = %s",i,value); |
tnhnrl | 17:7c16b5671d0e | 30 | |
tnhnrl | 17:7c16b5671d0e | 31 | sequenceStructLoaded[i] = process(value); //create the structs using process(string randomstring) |
tnhnrl | 17:7c16b5671d0e | 32 | } |
tnhnrl | 17:7c16b5671d0e | 33 | |
tnhnrl | 17:7c16b5671d0e | 34 | if (sequenceStructLoaded[i].title == "exit") { |
tnhnrl | 17:7c16b5671d0e | 35 | _number_of_sequences = i; //before the exit |
joel_ssc | 86:ba3a118b0080 | 36 | sprintf(bux2,"\n\rin loadsequence(): found final exit line num-sequences=%d\n", i); |
joel_ssc | 86:ba3a118b0080 | 37 | mbedLogger().appendDiagFile(bux2,0); |
tnhnrl | 17:7c16b5671d0e | 38 | break; |
tnhnrl | 17:7c16b5671d0e | 39 | } |
tnhnrl | 17:7c16b5671d0e | 40 | } |
tnhnrl | 17:7c16b5671d0e | 41 | } //end of successful read |
tnhnrl | 17:7c16b5671d0e | 42 | } |
tnhnrl | 17:7c16b5671d0e | 43 | |
tnhnrl | 17:7c16b5671d0e | 44 | sequenceStruct SequenceController::process(string randomstring) { |
tnhnrl | 17:7c16b5671d0e | 45 | //this is the struct that is loaded from the config variables |
joel_ssc | 86:ba3a118b0080 | 46 | char bux2[256]; |
tnhnrl | 21:38c8544db6f4 | 47 | sequenceStruct loadStruct; //local struct |
tnhnrl | 17:7c16b5671d0e | 48 | |
tnhnrl | 17:7c16b5671d0e | 49 | /* CONVERT STRING TO CHAR ARRAY */ |
tnhnrl | 17:7c16b5671d0e | 50 | const char *cstr = randomstring.c_str(); |
tnhnrl | 17:7c16b5671d0e | 51 | /* CONVERT STRING TO CHAR ARRAY */ |
tnhnrl | 17:7c16b5671d0e | 52 | |
tnhnrl | 17:7c16b5671d0e | 53 | /* DIVE */ |
tnhnrl | 45:16b8162188ca | 54 | //this can only be in the first position |
tnhnrl | 45:16b8162188ca | 55 | if ((signed int) randomstring.find("dive") != -1) { |
tnhnrl | 17:7c16b5671d0e | 56 | loadStruct.title = "dive"; |
tnhnrl | 17:7c16b5671d0e | 57 | loadStruct.state = MULTI_DIVE; //NEW: separate state handles multiple dives |
tnhnrl | 17:7c16b5671d0e | 58 | } |
tnhnrl | 17:7c16b5671d0e | 59 | /* DIVE */ |
tnhnrl | 17:7c16b5671d0e | 60 | |
tnhnrl | 17:7c16b5671d0e | 61 | /* PITCH */ |
tnhnrl | 45:16b8162188ca | 62 | if ((signed int) randomstring.find("neutral") != -1) { |
tnhnrl | 17:7c16b5671d0e | 63 | loadStruct.title = "neutral"; |
tnhnrl | 74:d281aaef9766 | 64 | xbee().printf("\n\rLOAD neutral. %d", randomstring.find("neutral")); |
tnhnrl | 17:7c16b5671d0e | 65 | loadStruct.state = FIND_NEUTRAL; |
joel_ssc | 86:ba3a118b0080 | 66 | sprintf(bux2,"\n\rin sequence().process(string): FOUND NEUTRAL in randomstring.find \n"); |
joel_ssc | 86:ba3a118b0080 | 67 | mbedLogger().appendDiagFile(bux2,3); |
tnhnrl | 17:7c16b5671d0e | 68 | } |
tnhnrl | 17:7c16b5671d0e | 69 | /* PITCH */ |
tnhnrl | 17:7c16b5671d0e | 70 | |
tnhnrl | 17:7c16b5671d0e | 71 | /* EXIT */ |
tnhnrl | 45:16b8162188ca | 72 | if ((signed int) randomstring.find("exit") != -1) { |
tnhnrl | 17:7c16b5671d0e | 73 | loadStruct.title = "exit"; |
tnhnrl | 74:d281aaef9766 | 74 | xbee().printf("\n\rReminder. Exit command is state FLOAT_BROADCAST\n\r"); |
joel_ssc | 86:ba3a118b0080 | 75 | sprintf(bux2,"\n\rin sequence().process(string): FOUND EXIT in randomstring.find \n"); |
joel_ssc | 86:ba3a118b0080 | 76 | mbedLogger().appendDiagFile(bux2,0); |
tnhnrl | 30:2964617e7676 | 77 | loadStruct.state = FLOAT_BROADCAST; //this is the new exit condition of the dive-rise sequence (11/4/17) |
tnhnrl | 17:7c16b5671d0e | 78 | } |
tnhnrl | 17:7c16b5671d0e | 79 | /* EXIT */ |
tnhnrl | 17:7c16b5671d0e | 80 | |
tnhnrl | 17:7c16b5671d0e | 81 | /* DEPTH TO FLOAT */ |
tnhnrl | 45:16b8162188ca | 82 | if ((signed int) randomstring.find("depth") != -1) { |
tnhnrl | 17:7c16b5671d0e | 83 | if (randomstring.find("neutral") || randomstring.find("dive")) { |
tnhnrl | 17:7c16b5671d0e | 84 | int depth_pos = randomstring.find("depth") + 6; //11 in example literally "depth=" |
tnhnrl | 17:7c16b5671d0e | 85 | char depth_array[256] = {0}; //clear memory |
tnhnrl | 17:7c16b5671d0e | 86 | int depth_counter = 0; |
tnhnrl | 17:7c16b5671d0e | 87 | for (int i = depth_pos; i < randomstring.length(); i++) { |
joel_ssc | 86:ba3a118b0080 | 88 | if (cstr[i] == ',') |
tnhnrl | 17:7c16b5671d0e | 89 | break; |
joel_ssc | 86:ba3a118b0080 | 90 | else if (cstr[i] == ';') |
tnhnrl | 17:7c16b5671d0e | 91 | break; |
tnhnrl | 17:7c16b5671d0e | 92 | else { |
joel_ssc | 86:ba3a118b0080 | 93 | depth_array[depth_counter] = cstr[i]; |
tnhnrl | 17:7c16b5671d0e | 94 | depth_counter++; |
tnhnrl | 17:7c16b5671d0e | 95 | } |
tnhnrl | 17:7c16b5671d0e | 96 | } |
tnhnrl | 17:7c16b5671d0e | 97 | loadStruct.depth = atof(depth_array); |
tnhnrl | 17:7c16b5671d0e | 98 | } |
tnhnrl | 17:7c16b5671d0e | 99 | } |
tnhnrl | 17:7c16b5671d0e | 100 | /* DEPTH TO FLOAT */ |
tnhnrl | 17:7c16b5671d0e | 101 | |
tnhnrl | 17:7c16b5671d0e | 102 | /* PITCH TO FLOAT */ |
tnhnrl | 45:16b8162188ca | 103 | if ((signed int) randomstring.find("pitch") != -1) { |
tnhnrl | 17:7c16b5671d0e | 104 | if (randomstring.find("neutral") || randomstring.find("dive")) { |
tnhnrl | 17:7c16b5671d0e | 105 | int pitch_pos = randomstring.find("pitch") + 6; //11 in example |
tnhnrl | 17:7c16b5671d0e | 106 | char pitch_array[256] = {0}; //clear memory |
tnhnrl | 17:7c16b5671d0e | 107 | int pitch_counter = 0; |
tnhnrl | 17:7c16b5671d0e | 108 | for (int i = pitch_pos; i < randomstring.length(); i++) { |
tnhnrl | 17:7c16b5671d0e | 109 | if (cstr[i] == ',') |
tnhnrl | 17:7c16b5671d0e | 110 | break; |
tnhnrl | 17:7c16b5671d0e | 111 | else if (cstr[i] == ';') |
tnhnrl | 17:7c16b5671d0e | 112 | break; |
tnhnrl | 17:7c16b5671d0e | 113 | else { |
tnhnrl | 17:7c16b5671d0e | 114 | pitch_array[pitch_counter] = cstr[i]; |
tnhnrl | 17:7c16b5671d0e | 115 | pitch_counter++; |
tnhnrl | 17:7c16b5671d0e | 116 | } |
tnhnrl | 17:7c16b5671d0e | 117 | } |
tnhnrl | 17:7c16b5671d0e | 118 | loadStruct.pitch = atof(pitch_array); |
tnhnrl | 17:7c16b5671d0e | 119 | } |
tnhnrl | 17:7c16b5671d0e | 120 | } |
tnhnrl | 17:7c16b5671d0e | 121 | /* PITCH TO FLOAT */ |
tnhnrl | 17:7c16b5671d0e | 122 | |
tnhnrl | 17:7c16b5671d0e | 123 | /* PAUSE */ |
tnhnrl | 45:16b8162188ca | 124 | if ((signed int) randomstring.find("pause") != -1) { |
tnhnrl | 17:7c16b5671d0e | 125 | loadStruct.title = "pause"; |
tnhnrl | 17:7c16b5671d0e | 126 | } |
tnhnrl | 17:7c16b5671d0e | 127 | /* PAUSE */ |
tnhnrl | 17:7c16b5671d0e | 128 | |
tnhnrl | 17:7c16b5671d0e | 129 | /* TIME TO FLOAT */ |
tnhnrl | 45:16b8162188ca | 130 | if ((signed int) randomstring.find("timeout") != -1) { |
tnhnrl | 45:16b8162188ca | 131 | |
tnhnrl | 17:7c16b5671d0e | 132 | int time_pos = randomstring.find("timeout") + 8; //position of timeout + "timeout=" so 8 |
tnhnrl | 17:7c16b5671d0e | 133 | char time_array[256] = {0}; |
tnhnrl | 17:7c16b5671d0e | 134 | int time_counter = 0; |
tnhnrl | 17:7c16b5671d0e | 135 | for (int i = time_pos; i < randomstring.length(); i++) { |
tnhnrl | 74:d281aaef9766 | 136 | //xbee().printf("time string cstr[i] = %c\n\r", cstr[i]); //debug |
tnhnrl | 17:7c16b5671d0e | 137 | |
tnhnrl | 17:7c16b5671d0e | 138 | if (cstr[i] == ',') |
tnhnrl | 17:7c16b5671d0e | 139 | break; |
tnhnrl | 17:7c16b5671d0e | 140 | else if (cstr[i] == ';') |
tnhnrl | 17:7c16b5671d0e | 141 | break; |
tnhnrl | 17:7c16b5671d0e | 142 | else { |
tnhnrl | 74:d281aaef9766 | 143 | //xbee().printf("time string cstr[i] = %c\n\r", cstr[i]); //debug |
tnhnrl | 17:7c16b5671d0e | 144 | time_array[time_counter] = cstr[i]; |
tnhnrl | 17:7c16b5671d0e | 145 | time_counter++; |
tnhnrl | 17:7c16b5671d0e | 146 | } |
tnhnrl | 17:7c16b5671d0e | 147 | } |
tnhnrl | 17:7c16b5671d0e | 148 | loadStruct.timeout = atof(time_array); |
tnhnrl | 17:7c16b5671d0e | 149 | } |
tnhnrl | 17:7c16b5671d0e | 150 | /* TIME TO FLOAT */ |
tnhnrl | 17:7c16b5671d0e | 151 | |
tnhnrl | 45:16b8162188ca | 152 | // /* EXIT */ |
tnhnrl | 45:16b8162188ca | 153 | // if (randomstring.find("exit") != 0) { |
tnhnrl | 45:16b8162188ca | 154 | // loadStruct.title = "exit"; |
tnhnrl | 74:d281aaef9766 | 155 | // xbee().printf("\n\rEXIT."); |
tnhnrl | 45:16b8162188ca | 156 | // } |
tnhnrl | 45:16b8162188ca | 157 | // /* EXIT */ |
tnhnrl | 17:7c16b5671d0e | 158 | |
joel_ssc | 82:0981b9ada820 | 159 | return loadStruct; //each iteration of this returns a completed struct |
tnhnrl | 17:7c16b5671d0e | 160 | } |
tnhnrl | 17:7c16b5671d0e | 161 | |
tnhnrl | 17:7c16b5671d0e | 162 | void SequenceController::sequenceFunction() { |
tnhnrl | 74:d281aaef9766 | 163 | //xbee().printf("sequenceFunction\n\r"); //debug (verified it is working correctly) |
tnhnrl | 17:7c16b5671d0e | 164 | |
tnhnrl | 17:7c16b5671d0e | 165 | int check_current_state = stateMachine().getState(); |
tnhnrl | 74:d281aaef9766 | 166 | xbee().printf("State Machine State: %d\n\r", check_current_state); |
tnhnrl | 17:7c16b5671d0e | 167 | |
tnhnrl | 17:7c16b5671d0e | 168 | if (stateMachine().getState() == SIT_IDLE) { |
tnhnrl | 17:7c16b5671d0e | 169 | //system starts idle |
tnhnrl | 17:7c16b5671d0e | 170 | //set the state machine to the current sequence in the array |
tnhnrl | 17:7c16b5671d0e | 171 | //example, set to "dive" and set pitch and depth and timeout |
tnhnrl | 17:7c16b5671d0e | 172 | |
tnhnrl | 17:7c16b5671d0e | 173 | _current_state = sequenceStructLoaded[_sequence_counter].state; |
tnhnrl | 74:d281aaef9766 | 174 | xbee().printf("_current_state: %d\n\r", _current_state); |
tnhnrl | 74:d281aaef9766 | 175 | xbee().printf("_sequence_counter: %d\n\r", _sequence_counter); |
tnhnrl | 74:d281aaef9766 | 176 | xbee().printf("_number_of_sequences: %d\n\r", _number_of_sequences); |
tnhnrl | 17:7c16b5671d0e | 177 | |
tnhnrl | 17:7c16b5671d0e | 178 | stateMachine().setState(_current_state); |
tnhnrl | 17:7c16b5671d0e | 179 | stateMachine().setDepthCommand(sequenceStructLoaded[_sequence_counter].depth); |
tnhnrl | 17:7c16b5671d0e | 180 | stateMachine().setPitchCommand(sequenceStructLoaded[_sequence_counter].pitch); |
tnhnrl | 17:7c16b5671d0e | 181 | stateMachine().setTimeout(sequenceStructLoaded[_sequence_counter].timeout); |
tnhnrl | 17:7c16b5671d0e | 182 | |
tnhnrl | 17:7c16b5671d0e | 183 | if (_sequence_counter == _number_of_sequences-1) //end when you finish all of the sequences |
tnhnrl | 17:7c16b5671d0e | 184 | sequenceTicker.detach(); |
tnhnrl | 17:7c16b5671d0e | 185 | |
tnhnrl | 17:7c16b5671d0e | 186 | _sequence_counter++; //exit ticker when counter complete |
tnhnrl | 17:7c16b5671d0e | 187 | } |
tnhnrl | 17:7c16b5671d0e | 188 | } |
tnhnrl | 17:7c16b5671d0e | 189 | |
joel_ssc | 82:0981b9ada820 | 190 | int LegController::getLegState() { |
tnhnrl | 17:7c16b5671d0e | 191 | return _current_state; |
joel_ssc | 82:0981b9ada820 | 192 | } |
joel_ssc | 82:0981b9ada820 | 193 | LegController::LegController() { |
joel_ssc | 82:0981b9ada820 | 194 | _leg_counter = 0; |
joel_ssc | 82:0981b9ada820 | 195 | } |
joel_ssc | 82:0981b9ada820 | 196 | |
joel_ssc | 82:0981b9ada820 | 197 | int LegController::loadLeg() { |
joel_ssc | 82:0981b9ada820 | 198 | xbee().printf("\n\rLoading Leg commands Dive File:"); |
joel_ssc | 82:0981b9ada820 | 199 | |
joel_ssc | 82:0981b9ada820 | 200 | ConfigFile read_leg_cfg; |
joel_ssc | 82:0981b9ada820 | 201 | char value[256]; |
joel_ssc | 82:0981b9ada820 | 202 | // char buf[256]; |
joel_ssc | 82:0981b9ada820 | 203 | char bux2[256]; |
joel_ssc | 82:0981b9ada820 | 204 | static int default_legstruct_loaded = 0; |
joel_ssc | 82:0981b9ada820 | 205 | sprintf(bux2,"\n\rin loadleg(): Loading Leg commands Dive File:"); |
joel_ssc | 82:0981b9ada820 | 206 | mbedLogger().appendDiagFile(bux2,0); |
joel_ssc | 82:0981b9ada820 | 207 | |
joel_ssc | 82:0981b9ada820 | 208 | if(default_legstruct_loaded == 0 ) { |
joel_ssc | 82:0981b9ada820 | 209 | legStructLoaded[0] = load_def_leg(); // is this right call pointer vs item? |
joel_ssc | 82:0981b9ada820 | 210 | legStructLoaded[1].title = "exit"; |
joel_ssc | 82:0981b9ada820 | 211 | default_legstruct_loaded = 1; |
joel_ssc | 82:0981b9ada820 | 212 | } |
joel_ssc | 82:0981b9ada820 | 213 | //read configuration file stored on MBED |
joel_ssc | 82:0981b9ada820 | 214 | if (!read_leg_cfg.read("/local/legfile.txt")) { //legfile.txt has exact same format as sequence.txt file,with starting 0=leg,.... and ending 1=exit line |
joel_ssc | 82:0981b9ada820 | 215 | xbee().printf("\n\rERROR:Failure to read legfile.txt file."); |
joel_ssc | 82:0981b9ada820 | 216 | sprintf(bux2,"\n\rERROR:Failure to read legfile.txt file."); |
joel_ssc | 86:ba3a118b0080 | 217 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 82:0981b9ada820 | 218 | |
joel_ssc | 82:0981b9ada820 | 219 | } |
joel_ssc | 82:0981b9ada820 | 220 | else { |
joel_ssc | 82:0981b9ada820 | 221 | /* Read values from the file until you reach an "exit" character" */ |
joel_ssc | 82:0981b9ada820 | 222 | //up to 256 items in the sequence |
joel_ssc | 82:0981b9ada820 | 223 | for (int i = 0; i < 256; i++) { //works |
joel_ssc | 82:0981b9ada820 | 224 | /* convert INT to string */ |
joel_ssc | 82:0981b9ada820 | 225 | char buf[256]; |
joel_ssc | 82:0981b9ada820 | 226 | sprintf(buf, "%d", i); //searching for 0,1,2,3... |
joel_ssc | 82:0981b9ada820 | 227 | /* convert INT to string */ |
joel_ssc | 82:0981b9ada820 | 228 | |
joel_ssc | 82:0981b9ada820 | 229 | if (read_leg_cfg.getValue(buf, &value[0], sizeof(value))) { |
joel_ssc | 82:0981b9ada820 | 230 | xbee().printf("\n\rsequence %d = %s",i,value); |
joel_ssc | 87:6d95f853dab3 | 231 | sprintf(bux2, "\n\r leg values sequence %d = %s\n",i,value); |
joel_ssc | 82:0981b9ada820 | 232 | mbedLogger().appendDiagFile(bux2,0); |
joel_ssc | 82:0981b9ada820 | 233 | |
joel_ssc | 82:0981b9ada820 | 234 | legStructLoaded[i] = process(value); //create the structs using process(string randomstring) |
joel_ssc | 82:0981b9ada820 | 235 | } |
joel_ssc | 82:0981b9ada820 | 236 | |
joel_ssc | 82:0981b9ada820 | 237 | if (legStructLoaded[i].title == "exit") { |
joel_ssc | 82:0981b9ada820 | 238 | _number_of_legs = i; //before the exit |
joel_ssc | 82:0981b9ada820 | 239 | break; |
joel_ssc | 82:0981b9ada820 | 240 | } |
joel_ssc | 82:0981b9ada820 | 241 | } |
joel_ssc | 82:0981b9ada820 | 242 | } //end of successful read |
joel_ssc | 82:0981b9ada820 | 243 | if (_number_of_legs > 0 ) {return 1; } |
joel_ssc | 82:0981b9ada820 | 244 | else {return 0; } |
joel_ssc | 82:0981b9ada820 | 245 | } |
joel_ssc | 82:0981b9ada820 | 246 | |
joel_ssc | 85:dd8176285b6e | 247 | legStruct LegController::load_def_leg() { // default leg structure and if used, will NOT start leg mode |
joel_ssc | 82:0981b9ada820 | 248 | legStruct loadStruct; |
joel_ssc | 82:0981b9ada820 | 249 | |
joel_ssc | 85:dd8176285b6e | 250 | loadStruct.title = "sit_idle"; |
joel_ssc | 85:dd8176285b6e | 251 | loadStruct.state = SIT_IDLE ; // LEG_POSITION_DIVE; |
joel_ssc | 82:0981b9ada820 | 252 | loadStruct.max_depth = 15; |
joel_ssc | 82:0981b9ada820 | 253 | loadStruct.min_depth = 5; |
joel_ssc | 85:dd8176285b6e | 254 | loadStruct.yo_time = 100; |
joel_ssc | 85:dd8176285b6e | 255 | loadStruct.timeout = 600; |
joel_ssc | 82:0981b9ada820 | 256 | loadStruct.heading = 90; |
joel_ssc | 82:0981b9ada820 | 257 | return loadStruct; |
joel_ssc | 82:0981b9ada820 | 258 | } |
joel_ssc | 82:0981b9ada820 | 259 | |
joel_ssc | 82:0981b9ada820 | 260 | legStruct LegController::process(string randomstring) { |
joel_ssc | 82:0981b9ada820 | 261 | //this is the struct that is loaded from the config variables |
joel_ssc | 82:0981b9ada820 | 262 | |
joel_ssc | 82:0981b9ada820 | 263 | legStruct loadStruct; //local struct |
joel_ssc | 86:ba3a118b0080 | 264 | static int callcount=0; |
joel_ssc | 82:0981b9ada820 | 265 | /* CONVERT STRING TO CHAR ARRAY */ |
joel_ssc | 82:0981b9ada820 | 266 | const char *cstr = randomstring.c_str(); |
joel_ssc | 82:0981b9ada820 | 267 | char bux2[256]; |
joel_ssc | 82:0981b9ada820 | 268 | /* CONVERT STRING TO CHAR ARRAY */ |
joel_ssc | 82:0981b9ada820 | 269 | |
joel_ssc | 85:dd8176285b6e | 270 | /* leg position DIVing */ |
joel_ssc | 82:0981b9ada820 | 271 | //this can only be in the first position |
joel_ssc | 86:ba3a118b0080 | 272 | |
joel_ssc | 86:ba3a118b0080 | 273 | callcount++; |
joel_ssc | 82:0981b9ada820 | 274 | if ((signed int) randomstring.find("leg") != -1) { |
joel_ssc | 82:0981b9ada820 | 275 | loadStruct.title = "leg"; |
joel_ssc | 82:0981b9ada820 | 276 | loadStruct.state = LEG_POSITION_DIVE; //NEW: separate state handles multiple dives |
joel_ssc | 82:0981b9ada820 | 277 | sprintf(bux2, "\n\r process leg file: found leg label, setting state to LEG_POSITION_DIVE"); |
joel_ssc | 82:0981b9ada820 | 278 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 82:0981b9ada820 | 279 | } |
joel_ssc | 85:dd8176285b6e | 280 | /* LPD */ |
joel_ssc | 82:0981b9ada820 | 281 | |
joel_ssc | 87:6d95f853dab3 | 282 | /* start-swim */ |
joel_ssc | 87:6d95f853dab3 | 283 | if ((signed int) randomstring.find("start_swim") != -1) { |
joel_ssc | 87:6d95f853dab3 | 284 | loadStruct.title = "start_swim"; |
joel_ssc | 87:6d95f853dab3 | 285 | xbee().printf("\n\rLOAD start-swim %d", randomstring.find("start_swim")); |
joel_ssc | 87:6d95f853dab3 | 286 | loadStruct.state = START_SWIM; |
joel_ssc | 87:6d95f853dab3 | 287 | sprintf(bux2, "\n\r process leg file: found START-SWIM label, setting state to same\n"); |
joel_ssc | 87:6d95f853dab3 | 288 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 82:0981b9ada820 | 289 | } |
joel_ssc | 87:6d95f853dab3 | 290 | /* start-swim */ |
joel_ssc | 87:6d95f853dab3 | 291 | /* flying_idle */ |
joel_ssc | 87:6d95f853dab3 | 292 | if ((signed int) randomstring.find("flying_idle") != -1) { |
joel_ssc | 87:6d95f853dab3 | 293 | loadStruct.title = "flying_idle"; |
joel_ssc | 87:6d95f853dab3 | 294 | xbee().printf("\n\rLOAD flying-idle found at %d", randomstring.find("flying_idle")); |
joel_ssc | 87:6d95f853dab3 | 295 | loadStruct.state = FLYING_IDLE; |
joel_ssc | 87:6d95f853dab3 | 296 | sprintf(bux2, "\n\r process leg file: found FLYING_IDLE label, setting state to FLYING_IDLE \n"); |
joel_ssc | 87:6d95f853dab3 | 297 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 87:6d95f853dab3 | 298 | } |
joel_ssc | 87:6d95f853dab3 | 299 | /* flying_idle */ |
joel_ssc | 82:0981b9ada820 | 300 | /* EXIT */ |
joel_ssc | 82:0981b9ada820 | 301 | if ((signed int) randomstring.find("exit") != -1) { |
joel_ssc | 82:0981b9ada820 | 302 | loadStruct.title = "exit"; |
joel_ssc | 82:0981b9ada820 | 303 | xbee().printf("\n\rReminder. Exit command is state FLOAT_BROADCAST\n\r"); |
joel_ssc | 82:0981b9ada820 | 304 | loadStruct.state = FLOAT_BROADCAST; //this is the new exit condition of the dive-rise sequence (11/4/17) |
joel_ssc | 87:6d95f853dab3 | 305 | sprintf(bux2, "\n\r process(valuestring) legfile: found exit key. Callcount=%d\n", callcount); |
joel_ssc | 82:0981b9ada820 | 306 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 82:0981b9ada820 | 307 | } |
joel_ssc | 82:0981b9ada820 | 308 | /* EXIT */ |
joel_ssc | 82:0981b9ada820 | 309 | |
joel_ssc | 82:0981b9ada820 | 310 | /* max DEPTH TO cycle to */ |
joel_ssc | 82:0981b9ada820 | 311 | |
joel_ssc | 82:0981b9ada820 | 312 | if ((signed int) randomstring.find("max_depth") != -1) { |
joel_ssc | 87:6d95f853dab3 | 313 | |
joel_ssc | 87:6d95f853dab3 | 314 | int depth_pos = randomstring.find("max_depth") + 10; //11 in example literally "depth=" |
joel_ssc | 87:6d95f853dab3 | 315 | char depth_array[256] = {0}; //clear memory |
joel_ssc | 87:6d95f853dab3 | 316 | int depth_counter = 0; |
joel_ssc | 87:6d95f853dab3 | 317 | for (int i = depth_pos; i < randomstring.length(); i++) { |
joel_ssc | 87:6d95f853dab3 | 318 | if (cstr[i] == ',') |
joel_ssc | 87:6d95f853dab3 | 319 | break; |
joel_ssc | 87:6d95f853dab3 | 320 | else if (cstr[i] == ';') |
joel_ssc | 87:6d95f853dab3 | 321 | break; |
joel_ssc | 87:6d95f853dab3 | 322 | else { |
joel_ssc | 87:6d95f853dab3 | 323 | depth_array[depth_counter] = cstr[i]; |
joel_ssc | 87:6d95f853dab3 | 324 | depth_counter++; |
joel_ssc | 82:0981b9ada820 | 325 | } |
joel_ssc | 82:0981b9ada820 | 326 | } |
joel_ssc | 87:6d95f853dab3 | 327 | loadStruct.max_depth = atof(depth_array); |
joel_ssc | 87:6d95f853dab3 | 328 | sprintf(bux2, "\n\r process legfile: key=max_depth val=%g process(legstring)count=%d \n", atof(depth_array), callcount); |
joel_ssc | 87:6d95f853dab3 | 329 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 87:6d95f853dab3 | 330 | |
joel_ssc | 87:6d95f853dab3 | 331 | } |
joel_ssc | 82:0981b9ada820 | 332 | |
joel_ssc | 82:0981b9ada820 | 333 | |
joel_ssc | 87:6d95f853dab3 | 334 | if ((signed int) randomstring.find("min_depth") != -1) { |
joel_ssc | 87:6d95f853dab3 | 335 | |
joel_ssc | 87:6d95f853dab3 | 336 | int depth_pos = randomstring.find("min_depth") + 10; //11 in example literally "depth=" |
joel_ssc | 87:6d95f853dab3 | 337 | char depth_array[256] = {0}; //clear memory |
joel_ssc | 87:6d95f853dab3 | 338 | int depth_counter = 0; |
joel_ssc | 87:6d95f853dab3 | 339 | for (int i = depth_pos; i < randomstring.length(); i++) { |
joel_ssc | 87:6d95f853dab3 | 340 | if (cstr[i] == ',') |
joel_ssc | 87:6d95f853dab3 | 341 | break; |
joel_ssc | 87:6d95f853dab3 | 342 | else if (cstr[i] == ';') |
joel_ssc | 87:6d95f853dab3 | 343 | break; |
joel_ssc | 87:6d95f853dab3 | 344 | else { |
joel_ssc | 87:6d95f853dab3 | 345 | depth_array[depth_counter] = cstr[i]; |
joel_ssc | 87:6d95f853dab3 | 346 | depth_counter++; |
joel_ssc | 82:0981b9ada820 | 347 | } |
joel_ssc | 87:6d95f853dab3 | 348 | } |
joel_ssc | 87:6d95f853dab3 | 349 | loadStruct.min_depth = atof(depth_array); |
joel_ssc | 87:6d95f853dab3 | 350 | sprintf(bux2, "\n\r process legfile: key=min_depth val=%g \n", atof(depth_array)); |
joel_ssc | 87:6d95f853dab3 | 351 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 87:6d95f853dab3 | 352 | |
joel_ssc | 87:6d95f853dab3 | 353 | } |
joel_ssc | 87:6d95f853dab3 | 354 | if ((signed int) randomstring.find("heading") != -1) { |
joel_ssc | 87:6d95f853dab3 | 355 | |
joel_ssc | 82:0981b9ada820 | 356 | int depth_pos = randomstring.find("heading") + 8; //11 in example literally "depth=" |
joel_ssc | 82:0981b9ada820 | 357 | char depth_array[256] = {0}; //clear memory |
joel_ssc | 82:0981b9ada820 | 358 | int depth_counter = 0; |
joel_ssc | 82:0981b9ada820 | 359 | for (int i = depth_pos; i < randomstring.length(); i++) { |
joel_ssc | 87:6d95f853dab3 | 360 | if (cstr[i] == ',') |
joel_ssc | 82:0981b9ada820 | 361 | break; |
joel_ssc | 87:6d95f853dab3 | 362 | else if (cstr[i] == ';') |
joel_ssc | 82:0981b9ada820 | 363 | break; |
joel_ssc | 82:0981b9ada820 | 364 | else { |
joel_ssc | 87:6d95f853dab3 | 365 | depth_array[depth_counter] = cstr[i]; |
joel_ssc | 82:0981b9ada820 | 366 | depth_counter++; |
joel_ssc | 87:6d95f853dab3 | 367 | } |
joel_ssc | 82:0981b9ada820 | 368 | } |
joel_ssc | 82:0981b9ada820 | 369 | loadStruct.heading = atof(depth_array); |
joel_ssc | 82:0981b9ada820 | 370 | sprintf(bux2, "\n\r process legfile: key=heading val=%g \n", atof(depth_array)); |
joel_ssc | 87:6d95f853dab3 | 371 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 87:6d95f853dab3 | 372 | |
joel_ssc | 87:6d95f853dab3 | 373 | } |
joel_ssc | 82:0981b9ada820 | 374 | /* DEPTH TO FLOAT */ |
joel_ssc | 82:0981b9ada820 | 375 | |
joel_ssc | 82:0981b9ada820 | 376 | |
joel_ssc | 82:0981b9ada820 | 377 | |
joel_ssc | 82:0981b9ada820 | 378 | /* PAUSE */ |
joel_ssc | 82:0981b9ada820 | 379 | if ((signed int) randomstring.find("pause") != -1) { |
joel_ssc | 82:0981b9ada820 | 380 | loadStruct.title = "pause"; |
joel_ssc | 82:0981b9ada820 | 381 | } |
joel_ssc | 82:0981b9ada820 | 382 | /* PAUSE */ |
joel_ssc | 82:0981b9ada820 | 383 | |
joel_ssc | 85:dd8176285b6e | 384 | /* TIME TO maintain leg yo-yo operations */ |
joel_ssc | 82:0981b9ada820 | 385 | if ((signed int) randomstring.find("timeout") != -1) { |
joel_ssc | 82:0981b9ada820 | 386 | |
joel_ssc | 82:0981b9ada820 | 387 | int time_pos = randomstring.find("timeout") + 8; //position of timeout + "timeout=" so 8 |
joel_ssc | 82:0981b9ada820 | 388 | char time_array[256] = {0}; |
joel_ssc | 82:0981b9ada820 | 389 | int time_counter = 0; |
joel_ssc | 82:0981b9ada820 | 390 | for (int i = time_pos; i < randomstring.length(); i++) { |
joel_ssc | 82:0981b9ada820 | 391 | //xbee().printf("time string cstr[i] = %c\n\r", cstr[i]); //debug |
joel_ssc | 82:0981b9ada820 | 392 | |
joel_ssc | 82:0981b9ada820 | 393 | if (cstr[i] == ',') |
joel_ssc | 82:0981b9ada820 | 394 | break; |
joel_ssc | 82:0981b9ada820 | 395 | else if (cstr[i] == ';') |
joel_ssc | 82:0981b9ada820 | 396 | break; |
joel_ssc | 82:0981b9ada820 | 397 | else { |
joel_ssc | 82:0981b9ada820 | 398 | //xbee().printf("time string cstr[i] = %c\n\r", cstr[i]); //debug |
joel_ssc | 82:0981b9ada820 | 399 | time_array[time_counter] = cstr[i]; |
joel_ssc | 82:0981b9ada820 | 400 | time_counter++; |
joel_ssc | 82:0981b9ada820 | 401 | } |
joel_ssc | 82:0981b9ada820 | 402 | } |
joel_ssc | 82:0981b9ada820 | 403 | loadStruct.timeout = atof(time_array); |
joel_ssc | 82:0981b9ada820 | 404 | sprintf(bux2, "\n\r process legfile: key=timeout val=%g \n", atof(time_array)); |
joel_ssc | 82:0981b9ada820 | 405 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 82:0981b9ada820 | 406 | } |
joel_ssc | 82:0981b9ada820 | 407 | // yo_time is to avoid a single down/or up segment going on too long - maybe 10 minutes? |
joel_ssc | 82:0981b9ada820 | 408 | if ((signed int) randomstring.find("yo_time") != -1) { |
joel_ssc | 82:0981b9ada820 | 409 | |
joel_ssc | 82:0981b9ada820 | 410 | int time_pos = randomstring.find("yo_time") + 8; //position of timeout + "timeout=" so 8 |
joel_ssc | 82:0981b9ada820 | 411 | char time_array[256] = {0}; |
joel_ssc | 82:0981b9ada820 | 412 | int time_counter = 0; |
joel_ssc | 82:0981b9ada820 | 413 | for (int i = time_pos; i < randomstring.length(); i++) { |
joel_ssc | 82:0981b9ada820 | 414 | //xbee().printf("time string cstr[i] = %c\n\r", cstr[i]); //debug |
joel_ssc | 82:0981b9ada820 | 415 | |
joel_ssc | 82:0981b9ada820 | 416 | if (cstr[i] == ',') |
joel_ssc | 82:0981b9ada820 | 417 | break; |
joel_ssc | 82:0981b9ada820 | 418 | else if (cstr[i] == ';') |
joel_ssc | 82:0981b9ada820 | 419 | break; |
joel_ssc | 82:0981b9ada820 | 420 | else { |
joel_ssc | 82:0981b9ada820 | 421 | //xbee().printf("time string cstr[i] = %c\n\r", cstr[i]); //debug |
joel_ssc | 82:0981b9ada820 | 422 | time_array[time_counter] = cstr[i]; |
joel_ssc | 82:0981b9ada820 | 423 | time_counter++; |
joel_ssc | 82:0981b9ada820 | 424 | } |
joel_ssc | 82:0981b9ada820 | 425 | } |
joel_ssc | 82:0981b9ada820 | 426 | loadStruct.yo_time = atof(time_array); |
joel_ssc | 82:0981b9ada820 | 427 | sprintf(bux2, "\n\r process legfile: key=yo_time val=%g \n", atof(time_array)); |
joel_ssc | 82:0981b9ada820 | 428 | mbedLogger().appendDiagFile(bux2,3); |
joel_ssc | 82:0981b9ada820 | 429 | } |
joel_ssc | 82:0981b9ada820 | 430 | /* TIME TO FLOAT */ |
joel_ssc | 82:0981b9ada820 | 431 | |
joel_ssc | 82:0981b9ada820 | 432 | // /* EXIT */ |
joel_ssc | 82:0981b9ada820 | 433 | // if (randomstring.find("exit") != 0) { |
joel_ssc | 82:0981b9ada820 | 434 | // loadStruct.title = "exit"; |
joel_ssc | 82:0981b9ada820 | 435 | // xbee().printf("\n\rEXIT."); |
joel_ssc | 82:0981b9ada820 | 436 | // } |
joel_ssc | 82:0981b9ada820 | 437 | // /* EXIT */ |
joel_ssc | 82:0981b9ada820 | 438 | |
joel_ssc | 82:0981b9ada820 | 439 | return loadStruct; //each iteration this returns a completed struct |
joel_ssc | 82:0981b9ada820 | 440 | } |