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:
Wed Dec 20 13:52:50 2017 +0000
Revision:
32:f2f8ae34aadc
Parent:
30:2964617e7676
Child:
33:29a4268fbc74
Child:
34:9b66c5188051
revA

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