most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
tnhnrl
Date:
Thu Jun 14 16:10:25 2018 +0000
Revision:
57:ec69651c8c21
Parent:
56:48a8a5a65b82
Child:
58:94b7fd55185e
Working version with debug and simple menu

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 52:f207567d3ea4 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 57:ec69651c8c21 15 //new commands
tnhnrl 57:ec69651c8c21 16 _BCE_position_command = bce().getTravelLimit(); //starting at the limits
tnhnrl 57:ec69651c8c21 17 _BMM_position_command = batt().getTravelLimit();
tnhnrl 57:ec69651c8c21 18 //new commands
tnhnrl 57:ec69651c8c21 19
tnhnrl 28:16c83a2fdefa 20 _neutral_timer = 0; //timer used in FIND_NEUTRAL sub-FSM
tnhnrl 20:8987a9ae2bc7 21
tnhnrl 28:16c83a2fdefa 22 _state = SIT_IDLE; // select starting state here
tnhnrl 28:16c83a2fdefa 23 _isTimeoutRunning = false; // default timer to not running
tnhnrl 28:16c83a2fdefa 24 _isSubStateTimerRunning = false; // default timer to not running
tnhnrl 17:7c16b5671d0e 25
tnhnrl 24:c7d9b5bf3829 26 _multi_dive_counter = 0;
tnhnrl 21:38c8544db6f4 27
tnhnrl 21:38c8544db6f4 28 _neutral_sub_state_active = false;
tnhnrl 17:7c16b5671d0e 29
tnhnrl 21:38c8544db6f4 30 _depth_KP = depthLoop().getControllerP(); // load current depth value
tnhnrl 21:38c8544db6f4 31 _depth_KI = depthLoop().getControllerI(); // load current depth value
tnhnrl 21:38c8544db6f4 32 _depth_KD = depthLoop().getControllerD(); // load current depth value
tnhnrl 21:38c8544db6f4 33
tnhnrl 21:38c8544db6f4 34 _pitch_KP = pitchLoop().getControllerP(); // load current pitch value
tnhnrl 21:38c8544db6f4 35 _pitch_KI = pitchLoop().getControllerI(); // load current pitch value
tnhnrl 21:38c8544db6f4 36 _pitch_KD = pitchLoop().getControllerD(); // load current pitch value
tnhnrl 21:38c8544db6f4 37
tnhnrl 21:38c8544db6f4 38 _neutral_bce_pos_mm = depthLoop().getOutputOffset(); //load current neutral buoyancy position offset
tnhnrl 21:38c8544db6f4 39 _neutral_batt_pos_mm = pitchLoop().getOutputOffset(); //load current neutral buoyancy position offset
tnhnrl 23:434f04ef1fad 40
tnhnrl 28:16c83a2fdefa 41 _state_array_counter = 1; //used to iterate through and record states
tnhnrl 28:16c83a2fdefa 42 _substate_array_counter = 0; //used to iterate through and record substates
tnhnrl 28:16c83a2fdefa 43
tnhnrl 28:16c83a2fdefa 44 _state_array[0] = SIT_IDLE; //system starts in the SIT_IDLE state, record this
tnhnrl 24:c7d9b5bf3829 45
tnhnrl 30:2964617e7676 46 _substate = NEUTRAL_SINKING; //start sub-FSM in NEUTRAL_SINKING
tnhnrl 28:16c83a2fdefa 47 _previous_substate = -1; //to start sub-FSM
tnhnrl 28:16c83a2fdefa 48 _previous_state = -1; //for tracking FSM states
tnhnrl 28:16c83a2fdefa 49
tnhnrl 28:16c83a2fdefa 50 _max_recorded_depth_neutral = -99; //float to record max depth
tnhnrl 28:16c83a2fdefa 51 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 32:f2f8ae34aadc 52
tnhnrl 32:f2f8ae34aadc 53 _neutral_sink_command_mm = -2.5; //defaults for neutral finding sub-FSM
tnhnrl 32:f2f8ae34aadc 54 _neutral_rise_command_mm = 2.0;
tnhnrl 32:f2f8ae34aadc 55 _neutral_pitch_command_mm = 0.5;
tnhnrl 32:f2f8ae34aadc 56
tnhnrl 32:f2f8ae34aadc 57 _max_recorded_auto_neutral_depth = -99;
tnhnrl 32:f2f8ae34aadc 58
tnhnrl 32:f2f8ae34aadc 59 _file_closed = true;
tnhnrl 57:ec69651c8c21 60
tnhnrl 57:ec69651c8c21 61 _debug_menu_on = false; //toggle between debug and simple menu screens
tnhnrl 16:3363b9f14913 62 }
tnhnrl 20:8987a9ae2bc7 63
tnhnrl 17:7c16b5671d0e 64 //Finite State Machine (FSM)
tnhnrl 45:16b8162188ca 65 int StateMachine::runStateMachine() {
tnhnrl 45:16b8162188ca 66 static int transmit_packet_number = 1; //for data transmission
tnhnrl 45:16b8162188ca 67
tnhnrl 16:3363b9f14913 68 // finite state machine ... each state has at least one exit criteria
tnhnrl 17:7c16b5671d0e 69 switch (_state) {
tnhnrl 16:3363b9f14913 70 case SIT_IDLE :
tnhnrl 28:16c83a2fdefa 71 case KEYBOARD:
tnhnrl 16:3363b9f14913 72 // there actually is no timeout for SIT_IDLE, but this enables some one-shot actions
tnhnrl 28:16c83a2fdefa 73 if (!_isTimeoutRunning) {
tnhnrl 57:ec69651c8c21 74
tnhnrl 57:ec69651c8c21 75 if (_debug_menu_on)
tnhnrl 57:ec69651c8c21 76 showDebugMenu();
tnhnrl 57:ec69651c8c21 77 else
tnhnrl 57:ec69651c8c21 78 showSimpleMenu();
tnhnrl 16:3363b9f14913 79 pc().printf("\r\n\nstate: SIT_IDLE\r\n");
tnhnrl 28:16c83a2fdefa 80 _isTimeoutRunning = true;
tnhnrl 20:8987a9ae2bc7 81
tnhnrl 16:3363b9f14913 82 // what is active?
tnhnrl 16:3363b9f14913 83 bce().pause();
tnhnrl 16:3363b9f14913 84 batt().pause();
tnhnrl 32:f2f8ae34aadc 85
tnhnrl 17:7c16b5671d0e 86 //reset sub FSM
tnhnrl 28:16c83a2fdefa 87 _isSubStateTimerRunning = false;
tnhnrl 32:f2f8ae34aadc 88
tnhnrl 32:f2f8ae34aadc 89 //close the MBED file
tnhnrl 32:f2f8ae34aadc 90 _file_closed = true;
tnhnrl 16:3363b9f14913 91 }
tnhnrl 20:8987a9ae2bc7 92
tnhnrl 16:3363b9f14913 93 // how exit?
tnhnrl 20:8987a9ae2bc7 94 keyboard(); // keyboard function will change the state if needed
tnhnrl 16:3363b9f14913 95 break;
tnhnrl 49:47ffa4feb6db 96
tnhnrl 49:47ffa4feb6db 97 case CHECK_TUNING : // state used to check the tuning of the pressure vessel
tnhnrl 49:47ffa4feb6db 98 // start local state timer and init any other one-shot actions
tnhnrl 49:47ffa4feb6db 99 if (!_isTimeoutRunning) {
tnhnrl 49:47ffa4feb6db 100 pc().printf("\r\n\nstate: CHECK_TUNING\r\n");
tnhnrl 49:47ffa4feb6db 101 timer.reset(); // timer goes back to zero
tnhnrl 49:47ffa4feb6db 102 timer.start(); // background timer starts running
tnhnrl 49:47ffa4feb6db 103 _isTimeoutRunning = true;
tnhnrl 49:47ffa4feb6db 104
tnhnrl 49:47ffa4feb6db 105 // what needs to be started?
tnhnrl 49:47ffa4feb6db 106 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 107 batt().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 108
tnhnrl 49:47ffa4feb6db 109 // what are the commands? (DRIVE THE MOTORS "DIRECTLY")
tnhnrl 49:47ffa4feb6db 110 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 111 batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 112
tnhnrl 49:47ffa4feb6db 113 // getSetPosition_mm is the commanded position in the LinearActuator class
tnhnrl 49:47ffa4feb6db 114
tnhnrl 49:47ffa4feb6db 115 pc().printf("CHECK_TUNING: BCE cmd: %3.1f (BCE current position: %3.1f)\r\n", bce().getSetPosition_mm(), bce().getPosition_mm());
tnhnrl 49:47ffa4feb6db 116 pc().printf("CHECK_TUNING: BATT cmd: %3.1f (BATT current position: %3.1f)\r\n", batt().getSetPosition_mm(), bce().getPosition_mm());
tnhnrl 49:47ffa4feb6db 117
tnhnrl 49:47ffa4feb6db 118 //show that this is the start of new dive sequence
tnhnrl 49:47ffa4feb6db 119 recordState(_state);
tnhnrl 49:47ffa4feb6db 120
tnhnrl 49:47ffa4feb6db 121 //triggers logger array
tnhnrl 49:47ffa4feb6db 122 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 49:47ffa4feb6db 123 recordData(_state);
tnhnrl 49:47ffa4feb6db 124 }
tnhnrl 49:47ffa4feb6db 125
tnhnrl 49:47ffa4feb6db 126 // how exit?
tnhnrl 49:47ffa4feb6db 127 if (timer > _timeout) {
tnhnrl 49:47ffa4feb6db 128 pc().printf("CHECK_TUNING: timed out!\r\n");
tnhnrl 49:47ffa4feb6db 129 _state = FLOAT_BROADCAST;
tnhnrl 49:47ffa4feb6db 130 timer.reset();
tnhnrl 49:47ffa4feb6db 131 _isTimeoutRunning = false;
tnhnrl 49:47ffa4feb6db 132 }
tnhnrl 49:47ffa4feb6db 133
tnhnrl 49:47ffa4feb6db 134 //print status to screen continuously
tnhnrl 49:47ffa4feb6db 135 pc().printf("CHECK_TUNING: BCE_position: %0.1f, BATT_position: %0.1f (BCE_cmd: %0.1f, BATT_cmd: %0.1f)(depth: %0.1f ft, pitch: %0.1f deg) [%0.1f sec]\r",bce().getPosition_mm(),batt().getPosition_mm(),bce().getSetPosition_mm(),batt().getSetPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition(),timer.read());
tnhnrl 49:47ffa4feb6db 136
tnhnrl 49:47ffa4feb6db 137 //record data every 5 seconds
tnhnrl 49:47ffa4feb6db 138 recordData(_state);
tnhnrl 49:47ffa4feb6db 139
tnhnrl 49:47ffa4feb6db 140 break;
tnhnrl 20:8987a9ae2bc7 141
tnhnrl 16:3363b9f14913 142 case EMERGENCY_CLIMB :
tnhnrl 16:3363b9f14913 143 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 144 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 145 pc().printf("\r\n\nstate: EMERGENCY_CLIMB\r\n");
tnhnrl 16:3363b9f14913 146 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 147 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 148 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 149
tnhnrl 16:3363b9f14913 150 // what needs to be started?
tnhnrl 16:3363b9f14913 151 bce().unpause();
tnhnrl 16:3363b9f14913 152 batt().unpause();
tnhnrl 20:8987a9ae2bc7 153
tnhnrl 20:8987a9ae2bc7 154 // what are the commands?
tnhnrl 16:3363b9f14913 155 bce().setPosition_mm(bce().getTravelLimit());
tnhnrl 16:3363b9f14913 156 batt().setPosition_mm(0.0);
tnhnrl 32:f2f8ae34aadc 157
tnhnrl 32:f2f8ae34aadc 158 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 159 ////////createNewFile();
tnhnrl 32:f2f8ae34aadc 160
tnhnrl 32:f2f8ae34aadc 161 //show that this is the start of a new EMERGENCY_CLIMB sequence
tnhnrl 32:f2f8ae34aadc 162 recordState(_state);
tnhnrl 32:f2f8ae34aadc 163
tnhnrl 49:47ffa4feb6db 164 //triggers logger array
tnhnrl 32:f2f8ae34aadc 165 _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 166 recordData(_state);
tnhnrl 16:3363b9f14913 167 }
tnhnrl 20:8987a9ae2bc7 168
tnhnrl 16:3363b9f14913 169 // how exit?
tnhnrl 17:7c16b5671d0e 170 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 171 pc().printf("EC: timed out\r\n");
tnhnrl 21:38c8544db6f4 172 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 173 timer.reset();
tnhnrl 28:16c83a2fdefa 174 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 175 }
tnhnrl 26:7e118fc02eea 176 else if (depthLoop().getPosition() < 2.0) { //if the depth is greater than 0.2 feet, go to float broadcast
tnhnrl 21:38c8544db6f4 177 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 178 timer.reset();
tnhnrl 28:16c83a2fdefa 179 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 180 }
tnhnrl 32:f2f8ae34aadc 181
tnhnrl 34:9b66c5188051 182 //print status to screen continuously
tnhnrl 34:9b66c5188051 183 pc().printf("EC: depth: %3.1f (BCE_cmd: %0.1f), pitch: %0.1f deg [%0.1f sec]\r",depthLoop().getPosition(),bce().getPosition_mm(),pitchLoop().getPosition(),timer.read());
tnhnrl 34:9b66c5188051 184
tnhnrl 32:f2f8ae34aadc 185 //record data every 5 seconds
tnhnrl 34:9b66c5188051 186 recordData(_state);
tnhnrl 32:f2f8ae34aadc 187
tnhnrl 16:3363b9f14913 188 break;
tnhnrl 20:8987a9ae2bc7 189
tnhnrl 16:3363b9f14913 190 case FIND_NEUTRAL :
tnhnrl 20:8987a9ae2bc7 191 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 192 if (!_isTimeoutRunning) {
tnhnrl 21:38c8544db6f4 193 pc().printf("\r\n\nstate: FIND_NEUTRAL\n\r");
tnhnrl 16:3363b9f14913 194 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 195 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 196 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 197
tnhnrl 16:3363b9f14913 198 // what needs to be started?
tnhnrl 16:3363b9f14913 199 bce().unpause();
tnhnrl 16:3363b9f14913 200 batt().unpause();
tnhnrl 28:16c83a2fdefa 201 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 24:c7d9b5bf3829 202 batt().setPosition_mm(_neutral_batt_pos_mm); //set battery to close-to-neutral setting from config file
tnhnrl 17:7c16b5671d0e 203
tnhnrl 24:c7d9b5bf3829 204 //first iteration goes into Neutral Finding Sub-FSM
tnhnrl 24:c7d9b5bf3829 205 //set the first state of the FSM, and start the sub-FSM
tnhnrl 30:2964617e7676 206 _substate = NEUTRAL_SINKING; //first state in neutral sub-FSM is the pressure vessel sinking
tnhnrl 28:16c83a2fdefa 207 _previous_substate = -1;
tnhnrl 28:16c83a2fdefa 208
tnhnrl 28:16c83a2fdefa 209 //save this state to the array
tnhnrl 30:2964617e7676 210 _substate_array[_substate_array_counter] = NEUTRAL_SINKING; //save to state array
tnhnrl 28:16c83a2fdefa 211 _substate_array_counter++;
tnhnrl 28:16c83a2fdefa 212
tnhnrl 32:f2f8ae34aadc 213 runNeutralStateMachine();
tnhnrl 32:f2f8ae34aadc 214
tnhnrl 32:f2f8ae34aadc 215 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 216 //createNewFile();
tnhnrl 32:f2f8ae34aadc 217
tnhnrl 32:f2f8ae34aadc 218 //show that this is the start of a new FIND_NEUTRAL sequence
tnhnrl 32:f2f8ae34aadc 219 recordState(_state);
tnhnrl 32:f2f8ae34aadc 220
tnhnrl 49:47ffa4feb6db 221 //triggers logger array
tnhnrl 32:f2f8ae34aadc 222 _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 223 recordData(_state);
tnhnrl 16:3363b9f14913 224 }
tnhnrl 20:8987a9ae2bc7 225
tnhnrl 20:8987a9ae2bc7 226 // how exit? (exit with the timer, if timer still running continue processing sub FSM)
tnhnrl 17:7c16b5671d0e 227 if (timer > _timeout) {
tnhnrl 17:7c16b5671d0e 228 pc().printf("FN: timed out [time: %0.1f sec]\r\n", timer.read());
tnhnrl 21:38c8544db6f4 229 _state = EMERGENCY_CLIMB; //new behavior (if this times out it emergency surfaces)
tnhnrl 16:3363b9f14913 230 timer.reset();
tnhnrl 28:16c83a2fdefa 231 _isTimeoutRunning = false;
tnhnrl 24:c7d9b5bf3829 232
tnhnrl 24:c7d9b5bf3829 233 //record this to the NEUTRAL sub-FSM tracker
tnhnrl 28:16c83a2fdefa 234 _substate_array[_substate_array_counter] = EMERGENCY_CLIMB; //save to state array
tnhnrl 28:16c83a2fdefa 235 _substate_array_counter++;
tnhnrl 16:3363b9f14913 236 }
tnhnrl 21:38c8544db6f4 237
tnhnrl 24:c7d9b5bf3829 238 //what is active? (neutral finding sub-function runs until completion)
tnhnrl 24:c7d9b5bf3829 239 //check if substate returned exit state, if so stop running the sub-FSM
tnhnrl 26:7e118fc02eea 240 else if (runNeutralStateMachine() == NEUTRAL_EXIT) {
tnhnrl 21:38c8544db6f4 241 //if successful, FIND_NEUTRAL then goes to RISE
tnhnrl 21:38c8544db6f4 242 pc().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\n\n\r");
tnhnrl 21:38c8544db6f4 243 _state = RISE;
tnhnrl 30:2964617e7676 244 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 245 }
tnhnrl 32:f2f8ae34aadc 246
tnhnrl 32:f2f8ae34aadc 247 //record data every 5 seconds
tnhnrl 34:9b66c5188051 248 recordData(_state);
tnhnrl 32:f2f8ae34aadc 249
tnhnrl 17:7c16b5671d0e 250 break;
tnhnrl 17:7c16b5671d0e 251
tnhnrl 16:3363b9f14913 252 case DIVE :
tnhnrl 16:3363b9f14913 253 // start local state timer and init any other one-shot actions
tnhnrl 32:f2f8ae34aadc 254
tnhnrl 28:16c83a2fdefa 255 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 256 pc().printf("\r\n\nstate: DIVE\r\n");
tnhnrl 16:3363b9f14913 257 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 258 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 259 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 260
tnhnrl 16:3363b9f14913 261 // what needs to be started?
tnhnrl 16:3363b9f14913 262 bce().unpause();
tnhnrl 16:3363b9f14913 263 batt().unpause();
tnhnrl 20:8987a9ae2bc7 264
tnhnrl 16:3363b9f14913 265 // what are the commands?
tnhnrl 32:f2f8ae34aadc 266 depthLoop().setCommand(_depth_command);
tnhnrl 32:f2f8ae34aadc 267 pitchLoop().setCommand(_pitch_command);
tnhnrl 32:f2f8ae34aadc 268
tnhnrl 34:9b66c5188051 269 pc().printf("DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand());
tnhnrl 34:9b66c5188051 270 pc().printf("DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 28:16c83a2fdefa 271
tnhnrl 28:16c83a2fdefa 272 //reset max dive depth
tnhnrl 28:16c83a2fdefa 273 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 32:f2f8ae34aadc 274
tnhnrl 32:f2f8ae34aadc 275 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 276 //createNewFile();
tnhnrl 32:f2f8ae34aadc 277
tnhnrl 32:f2f8ae34aadc 278 //show that this is the start of new dive sequence
tnhnrl 32:f2f8ae34aadc 279 recordState(_state);
tnhnrl 32:f2f8ae34aadc 280
tnhnrl 49:47ffa4feb6db 281 //triggers logger array
tnhnrl 32:f2f8ae34aadc 282 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 283 recordData(_state);
tnhnrl 16:3363b9f14913 284 }
tnhnrl 20:8987a9ae2bc7 285
tnhnrl 16:3363b9f14913 286 // how exit?
tnhnrl 32:f2f8ae34aadc 287 if (timer.read() > _timeout) {
tnhnrl 17:7c16b5671d0e 288 pc().printf("DIVE: timed out\n\n\r");
tnhnrl 21:38c8544db6f4 289 _state = RISE; //new behavior 11/17/2017
tnhnrl 16:3363b9f14913 290 timer.reset();
tnhnrl 28:16c83a2fdefa 291 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 292 }
tnhnrl 32:f2f8ae34aadc 293 else if (depthLoop().getPosition() > depthLoop().getCommand() - 0.5) { // including offset for low momentum approaches
tnhnrl 32:f2f8ae34aadc 294 pc().printf("DIVE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 21:38c8544db6f4 295 _state = RISE;
tnhnrl 16:3363b9f14913 296 timer.reset();
tnhnrl 28:16c83a2fdefa 297 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 298 }
tnhnrl 20:8987a9ae2bc7 299
tnhnrl 16:3363b9f14913 300 // what is active?
tnhnrl 21:38c8544db6f4 301 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 302 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 16:3363b9f14913 303 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 28:16c83a2fdefa 304
tnhnrl 28:16c83a2fdefa 305 if (depthLoop().getPosition() > _max_recorded_depth_dive) { //debug
tnhnrl 28:16c83a2fdefa 306 _max_recorded_depth_dive = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 307 }
tnhnrl 32:f2f8ae34aadc 308
tnhnrl 32:f2f8ae34aadc 309 //record data every 5 seconds
tnhnrl 34:9b66c5188051 310 recordData(_state);
tnhnrl 32:f2f8ae34aadc 311
tnhnrl 16:3363b9f14913 312 break;
tnhnrl 16:3363b9f14913 313
tnhnrl 16:3363b9f14913 314 case RISE :
tnhnrl 16:3363b9f14913 315 // start local state timer and init any other one-shot actions
tnhnrl 32:f2f8ae34aadc 316
tnhnrl 28:16c83a2fdefa 317 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 318 pc().printf("\r\n\nstate: RISE\r\n");
tnhnrl 16:3363b9f14913 319 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 320 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 321 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 322
tnhnrl 16:3363b9f14913 323 // what needs to be started?
tnhnrl 16:3363b9f14913 324 bce().unpause();
tnhnrl 16:3363b9f14913 325 batt().unpause();
tnhnrl 16:3363b9f14913 326
tnhnrl 16:3363b9f14913 327 // what are the commands?
tnhnrl 28:16c83a2fdefa 328 depthLoop().setCommand(-1.0); //make sure to get towards the surface (saw issues at LASR pool)
tnhnrl 32:f2f8ae34aadc 329 pitchLoop().setCommand(-_pitch_command);
tnhnrl 34:9b66c5188051 330
tnhnrl 34:9b66c5188051 331 pc().printf("RISE: depth cmd: %0.1f\r\n",depthLoop().getCommand());
tnhnrl 34:9b66c5188051 332 pc().printf("RISE: pitch cmd: %0.1f\r\n",pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 333
tnhnrl 32:f2f8ae34aadc 334 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 335 //createNewFile();
tnhnrl 32:f2f8ae34aadc 336
tnhnrl 32:f2f8ae34aadc 337 //show that this is the start of new rise sequence
tnhnrl 32:f2f8ae34aadc 338 recordState(_state);
tnhnrl 32:f2f8ae34aadc 339
tnhnrl 49:47ffa4feb6db 340 //triggers logger array
tnhnrl 32:f2f8ae34aadc 341 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 342 recordData(_state);
tnhnrl 16:3363b9f14913 343 }
tnhnrl 20:8987a9ae2bc7 344
tnhnrl 16:3363b9f14913 345 // how exit?
tnhnrl 32:f2f8ae34aadc 346 if (timer.read() > _timeout) {
tnhnrl 16:3363b9f14913 347 pc().printf("RISE: timed out\r\n");
tnhnrl 21:38c8544db6f4 348 _state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 349 timer.reset();
tnhnrl 28:16c83a2fdefa 350 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 351 }
tnhnrl 32:f2f8ae34aadc 352
tnhnrl 32:f2f8ae34aadc 353 //modified from (depthLoop().getPosition() < depthLoop().getCommand() + 0.5)
tnhnrl 32:f2f8ae34aadc 354 //did not work correctly in bench test (stuck in rise state)
tnhnrl 32:f2f8ae34aadc 355 else if (depthLoop().getPosition() < 0.5) {
tnhnrl 32:f2f8ae34aadc 356 pc().printf("RISE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 28:16c83a2fdefa 357 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 358 timer.reset();
tnhnrl 28:16c83a2fdefa 359 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 360 }
tnhnrl 20:8987a9ae2bc7 361
tnhnrl 20:8987a9ae2bc7 362 // what is active?
tnhnrl 16:3363b9f14913 363 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 364 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 32:f2f8ae34aadc 365 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 366
tnhnrl 32:f2f8ae34aadc 367 //record data every 5 seconds
tnhnrl 34:9b66c5188051 368 recordData(_state);
tnhnrl 32:f2f8ae34aadc 369
tnhnrl 16:3363b9f14913 370 break;
tnhnrl 16:3363b9f14913 371
tnhnrl 16:3363b9f14913 372 case FLOAT_LEVEL :
tnhnrl 16:3363b9f14913 373 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 374 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 375 pc().printf("\r\n\nstate: FLOAT_LEVEL\r\n");
tnhnrl 16:3363b9f14913 376 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 377 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 378 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 379
tnhnrl 16:3363b9f14913 380 // what needs to be started?
tnhnrl 16:3363b9f14913 381 bce().unpause();
tnhnrl 16:3363b9f14913 382 batt().unpause();
tnhnrl 16:3363b9f14913 383
tnhnrl 20:8987a9ae2bc7 384 // what are the commands?
tnhnrl 28:16c83a2fdefa 385 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 16:3363b9f14913 386 pitchLoop().setCommand(0.0);
tnhnrl 32:f2f8ae34aadc 387
tnhnrl 32:f2f8ae34aadc 388 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 389 //createNewFile();
tnhnrl 32:f2f8ae34aadc 390
tnhnrl 32:f2f8ae34aadc 391 //show that this is the start of a new float level sequence
tnhnrl 32:f2f8ae34aadc 392 recordState(_state);
tnhnrl 32:f2f8ae34aadc 393
tnhnrl 49:47ffa4feb6db 394 //triggers logger array
tnhnrl 32:f2f8ae34aadc 395 _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 396 recordData(_state);
tnhnrl 16:3363b9f14913 397 }
tnhnrl 20:8987a9ae2bc7 398
tnhnrl 16:3363b9f14913 399 // how exit?
tnhnrl 17:7c16b5671d0e 400 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 401 pc().printf("FL: timed out\r\n");
tnhnrl 21:38c8544db6f4 402 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 403 timer.reset();
tnhnrl 28:16c83a2fdefa 404 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 405 }
tnhnrl 28:16c83a2fdefa 406 else if (fabs(imu().getPitch() - pitchLoop().getCommand()) < fabs(_pitchTolerance)) { //current tolerance is 5 degrees
tnhnrl 28:16c83a2fdefa 407 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 408 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 409 timer.reset();
tnhnrl 28:16c83a2fdefa 410 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 411 }
tnhnrl 20:8987a9ae2bc7 412
tnhnrl 16:3363b9f14913 413 // what is active?
tnhnrl 16:3363b9f14913 414 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 415 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 416
tnhnrl 32:f2f8ae34aadc 417 //record data every 5 seconds
tnhnrl 34:9b66c5188051 418 recordData(_state);
tnhnrl 32:f2f8ae34aadc 419
tnhnrl 16:3363b9f14913 420 break;
tnhnrl 16:3363b9f14913 421
tnhnrl 16:3363b9f14913 422 case FLOAT_BROADCAST :
tnhnrl 16:3363b9f14913 423 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 424 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 425 pc().printf("\r\n\nstate: FLOAT_BROADCAST\r\n");
tnhnrl 16:3363b9f14913 426 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 427 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 428 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 429
tnhnrl 16:3363b9f14913 430 // what needs to be started?
tnhnrl 16:3363b9f14913 431 bce().unpause();
tnhnrl 16:3363b9f14913 432 batt().unpause();
tnhnrl 20:8987a9ae2bc7 433
tnhnrl 16:3363b9f14913 434 // what are the commands?
tnhnrl 55:f4ec445c42fe 435 bce().setPosition_mm(_bceFloatPosition); // 320.0
tnhnrl 55:f4ec445c42fe 436 batt().setPosition_mm(_battFloatPosition); // 73.0
tnhnrl 32:f2f8ae34aadc 437
tnhnrl 32:f2f8ae34aadc 438 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 439 //createNewFile();
tnhnrl 32:f2f8ae34aadc 440
tnhnrl 32:f2f8ae34aadc 441 //show that this is the start of a new float broadcast sequence
tnhnrl 32:f2f8ae34aadc 442 recordState(_state);
tnhnrl 32:f2f8ae34aadc 443
tnhnrl 55:f4ec445c42fe 444 //triggers logger arrayc
tnhnrl 32:f2f8ae34aadc 445 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 446 recordData(_state);
tnhnrl 16:3363b9f14913 447 }
tnhnrl 20:8987a9ae2bc7 448
tnhnrl 16:3363b9f14913 449 // how exit?
tnhnrl 17:7c16b5671d0e 450 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 451 pc().printf("FB: timed out\r\n");
tnhnrl 21:38c8544db6f4 452 _state = SIT_IDLE;
tnhnrl 16:3363b9f14913 453 timer.reset();
tnhnrl 32:f2f8ae34aadc 454
tnhnrl 32:f2f8ae34aadc 455 //stop recording data
tnhnrl 35:2f66ea4863d5 456 //mbedLogger().closeFile();
tnhnrl 32:f2f8ae34aadc 457
tnhnrl 28:16c83a2fdefa 458 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 459 }
tnhnrl 20:8987a9ae2bc7 460 else if ( (fabs(bce().getPosition_mm() - bce().getSetPosition_mm()) < bce().getDeadband()) and
tnhnrl 20:8987a9ae2bc7 461 (fabs(batt().getPosition_mm() - batt().getSetPosition_mm()) < batt().getDeadband()) ) {
tnhnrl 16:3363b9f14913 462 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 463 _state = SIT_IDLE;
tnhnrl 16:3363b9f14913 464 timer.reset();
tnhnrl 32:f2f8ae34aadc 465
tnhnrl 32:f2f8ae34aadc 466 //stop recording data
tnhnrl 35:2f66ea4863d5 467 //mbedLogger().closeFile();
tnhnrl 32:f2f8ae34aadc 468
tnhnrl 28:16c83a2fdefa 469 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 470 }
tnhnrl 20:8987a9ae2bc7 471
tnhnrl 20:8987a9ae2bc7 472 // what is active?
tnhnrl 49:47ffa4feb6db 473 pc().printf("FB: bce pos: %0.1f mm, batt pos: %0.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec] (CMD BCE: %0.1f BATT: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read(), bce().getSetPosition_mm(),batt().getSetPosition_mm());
tnhnrl 32:f2f8ae34aadc 474
tnhnrl 32:f2f8ae34aadc 475 //record data every 5 seconds
tnhnrl 34:9b66c5188051 476 recordData(_state);
tnhnrl 32:f2f8ae34aadc 477
tnhnrl 16:3363b9f14913 478 break;
tnhnrl 17:7c16b5671d0e 479
tnhnrl 17:7c16b5671d0e 480 case MULTI_DIVE :
tnhnrl 17:7c16b5671d0e 481 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 482 if (!_isTimeoutRunning) {
tnhnrl 17:7c16b5671d0e 483 pc().printf("\r\n\nstate: MULTI-DIVE\r\n");
tnhnrl 17:7c16b5671d0e 484 timer.reset(); // timer goes back to zero
tnhnrl 17:7c16b5671d0e 485 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 486 _isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 487
tnhnrl 17:7c16b5671d0e 488 // what needs to be started?
tnhnrl 17:7c16b5671d0e 489 bce().unpause();
tnhnrl 17:7c16b5671d0e 490 batt().unpause();
tnhnrl 17:7c16b5671d0e 491
tnhnrl 21:38c8544db6f4 492 //retrieve commands from structs (loaded from sequence.cfg file)
tnhnrl 32:f2f8ae34aadc 493 float sequence_depth_command = currentStateStruct.depth;
tnhnrl 32:f2f8ae34aadc 494 float sequence_pitch_command = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 495
tnhnrl 17:7c16b5671d0e 496 // what are the commands?
tnhnrl 32:f2f8ae34aadc 497 depthLoop().setCommand(sequence_depth_command);
tnhnrl 32:f2f8ae34aadc 498 pitchLoop().setCommand(sequence_pitch_command);
tnhnrl 21:38c8544db6f4 499 pc().printf("MULTI-DIVE: depth cmd: %3.1f ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 500
tnhnrl 32:f2f8ae34aadc 501 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 502 //createNewFile();
tnhnrl 32:f2f8ae34aadc 503
tnhnrl 32:f2f8ae34aadc 504 //show that this is the start of a new MULTI_DIVE sequence
tnhnrl 32:f2f8ae34aadc 505 recordState(_state);
tnhnrl 32:f2f8ae34aadc 506
tnhnrl 32:f2f8ae34aadc 507 //no max depth recording right now
tnhnrl 32:f2f8ae34aadc 508
tnhnrl 49:47ffa4feb6db 509 //triggers logger array
tnhnrl 32:f2f8ae34aadc 510 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 511 recordData(_state);
tnhnrl 17:7c16b5671d0e 512 }
tnhnrl 20:8987a9ae2bc7 513
tnhnrl 17:7c16b5671d0e 514 // how exit?
tnhnrl 17:7c16b5671d0e 515 if (timer > _timeout) {
tnhnrl 21:38c8544db6f4 516 pc().printf("\n\n\rMULTI-DIVE: timed out [time: %0.1f]\n\n\r", timer.read());
tnhnrl 21:38c8544db6f4 517 _state = MULTI_RISE; //new behavior 11/17/2017
tnhnrl 17:7c16b5671d0e 518 timer.reset();
tnhnrl 28:16c83a2fdefa 519 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 520 }
tnhnrl 17:7c16b5671d0e 521 else if (depthLoop().getPosition() > depthLoop().getCommand()) {
tnhnrl 17:7c16b5671d0e 522 pc().printf("MULTI-DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 21:38c8544db6f4 523 _state = MULTI_RISE;
tnhnrl 17:7c16b5671d0e 524 timer.reset();
tnhnrl 28:16c83a2fdefa 525 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 526 }
tnhnrl 20:8987a9ae2bc7 527
tnhnrl 17:7c16b5671d0e 528 // what is active?
tnhnrl 17:7c16b5671d0e 529 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 530 bce().setPosition_mm(depthLoop().getOutput());
tnhnrl 17:7c16b5671d0e 531 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 532
tnhnrl 32:f2f8ae34aadc 533 //record data every 5 seconds
tnhnrl 34:9b66c5188051 534 recordData(_state);
tnhnrl 32:f2f8ae34aadc 535
tnhnrl 17:7c16b5671d0e 536 break;
tnhnrl 17:7c16b5671d0e 537
tnhnrl 17:7c16b5671d0e 538 case MULTI_RISE :
tnhnrl 17:7c16b5671d0e 539 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 540 if (!_isTimeoutRunning) {
tnhnrl 17:7c16b5671d0e 541 pc().printf("\r\n\nstate: MULTI-RISE\r\n");
tnhnrl 17:7c16b5671d0e 542 timer.reset(); // timer goes back to zero
tnhnrl 17:7c16b5671d0e 543 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 544 _isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 545
tnhnrl 17:7c16b5671d0e 546 // what needs to be started?
tnhnrl 17:7c16b5671d0e 547 bce().unpause();
tnhnrl 17:7c16b5671d0e 548 batt().unpause();
tnhnrl 17:7c16b5671d0e 549
tnhnrl 17:7c16b5671d0e 550 //NEW: retrieve depth and pitch commands from config file struct
tnhnrl 17:7c16b5671d0e 551 // concept is to load this each time the multi-dive restarts
tnhnrl 17:7c16b5671d0e 552 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 553
tnhnrl 17:7c16b5671d0e 554 //retrieve just pitch command from struct
tnhnrl 32:f2f8ae34aadc 555 float sequence_pitch_command = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 556
tnhnrl 17:7c16b5671d0e 557 // what are the commands? (send back to 0.5 feet, not surface) // 11/21/2017
tnhnrl 17:7c16b5671d0e 558 depthLoop().setCommand(0.5);
tnhnrl 32:f2f8ae34aadc 559 pitchLoop().setCommand(-sequence_pitch_command);
tnhnrl 21:38c8544db6f4 560 pc().printf("MULTI-RISE: depth cmd: 0.0 ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 561
tnhnrl 32:f2f8ae34aadc 562 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 563 //createNewFile();
tnhnrl 32:f2f8ae34aadc 564
tnhnrl 32:f2f8ae34aadc 565 //show that this is the start of a new MULTI_DIVE sequence
tnhnrl 32:f2f8ae34aadc 566 recordState(_state);
tnhnrl 32:f2f8ae34aadc 567
tnhnrl 49:47ffa4feb6db 568 //triggers logger array
tnhnrl 32:f2f8ae34aadc 569 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 570 recordData(_state);
tnhnrl 17:7c16b5671d0e 571 }
tnhnrl 20:8987a9ae2bc7 572
tnhnrl 17:7c16b5671d0e 573 // how exit?
tnhnrl 17:7c16b5671d0e 574 if (timer > _timeout) {
tnhnrl 21:38c8544db6f4 575 pc().printf("MULTI-RISE: timed out [time: %0.1f]\n\n\r", timer.read());
tnhnrl 21:38c8544db6f4 576 _state = EMERGENCY_CLIMB;
tnhnrl 17:7c16b5671d0e 577 timer.reset();
tnhnrl 28:16c83a2fdefa 578 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 579
tnhnrl 17:7c16b5671d0e 580 //reset multi-dive sequence to start
tnhnrl 24:c7d9b5bf3829 581 _multi_dive_counter = 0;
tnhnrl 17:7c16b5671d0e 582 }
tnhnrl 20:8987a9ae2bc7 583 else if (depthLoop().getPosition() < 0.5) { // depth is less than 0.5 (zero is surface level)
tnhnrl 17:7c16b5671d0e 584 pc().printf("MULTI-RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 585
tnhnrl 17:7c16b5671d0e 586 //going to next state
tnhnrl 28:16c83a2fdefa 587 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 588
tnhnrl 17:7c16b5671d0e 589 //successful dive-rise sequence CONTINUES the multi-dive sequence
tnhnrl 24:c7d9b5bf3829 590 _multi_dive_counter++;
tnhnrl 17:7c16b5671d0e 591
tnhnrl 17:7c16b5671d0e 592 //UPDATE THE SEQUENCE DATA HERE
tnhnrl 17:7c16b5671d0e 593 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 594
tnhnrl 17:7c16b5671d0e 595 //check if this is the end of the dive sequence
tnhnrl 30:2964617e7676 596 //CHECK BEFORE ANYTHING ELSE that you have reached the "exit" state (FLOAT_BROADCAST)
tnhnrl 30:2964617e7676 597 if (currentStateStruct.state == FLOAT_BROADCAST) {
tnhnrl 28:16c83a2fdefa 598 _state = FLOAT_BROADCAST;
tnhnrl 17:7c16b5671d0e 599 }
tnhnrl 17:7c16b5671d0e 600
tnhnrl 17:7c16b5671d0e 601 else
tnhnrl 21:38c8544db6f4 602 _state = MULTI_DIVE;
tnhnrl 17:7c16b5671d0e 603
tnhnrl 24:c7d9b5bf3829 604 //have to stop this with the _multi_dive_counter variable!
tnhnrl 17:7c16b5671d0e 605 }
tnhnrl 20:8987a9ae2bc7 606
tnhnrl 20:8987a9ae2bc7 607 // what is active?
tnhnrl 17:7c16b5671d0e 608 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 609 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 17:7c16b5671d0e 610 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 611
tnhnrl 32:f2f8ae34aadc 612 //record data every 5 seconds
tnhnrl 34:9b66c5188051 613 recordData(_state);
tnhnrl 32:f2f8ae34aadc 614
tnhnrl 17:7c16b5671d0e 615 break;
tnhnrl 32:f2f8ae34aadc 616
tnhnrl 45:16b8162188ca 617 case TRANSMIT_LOG:
tnhnrl 32:f2f8ae34aadc 618 if (!_isTimeoutRunning) {
tnhnrl 45:16b8162188ca 619 pc().printf("\r\n\nstate: TRANSMIT_LOG\r\n");
tnhnrl 32:f2f8ae34aadc 620 timer.reset(); // timer goes back to zero
tnhnrl 32:f2f8ae34aadc 621 timer.start(); // background timer starts running
tnhnrl 32:f2f8ae34aadc 622 _isTimeoutRunning = true;
tnhnrl 32:f2f8ae34aadc 623
tnhnrl 45:16b8162188ca 624 mbedLogger().getNumberOfPacketsInCurrentLog(); //open the file, read the number of lines in the log file
tnhnrl 45:16b8162188ca 625 transmit_packet_number = mbedLogger().getNumberOfPackets();
tnhnrl 45:16b8162188ca 626
tnhnrl 45:16b8162188ca 627 //pc().printf("getNumberOfPacketsInCurrentLog is %d\n\r", transmit_packet_number);
tnhnrl 45:16b8162188ca 628 }
tnhnrl 45:16b8162188ca 629
tnhnrl 45:16b8162188ca 630 if (timer.read() > _timeout) {
tnhnrl 45:16b8162188ca 631 pc().printf("\n\rTRANSMIT_LOG: timed out!\n\r");
tnhnrl 45:16b8162188ca 632 _state = SIT_IDLE;
tnhnrl 45:16b8162188ca 633 timer.reset();
tnhnrl 45:16b8162188ca 634 _isTimeoutRunning = false;
tnhnrl 45:16b8162188ca 635
tnhnrl 45:16b8162188ca 636 mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading
tnhnrl 45:16b8162188ca 637 }
tnhnrl 45:16b8162188ca 638
tnhnrl 45:16b8162188ca 639 //IF THIS IS ZERO
tnhnrl 45:16b8162188ca 640 if (mbedLogger().currentPacketNumber() > transmit_packet_number) {
tnhnrl 45:16b8162188ca 641 pc().printf("mbedLogger().currentPacketNumber() > transmit_packet_number");
tnhnrl 45:16b8162188ca 642 _state = SIT_IDLE;
tnhnrl 45:16b8162188ca 643 timer.reset();
tnhnrl 45:16b8162188ca 644 _isTimeoutRunning = false;
tnhnrl 45:16b8162188ca 645
tnhnrl 45:16b8162188ca 646 mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading
tnhnrl 32:f2f8ae34aadc 647 }
tnhnrl 45:16b8162188ca 648
tnhnrl 45:16b8162188ca 649 if (mbedLogger().isTransmissionComplete()) {
tnhnrl 45:16b8162188ca 650 pc().printf("StateMachine isTransmissionComplete (true)\n\r");
tnhnrl 45:16b8162188ca 651 _state = SIT_IDLE;
tnhnrl 45:16b8162188ca 652 timer.reset();
tnhnrl 45:16b8162188ca 653 _isTimeoutRunning = false;
tnhnrl 45:16b8162188ca 654
tnhnrl 45:16b8162188ca 655 mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading
tnhnrl 45:16b8162188ca 656 }
tnhnrl 45:16b8162188ca 657
tnhnrl 32:f2f8ae34aadc 658
tnhnrl 45:16b8162188ca 659 // what is active? (no hardware should be active)
tnhnrl 45:16b8162188ca 660 mbedLogger().readTransmitPacketOneChar(); //led2 shows you pc readable
tnhnrl 45:16b8162188ca 661
tnhnrl 45:16b8162188ca 662 break;
tnhnrl 45:16b8162188ca 663
tnhnrl 45:16b8162188ca 664 case RECEIVE_SEQUENCE :
tnhnrl 45:16b8162188ca 665 pc().printf("state: RECEIVE_SEQUENCE\n\r");
tnhnrl 45:16b8162188ca 666
tnhnrl 45:16b8162188ca 667 if (!_isTimeoutRunning) {
tnhnrl 45:16b8162188ca 668 pc().printf("RECEIVE_SEQUENCE _isTimeoutRunning\r\n");
tnhnrl 45:16b8162188ca 669 timer.reset(); // timer goes back to zero
tnhnrl 45:16b8162188ca 670 timer.start(); // background timer starts running
tnhnrl 45:16b8162188ca 671 _isTimeoutRunning = true;
tnhnrl 45:16b8162188ca 672 }
tnhnrl 45:16b8162188ca 673
tnhnrl 34:9b66c5188051 674 if (timer.read() > _timeout) {
tnhnrl 45:16b8162188ca 675 pc().printf("RECEIVE_SEQUENCE: timed out!\n\r");
tnhnrl 34:9b66c5188051 676 _state = SIT_IDLE;
tnhnrl 34:9b66c5188051 677 timer.reset();
tnhnrl 32:f2f8ae34aadc 678 _isTimeoutRunning = false;
tnhnrl 32:f2f8ae34aadc 679 }
tnhnrl 32:f2f8ae34aadc 680
tnhnrl 45:16b8162188ca 681 // what is active?
tnhnrl 45:16b8162188ca 682 pc().printf("Receive sequence active?\n\r");
tnhnrl 32:f2f8ae34aadc 683
tnhnrl 32:f2f8ae34aadc 684 break;
tnhnrl 16:3363b9f14913 685
tnhnrl 16:3363b9f14913 686 default :
tnhnrl 17:7c16b5671d0e 687 pc().printf("DEBUG: SIT_IDLE\n\r");
tnhnrl 21:38c8544db6f4 688 _state = SIT_IDLE;
tnhnrl 28:16c83a2fdefa 689 }
tnhnrl 28:16c83a2fdefa 690
tnhnrl 28:16c83a2fdefa 691 //save the state to print to user
tnhnrl 28:16c83a2fdefa 692 if (_previous_state != _state) {
tnhnrl 28:16c83a2fdefa 693 _state_array[_state_array_counter] = _state; //save to state array
tnhnrl 28:16c83a2fdefa 694 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 695
tnhnrl 28:16c83a2fdefa 696 _previous_state = _state;
tnhnrl 28:16c83a2fdefa 697 }
tnhnrl 34:9b66c5188051 698
tnhnrl 34:9b66c5188051 699 return _state;
tnhnrl 34:9b66c5188051 700 // //if the state is SIT_IDLE, return 0 / false (for recording)
tnhnrl 34:9b66c5188051 701 // if (_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 702 // return 0;
tnhnrl 34:9b66c5188051 703 // else
tnhnrl 34:9b66c5188051 704 // return 1; //return true to indicate that you're recording
tnhnrl 16:3363b9f14913 705 }
tnhnrl 20:8987a9ae2bc7 706
tnhnrl 16:3363b9f14913 707 // output the keyboard menu for user's reference
tnhnrl 57:ec69651c8c21 708 void StateMachine::showSimpleMenu() {
tnhnrl 57:ec69651c8c21 709 // if (user_input == 'D') {
tnhnrl 57:ec69651c8c21 710 // _keyboard_state = DIVE;
tnhnrl 57:ec69651c8c21 711 // }
tnhnrl 57:ec69651c8c21 712 // else if (user_input == 'N') {
tnhnrl 57:ec69651c8c21 713 // _keyboard_state = FIND_NEUTRAL;
tnhnrl 57:ec69651c8c21 714 // }
tnhnrl 57:ec69651c8c21 715 // else if (user_input == 'L') {
tnhnrl 57:ec69651c8c21 716 // _keyboard_state = FLOAT_LEVEL;
tnhnrl 57:ec69651c8c21 717 // }
tnhnrl 57:ec69651c8c21 718 // else if (user_input == 'B') {
tnhnrl 57:ec69651c8c21 719 // _keyboard_state = FLOAT_BROADCAST;
tnhnrl 57:ec69651c8c21 720 // }
tnhnrl 57:ec69651c8c21 721 // else if (user_input == 'E') {
tnhnrl 57:ec69651c8c21 722 // _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 57:ec69651c8c21 723 // }
tnhnrl 57:ec69651c8c21 724 //
tnhnrl 57:ec69651c8c21 725 // // some debug tools below
tnhnrl 57:ec69651c8c21 726 // else if (user_input == 'P') {
tnhnrl 57:ec69651c8c21 727 // //Print current SD card log file
tnhnrl 57:ec69651c8c21 728 // //printCurrentSdLog();
tnhnrl 57:ec69651c8c21 729 // mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 57:ec69651c8c21 730 // }
tnhnrl 57:ec69651c8c21 731 // else if (user_input == 'O') {
tnhnrl 57:ec69651c8c21 732 // _keyboard_state = TRANSMIT_LOG; //Transmit data (work in progress)
tnhnrl 57:ec69651c8c21 733 // }
tnhnrl 57:ec69651c8c21 734 // else if (user_input == '~') {
tnhnrl 57:ec69651c8c21 735 // pc().printf("ERASING MBED LOG FILE\n\r");
tnhnrl 57:ec69651c8c21 736 // mbedLogger().eraseFile();
tnhnrl 57:ec69651c8c21 737
tnhnrl 57:ec69651c8c21 738 pc().printf("\r\r\n\nSIMPLE KEYBOARD MENU (06/14/2018):\r\r\n"); //make sure depth sensor tares itself on startup
tnhnrl 57:ec69651c8c21 739 pc().printf(" D to initiate dive cycle\r\n");
tnhnrl 57:ec69651c8c21 740 pc().printf(" J to float level\r\n");
tnhnrl 57:ec69651c8c21 741 pc().printf(" B to float at broadcast pitch\r\n");
tnhnrl 57:ec69651c8c21 742 pc().printf(" E to initiate emergency climb\r\n");
tnhnrl 57:ec69651c8c21 743 pc().printf(" P to print the current log file.\r\n");
tnhnrl 57:ec69651c8c21 744 pc().printf(" O to transmit current log file.\r\n");
tnhnrl 57:ec69651c8c21 745 pc().printf(" ~ to erase mbed log file. (clear before logging more than a few runs)\r\n");
tnhnrl 57:ec69651c8c21 746 pc().printf("[/] to change bce neutral position: %0.1f\r\n", _neutral_bce_pos_mm);
tnhnrl 57:ec69651c8c21 747 pc().printf("</> to change batt neutral position: %0.1f\r\n", _neutral_batt_pos_mm);
tnhnrl 57:ec69651c8c21 748 pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",_pitch_command);
tnhnrl 57:ec69651c8c21 749 pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",_depth_command);
tnhnrl 57:ec69651c8c21 750 pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout);
tnhnrl 57:ec69651c8c21 751 pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 57:ec69651c8c21 752 pc().printf(" ? to reset mbed\r\n");
tnhnrl 57:ec69651c8c21 753 pc().printf(" * (asterisk) to go to DEBUG menu\r\n");
tnhnrl 57:ec69651c8c21 754 }
tnhnrl 57:ec69651c8c21 755
tnhnrl 57:ec69651c8c21 756 void StateMachine::showDebugMenu() {
tnhnrl 57:ec69651c8c21 757 pc().printf("\r\r\n\nDEBUG MENU (06/14/2018):\r\r\n");
tnhnrl 55:f4ec445c42fe 758 pc().printf(" T to go into CHECK TUNING (This is on a timer! Uses NEUTRAL positions!)\r\n");
tnhnrl 16:3363b9f14913 759 pc().printf(" N to find neutral\r\n");
tnhnrl 17:7c16b5671d0e 760 pc().printf(" M to initiate multi-dive cycle\r\n");
tnhnrl 16:3363b9f14913 761 pc().printf(" D to initiate dive cycle\r\n");
tnhnrl 16:3363b9f14913 762 pc().printf(" R to initiate rise\r\n");
tnhnrl 57:ec69651c8c21 763 pc().printf(" J to float level\r\n");
tnhnrl 16:3363b9f14913 764 pc().printf(" B to float at broadcast pitch\r\n");
tnhnrl 16:3363b9f14913 765 pc().printf(" E to initiate emergency climb\r\n");
tnhnrl 49:47ffa4feb6db 766 pc().printf(" '|' to tare the depth sensor (vertical bar)\r\n");
tnhnrl 28:16c83a2fdefa 767 pc().printf(" Z to show FSM and sub-FSM states.\r\n");
tnhnrl 32:f2f8ae34aadc 768 pc().printf(" P to print the current log file.\r\n");
tnhnrl 32:f2f8ae34aadc 769 pc().printf(" X to print the list of log files.\r\n");
tnhnrl 49:47ffa4feb6db 770 pc().printf(" I to receive data.\r\n");
tnhnrl 49:47ffa4feb6db 771 pc().printf(" O to transmit data.\r\n");
tnhnrl 49:47ffa4feb6db 772 pc().printf(" ~ to erase mbed log file. (clear before logging more than a few runs)\r\n");
tnhnrl 32:f2f8ae34aadc 773 pc().printf("[/] to change bce neutral position: %0.1f\r\n", _neutral_bce_pos_mm);
tnhnrl 32:f2f8ae34aadc 774 pc().printf("</> to change batt neutral position: %0.1f\r\n", _neutral_batt_pos_mm);
tnhnrl 32:f2f8ae34aadc 775 pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",_pitch_command);
tnhnrl 32:f2f8ae34aadc 776 pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",_depth_command);
tnhnrl 17:7c16b5671d0e 777 pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout);
tnhnrl 16:3363b9f14913 778 pc().printf(" 1 BCE PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 779 pc().printf(" 2 BATT PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 780 pc().printf(" 3 Depth PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 781 pc().printf(" 4 Pitch PID sub-menu\r\n");
tnhnrl 49:47ffa4feb6db 782 pc().printf(" 5 MANUAL_TUNING sub-menu (does not have a timer!)\r\n");
tnhnrl 53:c0586fe62b01 783
tnhnrl 54:d4990fb68404 784 pc().printf(" 6 STREAM STATUS\r\n");
tnhnrl 53:c0586fe62b01 785 pc().printf(" 7 RUDDER SERVO settings menu\r\n");
tnhnrl 54:d4990fb68404 786 pc().printf(" 8 CHANNEL READINGS\r\n");
tnhnrl 55:f4ec445c42fe 787 pc().printf(" 9 POSITION READINGS\r\n");
tnhnrl 53:c0586fe62b01 788
tnhnrl 28:16c83a2fdefa 789 pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 16:3363b9f14913 790 pc().printf(" ? to reset mbed\r\n");
tnhnrl 57:ec69651c8c21 791 pc().printf(" * (asterisk) go to SIMPLE menu\r\n");
tnhnrl 16:3363b9f14913 792 }
tnhnrl 20:8987a9ae2bc7 793
tnhnrl 17:7c16b5671d0e 794 //Find Neutral sub finite state machine
tnhnrl 17:7c16b5671d0e 795 // Note: the sub-fsm only moves the pistons once at the start of each timer loop
tnhnrl 17:7c16b5671d0e 796 // (timer completes, move piston, timer completes, move piston, etc)
tnhnrl 28:16c83a2fdefa 797 int StateMachine::runNeutralStateMachine() {
tnhnrl 24:c7d9b5bf3829 798 switch (_substate) {
tnhnrl 20:8987a9ae2bc7 799 case NEUTRAL_SINKING :
tnhnrl 17:7c16b5671d0e 800 //start the 10 second timer
tnhnrl 28:16c83a2fdefa 801 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 802 _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 803
tnhnrl 39:58375ca6b6ff 804 pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f) (getSetPosition: %0.1f)\n\r", _neutral_timer, timer.read(), pitchLoop().getPosition(), bce().getSetPosition_mm());
tnhnrl 20:8987a9ae2bc7 805
tnhnrl 32:f2f8ae34aadc 806 // what are the commands? (BCE linear actuator active, no pitch movement)
tnhnrl 39:58375ca6b6ff 807 bce().setPosition_mm(bce().getSetPosition_mm() - 2.5); //Troy: There is some strange error where this has to be a hardcoded number.
tnhnrl 23:434f04ef1fad 808
tnhnrl 32:f2f8ae34aadc 809 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 810
tnhnrl 28:16c83a2fdefa 811 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 812 }
tnhnrl 20:8987a9ae2bc7 813
tnhnrl 20:8987a9ae2bc7 814 // how exit?
tnhnrl 20:8987a9ae2bc7 815 //once reached the travel limit, no need to keep trying, so exit
tnhnrl 25:249e4d56b27c 816 if (bce().getPosition_mm() <= 0) {
tnhnrl 25:249e4d56b27c 817 pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm());
tnhnrl 25:249e4d56b27c 818 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 819 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 25:249e4d56b27c 820 }
tnhnrl 20:8987a9ae2bc7 821 //once deeper than the commanded setpoint...
tnhnrl 32:f2f8ae34aadc 822 else if (depthLoop().getPosition() > _depth_command) {
tnhnrl 24:c7d9b5bf3829 823 _substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 28:16c83a2fdefa 824 _isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 20:8987a9ae2bc7 825 }
tnhnrl 20:8987a9ae2bc7 826
tnhnrl 20:8987a9ae2bc7 827 // what is active?
tnhnrl 20:8987a9ae2bc7 828 //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 829 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 830 pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", timer.read());
tnhnrl 17:7c16b5671d0e 831
tnhnrl 28:16c83a2fdefa 832 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 833 }
tnhnrl 39:58375ca6b6ff 834
tnhnrl 39:58375ca6b6ff 835 // what is active? (only the buoyancy engine moved every 5 seconds at start)
tnhnrl 39:58375ca6b6ff 836 pc().printf("BCE current pos: %0.1f mm (BCE setpoint: %0.1f mm) (current depth: %0.1f ft)\r", bce().getPosition_mm(),bce().getSetPosition_mm(),depthLoop().getPosition()); //debug
tnhnrl 17:7c16b5671d0e 837 break;
tnhnrl 17:7c16b5671d0e 838
tnhnrl 17:7c16b5671d0e 839 case NEUTRAL_SLOWLY_RISE:
tnhnrl 28:16c83a2fdefa 840 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 841 _neutral_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 842
tnhnrl 24:c7d9b5bf3829 843 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 844
tnhnrl 20:8987a9ae2bc7 845 // what are the commands?
tnhnrl 32:f2f8ae34aadc 846 //move piston at start of sequence (default: extend 2.0 mm)
tnhnrl 39:58375ca6b6ff 847 bce().setPosition_mm(bce().getSetPosition_mm() + 2.0); //no depth command
tnhnrl 23:434f04ef1fad 848
tnhnrl 23:434f04ef1fad 849 // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state
tnhnrl 23:434f04ef1fad 850 pitchLoop().setCommand(0.0);
tnhnrl 24:c7d9b5bf3829 851
tnhnrl 32:f2f8ae34aadc 852 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 853
tnhnrl 28:16c83a2fdefa 854 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 855 }
tnhnrl 17:7c16b5671d0e 856
tnhnrl 20:8987a9ae2bc7 857 // how exit?
tnhnrl 24:c7d9b5bf3829 858 //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit
tnhnrl 24:c7d9b5bf3829 859 if (bce().getSetPosition_mm() >= bce().getTravelLimit()) {
tnhnrl 24:c7d9b5bf3829 860 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 861 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 862 }
tnhnrl 17:7c16b5671d0e 863 //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration
tnhnrl 20:8987a9ae2bc7 864 else if (depthLoop().getVelocity() < 0) { //less than zero ft/s
tnhnrl 24:c7d9b5bf3829 865 pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 ft/s [time: %0.1f]\r\n", timer.read());
tnhnrl 24:c7d9b5bf3829 866 _substate = NEUTRAL_CHECK_PITCH;
tnhnrl 28:16c83a2fdefa 867 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 868 }
tnhnrl 17:7c16b5671d0e 869
tnhnrl 20:8987a9ae2bc7 870 // what is active?
tnhnrl 20:8987a9ae2bc7 871 //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 872 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 873 pc().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! [timer: %0.1f]\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 874
tnhnrl 28:16c83a2fdefa 875 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 876 }
tnhnrl 23:434f04ef1fad 877
tnhnrl 32:f2f8ae34aadc 878 // what is active? (only the buoyancy engine moved every 5 seconds)
tnhnrl 32:f2f8ae34aadc 879 pc().printf("depthLoop getOutput: %0.1f\r", depthLoop().getOutput()); //debug
tnhnrl 32:f2f8ae34aadc 880 bce().setPosition_mm(depthLoop().getOutput()); // (DID NOT WORK ON BENCH)
tnhnrl 17:7c16b5671d0e 881 break;
tnhnrl 17:7c16b5671d0e 882
danstrider 22:a10ee088403b 883 case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired
danstrider 22:a10ee088403b 884 // start local state timer and init any other one-shot actions
tnhnrl 23:434f04ef1fad 885
tnhnrl 28:16c83a2fdefa 886 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 887 _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds
tnhnrl 24:c7d9b5bf3829 888 pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read());
danstrider 22:a10ee088403b 889
tnhnrl 32:f2f8ae34aadc 890 // what are the commands? (default: retract or extend 0.5 mm)
tnhnrl 24:c7d9b5bf3829 891 if (pitchLoop().getPosition() > 2) { // nose is high
tnhnrl 39:58375ca6b6ff 892 batt().setPosition_mm(batt().getSetPosition_mm() + 0.5); // move battery forward (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 893 pc().printf("\n\rNeutral Check Pitch: moving battery FWD in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 894 }
tnhnrl 24:c7d9b5bf3829 895 else if (pitchLoop().getPosition() < -2) { // nose is low
tnhnrl 39:58375ca6b6ff 896 batt().setPosition_mm(batt().getSetPosition_mm() - 0.5); // move battery aft (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 897 pc().printf("\n\rNeutral Check Pitch: moving battery AFT in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 898 }
tnhnrl 24:c7d9b5bf3829 899
tnhnrl 28:16c83a2fdefa 900 _isSubStateTimerRunning = true; //disable this block after one iteration
danstrider 22:a10ee088403b 901 }
tnhnrl 20:8987a9ae2bc7 902
tnhnrl 28:16c83a2fdefa 903 // how exit?
tnhnrl 20:8987a9ae2bc7 904 //pitch angle and pitch rate within small tolerance
tnhnrl 20:8987a9ae2bc7 905 //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 23:434f04ef1fad 906 if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) {
tnhnrl 24:c7d9b5bf3829 907 pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug
danstrider 22:a10ee088403b 908 // found level, but don't need to save anything this time
tnhnrl 23:434f04ef1fad 909
tnhnrl 28:16c83a2fdefa 910 if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug
tnhnrl 28:16c83a2fdefa 911 _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 912 }
tnhnrl 28:16c83a2fdefa 913
tnhnrl 23:434f04ef1fad 914 // found level and at depth too, so save it all now
tnhnrl 32:f2f8ae34aadc 915 if (_substate == NEUTRAL_CHECK_PITCH) {
danstrider 22:a10ee088403b 916 //save positions locally
danstrider 22:a10ee088403b 917 _neutral_batt_pos_mm = batt().getPosition_mm();
danstrider 22:a10ee088403b 918 _neutral_bce_pos_mm = bce().getPosition_mm();
danstrider 22:a10ee088403b 919
danstrider 22:a10ee088403b 920 //set the neutral positions in each outer loop
danstrider 22:a10ee088403b 921 depthLoop().setOutputOffset(_neutral_bce_pos_mm);
danstrider 22:a10ee088403b 922 pitchLoop().setOutputOffset(_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 923
danstrider 22:a10ee088403b 924 // save into the depth.txt and pitch.txt files
danstrider 22:a10ee088403b 925 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset
danstrider 22:a10ee088403b 926 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset
danstrider 22:a10ee088403b 927
tnhnrl 32:f2f8ae34aadc 928 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 929
tnhnrl 24:c7d9b5bf3829 930 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 931 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 24:c7d9b5bf3829 932 }
tnhnrl 24:c7d9b5bf3829 933
tnhnrl 24:c7d9b5bf3829 934 else {
tnhnrl 24:c7d9b5bf3829 935 pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r");
tnhnrl 24:c7d9b5bf3829 936 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 937 }
tnhnrl 17:7c16b5671d0e 938 }
danstrider 22:a10ee088403b 939
danstrider 22:a10ee088403b 940 // what is active?
danstrider 22:a10ee088403b 941 //once timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 942 if (timer.read() >= _neutral_timer) {
danstrider 22:a10ee088403b 943 pc().printf("\r\n\nlevel timer COMPLETE!");
danstrider 22:a10ee088403b 944 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 945 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
danstrider 22:a10ee088403b 946 }
tnhnrl 17:7c16b5671d0e 947 break;
danstrider 22:a10ee088403b 948
danstrider 22:a10ee088403b 949 //this state could be removed, it is only used as a transition but is needed to stop entering this function
danstrider 22:a10ee088403b 950 case NEUTRAL_EXIT :
tnhnrl 23:434f04ef1fad 951 pc().printf("substate: NEUTRAL_EXIT\n\r");
tnhnrl 20:8987a9ae2bc7 952 break;
tnhnrl 21:38c8544db6f4 953
danstrider 22:a10ee088403b 954 default :
tnhnrl 24:c7d9b5bf3829 955 pc().printf("how did we get to substate: default?\n\r"); //debug
tnhnrl 23:434f04ef1fad 956 //a default within the sub-state machine
tnhnrl 24:c7d9b5bf3829 957 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 958 break;
tnhnrl 17:7c16b5671d0e 959 }
tnhnrl 20:8987a9ae2bc7 960
tnhnrl 30:2964617e7676 961 // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence)
tnhnrl 24:c7d9b5bf3829 962 if (_substate == NEUTRAL_EXIT) {
tnhnrl 24:c7d9b5bf3829 963 pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r");
tnhnrl 24:c7d9b5bf3829 964
tnhnrl 30:2964617e7676 965 //reset internal sub-state back to first entry conditions (first state is immediately sinking)
tnhnrl 30:2964617e7676 966 _substate = NEUTRAL_SINKING;
tnhnrl 28:16c83a2fdefa 967 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 21:38c8544db6f4 968
tnhnrl 24:c7d9b5bf3829 969 //record sub-states to view after sequence
tnhnrl 30:2964617e7676 970 _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array
tnhnrl 28:16c83a2fdefa 971 _substate_array_counter++;
tnhnrl 23:434f04ef1fad 972
tnhnrl 24:c7d9b5bf3829 973 //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit)
tnhnrl 24:c7d9b5bf3829 974 _previous_substate = -1;
tnhnrl 24:c7d9b5bf3829 975
tnhnrl 24:c7d9b5bf3829 976 //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 24:c7d9b5bf3829 977 return NEUTRAL_EXIT; // message to calling function we just exited
tnhnrl 21:38c8544db6f4 978 }
tnhnrl 23:434f04ef1fad 979 else {
tnhnrl 24:c7d9b5bf3829 980 //record sub-states to view after sequence (when changed)
tnhnrl 24:c7d9b5bf3829 981 if (_previous_substate != _substate) {
tnhnrl 28:16c83a2fdefa 982 _substate_array[_substate_array_counter] = _substate; //save current state to state array
tnhnrl 28:16c83a2fdefa 983 _substate_array_counter++;
tnhnrl 24:c7d9b5bf3829 984
tnhnrl 24:c7d9b5bf3829 985 //record the current substate for comparison
tnhnrl 24:c7d9b5bf3829 986 _previous_substate = _substate;
tnhnrl 24:c7d9b5bf3829 987 }
tnhnrl 24:c7d9b5bf3829 988
tnhnrl 24:c7d9b5bf3829 989 return _substate; // message to calling function of what sub-state it's in
tnhnrl 23:434f04ef1fad 990 }
tnhnrl 17:7c16b5671d0e 991 }
tnhnrl 20:8987a9ae2bc7 992
tnhnrl 57:ec69651c8c21 993 /* keyboard runs independently of the state machine, handling one key at a time
tnhnrl 57:ec69651c8c21 994 keyboard updates the desired _keyboard_state that is used in the state machine
tnhnrl 57:ec69651c8c21 995 and only allows input when the state is "idle" */
tnhnrl 57:ec69651c8c21 996
tnhnrl 17:7c16b5671d0e 997 void StateMachine::keyboard() {
tnhnrl 57:ec69651c8c21 998 char user_input;
tnhnrl 20:8987a9ae2bc7 999
tnhnrl 16:3363b9f14913 1000 // check keyboard and make settings changes as requested
tnhnrl 17:7c16b5671d0e 1001 // states can be changed only at the start of a sequence (when the system is in SIT_IDLE)
tnhnrl 21:38c8544db6f4 1002
tnhnrl 37:357e98a929cc 1003 //TEST
tnhnrl 37:357e98a929cc 1004 int _keyboard_state = 0; //made this a local variable because it was retaining the last keyboard state
tnhnrl 21:38c8544db6f4 1005
tnhnrl 28:16c83a2fdefa 1006 if (pc().readable() && (_state == SIT_IDLE || _state == KEYBOARD)) {
tnhnrl 16:3363b9f14913 1007 // get the key
tnhnrl 57:ec69651c8c21 1008 user_input = pc().getc();
tnhnrl 17:7c16b5671d0e 1009
tnhnrl 28:16c83a2fdefa 1010 //record that the keyboard was used
tnhnrl 28:16c83a2fdefa 1011 _state_array[_state_array_counter] = KEYBOARD;
tnhnrl 28:16c83a2fdefa 1012 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 1013
tnhnrl 21:38c8544db6f4 1014 // keyboard has to reset timer each time it's used
tnhnrl 28:16c83a2fdefa 1015 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 1016
tnhnrl 16:3363b9f14913 1017 // check command against desired control buttons
tnhnrl 57:ec69651c8c21 1018
tnhnrl 57:ec69651c8c21 1019 /***************************** DEBUG MENU *****************************/
tnhnrl 57:ec69651c8c21 1020 if (_debug_menu_on) {
tnhnrl 17:7c16b5671d0e 1021
tnhnrl 57:ec69651c8c21 1022 if (user_input == 'D') {
tnhnrl 57:ec69651c8c21 1023 _keyboard_state = DIVE;
tnhnrl 57:ec69651c8c21 1024 }
tnhnrl 57:ec69651c8c21 1025 else if (user_input == 'N') {
tnhnrl 57:ec69651c8c21 1026 _keyboard_state = FIND_NEUTRAL;
tnhnrl 57:ec69651c8c21 1027 }
tnhnrl 57:ec69651c8c21 1028 else if (user_input == 'M') {
tnhnrl 57:ec69651c8c21 1029 //currently does not run if there is no file.
tnhnrl 57:ec69651c8c21 1030
tnhnrl 57:ec69651c8c21 1031 //need to add method to Sequence Controller that returns -1
tnhnrl 57:ec69651c8c21 1032 // or some check that insures you cannot run the dive sequence without a file
tnhnrl 57:ec69651c8c21 1033
tnhnrl 57:ec69651c8c21 1034 stateMachine().getDiveSequence(); //get first sequence on keyboard press
tnhnrl 57:ec69651c8c21 1035 _keyboard_state = currentStateStruct.state;
tnhnrl 57:ec69651c8c21 1036
tnhnrl 57:ec69651c8c21 1037 pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles
tnhnrl 57:ec69651c8c21 1038 }
tnhnrl 57:ec69651c8c21 1039 else if (user_input == 'R') {
tnhnrl 57:ec69651c8c21 1040 _keyboard_state = RISE;
tnhnrl 57:ec69651c8c21 1041 }
tnhnrl 57:ec69651c8c21 1042 else if (user_input == 'J') {
tnhnrl 57:ec69651c8c21 1043 _keyboard_state = FLOAT_LEVEL;
tnhnrl 57:ec69651c8c21 1044 }
tnhnrl 57:ec69651c8c21 1045 else if (user_input == 'B') {
tnhnrl 57:ec69651c8c21 1046 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 57:ec69651c8c21 1047 }
tnhnrl 57:ec69651c8c21 1048 else if (user_input == 'E') {
tnhnrl 57:ec69651c8c21 1049 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 57:ec69651c8c21 1050 }
tnhnrl 17:7c16b5671d0e 1051
tnhnrl 57:ec69651c8c21 1052 else if (user_input == 'T') {
tnhnrl 57:ec69651c8c21 1053 _keyboard_state = CHECK_TUNING;
tnhnrl 57:ec69651c8c21 1054 }
tnhnrl 17:7c16b5671d0e 1055
tnhnrl 57:ec69651c8c21 1056 // some debug tools below
tnhnrl 57:ec69651c8c21 1057 else if (user_input == 'P') {
tnhnrl 57:ec69651c8c21 1058 //Print current SD card log file
tnhnrl 57:ec69651c8c21 1059 //printCurrentSdLog();
tnhnrl 57:ec69651c8c21 1060 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 57:ec69651c8c21 1061 }
tnhnrl 57:ec69651c8c21 1062 else if (user_input == 'X') {
tnhnrl 57:ec69651c8c21 1063 mbedLogger().printMbedDirectory(); //print all log files to the screen
tnhnrl 57:ec69651c8c21 1064 }
tnhnrl 57:ec69651c8c21 1065 else if (user_input == 'O') {
tnhnrl 57:ec69651c8c21 1066 _keyboard_state = TRANSMIT_LOG; //Transmit data (work in progress)
tnhnrl 57:ec69651c8c21 1067 }
tnhnrl 57:ec69651c8c21 1068 else if (user_input == 'I') {
tnhnrl 57:ec69651c8c21 1069 mbedLogger().receiveMissionDataWithTicker(); //receive sequence.txt files
tnhnrl 57:ec69651c8c21 1070 }
tnhnrl 57:ec69651c8c21 1071 else if (user_input == '~') {
tnhnrl 57:ec69651c8c21 1072 pc().printf("ERASING MBED LOG FILE\n\r");
tnhnrl 57:ec69651c8c21 1073 mbedLogger().eraseFile();
tnhnrl 57:ec69651c8c21 1074 }
tnhnrl 57:ec69651c8c21 1075 else if (user_input == 'Z') {
tnhnrl 57:ec69651c8c21 1076 pc().printf("FSG FSM States: \n\r");
tnhnrl 57:ec69651c8c21 1077 string string_state;
tnhnrl 57:ec69651c8c21 1078
tnhnrl 57:ec69651c8c21 1079 for (int i = 0; i < _state_array_counter; i++) {
tnhnrl 57:ec69651c8c21 1080 if (_state_array[i] == SIT_IDLE)
tnhnrl 57:ec69651c8c21 1081 string_state = "SIT_IDLE <END>";
tnhnrl 57:ec69651c8c21 1082 else if (_state_array[i] == FIND_NEUTRAL)
tnhnrl 57:ec69651c8c21 1083 string_state = "FIND_NEUTRAL";
tnhnrl 57:ec69651c8c21 1084 else if (_state_array[i] == DIVE)
tnhnrl 57:ec69651c8c21 1085 string_state = "DIVE";
tnhnrl 57:ec69651c8c21 1086 else if (_state_array[i] == RISE)
tnhnrl 57:ec69651c8c21 1087 string_state = "RISE";
tnhnrl 57:ec69651c8c21 1088 else if (_state_array[i] == FLOAT_LEVEL)
tnhnrl 57:ec69651c8c21 1089 string_state = "FLOAT_LEVEL";
tnhnrl 57:ec69651c8c21 1090 else if (_state_array[i] == FLOAT_BROADCAST)
tnhnrl 57:ec69651c8c21 1091 string_state = "FLOAT_BROADCAST";
tnhnrl 57:ec69651c8c21 1092 else if (_state_array[i] == EMERGENCY_CLIMB)
tnhnrl 57:ec69651c8c21 1093 string_state = "EMERGENCY_CLIMB";
tnhnrl 57:ec69651c8c21 1094 else if (_state_array[i] == MULTI_DIVE)
tnhnrl 57:ec69651c8c21 1095 string_state = "MULTI_DIVE";
tnhnrl 57:ec69651c8c21 1096 else if (_state_array[i] == MULTI_RISE)
tnhnrl 57:ec69651c8c21 1097 string_state = "MULTI_RISE";
tnhnrl 57:ec69651c8c21 1098 else if (_state_array[i] == KEYBOARD)
tnhnrl 57:ec69651c8c21 1099 string_state = "KEYBOARD";
tnhnrl 57:ec69651c8c21 1100 pc().printf("State #%d: %d (%s)\n\r", i, _state_array[i], string_state.c_str());
tnhnrl 57:ec69651c8c21 1101 }
tnhnrl 57:ec69651c8c21 1102
tnhnrl 57:ec69651c8c21 1103 pc().printf("\n\rNeutral sub-FSM States: \n\r");
tnhnrl 57:ec69651c8c21 1104 string string_substate;
tnhnrl 57:ec69651c8c21 1105
tnhnrl 57:ec69651c8c21 1106 for (int i = 0; i < _substate_array_counter; i++) {
tnhnrl 57:ec69651c8c21 1107 if (_substate_array[i] == NEUTRAL_SINKING)
tnhnrl 57:ec69651c8c21 1108 string_substate = "NEUTRAL_SINKING";
tnhnrl 57:ec69651c8c21 1109 else if (_substate_array[i] == NEUTRAL_SLOWLY_RISE)
tnhnrl 57:ec69651c8c21 1110 string_substate = "NEUTRAL_SLOWLY_RISE";
tnhnrl 57:ec69651c8c21 1111 else if (_substate_array[i] == NEUTRAL_CHECK_PITCH)
tnhnrl 57:ec69651c8c21 1112 string_substate = "NEUTRAL_CHECK_PITCH";
tnhnrl 57:ec69651c8c21 1113 else if (_substate_array[i] == NEUTRAL_EXIT)
tnhnrl 57:ec69651c8c21 1114 string_substate = "NEUTRAL_EXIT <-- ";
tnhnrl 57:ec69651c8c21 1115 else if (_substate_array[i] == EMERGENCY_CLIMB)
tnhnrl 57:ec69651c8c21 1116 string_substate = " -- > EMERGENCY_CLIMB <-- ";
tnhnrl 57:ec69651c8c21 1117 pc().printf("Neutral Substate #%d: %d (%s)\n\r", i, _state_array[i], string_substate.c_str());
tnhnrl 57:ec69651c8c21 1118 }
tnhnrl 57:ec69651c8c21 1119 pc().printf("\n\r"); //make space between printouts
tnhnrl 57:ec69651c8c21 1120 }
tnhnrl 57:ec69651c8c21 1121 else if (user_input == '|') {
tnhnrl 57:ec69651c8c21 1122 pc().printf("taring depth sensor\r\n");
tnhnrl 57:ec69651c8c21 1123 pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 57:ec69651c8c21 1124 wait(0.1);
tnhnrl 57:ec69651c8c21 1125 depth().tare(); // tares to ambient (do on surface)
tnhnrl 57:ec69651c8c21 1126 pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 57:ec69651c8c21 1127 }
tnhnrl 28:16c83a2fdefa 1128
tnhnrl 57:ec69651c8c21 1129 else if (user_input == '[' or user_input == '{') {
tnhnrl 57:ec69651c8c21 1130 _neutral_bce_pos_mm = depthLoop().getOutputOffset() - 1;
tnhnrl 57:ec69651c8c21 1131 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // decrease the bce neutral setpoint
tnhnrl 57:ec69651c8c21 1132 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1133 // save neutral depth value to config file
tnhnrl 57:ec69651c8c21 1134 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 57:ec69651c8c21 1135 }
tnhnrl 57:ec69651c8c21 1136 else if (user_input == ']' or user_input == '}') {
tnhnrl 57:ec69651c8c21 1137 _neutral_bce_pos_mm = depthLoop().getOutputOffset() + 1;
tnhnrl 57:ec69651c8c21 1138 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // increase the bce neutral setpoint
tnhnrl 57:ec69651c8c21 1139 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1140 // save neutral depth value to config file
tnhnrl 57:ec69651c8c21 1141 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 57:ec69651c8c21 1142 }
tnhnrl 57:ec69651c8c21 1143 else if (user_input == '<' or user_input == ',') {
tnhnrl 57:ec69651c8c21 1144 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() - 1;
tnhnrl 57:ec69651c8c21 1145 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // decrease the batt neutral setpoint
tnhnrl 57:ec69651c8c21 1146 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1147 // save neutral pitch value to config file
tnhnrl 57:ec69651c8c21 1148 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 57:ec69651c8c21 1149 }
tnhnrl 57:ec69651c8c21 1150 else if (user_input == '>' or user_input == '.') {
tnhnrl 57:ec69651c8c21 1151 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() + 1;
tnhnrl 57:ec69651c8c21 1152 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // increase the batt neutral setpoint
tnhnrl 57:ec69651c8c21 1153 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1154 // save neutral pitch value to config file
tnhnrl 57:ec69651c8c21 1155 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 28:16c83a2fdefa 1156 }
tnhnrl 28:16c83a2fdefa 1157
tnhnrl 57:ec69651c8c21 1158 else if (user_input == '?') {
tnhnrl 57:ec69651c8c21 1159 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 57:ec69651c8c21 1160 wait(0.5);
tnhnrl 57:ec69651c8c21 1161 mbed_reset();
tnhnrl 57:ec69651c8c21 1162 }
tnhnrl 57:ec69651c8c21 1163
tnhnrl 57:ec69651c8c21 1164 // change settings
tnhnrl 57:ec69651c8c21 1165 else if (user_input == 'Q' or user_input == 'q') {
tnhnrl 57:ec69651c8c21 1166 _pitch_command -= 0.5; //decrement the pitch setpoint
tnhnrl 57:ec69651c8c21 1167 pitchLoop().setCommand(_pitch_command);
tnhnrl 57:ec69651c8c21 1168 pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand());
tnhnrl 57:ec69651c8c21 1169 }
tnhnrl 57:ec69651c8c21 1170 else if (user_input == 'W' or user_input == 'w') {
tnhnrl 57:ec69651c8c21 1171 _pitch_command += 0.5; //increment the pitch setpoint
tnhnrl 57:ec69651c8c21 1172 pitchLoop().setCommand(_pitch_command);
tnhnrl 57:ec69651c8c21 1173 pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand());
tnhnrl 57:ec69651c8c21 1174 }
tnhnrl 57:ec69651c8c21 1175 else if (user_input == 'A' or user_input == 'a') {
tnhnrl 57:ec69651c8c21 1176 _depth_command -= 0.5; //decrement the depth setpoint
tnhnrl 57:ec69651c8c21 1177 depthLoop().setCommand(_depth_command);
tnhnrl 57:ec69651c8c21 1178 pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand());
tnhnrl 57:ec69651c8c21 1179 }
tnhnrl 57:ec69651c8c21 1180 else if (user_input == 'S' or user_input == 's') {
tnhnrl 57:ec69651c8c21 1181 _depth_command += 0.5; //increment the depth setpoint
tnhnrl 57:ec69651c8c21 1182 depthLoop().setCommand(_depth_command);
tnhnrl 57:ec69651c8c21 1183 pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand());
tnhnrl 57:ec69651c8c21 1184 }
tnhnrl 57:ec69651c8c21 1185 else if (user_input == '-') {
tnhnrl 57:ec69651c8c21 1186 _timeout -= 10.0; //decrement the timeout
tnhnrl 57:ec69651c8c21 1187 pc().printf(">>> timeout decreased: %d\r\n", _timeout);
tnhnrl 57:ec69651c8c21 1188 }
tnhnrl 57:ec69651c8c21 1189 else if (user_input == '=' or user_input == '+') {
tnhnrl 57:ec69651c8c21 1190 _timeout += 10.0; //increment the timeout
tnhnrl 57:ec69651c8c21 1191 pc().printf(">>> timeout increased: %d\r\n", _timeout);
tnhnrl 57:ec69651c8c21 1192 }
tnhnrl 28:16c83a2fdefa 1193
tnhnrl 57:ec69651c8c21 1194 // go to tuning sub-menu
tnhnrl 57:ec69651c8c21 1195 else if (user_input == '5') {
tnhnrl 57:ec69651c8c21 1196 keyboard_menu_MANUAL_TUNING();
tnhnrl 57:ec69651c8c21 1197 }
tnhnrl 57:ec69651c8c21 1198
tnhnrl 57:ec69651c8c21 1199 else if (user_input == '6') {
tnhnrl 57:ec69651c8c21 1200 keyboard_menu_STREAM_STATUS();
tnhnrl 57:ec69651c8c21 1201 }
tnhnrl 57:ec69651c8c21 1202
tnhnrl 57:ec69651c8c21 1203 else if (user_input == '7') {
tnhnrl 57:ec69651c8c21 1204 keyboard_menu_RUDDER_SERVO_settings();
tnhnrl 57:ec69651c8c21 1205 }
tnhnrl 57:ec69651c8c21 1206
tnhnrl 57:ec69651c8c21 1207 else if (user_input == '8') {
tnhnrl 57:ec69651c8c21 1208 keyboard_menu_CHANNEL_READINGS();
tnhnrl 57:ec69651c8c21 1209 }
tnhnrl 57:ec69651c8c21 1210 else if (user_input == '9') {
tnhnrl 57:ec69651c8c21 1211 keyboard_menu_POSITION_READINGS();
tnhnrl 57:ec69651c8c21 1212 }
tnhnrl 57:ec69651c8c21 1213
tnhnrl 57:ec69651c8c21 1214 // go to sub-menus for the PID gains (this is blocking)
tnhnrl 57:ec69651c8c21 1215 else if (user_input == '1') {
tnhnrl 57:ec69651c8c21 1216 keyboard_menu_BCE_PID_settings();
tnhnrl 57:ec69651c8c21 1217 }
tnhnrl 57:ec69651c8c21 1218 else if (user_input == '2') {
tnhnrl 57:ec69651c8c21 1219 keyboard_menu_BATT_PID_settings();
tnhnrl 57:ec69651c8c21 1220 }
tnhnrl 57:ec69651c8c21 1221 else if (user_input == '3') {
tnhnrl 57:ec69651c8c21 1222 keyboard_menu_DEPTH_PID_settings();
tnhnrl 57:ec69651c8c21 1223 }
tnhnrl 57:ec69651c8c21 1224 else if (user_input == '4') {
tnhnrl 57:ec69651c8c21 1225 keyboard_menu_PITCH_PID_settings();
tnhnrl 28:16c83a2fdefa 1226 }
tnhnrl 57:ec69651c8c21 1227
tnhnrl 57:ec69651c8c21 1228 else if (user_input == 'C' or user_input == 'c') {
tnhnrl 57:ec69651c8c21 1229
tnhnrl 57:ec69651c8c21 1230 pc().printf("\n\n\rCURRENT STATUS AND PARAMETERS:\n\r");
tnhnrl 57:ec69651c8c21 1231
tnhnrl 57:ec69651c8c21 1232 // Testing out ADC
tnhnrl 57:ec69651c8c21 1233 float vref = 5.6;
tnhnrl 57:ec69651c8c21 1234 float vmeasured = 0;
tnhnrl 57:ec69651c8c21 1235 unsigned int raw = adc().readCh5();
tnhnrl 57:ec69651c8c21 1236 vmeasured = ((float)raw)/4095.0*vref;
tnhnrl 57:ec69651c8c21 1237 pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
tnhnrl 57:ec69651c8c21 1238 pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
tnhnrl 57:ec69651c8c21 1239 pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
tnhnrl 57:ec69651c8c21 1240 pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
tnhnrl 57:ec69651c8c21 1241 pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
tnhnrl 57:ec69651c8c21 1242 pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
tnhnrl 57:ec69651c8c21 1243 pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
tnhnrl 57:ec69651c8c21 1244 pc().printf("raw board current: %d \r\n",adc().readCh7());
tnhnrl 57:ec69651c8c21 1245 pc().printf("raw BCE limit switch: %d \r\n",bce().getSwitch());
tnhnrl 57:ec69651c8c21 1246 pc().printf("raw BMM limit switch: %d \r\n",batt().getSwitch());
tnhnrl 57:ec69651c8c21 1247 pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
tnhnrl 57:ec69651c8c21 1248 // End of ADC Test
tnhnrl 57:ec69651c8c21 1249
tnhnrl 57:ec69651c8c21 1250 pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 57:ec69651c8c21 1251 pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 57:ec69651c8c21 1252 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 57:ec69651c8c21 1253 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 57:ec69651c8c21 1254 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 57:ec69651c8c21 1255 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 57:ec69651c8c21 1256 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 57:ec69651c8c21 1257 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 57:ec69651c8c21 1258
tnhnrl 57:ec69651c8c21 1259 pc().printf("\n\rNeutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
tnhnrl 57:ec69651c8c21 1260 pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1261 pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1262 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 57:ec69651c8c21 1263
tnhnrl 57:ec69651c8c21 1264 pc().printf("\n\r");
tnhnrl 57:ec69651c8c21 1265 pc().printf("bce P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %6.1f mm, slope %0.5f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
tnhnrl 57:ec69651c8c21 1266 pc().printf("batt P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %6.1f mm, slope %0.5f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
tnhnrl 57:ec69651c8c21 1267 pc().printf("depth P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1268 pc().printf("pitch P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1269 }
tnhnrl 57:ec69651c8c21 1270
tnhnrl 57:ec69651c8c21 1271 //POSITION DIVE COMMANDS
tnhnrl 57:ec69651c8c21 1272 else if (user_input == 'k') {
tnhnrl 57:ec69651c8c21 1273 _BCE_position_command -= 1.0;
tnhnrl 57:ec69651c8c21 1274 pc().printf("Decreased BCE CMD to %0.1f\n\r", _BCE_position_command);
tnhnrl 57:ec69651c8c21 1275 }
tnhnrl 57:ec69651c8c21 1276 else if (user_input == 'l') {
tnhnrl 57:ec69651c8c21 1277 _BCE_position_command += 1.0;
tnhnrl 57:ec69651c8c21 1278 pc().printf("Increased BCE CMD to %0.1f\n\r", _BCE_position_command);
tnhnrl 57:ec69651c8c21 1279 }
tnhnrl 57:ec69651c8c21 1280 else if (user_input == ';') {
tnhnrl 57:ec69651c8c21 1281 _BMM_position_command -= 1.0;
tnhnrl 57:ec69651c8c21 1282 pc().printf("Decreased BMM CMD to %0.1f\n\r", _BMM_position_command);
tnhnrl 57:ec69651c8c21 1283 }
tnhnrl 57:ec69651c8c21 1284 else if (user_input == '\'') {
tnhnrl 57:ec69651c8c21 1285 _BMM_position_command += 1.0;
tnhnrl 57:ec69651c8c21 1286 pc().printf("Increased BMM CMD to %0.1f\n\r", _BMM_position_command);
tnhnrl 57:ec69651c8c21 1287 }
tnhnrl 57:ec69651c8c21 1288 //POSITION DIVE COMMANDS
tnhnrl 57:ec69651c8c21 1289
tnhnrl 57:ec69651c8c21 1290 else if (user_input == '*') {
tnhnrl 57:ec69651c8c21 1291 pc().printf("SWITCHING TO SIMPLE MENU!\n\r");
tnhnrl 57:ec69651c8c21 1292 wait(2);
tnhnrl 57:ec69651c8c21 1293 _debug_menu_on = false;
tnhnrl 57:ec69651c8c21 1294 }
tnhnrl 57:ec69651c8c21 1295 } //end of debug menu
tnhnrl 57:ec69651c8c21 1296 /***************************** DEBUG MENU *****************************/
tnhnrl 57:ec69651c8c21 1297
tnhnrl 57:ec69651c8c21 1298 /***************************** SIMPLE MENU *****************************/
tnhnrl 57:ec69651c8c21 1299 else {
tnhnrl 57:ec69651c8c21 1300 if (user_input == 'D') {
tnhnrl 57:ec69651c8c21 1301 _keyboard_state = DIVE;
tnhnrl 57:ec69651c8c21 1302 }
tnhnrl 57:ec69651c8c21 1303 else if (user_input == 'N') {
tnhnrl 57:ec69651c8c21 1304 _keyboard_state = FIND_NEUTRAL;
tnhnrl 57:ec69651c8c21 1305 }
tnhnrl 57:ec69651c8c21 1306 else if (user_input == 'J') {
tnhnrl 57:ec69651c8c21 1307 _keyboard_state = FLOAT_LEVEL;
tnhnrl 57:ec69651c8c21 1308 }
tnhnrl 57:ec69651c8c21 1309 else if (user_input == 'B') {
tnhnrl 57:ec69651c8c21 1310 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 57:ec69651c8c21 1311 }
tnhnrl 57:ec69651c8c21 1312 else if (user_input == 'E') {
tnhnrl 57:ec69651c8c21 1313 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 57:ec69651c8c21 1314 }
tnhnrl 57:ec69651c8c21 1315
tnhnrl 57:ec69651c8c21 1316 // some debug tools below
tnhnrl 57:ec69651c8c21 1317 else if (user_input == 'P') {
tnhnrl 57:ec69651c8c21 1318 //Print current SD card log file
tnhnrl 57:ec69651c8c21 1319 //printCurrentSdLog();
tnhnrl 57:ec69651c8c21 1320 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 57:ec69651c8c21 1321 }
tnhnrl 57:ec69651c8c21 1322 else if (user_input == 'O') {
tnhnrl 57:ec69651c8c21 1323 _keyboard_state = TRANSMIT_LOG; //Transmit data (work in progress)
tnhnrl 57:ec69651c8c21 1324 }
tnhnrl 57:ec69651c8c21 1325 else if (user_input == '~') {
tnhnrl 57:ec69651c8c21 1326 pc().printf("ERASING MBED LOG FILE\n\r");
tnhnrl 57:ec69651c8c21 1327 mbedLogger().eraseFile();
tnhnrl 57:ec69651c8c21 1328 }
tnhnrl 57:ec69651c8c21 1329
tnhnrl 57:ec69651c8c21 1330 else if (user_input == '?') {
tnhnrl 57:ec69651c8c21 1331 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 57:ec69651c8c21 1332 wait(0.5);
tnhnrl 57:ec69651c8c21 1333 mbed_reset();
tnhnrl 57:ec69651c8c21 1334 }
tnhnrl 57:ec69651c8c21 1335
tnhnrl 57:ec69651c8c21 1336 //POSITION DIVE COMMANDS
tnhnrl 57:ec69651c8c21 1337 else if (user_input == 'k') {
tnhnrl 57:ec69651c8c21 1338 _BCE_position_command -= 1.0;
tnhnrl 57:ec69651c8c21 1339 pc().printf("Decreased BCE CMD to %0.1f\n\r", _BCE_position_command);
tnhnrl 57:ec69651c8c21 1340 }
tnhnrl 57:ec69651c8c21 1341 else if (user_input == 'l') {
tnhnrl 57:ec69651c8c21 1342 _BCE_position_command += 1.0;
tnhnrl 57:ec69651c8c21 1343 pc().printf("Increased BCE CMD to %0.1f\n\r", _BCE_position_command);
tnhnrl 57:ec69651c8c21 1344 }
tnhnrl 57:ec69651c8c21 1345 else if (user_input == ';') {
tnhnrl 57:ec69651c8c21 1346 _BMM_position_command -= 1.0;
tnhnrl 57:ec69651c8c21 1347 pc().printf("Decreased BMM CMD to %0.1f\n\r", _BMM_position_command);
tnhnrl 57:ec69651c8c21 1348 }
tnhnrl 57:ec69651c8c21 1349 else if (user_input == '\'') {
tnhnrl 57:ec69651c8c21 1350 _BMM_position_command += 1.0;
tnhnrl 57:ec69651c8c21 1351 pc().printf("Increased BMM CMD to %0.1f\n\r", _BMM_position_command);
tnhnrl 57:ec69651c8c21 1352 }
tnhnrl 57:ec69651c8c21 1353 //POSITION DIVE COMMANDS
tnhnrl 57:ec69651c8c21 1354
tnhnrl 57:ec69651c8c21 1355 else if (user_input == '*') {
tnhnrl 57:ec69651c8c21 1356 pc().printf("SWITCHING TO DEBUG MENU!\n\r");
tnhnrl 57:ec69651c8c21 1357 _debug_menu_on = true;
tnhnrl 57:ec69651c8c21 1358 wait(2);
tnhnrl 57:ec69651c8c21 1359 }
tnhnrl 16:3363b9f14913 1360 }
tnhnrl 57:ec69651c8c21 1361 /***************************** SIMPLE MENU *****************************/
tnhnrl 17:7c16b5671d0e 1362
tnhnrl 17:7c16b5671d0e 1363 //when you read the keyboard successfully, change the state
tnhnrl 28:16c83a2fdefa 1364 _state = _keyboard_state; //set state at the end of this function
tnhnrl 38:83d06c294807 1365 //pc().printf("\n\n\r ********* KEYBOARD STATE: %d *********\n\n\r", _state);
tnhnrl 16:3363b9f14913 1366 }
tnhnrl 16:3363b9f14913 1367 }
tnhnrl 52:f207567d3ea4 1368
tnhnrl 52:f207567d3ea4 1369 void StateMachine::keyboard_menu_STREAM_STATUS() {
tnhnrl 52:f207567d3ea4 1370 char STATUS_key;
tnhnrl 52:f207567d3ea4 1371
tnhnrl 52:f207567d3ea4 1372 // show the menu
tnhnrl 54:d4990fb68404 1373 pc().printf("\n\r1: STATUS DEBUG MENU (EXIT WITH 'X' !)\n\r");
tnhnrl 52:f207567d3ea4 1374
tnhnrl 52:f207567d3ea4 1375 while (1) {
tnhnrl 52:f207567d3ea4 1376 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1377 STATUS_key = pc().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1378 }
tnhnrl 52:f207567d3ea4 1379
tnhnrl 52:f207567d3ea4 1380 else {
tnhnrl 52:f207567d3ea4 1381
tnhnrl 52:f207567d3ea4 1382 wait(1);
tnhnrl 54:d4990fb68404 1383
tnhnrl 57:ec69651c8c21 1384 pc().printf("BCE POS (CMD): %0.1f (%0.1f) BATT POS: %0.1f (%0.1f) PRESS_psi: %0.2f (unfiltered: %0.2f) [depth_ft: %0.2f], PITCH: %0.2f, HEADING: %0.2f, rudder_servo_pwm: %0.1f\r\n",bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(),depth().getPsi(),depth().getRawPSI(),depthLoop().getPosition(),imu().getPitch(),imu().getHeading(),rudder().getPosition_pwm());
tnhnrl 54:d4990fb68404 1385
tnhnrl 56:48a8a5a65b82 1386 //pc().printf("(set) BCE POS: %0.1f (%0.1f) BATT POS: %0.1f (%0.1f) PRESS: %0.2f )(channel reading: %d) (volt: %0.2f) << %0.2f >> (Pressure: %0.2f (raw: %0.2f) PITCH: %0.2f HEADING: %0.2f)\r\n",bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(), depthLoop().getPosition(),depth().readADCCounts(),depth().readVoltage(),depth().getPsi(),depth().getRawPSI(),depth().getRawPSI(),imu().getPitch(),imu().getHeading());
tnhnrl 52:f207567d3ea4 1387
tnhnrl 54:d4990fb68404 1388 //pc().printf("Neutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
tnhnrl 54:d4990fb68404 1389 // pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 54:d4990fb68404 1390 // pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 54:d4990fb68404 1391 // pc().printf("heading (rudder): %3.1f deg\r\n",rudderLoop().getPosition()); //for heading
tnhnrl 52:f207567d3ea4 1392
tnhnrl 54:d4990fb68404 1393 // pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 54:d4990fb68404 1394 // pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 54:d4990fb68404 1395 //
tnhnrl 54:d4990fb68404 1396 // pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 54:d4990fb68404 1397 // pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 52:f207567d3ea4 1398
tnhnrl 54:d4990fb68404 1399 // pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 54:d4990fb68404 1400 // pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 54:d4990fb68404 1401 //
tnhnrl 54:d4990fb68404 1402 // pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 54:d4990fb68404 1403 // pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 52:f207567d3ea4 1404
tnhnrl 52:f207567d3ea4 1405 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1406 }
tnhnrl 52:f207567d3ea4 1407
tnhnrl 52:f207567d3ea4 1408 // process the keys
tnhnrl 52:f207567d3ea4 1409 if (STATUS_key == 'X') {
tnhnrl 52:f207567d3ea4 1410 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1411 }
tnhnrl 52:f207567d3ea4 1412
tnhnrl 52:f207567d3ea4 1413 else {
tnhnrl 52:f207567d3ea4 1414 pc().printf("\n\rThis key (%c) does nothing here. ", STATUS_key);
tnhnrl 52:f207567d3ea4 1415 }
tnhnrl 52:f207567d3ea4 1416 }
tnhnrl 52:f207567d3ea4 1417 }
tnhnrl 52:f207567d3ea4 1418
tnhnrl 52:f207567d3ea4 1419 void StateMachine::keyboard_menu_RUDDER_SERVO_settings() {
tnhnrl 52:f207567d3ea4 1420 //load current parameters from the rudder
tnhnrl 52:f207567d3ea4 1421 float rudder_min_pwm = rudder().getMinPWM();
tnhnrl 52:f207567d3ea4 1422 float rudder_max_pwm = rudder().getMaxPWM();
tnhnrl 52:f207567d3ea4 1423 float rudder_ctr_pwm = rudder().getCenterPWM();
tnhnrl 52:f207567d3ea4 1424 float rudder_min_deg = rudder().getMinDeg();
tnhnrl 52:f207567d3ea4 1425 float rudder_max_deg = rudder().getMaxDeg();
tnhnrl 52:f207567d3ea4 1426
tnhnrl 52:f207567d3ea4 1427 char RUDDER_PID_key;
tnhnrl 52:f207567d3ea4 1428
tnhnrl 52:f207567d3ea4 1429 // print the menu
tnhnrl 52:f207567d3ea4 1430 pc().printf("\n\rRUDDER (servo driver) settings (MENU)");
tnhnrl 52:f207567d3ea4 1431 pc().printf("\n\r(Adjust min/max/center PWM settings with the following keys: N and M and C");
tnhnrl 52:f207567d3ea4 1432 pc().printf("\n\r(Adjust DEGREE limit settings with the following keys: min = K, max = L");
tnhnrl 52:f207567d3ea4 1433 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 52:f207567d3ea4 1434 pc().printf("RUDDER min pwm: %f, max pwm: %f, center pwm: %f, min deg: %f, max deg: %f\r\n", rudder().getMinPWM(), rudder().getMaxPWM(), rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
tnhnrl 52:f207567d3ea4 1435
tnhnrl 52:f207567d3ea4 1436 // handle the key presses
tnhnrl 52:f207567d3ea4 1437 while(1) {
tnhnrl 52:f207567d3ea4 1438 // get the user's keystroke from either of the two inputs
tnhnrl 52:f207567d3ea4 1439 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1440 RUDDER_PID_key = pc().getc();
tnhnrl 52:f207567d3ea4 1441 }
tnhnrl 52:f207567d3ea4 1442 else {
tnhnrl 52:f207567d3ea4 1443 wait(0.5);
tnhnrl 52:f207567d3ea4 1444 pc().printf("RUDDER min pwm: %f, max pwm: %f, center pwm: %f, min deg: %f, max deg: %f\r\n", rudder().getMinPWM(), rudder().getMaxPWM(), rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
tnhnrl 52:f207567d3ea4 1445 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1446 }
tnhnrl 52:f207567d3ea4 1447
tnhnrl 52:f207567d3ea4 1448 // handle the user's key input
tnhnrl 52:f207567d3ea4 1449 if (RUDDER_PID_key == 'S') { // user wants to save the modified values
tnhnrl 52:f207567d3ea4 1450 // set global values
tnhnrl 52:f207567d3ea4 1451 rudder().setMinPWM(rudder_min_pwm);
tnhnrl 52:f207567d3ea4 1452 rudder().setMaxPWM(rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1453 rudder().setCenterPWM(rudder_ctr_pwm);
tnhnrl 52:f207567d3ea4 1454 rudder().setMinDeg(rudder_min_deg);
tnhnrl 52:f207567d3ea4 1455 rudder().setMaxDeg(rudder_max_deg);
tnhnrl 52:f207567d3ea4 1456
tnhnrl 52:f207567d3ea4 1457 // save rudder servo driver values for inner loop
tnhnrl 52:f207567d3ea4 1458 configFileIO().saveRudderData(rudder_min_deg, rudder_max_deg, rudder_ctr_pwm, rudder_min_pwm, rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1459 pc().printf("RUDDER min pwm: %f, max pwm: %f, center pwm: %f, min deg: %f, max deg: %f\r\n", rudder().getMinPWM(), rudder().getMaxPWM(), rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
tnhnrl 52:f207567d3ea4 1460 }
tnhnrl 52:f207567d3ea4 1461 else if (RUDDER_PID_key == 'X') {
tnhnrl 52:f207567d3ea4 1462 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1463 }
tnhnrl 52:f207567d3ea4 1464 // MIN PWM
tnhnrl 52:f207567d3ea4 1465 else if (RUDDER_PID_key == 'N') {
tnhnrl 52:f207567d3ea4 1466 pc().printf(">> Type in rudder_min_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1467 rudder_min_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1468 }
tnhnrl 52:f207567d3ea4 1469 // MAX PWM
tnhnrl 52:f207567d3ea4 1470 else if (RUDDER_PID_key == 'M') {
tnhnrl 52:f207567d3ea4 1471 pc().printf(">> Type in rudder_max_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1472 rudder_max_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1473 }
tnhnrl 52:f207567d3ea4 1474 // CENTER PWM
tnhnrl 52:f207567d3ea4 1475 else if (RUDDER_PID_key == 'C') {
tnhnrl 52:f207567d3ea4 1476 pc().printf(">> Type in rudder_ctr_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1477 rudder_ctr_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1478 }
tnhnrl 52:f207567d3ea4 1479 // MIN DEG
tnhnrl 52:f207567d3ea4 1480 else if (RUDDER_PID_key == 'K') {
tnhnrl 52:f207567d3ea4 1481 pc().printf(">> Type in rudder_min_deg with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1482 rudder_min_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1483 }
tnhnrl 52:f207567d3ea4 1484 // MAX DEG
tnhnrl 52:f207567d3ea4 1485 else if (RUDDER_PID_key == 'L') {
tnhnrl 52:f207567d3ea4 1486 pc().printf(">> Type in rudder_max_deg with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1487 rudder_max_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1488 }
tnhnrl 52:f207567d3ea4 1489
tnhnrl 52:f207567d3ea4 1490 else {
tnhnrl 52:f207567d3ea4 1491 pc().printf("RUDDER SETUP: [%c] This key does nothing here. \r", RUDDER_PID_key);
tnhnrl 52:f207567d3ea4 1492 }
tnhnrl 52:f207567d3ea4 1493 }
tnhnrl 52:f207567d3ea4 1494 }
tnhnrl 54:d4990fb68404 1495
tnhnrl 54:d4990fb68404 1496 void StateMachine::keyboard_menu_COUNTS_STATUS() {
tnhnrl 54:d4990fb68404 1497
tnhnrl 54:d4990fb68404 1498 }
tnhnrl 56:48a8a5a65b82 1499
tnhnrl 49:47ffa4feb6db 1500 void StateMachine::keyboard_menu_MANUAL_TUNING() {
tnhnrl 49:47ffa4feb6db 1501 char TUNING_key;
tnhnrl 49:47ffa4feb6db 1502
tnhnrl 49:47ffa4feb6db 1503 // show the menu
tnhnrl 56:48a8a5a65b82 1504 pc().printf("\n\r1: MANUAL TUNING MENU (EXIT WITH 'X' !) (Pause and Unpause rudder ticker with P and U\n");
tnhnrl 56:48a8a5a65b82 1505 pc().printf("\n\r(Adjust BCE and BATT positions in real-time. Timeout NOT running! (decrease/increase BCE with A/S, BATT with Q/W, RUDDER with E/R)\n\r");
tnhnrl 56:48a8a5a65b82 1506 pc().printf("\n\rMANUAL_TUNING: BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg, headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\r",bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(), imu().getHeading());
tnhnrl 56:48a8a5a65b82 1507
tnhnrl 56:48a8a5a65b82 1508 //made these into internal parameters
tnhnrl 56:48a8a5a65b82 1509 float _tuning_bce_pos_mm = 300.0; //safe starting position
tnhnrl 56:48a8a5a65b82 1510 float _tuning_batt_pos_mm = 60.0; //safe starting position
tnhnrl 56:48a8a5a65b82 1511 float _tuning_rudder_pos_deg = 0.0; //safe starting position
tnhnrl 56:48a8a5a65b82 1512
tnhnrl 56:48a8a5a65b82 1513 //immediately start at those positions
tnhnrl 56:48a8a5a65b82 1514 bce().setPosition_mm(_tuning_bce_pos_mm);
tnhnrl 56:48a8a5a65b82 1515 batt().setPosition_mm(_tuning_batt_pos_mm);
tnhnrl 56:48a8a5a65b82 1516 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 49:47ffa4feb6db 1517
tnhnrl 49:47ffa4feb6db 1518 // what needs to be started?
tnhnrl 49:47ffa4feb6db 1519 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 1520 batt().unpause(); //this is now active
tnhnrl 56:48a8a5a65b82 1521 rudder().unpause();
tnhnrl 49:47ffa4feb6db 1522
tnhnrl 56:48a8a5a65b82 1523 while (1) {
tnhnrl 49:47ffa4feb6db 1524 if (pc().readable()) {
tnhnrl 49:47ffa4feb6db 1525 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 49:47ffa4feb6db 1526 }
tnhnrl 49:47ffa4feb6db 1527
tnhnrl 56:48a8a5a65b82 1528 else {
tnhnrl 49:47ffa4feb6db 1529 continue; // didn't get a user input, so keep waiting for it
tnhnrl 49:47ffa4feb6db 1530 }
tnhnrl 49:47ffa4feb6db 1531
tnhnrl 49:47ffa4feb6db 1532 // process the keys
tnhnrl 49:47ffa4feb6db 1533 if (TUNING_key == 'X') {
tnhnrl 49:47ffa4feb6db 1534 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 49:47ffa4feb6db 1535 bce().pause();
tnhnrl 49:47ffa4feb6db 1536 batt().pause();
tnhnrl 56:48a8a5a65b82 1537 rudder().pause();
tnhnrl 56:48a8a5a65b82 1538
tnhnrl 56:48a8a5a65b82 1539 //right now the rudder is always active................................................hmm
tnhnrl 56:48a8a5a65b82 1540 //deactivate the pin? new/delete?
tnhnrl 49:47ffa4feb6db 1541
tnhnrl 49:47ffa4feb6db 1542 break; //exit the while loop
tnhnrl 49:47ffa4feb6db 1543 }
tnhnrl 56:48a8a5a65b82 1544
tnhnrl 56:48a8a5a65b82 1545 //Buoyancy Engine
tnhnrl 52:f207567d3ea4 1546 else if (TUNING_key == 'A') {
tnhnrl 56:48a8a5a65b82 1547 _tuning_bce_pos_mm = _tuning_bce_pos_mm - 1.0;
tnhnrl 56:48a8a5a65b82 1548 bce().setPosition_mm(_tuning_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 56:48a8a5a65b82 1549 pc().printf("BCE _tuning_bce_pos_mm now: %0.1f\n\r", _tuning_bce_pos_mm);
tnhnrl 56:48a8a5a65b82 1550 pc().printf("\n\rMANUAL_TUNING: (BCE CHANGE: %0.1f)\n\r BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 52:f207567d3ea4 1551 }
tnhnrl 52:f207567d3ea4 1552
tnhnrl 49:47ffa4feb6db 1553 else if (TUNING_key == 'S') {
tnhnrl 56:48a8a5a65b82 1554 _tuning_bce_pos_mm = _tuning_bce_pos_mm + 1.0;
tnhnrl 56:48a8a5a65b82 1555 bce().setPosition_mm(_tuning_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 56:48a8a5a65b82 1556 pc().printf("BCE _tuning_bce_pos_mm now: %0.1f\n\r", _tuning_bce_pos_mm);
tnhnrl 56:48a8a5a65b82 1557 pc().printf("\n\rMANUAL_TUNING: (BCE CHANGE: %0.1f)\n\r BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 49:47ffa4feb6db 1558 }
tnhnrl 49:47ffa4feb6db 1559
tnhnrl 56:48a8a5a65b82 1560 //BATTERY
tnhnrl 49:47ffa4feb6db 1561 else if (TUNING_key == 'Q') {
tnhnrl 56:48a8a5a65b82 1562 _tuning_batt_pos_mm = _tuning_batt_pos_mm - 1.0;
tnhnrl 56:48a8a5a65b82 1563 batt().setPosition_mm(_tuning_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 56:48a8a5a65b82 1564 pc().printf("\n\rMANUAL_TUNING: (BATT CHANGE: %0.1f)\n\r BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 49:47ffa4feb6db 1565 }
tnhnrl 49:47ffa4feb6db 1566
tnhnrl 49:47ffa4feb6db 1567 else if (TUNING_key == 'W') {
tnhnrl 56:48a8a5a65b82 1568 _tuning_batt_pos_mm = _tuning_batt_pos_mm + 1.0;
tnhnrl 56:48a8a5a65b82 1569 batt().setPosition_mm(_tuning_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 56:48a8a5a65b82 1570 pc().printf("\n\rMANUAL_TUNING: (BATT CHANGE: %0.1f)\n\r BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 56:48a8a5a65b82 1571 }
tnhnrl 56:48a8a5a65b82 1572
tnhnrl 56:48a8a5a65b82 1573 else if (TUNING_key == 'c' or TUNING_key == 'C') {
tnhnrl 56:48a8a5a65b82 1574 pc().printf("MANUAL_TUNING: (current stats) BCE pos: %3.1f mm (cmd: %3.1f mm), BATT pos: %3.1f mm (cmd: %3.1f mm) SERVO: %0.1f deg, %f pwm\r", bce().getPosition_mm(), bce().getSetPosition_mm(), batt().getPosition_mm(), batt().getSetPosition_mm(), rudder().getPosition_deg(), rudder().getPosition_pwm());
tnhnrl 56:48a8a5a65b82 1575 }
tnhnrl 56:48a8a5a65b82 1576
tnhnrl 56:48a8a5a65b82 1577 //RUDER
tnhnrl 56:48a8a5a65b82 1578 else if (TUNING_key == 'E') {
tnhnrl 56:48a8a5a65b82 1579 _tuning_rudder_pos_deg = _tuning_rudder_pos_deg - 0.5;
tnhnrl 56:48a8a5a65b82 1580 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 56:48a8a5a65b82 1581 pc().printf("MT: RUDDER %0.1f [servo pwm: %f, %0.1f deg] (headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\n\r", _tuning_rudder_pos_deg, rudder().getPosition_pwm(), rudder().getPosition_deg(), headingLoop().getPosition(), imu().getHeading());
tnhnrl 56:48a8a5a65b82 1582 }
tnhnrl 56:48a8a5a65b82 1583
tnhnrl 56:48a8a5a65b82 1584 else if (TUNING_key == 'R') {
tnhnrl 56:48a8a5a65b82 1585 _tuning_rudder_pos_deg = _tuning_rudder_pos_deg + 0.5;
tnhnrl 56:48a8a5a65b82 1586 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 56:48a8a5a65b82 1587 pc().printf("MT: RUDDER %0.1f [servo pwm: %f, %0.1f deg] (headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\n\r", _tuning_rudder_pos_deg, rudder().getPosition_pwm(), rudder().getPosition_deg(), headingLoop().getPosition(), imu().getHeading());
tnhnrl 56:48a8a5a65b82 1588 }
tnhnrl 56:48a8a5a65b82 1589
tnhnrl 56:48a8a5a65b82 1590 else if (TUNING_key == 'P') {
tnhnrl 56:48a8a5a65b82 1591 rudder().pause();
tnhnrl 56:48a8a5a65b82 1592 }
tnhnrl 56:48a8a5a65b82 1593
tnhnrl 56:48a8a5a65b82 1594 else if (TUNING_key == 'U') {
tnhnrl 56:48a8a5a65b82 1595 rudder().unpause();
tnhnrl 49:47ffa4feb6db 1596 }
tnhnrl 49:47ffa4feb6db 1597
tnhnrl 49:47ffa4feb6db 1598 else {
tnhnrl 56:48a8a5a65b82 1599 pc().printf("\n\rMANUAL_TUNING: [%c] This key does nothing here. \r", TUNING_key);
tnhnrl 56:48a8a5a65b82 1600 }
tnhnrl 49:47ffa4feb6db 1601 }
tnhnrl 49:47ffa4feb6db 1602 }
tnhnrl 56:48a8a5a65b82 1603
tnhnrl 56:48a8a5a65b82 1604 //void StateMachine::keyboard_menu_MANUAL_TUNING() {
tnhnrl 56:48a8a5a65b82 1605 // char TUNING_key;
tnhnrl 56:48a8a5a65b82 1606 //
tnhnrl 56:48a8a5a65b82 1607 // // show the menu
tnhnrl 56:48a8a5a65b82 1608 // pc().printf("\n\r1: MANUAL TUNING MENU (EXIT WITH 'X' !)");
tnhnrl 56:48a8a5a65b82 1609 // pc().printf("\n\r(Adjust BCE and BATT positions in real-time. Timeout NOT running! (decrease/increase BCE with A/S, BATT with Q/W)\n\r");
tnhnrl 56:48a8a5a65b82 1610 // pc().printf("MANUAL_TUNING: BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 56:48a8a5a65b82 1611 //
tnhnrl 56:48a8a5a65b82 1612 // // what needs to be started?
tnhnrl 56:48a8a5a65b82 1613 // bce().setPosition_mm(160);
tnhnrl 56:48a8a5a65b82 1614 //
tnhnrl 56:48a8a5a65b82 1615 // batt().setPosition_mm(50);
tnhnrl 56:48a8a5a65b82 1616 //
tnhnrl 56:48a8a5a65b82 1617 // bce().unpause(); //this is now active
tnhnrl 56:48a8a5a65b82 1618 // batt().unpause(); //this is now active
tnhnrl 56:48a8a5a65b82 1619 //
tnhnrl 56:48a8a5a65b82 1620 // while (1) {
tnhnrl 56:48a8a5a65b82 1621 // if (pc().readable()) {
tnhnrl 56:48a8a5a65b82 1622 // TUNING_key = pc().getc(); //get each keystroke
tnhnrl 56:48a8a5a65b82 1623 // }
tnhnrl 56:48a8a5a65b82 1624 //
tnhnrl 56:48a8a5a65b82 1625 // else {
tnhnrl 56:48a8a5a65b82 1626 // pc().printf("MT: ACTUAL POS (SET POS): BCE: %0.1f (%0.1f), BATT: %0.1f (%0.1f)\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm());
tnhnrl 56:48a8a5a65b82 1627 // continue; // didn't get a user input, so keep waiting for it
tnhnrl 56:48a8a5a65b82 1628 // }
tnhnrl 56:48a8a5a65b82 1629 //
tnhnrl 56:48a8a5a65b82 1630 // // process the keys
tnhnrl 56:48a8a5a65b82 1631 // if (TUNING_key == 'X') {
tnhnrl 56:48a8a5a65b82 1632 // // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 56:48a8a5a65b82 1633 // bce().pause();
tnhnrl 56:48a8a5a65b82 1634 // batt().pause();
tnhnrl 56:48a8a5a65b82 1635 //
tnhnrl 56:48a8a5a65b82 1636 // break; //exit the while loop
tnhnrl 56:48a8a5a65b82 1637 // }
tnhnrl 56:48a8a5a65b82 1638 //
tnhnrl 56:48a8a5a65b82 1639 // else if (TUNING_key == 'A') {
tnhnrl 56:48a8a5a65b82 1640 // _neutral_bce_pos_mm = _neutral_bce_pos_mm - 1;
tnhnrl 56:48a8a5a65b82 1641 // bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 56:48a8a5a65b82 1642 // pc().printf("MANUAL_TUNING: (BCE CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 56:48a8a5a65b82 1643 // }
tnhnrl 56:48a8a5a65b82 1644 //
tnhnrl 56:48a8a5a65b82 1645 // else if (TUNING_key == 'S') {
tnhnrl 56:48a8a5a65b82 1646 // _neutral_bce_pos_mm = _neutral_bce_pos_mm + 1;
tnhnrl 56:48a8a5a65b82 1647 // bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 56:48a8a5a65b82 1648 // pc().printf("MANUAL_TUNING: (BCE CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 56:48a8a5a65b82 1649 // }
tnhnrl 56:48a8a5a65b82 1650 //
tnhnrl 56:48a8a5a65b82 1651 // else if (TUNING_key == 'Q') {
tnhnrl 56:48a8a5a65b82 1652 // _neutral_batt_pos_mm = _neutral_batt_pos_mm - 1;
tnhnrl 56:48a8a5a65b82 1653 // batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 56:48a8a5a65b82 1654 // pc().printf("MANUAL_TUNING: (BATT CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 56:48a8a5a65b82 1655 // }
tnhnrl 56:48a8a5a65b82 1656 //
tnhnrl 56:48a8a5a65b82 1657 // else if (TUNING_key == 'W') {
tnhnrl 56:48a8a5a65b82 1658 // _neutral_batt_pos_mm = _neutral_batt_pos_mm + 1;
tnhnrl 56:48a8a5a65b82 1659 // batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 56:48a8a5a65b82 1660 // pc().printf("MANUAL_TUNING: (BATT CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 56:48a8a5a65b82 1661 // }
tnhnrl 56:48a8a5a65b82 1662 //
tnhnrl 56:48a8a5a65b82 1663 // else {
tnhnrl 56:48a8a5a65b82 1664 // pc().printf("\n\rThis key does nothing here. ");
tnhnrl 56:48a8a5a65b82 1665 // }
tnhnrl 56:48a8a5a65b82 1666 //
tnhnrl 56:48a8a5a65b82 1667 // }
tnhnrl 56:48a8a5a65b82 1668 //}
tnhnrl 52:f207567d3ea4 1669
tnhnrl 52:f207567d3ea4 1670 void StateMachine::keyboard_menu_CHANNEL_READINGS() {
tnhnrl 52:f207567d3ea4 1671 char TUNING_key;
tnhnrl 52:f207567d3ea4 1672
tnhnrl 52:f207567d3ea4 1673 // show the menu
tnhnrl 55:f4ec445c42fe 1674 pc().printf("\n\r8: CHANNEL READINGS (EXIT WITH 'X' !)");
tnhnrl 55:f4ec445c42fe 1675
tnhnrl 55:f4ec445c42fe 1676 while (1) {
tnhnrl 55:f4ec445c42fe 1677 if (pc().readable()) {
tnhnrl 55:f4ec445c42fe 1678 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 55:f4ec445c42fe 1679 }
tnhnrl 55:f4ec445c42fe 1680
tnhnrl 55:f4ec445c42fe 1681 // process the keys
tnhnrl 55:f4ec445c42fe 1682 if (TUNING_key == 'X') {
tnhnrl 55:f4ec445c42fe 1683 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 55:f4ec445c42fe 1684 bce().pause();
tnhnrl 55:f4ec445c42fe 1685 batt().pause();
tnhnrl 55:f4ec445c42fe 1686
tnhnrl 55:f4ec445c42fe 1687 break; //exit the while loop
tnhnrl 55:f4ec445c42fe 1688 }
tnhnrl 55:f4ec445c42fe 1689
tnhnrl 55:f4ec445c42fe 1690 else {
tnhnrl 55:f4ec445c42fe 1691 wait(0.5);
tnhnrl 55:f4ec445c42fe 1692 pc().printf("0(%d),1(%d),2(%d),6(%d),4(%d),5(%d),6(%d),7(%d)\r\n",adc().readCh0(),adc().readCh1(),adc().readCh2(),adc().readCh3(),adc().readCh4(),adc().readCh5(),adc().readCh6(),adc().readCh7());
tnhnrl 55:f4ec445c42fe 1693 continue; // didn't get a user input, so keep waiting for it
tnhnrl 55:f4ec445c42fe 1694 }
tnhnrl 55:f4ec445c42fe 1695 }
tnhnrl 55:f4ec445c42fe 1696 }
tnhnrl 55:f4ec445c42fe 1697
tnhnrl 55:f4ec445c42fe 1698 void StateMachine::keyboard_menu_POSITION_READINGS() {
tnhnrl 55:f4ec445c42fe 1699 char TUNING_key;
tnhnrl 55:f4ec445c42fe 1700
tnhnrl 55:f4ec445c42fe 1701 // show the menu
tnhnrl 55:f4ec445c42fe 1702 pc().printf("\n\r9: BCE and BMM POSITION READINGS (EXIT WITH 'X' !)");
tnhnrl 52:f207567d3ea4 1703
tnhnrl 52:f207567d3ea4 1704 while (1) {
tnhnrl 52:f207567d3ea4 1705 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1706 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1707 }
tnhnrl 52:f207567d3ea4 1708
tnhnrl 54:d4990fb68404 1709 // process the keys
tnhnrl 54:d4990fb68404 1710 if (TUNING_key == 'X') {
tnhnrl 54:d4990fb68404 1711 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 54:d4990fb68404 1712 bce().pause();
tnhnrl 54:d4990fb68404 1713 batt().pause();
tnhnrl 54:d4990fb68404 1714
tnhnrl 54:d4990fb68404 1715 break; //exit the while loop
tnhnrl 54:d4990fb68404 1716 }
tnhnrl 54:d4990fb68404 1717
tnhnrl 52:f207567d3ea4 1718 else {
tnhnrl 52:f207567d3ea4 1719 // Testing out ADC
tnhnrl 54:d4990fb68404 1720 wait(0.5);
tnhnrl 52:f207567d3ea4 1721 float vref = 5.6;
tnhnrl 52:f207567d3ea4 1722 float vmeasured = 0;
tnhnrl 52:f207567d3ea4 1723 unsigned int raw = adc().readCh5();
tnhnrl 52:f207567d3ea4 1724 vmeasured = ((float)raw)/4095.0*vref;
tnhnrl 52:f207567d3ea4 1725
tnhnrl 54:d4990fb68404 1726
tnhnrl 55:f4ec445c42fe 1727 //pc().printf("BCE POS(%d),BMM POS(%d), BCE CUR(%d), BMM CUR(%d), Depth Pressure (%d) \r\n",adc().readCh0(),adc().readCh1(),adc().readCh2(),adc().readCh3(),adc().readCh4());
tnhnrl 55:f4ec445c42fe 1728
tnhnrl 55:f4ec445c42fe 1729 pc().printf("BCE POS(%d), BMM POS(%d), BCE CUR(%d), BMM CUR(%d), Depth Pressure (%d) << POS: BCE %0.2f, BATT %0.2f >>\r\n",adc().readCh0(),adc().readCh1(),adc().readCh2(),adc().readCh3(),adc().readCh4(),bce().getPosition_mm(),batt().getPosition_mm());
tnhnrl 54:d4990fb68404 1730
tnhnrl 54:d4990fb68404 1731 //pc().printf("vessel pressure(%d) batt voltage(%d) board current(%d) LIMIT: BCE(%d) BMM(%d) (HW reading limit: BCE(%d) BMM (%d) \r\n",adc().readCh5(),adc().readCh6(),adc().readCh7(),bce().getSwitch(), batt().getSwitch(),bce().getSwitchState(),batt().getSwitchState());
tnhnrl 54:d4990fb68404 1732 //pc().printf("LIMIT: BCE(%d) BMM(%d) << HW reading limit: BCE(%d) BMM (%d) >> \r\n",bce().getSwitch(), batt().getSwitch(),bce().getHWSwitchReading(),batt().getHWSwitchReading());
tnhnrl 54:d4990fb68404 1733 //pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
tnhnrl 52:f207567d3ea4 1734 // End of ADC Test
tnhnrl 52:f207567d3ea4 1735
tnhnrl 54:d4990fb68404 1736 // pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
tnhnrl 54:d4990fb68404 1737 // pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
tnhnrl 54:d4990fb68404 1738 // pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
tnhnrl 54:d4990fb68404 1739 // pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
tnhnrl 54:d4990fb68404 1740 // pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
tnhnrl 54:d4990fb68404 1741 // pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
tnhnrl 54:d4990fb68404 1742 // pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
tnhnrl 54:d4990fb68404 1743 // pc().printf("raw board current: %d \r\n",adc().readCh7());
tnhnrl 54:d4990fb68404 1744
tnhnrl 52:f207567d3ea4 1745 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1746 }
tnhnrl 52:f207567d3ea4 1747 }
tnhnrl 52:f207567d3ea4 1748 }
tnhnrl 49:47ffa4feb6db 1749
tnhnrl 16:3363b9f14913 1750 void StateMachine::keyboard_menu_BCE_PID_settings() {
tnhnrl 16:3363b9f14913 1751 char PID_key;
tnhnrl 16:3363b9f14913 1752 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 38:83d06c294807 1753 float BCE_KP = bce().getControllerP(); // load current value
tnhnrl 38:83d06c294807 1754 float BCE_KI = bce().getControllerI(); // load current global value
tnhnrl 38:83d06c294807 1755 float BCE_KD = bce().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1756
tnhnrl 16:3363b9f14913 1757 // show the menu
tnhnrl 16:3363b9f14913 1758 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1759 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1760 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 21:38c8544db6f4 1761 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 1762
tnhnrl 16:3363b9f14913 1763 // handle the key presses
tnhnrl 16:3363b9f14913 1764 while(1) {
tnhnrl 16:3363b9f14913 1765 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1766 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1767 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1768 }
tnhnrl 16:3363b9f14913 1769 else {
tnhnrl 16:3363b9f14913 1770 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1771 }
tnhnrl 16:3363b9f14913 1772
tnhnrl 16:3363b9f14913 1773 // handle the user's key input
tnhnrl 16:3363b9f14913 1774 if (PID_key == '-') {
tnhnrl 38:83d06c294807 1775 BCE_KP -= gain_step_size;
tnhnrl 38:83d06c294807 1776 pc().printf("P gain: %0.5f \r\n", BCE_KP);
tnhnrl 16:3363b9f14913 1777 }
tnhnrl 16:3363b9f14913 1778 else if (PID_key == '=') {
tnhnrl 38:83d06c294807 1779 BCE_KP += gain_step_size;
tnhnrl 38:83d06c294807 1780 pc().printf("P gain: %0.5f \r\n", BCE_KP);
tnhnrl 16:3363b9f14913 1781 }
tnhnrl 16:3363b9f14913 1782 else if (PID_key == '[') {
tnhnrl 38:83d06c294807 1783 BCE_KI -= gain_step_size;
tnhnrl 38:83d06c294807 1784 pc().printf("I gain: %0.5f \r\n", BCE_KI);
tnhnrl 16:3363b9f14913 1785 }
tnhnrl 16:3363b9f14913 1786 else if (PID_key == ']') {
tnhnrl 38:83d06c294807 1787 BCE_KI += gain_step_size;
tnhnrl 38:83d06c294807 1788 pc().printf("I gain: %0.5f \r\n", BCE_KI);
tnhnrl 16:3363b9f14913 1789 }
tnhnrl 16:3363b9f14913 1790 else if (PID_key == ';') {
tnhnrl 38:83d06c294807 1791 BCE_KD -= gain_step_size;
tnhnrl 38:83d06c294807 1792 pc().printf("D gain: %0.5f \r\n", BCE_KD);
tnhnrl 16:3363b9f14913 1793 }
tnhnrl 16:3363b9f14913 1794 else if (PID_key == '\'') {
tnhnrl 38:83d06c294807 1795 BCE_KD += gain_step_size;
tnhnrl 38:83d06c294807 1796 pc().printf("D gain: %0.5f \r\n", BCE_KD);
tnhnrl 16:3363b9f14913 1797 }
tnhnrl 16:3363b9f14913 1798 else if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 16:3363b9f14913 1799 // set values
tnhnrl 38:83d06c294807 1800 bce().setControllerP(BCE_KP);
tnhnrl 38:83d06c294807 1801 bce().setControllerI(BCE_KI);
tnhnrl 38:83d06c294807 1802 bce().setControllerD(BCE_KD);
tnhnrl 16:3363b9f14913 1803
tnhnrl 38:83d06c294807 1804 // save to "BATT.TXT" file
tnhnrl 38:83d06c294807 1805 configFileIO().saveBCEData(BCE_KP, BCE_KI, BCE_KD);
tnhnrl 38:83d06c294807 1806
tnhnrl 16:3363b9f14913 1807 break; //exit the while loop
tnhnrl 16:3363b9f14913 1808 }
tnhnrl 16:3363b9f14913 1809 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1810 break; //exit the while loop
tnhnrl 16:3363b9f14913 1811 }
tnhnrl 16:3363b9f14913 1812 else {
tnhnrl 16:3363b9f14913 1813 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1814 }
tnhnrl 16:3363b9f14913 1815 }
tnhnrl 16:3363b9f14913 1816 }
tnhnrl 20:8987a9ae2bc7 1817
tnhnrl 16:3363b9f14913 1818 void StateMachine::keyboard_menu_BATT_PID_settings() {
tnhnrl 16:3363b9f14913 1819 char PID_key;
tnhnrl 16:3363b9f14913 1820 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 38:83d06c294807 1821 float batt_KP = batt().getControllerP(); // load current global value
tnhnrl 38:83d06c294807 1822 float batt_KI = batt().getControllerI(); // load current global value
tnhnrl 38:83d06c294807 1823 float batt_KD = batt().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1824
tnhnrl 16:3363b9f14913 1825 // print the menu
tnhnrl 16:3363b9f14913 1826 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1827 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1828 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 21:38c8544db6f4 1829 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 1830
tnhnrl 16:3363b9f14913 1831 // handle the key presses
tnhnrl 16:3363b9f14913 1832 while(1) {
tnhnrl 16:3363b9f14913 1833 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1834 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1835 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1836 }
tnhnrl 16:3363b9f14913 1837 else {
tnhnrl 16:3363b9f14913 1838 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1839 }
tnhnrl 16:3363b9f14913 1840
tnhnrl 16:3363b9f14913 1841 // handle the user's key input
tnhnrl 16:3363b9f14913 1842 if (PID_key == '-') {
tnhnrl 38:83d06c294807 1843 batt_KP -= gain_step_size;
tnhnrl 38:83d06c294807 1844 pc().printf("\rP gain: %0.5f ", batt_KP);
tnhnrl 16:3363b9f14913 1845 }
tnhnrl 16:3363b9f14913 1846 else if (PID_key == '=') {
tnhnrl 38:83d06c294807 1847 batt_KP += gain_step_size;
tnhnrl 38:83d06c294807 1848 pc().printf("\rP gain: %0.5f ", batt_KP);
tnhnrl 16:3363b9f14913 1849 }
tnhnrl 16:3363b9f14913 1850 else if (PID_key == '[') {
tnhnrl 38:83d06c294807 1851 batt_KI -= gain_step_size;
tnhnrl 38:83d06c294807 1852 pc().printf("\rI gain: %0.5f ", batt_KI);
tnhnrl 16:3363b9f14913 1853 }
tnhnrl 16:3363b9f14913 1854 else if (PID_key == ']') {
tnhnrl 38:83d06c294807 1855 batt_KI += gain_step_size;
tnhnrl 38:83d06c294807 1856 pc().printf("\rI gain: %0.5f ", batt_KI);
tnhnrl 16:3363b9f14913 1857 }
tnhnrl 16:3363b9f14913 1858 else if (PID_key == ';') {
tnhnrl 38:83d06c294807 1859 batt_KD -= gain_step_size;
tnhnrl 38:83d06c294807 1860 pc().printf("\rD gain: %0.5f ", batt_KD);
tnhnrl 16:3363b9f14913 1861 }
tnhnrl 16:3363b9f14913 1862 else if (PID_key == '\'') {
tnhnrl 38:83d06c294807 1863 batt_KD += gain_step_size;
tnhnrl 38:83d06c294807 1864 pc().printf("\rD gain: %0.5f ", batt_KD);
tnhnrl 16:3363b9f14913 1865 }
tnhnrl 16:3363b9f14913 1866 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1867 // set global values
tnhnrl 38:83d06c294807 1868 batt().setControllerP(batt_KP);
tnhnrl 38:83d06c294807 1869 batt().setControllerI(batt_KI);
tnhnrl 38:83d06c294807 1870 batt().setControllerD(batt_KD);
tnhnrl 16:3363b9f14913 1871
tnhnrl 38:83d06c294807 1872 // save to "BATT.TXT" file
tnhnrl 38:83d06c294807 1873 configFileIO().saveBattData(batt_KP, batt_KI, batt_KD);
tnhnrl 38:83d06c294807 1874
tnhnrl 16:3363b9f14913 1875 break; //exit the while loop
tnhnrl 16:3363b9f14913 1876 }
tnhnrl 16:3363b9f14913 1877 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1878 break; //exit the while loop
tnhnrl 16:3363b9f14913 1879 }
tnhnrl 16:3363b9f14913 1880 else {
tnhnrl 16:3363b9f14913 1881 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1882 }
tnhnrl 16:3363b9f14913 1883 }
tnhnrl 16:3363b9f14913 1884 }
tnhnrl 20:8987a9ae2bc7 1885
tnhnrl 16:3363b9f14913 1886 void StateMachine::keyboard_menu_DEPTH_PID_settings() {
tnhnrl 16:3363b9f14913 1887 char PID_key;
tnhnrl 16:3363b9f14913 1888 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1889
tnhnrl 16:3363b9f14913 1890 // show the menu
tnhnrl 16:3363b9f14913 1891 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1892 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1893 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r");
tnhnrl 16:3363b9f14913 1894 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 1895
tnhnrl 16:3363b9f14913 1896 // handle the key presses
tnhnrl 16:3363b9f14913 1897 while(1) {
tnhnrl 16:3363b9f14913 1898 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1899 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1900 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1901 }
tnhnrl 16:3363b9f14913 1902 else {
tnhnrl 16:3363b9f14913 1903 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1904 }
tnhnrl 16:3363b9f14913 1905
tnhnrl 16:3363b9f14913 1906 // handle the user's key input
tnhnrl 16:3363b9f14913 1907 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1908 _depth_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1909 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1910 }
tnhnrl 16:3363b9f14913 1911 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1912 _depth_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1913 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1914 }
tnhnrl 16:3363b9f14913 1915 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1916 _depth_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1917 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1918 }
tnhnrl 16:3363b9f14913 1919 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1920 _depth_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1921 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1922 }
tnhnrl 16:3363b9f14913 1923 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1924 _depth_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1925 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1926 }
tnhnrl 16:3363b9f14913 1927 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1928 _depth_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1929 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1930 }
tnhnrl 16:3363b9f14913 1931 else if (PID_key == 'S') { // user wants to save these settings
tnhnrl 16:3363b9f14913 1932 // set global values
tnhnrl 21:38c8544db6f4 1933 depthLoop().setControllerP(_depth_KP);
tnhnrl 21:38c8544db6f4 1934 depthLoop().setControllerI(_depth_KI);
tnhnrl 21:38c8544db6f4 1935 depthLoop().setControllerD(_depth_KD);
tnhnrl 16:3363b9f14913 1936
tnhnrl 21:38c8544db6f4 1937 // save depth PID values for outer loop
tnhnrl 21:38c8544db6f4 1938 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1939 break; //exit the while loop
tnhnrl 16:3363b9f14913 1940 }
tnhnrl 16:3363b9f14913 1941 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1942 break; //exit the while loop
tnhnrl 16:3363b9f14913 1943 }
tnhnrl 16:3363b9f14913 1944 else {
tnhnrl 16:3363b9f14913 1945 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1946 }
tnhnrl 16:3363b9f14913 1947 }
tnhnrl 16:3363b9f14913 1948 }
tnhnrl 16:3363b9f14913 1949
tnhnrl 16:3363b9f14913 1950 void StateMachine::keyboard_menu_PITCH_PID_settings() {
tnhnrl 16:3363b9f14913 1951 char PID_key;
tnhnrl 16:3363b9f14913 1952 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1953
tnhnrl 16:3363b9f14913 1954 // print the menu
tnhnrl 16:3363b9f14913 1955 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1956 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1957 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 16:3363b9f14913 1958 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 1959
tnhnrl 16:3363b9f14913 1960 // handle the key presses
tnhnrl 16:3363b9f14913 1961 while(1) {
tnhnrl 16:3363b9f14913 1962 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1963 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1964 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1965 }
tnhnrl 16:3363b9f14913 1966 else {
tnhnrl 16:3363b9f14913 1967 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1968 }
tnhnrl 16:3363b9f14913 1969
tnhnrl 16:3363b9f14913 1970 // handle the user's key input
tnhnrl 16:3363b9f14913 1971 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1972 _pitch_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1973 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1974 }
tnhnrl 16:3363b9f14913 1975 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1976 _pitch_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1977 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1978 }
tnhnrl 16:3363b9f14913 1979 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1980 _pitch_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1981 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1982 }
tnhnrl 16:3363b9f14913 1983 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1984 _pitch_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1985 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1986 }
tnhnrl 16:3363b9f14913 1987 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1988 _pitch_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1989 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1990 }
tnhnrl 16:3363b9f14913 1991 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1992 _pitch_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1993 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1994 }
tnhnrl 16:3363b9f14913 1995 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1996 // set global values
tnhnrl 21:38c8544db6f4 1997 pitchLoop().setControllerP(_pitch_KP);
tnhnrl 21:38c8544db6f4 1998 pitchLoop().setControllerI(_pitch_KI);
tnhnrl 21:38c8544db6f4 1999 pitchLoop().setControllerD(_pitch_KD);
tnhnrl 16:3363b9f14913 2000
tnhnrl 32:f2f8ae34aadc 2001 // save pitch PID values for outer loop (must save neutral position also)
tnhnrl 21:38c8544db6f4 2002 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 2003 break; //exit the while loop
tnhnrl 16:3363b9f14913 2004 }
tnhnrl 16:3363b9f14913 2005 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 2006 break; //exit the while loop
tnhnrl 16:3363b9f14913 2007 }
tnhnrl 16:3363b9f14913 2008 else {
tnhnrl 16:3363b9f14913 2009 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 2010 }
tnhnrl 16:3363b9f14913 2011 }
tnhnrl 16:3363b9f14913 2012 }
tnhnrl 20:8987a9ae2bc7 2013
tnhnrl 16:3363b9f14913 2014 float StateMachine::getDepthCommand() {
tnhnrl 32:f2f8ae34aadc 2015 return _depth_command;
tnhnrl 16:3363b9f14913 2016 }
tnhnrl 20:8987a9ae2bc7 2017
tnhnrl 16:3363b9f14913 2018 float StateMachine::getPitchCommand() {
tnhnrl 32:f2f8ae34aadc 2019 return _pitch_command;
tnhnrl 32:f2f8ae34aadc 2020 }
tnhnrl 32:f2f8ae34aadc 2021
tnhnrl 32:f2f8ae34aadc 2022 float StateMachine::getDepthReading() {
tnhnrl 32:f2f8ae34aadc 2023 return _depth_reading;
tnhnrl 32:f2f8ae34aadc 2024 }
tnhnrl 32:f2f8ae34aadc 2025
tnhnrl 32:f2f8ae34aadc 2026 float StateMachine::getPitchReading() {
tnhnrl 32:f2f8ae34aadc 2027 return _pitch_reading;
tnhnrl 32:f2f8ae34aadc 2028 }
tnhnrl 32:f2f8ae34aadc 2029
tnhnrl 32:f2f8ae34aadc 2030 float StateMachine::getTimerReading() {
tnhnrl 32:f2f8ae34aadc 2031 return _timer_reading;
tnhnrl 17:7c16b5671d0e 2032 }
tnhnrl 28:16c83a2fdefa 2033
tnhnrl 17:7c16b5671d0e 2034 void StateMachine::setState(int input_state) {
tnhnrl 21:38c8544db6f4 2035 _state = input_state;
tnhnrl 17:7c16b5671d0e 2036 }
tnhnrl 20:8987a9ae2bc7 2037
tnhnrl 17:7c16b5671d0e 2038 int StateMachine::getState() {
tnhnrl 17:7c16b5671d0e 2039 return _state; //return the current state of the system
tnhnrl 17:7c16b5671d0e 2040 }
tnhnrl 20:8987a9ae2bc7 2041
tnhnrl 17:7c16b5671d0e 2042 void StateMachine::setTimeout(float input_timeout) {
tnhnrl 17:7c16b5671d0e 2043 _timeout = input_timeout;
tnhnrl 17:7c16b5671d0e 2044 }
tnhnrl 20:8987a9ae2bc7 2045
tnhnrl 17:7c16b5671d0e 2046 void StateMachine::setDepthCommand(float input_depth_command) {
tnhnrl 32:f2f8ae34aadc 2047 _depth_command = input_depth_command;
tnhnrl 17:7c16b5671d0e 2048 }
tnhnrl 20:8987a9ae2bc7 2049
tnhnrl 17:7c16b5671d0e 2050 void StateMachine::setPitchCommand(float input_pitch_command) {
tnhnrl 32:f2f8ae34aadc 2051 _pitch_command = input_pitch_command;
tnhnrl 17:7c16b5671d0e 2052 }
tnhnrl 20:8987a9ae2bc7 2053
tnhnrl 17:7c16b5671d0e 2054 void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) {
tnhnrl 21:38c8544db6f4 2055 _neutral_batt_pos_mm = batt_pos_mm;
tnhnrl 21:38c8544db6f4 2056 _neutral_bce_pos_mm = bce_pos_mm;
tnhnrl 17:7c16b5671d0e 2057
tnhnrl 21:38c8544db6f4 2058 pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm);
tnhnrl 17:7c16b5671d0e 2059 }
tnhnrl 20:8987a9ae2bc7 2060
tnhnrl 17:7c16b5671d0e 2061 int StateMachine::timeoutRunning() {
tnhnrl 28:16c83a2fdefa 2062 return _isTimeoutRunning;
tnhnrl 17:7c16b5671d0e 2063 }
tnhnrl 20:8987a9ae2bc7 2064
tnhnrl 17:7c16b5671d0e 2065 //process one state at a time
tnhnrl 17:7c16b5671d0e 2066 void StateMachine::getDiveSequence() {
tnhnrl 17:7c16b5671d0e 2067 //iterate through this sequence using the FSM
tnhnrl 24:c7d9b5bf3829 2068 currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state;
tnhnrl 24:c7d9b5bf3829 2069 currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout;
tnhnrl 24:c7d9b5bf3829 2070 currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth;
tnhnrl 24:c7d9b5bf3829 2071 currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch;
tnhnrl 17:7c16b5671d0e 2072
tnhnrl 17:7c16b5671d0e 2073 _timeout = currentStateStruct.timeout; //set timeout before exiting this function
tnhnrl 32:f2f8ae34aadc 2074 }
tnhnrl 32:f2f8ae34aadc 2075
tnhnrl 32:f2f8ae34aadc 2076 void StateMachine::printCurrentSdLog() {
tnhnrl 32:f2f8ae34aadc 2077 pc().printf("SD card log work in progress\n\r");
tnhnrl 32:f2f8ae34aadc 2078 //might be worth saving the last few logs to the MBED...
tnhnrl 32:f2f8ae34aadc 2079 }
tnhnrl 32:f2f8ae34aadc 2080
tnhnrl 32:f2f8ae34aadc 2081 //check if the file is still opened
tnhnrl 32:f2f8ae34aadc 2082 void StateMachine::createNewFile() {
tnhnrl 32:f2f8ae34aadc 2083 if (_file_closed) {
tnhnrl 34:9b66c5188051 2084 //mbedLogger().createFile(); //create a new MBED file
tnhnrl 32:f2f8ae34aadc 2085
tnhnrl 32:f2f8ae34aadc 2086 _file_closed = false; //file is still open until you get to SIT_IDLE
tnhnrl 32:f2f8ae34aadc 2087 }
tnhnrl 32:f2f8ae34aadc 2088 }
tnhnrl 32:f2f8ae34aadc 2089
tnhnrl 32:f2f8ae34aadc 2090 void StateMachine::transmitData() {
tnhnrl 32:f2f8ae34aadc 2091 static float transmit_timer = 0;
tnhnrl 32:f2f8ae34aadc 2092 static bool is_transmit_timer_running = false;
tnhnrl 32:f2f8ae34aadc 2093
tnhnrl 32:f2f8ae34aadc 2094 if (!is_transmit_timer_running) {
tnhnrl 32:f2f8ae34aadc 2095 //pc().printf("\n\n\rTRANSMIT timer running...\n\n\r"); //debug
tnhnrl 32:f2f8ae34aadc 2096
tnhnrl 32:f2f8ae34aadc 2097 transmit_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 32:f2f8ae34aadc 2098 is_transmit_timer_running = true; //disable this block after one iteration
tnhnrl 32:f2f8ae34aadc 2099
tnhnrl 32:f2f8ae34aadc 2100 pc().printf("TESTING to see if this transmits once a second. (timer: %0.1f)\n\r", timer.read());
tnhnrl 32:f2f8ae34aadc 2101 }
tnhnrl 32:f2f8ae34aadc 2102 if (timer.read() >= transmit_timer) {
tnhnrl 32:f2f8ae34aadc 2103 is_transmit_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 32:f2f8ae34aadc 2104 }
tnhnrl 34:9b66c5188051 2105 }
tnhnrl 34:9b66c5188051 2106
tnhnrl 34:9b66c5188051 2107 void StateMachine::recordData(int input_state) {
tnhnrl 34:9b66c5188051 2108 string string_state;
tnhnrl 34:9b66c5188051 2109 if (input_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 2110 string_state = "SIT_IDLE";
tnhnrl 34:9b66c5188051 2111 else if (input_state == FIND_NEUTRAL)
tnhnrl 34:9b66c5188051 2112 string_state = "FIND_NEUTRAL";
tnhnrl 34:9b66c5188051 2113 else if (input_state == DIVE)
tnhnrl 34:9b66c5188051 2114 string_state = "DIVE";
tnhnrl 34:9b66c5188051 2115 else if (input_state == RISE)
tnhnrl 34:9b66c5188051 2116 string_state = "RISE";
tnhnrl 34:9b66c5188051 2117 else if (input_state == FLOAT_LEVEL)
tnhnrl 34:9b66c5188051 2118 string_state = "FLOAT_LEVEL";
tnhnrl 34:9b66c5188051 2119 else if (input_state == FLOAT_BROADCAST)
tnhnrl 34:9b66c5188051 2120 string_state = "FLOAT_BROADCAST";
tnhnrl 34:9b66c5188051 2121 else if (input_state == EMERGENCY_CLIMB)
tnhnrl 34:9b66c5188051 2122 string_state = "EMERGENCY_CLIMB";
tnhnrl 34:9b66c5188051 2123 else if (input_state == MULTI_DIVE)
tnhnrl 34:9b66c5188051 2124 string_state = "MULTI_DIVE";
tnhnrl 34:9b66c5188051 2125 else if (input_state == MULTI_RISE)
tnhnrl 34:9b66c5188051 2126 string_state = "MULTI_RISE";
tnhnrl 34:9b66c5188051 2127 else if (input_state == KEYBOARD)
tnhnrl 34:9b66c5188051 2128 string_state = "KEYBOARD";
tnhnrl 34:9b66c5188051 2129
tnhnrl 34:9b66c5188051 2130 if (!_is_log_timer_running) {
tnhnrl 34:9b66c5188051 2131 //pc().printf("\n\n\rlog timer running...\n\n\r"); //debug
tnhnrl 34:9b66c5188051 2132
tnhnrl 34:9b66c5188051 2133 _log_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 34:9b66c5188051 2134 _is_log_timer_running = true; //disable this block after one iteration
tnhnrl 34:9b66c5188051 2135
tnhnrl 37:357e98a929cc 2136 _data_log[0] = systemTime().read(); //system time reading
tnhnrl 34:9b66c5188051 2137 _data_log[1] = depthLoop().getCommand(); //depth command
tnhnrl 34:9b66c5188051 2138 _data_log[2] = depthLoop().getPosition(); //depth reading
tnhnrl 34:9b66c5188051 2139 _data_log[3] = pitchLoop().getCommand(); //pitch command
tnhnrl 34:9b66c5188051 2140 _data_log[4] = pitchLoop().getPosition(); //pitch reading
tnhnrl 34:9b66c5188051 2141 _data_log[5] = bce().getSetPosition_mm();
tnhnrl 34:9b66c5188051 2142 _data_log[6] = bce().getPosition_mm();
tnhnrl 34:9b66c5188051 2143 _data_log[7] = batt().getSetPosition_mm();
tnhnrl 34:9b66c5188051 2144 _data_log[8] = batt().getPosition_mm();
tnhnrl 34:9b66c5188051 2145
tnhnrl 34:9b66c5188051 2146 //record data to the MBED every 5 seconds
tnhnrl 35:2f66ea4863d5 2147 //mbedLogger().saveArrayToFile(string_state,input_state,_data_log);
tnhnrl 34:9b66c5188051 2148 }
tnhnrl 34:9b66c5188051 2149 if (timer.read() >= _log_timer) {
tnhnrl 34:9b66c5188051 2150 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 2151 }
tnhnrl 34:9b66c5188051 2152 }
tnhnrl 34:9b66c5188051 2153
tnhnrl 34:9b66c5188051 2154 void StateMachine::recordState(int input_state) {
tnhnrl 34:9b66c5188051 2155 string string_state;
tnhnrl 34:9b66c5188051 2156
tnhnrl 34:9b66c5188051 2157 if (input_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 2158 string_state = "SIT_IDLE";
tnhnrl 34:9b66c5188051 2159 else if (input_state == FIND_NEUTRAL)
tnhnrl 34:9b66c5188051 2160 string_state = "FIND_NEUTRAL";
tnhnrl 34:9b66c5188051 2161 else if (input_state == DIVE)
tnhnrl 34:9b66c5188051 2162 string_state = "DIVE";
tnhnrl 34:9b66c5188051 2163 else if (input_state == RISE)
tnhnrl 34:9b66c5188051 2164 string_state = "RISE";
tnhnrl 34:9b66c5188051 2165 else if (input_state == FLOAT_LEVEL)
tnhnrl 34:9b66c5188051 2166 string_state = "FLOAT_LEVEL";
tnhnrl 34:9b66c5188051 2167 else if (input_state == FLOAT_BROADCAST)
tnhnrl 34:9b66c5188051 2168 string_state = "FLOAT_BROADCAST";
tnhnrl 34:9b66c5188051 2169 else if (input_state == EMERGENCY_CLIMB)
tnhnrl 34:9b66c5188051 2170 string_state = "EMERGENCY_CLIMB";
tnhnrl 34:9b66c5188051 2171 else if (input_state == MULTI_DIVE)
tnhnrl 34:9b66c5188051 2172 string_state = "MULTI_DIVE";
tnhnrl 34:9b66c5188051 2173 else if (input_state == MULTI_RISE)
tnhnrl 34:9b66c5188051 2174 string_state = "MULTI_RISE";
tnhnrl 34:9b66c5188051 2175 else if (input_state == KEYBOARD)
tnhnrl 34:9b66c5188051 2176 string_state = "KEYBOARD";
tnhnrl 34:9b66c5188051 2177 //datalogger().printf("%s\n", string_state.c_str());
tnhnrl 36:966a86937e17 2178 }
tnhnrl 36:966a86937e17 2179
tnhnrl 36:966a86937e17 2180 float * StateMachine::dataArray() {
tnhnrl 36:966a86937e17 2181 //return the array to a calling function
tnhnrl 36:966a86937e17 2182 return _data_log;
tnhnrl 52:f207567d3ea4 2183 }
tnhnrl 52:f207567d3ea4 2184
tnhnrl 52:f207567d3ea4 2185 // 06/06/2018
tnhnrl 52:f207567d3ea4 2186 float StateMachine::getFloatUserInput() {
tnhnrl 52:f207567d3ea4 2187 float float_conversion = 0.0;
tnhnrl 52:f207567d3ea4 2188
tnhnrl 52:f207567d3ea4 2189 while(1) {
tnhnrl 52:f207567d3ea4 2190 bool valid_input = false; //flag for valid or invalid input
tnhnrl 52:f207567d3ea4 2191
tnhnrl 52:f207567d3ea4 2192 pc().printf("Please enter your number below and press ENTER:\n\r");
tnhnrl 52:f207567d3ea4 2193 char user_string [80]; //variable to store input as a character array
tnhnrl 52:f207567d3ea4 2194
tnhnrl 52:f207567d3ea4 2195 pc().scanf("%s", user_string); //read formatted data from stdin
tnhnrl 52:f207567d3ea4 2196 pc().printf("user_string was <%s>\n\r", user_string);
tnhnrl 52:f207567d3ea4 2197
tnhnrl 52:f207567d3ea4 2198 //check through the string for invalid characters (decimal values 43 through 57)
tnhnrl 52:f207567d3ea4 2199 for (int c = 0; c < strlen(user_string); c++) {
tnhnrl 52:f207567d3ea4 2200 //pc().printf("character is [%c]\n\r", user_string[c]); //debug
tnhnrl 52:f207567d3ea4 2201 if (user_string[c] >= 43 and user_string[c] <= 57) {
tnhnrl 52:f207567d3ea4 2202 //pc().printf("VALID CHARACTER!\n\r"); //debug
tnhnrl 52:f207567d3ea4 2203 ;
tnhnrl 52:f207567d3ea4 2204 }
tnhnrl 52:f207567d3ea4 2205 else {
tnhnrl 52:f207567d3ea4 2206 pc().printf("INVALID INPUT!\n\r");
tnhnrl 52:f207567d3ea4 2207 break;
tnhnrl 52:f207567d3ea4 2208 }
tnhnrl 52:f207567d3ea4 2209
tnhnrl 52:f207567d3ea4 2210 if (c == (strlen(user_string) - 1)) {
tnhnrl 52:f207567d3ea4 2211 valid_input = true;
tnhnrl 52:f207567d3ea4 2212 }
tnhnrl 52:f207567d3ea4 2213 }
tnhnrl 52:f207567d3ea4 2214
tnhnrl 52:f207567d3ea4 2215 if (valid_input) {
tnhnrl 52:f207567d3ea4 2216 float_conversion = atof(user_string);
tnhnrl 52:f207567d3ea4 2217 pc().printf("VALID INPUT! Your input was: %f\n\r", float_conversion);
tnhnrl 52:f207567d3ea4 2218 break;
tnhnrl 52:f207567d3ea4 2219 }
tnhnrl 52:f207567d3ea4 2220 }
tnhnrl 52:f207567d3ea4 2221
tnhnrl 52:f207567d3ea4 2222 return float_conversion;
tnhnrl 16:3363b9f14913 2223 }