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@27:0a5b90cd65d6, 2017-11-29 (annotated)
- Committer:
- tnhnrl
- Date:
- Wed Nov 29 22:37:47 2017 +0000
- Revision:
- 27:0a5b90cd65d6
- Parent:
- 26:7e118fc02eea
- Child:
- 28:16c83a2fdefa
depthTolerance variable removed 11/29
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 | 24:c7d9b5bf3829 | 5 | _timeout = 480; // generic timeout for every state, seconds |
tnhnrl | 20:8987a9ae2bc7 | 6 | |
tnhnrl | 16:3363b9f14913 | 7 | pitchTolerance = 1.0; // pitch angle tolerance for neutral finding exit criteria |
tnhnrl | 20:8987a9ae2bc7 | 8 | |
tnhnrl | 24:c7d9b5bf3829 | 9 | bceFloatPosition = bce().getTravelLimit(); // bce position for "float" states (max travel limit for BCE is 320 mm) |
tnhnrl | 24:c7d9b5bf3829 | 10 | battFloatPosition = batt().getTravelLimit(); // batt position tail high for "broadcast" state (max travel limit for battery is 75 mm) |
tnhnrl | 20:8987a9ae2bc7 | 11 | |
tnhnrl | 24:c7d9b5bf3829 | 12 | depthCommand = 2.0; // user keyboard depth (default) |
tnhnrl | 24:c7d9b5bf3829 | 13 | pitchCommand = -20.0; // user keyboard pitch (default) |
tnhnrl | 17:7c16b5671d0e | 14 | |
tnhnrl | 24:c7d9b5bf3829 | 15 | _neutral_timer = 0; //timer used in FIND_NEUTRAL sub-FSM |
tnhnrl | 20:8987a9ae2bc7 | 16 | |
tnhnrl | 20:8987a9ae2bc7 | 17 | _state = SIT_IDLE; // select starting state here |
tnhnrl | 17:7c16b5671d0e | 18 | isTimeoutRunning = false; // default timer to not running |
tnhnrl | 20:8987a9ae2bc7 | 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 | 23:434f04ef1fad | 36 | _state_array_counter = 0; |
tnhnrl | 24:c7d9b5bf3829 | 37 | |
tnhnrl | 24:c7d9b5bf3829 | 38 | _substate = NEUTRAL_FIRST_PITCH; //to start sub-FSM |
tnhnrl | 24:c7d9b5bf3829 | 39 | _previous_substate = -1; //to start sub-FSM |
tnhnrl | 16:3363b9f14913 | 40 | } |
tnhnrl | 20:8987a9ae2bc7 | 41 | |
tnhnrl | 17:7c16b5671d0e | 42 | //Finite State Machine (FSM) |
tnhnrl | 24:c7d9b5bf3829 | 43 | void StateMachine::runStateMachine() { |
tnhnrl | 16:3363b9f14913 | 44 | // finite state machine ... each state has at least one exit criteria |
tnhnrl | 17:7c16b5671d0e | 45 | switch (_state) { |
tnhnrl | 16:3363b9f14913 | 46 | case SIT_IDLE : |
tnhnrl | 16:3363b9f14913 | 47 | // there actually is no timeout for SIT_IDLE, but this enables some one-shot actions |
tnhnrl | 16:3363b9f14913 | 48 | if (!isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 49 | showMenu(); |
tnhnrl | 16:3363b9f14913 | 50 | pc().printf("\r\n\nstate: SIT_IDLE\r\n"); |
tnhnrl | 17:7c16b5671d0e | 51 | isTimeoutRunning = true; |
tnhnrl | 20:8987a9ae2bc7 | 52 | |
tnhnrl | 16:3363b9f14913 | 53 | // what is active? |
tnhnrl | 16:3363b9f14913 | 54 | bce().pause(); |
tnhnrl | 16:3363b9f14913 | 55 | batt().pause(); |
tnhnrl | 17:7c16b5671d0e | 56 | |
tnhnrl | 17:7c16b5671d0e | 57 | //reset sub FSM |
tnhnrl | 20:8987a9ae2bc7 | 58 | isSubStateTimerRunning = false; |
tnhnrl | 16:3363b9f14913 | 59 | } |
tnhnrl | 20:8987a9ae2bc7 | 60 | |
tnhnrl | 16:3363b9f14913 | 61 | // how exit? |
tnhnrl | 20:8987a9ae2bc7 | 62 | keyboard(); // keyboard function will change the state if needed |
tnhnrl | 16:3363b9f14913 | 63 | break; |
tnhnrl | 20:8987a9ae2bc7 | 64 | |
tnhnrl | 16:3363b9f14913 | 65 | case EMERGENCY_CLIMB : |
tnhnrl | 16:3363b9f14913 | 66 | // start local state timer and init any other one-shot actions |
tnhnrl | 16:3363b9f14913 | 67 | if (!isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 68 | pc().printf("\r\n\nstate: EMERGENCY_CLIMB\r\n"); |
tnhnrl | 16:3363b9f14913 | 69 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 70 | timer.start(); // background timer starts running |
tnhnrl | 17:7c16b5671d0e | 71 | isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 72 | |
tnhnrl | 16:3363b9f14913 | 73 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 74 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 75 | batt().unpause(); |
tnhnrl | 20:8987a9ae2bc7 | 76 | |
tnhnrl | 20:8987a9ae2bc7 | 77 | // what are the commands? |
tnhnrl | 16:3363b9f14913 | 78 | bce().setPosition_mm(bce().getTravelLimit()); |
tnhnrl | 16:3363b9f14913 | 79 | batt().setPosition_mm(0.0); |
tnhnrl | 16:3363b9f14913 | 80 | } |
tnhnrl | 20:8987a9ae2bc7 | 81 | |
tnhnrl | 16:3363b9f14913 | 82 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 83 | if (timer > _timeout) { |
tnhnrl | 16:3363b9f14913 | 84 | pc().printf("EC: timed out\r\n"); |
tnhnrl | 21:38c8544db6f4 | 85 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 86 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 87 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 88 | } |
tnhnrl | 26:7e118fc02eea | 89 | else if (depthLoop().getPosition() < 2.0) { //if the depth is greater than 0.2 feet, go to float broadcast |
tnhnrl | 16:3363b9f14913 | 90 | pc().printf("EC: depth: %3.1f, cmd: 0.5 [%0.1f sec]\r",depthLoop().getPosition(), timer.read()); |
tnhnrl | 21:38c8544db6f4 | 91 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 92 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 93 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 94 | } |
tnhnrl | 16:3363b9f14913 | 95 | break; |
tnhnrl | 20:8987a9ae2bc7 | 96 | |
tnhnrl | 16:3363b9f14913 | 97 | case FIND_NEUTRAL : |
tnhnrl | 20:8987a9ae2bc7 | 98 | // start local state timer and init any other one-shot actions |
tnhnrl | 16:3363b9f14913 | 99 | if (!isTimeoutRunning) { |
tnhnrl | 21:38c8544db6f4 | 100 | pc().printf("\r\n\nstate: FIND_NEUTRAL\n\r"); |
tnhnrl | 16:3363b9f14913 | 101 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 102 | timer.start(); // background timer starts running |
tnhnrl | 20:8987a9ae2bc7 | 103 | isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 104 | |
tnhnrl | 16:3363b9f14913 | 105 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 106 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 107 | batt().unpause(); |
tnhnrl | 24:c7d9b5bf3829 | 108 | bce().setPosition_mm(bceFloatPosition); |
tnhnrl | 24:c7d9b5bf3829 | 109 | batt().setPosition_mm(_neutral_batt_pos_mm); //set battery to close-to-neutral setting from config file |
tnhnrl | 17:7c16b5671d0e | 110 | |
tnhnrl | 24:c7d9b5bf3829 | 111 | //first iteration goes into Neutral Finding Sub-FSM |
tnhnrl | 24:c7d9b5bf3829 | 112 | //set the first state of the FSM, and start the sub-FSM |
tnhnrl | 24:c7d9b5bf3829 | 113 | _substate = NEUTRAL_FIRST_PITCH; _previous_substate = -1; |
tnhnrl | 24:c7d9b5bf3829 | 114 | runNeutralStateMachine(); |
tnhnrl | 16:3363b9f14913 | 115 | } |
tnhnrl | 20:8987a9ae2bc7 | 116 | |
tnhnrl | 20:8987a9ae2bc7 | 117 | // how exit? (exit with the timer, if timer still running continue processing sub FSM) |
tnhnrl | 17:7c16b5671d0e | 118 | if (timer > _timeout) { |
tnhnrl | 17:7c16b5671d0e | 119 | pc().printf("FN: timed out [time: %0.1f sec]\r\n", timer.read()); |
tnhnrl | 21:38c8544db6f4 | 120 | _state = EMERGENCY_CLIMB; //new behavior (if this times out it emergency surfaces) |
tnhnrl | 16:3363b9f14913 | 121 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 122 | isTimeoutRunning = false; |
tnhnrl | 24:c7d9b5bf3829 | 123 | |
tnhnrl | 24:c7d9b5bf3829 | 124 | //record this to the NEUTRAL sub-FSM tracker |
tnhnrl | 24:c7d9b5bf3829 | 125 | _state_array[_state_array_counter] = EMERGENCY_CLIMB; //save to state array |
tnhnrl | 24:c7d9b5bf3829 | 126 | _state_array_counter++; |
tnhnrl | 16:3363b9f14913 | 127 | } |
tnhnrl | 21:38c8544db6f4 | 128 | |
tnhnrl | 24:c7d9b5bf3829 | 129 | //what is active? (neutral finding sub-function runs until completion) |
tnhnrl | 24:c7d9b5bf3829 | 130 | //check if substate returned exit state, if so stop running the sub-FSM |
tnhnrl | 26:7e118fc02eea | 131 | else if (runNeutralStateMachine() == NEUTRAL_EXIT) { |
tnhnrl | 21:38c8544db6f4 | 132 | //if successful, FIND_NEUTRAL then goes to RISE |
tnhnrl | 21:38c8544db6f4 | 133 | pc().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\n\n\r"); |
tnhnrl | 21:38c8544db6f4 | 134 | _state = RISE; |
tnhnrl | 16:3363b9f14913 | 135 | } |
tnhnrl | 17:7c16b5671d0e | 136 | break; |
tnhnrl | 17:7c16b5671d0e | 137 | |
tnhnrl | 16:3363b9f14913 | 138 | case DIVE : |
tnhnrl | 16:3363b9f14913 | 139 | // start local state timer and init any other one-shot actions |
tnhnrl | 16:3363b9f14913 | 140 | if (!isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 141 | pc().printf("\r\n\nstate: DIVE\r\n"); |
tnhnrl | 16:3363b9f14913 | 142 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 143 | timer.start(); // background timer starts running |
tnhnrl | 17:7c16b5671d0e | 144 | isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 145 | |
tnhnrl | 16:3363b9f14913 | 146 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 147 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 148 | batt().unpause(); |
tnhnrl | 20:8987a9ae2bc7 | 149 | |
tnhnrl | 16:3363b9f14913 | 150 | // what are the commands? |
tnhnrl | 16:3363b9f14913 | 151 | depthLoop().setCommand(depthCommand); |
tnhnrl | 16:3363b9f14913 | 152 | pitchLoop().setCommand(pitchCommand); |
tnhnrl | 16:3363b9f14913 | 153 | pc().printf("DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 154 | pc().printf("DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 155 | } |
tnhnrl | 20:8987a9ae2bc7 | 156 | |
tnhnrl | 16:3363b9f14913 | 157 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 158 | if (timer > _timeout) { |
tnhnrl | 17:7c16b5671d0e | 159 | pc().printf("DIVE: timed out\n\n\r"); |
tnhnrl | 21:38c8544db6f4 | 160 | _state = RISE; //new behavior 11/17/2017 |
tnhnrl | 16:3363b9f14913 | 161 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 162 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 163 | } |
tnhnrl | 16:3363b9f14913 | 164 | else if (depthLoop().getPosition() > depthLoop().getCommand()) { |
tnhnrl | 16:3363b9f14913 | 165 | pc().printf("DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); |
tnhnrl | 21:38c8544db6f4 | 166 | _state = RISE; |
tnhnrl | 16:3363b9f14913 | 167 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 168 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 169 | } |
tnhnrl | 20:8987a9ae2bc7 | 170 | |
tnhnrl | 16:3363b9f14913 | 171 | // what is active? |
tnhnrl | 21:38c8544db6f4 | 172 | 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 | 16:3363b9f14913 | 173 | bce().setPosition_mm(depthLoop().getOutput()); |
tnhnrl | 16:3363b9f14913 | 174 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 16:3363b9f14913 | 175 | break; |
tnhnrl | 16:3363b9f14913 | 176 | |
tnhnrl | 16:3363b9f14913 | 177 | case RISE : |
tnhnrl | 16:3363b9f14913 | 178 | // start local state timer and init any other one-shot actions |
tnhnrl | 16:3363b9f14913 | 179 | if (!isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 180 | pc().printf("\r\n\nstate: RISE\r\n"); |
tnhnrl | 16:3363b9f14913 | 181 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 182 | timer.start(); // background timer starts running |
tnhnrl | 17:7c16b5671d0e | 183 | isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 184 | |
tnhnrl | 16:3363b9f14913 | 185 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 186 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 187 | batt().unpause(); |
tnhnrl | 16:3363b9f14913 | 188 | |
tnhnrl | 16:3363b9f14913 | 189 | // what are the commands? |
tnhnrl | 26:7e118fc02eea | 190 | depthLoop().setCommand(0.5); |
tnhnrl | 16:3363b9f14913 | 191 | pitchLoop().setCommand(-pitchCommand); |
tnhnrl | 16:3363b9f14913 | 192 | pc().printf("RISE: depth cmd: 0.0\r\n"); |
tnhnrl | 16:3363b9f14913 | 193 | pc().printf("RISE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 194 | } |
tnhnrl | 20:8987a9ae2bc7 | 195 | |
tnhnrl | 16:3363b9f14913 | 196 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 197 | if (timer > _timeout) { |
tnhnrl | 16:3363b9f14913 | 198 | pc().printf("RISE: timed out\r\n"); |
tnhnrl | 21:38c8544db6f4 | 199 | _state = EMERGENCY_CLIMB; |
tnhnrl | 16:3363b9f14913 | 200 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 201 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 202 | } |
tnhnrl | 16:3363b9f14913 | 203 | else if (depthLoop().getPosition() < depthLoop().getCommand()) { |
tnhnrl | 16:3363b9f14913 | 204 | pc().printf("RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); |
tnhnrl | 21:38c8544db6f4 | 205 | _state = FLOAT_LEVEL; |
tnhnrl | 16:3363b9f14913 | 206 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 207 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 208 | } |
tnhnrl | 20:8987a9ae2bc7 | 209 | |
tnhnrl | 20:8987a9ae2bc7 | 210 | // what is active? |
tnhnrl | 16:3363b9f14913 | 211 | 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 | 212 | bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors |
tnhnrl | 17:7c16b5671d0e | 213 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 16:3363b9f14913 | 214 | break; |
tnhnrl | 16:3363b9f14913 | 215 | |
tnhnrl | 16:3363b9f14913 | 216 | case FLOAT_LEVEL : |
tnhnrl | 16:3363b9f14913 | 217 | // start local state timer and init any other one-shot actions |
tnhnrl | 16:3363b9f14913 | 218 | if (!isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 219 | pc().printf("\r\n\nstate: FLOAT_LEVEL\r\n"); |
tnhnrl | 16:3363b9f14913 | 220 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 221 | timer.start(); // background timer starts running |
tnhnrl | 17:7c16b5671d0e | 222 | isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 223 | |
tnhnrl | 16:3363b9f14913 | 224 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 225 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 226 | batt().unpause(); |
tnhnrl | 16:3363b9f14913 | 227 | |
tnhnrl | 20:8987a9ae2bc7 | 228 | // what are the commands? |
tnhnrl | 16:3363b9f14913 | 229 | bce().setPosition_mm(bceFloatPosition); |
tnhnrl | 16:3363b9f14913 | 230 | pitchLoop().setCommand(0.0); |
tnhnrl | 16:3363b9f14913 | 231 | } |
tnhnrl | 20:8987a9ae2bc7 | 232 | |
tnhnrl | 16:3363b9f14913 | 233 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 234 | if (timer > _timeout) { |
tnhnrl | 16:3363b9f14913 | 235 | pc().printf("FL: timed out\r\n"); |
tnhnrl | 21:38c8544db6f4 | 236 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 237 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 238 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 239 | } |
tnhnrl | 17:7c16b5671d0e | 240 | else if (fabs(imu().getPitch() - pitchLoop().getCommand()) < fabs(pitchTolerance)) { |
tnhnrl | 16:3363b9f14913 | 241 | 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 | 242 | _state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 243 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 244 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 245 | } |
tnhnrl | 20:8987a9ae2bc7 | 246 | |
tnhnrl | 16:3363b9f14913 | 247 | // what is active? |
tnhnrl | 16:3363b9f14913 | 248 | 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 | 249 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 16:3363b9f14913 | 250 | break; |
tnhnrl | 16:3363b9f14913 | 251 | |
tnhnrl | 16:3363b9f14913 | 252 | case FLOAT_BROADCAST : |
tnhnrl | 16:3363b9f14913 | 253 | // start local state timer and init any other one-shot actions |
tnhnrl | 16:3363b9f14913 | 254 | if (!isTimeoutRunning) { |
tnhnrl | 16:3363b9f14913 | 255 | pc().printf("\r\n\nstate: FLOAT_BROADCAST\r\n"); |
tnhnrl | 16:3363b9f14913 | 256 | timer.reset(); // timer goes back to zero |
tnhnrl | 16:3363b9f14913 | 257 | timer.start(); // background timer starts running |
tnhnrl | 17:7c16b5671d0e | 258 | isTimeoutRunning = true; |
tnhnrl | 16:3363b9f14913 | 259 | |
tnhnrl | 16:3363b9f14913 | 260 | // what needs to be started? |
tnhnrl | 16:3363b9f14913 | 261 | bce().unpause(); |
tnhnrl | 16:3363b9f14913 | 262 | batt().unpause(); |
tnhnrl | 20:8987a9ae2bc7 | 263 | |
tnhnrl | 16:3363b9f14913 | 264 | // what are the commands? |
tnhnrl | 16:3363b9f14913 | 265 | bce().setPosition_mm(bceFloatPosition); |
tnhnrl | 16:3363b9f14913 | 266 | batt().setPosition_mm(battFloatPosition); |
tnhnrl | 16:3363b9f14913 | 267 | } |
tnhnrl | 20:8987a9ae2bc7 | 268 | |
tnhnrl | 16:3363b9f14913 | 269 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 270 | if (timer > _timeout) { |
tnhnrl | 16:3363b9f14913 | 271 | pc().printf("FB: timed out\r\n"); |
tnhnrl | 21:38c8544db6f4 | 272 | _state = SIT_IDLE; |
tnhnrl | 16:3363b9f14913 | 273 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 274 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 275 | } |
tnhnrl | 20:8987a9ae2bc7 | 276 | else if ( (fabs(bce().getPosition_mm() - bce().getSetPosition_mm()) < bce().getDeadband()) and |
tnhnrl | 20:8987a9ae2bc7 | 277 | (fabs(batt().getPosition_mm() - batt().getSetPosition_mm()) < batt().getDeadband()) ) { |
tnhnrl | 16:3363b9f14913 | 278 | 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 | 279 | _state = SIT_IDLE; |
tnhnrl | 16:3363b9f14913 | 280 | timer.reset(); |
tnhnrl | 16:3363b9f14913 | 281 | isTimeoutRunning = false; |
tnhnrl | 16:3363b9f14913 | 282 | } |
tnhnrl | 20:8987a9ae2bc7 | 283 | |
tnhnrl | 20:8987a9ae2bc7 | 284 | // what is active? |
tnhnrl | 26:7e118fc02eea | 285 | pc().printf("FB: bce pos: %0.1f mm, batt pos: %0.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec] (setPos batt: %0.1f bce: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read(), bce().getSetPosition_mm(),batt().getSetPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 286 | break; |
tnhnrl | 17:7c16b5671d0e | 287 | |
tnhnrl | 17:7c16b5671d0e | 288 | case MULTI_DIVE : |
tnhnrl | 17:7c16b5671d0e | 289 | // start local state timer and init any other one-shot actions |
tnhnrl | 17:7c16b5671d0e | 290 | if (!isTimeoutRunning) { |
tnhnrl | 17:7c16b5671d0e | 291 | pc().printf("\r\n\nstate: MULTI-DIVE\r\n"); |
tnhnrl | 17:7c16b5671d0e | 292 | timer.reset(); // timer goes back to zero |
tnhnrl | 17:7c16b5671d0e | 293 | timer.start(); // background timer starts running |
tnhnrl | 17:7c16b5671d0e | 294 | isTimeoutRunning = true; |
tnhnrl | 17:7c16b5671d0e | 295 | |
tnhnrl | 17:7c16b5671d0e | 296 | // what needs to be started? |
tnhnrl | 17:7c16b5671d0e | 297 | bce().unpause(); |
tnhnrl | 17:7c16b5671d0e | 298 | batt().unpause(); |
tnhnrl | 17:7c16b5671d0e | 299 | |
tnhnrl | 21:38c8544db6f4 | 300 | //retrieve commands from structs (loaded from sequence.cfg file) |
tnhnrl | 17:7c16b5671d0e | 301 | float sequenceDepthCommand = currentStateStruct.depth; |
tnhnrl | 17:7c16b5671d0e | 302 | float sequencePitchCommand = currentStateStruct.pitch; |
tnhnrl | 17:7c16b5671d0e | 303 | |
tnhnrl | 17:7c16b5671d0e | 304 | // what are the commands? |
tnhnrl | 17:7c16b5671d0e | 305 | depthLoop().setCommand(sequenceDepthCommand); |
tnhnrl | 17:7c16b5671d0e | 306 | pitchLoop().setCommand(sequencePitchCommand); |
tnhnrl | 21:38c8544db6f4 | 307 | pc().printf("MULTI-DIVE: depth cmd: %3.1f ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand()); |
tnhnrl | 17:7c16b5671d0e | 308 | } |
tnhnrl | 20:8987a9ae2bc7 | 309 | |
tnhnrl | 17:7c16b5671d0e | 310 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 311 | if (timer > _timeout) { |
tnhnrl | 21:38c8544db6f4 | 312 | pc().printf("\n\n\rMULTI-DIVE: timed out [time: %0.1f]\n\n\r", timer.read()); |
tnhnrl | 21:38c8544db6f4 | 313 | _state = MULTI_RISE; //new behavior 11/17/2017 |
tnhnrl | 17:7c16b5671d0e | 314 | timer.reset(); |
tnhnrl | 17:7c16b5671d0e | 315 | isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 316 | } |
tnhnrl | 17:7c16b5671d0e | 317 | else if (depthLoop().getPosition() > depthLoop().getCommand()) { |
tnhnrl | 17:7c16b5671d0e | 318 | pc().printf("MULTI-DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); |
tnhnrl | 21:38c8544db6f4 | 319 | _state = MULTI_RISE; |
tnhnrl | 17:7c16b5671d0e | 320 | timer.reset(); |
tnhnrl | 17:7c16b5671d0e | 321 | isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 322 | } |
tnhnrl | 20:8987a9ae2bc7 | 323 | |
tnhnrl | 17:7c16b5671d0e | 324 | // what is active? |
tnhnrl | 17:7c16b5671d0e | 325 | 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 | 326 | bce().setPosition_mm(depthLoop().getOutput()); |
tnhnrl | 17:7c16b5671d0e | 327 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 17:7c16b5671d0e | 328 | break; |
tnhnrl | 17:7c16b5671d0e | 329 | |
tnhnrl | 17:7c16b5671d0e | 330 | case MULTI_RISE : |
tnhnrl | 17:7c16b5671d0e | 331 | // start local state timer and init any other one-shot actions |
tnhnrl | 17:7c16b5671d0e | 332 | if (!isTimeoutRunning) { |
tnhnrl | 17:7c16b5671d0e | 333 | pc().printf("\r\n\nstate: MULTI-RISE\r\n"); |
tnhnrl | 17:7c16b5671d0e | 334 | timer.reset(); // timer goes back to zero |
tnhnrl | 17:7c16b5671d0e | 335 | timer.start(); // background timer starts running |
tnhnrl | 17:7c16b5671d0e | 336 | isTimeoutRunning = true; |
tnhnrl | 17:7c16b5671d0e | 337 | |
tnhnrl | 17:7c16b5671d0e | 338 | // what needs to be started? |
tnhnrl | 17:7c16b5671d0e | 339 | bce().unpause(); |
tnhnrl | 17:7c16b5671d0e | 340 | batt().unpause(); |
tnhnrl | 17:7c16b5671d0e | 341 | |
tnhnrl | 17:7c16b5671d0e | 342 | //NEW: retrieve depth and pitch commands from config file struct |
tnhnrl | 17:7c16b5671d0e | 343 | // concept is to load this each time the multi-dive restarts |
tnhnrl | 17:7c16b5671d0e | 344 | stateMachine().getDiveSequence(); |
tnhnrl | 17:7c16b5671d0e | 345 | |
tnhnrl | 17:7c16b5671d0e | 346 | //retrieve just pitch command from struct |
tnhnrl | 17:7c16b5671d0e | 347 | float sequencePitchCommand = currentStateStruct.pitch; |
tnhnrl | 17:7c16b5671d0e | 348 | |
tnhnrl | 17:7c16b5671d0e | 349 | // what are the commands? (send back to 0.5 feet, not surface) // 11/21/2017 |
tnhnrl | 17:7c16b5671d0e | 350 | depthLoop().setCommand(0.5); |
tnhnrl | 21:38c8544db6f4 | 351 | pitchLoop().setCommand(-sequencePitchCommand); |
tnhnrl | 21:38c8544db6f4 | 352 | pc().printf("MULTI-RISE: depth cmd: 0.0 ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand()); |
tnhnrl | 17:7c16b5671d0e | 353 | } |
tnhnrl | 20:8987a9ae2bc7 | 354 | |
tnhnrl | 17:7c16b5671d0e | 355 | // how exit? |
tnhnrl | 17:7c16b5671d0e | 356 | if (timer > _timeout) { |
tnhnrl | 21:38c8544db6f4 | 357 | pc().printf("MULTI-RISE: timed out [time: %0.1f]\n\n\r", timer.read()); |
tnhnrl | 21:38c8544db6f4 | 358 | _state = EMERGENCY_CLIMB; |
tnhnrl | 17:7c16b5671d0e | 359 | timer.reset(); |
tnhnrl | 17:7c16b5671d0e | 360 | isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 361 | |
tnhnrl | 17:7c16b5671d0e | 362 | //reset multi-dive sequence to start |
tnhnrl | 24:c7d9b5bf3829 | 363 | _multi_dive_counter = 0; |
tnhnrl | 17:7c16b5671d0e | 364 | } |
tnhnrl | 20:8987a9ae2bc7 | 365 | else if (depthLoop().getPosition() < 0.5) { // depth is less than 0.5 (zero is surface level) |
tnhnrl | 17:7c16b5671d0e | 366 | pc().printf("MULTI-RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand()); |
tnhnrl | 17:7c16b5671d0e | 367 | |
tnhnrl | 17:7c16b5671d0e | 368 | //going to next state |
tnhnrl | 17:7c16b5671d0e | 369 | isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 370 | |
tnhnrl | 17:7c16b5671d0e | 371 | //successful dive-rise sequence CONTINUES the multi-dive sequence |
tnhnrl | 24:c7d9b5bf3829 | 372 | _multi_dive_counter++; |
tnhnrl | 17:7c16b5671d0e | 373 | |
tnhnrl | 17:7c16b5671d0e | 374 | //UPDATE THE SEQUENCE DATA HERE |
tnhnrl | 17:7c16b5671d0e | 375 | stateMachine().getDiveSequence(); |
tnhnrl | 17:7c16b5671d0e | 376 | |
tnhnrl | 17:7c16b5671d0e | 377 | //check if this is the end of the dive sequence |
tnhnrl | 17:7c16b5671d0e | 378 | //CHECK BEFORE ANYTHING ELSE that you have reached the "exit" state (Float_Level) |
tnhnrl | 17:7c16b5671d0e | 379 | if (currentStateStruct.state == FLOAT_LEVEL) { |
tnhnrl | 21:38c8544db6f4 | 380 | _state = FLOAT_LEVEL; |
tnhnrl | 17:7c16b5671d0e | 381 | return; |
tnhnrl | 17:7c16b5671d0e | 382 | } |
tnhnrl | 17:7c16b5671d0e | 383 | |
tnhnrl | 17:7c16b5671d0e | 384 | else |
tnhnrl | 21:38c8544db6f4 | 385 | _state = MULTI_DIVE; |
tnhnrl | 17:7c16b5671d0e | 386 | |
tnhnrl | 24:c7d9b5bf3829 | 387 | //have to stop this with the _multi_dive_counter variable! |
tnhnrl | 17:7c16b5671d0e | 388 | } |
tnhnrl | 20:8987a9ae2bc7 | 389 | |
tnhnrl | 20:8987a9ae2bc7 | 390 | // what is active? |
tnhnrl | 17:7c16b5671d0e | 391 | 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 | 392 | bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors |
tnhnrl | 17:7c16b5671d0e | 393 | batt().setPosition_mm(pitchLoop().getOutput()); |
tnhnrl | 17:7c16b5671d0e | 394 | break; |
tnhnrl | 16:3363b9f14913 | 395 | |
tnhnrl | 16:3363b9f14913 | 396 | default : |
tnhnrl | 17:7c16b5671d0e | 397 | pc().printf("DEBUG: SIT_IDLE\n\r"); |
tnhnrl | 21:38c8544db6f4 | 398 | _state = SIT_IDLE; |
tnhnrl | 17:7c16b5671d0e | 399 | } |
tnhnrl | 16:3363b9f14913 | 400 | } |
tnhnrl | 20:8987a9ae2bc7 | 401 | |
tnhnrl | 16:3363b9f14913 | 402 | // output the keyboard menu for user's reference |
tnhnrl | 16:3363b9f14913 | 403 | void StateMachine::showMenu() { |
tnhnrl | 16:3363b9f14913 | 404 | pc().printf("\r\r\n\nKEYBOARD MENU:\r\r\n"); |
tnhnrl | 16:3363b9f14913 | 405 | pc().printf(" N to find neutral\r\n"); |
tnhnrl | 17:7c16b5671d0e | 406 | pc().printf(" M to initiate multi-dive cycle\r\n"); |
tnhnrl | 16:3363b9f14913 | 407 | pc().printf(" D to initiate dive cycle\r\n"); |
tnhnrl | 16:3363b9f14913 | 408 | pc().printf(" R to initiate rise\r\n"); |
tnhnrl | 16:3363b9f14913 | 409 | pc().printf(" L to float level\r\n"); |
tnhnrl | 16:3363b9f14913 | 410 | pc().printf(" B to float at broadcast pitch\r\n"); |
tnhnrl | 16:3363b9f14913 | 411 | pc().printf(" E to initiate emergency climb\r\n"); |
tnhnrl | 16:3363b9f14913 | 412 | pc().printf(" H to run homing sequence on both BCE and Batt\r\n"); |
tnhnrl | 16:3363b9f14913 | 413 | pc().printf(" T to tare the depth sensor\r\n"); |
tnhnrl | 16:3363b9f14913 | 414 | pc().printf("[/] to change bce neutral position\r\n"); |
tnhnrl | 16:3363b9f14913 | 415 | pc().printf("</> to change batt neutral position\r\n"); |
tnhnrl | 16:3363b9f14913 | 416 | pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",pitchCommand); |
tnhnrl | 16:3363b9f14913 | 417 | pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",depthCommand); |
tnhnrl | 17:7c16b5671d0e | 418 | pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout); |
tnhnrl | 16:3363b9f14913 | 419 | pc().printf(" 1 BCE PID sub-menu\r\n"); |
tnhnrl | 16:3363b9f14913 | 420 | pc().printf(" 2 BATT PID sub-menu\r\n"); |
tnhnrl | 16:3363b9f14913 | 421 | pc().printf(" 3 Depth PID sub-menu\r\n"); |
tnhnrl | 16:3363b9f14913 | 422 | pc().printf(" 4 Pitch PID sub-menu\r\n"); |
tnhnrl | 16:3363b9f14913 | 423 | pc().printf(" C See sensor readings\r\n"); |
tnhnrl | 16:3363b9f14913 | 424 | pc().printf(" ? to reset mbed\r\n"); |
tnhnrl | 16:3363b9f14913 | 425 | } |
tnhnrl | 20:8987a9ae2bc7 | 426 | |
tnhnrl | 17:7c16b5671d0e | 427 | //Find Neutral sub finite state machine |
tnhnrl | 17:7c16b5671d0e | 428 | // Note: the sub-fsm only moves the pistons once at the start of each timer loop |
tnhnrl | 17:7c16b5671d0e | 429 | // (timer completes, move piston, timer completes, move piston, etc) |
tnhnrl | 24:c7d9b5bf3829 | 430 | int StateMachine::runNeutralStateMachine() { |
tnhnrl | 24:c7d9b5bf3829 | 431 | //used to log sub-FSM states |
tnhnrl | 24:c7d9b5bf3829 | 432 | if (_substate == NEUTRAL_FIRST_PITCH) { |
tnhnrl | 24:c7d9b5bf3829 | 433 | _state_array[_state_array_counter] = NEUTRAL_FIRST_PITCH; //save to state array |
tnhnrl | 24:c7d9b5bf3829 | 434 | _state_array_counter++; |
tnhnrl | 24:c7d9b5bf3829 | 435 | } |
tnhnrl | 17:7c16b5671d0e | 436 | |
tnhnrl | 24:c7d9b5bf3829 | 437 | switch (_substate) { |
tnhnrl | 20:8987a9ae2bc7 | 438 | case NEUTRAL_SINKING : |
tnhnrl | 17:7c16b5671d0e | 439 | //start the 10 second timer |
tnhnrl | 23:434f04ef1fad | 440 | if (!isSubStateTimerRunning) { |
tnhnrl | 24:c7d9b5bf3829 | 441 | _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds |
tnhnrl | 17:7c16b5671d0e | 442 | |
tnhnrl | 24:c7d9b5bf3829 | 443 | pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f]\n\r", _neutral_timer, timer.read()); |
tnhnrl | 20:8987a9ae2bc7 | 444 | |
tnhnrl | 20:8987a9ae2bc7 | 445 | // what are the commands? |
tnhnrl | 24:c7d9b5bf3829 | 446 | //move piston at start of sequence (retract 5 mm) |
tnhnrl | 24:c7d9b5bf3829 | 447 | bce().setPosition_mm(bce().getSetPosition_mm() - 5); //no depth command |
tnhnrl | 23:434f04ef1fad | 448 | |
tnhnrl | 23:434f04ef1fad | 449 | // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state |
tnhnrl | 23:434f04ef1fad | 450 | pitchLoop().setCommand(0.0); |
tnhnrl | 24:c7d9b5bf3829 | 451 | |
tnhnrl | 24:c7d9b5bf3829 | 452 | pc().printf("NEUTRAL_SINKING: Retracting piston 5 mm [BCE CMD : %0.1f] [pitch cmd: %0.1f]\n\r", bce().getSetPosition_mm(), pitchLoop().getCommand()); |
tnhnrl | 17:7c16b5671d0e | 453 | |
tnhnrl | 20:8987a9ae2bc7 | 454 | isSubStateTimerRunning = true; //disable this block after one iteration |
tnhnrl | 17:7c16b5671d0e | 455 | } |
tnhnrl | 20:8987a9ae2bc7 | 456 | |
tnhnrl | 20:8987a9ae2bc7 | 457 | // how exit? |
tnhnrl | 20:8987a9ae2bc7 | 458 | //once reached the travel limit, no need to keep trying, so exit |
tnhnrl | 25:249e4d56b27c | 459 | if (bce().getPosition_mm() <= 0) { |
tnhnrl | 25:249e4d56b27c | 460 | pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm()); |
tnhnrl | 25:249e4d56b27c | 461 | _substate = NEUTRAL_EXIT; |
tnhnrl | 25:249e4d56b27c | 462 | isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 25:249e4d56b27c | 463 | } |
tnhnrl | 20:8987a9ae2bc7 | 464 | //once deeper than the commanded setpoint... |
tnhnrl | 20:8987a9ae2bc7 | 465 | else if (depthLoop().getPosition() > depthCommand) { |
tnhnrl | 24:c7d9b5bf3829 | 466 | _substate = NEUTRAL_SLOWLY_RISE; // next state |
tnhnrl | 20:8987a9ae2bc7 | 467 | isSubStateTimerRunning = false; //reset the sub state timer |
tnhnrl | 20:8987a9ae2bc7 | 468 | } |
tnhnrl | 20:8987a9ae2bc7 | 469 | |
tnhnrl | 20:8987a9ae2bc7 | 470 | // what is active? |
tnhnrl | 20:8987a9ae2bc7 | 471 | //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint |
tnhnrl | 24:c7d9b5bf3829 | 472 | if (timer.read() >= _neutral_timer) { |
tnhnrl | 24:c7d9b5bf3829 | 473 | pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! Retracting BCE piston 5 mm [current time: %0.1f]\r\n", timer.read()); |
tnhnrl | 17:7c16b5671d0e | 474 | |
tnhnrl | 24:c7d9b5bf3829 | 475 | isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 17:7c16b5671d0e | 476 | } |
tnhnrl | 21:38c8544db6f4 | 477 | |
tnhnrl | 23:434f04ef1fad | 478 | // what is active? |
tnhnrl | 23:434f04ef1fad | 479 | batt().setPosition_mm(pitchLoop().getOutput()); // pitch outer loop is running |
tnhnrl | 17:7c16b5671d0e | 480 | break; |
tnhnrl | 17:7c16b5671d0e | 481 | |
tnhnrl | 17:7c16b5671d0e | 482 | case NEUTRAL_SLOWLY_RISE: |
tnhnrl | 23:434f04ef1fad | 483 | if (!isSubStateTimerRunning) { |
tnhnrl | 24:c7d9b5bf3829 | 484 | _neutral_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds |
tnhnrl | 17:7c16b5671d0e | 485 | |
tnhnrl | 24:c7d9b5bf3829 | 486 | 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 | 487 | |
tnhnrl | 20:8987a9ae2bc7 | 488 | // what are the commands? |
tnhnrl | 20:8987a9ae2bc7 | 489 | //move piston at start of sequence (extend) |
tnhnrl | 24:c7d9b5bf3829 | 490 | bce().setPosition_mm(bce().getSetPosition_mm() + 2); //no depth command |
tnhnrl | 23:434f04ef1fad | 491 | |
tnhnrl | 23:434f04ef1fad | 492 | // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state |
tnhnrl | 23:434f04ef1fad | 493 | pitchLoop().setCommand(0.0); |
tnhnrl | 24:c7d9b5bf3829 | 494 | |
tnhnrl | 24:c7d9b5bf3829 | 495 | pc().printf("NEUTRAL_SLOWLY_RISE: Extending BCE piston 5 mm [BCE CMD : %0.1f] [pitch cmd: %0.1f]\n\r", bce().getSetPosition_mm(), pitchLoop().getCommand()); |
tnhnrl | 24:c7d9b5bf3829 | 496 | |
tnhnrl | 24:c7d9b5bf3829 | 497 | isSubStateTimerRunning = true; //disable this block after one iteration |
tnhnrl | 17:7c16b5671d0e | 498 | } |
tnhnrl | 17:7c16b5671d0e | 499 | |
tnhnrl | 20:8987a9ae2bc7 | 500 | // how exit? |
tnhnrl | 24:c7d9b5bf3829 | 501 | //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit |
tnhnrl | 24:c7d9b5bf3829 | 502 | if (bce().getSetPosition_mm() >= bce().getTravelLimit()) { |
tnhnrl | 24:c7d9b5bf3829 | 503 | _substate = NEUTRAL_EXIT; |
tnhnrl | 24:c7d9b5bf3829 | 504 | isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 17:7c16b5671d0e | 505 | } |
tnhnrl | 17:7c16b5671d0e | 506 | //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration |
tnhnrl | 20:8987a9ae2bc7 | 507 | else if (depthLoop().getVelocity() < 0) { //less than zero ft/s |
tnhnrl | 24:c7d9b5bf3829 | 508 | pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 ft/s [time: %0.1f]\r\n", timer.read()); |
tnhnrl | 24:c7d9b5bf3829 | 509 | _substate = NEUTRAL_CHECK_PITCH; |
tnhnrl | 24:c7d9b5bf3829 | 510 | isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 17:7c16b5671d0e | 511 | } |
tnhnrl | 17:7c16b5671d0e | 512 | |
tnhnrl | 20:8987a9ae2bc7 | 513 | // what is active? |
tnhnrl | 20:8987a9ae2bc7 | 514 | //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint |
tnhnrl | 24:c7d9b5bf3829 | 515 | if (timer.read() >= _neutral_timer) { |
tnhnrl | 24:c7d9b5bf3829 | 516 | pc().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! Extending 1 mm [timer: %0.1f]\r\n", timer.read()); |
tnhnrl | 20:8987a9ae2bc7 | 517 | |
tnhnrl | 24:c7d9b5bf3829 | 518 | isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 17:7c16b5671d0e | 519 | } |
tnhnrl | 23:434f04ef1fad | 520 | |
tnhnrl | 23:434f04ef1fad | 521 | // what is active? |
tnhnrl | 23:434f04ef1fad | 522 | batt().setPosition_mm(pitchLoop().getOutput()); // pitch outer loop is running |
tnhnrl | 17:7c16b5671d0e | 523 | break; |
tnhnrl | 17:7c16b5671d0e | 524 | |
danstrider | 22:a10ee088403b | 525 | case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired |
danstrider | 22:a10ee088403b | 526 | case NEUTRAL_FIRST_PITCH : |
danstrider | 22:a10ee088403b | 527 | // start local state timer and init any other one-shot actions |
tnhnrl | 23:434f04ef1fad | 528 | |
tnhnrl | 23:434f04ef1fad | 529 | if (!isSubStateTimerRunning) { |
tnhnrl | 24:c7d9b5bf3829 | 530 | _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds |
tnhnrl | 24:c7d9b5bf3829 | 531 | pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read()); |
danstrider | 22:a10ee088403b | 532 | |
danstrider | 22:a10ee088403b | 533 | // what are the commands? |
tnhnrl | 24:c7d9b5bf3829 | 534 | if (pitchLoop().getPosition() > 2) { // nose is high |
tnhnrl | 24:c7d9b5bf3829 | 535 | batt().setPosition_mm(batt().getSetPosition_mm() + 0.5); // move battery forward (using setpoint from linear actuator) |
tnhnrl | 23:434f04ef1fad | 536 | pc().printf("\n\rNeutral Check Pitch: moving battery FWD in 1mm increments\n\n\r"); |
danstrider | 22:a10ee088403b | 537 | } |
tnhnrl | 24:c7d9b5bf3829 | 538 | else if (pitchLoop().getPosition() < -2) { // nose is low |
tnhnrl | 24:c7d9b5bf3829 | 539 | batt().setPosition_mm(batt().getSetPosition_mm() - 0.5); // move battery aft (using setpoint from linear actuator) |
tnhnrl | 23:434f04ef1fad | 540 | pc().printf("\n\rNeutral Check Pitch: moving battery AFT in 1mm increments\n\n\r"); |
danstrider | 22:a10ee088403b | 541 | } |
tnhnrl | 24:c7d9b5bf3829 | 542 | |
tnhnrl | 24:c7d9b5bf3829 | 543 | isSubStateTimerRunning = true; //disable this block after one iteration |
danstrider | 22:a10ee088403b | 544 | } |
tnhnrl | 20:8987a9ae2bc7 | 545 | |
tnhnrl | 20:8987a9ae2bc7 | 546 | // how exit? |
tnhnrl | 20:8987a9ae2bc7 | 547 | //pitch angle and pitch rate within small tolerance |
tnhnrl | 20:8987a9ae2bc7 | 548 | //benchtop tests confirm angle needs to be around 2 degrees |
tnhnrl | 23:434f04ef1fad | 549 | if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) { |
tnhnrl | 24:c7d9b5bf3829 | 550 | pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug |
danstrider | 22:a10ee088403b | 551 | // found level, but don't need to save anything this time |
tnhnrl | 23:434f04ef1fad | 552 | |
tnhnrl | 24:c7d9b5bf3829 | 553 | if (_substate == NEUTRAL_FIRST_PITCH) { |
tnhnrl | 24:c7d9b5bf3829 | 554 | _substate = NEUTRAL_SINKING; // next state starts the sinking |
tnhnrl | 24:c7d9b5bf3829 | 555 | isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 24:c7d9b5bf3829 | 556 | |
tnhnrl | 23:434f04ef1fad | 557 | // save this neutral (not in file) for pitch |
tnhnrl | 23:434f04ef1fad | 558 | pitchLoop().setOutputOffset(batt().getPosition_mm()); |
tnhnrl | 23:434f04ef1fad | 559 | |
tnhnrl | 24:c7d9b5bf3829 | 560 | pc().printf("substate: NEUTRAL_FIRST_PITCH (next substate: NEUTRAL_SINKING)\n\r"); |
danstrider | 22:a10ee088403b | 561 | } |
tnhnrl | 23:434f04ef1fad | 562 | |
tnhnrl | 23:434f04ef1fad | 563 | // found level and at depth too, so save it all now |
tnhnrl | 24:c7d9b5bf3829 | 564 | else if (_substate == NEUTRAL_CHECK_PITCH) { |
danstrider | 22:a10ee088403b | 565 | //save positions locally |
danstrider | 22:a10ee088403b | 566 | _neutral_batt_pos_mm = batt().getPosition_mm(); |
danstrider | 22:a10ee088403b | 567 | _neutral_bce_pos_mm = bce().getPosition_mm(); |
danstrider | 22:a10ee088403b | 568 | |
danstrider | 22:a10ee088403b | 569 | //set the neutral positions in each outer loop |
danstrider | 22:a10ee088403b | 570 | depthLoop().setOutputOffset(_neutral_bce_pos_mm); |
danstrider | 22:a10ee088403b | 571 | pitchLoop().setOutputOffset(_neutral_batt_pos_mm); |
danstrider | 22:a10ee088403b | 572 | |
danstrider | 22:a10ee088403b | 573 | // save into the depth.txt and pitch.txt files |
danstrider | 22:a10ee088403b | 574 | configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset |
danstrider | 22:a10ee088403b | 575 | configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset |
danstrider | 22:a10ee088403b | 576 | |
danstrider | 22:a10ee088403b | 577 | pc().printf("\n\rSaving Positions: BCE: %0.1f mm, BATT: %0.1f\n\n\r",_neutral_bce_pos_mm,_neutral_batt_pos_mm); |
danstrider | 22:a10ee088403b | 578 | |
tnhnrl | 24:c7d9b5bf3829 | 579 | _substate = NEUTRAL_EXIT; |
tnhnrl | 24:c7d9b5bf3829 | 580 | isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
tnhnrl | 24:c7d9b5bf3829 | 581 | } |
tnhnrl | 24:c7d9b5bf3829 | 582 | |
tnhnrl | 24:c7d9b5bf3829 | 583 | else { |
tnhnrl | 24:c7d9b5bf3829 | 584 | pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r"); |
tnhnrl | 24:c7d9b5bf3829 | 585 | _substate = NEUTRAL_EXIT; |
danstrider | 22:a10ee088403b | 586 | } |
tnhnrl | 17:7c16b5671d0e | 587 | } |
danstrider | 22:a10ee088403b | 588 | |
danstrider | 22:a10ee088403b | 589 | // what is active? |
danstrider | 22:a10ee088403b | 590 | //once timer complete, reset the timeout so the state one-shot entry will move the setpoint |
tnhnrl | 24:c7d9b5bf3829 | 591 | if (timer.read() >= _neutral_timer) { |
danstrider | 22:a10ee088403b | 592 | pc().printf("\r\n\nlevel timer COMPLETE!"); |
danstrider | 22:a10ee088403b | 593 | pc().printf("\r\n\n (BATT POS: %0.1f) moving 1 mm [timer: %0.1f]\r\n", batt().getPosition_mm(), timer.read()); |
tnhnrl | 24:c7d9b5bf3829 | 594 | isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again |
danstrider | 22:a10ee088403b | 595 | } |
tnhnrl | 17:7c16b5671d0e | 596 | break; |
danstrider | 22:a10ee088403b | 597 | |
danstrider | 22:a10ee088403b | 598 | //this state could be removed, it is only used as a transition but is needed to stop entering this function |
danstrider | 22:a10ee088403b | 599 | case NEUTRAL_EXIT : |
tnhnrl | 23:434f04ef1fad | 600 | pc().printf("substate: NEUTRAL_EXIT\n\r"); |
tnhnrl | 20:8987a9ae2bc7 | 601 | break; |
tnhnrl | 21:38c8544db6f4 | 602 | |
danstrider | 22:a10ee088403b | 603 | default : |
tnhnrl | 24:c7d9b5bf3829 | 604 | pc().printf("how did we get to substate: default?\n\r"); //debug |
tnhnrl | 23:434f04ef1fad | 605 | //a default within the sub-state machine |
tnhnrl | 24:c7d9b5bf3829 | 606 | _substate = NEUTRAL_EXIT; |
danstrider | 22:a10ee088403b | 607 | break; |
tnhnrl | 17:7c16b5671d0e | 608 | } |
tnhnrl | 20:8987a9ae2bc7 | 609 | |
tnhnrl | 24:c7d9b5bf3829 | 610 | // reset the sub-FSM if needed |
tnhnrl | 24:c7d9b5bf3829 | 611 | if (_substate == NEUTRAL_EXIT) { |
tnhnrl | 24:c7d9b5bf3829 | 612 | pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r"); |
tnhnrl | 24:c7d9b5bf3829 | 613 | |
tnhnrl | 24:c7d9b5bf3829 | 614 | //reset internal sub-state back to first entry conditions |
tnhnrl | 24:c7d9b5bf3829 | 615 | _substate = NEUTRAL_FIRST_PITCH; |
tnhnrl | 24:c7d9b5bf3829 | 616 | isSubStateTimerRunning = false; // reset the sub state timer |
tnhnrl | 21:38c8544db6f4 | 617 | |
tnhnrl | 24:c7d9b5bf3829 | 618 | //record sub-states to view after sequence |
tnhnrl | 24:c7d9b5bf3829 | 619 | _state_array[_state_array_counter] = NEUTRAL_EXIT; //save to state array |
tnhnrl | 24:c7d9b5bf3829 | 620 | _state_array_counter++; |
tnhnrl | 23:434f04ef1fad | 621 | |
tnhnrl | 24:c7d9b5bf3829 | 622 | //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit) |
tnhnrl | 24:c7d9b5bf3829 | 623 | _previous_substate = -1; |
tnhnrl | 24:c7d9b5bf3829 | 624 | |
tnhnrl | 24:c7d9b5bf3829 | 625 | //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed |
tnhnrl | 24:c7d9b5bf3829 | 626 | return NEUTRAL_EXIT; // message to calling function we just exited |
tnhnrl | 21:38c8544db6f4 | 627 | } |
tnhnrl | 23:434f04ef1fad | 628 | else { |
tnhnrl | 24:c7d9b5bf3829 | 629 | //record sub-states to view after sequence (when changed) |
tnhnrl | 24:c7d9b5bf3829 | 630 | if (_previous_substate != _substate) { |
tnhnrl | 24:c7d9b5bf3829 | 631 | _state_array[_state_array_counter] = _substate; //save current state to state array |
tnhnrl | 24:c7d9b5bf3829 | 632 | _state_array_counter++; |
tnhnrl | 24:c7d9b5bf3829 | 633 | |
tnhnrl | 24:c7d9b5bf3829 | 634 | //record the current substate for comparison |
tnhnrl | 24:c7d9b5bf3829 | 635 | _previous_substate = _substate; |
tnhnrl | 24:c7d9b5bf3829 | 636 | } |
tnhnrl | 24:c7d9b5bf3829 | 637 | |
tnhnrl | 24:c7d9b5bf3829 | 638 | return _substate; // message to calling function of what sub-state it's in |
tnhnrl | 23:434f04ef1fad | 639 | } |
tnhnrl | 17:7c16b5671d0e | 640 | } |
tnhnrl | 20:8987a9ae2bc7 | 641 | |
tnhnrl | 20:8987a9ae2bc7 | 642 | // keyboard runs independently of the state machine, handling one key at a time |
tnhnrl | 20:8987a9ae2bc7 | 643 | //keyboard updates the desired _keyboard_state that is used in the state machine |
tnhnrl | 20:8987a9ae2bc7 | 644 | //and only allows input when the state is "idle" |
tnhnrl | 17:7c16b5671d0e | 645 | void StateMachine::keyboard() { |
tnhnrl | 16:3363b9f14913 | 646 | char userInput; |
tnhnrl | 20:8987a9ae2bc7 | 647 | |
tnhnrl | 16:3363b9f14913 | 648 | // check keyboard and make settings changes as requested |
tnhnrl | 17:7c16b5671d0e | 649 | // states can be changed only at the start of a sequence (when the system is in SIT_IDLE) |
tnhnrl | 21:38c8544db6f4 | 650 | |
tnhnrl | 21:38c8544db6f4 | 651 | int _keyboard_state = -1; //made this a local variable because it was retaining the last keyboard state |
tnhnrl | 21:38c8544db6f4 | 652 | |
tnhnrl | 21:38c8544db6f4 | 653 | if (pc().readable() && _state == SIT_IDLE) { |
tnhnrl | 16:3363b9f14913 | 654 | // get the key |
tnhnrl | 17:7c16b5671d0e | 655 | userInput = pc().getc(); |
tnhnrl | 17:7c16b5671d0e | 656 | |
tnhnrl | 21:38c8544db6f4 | 657 | // keyboard has to reset timer each time it's used |
tnhnrl | 20:8987a9ae2bc7 | 658 | isTimeoutRunning = false; |
tnhnrl | 17:7c16b5671d0e | 659 | |
tnhnrl | 16:3363b9f14913 | 660 | // check command against desired control buttons |
tnhnrl | 16:3363b9f14913 | 661 | if (userInput == 'D' or userInput == 'd') { |
tnhnrl | 17:7c16b5671d0e | 662 | _keyboard_state = DIVE; |
tnhnrl | 16:3363b9f14913 | 663 | } |
tnhnrl | 16:3363b9f14913 | 664 | else if (userInput == 'N' or userInput == 'n') { |
tnhnrl | 17:7c16b5671d0e | 665 | _keyboard_state = FIND_NEUTRAL; |
tnhnrl | 17:7c16b5671d0e | 666 | } |
tnhnrl | 17:7c16b5671d0e | 667 | else if (userInput == 'M' or userInput == 'm') { |
tnhnrl | 17:7c16b5671d0e | 668 | //currently does not run if there is no file. |
tnhnrl | 17:7c16b5671d0e | 669 | |
tnhnrl | 17:7c16b5671d0e | 670 | //need to add method to Sequence Controller that returns -1 |
tnhnrl | 17:7c16b5671d0e | 671 | // or some check that insures you cannot run the dive sequence without a file |
tnhnrl | 17:7c16b5671d0e | 672 | |
tnhnrl | 17:7c16b5671d0e | 673 | stateMachine().getDiveSequence(); //get first sequence on keyboard press |
tnhnrl | 17:7c16b5671d0e | 674 | _keyboard_state = currentStateStruct.state; |
tnhnrl | 17:7c16b5671d0e | 675 | |
tnhnrl | 17:7c16b5671d0e | 676 | pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles |
tnhnrl | 16:3363b9f14913 | 677 | } |
tnhnrl | 16:3363b9f14913 | 678 | else if (userInput == 'R' or userInput == 'r') { |
tnhnrl | 17:7c16b5671d0e | 679 | _keyboard_state = RISE; |
tnhnrl | 16:3363b9f14913 | 680 | } |
tnhnrl | 16:3363b9f14913 | 681 | else if (userInput == 'L' or userInput == 'l') { |
tnhnrl | 17:7c16b5671d0e | 682 | _keyboard_state = FLOAT_LEVEL; |
tnhnrl | 16:3363b9f14913 | 683 | } |
tnhnrl | 16:3363b9f14913 | 684 | else if (userInput == 'B' or userInput == 'b') { |
tnhnrl | 17:7c16b5671d0e | 685 | _keyboard_state = FLOAT_BROADCAST; |
tnhnrl | 16:3363b9f14913 | 686 | } |
tnhnrl | 16:3363b9f14913 | 687 | else if (userInput == 'E' or userInput == 'e') { |
tnhnrl | 17:7c16b5671d0e | 688 | _keyboard_state = EMERGENCY_CLIMB; |
tnhnrl | 16:3363b9f14913 | 689 | } |
tnhnrl | 16:3363b9f14913 | 690 | else if (userInput == 'H' or userInput == 'h') { |
tnhnrl | 16:3363b9f14913 | 691 | pc().printf("running homing procedure\r\n"); |
tnhnrl | 16:3363b9f14913 | 692 | bce().unpause(); bce().homePiston(); bce().pause(); |
tnhnrl | 16:3363b9f14913 | 693 | batt().unpause(); batt().homePiston(); batt().pause(); |
tnhnrl | 16:3363b9f14913 | 694 | } |
tnhnrl | 16:3363b9f14913 | 695 | else if (userInput == 'T' or userInput == 't') { |
tnhnrl | 16:3363b9f14913 | 696 | pc().printf("taring depth sensor\r\n"); |
tnhnrl | 16:3363b9f14913 | 697 | pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt()); |
tnhnrl | 16:3363b9f14913 | 698 | wait(0.1); |
tnhnrl | 16:3363b9f14913 | 699 | depth().tare(); // tares to ambient (do on surface) |
tnhnrl | 16:3363b9f14913 | 700 | pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt()); |
tnhnrl | 16:3363b9f14913 | 701 | } |
tnhnrl | 16:3363b9f14913 | 702 | |
tnhnrl | 16:3363b9f14913 | 703 | else if (userInput == '[' or userInput == '{') { |
tnhnrl | 16:3363b9f14913 | 704 | depthLoop().setOutputOffset(depthLoop().getOutputOffset() - 1); // decrease the bce neutral setpoint |
tnhnrl | 16:3363b9f14913 | 705 | pc().printf("Adjusting bce neutral position. new: %3.1f\r\n",depthLoop().getOutputOffset()); |
tnhnrl | 16:3363b9f14913 | 706 | } |
tnhnrl | 16:3363b9f14913 | 707 | else if (userInput == ']' or userInput == '}') { |
tnhnrl | 16:3363b9f14913 | 708 | depthLoop().setOutputOffset(depthLoop().getOutputOffset() + 1); // increase the bce neutral setpoint |
tnhnrl | 16:3363b9f14913 | 709 | pc().printf("Adjusting bce neutral position. new: %3.1f\r\n",depthLoop().getOutputOffset()); |
tnhnrl | 16:3363b9f14913 | 710 | } |
tnhnrl | 16:3363b9f14913 | 711 | else if (userInput == '<' or userInput == ',') { |
tnhnrl | 16:3363b9f14913 | 712 | pitchLoop().setOutputOffset(pitchLoop().getOutputOffset() - 1); // decrease the batt neutral setpoint |
tnhnrl | 16:3363b9f14913 | 713 | pc().printf("Adjusting batt neutral position. new: %3.1f\r\n",pitchLoop().getOutputOffset()); |
tnhnrl | 16:3363b9f14913 | 714 | } |
tnhnrl | 16:3363b9f14913 | 715 | else if (userInput == '>' or userInput == '.') { |
tnhnrl | 16:3363b9f14913 | 716 | pitchLoop().setOutputOffset(pitchLoop().getOutputOffset() + 1); // increase the batt neutral setpoint |
tnhnrl | 16:3363b9f14913 | 717 | pc().printf("Adjusting batt neutral position. new: %3.1f\r\n",pitchLoop().getOutputOffset()); |
tnhnrl | 16:3363b9f14913 | 718 | } |
tnhnrl | 16:3363b9f14913 | 719 | |
tnhnrl | 16:3363b9f14913 | 720 | else if (userInput == '?') { |
tnhnrl | 16:3363b9f14913 | 721 | pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n"); |
tnhnrl | 16:3363b9f14913 | 722 | wait(0.5); |
tnhnrl | 16:3363b9f14913 | 723 | mbed_reset(); |
tnhnrl | 16:3363b9f14913 | 724 | } |
tnhnrl | 20:8987a9ae2bc7 | 725 | |
tnhnrl | 16:3363b9f14913 | 726 | // change settings |
tnhnrl | 16:3363b9f14913 | 727 | else if (userInput == 'Q' or userInput == 'q') { |
tnhnrl | 16:3363b9f14913 | 728 | pitchCommand -= 0.5; //decrement the pitch setpoint |
tnhnrl | 16:3363b9f14913 | 729 | pitchLoop().setCommand(pitchCommand); |
tnhnrl | 16:3363b9f14913 | 730 | pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 731 | } |
tnhnrl | 16:3363b9f14913 | 732 | else if (userInput == 'W' or userInput == 'w') { |
tnhnrl | 16:3363b9f14913 | 733 | pitchCommand += 0.5; //increment the pitch setpoint |
tnhnrl | 16:3363b9f14913 | 734 | pitchLoop().setCommand(pitchCommand); |
tnhnrl | 16:3363b9f14913 | 735 | pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 736 | } |
tnhnrl | 16:3363b9f14913 | 737 | else if (userInput == 'A' or userInput == 'a') { |
tnhnrl | 16:3363b9f14913 | 738 | depthCommand -= 0.5; //decrement the depth setpoint |
tnhnrl | 16:3363b9f14913 | 739 | depthLoop().setCommand(depthCommand); |
tnhnrl | 16:3363b9f14913 | 740 | pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 741 | } |
tnhnrl | 16:3363b9f14913 | 742 | else if (userInput == 'S' or userInput == 's') { |
tnhnrl | 16:3363b9f14913 | 743 | depthCommand += 0.5; //increment the depth setpoint |
tnhnrl | 16:3363b9f14913 | 744 | depthLoop().setCommand(depthCommand); |
tnhnrl | 16:3363b9f14913 | 745 | pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 746 | } |
tnhnrl | 20:8987a9ae2bc7 | 747 | |
tnhnrl | 16:3363b9f14913 | 748 | else if (userInput == '-') { |
tnhnrl | 17:7c16b5671d0e | 749 | _timeout -= 10.0; //decrement the timeout |
tnhnrl | 17:7c16b5671d0e | 750 | pc().printf(">>> timeout decreased: %d\r\n", _timeout); |
tnhnrl | 16:3363b9f14913 | 751 | } |
tnhnrl | 16:3363b9f14913 | 752 | else if (userInput == '=' or userInput == '+') { |
tnhnrl | 17:7c16b5671d0e | 753 | _timeout += 10.0; //increment the timeout |
tnhnrl | 17:7c16b5671d0e | 754 | pc().printf(">>> timeout increased: %d\r\n", _timeout); |
tnhnrl | 16:3363b9f14913 | 755 | } |
tnhnrl | 16:3363b9f14913 | 756 | |
tnhnrl | 16:3363b9f14913 | 757 | // go to sub-menus for the PID gains (this is blocking) |
tnhnrl | 16:3363b9f14913 | 758 | else if (userInput == '1') { |
tnhnrl | 16:3363b9f14913 | 759 | keyboard_menu_BCE_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 760 | } |
tnhnrl | 16:3363b9f14913 | 761 | else if (userInput == '2') { |
tnhnrl | 16:3363b9f14913 | 762 | keyboard_menu_BATT_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 763 | } |
tnhnrl | 16:3363b9f14913 | 764 | else if (userInput == '3') { |
tnhnrl | 16:3363b9f14913 | 765 | keyboard_menu_DEPTH_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 766 | } |
tnhnrl | 16:3363b9f14913 | 767 | else if (userInput == '4') { |
tnhnrl | 16:3363b9f14913 | 768 | keyboard_menu_PITCH_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 769 | } |
tnhnrl | 16:3363b9f14913 | 770 | |
tnhnrl | 16:3363b9f14913 | 771 | else if (userInput == 'C' or userInput == 'c') { |
tnhnrl | 16:3363b9f14913 | 772 | pc().printf("depth: %3.1f\r\n",depthLoop().getPosition()); |
tnhnrl | 16:3363b9f14913 | 773 | pc().printf("pitch: %3.1f\r\n",imu().getPitch()); |
tnhnrl | 16:3363b9f14913 | 774 | pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 775 | pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 776 | pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 777 | pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm()); |
tnhnrl | 16:3363b9f14913 | 778 | pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand()); |
tnhnrl | 16:3363b9f14913 | 779 | pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand()); |
tnhnrl | 23:434f04ef1fad | 780 | |
tnhnrl | 23:434f04ef1fad | 781 | pc().printf("Neutral sub FSM states: \n\r"); |
tnhnrl | 24:c7d9b5bf3829 | 782 | string string_state; |
tnhnrl | 24:c7d9b5bf3829 | 783 | |
tnhnrl | 23:434f04ef1fad | 784 | for (int i = 0; i < _state_array_counter; i++) { |
tnhnrl | 24:c7d9b5bf3829 | 785 | if (_state_array[i] == NEUTRAL_FIRST_PITCH) |
tnhnrl | 24:c7d9b5bf3829 | 786 | string_state = "NEUTRAL_FIRST_PITCH"; |
tnhnrl | 24:c7d9b5bf3829 | 787 | else if (_state_array[i] == NEUTRAL_SINKING) |
tnhnrl | 24:c7d9b5bf3829 | 788 | string_state = "NEUTRAL_SINKING"; |
tnhnrl | 24:c7d9b5bf3829 | 789 | else if (_state_array[i] == NEUTRAL_SLOWLY_RISE) |
tnhnrl | 24:c7d9b5bf3829 | 790 | string_state = "NEUTRAL_SLOWLY_RISE"; |
tnhnrl | 24:c7d9b5bf3829 | 791 | else if (_state_array[i] == NEUTRAL_CHECK_PITCH) |
tnhnrl | 24:c7d9b5bf3829 | 792 | string_state = "NEUTRAL_CHECK_PITCH"; |
tnhnrl | 24:c7d9b5bf3829 | 793 | else if (_state_array[i] == NEUTRAL_EXIT) |
tnhnrl | 24:c7d9b5bf3829 | 794 | string_state = "NEUTRAL_EXIT <-- "; |
tnhnrl | 24:c7d9b5bf3829 | 795 | else if (_state_array[i] == EMERGENCY_CLIMB) |
tnhnrl | 24:c7d9b5bf3829 | 796 | string_state = "EMERGENCY_CLIMB <-- "; |
tnhnrl | 24:c7d9b5bf3829 | 797 | pc().printf("state #%d: %d (%s)\n\r", i, _state_array[i], string_state.c_str()); |
tnhnrl | 23:434f04ef1fad | 798 | } |
tnhnrl | 16:3363b9f14913 | 799 | } |
tnhnrl | 17:7c16b5671d0e | 800 | |
tnhnrl | 17:7c16b5671d0e | 801 | //when you read the keyboard successfully, change the state |
tnhnrl | 26:7e118fc02eea | 802 | _state = _keyboard_state; //set state at the end of this function |
tnhnrl | 16:3363b9f14913 | 803 | } |
tnhnrl | 16:3363b9f14913 | 804 | } |
tnhnrl | 20:8987a9ae2bc7 | 805 | |
tnhnrl | 20:8987a9ae2bc7 | 806 | |
tnhnrl | 16:3363b9f14913 | 807 | void StateMachine::keyboard_menu_BCE_PID_settings() { |
tnhnrl | 16:3363b9f14913 | 808 | char PID_key; |
tnhnrl | 16:3363b9f14913 | 809 | float gain_step_size = 0.01; // modify this to change gain step size |
tnhnrl | 16:3363b9f14913 | 810 | float KP = bce().getControllerP(); // load current value |
tnhnrl | 16:3363b9f14913 | 811 | float KI = bce().getControllerI(); // load current global value |
tnhnrl | 16:3363b9f14913 | 812 | float KD = bce().getControllerD(); // load current global value |
tnhnrl | 16:3363b9f14913 | 813 | |
tnhnrl | 16:3363b9f14913 | 814 | // show the menu |
tnhnrl | 16:3363b9f14913 | 815 | pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)"); |
tnhnrl | 16:3363b9f14913 | 816 | pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); |
tnhnrl | 16:3363b9f14913 | 817 | pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r"); |
tnhnrl | 21:38c8544db6f4 | 818 | 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 | 819 | |
tnhnrl | 16:3363b9f14913 | 820 | // handle the key presses |
tnhnrl | 16:3363b9f14913 | 821 | while(1) { |
tnhnrl | 16:3363b9f14913 | 822 | // get the user's keystroke from either of the two inputs |
tnhnrl | 16:3363b9f14913 | 823 | if (pc().readable()) { |
tnhnrl | 16:3363b9f14913 | 824 | PID_key = pc().getc(); |
tnhnrl | 16:3363b9f14913 | 825 | } |
tnhnrl | 16:3363b9f14913 | 826 | else { |
tnhnrl | 16:3363b9f14913 | 827 | continue; // didn't get a user input, so keep waiting for it |
tnhnrl | 16:3363b9f14913 | 828 | } |
tnhnrl | 16:3363b9f14913 | 829 | |
tnhnrl | 16:3363b9f14913 | 830 | // handle the user's key input |
tnhnrl | 16:3363b9f14913 | 831 | if (PID_key == '-') { |
tnhnrl | 16:3363b9f14913 | 832 | KP -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 833 | pc().printf("P gain: %0.5f \r\n", KP); |
tnhnrl | 16:3363b9f14913 | 834 | } |
tnhnrl | 16:3363b9f14913 | 835 | else if (PID_key == '=') { |
tnhnrl | 16:3363b9f14913 | 836 | KP += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 837 | pc().printf("P gain: %0.5f \r\n", KP); |
tnhnrl | 16:3363b9f14913 | 838 | } |
tnhnrl | 16:3363b9f14913 | 839 | else if (PID_key == '[') { |
tnhnrl | 16:3363b9f14913 | 840 | KI -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 841 | pc().printf("I gain: %0.5f \r\n", KI); |
tnhnrl | 16:3363b9f14913 | 842 | } |
tnhnrl | 16:3363b9f14913 | 843 | else if (PID_key == ']') { |
tnhnrl | 16:3363b9f14913 | 844 | KI += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 845 | pc().printf("I gain: %0.5f \r\n", KI); |
tnhnrl | 16:3363b9f14913 | 846 | } |
tnhnrl | 16:3363b9f14913 | 847 | else if (PID_key == ';') { |
tnhnrl | 16:3363b9f14913 | 848 | KD -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 849 | pc().printf("D gain: %0.5f \r\n", KD); |
tnhnrl | 16:3363b9f14913 | 850 | } |
tnhnrl | 16:3363b9f14913 | 851 | else if (PID_key == '\'') { |
tnhnrl | 16:3363b9f14913 | 852 | KD += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 853 | pc().printf("D gain: %0.5f \r\n", KD); |
tnhnrl | 16:3363b9f14913 | 854 | } |
tnhnrl | 16:3363b9f14913 | 855 | else if (PID_key == 'S') { // user wants to save these modified values |
tnhnrl | 16:3363b9f14913 | 856 | // set values |
tnhnrl | 16:3363b9f14913 | 857 | bce().setControllerP(KP); |
tnhnrl | 16:3363b9f14913 | 858 | bce().setControllerI(KI); |
tnhnrl | 16:3363b9f14913 | 859 | bce().setControllerD(KD); |
tnhnrl | 16:3363b9f14913 | 860 | |
tnhnrl | 16:3363b9f14913 | 861 | // save into "PID.cfg" |
tnhnrl | 16:3363b9f14913 | 862 | //Config_File_IO().write_manual_position_PID_values_to_config(batt_position_P,batt_position_I,batt_position_D,bce_position_P,bce_position_I,bce_position_D); |
tnhnrl | 16:3363b9f14913 | 863 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 864 | } |
tnhnrl | 16:3363b9f14913 | 865 | else if (PID_key == 'X') { |
tnhnrl | 16:3363b9f14913 | 866 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 867 | } |
tnhnrl | 16:3363b9f14913 | 868 | else { |
tnhnrl | 16:3363b9f14913 | 869 | pc().printf("\n\rThis key does nothing here. "); |
tnhnrl | 16:3363b9f14913 | 870 | } |
tnhnrl | 16:3363b9f14913 | 871 | } |
tnhnrl | 16:3363b9f14913 | 872 | } |
tnhnrl | 20:8987a9ae2bc7 | 873 | |
tnhnrl | 16:3363b9f14913 | 874 | void StateMachine::keyboard_menu_BATT_PID_settings() { |
tnhnrl | 16:3363b9f14913 | 875 | char PID_key; |
tnhnrl | 16:3363b9f14913 | 876 | float gain_step_size = 0.01; // modify this to change gain step size |
tnhnrl | 16:3363b9f14913 | 877 | float KP = batt().getControllerP(); // load current global value |
tnhnrl | 16:3363b9f14913 | 878 | float KI = batt().getControllerI(); // load current global value |
tnhnrl | 16:3363b9f14913 | 879 | float KD = batt().getControllerD(); // load current global value |
tnhnrl | 16:3363b9f14913 | 880 | |
tnhnrl | 16:3363b9f14913 | 881 | // print the menu |
tnhnrl | 16:3363b9f14913 | 882 | pc().printf("\n\r2: Battery Motor PID gain settings (MENU)"); |
tnhnrl | 16:3363b9f14913 | 883 | pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); |
tnhnrl | 16:3363b9f14913 | 884 | pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); |
tnhnrl | 21:38c8544db6f4 | 885 | 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 | 886 | |
tnhnrl | 16:3363b9f14913 | 887 | // handle the key presses |
tnhnrl | 16:3363b9f14913 | 888 | while(1) { |
tnhnrl | 16:3363b9f14913 | 889 | // get the user's keystroke from either of the two inputs |
tnhnrl | 16:3363b9f14913 | 890 | if (pc().readable()) { |
tnhnrl | 16:3363b9f14913 | 891 | PID_key = pc().getc(); |
tnhnrl | 16:3363b9f14913 | 892 | } |
tnhnrl | 16:3363b9f14913 | 893 | else { |
tnhnrl | 16:3363b9f14913 | 894 | continue; // didn't get a user input, so keep waiting for it |
tnhnrl | 16:3363b9f14913 | 895 | } |
tnhnrl | 16:3363b9f14913 | 896 | |
tnhnrl | 16:3363b9f14913 | 897 | // handle the user's key input |
tnhnrl | 16:3363b9f14913 | 898 | if (PID_key == '-') { |
tnhnrl | 16:3363b9f14913 | 899 | KP -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 900 | pc().printf("\rP gain: %0.5f ", KP); |
tnhnrl | 16:3363b9f14913 | 901 | } |
tnhnrl | 16:3363b9f14913 | 902 | else if (PID_key == '=') { |
tnhnrl | 16:3363b9f14913 | 903 | KP += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 904 | pc().printf("\rP gain: %0.5f ", KP); |
tnhnrl | 16:3363b9f14913 | 905 | } |
tnhnrl | 16:3363b9f14913 | 906 | else if (PID_key == '[') { |
tnhnrl | 16:3363b9f14913 | 907 | KI -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 908 | pc().printf("\rI gain: %0.5f ", KI); |
tnhnrl | 16:3363b9f14913 | 909 | } |
tnhnrl | 16:3363b9f14913 | 910 | else if (PID_key == ']') { |
tnhnrl | 16:3363b9f14913 | 911 | KI += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 912 | pc().printf("\rI gain: %0.5f ", KI); |
tnhnrl | 16:3363b9f14913 | 913 | } |
tnhnrl | 16:3363b9f14913 | 914 | else if (PID_key == ';') { |
tnhnrl | 16:3363b9f14913 | 915 | KD -= gain_step_size; |
tnhnrl | 16:3363b9f14913 | 916 | pc().printf("\rD gain: %0.5f ", KD); |
tnhnrl | 16:3363b9f14913 | 917 | } |
tnhnrl | 16:3363b9f14913 | 918 | else if (PID_key == '\'') { |
tnhnrl | 16:3363b9f14913 | 919 | KD += gain_step_size; |
tnhnrl | 16:3363b9f14913 | 920 | pc().printf("\rD gain: %0.5f ", KD); |
tnhnrl | 16:3363b9f14913 | 921 | } |
tnhnrl | 16:3363b9f14913 | 922 | else if (PID_key == 'S') { // user wants to save the modified values |
tnhnrl | 16:3363b9f14913 | 923 | // set global values |
tnhnrl | 16:3363b9f14913 | 924 | batt().setControllerP(KP); |
tnhnrl | 16:3363b9f14913 | 925 | batt().setControllerI(KI); |
tnhnrl | 16:3363b9f14913 | 926 | batt().setControllerD(KD); |
tnhnrl | 16:3363b9f14913 | 927 | |
tnhnrl | 16:3363b9f14913 | 928 | // save to "PID.cfg" file |
tnhnrl | 16:3363b9f14913 | 929 | //Config_File_IO().write_manual_position_PID_values_to_config(batt_position_P,batt_position_I,batt_position_D,bce_position_P,bce_position_I,bce_position_D); |
tnhnrl | 16:3363b9f14913 | 930 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 931 | } |
tnhnrl | 16:3363b9f14913 | 932 | else if (PID_key == 'X') { |
tnhnrl | 16:3363b9f14913 | 933 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 934 | } |
tnhnrl | 16:3363b9f14913 | 935 | else { |
tnhnrl | 16:3363b9f14913 | 936 | pc().printf("This key does nothing here.\r"); |
tnhnrl | 16:3363b9f14913 | 937 | } |
tnhnrl | 16:3363b9f14913 | 938 | } |
tnhnrl | 16:3363b9f14913 | 939 | } |
tnhnrl | 20:8987a9ae2bc7 | 940 | |
tnhnrl | 16:3363b9f14913 | 941 | void StateMachine::keyboard_menu_DEPTH_PID_settings() { |
tnhnrl | 16:3363b9f14913 | 942 | char PID_key; |
tnhnrl | 16:3363b9f14913 | 943 | float gain_step_size = 0.01; // modify this to change gain step size |
tnhnrl | 16:3363b9f14913 | 944 | |
tnhnrl | 16:3363b9f14913 | 945 | // show the menu |
tnhnrl | 16:3363b9f14913 | 946 | pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)"); |
tnhnrl | 16:3363b9f14913 | 947 | pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); |
tnhnrl | 16:3363b9f14913 | 948 | pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r"); |
tnhnrl | 16:3363b9f14913 | 949 | 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 | 950 | |
tnhnrl | 16:3363b9f14913 | 951 | // handle the key presses |
tnhnrl | 16:3363b9f14913 | 952 | while(1) { |
tnhnrl | 16:3363b9f14913 | 953 | // get the user's keystroke from either of the two inputs |
tnhnrl | 16:3363b9f14913 | 954 | if (pc().readable()) { |
tnhnrl | 16:3363b9f14913 | 955 | PID_key = pc().getc(); |
tnhnrl | 16:3363b9f14913 | 956 | } |
tnhnrl | 16:3363b9f14913 | 957 | else { |
tnhnrl | 16:3363b9f14913 | 958 | continue; // didn't get a user input, so keep waiting for it |
tnhnrl | 16:3363b9f14913 | 959 | } |
tnhnrl | 16:3363b9f14913 | 960 | |
tnhnrl | 16:3363b9f14913 | 961 | // handle the user's key input |
tnhnrl | 16:3363b9f14913 | 962 | if (PID_key == '-') { |
tnhnrl | 21:38c8544db6f4 | 963 | _depth_KP -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 964 | pc().printf("P gain: %0.5f \r\n", _depth_KP); |
tnhnrl | 16:3363b9f14913 | 965 | } |
tnhnrl | 16:3363b9f14913 | 966 | else if (PID_key == '=') { |
tnhnrl | 21:38c8544db6f4 | 967 | _depth_KP += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 968 | pc().printf("P gain: %0.5f \r\n", _depth_KP); |
tnhnrl | 16:3363b9f14913 | 969 | } |
tnhnrl | 16:3363b9f14913 | 970 | else if (PID_key == '[') { |
tnhnrl | 21:38c8544db6f4 | 971 | _depth_KI -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 972 | pc().printf("I gain: %0.5f \r\n", _depth_KI); |
tnhnrl | 16:3363b9f14913 | 973 | } |
tnhnrl | 16:3363b9f14913 | 974 | else if (PID_key == ']') { |
tnhnrl | 21:38c8544db6f4 | 975 | _depth_KI += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 976 | pc().printf("I gain: %0.5f \r\n", _depth_KI); |
tnhnrl | 16:3363b9f14913 | 977 | } |
tnhnrl | 16:3363b9f14913 | 978 | else if (PID_key == ';') { |
tnhnrl | 21:38c8544db6f4 | 979 | _depth_KD -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 980 | pc().printf("D gain: %0.5f \r\n", _depth_KD); |
tnhnrl | 16:3363b9f14913 | 981 | } |
tnhnrl | 16:3363b9f14913 | 982 | else if (PID_key == '\'') { |
tnhnrl | 21:38c8544db6f4 | 983 | _depth_KD += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 984 | pc().printf("D gain: %0.5f \r\n", _depth_KD); |
tnhnrl | 16:3363b9f14913 | 985 | } |
tnhnrl | 16:3363b9f14913 | 986 | else if (PID_key == 'S') { // user wants to save these settings |
tnhnrl | 16:3363b9f14913 | 987 | // set global values |
tnhnrl | 21:38c8544db6f4 | 988 | depthLoop().setControllerP(_depth_KP); |
tnhnrl | 21:38c8544db6f4 | 989 | depthLoop().setControllerI(_depth_KI); |
tnhnrl | 21:38c8544db6f4 | 990 | depthLoop().setControllerD(_depth_KD); |
tnhnrl | 16:3363b9f14913 | 991 | |
tnhnrl | 21:38c8544db6f4 | 992 | // save depth PID values for outer loop |
tnhnrl | 21:38c8544db6f4 | 993 | configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); |
tnhnrl | 16:3363b9f14913 | 994 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 995 | } |
tnhnrl | 16:3363b9f14913 | 996 | else if (PID_key == 'X') { |
tnhnrl | 16:3363b9f14913 | 997 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 998 | } |
tnhnrl | 16:3363b9f14913 | 999 | else { |
tnhnrl | 16:3363b9f14913 | 1000 | pc().printf("\n\rThis key does nothing here. "); |
tnhnrl | 16:3363b9f14913 | 1001 | } |
tnhnrl | 16:3363b9f14913 | 1002 | } |
tnhnrl | 16:3363b9f14913 | 1003 | } |
tnhnrl | 16:3363b9f14913 | 1004 | |
tnhnrl | 16:3363b9f14913 | 1005 | void StateMachine::keyboard_menu_PITCH_PID_settings() { |
tnhnrl | 16:3363b9f14913 | 1006 | char PID_key; |
tnhnrl | 16:3363b9f14913 | 1007 | float gain_step_size = 0.01; // modify this to change gain step size |
tnhnrl | 16:3363b9f14913 | 1008 | |
tnhnrl | 16:3363b9f14913 | 1009 | // print the menu |
tnhnrl | 16:3363b9f14913 | 1010 | pc().printf("\n\r2: Battery Motor PID gain settings (MENU)"); |
tnhnrl | 16:3363b9f14913 | 1011 | pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); |
tnhnrl | 16:3363b9f14913 | 1012 | pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); |
tnhnrl | 16:3363b9f14913 | 1013 | 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 | 1014 | |
tnhnrl | 16:3363b9f14913 | 1015 | // handle the key presses |
tnhnrl | 16:3363b9f14913 | 1016 | while(1) { |
tnhnrl | 16:3363b9f14913 | 1017 | // get the user's keystroke from either of the two inputs |
tnhnrl | 16:3363b9f14913 | 1018 | if (pc().readable()) { |
tnhnrl | 16:3363b9f14913 | 1019 | PID_key = pc().getc(); |
tnhnrl | 16:3363b9f14913 | 1020 | } |
tnhnrl | 16:3363b9f14913 | 1021 | else { |
tnhnrl | 16:3363b9f14913 | 1022 | continue; // didn't get a user input, so keep waiting for it |
tnhnrl | 16:3363b9f14913 | 1023 | } |
tnhnrl | 16:3363b9f14913 | 1024 | |
tnhnrl | 16:3363b9f14913 | 1025 | // handle the user's key input |
tnhnrl | 16:3363b9f14913 | 1026 | if (PID_key == '-') { |
tnhnrl | 21:38c8544db6f4 | 1027 | _pitch_KP -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1028 | pc().printf("\rP gain: %0.5f ", _pitch_KP); |
tnhnrl | 16:3363b9f14913 | 1029 | } |
tnhnrl | 16:3363b9f14913 | 1030 | else if (PID_key == '=') { |
tnhnrl | 21:38c8544db6f4 | 1031 | _pitch_KP += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1032 | pc().printf("\rP gain: %0.5f ", _pitch_KP); |
tnhnrl | 16:3363b9f14913 | 1033 | } |
tnhnrl | 16:3363b9f14913 | 1034 | else if (PID_key == '[') { |
tnhnrl | 21:38c8544db6f4 | 1035 | _pitch_KI -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1036 | pc().printf("\rI gain: %0.5f ", _pitch_KI); |
tnhnrl | 16:3363b9f14913 | 1037 | } |
tnhnrl | 16:3363b9f14913 | 1038 | else if (PID_key == ']') { |
tnhnrl | 21:38c8544db6f4 | 1039 | _pitch_KI += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1040 | pc().printf("\rI gain: %0.5f ", _pitch_KI); |
tnhnrl | 16:3363b9f14913 | 1041 | } |
tnhnrl | 16:3363b9f14913 | 1042 | else if (PID_key == ';') { |
tnhnrl | 21:38c8544db6f4 | 1043 | _pitch_KD -= gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1044 | pc().printf("\rD gain: %0.5f ", _pitch_KD); |
tnhnrl | 16:3363b9f14913 | 1045 | } |
tnhnrl | 16:3363b9f14913 | 1046 | else if (PID_key == '\'') { |
tnhnrl | 21:38c8544db6f4 | 1047 | _pitch_KD += gain_step_size; |
tnhnrl | 21:38c8544db6f4 | 1048 | pc().printf("\rD gain: %0.5f ", _pitch_KD); |
tnhnrl | 16:3363b9f14913 | 1049 | } |
tnhnrl | 16:3363b9f14913 | 1050 | else if (PID_key == 'S') { // user wants to save the modified values |
tnhnrl | 16:3363b9f14913 | 1051 | // set global values |
tnhnrl | 21:38c8544db6f4 | 1052 | pitchLoop().setControllerP(_pitch_KP); |
tnhnrl | 21:38c8544db6f4 | 1053 | pitchLoop().setControllerI(_pitch_KI); |
tnhnrl | 21:38c8544db6f4 | 1054 | pitchLoop().setControllerD(_pitch_KD); |
tnhnrl | 16:3363b9f14913 | 1055 | |
tnhnrl | 21:38c8544db6f4 | 1056 | // save pitch PID values for outer loop |
tnhnrl | 21:38c8544db6f4 | 1057 | configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); |
tnhnrl | 16:3363b9f14913 | 1058 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1059 | } |
tnhnrl | 16:3363b9f14913 | 1060 | else if (PID_key == 'X') { |
tnhnrl | 16:3363b9f14913 | 1061 | break; //exit the while loop |
tnhnrl | 16:3363b9f14913 | 1062 | } |
tnhnrl | 16:3363b9f14913 | 1063 | else { |
tnhnrl | 16:3363b9f14913 | 1064 | pc().printf("This key does nothing here.\r"); |
tnhnrl | 16:3363b9f14913 | 1065 | } |
tnhnrl | 16:3363b9f14913 | 1066 | } |
tnhnrl | 16:3363b9f14913 | 1067 | } |
tnhnrl | 20:8987a9ae2bc7 | 1068 | |
tnhnrl | 16:3363b9f14913 | 1069 | float StateMachine::getDepthCommand() { |
tnhnrl | 16:3363b9f14913 | 1070 | return depthCommand; |
tnhnrl | 16:3363b9f14913 | 1071 | } |
tnhnrl | 20:8987a9ae2bc7 | 1072 | |
tnhnrl | 16:3363b9f14913 | 1073 | float StateMachine::getPitchCommand() { |
tnhnrl | 16:3363b9f14913 | 1074 | return pitchCommand; |
tnhnrl | 17:7c16b5671d0e | 1075 | } |
tnhnrl | 20:8987a9ae2bc7 | 1076 | |
tnhnrl | 17:7c16b5671d0e | 1077 | void StateMachine::setState(int input_state) { |
tnhnrl | 17:7c16b5671d0e | 1078 | //pc().printf("input_state: %d\n\r", input_state); //debug |
tnhnrl | 17:7c16b5671d0e | 1079 | //_state = input_state; //changing wrong variable |
tnhnrl | 21:38c8544db6f4 | 1080 | _state = input_state; |
tnhnrl | 17:7c16b5671d0e | 1081 | } |
tnhnrl | 20:8987a9ae2bc7 | 1082 | |
tnhnrl | 17:7c16b5671d0e | 1083 | int StateMachine::getState() { |
tnhnrl | 17:7c16b5671d0e | 1084 | return _state; //return the current state of the system |
tnhnrl | 17:7c16b5671d0e | 1085 | } |
tnhnrl | 20:8987a9ae2bc7 | 1086 | |
tnhnrl | 17:7c16b5671d0e | 1087 | void StateMachine::setTimeout(float input_timeout) { |
tnhnrl | 17:7c16b5671d0e | 1088 | _timeout = input_timeout; |
tnhnrl | 17:7c16b5671d0e | 1089 | } |
tnhnrl | 20:8987a9ae2bc7 | 1090 | |
tnhnrl | 17:7c16b5671d0e | 1091 | void StateMachine::setDepthCommand(float input_depth_command) { |
tnhnrl | 17:7c16b5671d0e | 1092 | depthCommand = input_depth_command; |
tnhnrl | 17:7c16b5671d0e | 1093 | } |
tnhnrl | 20:8987a9ae2bc7 | 1094 | |
tnhnrl | 17:7c16b5671d0e | 1095 | void StateMachine::setPitchCommand(float input_pitch_command) { |
tnhnrl | 17:7c16b5671d0e | 1096 | pitchCommand = input_pitch_command; |
tnhnrl | 17:7c16b5671d0e | 1097 | } |
tnhnrl | 20:8987a9ae2bc7 | 1098 | |
tnhnrl | 17:7c16b5671d0e | 1099 | void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) { |
tnhnrl | 21:38c8544db6f4 | 1100 | _neutral_batt_pos_mm = batt_pos_mm; |
tnhnrl | 21:38c8544db6f4 | 1101 | _neutral_bce_pos_mm = bce_pos_mm; |
tnhnrl | 17:7c16b5671d0e | 1102 | |
tnhnrl | 21:38c8544db6f4 | 1103 | pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm); |
tnhnrl | 17:7c16b5671d0e | 1104 | } |
tnhnrl | 20:8987a9ae2bc7 | 1105 | |
tnhnrl | 17:7c16b5671d0e | 1106 | int StateMachine::timeoutRunning() { |
tnhnrl | 17:7c16b5671d0e | 1107 | return isTimeoutRunning; |
tnhnrl | 17:7c16b5671d0e | 1108 | } |
tnhnrl | 20:8987a9ae2bc7 | 1109 | |
tnhnrl | 17:7c16b5671d0e | 1110 | //process one state at a time |
tnhnrl | 17:7c16b5671d0e | 1111 | void StateMachine::getDiveSequence() { |
tnhnrl | 17:7c16b5671d0e | 1112 | //iterate through this sequence using the FSM |
tnhnrl | 24:c7d9b5bf3829 | 1113 | currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state; |
tnhnrl | 24:c7d9b5bf3829 | 1114 | currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout; |
tnhnrl | 24:c7d9b5bf3829 | 1115 | currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth; |
tnhnrl | 24:c7d9b5bf3829 | 1116 | currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch; |
tnhnrl | 17:7c16b5671d0e | 1117 | |
tnhnrl | 17:7c16b5671d0e | 1118 | _timeout = currentStateStruct.timeout; //set timeout before exiting this function |
tnhnrl | 16:3363b9f14913 | 1119 | } |