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
Diff: StateMachine/StateMachine.cpp
- Revision:
- 45:16b8162188ca
- Parent:
- 39:58375ca6b6ff
- Child:
- 49:47ffa4feb6db
--- a/StateMachine/StateMachine.cpp Wed Feb 14 21:58:44 2018 +0000 +++ b/StateMachine/StateMachine.cpp Thu Feb 15 02:39:13 2018 +0000 @@ -55,7 +55,9 @@ } //Finite State Machine (FSM) -int StateMachine::runStateMachine() { +int StateMachine::runStateMachine() { + static int transmit_packet_number = 1; //for data transmission + // finite state machine ... each state has at least one exit criteria switch (_state) { case SIT_IDLE : @@ -556,29 +558,72 @@ break; - case TRANSMIT_DATA : + case TRANSMIT_LOG: if (!_isTimeoutRunning) { - pc().printf("\r\n\nstate: TRANSMIT_DATA\r\n"); + pc().printf("\r\n\nstate: TRANSMIT_LOG\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running _isTimeoutRunning = true; - // what needs to be started? - - // what are the commands? - recordData(_state); + mbedLogger().getNumberOfPacketsInCurrentLog(); //open the file, read the number of lines in the log file + transmit_packet_number = mbedLogger().getNumberOfPackets(); + + //pc().printf("getNumberOfPacketsInCurrentLog is %d\n\r", transmit_packet_number); + } + + if (timer.read() > _timeout) { + pc().printf("\n\rTRANSMIT_LOG: timed out!\n\r"); + _state = SIT_IDLE; + timer.reset(); + _isTimeoutRunning = false; + + mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading + } + + //IF THIS IS ZERO + if (mbedLogger().currentPacketNumber() > transmit_packet_number) { + pc().printf("mbedLogger().currentPacketNumber() > transmit_packet_number"); + _state = SIT_IDLE; + timer.reset(); + _isTimeoutRunning = false; + + mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading } + + if (mbedLogger().isTransmissionComplete()) { + pc().printf("StateMachine isTransmissionComplete (true)\n\r"); + _state = SIT_IDLE; + timer.reset(); + _isTimeoutRunning = false; + + mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading + } + - // how exit? + // what is active? (no hardware should be active) + mbedLogger().readTransmitPacketOneChar(); //led2 shows you pc readable + + break; + + case RECEIVE_SEQUENCE : + pc().printf("state: RECEIVE_SEQUENCE\n\r"); + + if (!_isTimeoutRunning) { + pc().printf("RECEIVE_SEQUENCE _isTimeoutRunning\r\n"); + timer.reset(); // timer goes back to zero + timer.start(); // background timer starts running + _isTimeoutRunning = true; + } + if (timer.read() > _timeout) { - pc().printf("TRANSMIT_DATA: timed out\n\n\r"); + pc().printf("RECEIVE_SEQUENCE: timed out!\n\r"); _state = SIT_IDLE; timer.reset(); _isTimeoutRunning = false; } - //action - transmitData(); + // what is active? + pc().printf("Receive sequence active?\n\r"); break; @@ -872,16 +917,16 @@ pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles } - else if (userInput == 'R' or userInput == 'r') { + else if (userInput == 'R') { _keyboard_state = RISE; } - else if (userInput == 'L' or userInput == 'l') { + else if (userInput == 'L') { _keyboard_state = FLOAT_LEVEL; } - else if (userInput == 'B' or userInput == 'b') { + else if (userInput == 'B') { _keyboard_state = FLOAT_BROADCAST; } - else if (userInput == 'E' or userInput == 'e') { + else if (userInput == 'E') { _keyboard_state = EMERGENCY_CLIMB; } else if (userInput == 'P') { @@ -890,18 +935,12 @@ mbedLogger().printCurrentLogFile(); //print the current log file to the screen } else if (userInput == 'X') { - printDirectory(); - //mbedLogger().printDirectory(); //print all log files to the screen - } - else if (userInput == 'V') { - _keyboard_state = TRANSMIT_DATA; //Transmit data (work in progress) + mbedLogger().printMbedDirectory(); //print all log files to the screen } -// else if (userInput == 'H' or userInput == 'h') { -// pc().printf("running homing procedure\r\n"); -// bce().unpause(); bce().homePiston(); bce().pause(); -// batt().unpause(); batt().homePiston(); batt().pause(); -// } - else if (userInput == 'z' or userInput == 'Z') { + else if (userInput == 'O') { + _keyboard_state = TRANSMIT_LOG; //Transmit data (work in progress) + } + else if (userInput == 'Z') { pc().printf("FSG FSM States: \n\r"); string string_state; @@ -1390,35 +1429,6 @@ _timeout = currentStateStruct.timeout; //set timeout before exiting this function } -void StateMachine::printDirectory() { - //create a DirectoryList object that points to the local directory - DirectoryList mbed_dir( "/local" ); - - if ( mbed_dir.error_check() ) { - //error( "MBED directory could not be opened\r\n" ); - pc().printf("MBED directory could not be opened\r\n"); - } - - else { - pc().printf("\n\rFiles in MBED directory:\n\r"); - for ( int i = 0; i < mbed_dir.size(); i++ ) - pc().printf( "%s\r\n", mbed_dir[ i ].c_str() ); - } - - //SD CARD DIRECTORY (does not work for openlog) - DirectoryList sd_dir( "/sd" ); - - if ( sd_dir.error_check() ) { - //error( "MBED directory could not be opened\r\n" ); - pc().printf("SD directory could not be opened\r\n"); - } - else { - pc().printf("\n\rFiles in SD card directory:\n\r"); - for ( int i = 0; i < sd_dir.size(); i++ ) - pc().printf( "%s\r\n", sd_dir[ i ].c_str() ); - } -} - void StateMachine::printCurrentSdLog() { pc().printf("SD card log work in progress\n\r"); //might be worth saving the last few logs to the MBED...