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