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