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

Committer:
danstrider
Date:
Sat Dec 02 20:36:29 2017 +0000
Revision:
29:6c030ac0c362
Parent:
28:16c83a2fdefa
Fixed bug in FIND_NEUTRAL that did not reset _isTimeoutRunning, so RISE wasn't getting its one-shot actions.; Also removed a dozen timer.reset(); that weren't needed.; Changed RISE to 0.0ft cmd and added offset to exit criteria to match with DIVE

Who changed what in which revision?

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