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
StateMachine/StateMachine.cpp
- Committer:
- tnhnrl
- Date:
- 2017-11-28
- Revision:
- 23:434f04ef1fad
- Parent:
- 22:a10ee088403b
- Child:
- 24:c7d9b5bf3829
File content as of revision 23:434f04ef1fad:
#include "StateMachine.hpp" #include "StaticDefs.hpp" StateMachine::StateMachine() { _timeout = 20; // generic timeout for every state, seconds depthTolerance = 0.25; // depth tolerance for neutral finding exit critera pitchTolerance = 1.0; // pitch angle tolerance for neutral finding exit criteria bceFloatPosition = 300; // bce position for "float" states battFloatPosition = 50; // batt position for "broadcast" state depthCommand = 3.5; // user keyboard depth pitchCommand = -20.0; // user keyboard depth _neutral_sink_timer = 0; _neutral_rise_timer = 0; _level_timer = 0; previousPosition_mm = bce().getTravelLimit(); // Default is bce fully extended, overwritten in FIND_NEUTRAL _state = SIT_IDLE; // select starting state here isTimeoutRunning = false; // default timer to not running isSubStateTimerRunning = false; // default timer to not running _state_counter = 0; _neutral_sub_state_active = false; _depth_KP = depthLoop().getControllerP(); // load current depth value _depth_KI = depthLoop().getControllerI(); // load current depth value _depth_KD = depthLoop().getControllerD(); // load current depth value _pitch_KP = pitchLoop().getControllerP(); // load current pitch value _pitch_KI = pitchLoop().getControllerI(); // load current pitch value _pitch_KD = pitchLoop().getControllerD(); // load current pitch value _neutral_bce_pos_mm = depthLoop().getOutputOffset(); //load current neutral buoyancy position offset _neutral_batt_pos_mm = pitchLoop().getOutputOffset(); //load current neutral buoyancy position offset _state_array_counter = 0; } //Finite State Machine (FSM) void StateMachine::runStateMachine() { //static bool runFirstNeutral = false; // finite state machine ... each state has at least one exit criteria switch (_state) { case SIT_IDLE : // there actually is no timeout for SIT_IDLE, but this enables some one-shot actions if (!isTimeoutRunning) { showMenu(); pc().printf("\r\n\nstate: SIT_IDLE\r\n"); isTimeoutRunning = true; // what is active? bce().pause(); batt().pause(); //reset sub FSM isSubStateTimerRunning = false; } // how exit? keyboard(); // keyboard function will change the state if needed break; case EMERGENCY_CLIMB : // start local state timer and init any other one-shot actions if (!isTimeoutRunning) { pc().printf("\r\n\nstate: EMERGENCY_CLIMB\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running isTimeoutRunning = true; // what needs to be started? bce().unpause(); batt().unpause(); // what are the commands? bce().setPosition_mm(bce().getTravelLimit()); batt().setPosition_mm(0.0); } // how exit? if (timer > _timeout) { pc().printf("EC: timed out\r\n"); _state = FLOAT_BROADCAST; timer.reset(); isTimeoutRunning = false; } else if (depthLoop().getPosition() < 0.2) { //if the depth is greater than 0.2 feet, go to float broadcast pc().printf("EC: depth: %3.1f, cmd: 0.5 [%0.1f sec]\r",depthLoop().getPosition(), timer.read()); _state = FLOAT_BROADCAST; timer.reset(); isTimeoutRunning = false; } break; case FIND_NEUTRAL : // start local state timer and init any other one-shot actions if (!isTimeoutRunning) { pc().printf("\r\n\nstate: FIND_NEUTRAL\n\r"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running isTimeoutRunning = true; // what needs to be started? bce().unpause(); batt().unpause(); //load the current position of the BCE piston into the sub-FSM //previousPosition_mm = bce().getPosition_mm(); pc().printf("FN: Neutral sequence, BCE piston at %3.1f\n\r", bce().getPosition_mm()); //first iteration goes into Neutral Finding Sub-FSM runNeutralStateMachine(); //send first state of sub-FSM } // how exit? (exit with the timer, if timer still running continue processing sub FSM) if (timer > _timeout) { pc().printf("FN: timed out [time: %0.1f sec]\r\n", timer.read()); _state = EMERGENCY_CLIMB; //new behavior (if this times out it emergency surfaces) timer.reset(); isTimeoutRunning = false; } //what is active? (neutral finding sub-function runs until completion) if (runNeutralStateMachine() == NEUTRAL_EXIT) { //if the current state is NEUTRAL_EXIT stop running the sub FSM //if successful, FIND_NEUTRAL then goes to RISE pc().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\n\n\r"); _state = RISE; } break; case DIVE : // start local state timer and init any other one-shot actions if (!isTimeoutRunning) { pc().printf("\r\n\nstate: DIVE\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running isTimeoutRunning = true; // what needs to be started? bce().unpause(); batt().unpause(); // what are the commands? depthLoop().setCommand(depthCommand); pitchLoop().setCommand(pitchCommand); pc().printf("DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand()); pc().printf("DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand()); } // how exit? if (timer > _timeout) { pc().printf("DIVE: timed out\n\n\r"); _state = RISE; //new behavior 11/17/2017 timer.reset(); isTimeoutRunning = false; } else if (depthLoop().getPosition() > depthLoop().getCommand()) { pc().printf("DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); _state = RISE; timer.reset(); isTimeoutRunning = false; } // what is active? pc().printf("DIVE: bce pos: %3.1f mm, batt pos: %3.1f mm (depth: %3.1f ft) (pitch: %3.1f deg)[%0.2f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), pitchLoop().getPosition(), timer.read()); bce().setPosition_mm(depthLoop().getOutput()); batt().setPosition_mm(pitchLoop().getOutput()); break; case RISE : // start local state timer and init any other one-shot actions if (!isTimeoutRunning) { pc().printf("\r\n\nstate: RISE\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running isTimeoutRunning = true; // what needs to be started? bce().unpause(); batt().unpause(); // what are the commands? depthLoop().setCommand(0.0); pitchLoop().setCommand(-pitchCommand); pc().printf("RISE: depth cmd: 0.0\r\n"); pc().printf("RISE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand()); } // how exit? if (timer > _timeout) { pc().printf("RISE: timed out\r\n"); _state = EMERGENCY_CLIMB; timer.reset(); isTimeoutRunning = false; } else if (depthLoop().getPosition() < depthLoop().getCommand()) { pc().printf("RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); _state = FLOAT_LEVEL; timer.reset(); isTimeoutRunning = false; } // what is active? pc().printf("RISE: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read()); bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors batt().setPosition_mm(pitchLoop().getOutput()); break; case FLOAT_LEVEL : // start local state timer and init any other one-shot actions if (!isTimeoutRunning) { pc().printf("\r\n\nstate: FLOAT_LEVEL\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running isTimeoutRunning = true; // what needs to be started? bce().unpause(); batt().unpause(); // what are the commands? bce().setPosition_mm(bceFloatPosition); pitchLoop().setCommand(0.0); } // how exit? if (timer > _timeout) { pc().printf("FL: timed out\r\n"); _state = FLOAT_BROADCAST; timer.reset(); isTimeoutRunning = false; } else if (fabs(imu().getPitch() - pitchLoop().getCommand()) < fabs(pitchTolerance)) { pc().printf("FL: pitch: %3.1f mm, set pos: %3.1f mm, deadband: %3.1f mm\r\n",imu().getPitch(), pitchLoop().getCommand(), pitchTolerance); _state = FLOAT_BROADCAST; timer.reset(); isTimeoutRunning = false; } // what is active? pc().printf("FL: pitchLoop output: %3.1f, batt pos: %3.1f, piston pos: %3.1f [%0.1f sec]\r", pitchLoop().getOutput(), batt().getPosition_mm(), bce().getPosition_mm(), timer.read()); batt().setPosition_mm(pitchLoop().getOutput()); break; case FLOAT_BROADCAST : // start local state timer and init any other one-shot actions if (!isTimeoutRunning) { pc().printf("\r\n\nstate: FLOAT_BROADCAST\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running isTimeoutRunning = true; // what needs to be started? bce().unpause(); batt().unpause(); // what are the commands? bce().setPosition_mm(bceFloatPosition); batt().setPosition_mm(battFloatPosition); } // how exit? if (timer > _timeout) { pc().printf("FB: timed out\r\n"); _state = SIT_IDLE; timer.reset(); isTimeoutRunning = false; } else if ( (fabs(bce().getPosition_mm() - bce().getSetPosition_mm()) < bce().getDeadband()) and (fabs(batt().getPosition_mm() - batt().getSetPosition_mm()) < batt().getDeadband()) ) { pc().printf("FB: position: %3.1f mm, set pos: %3.1f mm, deadband: %3.1f mm\r\n",bce().getPosition_mm(), bce().getSetPosition_mm(), bce().getDeadband()); _state = SIT_IDLE; timer.reset(); isTimeoutRunning = false; } // what is active? pc().printf("FB: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read()); break; case MULTI_DIVE : // start local state timer and init any other one-shot actions if (!isTimeoutRunning) { pc().printf("\r\n\nstate: MULTI-DIVE\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running isTimeoutRunning = true; // what needs to be started? bce().unpause(); batt().unpause(); //retrieve commands from structs (loaded from sequence.cfg file) float sequenceDepthCommand = currentStateStruct.depth; float sequencePitchCommand = currentStateStruct.pitch; // what are the commands? depthLoop().setCommand(sequenceDepthCommand); pitchLoop().setCommand(sequencePitchCommand); pc().printf("MULTI-DIVE: depth cmd: %3.1f ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand()); } // how exit? if (timer > _timeout) { pc().printf("\n\n\rMULTI-DIVE: timed out [time: %0.1f]\n\n\r", timer.read()); _state = MULTI_RISE; //new behavior 11/17/2017 timer.reset(); isTimeoutRunning = false; } else if (depthLoop().getPosition() > depthLoop().getCommand()) { pc().printf("MULTI-DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); _state = MULTI_RISE; timer.reset(); isTimeoutRunning = false; } // what is active? pc().printf("MULTI-DIVE: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read()); bce().setPosition_mm(depthLoop().getOutput()); batt().setPosition_mm(pitchLoop().getOutput()); break; case MULTI_RISE : // start local state timer and init any other one-shot actions if (!isTimeoutRunning) { pc().printf("\r\n\nstate: MULTI-RISE\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running isTimeoutRunning = true; // what needs to be started? bce().unpause(); batt().unpause(); //NEW: retrieve depth and pitch commands from config file struct // concept is to load this each time the multi-dive restarts stateMachine().getDiveSequence(); //retrieve just pitch command from struct float sequencePitchCommand = currentStateStruct.pitch; // what are the commands? (send back to 0.5 feet, not surface) // 11/21/2017 depthLoop().setCommand(0.5); pitchLoop().setCommand(-sequencePitchCommand); pc().printf("MULTI-RISE: depth cmd: 0.0 ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand()); } // how exit? if (timer > _timeout) { pc().printf("MULTI-RISE: timed out [time: %0.1f]\n\n\r", timer.read()); _state = EMERGENCY_CLIMB; timer.reset(); isTimeoutRunning = false; //reset multi-dive sequence to start _state_counter = 0; } else if (depthLoop().getPosition() < 0.5) { // depth is less than 0.5 (zero is surface level) pc().printf("MULTI-RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); //going to next state isTimeoutRunning = false; //successful dive-rise sequence CONTINUES the multi-dive sequence _state_counter++; //UPDATE THE SEQUENCE DATA HERE stateMachine().getDiveSequence(); //check if this is the end of the dive sequence //CHECK BEFORE ANYTHING ELSE that you have reached the "exit" state (Float_Level) if (currentStateStruct.state == FLOAT_LEVEL) { _state = FLOAT_LEVEL; return; } else _state = MULTI_DIVE; //have to stop this with the _state_counter variable! } // what is active? pc().printf("MULTI-RISE: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read()); bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors batt().setPosition_mm(pitchLoop().getOutput()); break; default : pc().printf("DEBUG: SIT_IDLE\n\r"); _state = SIT_IDLE; } } // output the keyboard menu for user's reference void StateMachine::showMenu() { pc().printf("\r\r\n\nKEYBOARD MENU:\r\r\n"); pc().printf(" N to find neutral\r\n"); pc().printf(" M to initiate multi-dive cycle\r\n"); pc().printf(" D to initiate dive cycle\r\n"); pc().printf(" R to initiate rise\r\n"); pc().printf(" L to float level\r\n"); pc().printf(" B to float at broadcast pitch\r\n"); pc().printf(" E to initiate emergency climb\r\n"); pc().printf(" H to run homing sequence on both BCE and Batt\r\n"); pc().printf(" T to tare the depth sensor\r\n"); pc().printf("[/] to change bce neutral position\r\n"); pc().printf("</> to change batt neutral position\r\n"); pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",pitchCommand); pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",depthCommand); pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout); pc().printf(" 1 BCE PID sub-menu\r\n"); pc().printf(" 2 BATT PID sub-menu\r\n"); pc().printf(" 3 Depth PID sub-menu\r\n"); pc().printf(" 4 Pitch PID sub-menu\r\n"); pc().printf(" C See sensor readings\r\n"); pc().printf(" ? to reset mbed\r\n"); } //Find Neutral sub finite state machine // Note: the sub-fsm only moves the pistons once at the start of each timer loop // (timer completes, move piston, timer completes, move piston, etc) int StateMachine::runNeutralStateMachine() { static int substate = NEUTRAL_FIRST_PITCH; //to start //pc().printf("Neutral FSM: substate: %d [time: %0.1f] (depth: %0.1f, pitch: %0.1f, pitch rate: %0.1f) (bce: %0.1f, batt: %0.1f, prev: %0.1f)\r", substate, timer.read(), depthLoop().getPosition(), pitchLoop().getPosition(), pitchLoop().getVelocity(), bce().getPosition_mm(), batt().getPosition_mm(), previousPosition_mm); switch (substate) { case NEUTRAL_SINKING : //start the 10 second timer if (!isSubStateTimerRunning) { _neutral_sink_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds pc().printf("\r\n\nSTART NEUTRAL_SINKING (next retraction at %0.1f sec) [current time: %0.1f]\n\r", _neutral_sink_timer, timer.read()); // what are the commands? //move piston at start of sequence (retract 10 mm) previousPosition_mm = bce().getPosition_mm() - 5; bce().setPosition_mm(previousPosition_mm); //no depth command pc().printf("NEUTRAL_SINKING: Retracting piston 10 mm (sent cmd: %0.1f) [CMD actual: %0.1f]\n\r",previousPosition_mm, bce().getSetPosition_mm()); // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state pitchLoop().setCommand(0.0); pc().printf("NEUTRAL_SINKING: pitch cmd: %3.1f\r\n",pitchLoop().getCommand()); isSubStateTimerRunning = true; //disable this block after one iteration } // how exit? //once reached the travel limit, no need to keep trying, so exit if (bce().getPosition_mm() <= 0) { pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT STATE NEUTRAL EXIT)\n\r", bce().getPosition_mm()); substate = NEUTRAL_EXIT; } //once deeper than the commanded setpoint... else if (depthLoop().getPosition() > depthCommand) { substate = NEUTRAL_SLOWLY_RISE; // next state isSubStateTimerRunning = false; //reset the sub state timer } // what is active? //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint if (timer.read() >= _neutral_sink_timer) { pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE!"); pc().printf("\r\n\n (BATT POS: %0.1f) retract 10 mm [current time: %3.1f]\r\n", previousPosition_mm, timer.read()); isSubStateTimerRunning = false; //reset the sub state timer } // what is active? batt().setPosition_mm(pitchLoop().getOutput()); // pitch outer loop is running break; case NEUTRAL_SLOWLY_RISE: if (!isSubStateTimerRunning) { _neutral_rise_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds pc().printf("\r\n\nSTART NEUTRAL_SLOWLY_RISE (next retraction at %0.1f sec) [current time: %0.1f]\r\n",_neutral_rise_timer,timer.read()); isSubStateTimerRunning = true; //disable this block after one iteration // what are the commands? //move piston at start of sequence (extend) previousPosition_mm = bce().getPosition_mm() + 2; bce().setPosition_mm(previousPosition_mm); //no depth command pc().printf("NEUTRAL_SLOWLY_RISE: Extending piston 2 mm (sent cmd: %0.1f) [CMD actual: %0.1f]\n\r",previousPosition_mm, bce().getSetPosition_mm()); // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state pitchLoop().setCommand(0.0); pc().printf("NEUTRAL_SLOWLY_RISE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand()); } // how exit? //once at full travel limit and haven't yet risen, time to give up and exit if (bce().getPosition_mm() >= bce().getTravelLimit()) { substate = NEUTRAL_EXIT; } //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration else if (depthLoop().getVelocity() < 0) { //less than zero ft/s pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate below 0 ft/s [time: %0.1f]\r\n", timer.read()); pc().printf("\r\n\n ###################### NEXT STATE NEUTRAL_CHECK_PITCH ###################### \r\n"); substate = NEUTRAL_CHECK_PITCH; } // what is active? //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint if (timer.read() >= _neutral_sink_timer) { pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE TIMER COMPLETE!"); pc().printf("\r\n\n (BATT POS: %0.1f) extend 1 mm [timer: %0.1f]\r\n", previousPosition_mm, timer.read()); isSubStateTimerRunning = false; // reset the sub state timer } // what is active? batt().setPosition_mm(pitchLoop().getOutput()); // pitch outer loop is running break; case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired case NEUTRAL_FIRST_PITCH : // start local state timer and init any other one-shot actions //pc().printf("\n\rNEUTRAL_FIRST_PITCH or NEUTRAL_CHECK_PITCH\n\r"); if (!isSubStateTimerRunning) { _level_timer = timer.read() + 10; // record time when this block is entered and add several seconds pc().printf("\r\n\nSTART NEUTRAL_CHECK_PITCH (next move in %0.1f sec)\r\n",_level_timer - timer.read()); isSubStateTimerRunning = true; // out of the initialization block // what are the commands? //pc().printf("Neutral Check Pitch: moving battery in 1mm increments\n\r"); if (imu().getPitch() > 2) { // nose is high batt().setPosition_mm(batt().getPosition_mm() + 1); // move battery forward pc().printf("\n\rNeutral Check Pitch: moving battery FWD in 1mm increments\n\n\r"); } else if (imu().getPitch() < -2) { // nose is low batt().setPosition_mm(batt().getPosition_mm() - 1); // move battery aft pc().printf("\n\rNeutral Check Pitch: moving battery AFT in 1mm increments\n\n\r"); } } // how exit? //pitch angle and pitch rate within small tolerance //benchtop tests confirm angle needs to be around 2 degrees if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) { pc().printf("\n\rFound Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug // found level, but don't need to save anything this time if (substate == NEUTRAL_FIRST_PITCH) { pc().printf("\n\rsubstate: NEUTRAL_FIRST_PITCH \n\r"); //debug substate = NEUTRAL_SINKING; // next state starts the sinking pc().printf("\n\rsubstate: %d \n\r", substate); //debug // save this neutral (not in file) for pitch pitchLoop().setOutputOffset(batt().getPosition_mm()); } // found level and at depth too, so save it all now else { //save positions locally _neutral_batt_pos_mm = batt().getPosition_mm(); _neutral_bce_pos_mm = bce().getPosition_mm(); //set the neutral positions in each outer loop depthLoop().setOutputOffset(_neutral_bce_pos_mm); pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // save into the depth.txt and pitch.txt files configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset pc().printf("\n\rSaving Positions: BCE: %0.1f mm, BATT: %0.1f\n\n\r",_neutral_bce_pos_mm,_neutral_batt_pos_mm); substate = NEUTRAL_EXIT; } } // what is active? //once timer complete, reset the timeout so the state one-shot entry will move the setpoint if (timer.read() >= _level_timer) { pc().printf("\r\n\nlevel timer COMPLETE!"); pc().printf("\r\n\n (BATT POS: %0.1f) moving 1 mm [timer: %0.1f]\r\n", batt().getPosition_mm(), timer.read()); isSubStateTimerRunning = false; // reset the sub state timer } //pc().printf("NEUTRAL_CHECK_PITCH (time: %3.1f) [%0.1f deg, %0.1f deg/s] \n\r", timer.read(), pitchLoop().getPosition(), pitchLoop().getVelocity()); //debug break; //this state could be removed, it is only used as a transition but is needed to stop entering this function case NEUTRAL_EXIT : //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed pc().printf("substate: NEUTRAL_EXIT\n\r"); break; default : pc().printf("how did we get to substate default?\n\r"); //debug //a default within the sub-state machine substate = NEUTRAL_EXIT; break; } //check if the sub-FSM has completed if (substate == NEUTRAL_EXIT) { //reset substate to NEUTRAL_FIRST_PITCH (first state) substate = NEUTRAL_FIRST_PITCH; //indicate to outer FSM that sub-FSM has completed _state_array[_state_array_counter] = NEUTRAL_EXIT; //record what state the hardware is currently in _state_array_counter++; //increment at the end of each run pc().printf("********************************DID YOU EXIT? *******************************\n\n\r"); return NEUTRAL_EXIT; } else { _state_array[_state_array_counter] = substate; //record what state the hardware is currently in _state_array_counter++; //increment at the end of each run return substate; //otherwise continue running through sequence } } // keyboard runs independently of the state machine, handling one key at a time //keyboard updates the desired _keyboard_state that is used in the state machine //and only allows input when the state is "idle" void StateMachine::keyboard() { char userInput; // check keyboard and make settings changes as requested // states can be changed only at the start of a sequence (when the system is in SIT_IDLE) int _keyboard_state = -1; //made this a local variable because it was retaining the last keyboard state if (pc().readable() && _state == SIT_IDLE) { // get the key userInput = pc().getc(); // keyboard has to reset timer each time it's used isTimeoutRunning = false; // check command against desired control buttons if (userInput == 'D' or userInput == 'd') { _keyboard_state = DIVE; } else if (userInput == 'N' or userInput == 'n') { _keyboard_state = FIND_NEUTRAL; } else if (userInput == 'M' or userInput == 'm') { //currently does not run if there is no file. //need to add method to Sequence Controller that returns -1 // or some check that insures you cannot run the dive sequence without a file stateMachine().getDiveSequence(); //get first sequence on keyboard press _keyboard_state = currentStateStruct.state; pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles } else if (userInput == 'R' or userInput == 'r') { _keyboard_state = RISE; } else if (userInput == 'L' or userInput == 'l') { _keyboard_state = FLOAT_LEVEL; } else if (userInput == 'B' or userInput == 'b') { _keyboard_state = FLOAT_BROADCAST; } else if (userInput == 'E' or userInput == 'e') { _keyboard_state = EMERGENCY_CLIMB; } 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 == 'T' or userInput == 't') { pc().printf("taring depth sensor\r\n"); pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt()); wait(0.1); depth().tare(); // tares to ambient (do on surface) pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt()); } else if (userInput == '[' or userInput == '{') { depthLoop().setOutputOffset(depthLoop().getOutputOffset() - 1); // decrease the bce neutral setpoint pc().printf("Adjusting bce neutral position. new: %3.1f\r\n",depthLoop().getOutputOffset()); } else if (userInput == ']' or userInput == '}') { depthLoop().setOutputOffset(depthLoop().getOutputOffset() + 1); // increase the bce neutral setpoint pc().printf("Adjusting bce neutral position. new: %3.1f\r\n",depthLoop().getOutputOffset()); } else if (userInput == '<' or userInput == ',') { pitchLoop().setOutputOffset(pitchLoop().getOutputOffset() - 1); // decrease the batt neutral setpoint pc().printf("Adjusting batt neutral position. new: %3.1f\r\n",pitchLoop().getOutputOffset()); } else if (userInput == '>' or userInput == '.') { pitchLoop().setOutputOffset(pitchLoop().getOutputOffset() + 1); // increase the batt neutral setpoint pc().printf("Adjusting batt neutral position. new: %3.1f\r\n",pitchLoop().getOutputOffset()); } else if (userInput == '?') { pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n"); wait(0.5); mbed_reset(); } // change settings else if (userInput == 'Q' or userInput == 'q') { pitchCommand -= 0.5; //decrement the pitch setpoint pitchLoop().setCommand(pitchCommand); pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand()); } else if (userInput == 'W' or userInput == 'w') { pitchCommand += 0.5; //increment the pitch setpoint pitchLoop().setCommand(pitchCommand); pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand()); } else if (userInput == 'A' or userInput == 'a') { depthCommand -= 0.5; //decrement the depth setpoint depthLoop().setCommand(depthCommand); pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand()); } else if (userInput == 'S' or userInput == 's') { depthCommand += 0.5; //increment the depth setpoint depthLoop().setCommand(depthCommand); pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand()); } else if (userInput == '-') { _timeout -= 10.0; //decrement the timeout pc().printf(">>> timeout decreased: %d\r\n", _timeout); } else if (userInput == '=' or userInput == '+') { _timeout += 10.0; //increment the timeout pc().printf(">>> timeout increased: %d\r\n", _timeout); } // go to sub-menus for the PID gains (this is blocking) else if (userInput == '1') { keyboard_menu_BCE_PID_settings(); } else if (userInput == '2') { keyboard_menu_BATT_PID_settings(); } else if (userInput == '3') { keyboard_menu_DEPTH_PID_settings(); } else if (userInput == '4') { keyboard_menu_PITCH_PID_settings(); } else if (userInput == 'C' or userInput == 'c') { pc().printf("depth: %3.1f\r\n",depthLoop().getPosition()); pc().printf("pitch: %3.1f\r\n",imu().getPitch()); pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm()); pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm()); pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm()); pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm()); pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand()); pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand()); pc().printf("Neutral sub FSM states: \n\r"); for (int i = 0; i < _state_array_counter; i++) { pc().printf("state #%d: %d\n\r", i, _state_array[i]); } } //when you read the keyboard successfully, change the state _state = _keyboard_state; //set state at the end of this function } } void StateMachine::keyboard_menu_BCE_PID_settings() { char PID_key; float gain_step_size = 0.01; // modify this to change gain step size float KP = bce().getControllerP(); // load current value float KI = bce().getControllerI(); // load current global value float KD = bce().getControllerD(); // load current global value // show the menu pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)"); pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r"); pc().printf("bce P: %3.2f, I: %3.2f, D %3.2f, zero %d, limit %3.0f mm, slope %3.3f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope()); // handle the key presses while(1) { // get the user's keystroke from either of the two inputs if (pc().readable()) { PID_key = pc().getc(); } else { continue; // didn't get a user input, so keep waiting for it } // handle the user's key input if (PID_key == '-') { KP -= gain_step_size; pc().printf("P gain: %0.5f \r\n", KP); } else if (PID_key == '=') { KP += gain_step_size; pc().printf("P gain: %0.5f \r\n", KP); } else if (PID_key == '[') { KI -= gain_step_size; pc().printf("I gain: %0.5f \r\n", KI); } else if (PID_key == ']') { KI += gain_step_size; pc().printf("I gain: %0.5f \r\n", KI); } else if (PID_key == ';') { KD -= gain_step_size; pc().printf("D gain: %0.5f \r\n", KD); } else if (PID_key == '\'') { KD += gain_step_size; pc().printf("D gain: %0.5f \r\n", KD); } else if (PID_key == 'S') { // user wants to save these modified values // set values bce().setControllerP(KP); bce().setControllerI(KI); bce().setControllerD(KD); // save into "PID.cfg" //Config_File_IO().write_manual_position_PID_values_to_config(batt_position_P,batt_position_I,batt_position_D,bce_position_P,bce_position_I,bce_position_D); break; //exit the while loop } else if (PID_key == 'X') { break; //exit the while loop } else { pc().printf("\n\rThis key does nothing here. "); } } } void StateMachine::keyboard_menu_BATT_PID_settings() { char PID_key; float gain_step_size = 0.01; // modify this to change gain step size float KP = batt().getControllerP(); // load current global value float KI = batt().getControllerI(); // load current global value float KD = batt().getControllerD(); // load current global value // print the menu pc().printf("\n\r2: Battery Motor PID gain settings (MENU)"); pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); pc().printf("batt P: %3.2f, I: %3.2f, D %3.2f, zero %d, limit %3.0f mm, slope %3.3f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope()); // handle the key presses while(1) { // get the user's keystroke from either of the two inputs if (pc().readable()) { PID_key = pc().getc(); } else { continue; // didn't get a user input, so keep waiting for it } // handle the user's key input if (PID_key == '-') { KP -= gain_step_size; pc().printf("\rP gain: %0.5f ", KP); } else if (PID_key == '=') { KP += gain_step_size; pc().printf("\rP gain: %0.5f ", KP); } else if (PID_key == '[') { KI -= gain_step_size; pc().printf("\rI gain: %0.5f ", KI); } else if (PID_key == ']') { KI += gain_step_size; pc().printf("\rI gain: %0.5f ", KI); } else if (PID_key == ';') { KD -= gain_step_size; pc().printf("\rD gain: %0.5f ", KD); } else if (PID_key == '\'') { KD += gain_step_size; pc().printf("\rD gain: %0.5f ", KD); } else if (PID_key == 'S') { // user wants to save the modified values // set global values batt().setControllerP(KP); batt().setControllerI(KI); batt().setControllerD(KD); // save to "PID.cfg" file //Config_File_IO().write_manual_position_PID_values_to_config(batt_position_P,batt_position_I,batt_position_D,bce_position_P,bce_position_I,bce_position_D); break; //exit the while loop } else if (PID_key == 'X') { break; //exit the while loop } else { pc().printf("This key does nothing here.\r"); } } } void StateMachine::keyboard_menu_DEPTH_PID_settings() { char PID_key; float gain_step_size = 0.01; // modify this to change gain step size // show the menu pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)"); pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r"); pc().printf("depth P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset()); // handle the key presses while(1) { // get the user's keystroke from either of the two inputs if (pc().readable()) { PID_key = pc().getc(); } else { continue; // didn't get a user input, so keep waiting for it } // handle the user's key input if (PID_key == '-') { _depth_KP -= gain_step_size; pc().printf("P gain: %0.5f \r\n", _depth_KP); } else if (PID_key == '=') { _depth_KP += gain_step_size; pc().printf("P gain: %0.5f \r\n", _depth_KP); } else if (PID_key == '[') { _depth_KI -= gain_step_size; pc().printf("I gain: %0.5f \r\n", _depth_KI); } else if (PID_key == ']') { _depth_KI += gain_step_size; pc().printf("I gain: %0.5f \r\n", _depth_KI); } else if (PID_key == ';') { _depth_KD -= gain_step_size; pc().printf("D gain: %0.5f \r\n", _depth_KD); } else if (PID_key == '\'') { _depth_KD += gain_step_size; pc().printf("D gain: %0.5f \r\n", _depth_KD); } else if (PID_key == 'S') { // user wants to save these settings // set global values depthLoop().setControllerP(_depth_KP); depthLoop().setControllerI(_depth_KI); depthLoop().setControllerD(_depth_KD); // save depth PID values for outer loop configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); break; //exit the while loop } else if (PID_key == 'X') { break; //exit the while loop } else { pc().printf("\n\rThis key does nothing here. "); } } } void StateMachine::keyboard_menu_PITCH_PID_settings() { char PID_key; float gain_step_size = 0.01; // modify this to change gain step size // print the menu pc().printf("\n\r2: Battery Motor PID gain settings (MENU)"); pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); pc().printf("pitch P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset()); // handle the key presses while(1) { // get the user's keystroke from either of the two inputs if (pc().readable()) { PID_key = pc().getc(); } else { continue; // didn't get a user input, so keep waiting for it } // handle the user's key input if (PID_key == '-') { _pitch_KP -= gain_step_size; pc().printf("\rP gain: %0.5f ", _pitch_KP); } else if (PID_key == '=') { _pitch_KP += gain_step_size; pc().printf("\rP gain: %0.5f ", _pitch_KP); } else if (PID_key == '[') { _pitch_KI -= gain_step_size; pc().printf("\rI gain: %0.5f ", _pitch_KI); } else if (PID_key == ']') { _pitch_KI += gain_step_size; pc().printf("\rI gain: %0.5f ", _pitch_KI); } else if (PID_key == ';') { _pitch_KD -= gain_step_size; pc().printf("\rD gain: %0.5f ", _pitch_KD); } else if (PID_key == '\'') { _pitch_KD += gain_step_size; pc().printf("\rD gain: %0.5f ", _pitch_KD); } else if (PID_key == 'S') { // user wants to save the modified values // set global values pitchLoop().setControllerP(_pitch_KP); pitchLoop().setControllerI(_pitch_KI); pitchLoop().setControllerD(_pitch_KD); // save pitch PID values for outer loop configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); break; //exit the while loop } else if (PID_key == 'X') { break; //exit the while loop } else { pc().printf("This key does nothing here.\r"); } } } float StateMachine::getDepthCommand() { return depthCommand; } float StateMachine::getPitchCommand() { return pitchCommand; } void StateMachine::setState(int input_state) { //pc().printf("input_state: %d\n\r", input_state); //debug //_state = input_state; //changing wrong variable _state = input_state; } int StateMachine::getState() { return _state; //return the current state of the system } void StateMachine::setTimeout(float input_timeout) { _timeout = input_timeout; } void StateMachine::setDepthCommand(float input_depth_command) { depthCommand = input_depth_command; } void StateMachine::setPitchCommand(float input_pitch_command) { pitchCommand = input_pitch_command; } void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) { _neutral_batt_pos_mm = batt_pos_mm; _neutral_bce_pos_mm = bce_pos_mm; pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm); } int StateMachine::timeoutRunning() { return isTimeoutRunning; } //process one state at a time void StateMachine::getDiveSequence() { //iterate through this sequence using the FSM currentStateStruct.state = sequenceController().sequenceStructLoaded[_state_counter].state; currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_state_counter].timeout; currentStateStruct.depth = sequenceController().sequenceStructLoaded[_state_counter].depth; currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_state_counter].pitch; _timeout = currentStateStruct.timeout; //set timeout before exiting this function }