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:
tnhnrl
Date:
Thu Dec 21 23:13:44 2017 +0000
Revision:
39:58375ca6b6ff
Parent:
38:83d06c294807
Child:
45:16b8162188ca
Pool-tested code with Mbed logging and OpenLog (SD card)

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