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