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 17:05:35 2017 +0000
Revision:
38:83d06c294807
Parent:
37:357e98a929cc
Child:
39:58375ca6b6ff
system time and BCE and Battery logger fixes

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 28:16c83a2fdefa 645 pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f)\n\r", _neutral_timer, timer.read(), pitchLoop().getPosition());
tnhnrl 20:8987a9ae2bc7 646
tnhnrl 32:f2f8ae34aadc 647 // what are the commands? (BCE linear actuator active, no pitch movement)
tnhnrl 32:f2f8ae34aadc 648 //move piston at start of sequence (default: retract 2.5 mm)
tnhnrl 32:f2f8ae34aadc 649 bce().setPosition_mm(bce().getSetPosition_mm() - _neutral_sink_command_mm); //no depth command
tnhnrl 23:434f04ef1fad 650
tnhnrl 32:f2f8ae34aadc 651 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 652
tnhnrl 28:16c83a2fdefa 653 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 654 }
tnhnrl 20:8987a9ae2bc7 655
tnhnrl 20:8987a9ae2bc7 656 // how exit?
tnhnrl 20:8987a9ae2bc7 657 //once reached the travel limit, no need to keep trying, so exit
tnhnrl 25:249e4d56b27c 658 if (bce().getPosition_mm() <= 0) {
tnhnrl 25:249e4d56b27c 659 pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm());
tnhnrl 25:249e4d56b27c 660 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 661 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 25:249e4d56b27c 662 }
tnhnrl 20:8987a9ae2bc7 663 //once deeper than the commanded setpoint...
tnhnrl 32:f2f8ae34aadc 664 else if (depthLoop().getPosition() > _depth_command) {
tnhnrl 24:c7d9b5bf3829 665 _substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 28:16c83a2fdefa 666 _isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 20:8987a9ae2bc7 667 }
tnhnrl 20:8987a9ae2bc7 668
tnhnrl 20:8987a9ae2bc7 669 // what is active?
tnhnrl 20:8987a9ae2bc7 670 //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 671 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 672 pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", timer.read());
tnhnrl 17:7c16b5671d0e 673
tnhnrl 28:16c83a2fdefa 674 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 675 }
tnhnrl 32:f2f8ae34aadc 676 // what is active? (only the buoyancy engine moved every 5 seconds)
tnhnrl 32:f2f8ae34aadc 677 pc().printf("depthLoop getOutput (position): %0.1f (current depth: %0.1f ft)\r", depthLoop().getOutput(), depthLoop().getPosition()); //debug
tnhnrl 32:f2f8ae34aadc 678 bce().setPosition_mm(depthLoop().getOutput()); // (DID NOT WORK ON BENCH)
tnhnrl 17:7c16b5671d0e 679 break;
tnhnrl 17:7c16b5671d0e 680
tnhnrl 17:7c16b5671d0e 681 case NEUTRAL_SLOWLY_RISE:
tnhnrl 28:16c83a2fdefa 682 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 683 _neutral_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 684
tnhnrl 24:c7d9b5bf3829 685 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 686
tnhnrl 20:8987a9ae2bc7 687 // what are the commands?
tnhnrl 32:f2f8ae34aadc 688 //move piston at start of sequence (default: extend 2.0 mm)
tnhnrl 32:f2f8ae34aadc 689 bce().setPosition_mm(bce().getSetPosition_mm() + _neutral_rise_command_mm); //no depth command
tnhnrl 23:434f04ef1fad 690
tnhnrl 23:434f04ef1fad 691 // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state
tnhnrl 23:434f04ef1fad 692 pitchLoop().setCommand(0.0);
tnhnrl 24:c7d9b5bf3829 693
tnhnrl 32:f2f8ae34aadc 694 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 695
tnhnrl 28:16c83a2fdefa 696 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 697 }
tnhnrl 17:7c16b5671d0e 698
tnhnrl 20:8987a9ae2bc7 699 // how exit?
tnhnrl 24:c7d9b5bf3829 700 //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit
tnhnrl 24:c7d9b5bf3829 701 if (bce().getSetPosition_mm() >= bce().getTravelLimit()) {
tnhnrl 24:c7d9b5bf3829 702 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 703 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 704 }
tnhnrl 17:7c16b5671d0e 705 //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration
tnhnrl 20:8987a9ae2bc7 706 else if (depthLoop().getVelocity() < 0) { //less than zero ft/s
tnhnrl 24:c7d9b5bf3829 707 pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 ft/s [time: %0.1f]\r\n", timer.read());
tnhnrl 24:c7d9b5bf3829 708 _substate = NEUTRAL_CHECK_PITCH;
tnhnrl 28:16c83a2fdefa 709 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 710 }
tnhnrl 17:7c16b5671d0e 711
tnhnrl 20:8987a9ae2bc7 712 // what is active?
tnhnrl 20:8987a9ae2bc7 713 //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 714 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 715 pc().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! [timer: %0.1f]\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 716
tnhnrl 28:16c83a2fdefa 717 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 718 }
tnhnrl 23:434f04ef1fad 719
tnhnrl 32:f2f8ae34aadc 720 // what is active? (only the buoyancy engine moved every 5 seconds)
tnhnrl 32:f2f8ae34aadc 721 pc().printf("depthLoop getOutput: %0.1f\r", depthLoop().getOutput()); //debug
tnhnrl 32:f2f8ae34aadc 722 bce().setPosition_mm(depthLoop().getOutput()); // (DID NOT WORK ON BENCH)
tnhnrl 17:7c16b5671d0e 723 break;
tnhnrl 17:7c16b5671d0e 724
danstrider 22:a10ee088403b 725 case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired
danstrider 22:a10ee088403b 726 // start local state timer and init any other one-shot actions
tnhnrl 23:434f04ef1fad 727
tnhnrl 28:16c83a2fdefa 728 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 729 _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds
tnhnrl 24:c7d9b5bf3829 730 pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read());
danstrider 22:a10ee088403b 731
tnhnrl 32:f2f8ae34aadc 732 // what are the commands? (default: retract or extend 0.5 mm)
tnhnrl 24:c7d9b5bf3829 733 if (pitchLoop().getPosition() > 2) { // nose is high
tnhnrl 32:f2f8ae34aadc 734 batt().setPosition_mm(batt().getSetPosition_mm() + _neutral_pitch_command_mm); // move battery forward (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 735 pc().printf("\n\rNeutral Check Pitch: moving battery FWD in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 736 }
tnhnrl 24:c7d9b5bf3829 737 else if (pitchLoop().getPosition() < -2) { // nose is low
tnhnrl 32:f2f8ae34aadc 738 batt().setPosition_mm(batt().getSetPosition_mm() - _neutral_pitch_command_mm); // move battery aft (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 739 pc().printf("\n\rNeutral Check Pitch: moving battery AFT in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 740 }
tnhnrl 24:c7d9b5bf3829 741
tnhnrl 28:16c83a2fdefa 742 _isSubStateTimerRunning = true; //disable this block after one iteration
danstrider 22:a10ee088403b 743 }
tnhnrl 20:8987a9ae2bc7 744
tnhnrl 28:16c83a2fdefa 745 // how exit?
tnhnrl 20:8987a9ae2bc7 746 //pitch angle and pitch rate within small tolerance
tnhnrl 20:8987a9ae2bc7 747 //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 23:434f04ef1fad 748 if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) {
tnhnrl 24:c7d9b5bf3829 749 pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug
danstrider 22:a10ee088403b 750 // found level, but don't need to save anything this time
tnhnrl 23:434f04ef1fad 751
tnhnrl 28:16c83a2fdefa 752 if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug
tnhnrl 28:16c83a2fdefa 753 _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 754 }
tnhnrl 28:16c83a2fdefa 755
tnhnrl 23:434f04ef1fad 756 // found level and at depth too, so save it all now
tnhnrl 32:f2f8ae34aadc 757 if (_substate == NEUTRAL_CHECK_PITCH) {
danstrider 22:a10ee088403b 758 //save positions locally
danstrider 22:a10ee088403b 759 _neutral_batt_pos_mm = batt().getPosition_mm();
danstrider 22:a10ee088403b 760 _neutral_bce_pos_mm = bce().getPosition_mm();
danstrider 22:a10ee088403b 761
danstrider 22:a10ee088403b 762 //set the neutral positions in each outer loop
danstrider 22:a10ee088403b 763 depthLoop().setOutputOffset(_neutral_bce_pos_mm);
danstrider 22:a10ee088403b 764 pitchLoop().setOutputOffset(_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 765
danstrider 22:a10ee088403b 766 // save into the depth.txt and pitch.txt files
danstrider 22:a10ee088403b 767 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset
danstrider 22:a10ee088403b 768 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset
danstrider 22:a10ee088403b 769
tnhnrl 32:f2f8ae34aadc 770 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 771
tnhnrl 24:c7d9b5bf3829 772 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 773 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 24:c7d9b5bf3829 774 }
tnhnrl 24:c7d9b5bf3829 775
tnhnrl 24:c7d9b5bf3829 776 else {
tnhnrl 24:c7d9b5bf3829 777 pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r");
tnhnrl 24:c7d9b5bf3829 778 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 779 }
tnhnrl 17:7c16b5671d0e 780 }
danstrider 22:a10ee088403b 781
danstrider 22:a10ee088403b 782 // what is active?
danstrider 22:a10ee088403b 783 //once timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 784 if (timer.read() >= _neutral_timer) {
danstrider 22:a10ee088403b 785 pc().printf("\r\n\nlevel timer COMPLETE!");
danstrider 22:a10ee088403b 786 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 787 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
danstrider 22:a10ee088403b 788 }
tnhnrl 17:7c16b5671d0e 789 break;
danstrider 22:a10ee088403b 790
danstrider 22:a10ee088403b 791 //this state could be removed, it is only used as a transition but is needed to stop entering this function
danstrider 22:a10ee088403b 792 case NEUTRAL_EXIT :
tnhnrl 23:434f04ef1fad 793 pc().printf("substate: NEUTRAL_EXIT\n\r");
tnhnrl 20:8987a9ae2bc7 794 break;
tnhnrl 21:38c8544db6f4 795
danstrider 22:a10ee088403b 796 default :
tnhnrl 24:c7d9b5bf3829 797 pc().printf("how did we get to substate: default?\n\r"); //debug
tnhnrl 23:434f04ef1fad 798 //a default within the sub-state machine
tnhnrl 24:c7d9b5bf3829 799 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 800 break;
tnhnrl 17:7c16b5671d0e 801 }
tnhnrl 20:8987a9ae2bc7 802
tnhnrl 30:2964617e7676 803 // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence)
tnhnrl 24:c7d9b5bf3829 804 if (_substate == NEUTRAL_EXIT) {
tnhnrl 24:c7d9b5bf3829 805 pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r");
tnhnrl 24:c7d9b5bf3829 806
tnhnrl 30:2964617e7676 807 //reset internal sub-state back to first entry conditions (first state is immediately sinking)
tnhnrl 30:2964617e7676 808 _substate = NEUTRAL_SINKING;
tnhnrl 28:16c83a2fdefa 809 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 21:38c8544db6f4 810
tnhnrl 24:c7d9b5bf3829 811 //record sub-states to view after sequence
tnhnrl 30:2964617e7676 812 _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array
tnhnrl 28:16c83a2fdefa 813 _substate_array_counter++;
tnhnrl 23:434f04ef1fad 814
tnhnrl 24:c7d9b5bf3829 815 //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit)
tnhnrl 24:c7d9b5bf3829 816 _previous_substate = -1;
tnhnrl 24:c7d9b5bf3829 817
tnhnrl 24:c7d9b5bf3829 818 //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 24:c7d9b5bf3829 819 return NEUTRAL_EXIT; // message to calling function we just exited
tnhnrl 21:38c8544db6f4 820 }
tnhnrl 23:434f04ef1fad 821 else {
tnhnrl 24:c7d9b5bf3829 822 //record sub-states to view after sequence (when changed)
tnhnrl 24:c7d9b5bf3829 823 if (_previous_substate != _substate) {
tnhnrl 28:16c83a2fdefa 824 _substate_array[_substate_array_counter] = _substate; //save current state to state array
tnhnrl 28:16c83a2fdefa 825 _substate_array_counter++;
tnhnrl 24:c7d9b5bf3829 826
tnhnrl 24:c7d9b5bf3829 827 //record the current substate for comparison
tnhnrl 24:c7d9b5bf3829 828 _previous_substate = _substate;
tnhnrl 24:c7d9b5bf3829 829 }
tnhnrl 24:c7d9b5bf3829 830
tnhnrl 24:c7d9b5bf3829 831 return _substate; // message to calling function of what sub-state it's in
tnhnrl 23:434f04ef1fad 832 }
tnhnrl 17:7c16b5671d0e 833 }
tnhnrl 32:f2f8ae34aadc 834
tnhnrl 32:f2f8ae34aadc 835 ////Find Neutral sub finite state machine
tnhnrl 32:f2f8ae34aadc 836 //// Note: the sub-fsm only moves the pistons once at the start of each timer loop
tnhnrl 32:f2f8ae34aadc 837 //// (timer completes, move piston, timer completes, move piston, etc)
tnhnrl 32:f2f8ae34aadc 838 //int StateMachine::runActiveNeutralStateMachine() {
tnhnrl 32:f2f8ae34aadc 839 // switch (_substate) {
tnhnrl 32:f2f8ae34aadc 840 // case NEUTRAL_AUTO_DEPTH :
tnhnrl 32:f2f8ae34aadc 841 // //start the 10 second timer
tnhnrl 32:f2f8ae34aadc 842 // if (!_isSubStateTimerRunning) {
tnhnrl 32:f2f8ae34aadc 843 // _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 32:f2f8ae34aadc 844 //
tnhnrl 32:f2f8ae34aadc 845 // pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f)\n\r", _neutral_timer, timer.read(), pitchLoop().getPosition());
tnhnrl 32:f2f8ae34aadc 846 //
tnhnrl 32:f2f8ae34aadc 847 // // what are the commands? (BCE linear actuator active, no pitch movement)
tnhnrl 32:f2f8ae34aadc 848 // //move piston at start of sequence (default: retract 2.5 mm)
tnhnrl 32:f2f8ae34aadc 849 // bce().setPosition_mm(bce().getSetPosition_mm() - _neutral_sink_command_mm); //no depth command
tnhnrl 32:f2f8ae34aadc 850 //
tnhnrl 32:f2f8ae34aadc 851 // 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 32:f2f8ae34aadc 852 //
tnhnrl 32:f2f8ae34aadc 853 // _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 32:f2f8ae34aadc 854 // }
tnhnrl 32:f2f8ae34aadc 855 //
tnhnrl 32:f2f8ae34aadc 856 // // how exit?
tnhnrl 32:f2f8ae34aadc 857 // //once reached the travel limit, no need to keep trying, so exit
tnhnrl 32:f2f8ae34aadc 858 // if (bce().getPosition_mm() <= 0) {
tnhnrl 32:f2f8ae34aadc 859 // pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm());
tnhnrl 32:f2f8ae34aadc 860 // _substate = NEUTRAL_EXIT;
tnhnrl 32:f2f8ae34aadc 861 // _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 32:f2f8ae34aadc 862 // }
tnhnrl 32:f2f8ae34aadc 863 // //once deeper than the commanded setpoint...
tnhnrl 32:f2f8ae34aadc 864 // else if (depthLoop().getPosition() > _depth_command) {
tnhnrl 32:f2f8ae34aadc 865 // _substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 32:f2f8ae34aadc 866 // _isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 32:f2f8ae34aadc 867 // }
tnhnrl 32:f2f8ae34aadc 868 //
tnhnrl 32:f2f8ae34aadc 869 // // what is active?
tnhnrl 32:f2f8ae34aadc 870 // //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 32:f2f8ae34aadc 871 // if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 872 // pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", timer.read());
tnhnrl 32:f2f8ae34aadc 873 //
tnhnrl 32:f2f8ae34aadc 874 // _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 32:f2f8ae34aadc 875 // }
tnhnrl 32:f2f8ae34aadc 876 // // what is active? (only the buoyancy engine moved every 5 seconds)
tnhnrl 32:f2f8ae34aadc 877 // break;
tnhnrl 32:f2f8ae34aadc 878 //
tnhnrl 32:f2f8ae34aadc 879 // case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired
tnhnrl 32:f2f8ae34aadc 880 // // start local state timer and init any other one-shot actions
tnhnrl 32:f2f8ae34aadc 881 //
tnhnrl 32:f2f8ae34aadc 882 // if (!_isSubStateTimerRunning) {
tnhnrl 32:f2f8ae34aadc 883 // _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds
tnhnrl 32:f2f8ae34aadc 884 // pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read());
tnhnrl 32:f2f8ae34aadc 885 //
tnhnrl 32:f2f8ae34aadc 886 // // what are the commands? (default: retract or extend 0.5 mm)
tnhnrl 32:f2f8ae34aadc 887 // if (pitchLoop().getPosition() > 2) { // nose is high
tnhnrl 32:f2f8ae34aadc 888 // batt().setPosition_mm(batt().getSetPosition_mm() + _neutral_pitch_command_mm); // move battery forward (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 889 // pc().printf("\n\rNeutral Check Pitch: moving battery FWD in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
tnhnrl 32:f2f8ae34aadc 890 // }
tnhnrl 32:f2f8ae34aadc 891 // else if (pitchLoop().getPosition() < -2) { // nose is low
tnhnrl 32:f2f8ae34aadc 892 // batt().setPosition_mm(batt().getSetPosition_mm() - _neutral_pitch_command_mm); // move battery aft (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 893 // pc().printf("\n\rNeutral Check Pitch: moving battery AFT in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
tnhnrl 32:f2f8ae34aadc 894 // }
tnhnrl 32:f2f8ae34aadc 895 //
tnhnrl 32:f2f8ae34aadc 896 // _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 32:f2f8ae34aadc 897 // }
tnhnrl 32:f2f8ae34aadc 898 //
tnhnrl 32:f2f8ae34aadc 899 // // how exit?
tnhnrl 32:f2f8ae34aadc 900 // //pitch angle and pitch rate within small tolerance
tnhnrl 32:f2f8ae34aadc 901 // //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 32:f2f8ae34aadc 902 // if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) {
tnhnrl 32:f2f8ae34aadc 903 // pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug
tnhnrl 32:f2f8ae34aadc 904 // // found level, but don't need to save anything this time
tnhnrl 32:f2f8ae34aadc 905 //
tnhnrl 32:f2f8ae34aadc 906 // if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug
tnhnrl 32:f2f8ae34aadc 907 // _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded
tnhnrl 32:f2f8ae34aadc 908 // }
tnhnrl 32:f2f8ae34aadc 909 //
tnhnrl 32:f2f8ae34aadc 910 // // found level and at depth too, so save it all now
tnhnrl 32:f2f8ae34aadc 911 // if (_substate == NEUTRAL_CHECK_PITCH) {
tnhnrl 32:f2f8ae34aadc 912 // //save positions locally
tnhnrl 32:f2f8ae34aadc 913 // _neutral_batt_pos_mm = batt().getPosition_mm();
tnhnrl 32:f2f8ae34aadc 914 // _neutral_bce_pos_mm = bce().getPosition_mm();
tnhnrl 32:f2f8ae34aadc 915 //
tnhnrl 32:f2f8ae34aadc 916 // //set the neutral positions in each outer loop
tnhnrl 32:f2f8ae34aadc 917 // depthLoop().setOutputOffset(_neutral_bce_pos_mm);
tnhnrl 32:f2f8ae34aadc 918 // pitchLoop().setOutputOffset(_neutral_batt_pos_mm);
tnhnrl 32:f2f8ae34aadc 919 //
tnhnrl 32:f2f8ae34aadc 920 // // save into the depth.txt and pitch.txt files
tnhnrl 32:f2f8ae34aadc 921 // configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset
tnhnrl 32:f2f8ae34aadc 922 // configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset
tnhnrl 32:f2f8ae34aadc 923 //
tnhnrl 32:f2f8ae34aadc 924 // pc().printf("\n\rSaving Positions: BCE: %0.1f mm, BATT: %0.1f\n\n\r",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
tnhnrl 32:f2f8ae34aadc 925 //
tnhnrl 32:f2f8ae34aadc 926 // _substate = NEUTRAL_EXIT;
tnhnrl 32:f2f8ae34aadc 927 // _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 32:f2f8ae34aadc 928 // }
tnhnrl 32:f2f8ae34aadc 929 //
tnhnrl 32:f2f8ae34aadc 930 // else {
tnhnrl 32:f2f8ae34aadc 931 // pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r");
tnhnrl 32:f2f8ae34aadc 932 // _substate = NEUTRAL_EXIT;
tnhnrl 32:f2f8ae34aadc 933 // }
tnhnrl 32:f2f8ae34aadc 934 // }
tnhnrl 32:f2f8ae34aadc 935 //
tnhnrl 32:f2f8ae34aadc 936 // // what is active?
tnhnrl 32:f2f8ae34aadc 937 // //once timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 32:f2f8ae34aadc 938 // if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 939 // pc().printf("\r\n\nlevel timer COMPLETE!");
tnhnrl 32:f2f8ae34aadc 940 // pc().printf("\r\n\n (BATT POS: %0.1f) moving 1 mm [timer: %0.1f]\r\n", batt().getPosition_mm(), timer.read());
tnhnrl 32:f2f8ae34aadc 941 // _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 32:f2f8ae34aadc 942 // }
tnhnrl 32:f2f8ae34aadc 943 // break;
tnhnrl 32:f2f8ae34aadc 944 //
tnhnrl 32:f2f8ae34aadc 945 // //this state could be removed, it is only used as a transition but is needed to stop entering this function
tnhnrl 32:f2f8ae34aadc 946 // case NEUTRAL_EXIT :
tnhnrl 32:f2f8ae34aadc 947 // pc().printf("substate: NEUTRAL_EXIT\n\r");
tnhnrl 32:f2f8ae34aadc 948 // break;
tnhnrl 32:f2f8ae34aadc 949 //
tnhnrl 32:f2f8ae34aadc 950 // default :
tnhnrl 32:f2f8ae34aadc 951 // pc().printf("how did we get to substate: default?\n\r"); //debug
tnhnrl 32:f2f8ae34aadc 952 // //a default within the sub-state machine
tnhnrl 32:f2f8ae34aadc 953 // _substate = NEUTRAL_EXIT;
tnhnrl 32:f2f8ae34aadc 954 // break;
tnhnrl 32:f2f8ae34aadc 955 // }
tnhnrl 32:f2f8ae34aadc 956 //
tnhnrl 32:f2f8ae34aadc 957 // // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence)
tnhnrl 32:f2f8ae34aadc 958 // if (_substate == NEUTRAL_EXIT) {
tnhnrl 32:f2f8ae34aadc 959 // pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r");
tnhnrl 32:f2f8ae34aadc 960 //
tnhnrl 32:f2f8ae34aadc 961 // //reset internal sub-state back to first entry conditions (first state is immediately sinking)
tnhnrl 32:f2f8ae34aadc 962 // _substate = NEUTRAL_SINKING;
tnhnrl 32:f2f8ae34aadc 963 // _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 32:f2f8ae34aadc 964 //
tnhnrl 32:f2f8ae34aadc 965 // //record sub-states to view after sequence
tnhnrl 32:f2f8ae34aadc 966 // _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array
tnhnrl 32:f2f8ae34aadc 967 // _substate_array_counter++;
tnhnrl 32:f2f8ae34aadc 968 //
tnhnrl 32:f2f8ae34aadc 969 // //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit)
tnhnrl 32:f2f8ae34aadc 970 // _previous_substate = -1;
tnhnrl 32:f2f8ae34aadc 971 //
tnhnrl 32:f2f8ae34aadc 972 // //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 32:f2f8ae34aadc 973 // return NEUTRAL_EXIT; // message to calling function we just exited
tnhnrl 32:f2f8ae34aadc 974 // }
tnhnrl 32:f2f8ae34aadc 975 // else {
tnhnrl 32:f2f8ae34aadc 976 // //record sub-states to view after sequence (when changed)
tnhnrl 32:f2f8ae34aadc 977 // if (_previous_substate != _substate) {
tnhnrl 32:f2f8ae34aadc 978 // _substate_array[_substate_array_counter] = _substate; //save current state to state array
tnhnrl 32:f2f8ae34aadc 979 // _substate_array_counter++;
tnhnrl 32:f2f8ae34aadc 980 //
tnhnrl 32:f2f8ae34aadc 981 // //record the current substate for comparison
tnhnrl 32:f2f8ae34aadc 982 // _previous_substate = _substate;
tnhnrl 32:f2f8ae34aadc 983 // }
tnhnrl 32:f2f8ae34aadc 984 //
tnhnrl 32:f2f8ae34aadc 985 // return _substate; // message to calling function of what sub-state it's in
tnhnrl 32:f2f8ae34aadc 986 // }
tnhnrl 32:f2f8ae34aadc 987 //}
tnhnrl 20:8987a9ae2bc7 988
tnhnrl 20:8987a9ae2bc7 989 // keyboard runs independently of the state machine, handling one key at a time
tnhnrl 20:8987a9ae2bc7 990 //keyboard updates the desired _keyboard_state that is used in the state machine
tnhnrl 20:8987a9ae2bc7 991 //and only allows input when the state is "idle"
tnhnrl 17:7c16b5671d0e 992 void StateMachine::keyboard() {
tnhnrl 16:3363b9f14913 993 char userInput;
tnhnrl 20:8987a9ae2bc7 994
tnhnrl 16:3363b9f14913 995 // check keyboard and make settings changes as requested
tnhnrl 17:7c16b5671d0e 996 // states can be changed only at the start of a sequence (when the system is in SIT_IDLE)
tnhnrl 21:38c8544db6f4 997
tnhnrl 37:357e98a929cc 998 //TEST
tnhnrl 37:357e98a929cc 999 int _keyboard_state = 0; //made this a local variable because it was retaining the last keyboard state
tnhnrl 21:38c8544db6f4 1000
tnhnrl 28:16c83a2fdefa 1001 if (pc().readable() && (_state == SIT_IDLE || _state == KEYBOARD)) {
tnhnrl 16:3363b9f14913 1002 // get the key
tnhnrl 17:7c16b5671d0e 1003 userInput = pc().getc();
tnhnrl 17:7c16b5671d0e 1004
tnhnrl 28:16c83a2fdefa 1005 //record that the keyboard was used
tnhnrl 28:16c83a2fdefa 1006 _state_array[_state_array_counter] = KEYBOARD;
tnhnrl 28:16c83a2fdefa 1007 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 1008
tnhnrl 21:38c8544db6f4 1009 // keyboard has to reset timer each time it's used
tnhnrl 28:16c83a2fdefa 1010 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 1011
tnhnrl 16:3363b9f14913 1012 // check command against desired control buttons
tnhnrl 16:3363b9f14913 1013 if (userInput == 'D' or userInput == 'd') {
tnhnrl 17:7c16b5671d0e 1014 _keyboard_state = DIVE;
tnhnrl 16:3363b9f14913 1015 }
tnhnrl 16:3363b9f14913 1016 else if (userInput == 'N' or userInput == 'n') {
tnhnrl 17:7c16b5671d0e 1017 _keyboard_state = FIND_NEUTRAL;
tnhnrl 17:7c16b5671d0e 1018 }
tnhnrl 17:7c16b5671d0e 1019 else if (userInput == 'M' or userInput == 'm') {
tnhnrl 17:7c16b5671d0e 1020 //currently does not run if there is no file.
tnhnrl 17:7c16b5671d0e 1021
tnhnrl 17:7c16b5671d0e 1022 //need to add method to Sequence Controller that returns -1
tnhnrl 17:7c16b5671d0e 1023 // or some check that insures you cannot run the dive sequence without a file
tnhnrl 17:7c16b5671d0e 1024
tnhnrl 17:7c16b5671d0e 1025 stateMachine().getDiveSequence(); //get first sequence on keyboard press
tnhnrl 17:7c16b5671d0e 1026 _keyboard_state = currentStateStruct.state;
tnhnrl 17:7c16b5671d0e 1027
tnhnrl 17:7c16b5671d0e 1028 pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles
tnhnrl 16:3363b9f14913 1029 }
tnhnrl 16:3363b9f14913 1030 else if (userInput == 'R' or userInput == 'r') {
tnhnrl 17:7c16b5671d0e 1031 _keyboard_state = RISE;
tnhnrl 16:3363b9f14913 1032 }
tnhnrl 16:3363b9f14913 1033 else if (userInput == 'L' or userInput == 'l') {
tnhnrl 17:7c16b5671d0e 1034 _keyboard_state = FLOAT_LEVEL;
tnhnrl 16:3363b9f14913 1035 }
tnhnrl 16:3363b9f14913 1036 else if (userInput == 'B' or userInput == 'b') {
tnhnrl 17:7c16b5671d0e 1037 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 1038 }
tnhnrl 16:3363b9f14913 1039 else if (userInput == 'E' or userInput == 'e') {
tnhnrl 17:7c16b5671d0e 1040 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 1041 }
tnhnrl 32:f2f8ae34aadc 1042 else if (userInput == 'P') {
tnhnrl 32:f2f8ae34aadc 1043 //Print current SD card log file
tnhnrl 36:966a86937e17 1044 //printCurrentSdLog();
tnhnrl 36:966a86937e17 1045 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 32:f2f8ae34aadc 1046 }
tnhnrl 32:f2f8ae34aadc 1047 else if (userInput == 'X') {
tnhnrl 32:f2f8ae34aadc 1048 printDirectory();
tnhnrl 34:9b66c5188051 1049 //mbedLogger().printDirectory(); //print all log files to the screen
tnhnrl 32:f2f8ae34aadc 1050 }
tnhnrl 34:9b66c5188051 1051 else if (userInput == 'V') {
tnhnrl 34:9b66c5188051 1052 _keyboard_state = TRANSMIT_DATA; //Transmit data (work in progress)
tnhnrl 32:f2f8ae34aadc 1053 }
tnhnrl 28:16c83a2fdefa 1054 // else if (userInput == 'H' or userInput == 'h') {
tnhnrl 28:16c83a2fdefa 1055 // pc().printf("running homing procedure\r\n");
tnhnrl 28:16c83a2fdefa 1056 // bce().unpause(); bce().homePiston(); bce().pause();
tnhnrl 28:16c83a2fdefa 1057 // batt().unpause(); batt().homePiston(); batt().pause();
tnhnrl 28:16c83a2fdefa 1058 // }
tnhnrl 28:16c83a2fdefa 1059 else if (userInput == 'z' or userInput == 'Z') {
tnhnrl 28:16c83a2fdefa 1060 pc().printf("FSG FSM States: \n\r");
tnhnrl 28:16c83a2fdefa 1061 string string_state;
tnhnrl 28:16c83a2fdefa 1062
tnhnrl 28:16c83a2fdefa 1063 for (int i = 0; i < _state_array_counter; i++) {
tnhnrl 28:16c83a2fdefa 1064 if (_state_array[i] == SIT_IDLE)
tnhnrl 28:16c83a2fdefa 1065 string_state = "SIT_IDLE <END>";
tnhnrl 28:16c83a2fdefa 1066 else if (_state_array[i] == FIND_NEUTRAL)
tnhnrl 28:16c83a2fdefa 1067 string_state = "FIND_NEUTRAL";
tnhnrl 28:16c83a2fdefa 1068 else if (_state_array[i] == DIVE)
tnhnrl 28:16c83a2fdefa 1069 string_state = "DIVE";
tnhnrl 28:16c83a2fdefa 1070 else if (_state_array[i] == RISE)
tnhnrl 28:16c83a2fdefa 1071 string_state = "RISE";
tnhnrl 28:16c83a2fdefa 1072 else if (_state_array[i] == FLOAT_LEVEL)
tnhnrl 28:16c83a2fdefa 1073 string_state = "FLOAT_LEVEL";
tnhnrl 28:16c83a2fdefa 1074 else if (_state_array[i] == FLOAT_BROADCAST)
tnhnrl 28:16c83a2fdefa 1075 string_state = "FLOAT_BROADCAST";
tnhnrl 28:16c83a2fdefa 1076 else if (_state_array[i] == EMERGENCY_CLIMB)
tnhnrl 28:16c83a2fdefa 1077 string_state = "EMERGENCY_CLIMB";
tnhnrl 28:16c83a2fdefa 1078 else if (_state_array[i] == MULTI_DIVE)
tnhnrl 28:16c83a2fdefa 1079 string_state = "MULTI_DIVE";
tnhnrl 28:16c83a2fdefa 1080 else if (_state_array[i] == MULTI_RISE)
tnhnrl 28:16c83a2fdefa 1081 string_state = "MULTI_RISE";
tnhnrl 28:16c83a2fdefa 1082 else if (_state_array[i] == KEYBOARD)
tnhnrl 28:16c83a2fdefa 1083 string_state = "KEYBOARD";
tnhnrl 28:16c83a2fdefa 1084 pc().printf("State #%d: %d (%s)\n\r", i, _state_array[i], string_state.c_str());
tnhnrl 28:16c83a2fdefa 1085 }
tnhnrl 28:16c83a2fdefa 1086
tnhnrl 28:16c83a2fdefa 1087 pc().printf("\n\rNeutral sub-FSM States: \n\r");
tnhnrl 28:16c83a2fdefa 1088 string string_substate;
tnhnrl 28:16c83a2fdefa 1089
tnhnrl 28:16c83a2fdefa 1090 for (int i = 0; i < _substate_array_counter; i++) {
tnhnrl 32:f2f8ae34aadc 1091 if (_substate_array[i] == NEUTRAL_SINKING)
tnhnrl 28:16c83a2fdefa 1092 string_substate = "NEUTRAL_SINKING";
tnhnrl 28:16c83a2fdefa 1093 else if (_substate_array[i] == NEUTRAL_SLOWLY_RISE)
tnhnrl 28:16c83a2fdefa 1094 string_substate = "NEUTRAL_SLOWLY_RISE";
tnhnrl 28:16c83a2fdefa 1095 else if (_substate_array[i] == NEUTRAL_CHECK_PITCH)
tnhnrl 28:16c83a2fdefa 1096 string_substate = "NEUTRAL_CHECK_PITCH";
tnhnrl 28:16c83a2fdefa 1097 else if (_substate_array[i] == NEUTRAL_EXIT)
tnhnrl 28:16c83a2fdefa 1098 string_substate = "NEUTRAL_EXIT <-- ";
tnhnrl 28:16c83a2fdefa 1099 else if (_substate_array[i] == EMERGENCY_CLIMB)
tnhnrl 28:16c83a2fdefa 1100 string_substate = " -- > EMERGENCY_CLIMB <-- ";
tnhnrl 28:16c83a2fdefa 1101 pc().printf("Neutral Substate #%d: %d (%s)\n\r", i, _state_array[i], string_substate.c_str());
tnhnrl 28:16c83a2fdefa 1102 }
tnhnrl 28:16c83a2fdefa 1103 pc().printf("\n\r"); //make space between printouts
tnhnrl 16:3363b9f14913 1104 }
tnhnrl 16:3363b9f14913 1105 else if (userInput == 'T' or userInput == 't') {
tnhnrl 16:3363b9f14913 1106 pc().printf("taring depth sensor\r\n");
tnhnrl 16:3363b9f14913 1107 pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 1108 wait(0.1);
tnhnrl 16:3363b9f14913 1109 depth().tare(); // tares to ambient (do on surface)
tnhnrl 16:3363b9f14913 1110 pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 1111 }
tnhnrl 16:3363b9f14913 1112
tnhnrl 16:3363b9f14913 1113 else if (userInput == '[' or userInput == '{') {
tnhnrl 32:f2f8ae34aadc 1114 _neutral_bce_pos_mm = depthLoop().getOutputOffset() - 1;
tnhnrl 32:f2f8ae34aadc 1115 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // decrease the bce neutral setpoint
tnhnrl 32:f2f8ae34aadc 1116 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1117 // save neutral depth value to config file
tnhnrl 32:f2f8ae34aadc 1118 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1119 }
tnhnrl 16:3363b9f14913 1120 else if (userInput == ']' or userInput == '}') {
tnhnrl 32:f2f8ae34aadc 1121 _neutral_bce_pos_mm = depthLoop().getOutputOffset() + 1;
tnhnrl 32:f2f8ae34aadc 1122 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // increase the bce neutral setpoint
tnhnrl 32:f2f8ae34aadc 1123 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1124 // save neutral depth value to config file
tnhnrl 32:f2f8ae34aadc 1125 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1126 }
tnhnrl 16:3363b9f14913 1127 else if (userInput == '<' or userInput == ',') {
tnhnrl 32:f2f8ae34aadc 1128 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() - 1;
tnhnrl 32:f2f8ae34aadc 1129 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // decrease the batt neutral setpoint
tnhnrl 32:f2f8ae34aadc 1130 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1131 // save neutral pitch value to config file
tnhnrl 32:f2f8ae34aadc 1132 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1133 }
tnhnrl 16:3363b9f14913 1134 else if (userInput == '>' or userInput == '.') {
tnhnrl 32:f2f8ae34aadc 1135 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() + 1;
tnhnrl 32:f2f8ae34aadc 1136 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // increase the batt neutral setpoint
tnhnrl 32:f2f8ae34aadc 1137 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1138 // save neutral pitch value to config file
tnhnrl 32:f2f8ae34aadc 1139 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1140 }
tnhnrl 16:3363b9f14913 1141
tnhnrl 16:3363b9f14913 1142 else if (userInput == '?') {
tnhnrl 16:3363b9f14913 1143 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 16:3363b9f14913 1144 wait(0.5);
tnhnrl 16:3363b9f14913 1145 mbed_reset();
tnhnrl 16:3363b9f14913 1146 }
tnhnrl 20:8987a9ae2bc7 1147
tnhnrl 16:3363b9f14913 1148 // change settings
tnhnrl 16:3363b9f14913 1149 else if (userInput == 'Q' or userInput == 'q') {
tnhnrl 32:f2f8ae34aadc 1150 _pitch_command -= 0.5; //decrement the pitch setpoint
tnhnrl 32:f2f8ae34aadc 1151 pitchLoop().setCommand(_pitch_command);
tnhnrl 16:3363b9f14913 1152 pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 1153 }
tnhnrl 16:3363b9f14913 1154 else if (userInput == 'W' or userInput == 'w') {
tnhnrl 32:f2f8ae34aadc 1155 _pitch_command += 0.5; //increment the pitch setpoint
tnhnrl 32:f2f8ae34aadc 1156 pitchLoop().setCommand(_pitch_command);
tnhnrl 16:3363b9f14913 1157 pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 1158 }
tnhnrl 16:3363b9f14913 1159 else if (userInput == 'A' or userInput == 'a') {
tnhnrl 32:f2f8ae34aadc 1160 _depth_command -= 0.5; //decrement the depth setpoint
tnhnrl 32:f2f8ae34aadc 1161 depthLoop().setCommand(_depth_command);
tnhnrl 16:3363b9f14913 1162 pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1163 }
tnhnrl 16:3363b9f14913 1164 else if (userInput == 'S' or userInput == 's') {
tnhnrl 32:f2f8ae34aadc 1165 _depth_command += 0.5; //increment the depth setpoint
tnhnrl 32:f2f8ae34aadc 1166 depthLoop().setCommand(_depth_command);
tnhnrl 16:3363b9f14913 1167 pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1168 }
tnhnrl 16:3363b9f14913 1169 else if (userInput == '-') {
tnhnrl 17:7c16b5671d0e 1170 _timeout -= 10.0; //decrement the timeout
tnhnrl 17:7c16b5671d0e 1171 pc().printf(">>> timeout decreased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 1172 }
tnhnrl 16:3363b9f14913 1173 else if (userInput == '=' or userInput == '+') {
tnhnrl 17:7c16b5671d0e 1174 _timeout += 10.0; //increment the timeout
tnhnrl 17:7c16b5671d0e 1175 pc().printf(">>> timeout increased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 1176 }
tnhnrl 16:3363b9f14913 1177
tnhnrl 16:3363b9f14913 1178 // go to sub-menus for the PID gains (this is blocking)
tnhnrl 16:3363b9f14913 1179 else if (userInput == '1') {
tnhnrl 16:3363b9f14913 1180 keyboard_menu_BCE_PID_settings();
tnhnrl 16:3363b9f14913 1181 }
tnhnrl 16:3363b9f14913 1182 else if (userInput == '2') {
tnhnrl 16:3363b9f14913 1183 keyboard_menu_BATT_PID_settings();
tnhnrl 16:3363b9f14913 1184 }
tnhnrl 16:3363b9f14913 1185 else if (userInput == '3') {
tnhnrl 16:3363b9f14913 1186 keyboard_menu_DEPTH_PID_settings();
tnhnrl 16:3363b9f14913 1187 }
tnhnrl 16:3363b9f14913 1188 else if (userInput == '4') {
tnhnrl 16:3363b9f14913 1189 keyboard_menu_PITCH_PID_settings();
tnhnrl 16:3363b9f14913 1190 }
tnhnrl 16:3363b9f14913 1191
tnhnrl 16:3363b9f14913 1192 else if (userInput == 'C' or userInput == 'c') {
tnhnrl 32:f2f8ae34aadc 1193
tnhnrl 32:f2f8ae34aadc 1194 pc().printf("\n\n\rCURRENT STATUS AND PARAMETERS:\n\r");
tnhnrl 32:f2f8ae34aadc 1195 pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 32:f2f8ae34aadc 1196 pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 16:3363b9f14913 1197 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 16:3363b9f14913 1198 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 16:3363b9f14913 1199 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 16:3363b9f14913 1200 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 16:3363b9f14913 1201 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1202 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 1203
tnhnrl 32:f2f8ae34aadc 1204 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 1205 pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 28:16c83a2fdefa 1206 pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1207 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 1208
tnhnrl 38:83d06c294807 1209 pc().printf("\n\r");
tnhnrl 38:83d06c294807 1210 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 1211 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 1212 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 1213 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 1214 }
tnhnrl 17:7c16b5671d0e 1215
tnhnrl 17:7c16b5671d0e 1216 //when you read the keyboard successfully, change the state
tnhnrl 28:16c83a2fdefa 1217 _state = _keyboard_state; //set state at the end of this function
tnhnrl 38:83d06c294807 1218 //pc().printf("\n\n\r ********* KEYBOARD STATE: %d *********\n\n\r", _state);
tnhnrl 16:3363b9f14913 1219 }
tnhnrl 16:3363b9f14913 1220 }
tnhnrl 20:8987a9ae2bc7 1221
tnhnrl 16:3363b9f14913 1222 void StateMachine::keyboard_menu_BCE_PID_settings() {
tnhnrl 16:3363b9f14913 1223 char PID_key;
tnhnrl 16:3363b9f14913 1224 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 38:83d06c294807 1225 float BCE_KP = bce().getControllerP(); // load current value
tnhnrl 38:83d06c294807 1226 float BCE_KI = bce().getControllerI(); // load current global value
tnhnrl 38:83d06c294807 1227 float BCE_KD = bce().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1228
tnhnrl 16:3363b9f14913 1229 // show the menu
tnhnrl 16:3363b9f14913 1230 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1231 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1232 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 21:38c8544db6f4 1233 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 1234
tnhnrl 16:3363b9f14913 1235 // handle the key presses
tnhnrl 16:3363b9f14913 1236 while(1) {
tnhnrl 16:3363b9f14913 1237 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1238 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1239 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1240 }
tnhnrl 16:3363b9f14913 1241 else {
tnhnrl 16:3363b9f14913 1242 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1243 }
tnhnrl 16:3363b9f14913 1244
tnhnrl 16:3363b9f14913 1245 // handle the user's key input
tnhnrl 16:3363b9f14913 1246 if (PID_key == '-') {
tnhnrl 38:83d06c294807 1247 BCE_KP -= gain_step_size;
tnhnrl 38:83d06c294807 1248 pc().printf("P gain: %0.5f \r\n", BCE_KP);
tnhnrl 16:3363b9f14913 1249 }
tnhnrl 16:3363b9f14913 1250 else if (PID_key == '=') {
tnhnrl 38:83d06c294807 1251 BCE_KP += gain_step_size;
tnhnrl 38:83d06c294807 1252 pc().printf("P gain: %0.5f \r\n", BCE_KP);
tnhnrl 16:3363b9f14913 1253 }
tnhnrl 16:3363b9f14913 1254 else if (PID_key == '[') {
tnhnrl 38:83d06c294807 1255 BCE_KI -= gain_step_size;
tnhnrl 38:83d06c294807 1256 pc().printf("I gain: %0.5f \r\n", BCE_KI);
tnhnrl 16:3363b9f14913 1257 }
tnhnrl 16:3363b9f14913 1258 else if (PID_key == ']') {
tnhnrl 38:83d06c294807 1259 BCE_KI += gain_step_size;
tnhnrl 38:83d06c294807 1260 pc().printf("I gain: %0.5f \r\n", BCE_KI);
tnhnrl 16:3363b9f14913 1261 }
tnhnrl 16:3363b9f14913 1262 else if (PID_key == ';') {
tnhnrl 38:83d06c294807 1263 BCE_KD -= gain_step_size;
tnhnrl 38:83d06c294807 1264 pc().printf("D gain: %0.5f \r\n", BCE_KD);
tnhnrl 16:3363b9f14913 1265 }
tnhnrl 16:3363b9f14913 1266 else if (PID_key == '\'') {
tnhnrl 38:83d06c294807 1267 BCE_KD += gain_step_size;
tnhnrl 38:83d06c294807 1268 pc().printf("D gain: %0.5f \r\n", BCE_KD);
tnhnrl 16:3363b9f14913 1269 }
tnhnrl 16:3363b9f14913 1270 else if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 16:3363b9f14913 1271 // set values
tnhnrl 38:83d06c294807 1272 bce().setControllerP(BCE_KP);
tnhnrl 38:83d06c294807 1273 bce().setControllerI(BCE_KI);
tnhnrl 38:83d06c294807 1274 bce().setControllerD(BCE_KD);
tnhnrl 16:3363b9f14913 1275
tnhnrl 38:83d06c294807 1276 // save to "BATT.TXT" file
tnhnrl 38:83d06c294807 1277 configFileIO().saveBCEData(BCE_KP, BCE_KI, BCE_KD);
tnhnrl 38:83d06c294807 1278
tnhnrl 16:3363b9f14913 1279 break; //exit the while loop
tnhnrl 16:3363b9f14913 1280 }
tnhnrl 16:3363b9f14913 1281 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1282 break; //exit the while loop
tnhnrl 16:3363b9f14913 1283 }
tnhnrl 16:3363b9f14913 1284 else {
tnhnrl 16:3363b9f14913 1285 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1286 }
tnhnrl 16:3363b9f14913 1287 }
tnhnrl 16:3363b9f14913 1288 }
tnhnrl 20:8987a9ae2bc7 1289
tnhnrl 16:3363b9f14913 1290 void StateMachine::keyboard_menu_BATT_PID_settings() {
tnhnrl 16:3363b9f14913 1291 char PID_key;
tnhnrl 16:3363b9f14913 1292 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 38:83d06c294807 1293 float batt_KP = batt().getControllerP(); // load current global value
tnhnrl 38:83d06c294807 1294 float batt_KI = batt().getControllerI(); // load current global value
tnhnrl 38:83d06c294807 1295 float batt_KD = batt().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1296
tnhnrl 16:3363b9f14913 1297 // print the menu
tnhnrl 16:3363b9f14913 1298 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1299 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1300 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 21:38c8544db6f4 1301 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 1302
tnhnrl 16:3363b9f14913 1303 // handle the key presses
tnhnrl 16:3363b9f14913 1304 while(1) {
tnhnrl 16:3363b9f14913 1305 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1306 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1307 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1308 }
tnhnrl 16:3363b9f14913 1309 else {
tnhnrl 16:3363b9f14913 1310 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1311 }
tnhnrl 16:3363b9f14913 1312
tnhnrl 16:3363b9f14913 1313 // handle the user's key input
tnhnrl 16:3363b9f14913 1314 if (PID_key == '-') {
tnhnrl 38:83d06c294807 1315 batt_KP -= gain_step_size;
tnhnrl 38:83d06c294807 1316 pc().printf("\rP gain: %0.5f ", batt_KP);
tnhnrl 16:3363b9f14913 1317 }
tnhnrl 16:3363b9f14913 1318 else if (PID_key == '=') {
tnhnrl 38:83d06c294807 1319 batt_KP += gain_step_size;
tnhnrl 38:83d06c294807 1320 pc().printf("\rP gain: %0.5f ", batt_KP);
tnhnrl 16:3363b9f14913 1321 }
tnhnrl 16:3363b9f14913 1322 else if (PID_key == '[') {
tnhnrl 38:83d06c294807 1323 batt_KI -= gain_step_size;
tnhnrl 38:83d06c294807 1324 pc().printf("\rI gain: %0.5f ", batt_KI);
tnhnrl 16:3363b9f14913 1325 }
tnhnrl 16:3363b9f14913 1326 else if (PID_key == ']') {
tnhnrl 38:83d06c294807 1327 batt_KI += gain_step_size;
tnhnrl 38:83d06c294807 1328 pc().printf("\rI gain: %0.5f ", batt_KI);
tnhnrl 16:3363b9f14913 1329 }
tnhnrl 16:3363b9f14913 1330 else if (PID_key == ';') {
tnhnrl 38:83d06c294807 1331 batt_KD -= gain_step_size;
tnhnrl 38:83d06c294807 1332 pc().printf("\rD gain: %0.5f ", batt_KD);
tnhnrl 16:3363b9f14913 1333 }
tnhnrl 16:3363b9f14913 1334 else if (PID_key == '\'') {
tnhnrl 38:83d06c294807 1335 batt_KD += gain_step_size;
tnhnrl 38:83d06c294807 1336 pc().printf("\rD gain: %0.5f ", batt_KD);
tnhnrl 16:3363b9f14913 1337 }
tnhnrl 16:3363b9f14913 1338 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1339 // set global values
tnhnrl 38:83d06c294807 1340 batt().setControllerP(batt_KP);
tnhnrl 38:83d06c294807 1341 batt().setControllerI(batt_KI);
tnhnrl 38:83d06c294807 1342 batt().setControllerD(batt_KD);
tnhnrl 16:3363b9f14913 1343
tnhnrl 38:83d06c294807 1344 // save to "BATT.TXT" file
tnhnrl 38:83d06c294807 1345 configFileIO().saveBattData(batt_KP, batt_KI, batt_KD);
tnhnrl 38:83d06c294807 1346
tnhnrl 16:3363b9f14913 1347 break; //exit the while loop
tnhnrl 16:3363b9f14913 1348 }
tnhnrl 16:3363b9f14913 1349 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1350 break; //exit the while loop
tnhnrl 16:3363b9f14913 1351 }
tnhnrl 16:3363b9f14913 1352 else {
tnhnrl 16:3363b9f14913 1353 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1354 }
tnhnrl 16:3363b9f14913 1355 }
tnhnrl 16:3363b9f14913 1356 }
tnhnrl 20:8987a9ae2bc7 1357
tnhnrl 16:3363b9f14913 1358 void StateMachine::keyboard_menu_DEPTH_PID_settings() {
tnhnrl 16:3363b9f14913 1359 char PID_key;
tnhnrl 16:3363b9f14913 1360 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1361
tnhnrl 16:3363b9f14913 1362 // show the menu
tnhnrl 16:3363b9f14913 1363 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1364 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1365 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r");
tnhnrl 16:3363b9f14913 1366 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 1367
tnhnrl 16:3363b9f14913 1368 // handle the key presses
tnhnrl 16:3363b9f14913 1369 while(1) {
tnhnrl 16:3363b9f14913 1370 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1371 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1372 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1373 }
tnhnrl 16:3363b9f14913 1374 else {
tnhnrl 16:3363b9f14913 1375 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1376 }
tnhnrl 16:3363b9f14913 1377
tnhnrl 16:3363b9f14913 1378 // handle the user's key input
tnhnrl 16:3363b9f14913 1379 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1380 _depth_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1381 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1382 }
tnhnrl 16:3363b9f14913 1383 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1384 _depth_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1385 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1386 }
tnhnrl 16:3363b9f14913 1387 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1388 _depth_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1389 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1390 }
tnhnrl 16:3363b9f14913 1391 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1392 _depth_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1393 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1394 }
tnhnrl 16:3363b9f14913 1395 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1396 _depth_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1397 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1398 }
tnhnrl 16:3363b9f14913 1399 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1400 _depth_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1401 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1402 }
tnhnrl 16:3363b9f14913 1403 else if (PID_key == 'S') { // user wants to save these settings
tnhnrl 16:3363b9f14913 1404 // set global values
tnhnrl 21:38c8544db6f4 1405 depthLoop().setControllerP(_depth_KP);
tnhnrl 21:38c8544db6f4 1406 depthLoop().setControllerI(_depth_KI);
tnhnrl 21:38c8544db6f4 1407 depthLoop().setControllerD(_depth_KD);
tnhnrl 16:3363b9f14913 1408
tnhnrl 21:38c8544db6f4 1409 // save depth PID values for outer loop
tnhnrl 21:38c8544db6f4 1410 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1411 break; //exit the while loop
tnhnrl 16:3363b9f14913 1412 }
tnhnrl 16:3363b9f14913 1413 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1414 break; //exit the while loop
tnhnrl 16:3363b9f14913 1415 }
tnhnrl 16:3363b9f14913 1416 else {
tnhnrl 16:3363b9f14913 1417 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1418 }
tnhnrl 16:3363b9f14913 1419 }
tnhnrl 16:3363b9f14913 1420 }
tnhnrl 16:3363b9f14913 1421
tnhnrl 16:3363b9f14913 1422 void StateMachine::keyboard_menu_PITCH_PID_settings() {
tnhnrl 16:3363b9f14913 1423 char PID_key;
tnhnrl 16:3363b9f14913 1424 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1425
tnhnrl 16:3363b9f14913 1426 // print the menu
tnhnrl 16:3363b9f14913 1427 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1428 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1429 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 16:3363b9f14913 1430 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 1431
tnhnrl 16:3363b9f14913 1432 // handle the key presses
tnhnrl 16:3363b9f14913 1433 while(1) {
tnhnrl 16:3363b9f14913 1434 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1435 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1436 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1437 }
tnhnrl 16:3363b9f14913 1438 else {
tnhnrl 16:3363b9f14913 1439 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1440 }
tnhnrl 16:3363b9f14913 1441
tnhnrl 16:3363b9f14913 1442 // handle the user's key input
tnhnrl 16:3363b9f14913 1443 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1444 _pitch_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1445 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1446 }
tnhnrl 16:3363b9f14913 1447 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1448 _pitch_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1449 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1450 }
tnhnrl 16:3363b9f14913 1451 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1452 _pitch_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1453 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1454 }
tnhnrl 16:3363b9f14913 1455 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1456 _pitch_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1457 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1458 }
tnhnrl 16:3363b9f14913 1459 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1460 _pitch_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1461 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1462 }
tnhnrl 16:3363b9f14913 1463 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1464 _pitch_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1465 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1466 }
tnhnrl 16:3363b9f14913 1467 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1468 // set global values
tnhnrl 21:38c8544db6f4 1469 pitchLoop().setControllerP(_pitch_KP);
tnhnrl 21:38c8544db6f4 1470 pitchLoop().setControllerI(_pitch_KI);
tnhnrl 21:38c8544db6f4 1471 pitchLoop().setControllerD(_pitch_KD);
tnhnrl 16:3363b9f14913 1472
tnhnrl 32:f2f8ae34aadc 1473 // save pitch PID values for outer loop (must save neutral position also)
tnhnrl 21:38c8544db6f4 1474 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1475 break; //exit the while loop
tnhnrl 16:3363b9f14913 1476 }
tnhnrl 16:3363b9f14913 1477 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1478 break; //exit the while loop
tnhnrl 16:3363b9f14913 1479 }
tnhnrl 16:3363b9f14913 1480 else {
tnhnrl 16:3363b9f14913 1481 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1482 }
tnhnrl 16:3363b9f14913 1483 }
tnhnrl 16:3363b9f14913 1484 }
tnhnrl 20:8987a9ae2bc7 1485
tnhnrl 16:3363b9f14913 1486 float StateMachine::getDepthCommand() {
tnhnrl 32:f2f8ae34aadc 1487 return _depth_command;
tnhnrl 16:3363b9f14913 1488 }
tnhnrl 20:8987a9ae2bc7 1489
tnhnrl 16:3363b9f14913 1490 float StateMachine::getPitchCommand() {
tnhnrl 32:f2f8ae34aadc 1491 return _pitch_command;
tnhnrl 32:f2f8ae34aadc 1492 }
tnhnrl 32:f2f8ae34aadc 1493
tnhnrl 32:f2f8ae34aadc 1494 float StateMachine::getDepthReading() {
tnhnrl 32:f2f8ae34aadc 1495 return _depth_reading;
tnhnrl 32:f2f8ae34aadc 1496 }
tnhnrl 32:f2f8ae34aadc 1497
tnhnrl 32:f2f8ae34aadc 1498 float StateMachine::getPitchReading() {
tnhnrl 32:f2f8ae34aadc 1499 return _pitch_reading;
tnhnrl 32:f2f8ae34aadc 1500 }
tnhnrl 32:f2f8ae34aadc 1501
tnhnrl 32:f2f8ae34aadc 1502 float StateMachine::getTimerReading() {
tnhnrl 32:f2f8ae34aadc 1503 return _timer_reading;
tnhnrl 17:7c16b5671d0e 1504 }
tnhnrl 28:16c83a2fdefa 1505
tnhnrl 17:7c16b5671d0e 1506 void StateMachine::setState(int input_state) {
tnhnrl 21:38c8544db6f4 1507 _state = input_state;
tnhnrl 17:7c16b5671d0e 1508 }
tnhnrl 20:8987a9ae2bc7 1509
tnhnrl 17:7c16b5671d0e 1510 int StateMachine::getState() {
tnhnrl 17:7c16b5671d0e 1511 return _state; //return the current state of the system
tnhnrl 17:7c16b5671d0e 1512 }
tnhnrl 20:8987a9ae2bc7 1513
tnhnrl 17:7c16b5671d0e 1514 void StateMachine::setTimeout(float input_timeout) {
tnhnrl 17:7c16b5671d0e 1515 _timeout = input_timeout;
tnhnrl 17:7c16b5671d0e 1516 }
tnhnrl 20:8987a9ae2bc7 1517
tnhnrl 17:7c16b5671d0e 1518 void StateMachine::setDepthCommand(float input_depth_command) {
tnhnrl 32:f2f8ae34aadc 1519 _depth_command = input_depth_command;
tnhnrl 17:7c16b5671d0e 1520 }
tnhnrl 20:8987a9ae2bc7 1521
tnhnrl 17:7c16b5671d0e 1522 void StateMachine::setPitchCommand(float input_pitch_command) {
tnhnrl 32:f2f8ae34aadc 1523 _pitch_command = input_pitch_command;
tnhnrl 17:7c16b5671d0e 1524 }
tnhnrl 20:8987a9ae2bc7 1525
tnhnrl 17:7c16b5671d0e 1526 void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) {
tnhnrl 21:38c8544db6f4 1527 _neutral_batt_pos_mm = batt_pos_mm;
tnhnrl 21:38c8544db6f4 1528 _neutral_bce_pos_mm = bce_pos_mm;
tnhnrl 17:7c16b5671d0e 1529
tnhnrl 21:38c8544db6f4 1530 pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm);
tnhnrl 17:7c16b5671d0e 1531 }
tnhnrl 20:8987a9ae2bc7 1532
tnhnrl 17:7c16b5671d0e 1533 int StateMachine::timeoutRunning() {
tnhnrl 28:16c83a2fdefa 1534 return _isTimeoutRunning;
tnhnrl 17:7c16b5671d0e 1535 }
tnhnrl 20:8987a9ae2bc7 1536
tnhnrl 17:7c16b5671d0e 1537 //process one state at a time
tnhnrl 17:7c16b5671d0e 1538 void StateMachine::getDiveSequence() {
tnhnrl 17:7c16b5671d0e 1539 //iterate through this sequence using the FSM
tnhnrl 24:c7d9b5bf3829 1540 currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state;
tnhnrl 24:c7d9b5bf3829 1541 currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout;
tnhnrl 24:c7d9b5bf3829 1542 currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth;
tnhnrl 24:c7d9b5bf3829 1543 currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch;
tnhnrl 17:7c16b5671d0e 1544
tnhnrl 17:7c16b5671d0e 1545 _timeout = currentStateStruct.timeout; //set timeout before exiting this function
tnhnrl 32:f2f8ae34aadc 1546 }
tnhnrl 32:f2f8ae34aadc 1547
tnhnrl 32:f2f8ae34aadc 1548 void StateMachine::printDirectory() {
tnhnrl 32:f2f8ae34aadc 1549 //create a DirectoryList object that points to the local directory
tnhnrl 32:f2f8ae34aadc 1550 DirectoryList mbed_dir( "/local" );
tnhnrl 32:f2f8ae34aadc 1551
tnhnrl 32:f2f8ae34aadc 1552 if ( mbed_dir.error_check() ) {
tnhnrl 32:f2f8ae34aadc 1553 //error( "MBED directory could not be opened\r\n" );
tnhnrl 32:f2f8ae34aadc 1554 pc().printf("MBED directory could not be opened\r\n");
tnhnrl 32:f2f8ae34aadc 1555 }
tnhnrl 32:f2f8ae34aadc 1556
tnhnrl 35:2f66ea4863d5 1557 else {
tnhnrl 35:2f66ea4863d5 1558 pc().printf("\n\rFiles in MBED directory:\n\r");
tnhnrl 35:2f66ea4863d5 1559 for ( int i = 0; i < mbed_dir.size(); i++ )
tnhnrl 35:2f66ea4863d5 1560 pc().printf( "%s\r\n", mbed_dir[ i ].c_str() );
tnhnrl 35:2f66ea4863d5 1561 }
tnhnrl 35:2f66ea4863d5 1562
tnhnrl 35:2f66ea4863d5 1563 //SD CARD DIRECTORY (does not work for openlog)
tnhnrl 32:f2f8ae34aadc 1564 DirectoryList sd_dir( "/sd" );
tnhnrl 32:f2f8ae34aadc 1565
tnhnrl 32:f2f8ae34aadc 1566 if ( sd_dir.error_check() ) {
tnhnrl 32:f2f8ae34aadc 1567 //error( "MBED directory could not be opened\r\n" );
tnhnrl 35:2f66ea4863d5 1568 pc().printf("SD directory could not be opened\r\n");
tnhnrl 32:f2f8ae34aadc 1569 }
tnhnrl 35:2f66ea4863d5 1570 else {
tnhnrl 35:2f66ea4863d5 1571 pc().printf("\n\rFiles in SD card directory:\n\r");
tnhnrl 35:2f66ea4863d5 1572 for ( int i = 0; i < sd_dir.size(); i++ )
tnhnrl 35:2f66ea4863d5 1573 pc().printf( "%s\r\n", sd_dir[ i ].c_str() );
tnhnrl 35:2f66ea4863d5 1574 }
tnhnrl 32:f2f8ae34aadc 1575 }
tnhnrl 32:f2f8ae34aadc 1576
tnhnrl 32:f2f8ae34aadc 1577 void StateMachine::printCurrentSdLog() {
tnhnrl 32:f2f8ae34aadc 1578 pc().printf("SD card log work in progress\n\r");
tnhnrl 32:f2f8ae34aadc 1579 //might be worth saving the last few logs to the MBED...
tnhnrl 32:f2f8ae34aadc 1580 }
tnhnrl 32:f2f8ae34aadc 1581
tnhnrl 32:f2f8ae34aadc 1582 //check if the file is still opened
tnhnrl 32:f2f8ae34aadc 1583 void StateMachine::createNewFile() {
tnhnrl 32:f2f8ae34aadc 1584 if (_file_closed) {
tnhnrl 34:9b66c5188051 1585 //mbedLogger().createFile(); //create a new MBED file
tnhnrl 32:f2f8ae34aadc 1586
tnhnrl 32:f2f8ae34aadc 1587 _file_closed = false; //file is still open until you get to SIT_IDLE
tnhnrl 32:f2f8ae34aadc 1588 }
tnhnrl 32:f2f8ae34aadc 1589 }
tnhnrl 32:f2f8ae34aadc 1590
tnhnrl 32:f2f8ae34aadc 1591 void StateMachine::transmitData() {
tnhnrl 32:f2f8ae34aadc 1592 static float transmit_timer = 0;
tnhnrl 32:f2f8ae34aadc 1593 static bool is_transmit_timer_running = false;
tnhnrl 32:f2f8ae34aadc 1594
tnhnrl 32:f2f8ae34aadc 1595 if (!is_transmit_timer_running) {
tnhnrl 32:f2f8ae34aadc 1596 //pc().printf("\n\n\rTRANSMIT timer running...\n\n\r"); //debug
tnhnrl 32:f2f8ae34aadc 1597
tnhnrl 32:f2f8ae34aadc 1598 transmit_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 32:f2f8ae34aadc 1599 is_transmit_timer_running = true; //disable this block after one iteration
tnhnrl 32:f2f8ae34aadc 1600
tnhnrl 32:f2f8ae34aadc 1601 pc().printf("TESTING to see if this transmits once a second. (timer: %0.1f)\n\r", timer.read());
tnhnrl 32:f2f8ae34aadc 1602 }
tnhnrl 32:f2f8ae34aadc 1603 if (timer.read() >= transmit_timer) {
tnhnrl 32:f2f8ae34aadc 1604 is_transmit_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 32:f2f8ae34aadc 1605 }
tnhnrl 34:9b66c5188051 1606 }
tnhnrl 34:9b66c5188051 1607
tnhnrl 34:9b66c5188051 1608 void StateMachine::recordData(int input_state) {
tnhnrl 34:9b66c5188051 1609 string string_state;
tnhnrl 34:9b66c5188051 1610 if (input_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 1611 string_state = "SIT_IDLE";
tnhnrl 34:9b66c5188051 1612 else if (input_state == FIND_NEUTRAL)
tnhnrl 34:9b66c5188051 1613 string_state = "FIND_NEUTRAL";
tnhnrl 34:9b66c5188051 1614 else if (input_state == DIVE)
tnhnrl 34:9b66c5188051 1615 string_state = "DIVE";
tnhnrl 34:9b66c5188051 1616 else if (input_state == RISE)
tnhnrl 34:9b66c5188051 1617 string_state = "RISE";
tnhnrl 34:9b66c5188051 1618 else if (input_state == FLOAT_LEVEL)
tnhnrl 34:9b66c5188051 1619 string_state = "FLOAT_LEVEL";
tnhnrl 34:9b66c5188051 1620 else if (input_state == FLOAT_BROADCAST)
tnhnrl 34:9b66c5188051 1621 string_state = "FLOAT_BROADCAST";
tnhnrl 34:9b66c5188051 1622 else if (input_state == EMERGENCY_CLIMB)
tnhnrl 34:9b66c5188051 1623 string_state = "EMERGENCY_CLIMB";
tnhnrl 34:9b66c5188051 1624 else if (input_state == MULTI_DIVE)
tnhnrl 34:9b66c5188051 1625 string_state = "MULTI_DIVE";
tnhnrl 34:9b66c5188051 1626 else if (input_state == MULTI_RISE)
tnhnrl 34:9b66c5188051 1627 string_state = "MULTI_RISE";
tnhnrl 34:9b66c5188051 1628 else if (input_state == KEYBOARD)
tnhnrl 34:9b66c5188051 1629 string_state = "KEYBOARD";
tnhnrl 34:9b66c5188051 1630
tnhnrl 34:9b66c5188051 1631 if (!_is_log_timer_running) {
tnhnrl 34:9b66c5188051 1632 //pc().printf("\n\n\rlog timer running...\n\n\r"); //debug
tnhnrl 34:9b66c5188051 1633
tnhnrl 34:9b66c5188051 1634 _log_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 34:9b66c5188051 1635 _is_log_timer_running = true; //disable this block after one iteration
tnhnrl 34:9b66c5188051 1636
tnhnrl 37:357e98a929cc 1637 _data_log[0] = systemTime().read(); //system time reading
tnhnrl 34:9b66c5188051 1638 _data_log[1] = depthLoop().getCommand(); //depth command
tnhnrl 34:9b66c5188051 1639 _data_log[2] = depthLoop().getPosition(); //depth reading
tnhnrl 34:9b66c5188051 1640 _data_log[3] = pitchLoop().getCommand(); //pitch command
tnhnrl 34:9b66c5188051 1641 _data_log[4] = pitchLoop().getPosition(); //pitch reading
tnhnrl 34:9b66c5188051 1642 _data_log[5] = bce().getSetPosition_mm();
tnhnrl 34:9b66c5188051 1643 _data_log[6] = bce().getPosition_mm();
tnhnrl 34:9b66c5188051 1644 _data_log[7] = batt().getSetPosition_mm();
tnhnrl 34:9b66c5188051 1645 _data_log[8] = batt().getPosition_mm();
tnhnrl 34:9b66c5188051 1646
tnhnrl 34:9b66c5188051 1647 //record data to the MBED every 5 seconds
tnhnrl 35:2f66ea4863d5 1648 //mbedLogger().saveArrayToFile(string_state,input_state,_data_log);
tnhnrl 34:9b66c5188051 1649 }
tnhnrl 34:9b66c5188051 1650 if (timer.read() >= _log_timer) {
tnhnrl 34:9b66c5188051 1651 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 1652 }
tnhnrl 34:9b66c5188051 1653 }
tnhnrl 34:9b66c5188051 1654
tnhnrl 34:9b66c5188051 1655 void StateMachine::recordState(int input_state) {
tnhnrl 34:9b66c5188051 1656 string string_state;
tnhnrl 34:9b66c5188051 1657
tnhnrl 34:9b66c5188051 1658 if (input_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 1659 string_state = "SIT_IDLE";
tnhnrl 34:9b66c5188051 1660 else if (input_state == FIND_NEUTRAL)
tnhnrl 34:9b66c5188051 1661 string_state = "FIND_NEUTRAL";
tnhnrl 34:9b66c5188051 1662 else if (input_state == DIVE)
tnhnrl 34:9b66c5188051 1663 string_state = "DIVE";
tnhnrl 34:9b66c5188051 1664 else if (input_state == RISE)
tnhnrl 34:9b66c5188051 1665 string_state = "RISE";
tnhnrl 34:9b66c5188051 1666 else if (input_state == FLOAT_LEVEL)
tnhnrl 34:9b66c5188051 1667 string_state = "FLOAT_LEVEL";
tnhnrl 34:9b66c5188051 1668 else if (input_state == FLOAT_BROADCAST)
tnhnrl 34:9b66c5188051 1669 string_state = "FLOAT_BROADCAST";
tnhnrl 34:9b66c5188051 1670 else if (input_state == EMERGENCY_CLIMB)
tnhnrl 34:9b66c5188051 1671 string_state = "EMERGENCY_CLIMB";
tnhnrl 34:9b66c5188051 1672 else if (input_state == MULTI_DIVE)
tnhnrl 34:9b66c5188051 1673 string_state = "MULTI_DIVE";
tnhnrl 34:9b66c5188051 1674 else if (input_state == MULTI_RISE)
tnhnrl 34:9b66c5188051 1675 string_state = "MULTI_RISE";
tnhnrl 34:9b66c5188051 1676 else if (input_state == KEYBOARD)
tnhnrl 34:9b66c5188051 1677 string_state = "KEYBOARD";
tnhnrl 34:9b66c5188051 1678 //datalogger().printf("%s\n", string_state.c_str());
tnhnrl 36:966a86937e17 1679 }
tnhnrl 36:966a86937e17 1680
tnhnrl 36:966a86937e17 1681 float * StateMachine::dataArray() {
tnhnrl 36:966a86937e17 1682 //return the array to a calling function
tnhnrl 36:966a86937e17 1683 return _data_log;
tnhnrl 16:3363b9f14913 1684 }