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:
Fri Jun 15 20:11:39 2018 +0000
Revision:
58:94b7fd55185e
Parent:
57:ec69651c8c21
Child:
63:6cb0405fc6e6
For checking the menu only.

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