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@36:966a86937e17, 2017-12-20 (annotated)
- Committer:
- tnhnrl
- Date:
- Wed Dec 20 23:21:15 2017 +0000
- Revision:
- 36:966a86937e17
- Parent:
- 35:2f66ea4863d5
- Child:
- 37:357e98a929cc
Working version for pool testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tnhnrl | 16:3363b9f14913 | 1 | #include "StateMachine.hpp" |
tnhnrl | 16:3363b9f14913 | 2 | #include "StaticDefs.hpp" |
tnhnrl | 20:8987a9ae2bc7 | 3 | |
tnhnrl | 16:3363b9f14913 | 4 | StateMachine::StateMachine() { |
tnhnrl | 32:f2f8ae34aadc | 5 | _timeout = 20; // generic timeout for every state, seconds |
tnhnrl | 20:8987a9ae2bc7 | 6 | |
tnhnrl | 28:16c83a2fdefa | 7 | _pitchTolerance = 5.0; // pitch angle tolerance for FLOAT_LEVEL state |
tnhnrl | 20:8987a9ae2bc7 | 8 | |
tnhnrl | 28:16c83a2fdefa | 9 | _bceFloatPosition = bce().getTravelLimit(); // bce position for "float" states (max travel limit for BCE is 320 mm) |
tnhnrl | 28:16c83a2fdefa | 10 | _battFloatPosition = batt().getTravelLimit(); // batt position tail high for "broadcast" state (max travel limit for battery is 75 mm) |
tnhnrl | 20:8987a9ae2bc7 | 11 | |
tnhnrl | 32:f2f8ae34aadc | 12 | _depth_command = 2.0; // user keyboard depth (default) |
tnhnrl | 32:f2f8ae34aadc | 13 | _pitch_command = -20.0; // user keyboard pitch (default) |
tnhnrl | 17:7c16b5671d0e | 14 | |
tnhnrl | 28:16c83a2fdefa | 15 | _neutral_timer = 0; //timer used in FIND_NEUTRAL sub-FSM |
tnhnrl | 20:8987a9ae2bc7 | 16 | |
tnhnrl | 28:16c83a2fdefa | 17 | _state = SIT_IDLE; // select starting state here |
tnhnrl | 28:16c83a2fdefa | 18 | _isTimeoutRunning = false; // default timer to not running |
tnhnrl | 28:16c83a2fdefa | 19 | _isSubStateTimerRunning = false; // default timer to not running |
tnhnrl | 17:7c16b5671d0e | 20 | |
tnhnrl | 24:c7d9b5bf3829 | 21 | _multi_dive_counter = 0; |
tnhnrl | 21:38c8544db6f4 | 22 | |
tnhnrl | 21:38c8544db6f4 | 23 | _neutral_sub_state_active = false; |
tnhnrl | 17:7c16b5671d0e | 24 | |
tnhnrl | 21:38c8544db6f4 | 25 | _depth_KP = depthLoop().getControllerP(); // load current depth value |
tnhnrl | 21:38c8544db6f4 | 26 | _depth_KI = depthLoop().getControllerI(); // load current depth value |
tnhnrl | 21:38c8544db6f4 | 27 | _depth_KD = depthLoop().getControllerD(); // load current depth value |
tnhnrl | 21:38c8544db6f4 | 28 | |
tnhnrl | 21:38c8544db6f4 | 29 | _pitch_KP = pitchLoop().getControllerP(); // load current pitch value |
tnhnrl | 21:38c8544db6f4 | 30 | _pitch_KI = pitchLoop().getControllerI(); // load current pitch value |
tnhnrl | 21:38c8544db6f4 | 31 | _pitch_KD = pitchLoop().getControllerD(); // load current pitch value |
tnhnrl | 21:38c8544db6f4 | 32 | |
tnhnrl | 21:38c8544db6f4 | 33 | _neutral_bce_pos_mm = depthLoop().getOutputOffset(); //load current neutral buoyancy position offset |
tnhnrl | 21:38c8544db6f4 | 34 | _neutral_batt_pos_mm = pitchLoop().getOutputOffset(); //load current neutral buoyancy position offset |
tnhnrl | 23:434f04ef1fad | 35 | |
tnhnrl | 28:16c83a2fdefa | 36 | _state_array_counter = 1; //used to iterate through and record states |
tnhnrl | 28:16c83a2fdefa | 37 | _substate_array_counter = 0; //used to iterate through and record substates |
tnhnrl | 28:16c83a2fdefa | 38 | |
tnhnrl | 28:16c83a2fdefa | 39 | _state_array[0] = SIT_IDLE; //system starts in the SIT_IDLE state, record this |
tnhnrl | 24:c7d9b5bf3829 | 40 | |
tnhnrl | 30:2964617e7676 | 41 | _substate = NEUTRAL_SINKING; //start sub-FSM in NEUTRAL_SINKING |
tnhnrl | 28:16c83a2fdefa | 42 | _previous_substate = -1; //to start sub-FSM |
tnhnrl | 28:16c83a2fdefa | 43 | _previous_state = -1; //for tracking FSM states |
tnhnrl | 28:16c83a2fdefa | 44 | |
tnhnrl | 28:16c83a2fdefa | 45 | _max_recorded_depth_neutral = -99; //float to record max depth |
tnhnrl | 28:16c83a2fdefa | 46 | _max_recorded_depth_dive = -99; //float to record max depth |
tnhnrl | 32:f2f8ae34aadc | 47 | |
tnhnrl | 32:f2f8ae34aadc | 48 | _neutral_sink_command_mm = -2.5; //defaults for neutral finding sub-FSM |
tnhnrl | 32:f2f8ae34aadc | 49 | _neutral_rise_command_mm = 2.0; |
tnhnrl | 32:f2f8ae34aadc | 50 | _neutral_pitch_command_mm = 0.5; |
tnhnrl | 32:f2f8ae34aadc | 51 | |
tnhnrl | 32:f2f8ae34aadc | 52 | _max_recorded_auto_neutral_depth = -99; |
tnhnrl | 32:f2f8ae34aadc | 53 | |
tnhnrl | 32:f2f8ae34aadc | 54 | _file_closed = true; |
tnhnrl | 16:3363b9f14913 | 55 | } |
tnhnrl | 20:8987a9ae2bc7 | 56 | |
tnhnrl | 17:7c16b5671d0e | 57 | //Finite State Machine (FSM) |
tnhnrl | 34:9b66c5188051 | 58 | int StateMachine::runStateMachine() { |
tnhnrl | 16:3363b9f14913 | 59 | // finite state machine ... each state has at least one exit criteria |
tnhnrl | 17:7c16b5671d0e | 60 | switch (_state) { |
tnhnrl | 16:3363b9f14913 | 61 | case SIT_IDLE : |
tnhnrl | 28:16c83a2fdefa | 62 | case KEYBOARD: |
tnhnrl | 16:3363b9f14913 | 63 | // there actually is no timeout for SIT_IDLE, but this enables some one-shot actions |
tnhnrl | 28:16c83a2fdefa | 64 | if (!_isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 65 | showMenu(); |
tnhnrl | 16:3363b9f14913 | 66 | pc().printf("\r\n\nstate: SIT_IDLE\r\n"); |
tnhnrl | 28:16c83a2fdefa | 67 | _isTimeoutRunning = true; |
tnhnrl | 20:8987a9ae2bc7 | 68 | |
tnhnrl | 16:3363b9f14913 | 69 | // what is active? |
tnhnrl | 16:3363b9f14913 | 70 | bce().pause(); |
tnhnrl | 16:3363b9f14913 | 71 | batt().pause(); |
tnhnrl | 32:f2f8ae34aadc | 72 | |
tnhnrl | 17:7c16b5671d0e | 73 | //reset sub FSM |
tnhnrl | 28:16c83a2fdefa | 74 | _isSubStateTimerRunning = false; |
tnhnrl | 32:f2f8ae34aadc | 75 | |
tnhnrl | 32:f2f8ae34aadc | 76 | //close the MBED file |
tnhnrl | 32:f2f8ae34aadc | 77 | _file_closed = true; |
tnhnrl | 16:3363b9f14913 | 78 | } |
tnhnrl | 20:8987a9ae2bc7 | 79 | |
tnhnrl | 16:3363b9f14913 | 80 | // how exit? |
tnhnrl | 20:8987a9ae2bc7 | 81 | keyboard(); // keyboard function will change the state if needed |
tnhnrl | 16:3363b9f14913 | 82 | break; |
tnhnrl | 20:8987a9ae2bc7 | 83 | |
tnhnrl | 16:3363b9f14913 | 84 | case EMERGENCY_CLIMB : |
tnhnrl | 16:3363b9f14913 | 85 | // start local state timer and init any other one-shot actions |
tnhnrl | 28:16c83a2fdefa | 86 | if (!_isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 87 | pc().printf("\r\n\nstate: EMERGENCY_CLIMB\r\n"); |
tnhnrl | 16:3363b9f14913 | 88 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 89 | timer.start(); // background timer starts running |
tnhnrl | 28:16c83a2fdefa | 90 | _isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 91 | |
tnhnrl | 16:3363b9f14913 | 92 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 93 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 94 | batt().unpause(); |
tnhnrl | 20:8987a9ae2bc7 | 95 | |
tnhnrl | 20:8987a9ae2bc7 | 96 | // what are the commands? |
tnhnrl | 16:3363b9f14913 | 97 | bce().setPosition_mm(bce().getTravelLimit()); |
tnhnrl | 16:3363b9f14913 | 98 | batt().setPosition_mm(0.0); |
tnhnrl | 32:f2f8ae34aadc | 99 | |
tnhnrl | 32:f2f8ae34aadc | 100 | //create the log file (works only if the file is closed) |
tnhnrl | 34:9b66c5188051 | 101 | ////////createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 102 | |
tnhnrl | 32:f2f8ae34aadc | 103 | //show that this is the start of a new EMERGENCY_CLIMB sequence |
tnhnrl | 32:f2f8ae34aadc | 104 | recordState(_state); |
tnhnrl | 32:f2f8ae34aadc | 105 | |
tnhnrl | 32:f2f8ae34aadc | 106 | //this was missing |
tnhnrl | 32:f2f8ae34aadc | 107 | _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 108 | recordData(_state); |
tnhnrl | 16:3363b9f14913 | 109 | } |
tnhnrl | 20:8987a9ae2bc7 | 110 | |
tnhnrl | 16:3363b9f14913 | 111 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 112 | if (timer > _timeout) { |
tnhnrl | 16:3363b9f14913 | 113 | pc().printf("EC: timed out\r\n"); |
tnhnrl | 21:38c8544db6f4 | 114 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 115 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 116 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 117 | } |
tnhnrl | 26:7e118fc02eea | 118 | else if (depthLoop().getPosition() < 2.0) { //if the depth is greater than 0.2 feet, go to float broadcast |
tnhnrl | 21:38c8544db6f4 | 119 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 120 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 121 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 122 | } |
tnhnrl | 32:f2f8ae34aadc | 123 | |
tnhnrl | 34:9b66c5188051 | 124 | //print status to screen continuously |
tnhnrl | 34:9b66c5188051 | 125 | pc().printf("EC: depth: %3.1f (BCE_cmd: %0.1f), pitch: %0.1f deg [%0.1f sec]\r",depthLoop().getPosition(),bce().getPosition_mm(),pitchLoop().getPosition(),timer.read()); |
tnhnrl | 34:9b66c5188051 | 126 | |
tnhnrl | 32:f2f8ae34aadc | 127 | //record data every 5 seconds |
tnhnrl | 34:9b66c5188051 | 128 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 129 | |
tnhnrl | 16:3363b9f14913 | 130 | break; |
tnhnrl | 20:8987a9ae2bc7 | 131 | |
tnhnrl | 16:3363b9f14913 | 132 | case FIND_NEUTRAL : |
tnhnrl | 20:8987a9ae2bc7 | 133 | // start local state timer and init any other one-shot actions |
tnhnrl | 28:16c83a2fdefa | 134 | if (!_isTimeoutRunning) { |
tnhnrl | 21:38c8544db6f4 | 135 | pc().printf("\r\n\nstate: FIND_NEUTRAL\n\r"); |
tnhnrl | 16:3363b9f14913 | 136 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 137 | timer.start(); // background timer starts running |
tnhnrl | 28:16c83a2fdefa | 138 | _isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 139 | |
tnhnrl | 16:3363b9f14913 | 140 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 141 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 142 | batt().unpause(); |
tnhnrl | 28:16c83a2fdefa | 143 | bce().setPosition_mm(_bceFloatPosition); |
tnhnrl | 24:c7d9b5bf3829 | 144 | batt().setPosition_mm(_neutral_batt_pos_mm); //set battery to close-to-neutral setting from config file |
tnhnrl | 17:7c16b5671d0e | 145 | |
tnhnrl | 24:c7d9b5bf3829 | 146 | //first iteration goes into Neutral Finding Sub-FSM |
tnhnrl | 24:c7d9b5bf3829 | 147 | //set the first state of the FSM, and start the sub-FSM |
tnhnrl | 30:2964617e7676 | 148 | _substate = NEUTRAL_SINKING; //first state in neutral sub-FSM is the pressure vessel sinking |
tnhnrl | 28:16c83a2fdefa | 149 | _previous_substate = -1; |
tnhnrl | 28:16c83a2fdefa | 150 | |
tnhnrl | 28:16c83a2fdefa | 151 | //save this state to the array |
tnhnrl | 30:2964617e7676 | 152 | _substate_array[_substate_array_counter] = NEUTRAL_SINKING; //save to state array |
tnhnrl | 28:16c83a2fdefa | 153 | _substate_array_counter++; |
tnhnrl | 28:16c83a2fdefa | 154 | |
tnhnrl | 32:f2f8ae34aadc | 155 | runNeutralStateMachine(); |
tnhnrl | 32:f2f8ae34aadc | 156 | |
tnhnrl | 32:f2f8ae34aadc | 157 | //create the log file (works only if the file is closed) |
tnhnrl | 34:9b66c5188051 | 158 | //createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 159 | |
tnhnrl | 32:f2f8ae34aadc | 160 | //show that this is the start of a new FIND_NEUTRAL sequence |
tnhnrl | 32:f2f8ae34aadc | 161 | recordState(_state); |
tnhnrl | 32:f2f8ae34aadc | 162 | |
tnhnrl | 32:f2f8ae34aadc | 163 | //this was missing |
tnhnrl | 32:f2f8ae34aadc | 164 | _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 165 | recordData(_state); |
tnhnrl | 16:3363b9f14913 | 166 | } |
tnhnrl | 20:8987a9ae2bc7 | 167 | |
tnhnrl | 20:8987a9ae2bc7 | 168 | // how exit? (exit with the timer, if timer still running continue processing sub FSM) |
tnhnrl | 17:7c16b5671d0e | 169 | if (timer > _timeout) { |
tnhnrl | 17:7c16b5671d0e | 170 | pc().printf("FN: timed out [time: %0.1f sec]\r\n", timer.read()); |
tnhnrl | 21:38c8544db6f4 | 171 | _state = EMERGENCY_CLIMB; //new behavior (if this times out it emergency surfaces) |
tnhnrl | 16:3363b9f14913 | 172 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 173 | _isTimeoutRunning = false; |
tnhnrl | 24:c7d9b5bf3829 | 174 | |
tnhnrl | 24:c7d9b5bf3829 | 175 | //record this to the NEUTRAL sub-FSM tracker |
tnhnrl | 28:16c83a2fdefa | 176 | _substate_array[_substate_array_counter] = EMERGENCY_CLIMB; //save to state array |
tnhnrl | 28:16c83a2fdefa | 177 | _substate_array_counter++; |
tnhnrl | 16:3363b9f14913 | 178 | } |
tnhnrl | 21:38c8544db6f4 | 179 | |
tnhnrl | 24:c7d9b5bf3829 | 180 | //what is active? (neutral finding sub-function runs until completion) |
tnhnrl | 24:c7d9b5bf3829 | 181 | //check if substate returned exit state, if so stop running the sub-FSM |
tnhnrl | 26:7e118fc02eea | 182 | else if (runNeutralStateMachine() == NEUTRAL_EXIT) { |
tnhnrl | 21:38c8544db6f4 | 183 | //if successful, FIND_NEUTRAL then goes to RISE |
tnhnrl | 21:38c8544db6f4 | 184 | pc().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\n\n\r"); |
tnhnrl | 21:38c8544db6f4 | 185 | _state = RISE; |
tnhnrl | 30:2964617e7676 | 186 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 187 | } |
tnhnrl | 32:f2f8ae34aadc | 188 | |
tnhnrl | 32:f2f8ae34aadc | 189 | //record data every 5 seconds |
tnhnrl | 34:9b66c5188051 | 190 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 191 | |
tnhnrl | 17:7c16b5671d0e | 192 | break; |
tnhnrl | 17:7c16b5671d0e | 193 | |
tnhnrl | 16:3363b9f14913 | 194 | case DIVE : |
tnhnrl | 16:3363b9f14913 | 195 | // start local state timer and init any other one-shot actions |
tnhnrl | 32:f2f8ae34aadc | 196 | |
tnhnrl | 28:16c83a2fdefa | 197 | if (!_isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 198 | pc().printf("\r\n\nstate: DIVE\r\n"); |
tnhnrl | 16:3363b9f14913 | 199 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 200 | timer.start(); // background timer starts running |
tnhnrl | 28:16c83a2fdefa | 201 | _isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 202 | |
tnhnrl | 16:3363b9f14913 | 203 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 204 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 205 | batt().unpause(); |
tnhnrl | 20:8987a9ae2bc7 | 206 | |
tnhnrl | 16:3363b9f14913 | 207 | // what are the commands? |
tnhnrl | 32:f2f8ae34aadc | 208 | depthLoop().setCommand(_depth_command); |
tnhnrl | 32:f2f8ae34aadc | 209 | pitchLoop().setCommand(_pitch_command); |
tnhnrl | 32:f2f8ae34aadc | 210 | |
tnhnrl | 34:9b66c5188051 | 211 | pc().printf("DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand()); |
tnhnrl | 34:9b66c5188051 | 212 | pc().printf("DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand()); |
tnhnrl | 28:16c83a2fdefa | 213 | |
tnhnrl | 28:16c83a2fdefa | 214 | //reset max dive depth |
tnhnrl | 28:16c83a2fdefa | 215 | _max_recorded_depth_dive = -99; //float to record max depth |
tnhnrl | 32:f2f8ae34aadc | 216 | |
tnhnrl | 32:f2f8ae34aadc | 217 | //create the log file (works only if the file is closed) |
tnhnrl | 34:9b66c5188051 | 218 | //createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 219 | |
tnhnrl | 32:f2f8ae34aadc | 220 | //show that this is the start of new dive sequence |
tnhnrl | 32:f2f8ae34aadc | 221 | recordState(_state); |
tnhnrl | 32:f2f8ae34aadc | 222 | |
tnhnrl | 32:f2f8ae34aadc | 223 | //this was missing |
tnhnrl | 32:f2f8ae34aadc | 224 | _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 225 | recordData(_state); |
tnhnrl | 16:3363b9f14913 | 226 | } |
tnhnrl | 20:8987a9ae2bc7 | 227 | |
tnhnrl | 16:3363b9f14913 | 228 | // how exit? |
tnhnrl | 32:f2f8ae34aadc | 229 | if (timer.read() > _timeout) { |
tnhnrl | 17:7c16b5671d0e | 230 | pc().printf("DIVE: timed out\n\n\r"); |
tnhnrl | 21:38c8544db6f4 | 231 | _state = RISE; //new behavior 11/17/2017 |
tnhnrl | 16:3363b9f14913 | 232 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 233 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 234 | } |
tnhnrl | 32:f2f8ae34aadc | 235 | else if (depthLoop().getPosition() > depthLoop().getCommand() - 0.5) { // including offset for low momentum approaches |
tnhnrl | 32:f2f8ae34aadc | 236 | pc().printf("DIVE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand()); |
tnhnrl | 21:38c8544db6f4 | 237 | _state = RISE; |
tnhnrl | 16:3363b9f14913 | 238 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 239 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 240 | } |
tnhnrl | 20:8987a9ae2bc7 | 241 | |
tnhnrl | 16:3363b9f14913 | 242 | // what is active? |
tnhnrl | 21:38c8544db6f4 | 243 | 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()); |
tnhnrl | 32:f2f8ae34aadc | 244 | bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors |
tnhnrl | 16:3363b9f14913 | 245 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 28:16c83a2fdefa | 246 | |
tnhnrl | 28:16c83a2fdefa | 247 | if (depthLoop().getPosition() > _max_recorded_depth_dive) { //debug |
tnhnrl | 28:16c83a2fdefa | 248 | _max_recorded_depth_dive = depthLoop().getPosition(); //new max depth recorded |
tnhnrl | 28:16c83a2fdefa | 249 | } |
tnhnrl | 32:f2f8ae34aadc | 250 | |
tnhnrl | 32:f2f8ae34aadc | 251 | //record data every 5 seconds |
tnhnrl | 34:9b66c5188051 | 252 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 253 | |
tnhnrl | 16:3363b9f14913 | 254 | break; |
tnhnrl | 16:3363b9f14913 | 255 | |
tnhnrl | 16:3363b9f14913 | 256 | case RISE : |
tnhnrl | 16:3363b9f14913 | 257 | // start local state timer and init any other one-shot actions |
tnhnrl | 32:f2f8ae34aadc | 258 | |
tnhnrl | 28:16c83a2fdefa | 259 | if (!_isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 260 | pc().printf("\r\n\nstate: RISE\r\n"); |
tnhnrl | 16:3363b9f14913 | 261 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 262 | timer.start(); // background timer starts running |
tnhnrl | 28:16c83a2fdefa | 263 | _isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 264 | |
tnhnrl | 16:3363b9f14913 | 265 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 266 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 267 | batt().unpause(); |
tnhnrl | 16:3363b9f14913 | 268 | |
tnhnrl | 16:3363b9f14913 | 269 | // what are the commands? |
tnhnrl | 28:16c83a2fdefa | 270 | depthLoop().setCommand(-1.0); //make sure to get towards the surface (saw issues at LASR pool) |
tnhnrl | 32:f2f8ae34aadc | 271 | pitchLoop().setCommand(-_pitch_command); |
tnhnrl | 34:9b66c5188051 | 272 | |
tnhnrl | 34:9b66c5188051 | 273 | pc().printf("RISE: depth cmd: %0.1f\r\n",depthLoop().getCommand()); |
tnhnrl | 34:9b66c5188051 | 274 | pc().printf("RISE: pitch cmd: %0.1f\r\n",pitchLoop().getCommand()); |
tnhnrl | 32:f2f8ae34aadc | 275 | |
tnhnrl | 32:f2f8ae34aadc | 276 | //create the log file (works only if the file is closed) |
tnhnrl | 34:9b66c5188051 | 277 | //createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 278 | |
tnhnrl | 32:f2f8ae34aadc | 279 | //show that this is the start of new rise sequence |
tnhnrl | 32:f2f8ae34aadc | 280 | recordState(_state); |
tnhnrl | 32:f2f8ae34aadc | 281 | |
tnhnrl | 32:f2f8ae34aadc | 282 | //this was missing |
tnhnrl | 32:f2f8ae34aadc | 283 | _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 284 | recordData(_state); |
tnhnrl | 16:3363b9f14913 | 285 | } |
tnhnrl | 20:8987a9ae2bc7 | 286 | |
tnhnrl | 16:3363b9f14913 | 287 | // how exit? |
tnhnrl | 32:f2f8ae34aadc | 288 | if (timer.read() > _timeout) { |
tnhnrl | 16:3363b9f14913 | 289 | pc().printf("RISE: timed out\r\n"); |
tnhnrl | 21:38c8544db6f4 | 290 | _state = EMERGENCY_CLIMB; |
tnhnrl | 16:3363b9f14913 | 291 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 292 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 293 | } |
tnhnrl | 32:f2f8ae34aadc | 294 | |
tnhnrl | 32:f2f8ae34aadc | 295 | //modified from (depthLoop().getPosition() < depthLoop().getCommand() + 0.5) |
tnhnrl | 32:f2f8ae34aadc | 296 | //did not work correctly in bench test (stuck in rise state) |
tnhnrl | 32:f2f8ae34aadc | 297 | else if (depthLoop().getPosition() < 0.5) { |
tnhnrl | 32:f2f8ae34aadc | 298 | pc().printf("RISE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand()); |
tnhnrl | 28:16c83a2fdefa | 299 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 300 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 301 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 302 | } |
tnhnrl | 20:8987a9ae2bc7 | 303 | |
tnhnrl | 20:8987a9ae2bc7 | 304 | // what is active? |
tnhnrl | 16:3363b9f14913 | 305 | 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()); |
tnhnrl | 17:7c16b5671d0e | 306 | bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors |
tnhnrl | 32:f2f8ae34aadc | 307 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 32:f2f8ae34aadc | 308 | |
tnhnrl | 32:f2f8ae34aadc | 309 | //record data every 5 seconds |
tnhnrl | 34:9b66c5188051 | 310 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 311 | |
tnhnrl | 16:3363b9f14913 | 312 | break; |
tnhnrl | 16:3363b9f14913 | 313 | |
tnhnrl | 16:3363b9f14913 | 314 | case FLOAT_LEVEL : |
tnhnrl | 16:3363b9f14913 | 315 | // start local state timer and init any other one-shot actions |
tnhnrl | 28:16c83a2fdefa | 316 | if (!_isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 317 | pc().printf("\r\n\nstate: FLOAT_LEVEL\r\n"); |
tnhnrl | 16:3363b9f14913 | 318 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 319 | timer.start(); // background timer starts running |
tnhnrl | 28:16c83a2fdefa | 320 | _isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 321 | |
tnhnrl | 16:3363b9f14913 | 322 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 323 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 324 | batt().unpause(); |
tnhnrl | 16:3363b9f14913 | 325 | |
tnhnrl | 20:8987a9ae2bc7 | 326 | // what are the commands? |
tnhnrl | 28:16c83a2fdefa | 327 | bce().setPosition_mm(_bceFloatPosition); |
tnhnrl | 16:3363b9f14913 | 328 | pitchLoop().setCommand(0.0); |
tnhnrl | 32:f2f8ae34aadc | 329 | |
tnhnrl | 32:f2f8ae34aadc | 330 | //create the log file (works only if the file is closed) |
tnhnrl | 34:9b66c5188051 | 331 | //createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 332 | |
tnhnrl | 32:f2f8ae34aadc | 333 | //show that this is the start of a new float level sequence |
tnhnrl | 32:f2f8ae34aadc | 334 | recordState(_state); |
tnhnrl | 32:f2f8ae34aadc | 335 | |
tnhnrl | 32:f2f8ae34aadc | 336 | //this was missing |
tnhnrl | 32:f2f8ae34aadc | 337 | _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 338 | recordData(_state); |
tnhnrl | 16:3363b9f14913 | 339 | } |
tnhnrl | 20:8987a9ae2bc7 | 340 | |
tnhnrl | 16:3363b9f14913 | 341 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 342 | if (timer > _timeout) { |
tnhnrl | 16:3363b9f14913 | 343 | pc().printf("FL: timed out\r\n"); |
tnhnrl | 21:38c8544db6f4 | 344 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 345 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 346 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 347 | } |
tnhnrl | 28:16c83a2fdefa | 348 | else if (fabs(imu().getPitch() - pitchLoop().getCommand()) < fabs(_pitchTolerance)) { //current tolerance is 5 degrees |
tnhnrl | 28:16c83a2fdefa | 349 | pc().printf("FL: pitch: %3.1f mm, set pos: %3.1f mm, deadband: %3.1f mm\r\n",imu().getPitch(), pitchLoop().getCommand(), _pitchTolerance); |
tnhnrl | 21:38c8544db6f4 | 350 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 351 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 352 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 353 | } |
tnhnrl | 20:8987a9ae2bc7 | 354 | |
tnhnrl | 16:3363b9f14913 | 355 | // what is active? |
tnhnrl | 16:3363b9f14913 | 356 | 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()); |
tnhnrl | 16:3363b9f14913 | 357 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 32:f2f8ae34aadc | 358 | |
tnhnrl | 32:f2f8ae34aadc | 359 | //record data every 5 seconds |
tnhnrl | 34:9b66c5188051 | 360 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 361 | |
tnhnrl | 16:3363b9f14913 | 362 | break; |
tnhnrl | 16:3363b9f14913 | 363 | |
tnhnrl | 16:3363b9f14913 | 364 | case FLOAT_BROADCAST : |
tnhnrl | 16:3363b9f14913 | 365 | // start local state timer and init any other one-shot actions |
tnhnrl | 28:16c83a2fdefa | 366 | if (!_isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 367 | pc().printf("\r\n\nstate: FLOAT_BROADCAST\r\n"); |
tnhnrl | 16:3363b9f14913 | 368 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 369 | timer.start(); // background timer starts running |
tnhnrl | 28:16c83a2fdefa | 370 | _isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 371 | |
tnhnrl | 16:3363b9f14913 | 372 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 373 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 374 | batt().unpause(); |
tnhnrl | 20:8987a9ae2bc7 | 375 | |
tnhnrl | 16:3363b9f14913 | 376 | // what are the commands? |
tnhnrl | 28:16c83a2fdefa | 377 | bce().setPosition_mm(_bceFloatPosition); |
tnhnrl | 28:16c83a2fdefa | 378 | batt().setPosition_mm(_battFloatPosition); |
tnhnrl | 32:f2f8ae34aadc | 379 | |
tnhnrl | 32:f2f8ae34aadc | 380 | //create the log file (works only if the file is closed) |
tnhnrl | 34:9b66c5188051 | 381 | //createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 382 | |
tnhnrl | 32:f2f8ae34aadc | 383 | //show that this is the start of a new float broadcast sequence |
tnhnrl | 32:f2f8ae34aadc | 384 | recordState(_state); |
tnhnrl | 32:f2f8ae34aadc | 385 | |
tnhnrl | 32:f2f8ae34aadc | 386 | //this was missing |
tnhnrl | 32:f2f8ae34aadc | 387 | _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 388 | recordData(_state); |
tnhnrl | 16:3363b9f14913 | 389 | } |
tnhnrl | 20:8987a9ae2bc7 | 390 | |
tnhnrl | 16:3363b9f14913 | 391 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 392 | if (timer > _timeout) { |
tnhnrl | 16:3363b9f14913 | 393 | pc().printf("FB: timed out\r\n"); |
tnhnrl | 21:38c8544db6f4 | 394 | _state = SIT_IDLE; |
tnhnrl | 16:3363b9f14913 | 395 | timer.reset(); |
tnhnrl | 32:f2f8ae34aadc | 396 | |
tnhnrl | 32:f2f8ae34aadc | 397 | //stop recording data |
tnhnrl | 35:2f66ea4863d5 | 398 | //mbedLogger().closeFile(); |
tnhnrl | 32:f2f8ae34aadc | 399 | |
tnhnrl | 28:16c83a2fdefa | 400 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 401 | } |
tnhnrl | 20:8987a9ae2bc7 | 402 | else if ( (fabs(bce().getPosition_mm() - bce().getSetPosition_mm()) < bce().getDeadband()) and |
tnhnrl | 20:8987a9ae2bc7 | 403 | (fabs(batt().getPosition_mm() - batt().getSetPosition_mm()) < batt().getDeadband()) ) { |
tnhnrl | 16:3363b9f14913 | 404 | 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()); |
tnhnrl | 21:38c8544db6f4 | 405 | _state = SIT_IDLE; |
tnhnrl | 16:3363b9f14913 | 406 | timer.reset(); |
tnhnrl | 32:f2f8ae34aadc | 407 | |
tnhnrl | 32:f2f8ae34aadc | 408 | //stop recording data |
tnhnrl | 35:2f66ea4863d5 | 409 | //mbedLogger().closeFile(); |
tnhnrl | 32:f2f8ae34aadc | 410 | |
tnhnrl | 28:16c83a2fdefa | 411 | _isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 412 | } |
tnhnrl | 20:8987a9ae2bc7 | 413 | |
tnhnrl | 20:8987a9ae2bc7 | 414 | // what is active? |
tnhnrl | 32:f2f8ae34aadc | 415 | pc().printf("FB: bce pos: %0.1f mm, batt pos: %0.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec] (CMD batt: %0.1f bce: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read(), bce().getSetPosition_mm(),batt().getSetPosition_mm()); |
tnhnrl | 32:f2f8ae34aadc | 416 | |
tnhnrl | 32:f2f8ae34aadc | 417 | //record data every 5 seconds |
tnhnrl | 34:9b66c5188051 | 418 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 419 | |
tnhnrl | 16:3363b9f14913 | 420 | break; |
tnhnrl | 17:7c16b5671d0e | 421 | |
tnhnrl | 17:7c16b5671d0e | 422 | case MULTI_DIVE : |
tnhnrl | 17:7c16b5671d0e | 423 | // start local state timer and init any other one-shot actions |
tnhnrl | 28:16c83a2fdefa | 424 | if (!_isTimeoutRunning) { |
tnhnrl | 17:7c16b5671d0e | 425 | pc().printf("\r\n\nstate: MULTI-DIVE\r\n"); |
tnhnrl | 17:7c16b5671d0e | 426 | timer.reset(); // timer goes back to zero |
tnhnrl | 17:7c16b5671d0e | 427 | timer.start(); // background timer starts running |
tnhnrl | 28:16c83a2fdefa | 428 | _isTimeoutRunning = true; |
tnhnrl | 17:7c16b5671d0e | 429 | |
tnhnrl | 17:7c16b5671d0e | 430 | // what needs to be started? |
tnhnrl | 17:7c16b5671d0e | 431 | bce().unpause(); |
tnhnrl | 17:7c16b5671d0e | 432 | batt().unpause(); |
tnhnrl | 17:7c16b5671d0e | 433 | |
tnhnrl | 21:38c8544db6f4 | 434 | //retrieve commands from structs (loaded from sequence.cfg file) |
tnhnrl | 32:f2f8ae34aadc | 435 | float sequence_depth_command = currentStateStruct.depth; |
tnhnrl | 32:f2f8ae34aadc | 436 | float sequence_pitch_command = currentStateStruct.pitch; |
tnhnrl | 17:7c16b5671d0e | 437 | |
tnhnrl | 17:7c16b5671d0e | 438 | // what are the commands? |
tnhnrl | 32:f2f8ae34aadc | 439 | depthLoop().setCommand(sequence_depth_command); |
tnhnrl | 32:f2f8ae34aadc | 440 | pitchLoop().setCommand(sequence_pitch_command); |
tnhnrl | 21:38c8544db6f4 | 441 | pc().printf("MULTI-DIVE: depth cmd: %3.1f ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand()); |
tnhnrl | 32:f2f8ae34aadc | 442 | |
tnhnrl | 32:f2f8ae34aadc | 443 | //create the log file (works only if the file is closed) |
tnhnrl | 34:9b66c5188051 | 444 | //createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 445 | |
tnhnrl | 32:f2f8ae34aadc | 446 | //show that this is the start of a new MULTI_DIVE sequence |
tnhnrl | 32:f2f8ae34aadc | 447 | recordState(_state); |
tnhnrl | 32:f2f8ae34aadc | 448 | |
tnhnrl | 32:f2f8ae34aadc | 449 | //no max depth recording right now |
tnhnrl | 32:f2f8ae34aadc | 450 | |
tnhnrl | 32:f2f8ae34aadc | 451 | //this was missing |
tnhnrl | 32:f2f8ae34aadc | 452 | _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 453 | recordData(_state); |
tnhnrl | 17:7c16b5671d0e | 454 | } |
tnhnrl | 20:8987a9ae2bc7 | 455 | |
tnhnrl | 17:7c16b5671d0e | 456 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 457 | if (timer > _timeout) { |
tnhnrl | 21:38c8544db6f4 | 458 | pc().printf("\n\n\rMULTI-DIVE: timed out [time: %0.1f]\n\n\r", timer.read()); |
tnhnrl | 21:38c8544db6f4 | 459 | _state = MULTI_RISE; //new behavior 11/17/2017 |
tnhnrl | 17:7c16b5671d0e | 460 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 461 | _isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 462 | } |
tnhnrl | 17:7c16b5671d0e | 463 | else if (depthLoop().getPosition() > depthLoop().getCommand()) { |
tnhnrl | 17:7c16b5671d0e | 464 | pc().printf("MULTI-DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); |
tnhnrl | 21:38c8544db6f4 | 465 | _state = MULTI_RISE; |
tnhnrl | 17:7c16b5671d0e | 466 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 467 | _isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 468 | } |
tnhnrl | 20:8987a9ae2bc7 | 469 | |
tnhnrl | 17:7c16b5671d0e | 470 | // what is active? |
tnhnrl | 17:7c16b5671d0e | 471 | 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()); |
tnhnrl | 17:7c16b5671d0e | 472 | bce().setPosition_mm(depthLoop().getOutput()); |
tnhnrl | 17:7c16b5671d0e | 473 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 32:f2f8ae34aadc | 474 | |
tnhnrl | 32:f2f8ae34aadc | 475 | //record data every 5 seconds |
tnhnrl | 34:9b66c5188051 | 476 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 477 | |
tnhnrl | 17:7c16b5671d0e | 478 | break; |
tnhnrl | 17:7c16b5671d0e | 479 | |
tnhnrl | 17:7c16b5671d0e | 480 | case MULTI_RISE : |
tnhnrl | 17:7c16b5671d0e | 481 | // start local state timer and init any other one-shot actions |
tnhnrl | 28:16c83a2fdefa | 482 | if (!_isTimeoutRunning) { |
tnhnrl | 17:7c16b5671d0e | 483 | pc().printf("\r\n\nstate: MULTI-RISE\r\n"); |
tnhnrl | 17:7c16b5671d0e | 484 | timer.reset(); // timer goes back to zero |
tnhnrl | 17:7c16b5671d0e | 485 | timer.start(); // background timer starts running |
tnhnrl | 28:16c83a2fdefa | 486 | _isTimeoutRunning = true; |
tnhnrl | 17:7c16b5671d0e | 487 | |
tnhnrl | 17:7c16b5671d0e | 488 | // what needs to be started? |
tnhnrl | 17:7c16b5671d0e | 489 | bce().unpause(); |
tnhnrl | 17:7c16b5671d0e | 490 | batt().unpause(); |
tnhnrl | 17:7c16b5671d0e | 491 | |
tnhnrl | 17:7c16b5671d0e | 492 | //NEW: retrieve depth and pitch commands from config file struct |
tnhnrl | 17:7c16b5671d0e | 493 | // concept is to load this each time the multi-dive restarts |
tnhnrl | 17:7c16b5671d0e | 494 | stateMachine().getDiveSequence(); |
tnhnrl | 17:7c16b5671d0e | 495 | |
tnhnrl | 17:7c16b5671d0e | 496 | //retrieve just pitch command from struct |
tnhnrl | 32:f2f8ae34aadc | 497 | float sequence_pitch_command = currentStateStruct.pitch; |
tnhnrl | 17:7c16b5671d0e | 498 | |
tnhnrl | 17:7c16b5671d0e | 499 | // what are the commands? (send back to 0.5 feet, not surface) // 11/21/2017 |
tnhnrl | 17:7c16b5671d0e | 500 | depthLoop().setCommand(0.5); |
tnhnrl | 32:f2f8ae34aadc | 501 | pitchLoop().setCommand(-sequence_pitch_command); |
tnhnrl | 21:38c8544db6f4 | 502 | pc().printf("MULTI-RISE: depth cmd: 0.0 ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand()); |
tnhnrl | 32:f2f8ae34aadc | 503 | |
tnhnrl | 32:f2f8ae34aadc | 504 | //create the log file (works only if the file is closed) |
tnhnrl | 34:9b66c5188051 | 505 | //createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 506 | |
tnhnrl | 32:f2f8ae34aadc | 507 | //show that this is the start of a new MULTI_DIVE sequence |
tnhnrl | 32:f2f8ae34aadc | 508 | recordState(_state); |
tnhnrl | 32:f2f8ae34aadc | 509 | |
tnhnrl | 32:f2f8ae34aadc | 510 | //this was missing |
tnhnrl | 32:f2f8ae34aadc | 511 | _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 512 | recordData(_state); |
tnhnrl | 17:7c16b5671d0e | 513 | } |
tnhnrl | 20:8987a9ae2bc7 | 514 | |
tnhnrl | 17:7c16b5671d0e | 515 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 516 | if (timer > _timeout) { |
tnhnrl | 21:38c8544db6f4 | 517 | pc().printf("MULTI-RISE: timed out [time: %0.1f]\n\n\r", timer.read()); |
tnhnrl | 21:38c8544db6f4 | 518 | _state = EMERGENCY_CLIMB; |
tnhnrl | 17:7c16b5671d0e | 519 | timer.reset(); |
tnhnrl | 28:16c83a2fdefa | 520 | _isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 521 | |
tnhnrl | 17:7c16b5671d0e | 522 | //reset multi-dive sequence to start |
tnhnrl | 24:c7d9b5bf3829 | 523 | _multi_dive_counter = 0; |
tnhnrl | 17:7c16b5671d0e | 524 | } |
tnhnrl | 20:8987a9ae2bc7 | 525 | else if (depthLoop().getPosition() < 0.5) { // depth is less than 0.5 (zero is surface level) |
tnhnrl | 17:7c16b5671d0e | 526 | pc().printf("MULTI-RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); |
tnhnrl | 17:7c16b5671d0e | 527 | |
tnhnrl | 17:7c16b5671d0e | 528 | //going to next state |
tnhnrl | 28:16c83a2fdefa | 529 | _isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 530 | |
tnhnrl | 17:7c16b5671d0e | 531 | //successful dive-rise sequence CONTINUES the multi-dive sequence |
tnhnrl | 24:c7d9b5bf3829 | 532 | _multi_dive_counter++; |
tnhnrl | 17:7c16b5671d0e | 533 | |
tnhnrl | 17:7c16b5671d0e | 534 | //UPDATE THE SEQUENCE DATA HERE |
tnhnrl | 17:7c16b5671d0e | 535 | stateMachine().getDiveSequence(); |
tnhnrl | 17:7c16b5671d0e | 536 | |
tnhnrl | 17:7c16b5671d0e | 537 | //check if this is the end of the dive sequence |
tnhnrl | 30:2964617e7676 | 538 | //CHECK BEFORE ANYTHING ELSE that you have reached the "exit" state (FLOAT_BROADCAST) |
tnhnrl | 30:2964617e7676 | 539 | if (currentStateStruct.state == FLOAT_BROADCAST) { |
tnhnrl | 28:16c83a2fdefa | 540 | _state = FLOAT_BROADCAST; |
tnhnrl | 17:7c16b5671d0e | 541 | } |
tnhnrl | 17:7c16b5671d0e | 542 | |
tnhnrl | 17:7c16b5671d0e | 543 | else |
tnhnrl | 21:38c8544db6f4 | 544 | _state = MULTI_DIVE; |
tnhnrl | 17:7c16b5671d0e | 545 | |
tnhnrl | 24:c7d9b5bf3829 | 546 | //have to stop this with the _multi_dive_counter variable! |
tnhnrl | 17:7c16b5671d0e | 547 | } |
tnhnrl | 20:8987a9ae2bc7 | 548 | |
tnhnrl | 20:8987a9ae2bc7 | 549 | // what is active? |
tnhnrl | 17:7c16b5671d0e | 550 | 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()); |
tnhnrl | 17:7c16b5671d0e | 551 | bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors |
tnhnrl | 17:7c16b5671d0e | 552 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 32:f2f8ae34aadc | 553 | |
tnhnrl | 32:f2f8ae34aadc | 554 | //record data every 5 seconds |
tnhnrl | 34:9b66c5188051 | 555 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 556 | |
tnhnrl | 17:7c16b5671d0e | 557 | break; |
tnhnrl | 32:f2f8ae34aadc | 558 | |
tnhnrl | 34:9b66c5188051 | 559 | case TRANSMIT_DATA : |
tnhnrl | 32:f2f8ae34aadc | 560 | if (!_isTimeoutRunning) { |
tnhnrl | 34:9b66c5188051 | 561 | pc().printf("\r\n\nstate: TRANSMIT_DATA\r\n"); |
tnhnrl | 32:f2f8ae34aadc | 562 | timer.reset(); // timer goes back to zero |
tnhnrl | 32:f2f8ae34aadc | 563 | timer.start(); // background timer starts running |
tnhnrl | 32:f2f8ae34aadc | 564 | _isTimeoutRunning = true; |
tnhnrl | 32:f2f8ae34aadc | 565 | |
tnhnrl | 32:f2f8ae34aadc | 566 | // what needs to be started? |
tnhnrl | 32:f2f8ae34aadc | 567 | |
tnhnrl | 32:f2f8ae34aadc | 568 | // what are the commands? |
tnhnrl | 34:9b66c5188051 | 569 | recordData(_state); |
tnhnrl | 32:f2f8ae34aadc | 570 | } |
tnhnrl | 32:f2f8ae34aadc | 571 | |
tnhnrl | 32:f2f8ae34aadc | 572 | // how exit? |
tnhnrl | 34:9b66c5188051 | 573 | if (timer.read() > _timeout) { |
tnhnrl | 34:9b66c5188051 | 574 | pc().printf("TRANSMIT_DATA: timed out\n\n\r"); |
tnhnrl | 34:9b66c5188051 | 575 | _state = SIT_IDLE; |
tnhnrl | 34:9b66c5188051 | 576 | timer.reset(); |
tnhnrl | 32:f2f8ae34aadc | 577 | _isTimeoutRunning = false; |
tnhnrl | 32:f2f8ae34aadc | 578 | } |
tnhnrl | 32:f2f8ae34aadc | 579 | |
tnhnrl | 34:9b66c5188051 | 580 | //action |
tnhnrl | 34:9b66c5188051 | 581 | transmitData(); |
tnhnrl | 32:f2f8ae34aadc | 582 | |
tnhnrl | 32:f2f8ae34aadc | 583 | break; |
tnhnrl | 16:3363b9f14913 | 584 | |
tnhnrl | 16:3363b9f14913 | 585 | default : |
tnhnrl | 17:7c16b5671d0e | 586 | pc().printf("DEBUG: SIT_IDLE\n\r"); |
tnhnrl | 21:38c8544db6f4 | 587 | _state = SIT_IDLE; |
tnhnrl | 28:16c83a2fdefa | 588 | } |
tnhnrl | 28:16c83a2fdefa | 589 | |
tnhnrl | 28:16c83a2fdefa | 590 | //save the state to print to user |
tnhnrl | 28:16c83a2fdefa | 591 | if (_previous_state != _state) { |
tnhnrl | 28:16c83a2fdefa | 592 | _state_array[_state_array_counter] = _state; //save to state array |
tnhnrl | 28:16c83a2fdefa | 593 | _state_array_counter++; |
tnhnrl | 28:16c83a2fdefa | 594 | |
tnhnrl | 28:16c83a2fdefa | 595 | _previous_state = _state; |
tnhnrl | 28:16c83a2fdefa | 596 | } |
tnhnrl | 34:9b66c5188051 | 597 | |
tnhnrl | 34:9b66c5188051 | 598 | return _state; |
tnhnrl | 34:9b66c5188051 | 599 | // //if the state is SIT_IDLE, return 0 / false (for recording) |
tnhnrl | 34:9b66c5188051 | 600 | // if (_state == SIT_IDLE) |
tnhnrl | 34:9b66c5188051 | 601 | // return 0; |
tnhnrl | 34:9b66c5188051 | 602 | // else |
tnhnrl | 34:9b66c5188051 | 603 | // return 1; //return true to indicate that you're recording |
tnhnrl | 16:3363b9f14913 | 604 | } |
tnhnrl | 20:8987a9ae2bc7 | 605 | |
tnhnrl | 16:3363b9f14913 | 606 | // output the keyboard menu for user's reference |
tnhnrl | 16:3363b9f14913 | 607 | void StateMachine::showMenu() { |
tnhnrl | 16:3363b9f14913 | 608 | pc().printf("\r\r\n\nKEYBOARD MENU:\r\r\n"); |
tnhnrl | 32:f2f8ae34aadc | 609 | pc().printf(" G to initiate auto-neutral sequence\r\n"); |
tnhnrl | 32:f2f8ae34aadc | 610 | pc().printf(" F to jump to auto-neutral FIND PITCH state\r\n"); |
tnhnrl | 16:3363b9f14913 | 611 | pc().printf(" N to find neutral\r\n"); |
tnhnrl | 17:7c16b5671d0e | 612 | pc().printf(" M to initiate multi-dive cycle\r\n"); |
tnhnrl | 16:3363b9f14913 | 613 | pc().printf(" D to initiate dive cycle\r\n"); |
tnhnrl | 16:3363b9f14913 | 614 | pc().printf(" R to initiate rise\r\n"); |
tnhnrl | 16:3363b9f14913 | 615 | pc().printf(" L to float level\r\n"); |
tnhnrl | 16:3363b9f14913 | 616 | pc().printf(" B to float at broadcast pitch\r\n"); |
tnhnrl | 16:3363b9f14913 | 617 | pc().printf(" E to initiate emergency climb\r\n"); |
tnhnrl | 28:16c83a2fdefa | 618 | //pc().printf(" H to run homing sequence on both BCE and Batt\r\n"); |
tnhnrl | 16:3363b9f14913 | 619 | pc().printf(" T to tare the depth sensor\r\n"); |
tnhnrl | 28:16c83a2fdefa | 620 | pc().printf(" Z to show FSM and sub-FSM states.\r\n"); |
tnhnrl | 32:f2f8ae34aadc | 621 | pc().printf(" P to print the current log file.\r\n"); |
tnhnrl | 32:f2f8ae34aadc | 622 | pc().printf(" X to print the list of log files.\r\n"); |
tnhnrl | 34:9b66c5188051 | 623 | pc().printf(" V to transmit data (work in progress).\r\n"); |
tnhnrl | 32:f2f8ae34aadc | 624 | pc().printf("[/] to change bce neutral position: %0.1f\r\n", _neutral_bce_pos_mm); |
tnhnrl | 32:f2f8ae34aadc | 625 | pc().printf("</> to change batt neutral position: %0.1f\r\n", _neutral_batt_pos_mm); |
tnhnrl | 32:f2f8ae34aadc | 626 | pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",_pitch_command); |
tnhnrl | 32:f2f8ae34aadc | 627 | pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",_depth_command); |
tnhnrl | 17:7c16b5671d0e | 628 | pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout); |
tnhnrl | 16:3363b9f14913 | 629 | pc().printf(" 1 BCE PID sub-menu\r\n"); |
tnhnrl | 16:3363b9f14913 | 630 | pc().printf(" 2 BATT PID sub-menu\r\n"); |
tnhnrl | 16:3363b9f14913 | 631 | pc().printf(" 3 Depth PID sub-menu\r\n"); |
tnhnrl | 16:3363b9f14913 | 632 | pc().printf(" 4 Pitch PID sub-menu\r\n"); |
tnhnrl | 28:16c83a2fdefa | 633 | pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n"); |
tnhnrl | 16:3363b9f14913 | 634 | pc().printf(" ? to reset mbed\r\n"); |
tnhnrl | 16:3363b9f14913 | 635 | } |
tnhnrl | 20:8987a9ae2bc7 | 636 | |
tnhnrl | 17:7c16b5671d0e | 637 | //Find Neutral sub finite state machine |
tnhnrl | 17:7c16b5671d0e | 638 | // Note: the sub-fsm only moves the pistons once at the start of each timer loop |
tnhnrl | 17:7c16b5671d0e | 639 | // (timer completes, move piston, timer completes, move piston, etc) |
tnhnrl | 28:16c83a2fdefa | 640 | int StateMachine::runNeutralStateMachine() { |
tnhnrl | 24:c7d9b5bf3829 | 641 | switch (_substate) { |
tnhnrl | 20:8987a9ae2bc7 | 642 | case NEUTRAL_SINKING : |
tnhnrl | 17:7c16b5671d0e | 643 | //start the 10 second timer |
tnhnrl | 28:16c83a2fdefa | 644 | if (!_isSubStateTimerRunning) { |
tnhnrl | 24:c7d9b5bf3829 | 645 | _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds |
tnhnrl | 17:7c16b5671d0e | 646 | |
tnhnrl | 28:16c83a2fdefa | 647 | pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f)\n\r", _neutral_timer, timer.read(), pitchLoop().getPosition()); |
tnhnrl | 20:8987a9ae2bc7 | 648 | |
tnhnrl | 32:f2f8ae34aadc | 649 | // what are the commands? (BCE linear actuator active, no pitch movement) |
tnhnrl | 32:f2f8ae34aadc | 650 | //move piston at start of sequence (default: retract 2.5 mm) |
tnhnrl | 32:f2f8ae34aadc | 651 | bce().setPosition_mm(bce().getSetPosition_mm() - _neutral_sink_command_mm); //no depth command |
tnhnrl | 23:434f04ef1fad | 652 | |
tnhnrl | 32:f2f8ae34aadc | 653 | pc().printf("NEUTRAL_SINKING: Retracting piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f] (pitch: %0.1f)\n\r", _neutral_sink_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand(), pitchLoop().getPosition()); |
tnhnrl | 17:7c16b5671d0e | 654 | |
tnhnrl | 28:16c83a2fdefa | 655 | _isSubStateTimerRunning = true; //disable this block after one iteration |
tnhnrl | 17:7c16b5671d0e | 656 | } |
tnhnrl | 20:8987a9ae2bc7 | 657 | |
tnhnrl | 20:8987a9ae2bc7 | 658 | // how exit? |
tnhnrl | 20:8987a9ae2bc7 | 659 | //once reached the travel limit, no need to keep trying, so exit |
tnhnrl | 25:249e4d56b27c | 660 | if (bce().getPosition_mm() <= 0) { |
tnhnrl | 25:249e4d56b27c | 661 | pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm()); |
tnhnrl | 25:249e4d56b27c | 662 | _substate = NEUTRAL_EXIT; |
tnhnrl | 28:16c83a2fdefa | 663 | _isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 25:249e4d56b27c | 664 | } |
tnhnrl | 20:8987a9ae2bc7 | 665 | //once deeper than the commanded setpoint... |
tnhnrl | 32:f2f8ae34aadc | 666 | else if (depthLoop().getPosition() > _depth_command) { |
tnhnrl | 24:c7d9b5bf3829 | 667 | _substate = NEUTRAL_SLOWLY_RISE; // next state |
tnhnrl | 28:16c83a2fdefa | 668 | _isSubStateTimerRunning = false; //reset the sub state timer |
tnhnrl | 20:8987a9ae2bc7 | 669 | } |
tnhnrl | 20:8987a9ae2bc7 | 670 | |
tnhnrl | 20:8987a9ae2bc7 | 671 | // what is active? |
tnhnrl | 20:8987a9ae2bc7 | 672 | //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint |
tnhnrl | 24:c7d9b5bf3829 | 673 | if (timer.read() >= _neutral_timer) { |
tnhnrl | 32:f2f8ae34aadc | 674 | pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", timer.read()); |
tnhnrl | 17:7c16b5671d0e | 675 | |
tnhnrl | 28:16c83a2fdefa | 676 | _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 17:7c16b5671d0e | 677 | } |
tnhnrl | 32:f2f8ae34aadc | 678 | // what is active? (only the buoyancy engine moved every 5 seconds) |
tnhnrl | 32:f2f8ae34aadc | 679 | pc().printf("depthLoop getOutput (position): %0.1f (current depth: %0.1f ft)\r", depthLoop().getOutput(), depthLoop().getPosition()); //debug |
tnhnrl | 32:f2f8ae34aadc | 680 | bce().setPosition_mm(depthLoop().getOutput()); // (DID NOT WORK ON BENCH) |
tnhnrl | 17:7c16b5671d0e | 681 | break; |
tnhnrl | 17:7c16b5671d0e | 682 | |
tnhnrl | 17:7c16b5671d0e | 683 | case NEUTRAL_SLOWLY_RISE: |
tnhnrl | 28:16c83a2fdefa | 684 | if (!_isSubStateTimerRunning) { |
tnhnrl | 24:c7d9b5bf3829 | 685 | _neutral_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds |
tnhnrl | 17:7c16b5671d0e | 686 | |
tnhnrl | 24:c7d9b5bf3829 | 687 | pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Next extension at %0.1f sec) [current time: %0.1f]\r\n",_neutral_timer,timer.read()); |
tnhnrl | 17:7c16b5671d0e | 688 | |
tnhnrl | 20:8987a9ae2bc7 | 689 | // what are the commands? |
tnhnrl | 32:f2f8ae34aadc | 690 | //move piston at start of sequence (default: extend 2.0 mm) |
tnhnrl | 32:f2f8ae34aadc | 691 | bce().setPosition_mm(bce().getSetPosition_mm() + _neutral_rise_command_mm); //no depth command |
tnhnrl | 23:434f04ef1fad | 692 | |
tnhnrl | 23:434f04ef1fad | 693 | // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state |
tnhnrl | 23:434f04ef1fad | 694 | pitchLoop().setCommand(0.0); |
tnhnrl | 24:c7d9b5bf3829 | 695 | |
tnhnrl | 32:f2f8ae34aadc | 696 | pc().printf("NEUTRAL_SLOWLY_RISE: Extending BCE piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f]\n\r", _neutral_rise_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand()); |
tnhnrl | 24:c7d9b5bf3829 | 697 | |
tnhnrl | 28:16c83a2fdefa | 698 | _isSubStateTimerRunning = true; //disable this block after one iteration |
tnhnrl | 17:7c16b5671d0e | 699 | } |
tnhnrl | 17:7c16b5671d0e | 700 | |
tnhnrl | 20:8987a9ae2bc7 | 701 | // how exit? |
tnhnrl | 24:c7d9b5bf3829 | 702 | //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit |
tnhnrl | 24:c7d9b5bf3829 | 703 | if (bce().getSetPosition_mm() >= bce().getTravelLimit()) { |
tnhnrl | 24:c7d9b5bf3829 | 704 | _substate = NEUTRAL_EXIT; |
tnhnrl | 28:16c83a2fdefa | 705 | _isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 17:7c16b5671d0e | 706 | } |
tnhnrl | 17:7c16b5671d0e | 707 | //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration |
tnhnrl | 20:8987a9ae2bc7 | 708 | else if (depthLoop().getVelocity() < 0) { //less than zero ft/s |
tnhnrl | 24:c7d9b5bf3829 | 709 | pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 ft/s [time: %0.1f]\r\n", timer.read()); |
tnhnrl | 24:c7d9b5bf3829 | 710 | _substate = NEUTRAL_CHECK_PITCH; |
tnhnrl | 28:16c83a2fdefa | 711 | _isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 17:7c16b5671d0e | 712 | } |
tnhnrl | 17:7c16b5671d0e | 713 | |
tnhnrl | 20:8987a9ae2bc7 | 714 | // what is active? |
tnhnrl | 20:8987a9ae2bc7 | 715 | //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint |
tnhnrl | 24:c7d9b5bf3829 | 716 | if (timer.read() >= _neutral_timer) { |
tnhnrl | 32:f2f8ae34aadc | 717 | pc().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! [timer: %0.1f]\r\n", timer.read()); |
tnhnrl | 20:8987a9ae2bc7 | 718 | |
tnhnrl | 28:16c83a2fdefa | 719 | _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 17:7c16b5671d0e | 720 | } |
tnhnrl | 23:434f04ef1fad | 721 | |
tnhnrl | 32:f2f8ae34aadc | 722 | // what is active? (only the buoyancy engine moved every 5 seconds) |
tnhnrl | 32:f2f8ae34aadc | 723 | pc().printf("depthLoop getOutput: %0.1f\r", depthLoop().getOutput()); //debug |
tnhnrl | 32:f2f8ae34aadc | 724 | bce().setPosition_mm(depthLoop().getOutput()); // (DID NOT WORK ON BENCH) |
tnhnrl | 17:7c16b5671d0e | 725 | break; |
tnhnrl | 17:7c16b5671d0e | 726 | |
danstrider | 22:a10ee088403b | 727 | case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired |
danstrider | 22:a10ee088403b | 728 | // start local state timer and init any other one-shot actions |
tnhnrl | 23:434f04ef1fad | 729 | |
tnhnrl | 28:16c83a2fdefa | 730 | if (!_isSubStateTimerRunning) { |
tnhnrl | 24:c7d9b5bf3829 | 731 | _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds |
tnhnrl | 24:c7d9b5bf3829 | 732 | pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read()); |
danstrider | 22:a10ee088403b | 733 | |
tnhnrl | 32:f2f8ae34aadc | 734 | // what are the commands? (default: retract or extend 0.5 mm) |
tnhnrl | 24:c7d9b5bf3829 | 735 | if (pitchLoop().getPosition() > 2) { // nose is high |
tnhnrl | 32:f2f8ae34aadc | 736 | batt().setPosition_mm(batt().getSetPosition_mm() + _neutral_pitch_command_mm); // move battery forward (using setpoint from linear actuator) |
tnhnrl | 32:f2f8ae34aadc | 737 | pc().printf("\n\rNeutral Check Pitch: moving battery FWD in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm); |
danstrider | 22:a10ee088403b | 738 | } |
tnhnrl | 24:c7d9b5bf3829 | 739 | else if (pitchLoop().getPosition() < -2) { // nose is low |
tnhnrl | 32:f2f8ae34aadc | 740 | batt().setPosition_mm(batt().getSetPosition_mm() - _neutral_pitch_command_mm); // move battery aft (using setpoint from linear actuator) |
tnhnrl | 32:f2f8ae34aadc | 741 | pc().printf("\n\rNeutral Check Pitch: moving battery AFT in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm); |
danstrider | 22:a10ee088403b | 742 | } |
tnhnrl | 24:c7d9b5bf3829 | 743 | |
tnhnrl | 28:16c83a2fdefa | 744 | _isSubStateTimerRunning = true; //disable this block after one iteration |
danstrider | 22:a10ee088403b | 745 | } |
tnhnrl | 20:8987a9ae2bc7 | 746 | |
tnhnrl | 28:16c83a2fdefa | 747 | // how exit? |
tnhnrl | 20:8987a9ae2bc7 | 748 | //pitch angle and pitch rate within small tolerance |
tnhnrl | 20:8987a9ae2bc7 | 749 | //benchtop tests confirm angle needs to be around 2 degrees |
tnhnrl | 23:434f04ef1fad | 750 | if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) { |
tnhnrl | 24:c7d9b5bf3829 | 751 | pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug |
danstrider | 22:a10ee088403b | 752 | // found level, but don't need to save anything this time |
tnhnrl | 23:434f04ef1fad | 753 | |
tnhnrl | 28:16c83a2fdefa | 754 | if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug |
tnhnrl | 28:16c83a2fdefa | 755 | _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded |
tnhnrl | 28:16c83a2fdefa | 756 | } |
tnhnrl | 28:16c83a2fdefa | 757 | |
tnhnrl | 23:434f04ef1fad | 758 | // found level and at depth too, so save it all now |
tnhnrl | 32:f2f8ae34aadc | 759 | if (_substate == NEUTRAL_CHECK_PITCH) { |
danstrider | 22:a10ee088403b | 760 | //save positions locally |
danstrider | 22:a10ee088403b | 761 | _neutral_batt_pos_mm = batt().getPosition_mm(); |
danstrider | 22:a10ee088403b | 762 | _neutral_bce_pos_mm = bce().getPosition_mm(); |
danstrider | 22:a10ee088403b | 763 | |
danstrider | 22:a10ee088403b | 764 | //set the neutral positions in each outer loop |
danstrider | 22:a10ee088403b | 765 | depthLoop().setOutputOffset(_neutral_bce_pos_mm); |
danstrider | 22:a10ee088403b | 766 | pitchLoop().setOutputOffset(_neutral_batt_pos_mm); |
danstrider | 22:a10ee088403b | 767 | |
danstrider | 22:a10ee088403b | 768 | // save into the depth.txt and pitch.txt files |
danstrider | 22:a10ee088403b | 769 | configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset |
danstrider | 22:a10ee088403b | 770 | configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset |
danstrider | 22:a10ee088403b | 771 | |
tnhnrl | 32:f2f8ae34aadc | 772 | pc().printf("\n\n\r>>> Saving Positions: BCE: %0.1f mm, BATT: %0.1f <<<\n\n\r",_neutral_bce_pos_mm,_neutral_batt_pos_mm); |
danstrider | 22:a10ee088403b | 773 | |
tnhnrl | 24:c7d9b5bf3829 | 774 | _substate = NEUTRAL_EXIT; |
tnhnrl | 28:16c83a2fdefa | 775 | _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 24:c7d9b5bf3829 | 776 | } |
tnhnrl | 24:c7d9b5bf3829 | 777 | |
tnhnrl | 24:c7d9b5bf3829 | 778 | else { |
tnhnrl | 24:c7d9b5bf3829 | 779 | pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r"); |
tnhnrl | 24:c7d9b5bf3829 | 780 | _substate = NEUTRAL_EXIT; |
danstrider | 22:a10ee088403b | 781 | } |
tnhnrl | 17:7c16b5671d0e | 782 | } |
danstrider | 22:a10ee088403b | 783 | |
danstrider | 22:a10ee088403b | 784 | // what is active? |
danstrider | 22:a10ee088403b | 785 | //once timer complete, reset the timeout so the state one-shot entry will move the setpoint |
tnhnrl | 24:c7d9b5bf3829 | 786 | if (timer.read() >= _neutral_timer) { |
danstrider | 22:a10ee088403b | 787 | pc().printf("\r\n\nlevel timer COMPLETE!"); |
danstrider | 22:a10ee088403b | 788 | pc().printf("\r\n\n (BATT POS: %0.1f) moving 1 mm [timer: %0.1f]\r\n", batt().getPosition_mm(), timer.read()); |
tnhnrl | 28:16c83a2fdefa | 789 | _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
danstrider | 22:a10ee088403b | 790 | } |
tnhnrl | 17:7c16b5671d0e | 791 | break; |
danstrider | 22:a10ee088403b | 792 | |
danstrider | 22:a10ee088403b | 793 | //this state could be removed, it is only used as a transition but is needed to stop entering this function |
danstrider | 22:a10ee088403b | 794 | case NEUTRAL_EXIT : |
tnhnrl | 23:434f04ef1fad | 795 | pc().printf("substate: NEUTRAL_EXIT\n\r"); |
tnhnrl | 20:8987a9ae2bc7 | 796 | break; |
tnhnrl | 21:38c8544db6f4 | 797 | |
danstrider | 22:a10ee088403b | 798 | default : |
tnhnrl | 24:c7d9b5bf3829 | 799 | pc().printf("how did we get to substate: default?\n\r"); //debug |
tnhnrl | 23:434f04ef1fad | 800 | //a default within the sub-state machine |
tnhnrl | 24:c7d9b5bf3829 | 801 | _substate = NEUTRAL_EXIT; |
danstrider | 22:a10ee088403b | 802 | break; |
tnhnrl | 17:7c16b5671d0e | 803 | } |
tnhnrl | 20:8987a9ae2bc7 | 804 | |
tnhnrl | 30:2964617e7676 | 805 | // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence) |
tnhnrl | 24:c7d9b5bf3829 | 806 | if (_substate == NEUTRAL_EXIT) { |
tnhnrl | 24:c7d9b5bf3829 | 807 | pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r"); |
tnhnrl | 24:c7d9b5bf3829 | 808 | |
tnhnrl | 30:2964617e7676 | 809 | //reset internal sub-state back to first entry conditions (first state is immediately sinking) |
tnhnrl | 30:2964617e7676 | 810 | _substate = NEUTRAL_SINKING; |
tnhnrl | 28:16c83a2fdefa | 811 | _isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 21:38c8544db6f4 | 812 | |
tnhnrl | 24:c7d9b5bf3829 | 813 | //record sub-states to view after sequence |
tnhnrl | 30:2964617e7676 | 814 | _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array |
tnhnrl | 28:16c83a2fdefa | 815 | _substate_array_counter++; |
tnhnrl | 23:434f04ef1fad | 816 | |
tnhnrl | 24:c7d9b5bf3829 | 817 | //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit) |
tnhnrl | 24:c7d9b5bf3829 | 818 | _previous_substate = -1; |
tnhnrl | 24:c7d9b5bf3829 | 819 | |
tnhnrl | 24:c7d9b5bf3829 | 820 | //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed |
tnhnrl | 24:c7d9b5bf3829 | 821 | return NEUTRAL_EXIT; // message to calling function we just exited |
tnhnrl | 21:38c8544db6f4 | 822 | } |
tnhnrl | 23:434f04ef1fad | 823 | else { |
tnhnrl | 24:c7d9b5bf3829 | 824 | //record sub-states to view after sequence (when changed) |
tnhnrl | 24:c7d9b5bf3829 | 825 | if (_previous_substate != _substate) { |
tnhnrl | 28:16c83a2fdefa | 826 | _substate_array[_substate_array_counter] = _substate; //save current state to state array |
tnhnrl | 28:16c83a2fdefa | 827 | _substate_array_counter++; |
tnhnrl | 24:c7d9b5bf3829 | 828 | |
tnhnrl | 24:c7d9b5bf3829 | 829 | //record the current substate for comparison |
tnhnrl | 24:c7d9b5bf3829 | 830 | _previous_substate = _substate; |
tnhnrl | 24:c7d9b5bf3829 | 831 | } |
tnhnrl | 24:c7d9b5bf3829 | 832 | |
tnhnrl | 24:c7d9b5bf3829 | 833 | return _substate; // message to calling function of what sub-state it's in |
tnhnrl | 23:434f04ef1fad | 834 | } |
tnhnrl | 17:7c16b5671d0e | 835 | } |
tnhnrl | 32:f2f8ae34aadc | 836 | |
tnhnrl | 32:f2f8ae34aadc | 837 | ////Find Neutral sub finite state machine |
tnhnrl | 32:f2f8ae34aadc | 838 | //// Note: the sub-fsm only moves the pistons once at the start of each timer loop |
tnhnrl | 32:f2f8ae34aadc | 839 | //// (timer completes, move piston, timer completes, move piston, etc) |
tnhnrl | 32:f2f8ae34aadc | 840 | //int StateMachine::runActiveNeutralStateMachine() { |
tnhnrl | 32:f2f8ae34aadc | 841 | // switch (_substate) { |
tnhnrl | 32:f2f8ae34aadc | 842 | // case NEUTRAL_AUTO_DEPTH : |
tnhnrl | 32:f2f8ae34aadc | 843 | // //start the 10 second timer |
tnhnrl | 32:f2f8ae34aadc | 844 | // if (!_isSubStateTimerRunning) { |
tnhnrl | 32:f2f8ae34aadc | 845 | // _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds |
tnhnrl | 32:f2f8ae34aadc | 846 | // |
tnhnrl | 32:f2f8ae34aadc | 847 | // pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f)\n\r", _neutral_timer, timer.read(), pitchLoop().getPosition()); |
tnhnrl | 32:f2f8ae34aadc | 848 | // |
tnhnrl | 32:f2f8ae34aadc | 849 | // // what are the commands? (BCE linear actuator active, no pitch movement) |
tnhnrl | 32:f2f8ae34aadc | 850 | // //move piston at start of sequence (default: retract 2.5 mm) |
tnhnrl | 32:f2f8ae34aadc | 851 | // bce().setPosition_mm(bce().getSetPosition_mm() - _neutral_sink_command_mm); //no depth command |
tnhnrl | 32:f2f8ae34aadc | 852 | // |
tnhnrl | 32:f2f8ae34aadc | 853 | // pc().printf("NEUTRAL_SINKING: Retracting piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f] (pitch: %0.1f)\n\r", _neutral_sink_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand(), pitchLoop().getPosition()); |
tnhnrl | 32:f2f8ae34aadc | 854 | // |
tnhnrl | 32:f2f8ae34aadc | 855 | // _isSubStateTimerRunning = true; //disable this block after one iteration |
tnhnrl | 32:f2f8ae34aadc | 856 | // } |
tnhnrl | 32:f2f8ae34aadc | 857 | // |
tnhnrl | 32:f2f8ae34aadc | 858 | // // how exit? |
tnhnrl | 32:f2f8ae34aadc | 859 | // //once reached the travel limit, no need to keep trying, so exit |
tnhnrl | 32:f2f8ae34aadc | 860 | // if (bce().getPosition_mm() <= 0) { |
tnhnrl | 32:f2f8ae34aadc | 861 | // pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm()); |
tnhnrl | 32:f2f8ae34aadc | 862 | // _substate = NEUTRAL_EXIT; |
tnhnrl | 32:f2f8ae34aadc | 863 | // _isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 32:f2f8ae34aadc | 864 | // } |
tnhnrl | 32:f2f8ae34aadc | 865 | // //once deeper than the commanded setpoint... |
tnhnrl | 32:f2f8ae34aadc | 866 | // else if (depthLoop().getPosition() > _depth_command) { |
tnhnrl | 32:f2f8ae34aadc | 867 | // _substate = NEUTRAL_SLOWLY_RISE; // next state |
tnhnrl | 32:f2f8ae34aadc | 868 | // _isSubStateTimerRunning = false; //reset the sub state timer |
tnhnrl | 32:f2f8ae34aadc | 869 | // } |
tnhnrl | 32:f2f8ae34aadc | 870 | // |
tnhnrl | 32:f2f8ae34aadc | 871 | // // what is active? |
tnhnrl | 32:f2f8ae34aadc | 872 | // //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint |
tnhnrl | 32:f2f8ae34aadc | 873 | // if (timer.read() >= _neutral_timer) { |
tnhnrl | 32:f2f8ae34aadc | 874 | // pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", timer.read()); |
tnhnrl | 32:f2f8ae34aadc | 875 | // |
tnhnrl | 32:f2f8ae34aadc | 876 | // _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 32:f2f8ae34aadc | 877 | // } |
tnhnrl | 32:f2f8ae34aadc | 878 | // // what is active? (only the buoyancy engine moved every 5 seconds) |
tnhnrl | 32:f2f8ae34aadc | 879 | // break; |
tnhnrl | 32:f2f8ae34aadc | 880 | // |
tnhnrl | 32:f2f8ae34aadc | 881 | // case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired |
tnhnrl | 32:f2f8ae34aadc | 882 | // // start local state timer and init any other one-shot actions |
tnhnrl | 32:f2f8ae34aadc | 883 | // |
tnhnrl | 32:f2f8ae34aadc | 884 | // if (!_isSubStateTimerRunning) { |
tnhnrl | 32:f2f8ae34aadc | 885 | // _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds |
tnhnrl | 32:f2f8ae34aadc | 886 | // pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read()); |
tnhnrl | 32:f2f8ae34aadc | 887 | // |
tnhnrl | 32:f2f8ae34aadc | 888 | // // what are the commands? (default: retract or extend 0.5 mm) |
tnhnrl | 32:f2f8ae34aadc | 889 | // if (pitchLoop().getPosition() > 2) { // nose is high |
tnhnrl | 32:f2f8ae34aadc | 890 | // batt().setPosition_mm(batt().getSetPosition_mm() + _neutral_pitch_command_mm); // move battery forward (using setpoint from linear actuator) |
tnhnrl | 32:f2f8ae34aadc | 891 | // pc().printf("\n\rNeutral Check Pitch: moving battery FWD in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm); |
tnhnrl | 32:f2f8ae34aadc | 892 | // } |
tnhnrl | 32:f2f8ae34aadc | 893 | // else if (pitchLoop().getPosition() < -2) { // nose is low |
tnhnrl | 32:f2f8ae34aadc | 894 | // batt().setPosition_mm(batt().getSetPosition_mm() - _neutral_pitch_command_mm); // move battery aft (using setpoint from linear actuator) |
tnhnrl | 32:f2f8ae34aadc | 895 | // pc().printf("\n\rNeutral Check Pitch: moving battery AFT in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm); |
tnhnrl | 32:f2f8ae34aadc | 896 | // } |
tnhnrl | 32:f2f8ae34aadc | 897 | // |
tnhnrl | 32:f2f8ae34aadc | 898 | // _isSubStateTimerRunning = true; //disable this block after one iteration |
tnhnrl | 32:f2f8ae34aadc | 899 | // } |
tnhnrl | 32:f2f8ae34aadc | 900 | // |
tnhnrl | 32:f2f8ae34aadc | 901 | // // how exit? |
tnhnrl | 32:f2f8ae34aadc | 902 | // //pitch angle and pitch rate within small tolerance |
tnhnrl | 32:f2f8ae34aadc | 903 | // //benchtop tests confirm angle needs to be around 2 degrees |
tnhnrl | 32:f2f8ae34aadc | 904 | // if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) { |
tnhnrl | 32:f2f8ae34aadc | 905 | // pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug |
tnhnrl | 32:f2f8ae34aadc | 906 | // // found level, but don't need to save anything this time |
tnhnrl | 32:f2f8ae34aadc | 907 | // |
tnhnrl | 32:f2f8ae34aadc | 908 | // if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug |
tnhnrl | 32:f2f8ae34aadc | 909 | // _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded |
tnhnrl | 32:f2f8ae34aadc | 910 | // } |
tnhnrl | 32:f2f8ae34aadc | 911 | // |
tnhnrl | 32:f2f8ae34aadc | 912 | // // found level and at depth too, so save it all now |
tnhnrl | 32:f2f8ae34aadc | 913 | // if (_substate == NEUTRAL_CHECK_PITCH) { |
tnhnrl | 32:f2f8ae34aadc | 914 | // //save positions locally |
tnhnrl | 32:f2f8ae34aadc | 915 | // _neutral_batt_pos_mm = batt().getPosition_mm(); |
tnhnrl | 32:f2f8ae34aadc | 916 | // _neutral_bce_pos_mm = bce().getPosition_mm(); |
tnhnrl | 32:f2f8ae34aadc | 917 | // |
tnhnrl | 32:f2f8ae34aadc | 918 | // //set the neutral positions in each outer loop |
tnhnrl | 32:f2f8ae34aadc | 919 | // depthLoop().setOutputOffset(_neutral_bce_pos_mm); |
tnhnrl | 32:f2f8ae34aadc | 920 | // pitchLoop().setOutputOffset(_neutral_batt_pos_mm); |
tnhnrl | 32:f2f8ae34aadc | 921 | // |
tnhnrl | 32:f2f8ae34aadc | 922 | // // save into the depth.txt and pitch.txt files |
tnhnrl | 32:f2f8ae34aadc | 923 | // configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset |
tnhnrl | 32:f2f8ae34aadc | 924 | // configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset |
tnhnrl | 32:f2f8ae34aadc | 925 | // |
tnhnrl | 32:f2f8ae34aadc | 926 | // pc().printf("\n\rSaving Positions: BCE: %0.1f mm, BATT: %0.1f\n\n\r",_neutral_bce_pos_mm,_neutral_batt_pos_mm); |
tnhnrl | 32:f2f8ae34aadc | 927 | // |
tnhnrl | 32:f2f8ae34aadc | 928 | // _substate = NEUTRAL_EXIT; |
tnhnrl | 32:f2f8ae34aadc | 929 | // _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 32:f2f8ae34aadc | 930 | // } |
tnhnrl | 32:f2f8ae34aadc | 931 | // |
tnhnrl | 32:f2f8ae34aadc | 932 | // else { |
tnhnrl | 32:f2f8ae34aadc | 933 | // pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r"); |
tnhnrl | 32:f2f8ae34aadc | 934 | // _substate = NEUTRAL_EXIT; |
tnhnrl | 32:f2f8ae34aadc | 935 | // } |
tnhnrl | 32:f2f8ae34aadc | 936 | // } |
tnhnrl | 32:f2f8ae34aadc | 937 | // |
tnhnrl | 32:f2f8ae34aadc | 938 | // // what is active? |
tnhnrl | 32:f2f8ae34aadc | 939 | // //once timer complete, reset the timeout so the state one-shot entry will move the setpoint |
tnhnrl | 32:f2f8ae34aadc | 940 | // if (timer.read() >= _neutral_timer) { |
tnhnrl | 32:f2f8ae34aadc | 941 | // pc().printf("\r\n\nlevel timer COMPLETE!"); |
tnhnrl | 32:f2f8ae34aadc | 942 | // pc().printf("\r\n\n (BATT POS: %0.1f) moving 1 mm [timer: %0.1f]\r\n", batt().getPosition_mm(), timer.read()); |
tnhnrl | 32:f2f8ae34aadc | 943 | // _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 32:f2f8ae34aadc | 944 | // } |
tnhnrl | 32:f2f8ae34aadc | 945 | // break; |
tnhnrl | 32:f2f8ae34aadc | 946 | // |
tnhnrl | 32:f2f8ae34aadc | 947 | // //this state could be removed, it is only used as a transition but is needed to stop entering this function |
tnhnrl | 32:f2f8ae34aadc | 948 | // case NEUTRAL_EXIT : |
tnhnrl | 32:f2f8ae34aadc | 949 | // pc().printf("substate: NEUTRAL_EXIT\n\r"); |
tnhnrl | 32:f2f8ae34aadc | 950 | // break; |
tnhnrl | 32:f2f8ae34aadc | 951 | // |
tnhnrl | 32:f2f8ae34aadc | 952 | // default : |
tnhnrl | 32:f2f8ae34aadc | 953 | // pc().printf("how did we get to substate: default?\n\r"); //debug |
tnhnrl | 32:f2f8ae34aadc | 954 | // //a default within the sub-state machine |
tnhnrl | 32:f2f8ae34aadc | 955 | // _substate = NEUTRAL_EXIT; |
tnhnrl | 32:f2f8ae34aadc | 956 | // break; |
tnhnrl | 32:f2f8ae34aadc | 957 | // } |
tnhnrl | 32:f2f8ae34aadc | 958 | // |
tnhnrl | 32:f2f8ae34aadc | 959 | // // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence) |
tnhnrl | 32:f2f8ae34aadc | 960 | // if (_substate == NEUTRAL_EXIT) { |
tnhnrl | 32:f2f8ae34aadc | 961 | // pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r"); |
tnhnrl | 32:f2f8ae34aadc | 962 | // |
tnhnrl | 32:f2f8ae34aadc | 963 | // //reset internal sub-state back to first entry conditions (first state is immediately sinking) |
tnhnrl | 32:f2f8ae34aadc | 964 | // _substate = NEUTRAL_SINKING; |
tnhnrl | 32:f2f8ae34aadc | 965 | // _isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 32:f2f8ae34aadc | 966 | // |
tnhnrl | 32:f2f8ae34aadc | 967 | // //record sub-states to view after sequence |
tnhnrl | 32:f2f8ae34aadc | 968 | // _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array |
tnhnrl | 32:f2f8ae34aadc | 969 | // _substate_array_counter++; |
tnhnrl | 32:f2f8ae34aadc | 970 | // |
tnhnrl | 32:f2f8ae34aadc | 971 | // //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit) |
tnhnrl | 32:f2f8ae34aadc | 972 | // _previous_substate = -1; |
tnhnrl | 32:f2f8ae34aadc | 973 | // |
tnhnrl | 32:f2f8ae34aadc | 974 | // //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed |
tnhnrl | 32:f2f8ae34aadc | 975 | // return NEUTRAL_EXIT; // message to calling function we just exited |
tnhnrl | 32:f2f8ae34aadc | 976 | // } |
tnhnrl | 32:f2f8ae34aadc | 977 | // else { |
tnhnrl | 32:f2f8ae34aadc | 978 | // //record sub-states to view after sequence (when changed) |
tnhnrl | 32:f2f8ae34aadc | 979 | // if (_previous_substate != _substate) { |
tnhnrl | 32:f2f8ae34aadc | 980 | // _substate_array[_substate_array_counter] = _substate; //save current state to state array |
tnhnrl | 32:f2f8ae34aadc | 981 | // _substate_array_counter++; |
tnhnrl | 32:f2f8ae34aadc | 982 | // |
tnhnrl | 32:f2f8ae34aadc | 983 | // //record the current substate for comparison |
tnhnrl | 32:f2f8ae34aadc | 984 | // _previous_substate = _substate; |
tnhnrl | 32:f2f8ae34aadc | 985 | // } |
tnhnrl | 32:f2f8ae34aadc | 986 | // |
tnhnrl | 32:f2f8ae34aadc | 987 | // return _substate; // message to calling function of what sub-state it's in |
tnhnrl | 32:f2f8ae34aadc | 988 | // } |
tnhnrl | 32:f2f8ae34aadc | 989 | //} |
tnhnrl | 20:8987a9ae2bc7 | 990 | |
tnhnrl | 20:8987a9ae2bc7 | 991 | // keyboard runs independently of the state machine, handling one key at a time |
tnhnrl | 20:8987a9ae2bc7 | 992 | //keyboard updates the desired _keyboard_state that is used in the state machine |
tnhnrl | 20:8987a9ae2bc7 | 993 | //and only allows input when the state is "idle" |
tnhnrl | 17:7c16b5671d0e | 994 | void StateMachine::keyboard() { |
tnhnrl | 16:3363b9f14913 | 995 | char userInput; |
tnhnrl | 20:8987a9ae2bc7 | 996 | |
tnhnrl | 16:3363b9f14913 | 997 | // check keyboard and make settings changes as requested |
tnhnrl | 17:7c16b5671d0e | 998 | // states can be changed only at the start of a sequence (when the system is in SIT_IDLE) |
tnhnrl | 21:38c8544db6f4 | 999 | |
tnhnrl | 21:38c8544db6f4 | 1000 | int _keyboard_state = -1; //made this a local variable because it was retaining the last keyboard state |
tnhnrl | 21:38c8544db6f4 | 1001 | |
tnhnrl | 28:16c83a2fdefa | 1002 | if (pc().readable() && (_state == SIT_IDLE || _state == KEYBOARD)) { |
tnhnrl | 16:3363b9f14913 | 1003 | // get the key |
tnhnrl | 17:7c16b5671d0e | 1004 | userInput = pc().getc(); |
tnhnrl | 17:7c16b5671d0e | 1005 | |
tnhnrl | 28:16c83a2fdefa | 1006 | //record that the keyboard was used |
tnhnrl | 28:16c83a2fdefa | 1007 | _state_array[_state_array_counter] = KEYBOARD; |
tnhnrl | 28:16c83a2fdefa | 1008 | _state_array_counter++; |
tnhnrl | 28:16c83a2fdefa | 1009 | |
tnhnrl | 21:38c8544db6f4 | 1010 | // keyboard has to reset timer each time it's used |
tnhnrl | 28:16c83a2fdefa | 1011 | _isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 1012 | |
tnhnrl | 16:3363b9f14913 | 1013 | // check command against desired control buttons |
tnhnrl | 16:3363b9f14913 | 1014 | if (userInput == 'D' or userInput == 'd') { |
tnhnrl | 17:7c16b5671d0e | 1015 | _keyboard_state = DIVE; |
tnhnrl | 16:3363b9f14913 | 1016 | } |
tnhnrl | 32:f2f8ae34aadc | 1017 | else if (userInput == 'G') { |
tnhnrl | 32:f2f8ae34aadc | 1018 | _keyboard_state = FIND_AUTO_NEUTRAL_DEPTH; //new test 12/7/2017 |
tnhnrl | 32:f2f8ae34aadc | 1019 | } |
tnhnrl | 32:f2f8ae34aadc | 1020 | else if (userInput == 'F') { |
tnhnrl | 32:f2f8ae34aadc | 1021 | _keyboard_state = FIND_AUTO_NEUTRAL_PITCH; //new test 12/7/2017 |
tnhnrl | 32:f2f8ae34aadc | 1022 | } |
tnhnrl | 16:3363b9f14913 | 1023 | else if (userInput == 'N' or userInput == 'n') { |
tnhnrl | 17:7c16b5671d0e | 1024 | _keyboard_state = FIND_NEUTRAL; |
tnhnrl | 17:7c16b5671d0e | 1025 | } |
tnhnrl | 17:7c16b5671d0e | 1026 | else if (userInput == 'M' or userInput == 'm') { |
tnhnrl | 17:7c16b5671d0e | 1027 | //currently does not run if there is no file. |
tnhnrl | 17:7c16b5671d0e | 1028 | |
tnhnrl | 17:7c16b5671d0e | 1029 | //need to add method to Sequence Controller that returns -1 |
tnhnrl | 17:7c16b5671d0e | 1030 | // or some check that insures you cannot run the dive sequence without a file |
tnhnrl | 17:7c16b5671d0e | 1031 | |
tnhnrl | 17:7c16b5671d0e | 1032 | stateMachine().getDiveSequence(); //get first sequence on keyboard press |
tnhnrl | 17:7c16b5671d0e | 1033 | _keyboard_state = currentStateStruct.state; |
tnhnrl | 17:7c16b5671d0e | 1034 | |
tnhnrl | 17:7c16b5671d0e | 1035 | pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles |
tnhnrl | 16:3363b9f14913 | 1036 | } |
tnhnrl | 16:3363b9f14913 | 1037 | else if (userInput == 'R' or userInput == 'r') { |
tnhnrl | 17:7c16b5671d0e | 1038 | _keyboard_state = RISE; |
tnhnrl | 16:3363b9f14913 | 1039 | } |
tnhnrl | 16:3363b9f14913 | 1040 | else if (userInput == 'L' or userInput == 'l') { |
tnhnrl | 17:7c16b5671d0e | 1041 | _keyboard_state = FLOAT_LEVEL; |
tnhnrl | 16:3363b9f14913 | 1042 | } |
tnhnrl | 16:3363b9f14913 | 1043 | else if (userInput == 'B' or userInput == 'b') { |
tnhnrl | 17:7c16b5671d0e | 1044 | _keyboard_state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 1045 | } |
tnhnrl | 16:3363b9f14913 | 1046 | else if (userInput == 'E' or userInput == 'e') { |
tnhnrl | 17:7c16b5671d0e | 1047 | _keyboard_state = EMERGENCY_CLIMB; |
tnhnrl | 16:3363b9f14913 | 1048 | } |
tnhnrl | 32:f2f8ae34aadc | 1049 | else if (userInput == 'P') { |
tnhnrl | 32:f2f8ae34aadc | 1050 | //Print current SD card log file |
tnhnrl | 36:966a86937e17 | 1051 | //printCurrentSdLog(); |
tnhnrl | 36:966a86937e17 | 1052 | mbedLogger().printCurrentLogFile(); //print the current log file to the screen |
tnhnrl | 32:f2f8ae34aadc | 1053 | } |
tnhnrl | 32:f2f8ae34aadc | 1054 | else if (userInput == 'X') { |
tnhnrl | 32:f2f8ae34aadc | 1055 | printDirectory(); |
tnhnrl | 34:9b66c5188051 | 1056 | //mbedLogger().printDirectory(); //print all log files to the screen |
tnhnrl | 32:f2f8ae34aadc | 1057 | } |
tnhnrl | 34:9b66c5188051 | 1058 | else if (userInput == 'V') { |
tnhnrl | 34:9b66c5188051 | 1059 | _keyboard_state = TRANSMIT_DATA; //Transmit data (work in progress) |
tnhnrl | 32:f2f8ae34aadc | 1060 | } |
tnhnrl | 28:16c83a2fdefa | 1061 | // else if (userInput == 'H' or userInput == 'h') { |
tnhnrl | 28:16c83a2fdefa | 1062 | // pc().printf("running homing procedure\r\n"); |
tnhnrl | 28:16c83a2fdefa | 1063 | // bce().unpause(); bce().homePiston(); bce().pause(); |
tnhnrl | 28:16c83a2fdefa | 1064 | // batt().unpause(); batt().homePiston(); batt().pause(); |
tnhnrl | 28:16c83a2fdefa | 1065 | // } |
tnhnrl | 28:16c83a2fdefa | 1066 | else if (userInput == 'z' or userInput == 'Z') { |
tnhnrl | 28:16c83a2fdefa | 1067 | pc().printf("FSG FSM States: \n\r"); |
tnhnrl | 28:16c83a2fdefa | 1068 | string string_state; |
tnhnrl | 28:16c83a2fdefa | 1069 | |
tnhnrl | 28:16c83a2fdefa | 1070 | for (int i = 0; i < _state_array_counter; i++) { |
tnhnrl | 28:16c83a2fdefa | 1071 | if (_state_array[i] == SIT_IDLE) |
tnhnrl | 28:16c83a2fdefa | 1072 | string_state = "SIT_IDLE <END>"; |
tnhnrl | 28:16c83a2fdefa | 1073 | else if (_state_array[i] == FIND_NEUTRAL) |
tnhnrl | 28:16c83a2fdefa | 1074 | string_state = "FIND_NEUTRAL"; |
tnhnrl | 28:16c83a2fdefa | 1075 | else if (_state_array[i] == DIVE) |
tnhnrl | 28:16c83a2fdefa | 1076 | string_state = "DIVE"; |
tnhnrl | 28:16c83a2fdefa | 1077 | else if (_state_array[i] == RISE) |
tnhnrl | 28:16c83a2fdefa | 1078 | string_state = "RISE"; |
tnhnrl | 28:16c83a2fdefa | 1079 | else if (_state_array[i] == FLOAT_LEVEL) |
tnhnrl | 28:16c83a2fdefa | 1080 | string_state = "FLOAT_LEVEL"; |
tnhnrl | 28:16c83a2fdefa | 1081 | else if (_state_array[i] == FLOAT_BROADCAST) |
tnhnrl | 28:16c83a2fdefa | 1082 | string_state = "FLOAT_BROADCAST"; |
tnhnrl | 28:16c83a2fdefa | 1083 | else if (_state_array[i] == EMERGENCY_CLIMB) |
tnhnrl | 28:16c83a2fdefa | 1084 | string_state = "EMERGENCY_CLIMB"; |
tnhnrl | 28:16c83a2fdefa | 1085 | else if (_state_array[i] == MULTI_DIVE) |
tnhnrl | 28:16c83a2fdefa | 1086 | string_state = "MULTI_DIVE"; |
tnhnrl | 28:16c83a2fdefa | 1087 | else if (_state_array[i] == MULTI_RISE) |
tnhnrl | 28:16c83a2fdefa | 1088 | string_state = "MULTI_RISE"; |
tnhnrl | 28:16c83a2fdefa | 1089 | else if (_state_array[i] == KEYBOARD) |
tnhnrl | 28:16c83a2fdefa | 1090 | string_state = "KEYBOARD"; |
tnhnrl | 28:16c83a2fdefa | 1091 | pc().printf("State #%d: %d (%s)\n\r", i, _state_array[i], string_state.c_str()); |
tnhnrl | 28:16c83a2fdefa | 1092 | } |
tnhnrl | 28:16c83a2fdefa | 1093 | |
tnhnrl | 28:16c83a2fdefa | 1094 | pc().printf("\n\rNeutral sub-FSM States: \n\r"); |
tnhnrl | 28:16c83a2fdefa | 1095 | string string_substate; |
tnhnrl | 28:16c83a2fdefa | 1096 | |
tnhnrl | 28:16c83a2fdefa | 1097 | for (int i = 0; i < _substate_array_counter; i++) { |
tnhnrl | 32:f2f8ae34aadc | 1098 | if (_substate_array[i] == NEUTRAL_SINKING) |
tnhnrl | 28:16c83a2fdefa | 1099 | string_substate = "NEUTRAL_SINKING"; |
tnhnrl | 28:16c83a2fdefa | 1100 | else if (_substate_array[i] == NEUTRAL_SLOWLY_RISE) |
tnhnrl | 28:16c83a2fdefa | 1101 | string_substate = "NEUTRAL_SLOWLY_RISE"; |
tnhnrl | 28:16c83a2fdefa | 1102 | else if (_substate_array[i] == NEUTRAL_CHECK_PITCH) |
tnhnrl | 28:16c83a2fdefa | 1103 | string_substate = "NEUTRAL_CHECK_PITCH"; |
tnhnrl | 28:16c83a2fdefa | 1104 | else if (_substate_array[i] == NEUTRAL_EXIT) |
tnhnrl | 28:16c83a2fdefa | 1105 | string_substate = "NEUTRAL_EXIT <-- "; |
tnhnrl | 28:16c83a2fdefa | 1106 | else if (_substate_array[i] == EMERGENCY_CLIMB) |
tnhnrl | 28:16c83a2fdefa | 1107 | string_substate = " -- > EMERGENCY_CLIMB <-- "; |
tnhnrl | 28:16c83a2fdefa | 1108 | pc().printf("Neutral Substate #%d: %d (%s)\n\r", i, _state_array[i], string_substate.c_str()); |
tnhnrl | 28:16c83a2fdefa | 1109 | } |
tnhnrl | 28:16c83a2fdefa | 1110 | pc().printf("\n\r"); //make space between printouts |
tnhnrl | 16:3363b9f14913 | 1111 | } |
tnhnrl | 16:3363b9f14913 | 1112 | else if (userInput == 'T' or userInput == 't') { |
tnhnrl | 16:3363b9f14913 | 1113 | pc().printf("taring depth sensor\r\n"); |
tnhnrl | 16:3363b9f14913 | 1114 | pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt()); |
tnhnrl | 16:3363b9f14913 | 1115 | wait(0.1); |
tnhnrl | 16:3363b9f14913 | 1116 | depth().tare(); // tares to ambient (do on surface) |
tnhnrl | 16:3363b9f14913 | 1117 | pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt()); |
tnhnrl | 16:3363b9f14913 | 1118 | } |
tnhnrl | 16:3363b9f14913 | 1119 | |
tnhnrl | 16:3363b9f14913 | 1120 | else if (userInput == '[' or userInput == '{') { |
tnhnrl | 32:f2f8ae34aadc | 1121 | _neutral_bce_pos_mm = depthLoop().getOutputOffset() - 1; |
tnhnrl | 32:f2f8ae34aadc | 1122 | depthLoop().setOutputOffset(_neutral_bce_pos_mm); // decrease the bce neutral setpoint |
tnhnrl | 32:f2f8ae34aadc | 1123 | pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset()); |
tnhnrl | 32:f2f8ae34aadc | 1124 | // save neutral depth value to config file |
tnhnrl | 32:f2f8ae34aadc | 1125 | configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); |
tnhnrl | 16:3363b9f14913 | 1126 | } |
tnhnrl | 16:3363b9f14913 | 1127 | else if (userInput == ']' or userInput == '}') { |
tnhnrl | 32:f2f8ae34aadc | 1128 | _neutral_bce_pos_mm = depthLoop().getOutputOffset() + 1; |
tnhnrl | 32:f2f8ae34aadc | 1129 | depthLoop().setOutputOffset(_neutral_bce_pos_mm); // increase the bce neutral setpoint |
tnhnrl | 32:f2f8ae34aadc | 1130 | pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset()); |
tnhnrl | 32:f2f8ae34aadc | 1131 | // save neutral depth value to config file |
tnhnrl | 32:f2f8ae34aadc | 1132 | configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); |
tnhnrl | 16:3363b9f14913 | 1133 | } |
tnhnrl | 16:3363b9f14913 | 1134 | else if (userInput == '<' or userInput == ',') { |
tnhnrl | 32:f2f8ae34aadc | 1135 | _neutral_batt_pos_mm = pitchLoop().getOutputOffset() - 1; |
tnhnrl | 32:f2f8ae34aadc | 1136 | pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // decrease the batt neutral setpoint |
tnhnrl | 32:f2f8ae34aadc | 1137 | pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset()); |
tnhnrl | 32:f2f8ae34aadc | 1138 | // save neutral pitch value to config file |
tnhnrl | 32:f2f8ae34aadc | 1139 | configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); |
tnhnrl | 16:3363b9f14913 | 1140 | } |
tnhnrl | 16:3363b9f14913 | 1141 | else if (userInput == '>' or userInput == '.') { |
tnhnrl | 32:f2f8ae34aadc | 1142 | _neutral_batt_pos_mm = pitchLoop().getOutputOffset() + 1; |
tnhnrl | 32:f2f8ae34aadc | 1143 | pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // increase the batt neutral setpoint |
tnhnrl | 32:f2f8ae34aadc | 1144 | pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset()); |
tnhnrl | 32:f2f8ae34aadc | 1145 | // save neutral pitch value to config file |
tnhnrl | 32:f2f8ae34aadc | 1146 | configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); |
tnhnrl | 16:3363b9f14913 | 1147 | } |
tnhnrl | 16:3363b9f14913 | 1148 | |
tnhnrl | 16:3363b9f14913 | 1149 | else if (userInput == '?') { |
tnhnrl | 16:3363b9f14913 | 1150 | pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n"); |
tnhnrl | 16:3363b9f14913 | 1151 | wait(0.5); |
tnhnrl | 16:3363b9f14913 | 1152 | mbed_reset(); |
tnhnrl | 16:3363b9f14913 | 1153 | } |
tnhnrl | 20:8987a9ae2bc7 | 1154 | |
tnhnrl | 16:3363b9f14913 | 1155 | // change settings |
tnhnrl | 16:3363b9f14913 | 1156 | else if (userInput == 'Q' or userInput == 'q') { |
tnhnrl | 32:f2f8ae34aadc | 1157 | _pitch_command -= 0.5; //decrement the pitch setpoint |
tnhnrl | 32:f2f8ae34aadc | 1158 | pitchLoop().setCommand(_pitch_command); |
tnhnrl | 16:3363b9f14913 | 1159 | pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 1160 | } |
tnhnrl | 16:3363b9f14913 | 1161 | else if (userInput == 'W' or userInput == 'w') { |
tnhnrl | 32:f2f8ae34aadc | 1162 | _pitch_command += 0.5; //increment the pitch setpoint |
tnhnrl | 32:f2f8ae34aadc | 1163 | pitchLoop().setCommand(_pitch_command); |
tnhnrl | 16:3363b9f14913 | 1164 | pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 1165 | } |
tnhnrl | 16:3363b9f14913 | 1166 | else if (userInput == 'A' or userInput == 'a') { |
tnhnrl | 32:f2f8ae34aadc | 1167 | _depth_command -= 0.5; //decrement the depth setpoint |
tnhnrl | 32:f2f8ae34aadc | 1168 | depthLoop().setCommand(_depth_command); |
tnhnrl | 16:3363b9f14913 | 1169 | pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 1170 | } |
tnhnrl | 16:3363b9f14913 | 1171 | else if (userInput == 'S' or userInput == 's') { |
tnhnrl | 32:f2f8ae34aadc | 1172 | _depth_command += 0.5; //increment the depth setpoint |
tnhnrl | 32:f2f8ae34aadc | 1173 | depthLoop().setCommand(_depth_command); |
tnhnrl | 16:3363b9f14913 | 1174 | pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 1175 | } |
tnhnrl | 16:3363b9f14913 | 1176 | else if (userInput == '-') { |
tnhnrl | 17:7c16b5671d0e | 1177 | _timeout -= 10.0; //decrement the timeout |
tnhnrl | 17:7c16b5671d0e | 1178 | pc().printf(">>> timeout decreased: %d\r\n", _timeout); |
tnhnrl | 16:3363b9f14913 | 1179 | } |
tnhnrl | 16:3363b9f14913 | 1180 | else if (userInput == '=' or userInput == '+') { |
tnhnrl | 17:7c16b5671d0e | 1181 | _timeout += 10.0; //increment the timeout |
tnhnrl | 17:7c16b5671d0e | 1182 | pc().printf(">>> timeout increased: %d\r\n", _timeout); |
tnhnrl | 16:3363b9f14913 | 1183 | } |
tnhnrl | 16:3363b9f14913 | 1184 | |
tnhnrl | 16:3363b9f14913 | 1185 | // go to sub-menus for the PID gains (this is blocking) |
tnhnrl | 16:3363b9f14913 | 1186 | else if (userInput == '1') { |
tnhnrl | 16:3363b9f14913 | 1187 | keyboard_menu_BCE_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 1188 | } |
tnhnrl | 16:3363b9f14913 | 1189 | else if (userInput == '2') { |
tnhnrl | 16:3363b9f14913 | 1190 | keyboard_menu_BATT_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 1191 | } |
tnhnrl | 16:3363b9f14913 | 1192 | else if (userInput == '3') { |
tnhnrl | 16:3363b9f14913 | 1193 | keyboard_menu_DEPTH_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 1194 | } |
tnhnrl | 16:3363b9f14913 | 1195 | else if (userInput == '4') { |
tnhnrl | 16:3363b9f14913 | 1196 | keyboard_menu_PITCH_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 1197 | } |
tnhnrl | 16:3363b9f14913 | 1198 | |
tnhnrl | 16:3363b9f14913 | 1199 | else if (userInput == 'C' or userInput == 'c') { |
tnhnrl | 32:f2f8ae34aadc | 1200 | |
tnhnrl | 32:f2f8ae34aadc | 1201 | pc().printf("\n\n\rCURRENT STATUS AND PARAMETERS:\n\r"); |
tnhnrl | 32:f2f8ae34aadc | 1202 | pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition()); |
tnhnrl | 32:f2f8ae34aadc | 1203 | pc().printf("pitch: %3.1f deg\r\n",imu().getPitch()); |
tnhnrl | 16:3363b9f14913 | 1204 | pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 1205 | pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 1206 | pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 1207 | pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 1208 | pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 1209 | pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand()); |
tnhnrl | 32:f2f8ae34aadc | 1210 | |
tnhnrl | 32:f2f8ae34aadc | 1211 | pc().printf("\n\rNeutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm); |
tnhnrl | 28:16c83a2fdefa | 1212 | pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset()); |
tnhnrl | 28:16c83a2fdefa | 1213 | pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset()); |
tnhnrl | 32:f2f8ae34aadc | 1214 | pc().printf("Max recorded depth: neutral: %0.1f, dive: %0.1f, auto_neutral_depth: %0.1f\n\n\r",_max_recorded_depth_neutral, _max_recorded_depth_dive, _max_recorded_auto_neutral_depth); |
tnhnrl | 16:3363b9f14913 | 1215 | } |
tnhnrl | 17:7c16b5671d0e | 1216 | |
tnhnrl | 17:7c16b5671d0e | 1217 | //when you read the keyboard successfully, change the state |
tnhnrl | 28:16c83a2fdefa | 1218 | _state = _keyboard_state; //set state at the end of this function |
tnhnrl | 16:3363b9f14913 | 1219 | } |
tnhnrl | 16:3363b9f14913 | 1220 | } |
tnhnrl | 20:8987a9ae2bc7 | 1221 | |
tnhnrl | 20:8987a9ae2bc7 | 1222 | |
tnhnrl | 16:3363b9f14913 | 1223 | void StateMachine::keyboard_menu_BCE_PID_settings() { |
tnhnrl | 16:3363b9f14913 | 1224 | char PID_key; |
tnhnrl | 16:3363b9f14913 | 1225 | float gain_step_size = 0.01; // modify this to change gain step size |
tnhnrl | 16:3363b9f14913 | 1226 | float KP = bce().getControllerP(); // load current value |
tnhnrl | 16:3363b9f14913 | 1227 | float KI = bce().getControllerI(); // load current global value |
tnhnrl | 16:3363b9f14913 | 1228 | float KD = bce().getControllerD(); // load current global value |
tnhnrl | 16:3363b9f14913 | 1229 | |
tnhnrl | 16:3363b9f14913 | 1230 | // show the menu |
tnhnrl | 16:3363b9f14913 | 1231 | pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)"); |
tnhnrl | 16:3363b9f14913 | 1232 | pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); |
tnhnrl | 16:3363b9f14913 | 1233 | pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r"); |
tnhnrl | 21:38c8544db6f4 | 1234 | 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()); |
tnhnrl | 16:3363b9f14913 | 1235 | |
tnhnrl | 16:3363b9f14913 | 1236 | // handle the key presses |
tnhnrl | 16:3363b9f14913 | 1237 | while(1) { |
tnhnrl | 16:3363b9f14913 | 1238 | // get the user's keystroke from either of the two inputs |
tnhnrl | 16:3363b9f14913 | 1239 | if (pc().readable()) { |
tnhnrl | 16:3363b9f14913 | 1240 | PID_key = pc().getc(); |
tnhnrl | 16:3363b9f14913 | 1241 | } |
tnhnrl | 16:3363b9f14913 | 1242 | else { |
tnhnrl | 16:3363b9f14913 | 1243 | continue; // didn't get a user input, so keep waiting for it |
tnhnrl | 16:3363b9f14913 | 1244 | } |
tnhnrl | 16:3363b9f14913 | 1245 | |
tnhnrl | 16:3363b9f14913 | 1246 | // handle the user's key input |
tnhnrl | 16:3363b9f14913 | 1247 | if (PID_key == '-') { |
tnhnrl | 16:3363b9f14913 | 1248 | KP -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1249 | pc().printf("P gain: %0.5f \r\n", KP); |
tnhnrl | 16:3363b9f14913 | 1250 | } |
tnhnrl | 16:3363b9f14913 | 1251 | else if (PID_key == '=') { |
tnhnrl | 16:3363b9f14913 | 1252 | KP += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1253 | pc().printf("P gain: %0.5f \r\n", KP); |
tnhnrl | 16:3363b9f14913 | 1254 | } |
tnhnrl | 16:3363b9f14913 | 1255 | else if (PID_key == '[') { |
tnhnrl | 16:3363b9f14913 | 1256 | KI -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1257 | pc().printf("I gain: %0.5f \r\n", KI); |
tnhnrl | 16:3363b9f14913 | 1258 | } |
tnhnrl | 16:3363b9f14913 | 1259 | else if (PID_key == ']') { |
tnhnrl | 16:3363b9f14913 | 1260 | KI += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1261 | pc().printf("I gain: %0.5f \r\n", KI); |
tnhnrl | 16:3363b9f14913 | 1262 | } |
tnhnrl | 16:3363b9f14913 | 1263 | else if (PID_key == ';') { |
tnhnrl | 16:3363b9f14913 | 1264 | KD -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1265 | pc().printf("D gain: %0.5f \r\n", KD); |
tnhnrl | 16:3363b9f14913 | 1266 | } |
tnhnrl | 16:3363b9f14913 | 1267 | else if (PID_key == '\'') { |
tnhnrl | 16:3363b9f14913 | 1268 | KD += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1269 | pc().printf("D gain: %0.5f \r\n", KD); |
tnhnrl | 16:3363b9f14913 | 1270 | } |
tnhnrl | 16:3363b9f14913 | 1271 | else if (PID_key == 'S') { // user wants to save these modified values |
tnhnrl | 16:3363b9f14913 | 1272 | // set values |
tnhnrl | 16:3363b9f14913 | 1273 | bce().setControllerP(KP); |
tnhnrl | 16:3363b9f14913 | 1274 | bce().setControllerI(KI); |
tnhnrl | 16:3363b9f14913 | 1275 | bce().setControllerD(KD); |
tnhnrl | 16:3363b9f14913 | 1276 | |
tnhnrl | 16:3363b9f14913 | 1277 | // save into "PID.cfg" |
tnhnrl | 16:3363b9f14913 | 1278 | //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); |
tnhnrl | 16:3363b9f14913 | 1279 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1280 | } |
tnhnrl | 16:3363b9f14913 | 1281 | else if (PID_key == 'X') { |
tnhnrl | 16:3363b9f14913 | 1282 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1283 | } |
tnhnrl | 16:3363b9f14913 | 1284 | else { |
tnhnrl | 16:3363b9f14913 | 1285 | pc().printf("\n\rThis key does nothing here. "); |
tnhnrl | 16:3363b9f14913 | 1286 | } |
tnhnrl | 16:3363b9f14913 | 1287 | } |
tnhnrl | 16:3363b9f14913 | 1288 | } |
tnhnrl | 20:8987a9ae2bc7 | 1289 | |
tnhnrl | 16:3363b9f14913 | 1290 | void StateMachine::keyboard_menu_BATT_PID_settings() { |
tnhnrl | 16:3363b9f14913 | 1291 | char PID_key; |
tnhnrl | 16:3363b9f14913 | 1292 | float gain_step_size = 0.01; // modify this to change gain step size |
tnhnrl | 16:3363b9f14913 | 1293 | float KP = batt().getControllerP(); // load current global value |
tnhnrl | 16:3363b9f14913 | 1294 | float KI = batt().getControllerI(); // load current global value |
tnhnrl | 16:3363b9f14913 | 1295 | float KD = batt().getControllerD(); // load current global value |
tnhnrl | 16:3363b9f14913 | 1296 | |
tnhnrl | 16:3363b9f14913 | 1297 | // print the menu |
tnhnrl | 16:3363b9f14913 | 1298 | pc().printf("\n\r2: Battery Motor PID gain settings (MENU)"); |
tnhnrl | 16:3363b9f14913 | 1299 | pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); |
tnhnrl | 16:3363b9f14913 | 1300 | pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); |
tnhnrl | 21:38c8544db6f4 | 1301 | 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()); |
tnhnrl | 20:8987a9ae2bc7 | 1302 | |
tnhnrl | 16:3363b9f14913 | 1303 | // handle the key presses |
tnhnrl | 16:3363b9f14913 | 1304 | while(1) { |
tnhnrl | 16:3363b9f14913 | 1305 | // get the user's keystroke from either of the two inputs |
tnhnrl | 16:3363b9f14913 | 1306 | if (pc().readable()) { |
tnhnrl | 16:3363b9f14913 | 1307 | PID_key = pc().getc(); |
tnhnrl | 16:3363b9f14913 | 1308 | } |
tnhnrl | 16:3363b9f14913 | 1309 | else { |
tnhnrl | 16:3363b9f14913 | 1310 | continue; // didn't get a user input, so keep waiting for it |
tnhnrl | 16:3363b9f14913 | 1311 | } |
tnhnrl | 16:3363b9f14913 | 1312 | |
tnhnrl | 16:3363b9f14913 | 1313 | // handle the user's key input |
tnhnrl | 16:3363b9f14913 | 1314 | if (PID_key == '-') { |
tnhnrl | 16:3363b9f14913 | 1315 | KP -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1316 | pc().printf("\rP gain: %0.5f ", KP); |
tnhnrl | 16:3363b9f14913 | 1317 | } |
tnhnrl | 16:3363b9f14913 | 1318 | else if (PID_key == '=') { |
tnhnrl | 16:3363b9f14913 | 1319 | KP += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1320 | pc().printf("\rP gain: %0.5f ", KP); |
tnhnrl | 16:3363b9f14913 | 1321 | } |
tnhnrl | 16:3363b9f14913 | 1322 | else if (PID_key == '[') { |
tnhnrl | 16:3363b9f14913 | 1323 | KI -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1324 | pc().printf("\rI gain: %0.5f ", KI); |
tnhnrl | 16:3363b9f14913 | 1325 | } |
tnhnrl | 16:3363b9f14913 | 1326 | else if (PID_key == ']') { |
tnhnrl | 16:3363b9f14913 | 1327 | KI += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1328 | pc().printf("\rI gain: %0.5f ", KI); |
tnhnrl | 16:3363b9f14913 | 1329 | } |
tnhnrl | 16:3363b9f14913 | 1330 | else if (PID_key == ';') { |
tnhnrl | 16:3363b9f14913 | 1331 | KD -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1332 | pc().printf("\rD gain: %0.5f ", KD); |
tnhnrl | 16:3363b9f14913 | 1333 | } |
tnhnrl | 16:3363b9f14913 | 1334 | else if (PID_key == '\'') { |
tnhnrl | 16:3363b9f14913 | 1335 | KD += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 1336 | pc().printf("\rD gain: %0.5f ", KD); |
tnhnrl | 16:3363b9f14913 | 1337 | } |
tnhnrl | 16:3363b9f14913 | 1338 | else if (PID_key == 'S') { // user wants to save the modified values |
tnhnrl | 16:3363b9f14913 | 1339 | // set global values |
tnhnrl | 16:3363b9f14913 | 1340 | batt().setControllerP(KP); |
tnhnrl | 16:3363b9f14913 | 1341 | batt().setControllerI(KI); |
tnhnrl | 16:3363b9f14913 | 1342 | batt().setControllerD(KD); |
tnhnrl | 16:3363b9f14913 | 1343 | |
tnhnrl | 16:3363b9f14913 | 1344 | // save to "PID.cfg" file |
tnhnrl | 16:3363b9f14913 | 1345 | //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); |
tnhnrl | 16:3363b9f14913 | 1346 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1347 | } |
tnhnrl | 16:3363b9f14913 | 1348 | else if (PID_key == 'X') { |
tnhnrl | 16:3363b9f14913 | 1349 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1350 | } |
tnhnrl | 16:3363b9f14913 | 1351 | else { |
tnhnrl | 16:3363b9f14913 | 1352 | pc().printf("This key does nothing here.\r"); |
tnhnrl | 16:3363b9f14913 | 1353 | } |
tnhnrl | 16:3363b9f14913 | 1354 | } |
tnhnrl | 16:3363b9f14913 | 1355 | } |
tnhnrl | 20:8987a9ae2bc7 | 1356 | |
tnhnrl | 16:3363b9f14913 | 1357 | void StateMachine::keyboard_menu_DEPTH_PID_settings() { |
tnhnrl | 16:3363b9f14913 | 1358 | char PID_key; |
tnhnrl | 16:3363b9f14913 | 1359 | float gain_step_size = 0.01; // modify this to change gain step size |
tnhnrl | 16:3363b9f14913 | 1360 | |
tnhnrl | 16:3363b9f14913 | 1361 | // show the menu |
tnhnrl | 16:3363b9f14913 | 1362 | pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)"); |
tnhnrl | 16:3363b9f14913 | 1363 | pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); |
tnhnrl | 16:3363b9f14913 | 1364 | pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r"); |
tnhnrl | 16:3363b9f14913 | 1365 | 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()); |
tnhnrl | 16:3363b9f14913 | 1366 | |
tnhnrl | 16:3363b9f14913 | 1367 | // handle the key presses |
tnhnrl | 16:3363b9f14913 | 1368 | while(1) { |
tnhnrl | 16:3363b9f14913 | 1369 | // get the user's keystroke from either of the two inputs |
tnhnrl | 16:3363b9f14913 | 1370 | if (pc().readable()) { |
tnhnrl | 16:3363b9f14913 | 1371 | PID_key = pc().getc(); |
tnhnrl | 16:3363b9f14913 | 1372 | } |
tnhnrl | 16:3363b9f14913 | 1373 | else { |
tnhnrl | 16:3363b9f14913 | 1374 | continue; // didn't get a user input, so keep waiting for it |
tnhnrl | 16:3363b9f14913 | 1375 | } |
tnhnrl | 16:3363b9f14913 | 1376 | |
tnhnrl | 16:3363b9f14913 | 1377 | // handle the user's key input |
tnhnrl | 16:3363b9f14913 | 1378 | if (PID_key == '-') { |
tnhnrl | 21:38c8544db6f4 | 1379 | _depth_KP -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1380 | pc().printf("P gain: %0.5f \r\n", _depth_KP); |
tnhnrl | 16:3363b9f14913 | 1381 | } |
tnhnrl | 16:3363b9f14913 | 1382 | else if (PID_key == '=') { |
tnhnrl | 21:38c8544db6f4 | 1383 | _depth_KP += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1384 | pc().printf("P gain: %0.5f \r\n", _depth_KP); |
tnhnrl | 16:3363b9f14913 | 1385 | } |
tnhnrl | 16:3363b9f14913 | 1386 | else if (PID_key == '[') { |
tnhnrl | 21:38c8544db6f4 | 1387 | _depth_KI -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1388 | pc().printf("I gain: %0.5f \r\n", _depth_KI); |
tnhnrl | 16:3363b9f14913 | 1389 | } |
tnhnrl | 16:3363b9f14913 | 1390 | else if (PID_key == ']') { |
tnhnrl | 21:38c8544db6f4 | 1391 | _depth_KI += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1392 | pc().printf("I gain: %0.5f \r\n", _depth_KI); |
tnhnrl | 16:3363b9f14913 | 1393 | } |
tnhnrl | 16:3363b9f14913 | 1394 | else if (PID_key == ';') { |
tnhnrl | 21:38c8544db6f4 | 1395 | _depth_KD -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1396 | pc().printf("D gain: %0.5f \r\n", _depth_KD); |
tnhnrl | 16:3363b9f14913 | 1397 | } |
tnhnrl | 16:3363b9f14913 | 1398 | else if (PID_key == '\'') { |
tnhnrl | 21:38c8544db6f4 | 1399 | _depth_KD += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1400 | pc().printf("D gain: %0.5f \r\n", _depth_KD); |
tnhnrl | 16:3363b9f14913 | 1401 | } |
tnhnrl | 16:3363b9f14913 | 1402 | else if (PID_key == 'S') { // user wants to save these settings |
tnhnrl | 16:3363b9f14913 | 1403 | // set global values |
tnhnrl | 21:38c8544db6f4 | 1404 | depthLoop().setControllerP(_depth_KP); |
tnhnrl | 21:38c8544db6f4 | 1405 | depthLoop().setControllerI(_depth_KI); |
tnhnrl | 21:38c8544db6f4 | 1406 | depthLoop().setControllerD(_depth_KD); |
tnhnrl | 16:3363b9f14913 | 1407 | |
tnhnrl | 21:38c8544db6f4 | 1408 | // save depth PID values for outer loop |
tnhnrl | 21:38c8544db6f4 | 1409 | configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); |
tnhnrl | 16:3363b9f14913 | 1410 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1411 | } |
tnhnrl | 16:3363b9f14913 | 1412 | else if (PID_key == 'X') { |
tnhnrl | 16:3363b9f14913 | 1413 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1414 | } |
tnhnrl | 16:3363b9f14913 | 1415 | else { |
tnhnrl | 16:3363b9f14913 | 1416 | pc().printf("\n\rThis key does nothing here. "); |
tnhnrl | 16:3363b9f14913 | 1417 | } |
tnhnrl | 16:3363b9f14913 | 1418 | } |
tnhnrl | 16:3363b9f14913 | 1419 | } |
tnhnrl | 16:3363b9f14913 | 1420 | |
tnhnrl | 16:3363b9f14913 | 1421 | void StateMachine::keyboard_menu_PITCH_PID_settings() { |
tnhnrl | 16:3363b9f14913 | 1422 | char PID_key; |
tnhnrl | 16:3363b9f14913 | 1423 | float gain_step_size = 0.01; // modify this to change gain step size |
tnhnrl | 16:3363b9f14913 | 1424 | |
tnhnrl | 16:3363b9f14913 | 1425 | // print the menu |
tnhnrl | 16:3363b9f14913 | 1426 | pc().printf("\n\r2: Battery Motor PID gain settings (MENU)"); |
tnhnrl | 16:3363b9f14913 | 1427 | pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); |
tnhnrl | 16:3363b9f14913 | 1428 | pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); |
tnhnrl | 16:3363b9f14913 | 1429 | 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()); |
tnhnrl | 20:8987a9ae2bc7 | 1430 | |
tnhnrl | 16:3363b9f14913 | 1431 | // handle the key presses |
tnhnrl | 16:3363b9f14913 | 1432 | while(1) { |
tnhnrl | 16:3363b9f14913 | 1433 | // get the user's keystroke from either of the two inputs |
tnhnrl | 16:3363b9f14913 | 1434 | if (pc().readable()) { |
tnhnrl | 16:3363b9f14913 | 1435 | PID_key = pc().getc(); |
tnhnrl | 16:3363b9f14913 | 1436 | } |
tnhnrl | 16:3363b9f14913 | 1437 | else { |
tnhnrl | 16:3363b9f14913 | 1438 | continue; // didn't get a user input, so keep waiting for it |
tnhnrl | 16:3363b9f14913 | 1439 | } |
tnhnrl | 16:3363b9f14913 | 1440 | |
tnhnrl | 16:3363b9f14913 | 1441 | // handle the user's key input |
tnhnrl | 16:3363b9f14913 | 1442 | if (PID_key == '-') { |
tnhnrl | 21:38c8544db6f4 | 1443 | _pitch_KP -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1444 | pc().printf("\rP gain: %0.5f ", _pitch_KP); |
tnhnrl | 16:3363b9f14913 | 1445 | } |
tnhnrl | 16:3363b9f14913 | 1446 | else if (PID_key == '=') { |
tnhnrl | 21:38c8544db6f4 | 1447 | _pitch_KP += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1448 | pc().printf("\rP gain: %0.5f ", _pitch_KP); |
tnhnrl | 16:3363b9f14913 | 1449 | } |
tnhnrl | 16:3363b9f14913 | 1450 | else if (PID_key == '[') { |
tnhnrl | 21:38c8544db6f4 | 1451 | _pitch_KI -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1452 | pc().printf("\rI gain: %0.5f ", _pitch_KI); |
tnhnrl | 16:3363b9f14913 | 1453 | } |
tnhnrl | 16:3363b9f14913 | 1454 | else if (PID_key == ']') { |
tnhnrl | 21:38c8544db6f4 | 1455 | _pitch_KI += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1456 | pc().printf("\rI gain: %0.5f ", _pitch_KI); |
tnhnrl | 16:3363b9f14913 | 1457 | } |
tnhnrl | 16:3363b9f14913 | 1458 | else if (PID_key == ';') { |
tnhnrl | 21:38c8544db6f4 | 1459 | _pitch_KD -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1460 | pc().printf("\rD gain: %0.5f ", _pitch_KD); |
tnhnrl | 16:3363b9f14913 | 1461 | } |
tnhnrl | 16:3363b9f14913 | 1462 | else if (PID_key == '\'') { |
tnhnrl | 21:38c8544db6f4 | 1463 | _pitch_KD += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1464 | pc().printf("\rD gain: %0.5f ", _pitch_KD); |
tnhnrl | 16:3363b9f14913 | 1465 | } |
tnhnrl | 16:3363b9f14913 | 1466 | else if (PID_key == 'S') { // user wants to save the modified values |
tnhnrl | 16:3363b9f14913 | 1467 | // set global values |
tnhnrl | 21:38c8544db6f4 | 1468 | pitchLoop().setControllerP(_pitch_KP); |
tnhnrl | 21:38c8544db6f4 | 1469 | pitchLoop().setControllerI(_pitch_KI); |
tnhnrl | 21:38c8544db6f4 | 1470 | pitchLoop().setControllerD(_pitch_KD); |
tnhnrl | 16:3363b9f14913 | 1471 | |
tnhnrl | 32:f2f8ae34aadc | 1472 | // save pitch PID values for outer loop (must save neutral position also) |
tnhnrl | 21:38c8544db6f4 | 1473 | configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); |
tnhnrl | 16:3363b9f14913 | 1474 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1475 | } |
tnhnrl | 16:3363b9f14913 | 1476 | else if (PID_key == 'X') { |
tnhnrl | 16:3363b9f14913 | 1477 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1478 | } |
tnhnrl | 16:3363b9f14913 | 1479 | else { |
tnhnrl | 16:3363b9f14913 | 1480 | pc().printf("This key does nothing here.\r"); |
tnhnrl | 16:3363b9f14913 | 1481 | } |
tnhnrl | 16:3363b9f14913 | 1482 | } |
tnhnrl | 16:3363b9f14913 | 1483 | } |
tnhnrl | 20:8987a9ae2bc7 | 1484 | |
tnhnrl | 16:3363b9f14913 | 1485 | float StateMachine::getDepthCommand() { |
tnhnrl | 32:f2f8ae34aadc | 1486 | return _depth_command; |
tnhnrl | 16:3363b9f14913 | 1487 | } |
tnhnrl | 20:8987a9ae2bc7 | 1488 | |
tnhnrl | 16:3363b9f14913 | 1489 | float StateMachine::getPitchCommand() { |
tnhnrl | 32:f2f8ae34aadc | 1490 | return _pitch_command; |
tnhnrl | 32:f2f8ae34aadc | 1491 | } |
tnhnrl | 32:f2f8ae34aadc | 1492 | |
tnhnrl | 32:f2f8ae34aadc | 1493 | float StateMachine::getDepthReading() { |
tnhnrl | 32:f2f8ae34aadc | 1494 | return _depth_reading; |
tnhnrl | 32:f2f8ae34aadc | 1495 | } |
tnhnrl | 32:f2f8ae34aadc | 1496 | |
tnhnrl | 32:f2f8ae34aadc | 1497 | float StateMachine::getPitchReading() { |
tnhnrl | 32:f2f8ae34aadc | 1498 | return _pitch_reading; |
tnhnrl | 32:f2f8ae34aadc | 1499 | } |
tnhnrl | 32:f2f8ae34aadc | 1500 | |
tnhnrl | 32:f2f8ae34aadc | 1501 | float StateMachine::getTimerReading() { |
tnhnrl | 32:f2f8ae34aadc | 1502 | return _timer_reading; |
tnhnrl | 17:7c16b5671d0e | 1503 | } |
tnhnrl | 28:16c83a2fdefa | 1504 | |
tnhnrl | 17:7c16b5671d0e | 1505 | void StateMachine::setState(int input_state) { |
tnhnrl | 21:38c8544db6f4 | 1506 | _state = input_state; |
tnhnrl | 17:7c16b5671d0e | 1507 | } |
tnhnrl | 20:8987a9ae2bc7 | 1508 | |
tnhnrl | 17:7c16b5671d0e | 1509 | int StateMachine::getState() { |
tnhnrl | 17:7c16b5671d0e | 1510 | return _state; //return the current state of the system |
tnhnrl | 17:7c16b5671d0e | 1511 | } |
tnhnrl | 20:8987a9ae2bc7 | 1512 | |
tnhnrl | 17:7c16b5671d0e | 1513 | void StateMachine::setTimeout(float input_timeout) { |
tnhnrl | 17:7c16b5671d0e | 1514 | _timeout = input_timeout; |
tnhnrl | 17:7c16b5671d0e | 1515 | } |
tnhnrl | 20:8987a9ae2bc7 | 1516 | |
tnhnrl | 17:7c16b5671d0e | 1517 | void StateMachine::setDepthCommand(float input_depth_command) { |
tnhnrl | 32:f2f8ae34aadc | 1518 | _depth_command = input_depth_command; |
tnhnrl | 17:7c16b5671d0e | 1519 | } |
tnhnrl | 20:8987a9ae2bc7 | 1520 | |
tnhnrl | 17:7c16b5671d0e | 1521 | void StateMachine::setPitchCommand(float input_pitch_command) { |
tnhnrl | 32:f2f8ae34aadc | 1522 | _pitch_command = input_pitch_command; |
tnhnrl | 17:7c16b5671d0e | 1523 | } |
tnhnrl | 20:8987a9ae2bc7 | 1524 | |
tnhnrl | 17:7c16b5671d0e | 1525 | void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) { |
tnhnrl | 21:38c8544db6f4 | 1526 | _neutral_batt_pos_mm = batt_pos_mm; |
tnhnrl | 21:38c8544db6f4 | 1527 | _neutral_bce_pos_mm = bce_pos_mm; |
tnhnrl | 17:7c16b5671d0e | 1528 | |
tnhnrl | 21:38c8544db6f4 | 1529 | pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm); |
tnhnrl | 17:7c16b5671d0e | 1530 | } |
tnhnrl | 20:8987a9ae2bc7 | 1531 | |
tnhnrl | 17:7c16b5671d0e | 1532 | int StateMachine::timeoutRunning() { |
tnhnrl | 28:16c83a2fdefa | 1533 | return _isTimeoutRunning; |
tnhnrl | 17:7c16b5671d0e | 1534 | } |
tnhnrl | 20:8987a9ae2bc7 | 1535 | |
tnhnrl | 17:7c16b5671d0e | 1536 | //process one state at a time |
tnhnrl | 17:7c16b5671d0e | 1537 | void StateMachine::getDiveSequence() { |
tnhnrl | 17:7c16b5671d0e | 1538 | //iterate through this sequence using the FSM |
tnhnrl | 24:c7d9b5bf3829 | 1539 | currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state; |
tnhnrl | 24:c7d9b5bf3829 | 1540 | currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout; |
tnhnrl | 24:c7d9b5bf3829 | 1541 | currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth; |
tnhnrl | 24:c7d9b5bf3829 | 1542 | currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch; |
tnhnrl | 17:7c16b5671d0e | 1543 | |
tnhnrl | 17:7c16b5671d0e | 1544 | _timeout = currentStateStruct.timeout; //set timeout before exiting this function |
tnhnrl | 32:f2f8ae34aadc | 1545 | } |
tnhnrl | 32:f2f8ae34aadc | 1546 | |
tnhnrl | 32:f2f8ae34aadc | 1547 | void StateMachine::printDirectory() { |
tnhnrl | 32:f2f8ae34aadc | 1548 | //create a DirectoryList object that points to the local directory |
tnhnrl | 32:f2f8ae34aadc | 1549 | DirectoryList mbed_dir( "/local" ); |
tnhnrl | 32:f2f8ae34aadc | 1550 | |
tnhnrl | 32:f2f8ae34aadc | 1551 | if ( mbed_dir.error_check() ) { |
tnhnrl | 32:f2f8ae34aadc | 1552 | //error( "MBED directory could not be opened\r\n" ); |
tnhnrl | 32:f2f8ae34aadc | 1553 | pc().printf("MBED directory could not be opened\r\n"); |
tnhnrl | 32:f2f8ae34aadc | 1554 | } |
tnhnrl | 32:f2f8ae34aadc | 1555 | |
tnhnrl | 35:2f66ea4863d5 | 1556 | else { |
tnhnrl | 35:2f66ea4863d5 | 1557 | pc().printf("\n\rFiles in MBED directory:\n\r"); |
tnhnrl | 35:2f66ea4863d5 | 1558 | for ( int i = 0; i < mbed_dir.size(); i++ ) |
tnhnrl | 35:2f66ea4863d5 | 1559 | pc().printf( "%s\r\n", mbed_dir[ i ].c_str() ); |
tnhnrl | 35:2f66ea4863d5 | 1560 | } |
tnhnrl | 35:2f66ea4863d5 | 1561 | |
tnhnrl | 35:2f66ea4863d5 | 1562 | //SD CARD DIRECTORY (does not work for openlog) |
tnhnrl | 32:f2f8ae34aadc | 1563 | DirectoryList sd_dir( "/sd" ); |
tnhnrl | 32:f2f8ae34aadc | 1564 | |
tnhnrl | 32:f2f8ae34aadc | 1565 | if ( sd_dir.error_check() ) { |
tnhnrl | 32:f2f8ae34aadc | 1566 | //error( "MBED directory could not be opened\r\n" ); |
tnhnrl | 35:2f66ea4863d5 | 1567 | pc().printf("SD directory could not be opened\r\n"); |
tnhnrl | 32:f2f8ae34aadc | 1568 | } |
tnhnrl | 35:2f66ea4863d5 | 1569 | else { |
tnhnrl | 35:2f66ea4863d5 | 1570 | pc().printf("\n\rFiles in SD card directory:\n\r"); |
tnhnrl | 35:2f66ea4863d5 | 1571 | for ( int i = 0; i < sd_dir.size(); i++ ) |
tnhnrl | 35:2f66ea4863d5 | 1572 | pc().printf( "%s\r\n", sd_dir[ i ].c_str() ); |
tnhnrl | 35:2f66ea4863d5 | 1573 | } |
tnhnrl | 32:f2f8ae34aadc | 1574 | } |
tnhnrl | 32:f2f8ae34aadc | 1575 | |
tnhnrl | 32:f2f8ae34aadc | 1576 | void StateMachine::printCurrentSdLog() { |
tnhnrl | 32:f2f8ae34aadc | 1577 | pc().printf("SD card log work in progress\n\r"); |
tnhnrl | 32:f2f8ae34aadc | 1578 | //might be worth saving the last few logs to the MBED... |
tnhnrl | 32:f2f8ae34aadc | 1579 | } |
tnhnrl | 32:f2f8ae34aadc | 1580 | |
tnhnrl | 32:f2f8ae34aadc | 1581 | //check if the file is still opened |
tnhnrl | 32:f2f8ae34aadc | 1582 | void StateMachine::createNewFile() { |
tnhnrl | 32:f2f8ae34aadc | 1583 | if (_file_closed) { |
tnhnrl | 34:9b66c5188051 | 1584 | //mbedLogger().createFile(); //create a new MBED file |
tnhnrl | 32:f2f8ae34aadc | 1585 | |
tnhnrl | 32:f2f8ae34aadc | 1586 | _file_closed = false; //file is still open until you get to SIT_IDLE |
tnhnrl | 32:f2f8ae34aadc | 1587 | } |
tnhnrl | 32:f2f8ae34aadc | 1588 | } |
tnhnrl | 32:f2f8ae34aadc | 1589 | |
tnhnrl | 32:f2f8ae34aadc | 1590 | void StateMachine::transmitData() { |
tnhnrl | 32:f2f8ae34aadc | 1591 | static float transmit_timer = 0; |
tnhnrl | 32:f2f8ae34aadc | 1592 | static bool is_transmit_timer_running = false; |
tnhnrl | 32:f2f8ae34aadc | 1593 | |
tnhnrl | 32:f2f8ae34aadc | 1594 | if (!is_transmit_timer_running) { |
tnhnrl | 32:f2f8ae34aadc | 1595 | //pc().printf("\n\n\rTRANSMIT timer running...\n\n\r"); //debug |
tnhnrl | 32:f2f8ae34aadc | 1596 | |
tnhnrl | 32:f2f8ae34aadc | 1597 | transmit_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds |
tnhnrl | 32:f2f8ae34aadc | 1598 | is_transmit_timer_running = true; //disable this block after one iteration |
tnhnrl | 32:f2f8ae34aadc | 1599 | |
tnhnrl | 32:f2f8ae34aadc | 1600 | pc().printf("TESTING to see if this transmits once a second. (timer: %0.1f)\n\r", timer.read()); |
tnhnrl | 32:f2f8ae34aadc | 1601 | } |
tnhnrl | 32:f2f8ae34aadc | 1602 | if (timer.read() >= transmit_timer) { |
tnhnrl | 32:f2f8ae34aadc | 1603 | is_transmit_timer_running = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 32:f2f8ae34aadc | 1604 | } |
tnhnrl | 34:9b66c5188051 | 1605 | } |
tnhnrl | 34:9b66c5188051 | 1606 | |
tnhnrl | 34:9b66c5188051 | 1607 | void StateMachine::recordData(int input_state) { |
tnhnrl | 34:9b66c5188051 | 1608 | string string_state; |
tnhnrl | 34:9b66c5188051 | 1609 | if (input_state == SIT_IDLE) |
tnhnrl | 34:9b66c5188051 | 1610 | string_state = "SIT_IDLE"; |
tnhnrl | 34:9b66c5188051 | 1611 | else if (input_state == FIND_NEUTRAL) |
tnhnrl | 34:9b66c5188051 | 1612 | string_state = "FIND_NEUTRAL"; |
tnhnrl | 34:9b66c5188051 | 1613 | else if (input_state == DIVE) |
tnhnrl | 34:9b66c5188051 | 1614 | string_state = "DIVE"; |
tnhnrl | 34:9b66c5188051 | 1615 | else if (input_state == RISE) |
tnhnrl | 34:9b66c5188051 | 1616 | string_state = "RISE"; |
tnhnrl | 34:9b66c5188051 | 1617 | else if (input_state == FLOAT_LEVEL) |
tnhnrl | 34:9b66c5188051 | 1618 | string_state = "FLOAT_LEVEL"; |
tnhnrl | 34:9b66c5188051 | 1619 | else if (input_state == FLOAT_BROADCAST) |
tnhnrl | 34:9b66c5188051 | 1620 | string_state = "FLOAT_BROADCAST"; |
tnhnrl | 34:9b66c5188051 | 1621 | else if (input_state == EMERGENCY_CLIMB) |
tnhnrl | 34:9b66c5188051 | 1622 | string_state = "EMERGENCY_CLIMB"; |
tnhnrl | 34:9b66c5188051 | 1623 | else if (input_state == MULTI_DIVE) |
tnhnrl | 34:9b66c5188051 | 1624 | string_state = "MULTI_DIVE"; |
tnhnrl | 34:9b66c5188051 | 1625 | else if (input_state == MULTI_RISE) |
tnhnrl | 34:9b66c5188051 | 1626 | string_state = "MULTI_RISE"; |
tnhnrl | 34:9b66c5188051 | 1627 | else if (input_state == KEYBOARD) |
tnhnrl | 34:9b66c5188051 | 1628 | string_state = "KEYBOARD"; |
tnhnrl | 34:9b66c5188051 | 1629 | |
tnhnrl | 34:9b66c5188051 | 1630 | if (!_is_log_timer_running) { |
tnhnrl | 34:9b66c5188051 | 1631 | //pc().printf("\n\n\rlog timer running...\n\n\r"); //debug |
tnhnrl | 34:9b66c5188051 | 1632 | |
tnhnrl | 34:9b66c5188051 | 1633 | _log_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds |
tnhnrl | 34:9b66c5188051 | 1634 | _is_log_timer_running = true; //disable this block after one iteration |
tnhnrl | 34:9b66c5188051 | 1635 | |
tnhnrl | 34:9b66c5188051 | 1636 | _data_log[0] = timer.read(); //timer reading (shouldn't this be constant throughout the state?) |
tnhnrl | 34:9b66c5188051 | 1637 | _data_log[1] = depthLoop().getCommand(); //depth command |
tnhnrl | 34:9b66c5188051 | 1638 | _data_log[2] = depthLoop().getPosition(); //depth reading |
tnhnrl | 34:9b66c5188051 | 1639 | _data_log[3] = pitchLoop().getCommand(); //pitch command |
tnhnrl | 34:9b66c5188051 | 1640 | _data_log[4] = pitchLoop().getPosition(); //pitch reading |
tnhnrl | 34:9b66c5188051 | 1641 | _data_log[5] = bce().getSetPosition_mm(); |
tnhnrl | 34:9b66c5188051 | 1642 | _data_log[6] = bce().getPosition_mm(); |
tnhnrl | 34:9b66c5188051 | 1643 | _data_log[7] = batt().getSetPosition_mm(); |
tnhnrl | 34:9b66c5188051 | 1644 | _data_log[8] = batt().getPosition_mm(); |
tnhnrl | 34:9b66c5188051 | 1645 | |
tnhnrl | 34:9b66c5188051 | 1646 | //record data to the MBED every 5 seconds |
tnhnrl | 35:2f66ea4863d5 | 1647 | //mbedLogger().saveArrayToFile(string_state,input_state,_data_log); |
tnhnrl | 34:9b66c5188051 | 1648 | } |
tnhnrl | 34:9b66c5188051 | 1649 | if (timer.read() >= _log_timer) { |
tnhnrl | 34:9b66c5188051 | 1650 | _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 34:9b66c5188051 | 1651 | } |
tnhnrl | 34:9b66c5188051 | 1652 | } |
tnhnrl | 34:9b66c5188051 | 1653 | |
tnhnrl | 34:9b66c5188051 | 1654 | void StateMachine::recordState(int input_state) { |
tnhnrl | 34:9b66c5188051 | 1655 | string string_state; |
tnhnrl | 34:9b66c5188051 | 1656 | |
tnhnrl | 34:9b66c5188051 | 1657 | if (input_state == SIT_IDLE) |
tnhnrl | 34:9b66c5188051 | 1658 | string_state = "SIT_IDLE"; |
tnhnrl | 34:9b66c5188051 | 1659 | else if (input_state == FIND_NEUTRAL) |
tnhnrl | 34:9b66c5188051 | 1660 | string_state = "FIND_NEUTRAL"; |
tnhnrl | 34:9b66c5188051 | 1661 | else if (input_state == DIVE) |
tnhnrl | 34:9b66c5188051 | 1662 | string_state = "DIVE"; |
tnhnrl | 34:9b66c5188051 | 1663 | else if (input_state == RISE) |
tnhnrl | 34:9b66c5188051 | 1664 | string_state = "RISE"; |
tnhnrl | 34:9b66c5188051 | 1665 | else if (input_state == FLOAT_LEVEL) |
tnhnrl | 34:9b66c5188051 | 1666 | string_state = "FLOAT_LEVEL"; |
tnhnrl | 34:9b66c5188051 | 1667 | else if (input_state == FLOAT_BROADCAST) |
tnhnrl | 34:9b66c5188051 | 1668 | string_state = "FLOAT_BROADCAST"; |
tnhnrl | 34:9b66c5188051 | 1669 | else if (input_state == EMERGENCY_CLIMB) |
tnhnrl | 34:9b66c5188051 | 1670 | string_state = "EMERGENCY_CLIMB"; |
tnhnrl | 34:9b66c5188051 | 1671 | else if (input_state == MULTI_DIVE) |
tnhnrl | 34:9b66c5188051 | 1672 | string_state = "MULTI_DIVE"; |
tnhnrl | 34:9b66c5188051 | 1673 | else if (input_state == MULTI_RISE) |
tnhnrl | 34:9b66c5188051 | 1674 | string_state = "MULTI_RISE"; |
tnhnrl | 34:9b66c5188051 | 1675 | else if (input_state == KEYBOARD) |
tnhnrl | 34:9b66c5188051 | 1676 | string_state = "KEYBOARD"; |
tnhnrl | 34:9b66c5188051 | 1677 | //datalogger().printf("%s\n", string_state.c_str()); |
tnhnrl | 36:966a86937e17 | 1678 | } |
tnhnrl | 36:966a86937e17 | 1679 | |
tnhnrl | 36:966a86937e17 | 1680 | float * StateMachine::dataArray() { |
tnhnrl | 36:966a86937e17 | 1681 | //return the array to a calling function |
tnhnrl | 36:966a86937e17 | 1682 | return _data_log; |
tnhnrl | 16:3363b9f14913 | 1683 | } |