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:
joel_ssc
Date:
Mon May 13 19:25:26 2019 +0000
Revision:
92:52a91656458a
Parent:
88:1813f583cee9
version for first flight test, timeouts not yet set correctly

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
joel_ssc 85:dd8176285b6e 6 _yo_time = 40; // previously= 400 ; timeout for a dive or rise yo, not set for other ops
joel_ssc 85:dd8176285b6e 7 _state_transition_time = 20; // previously =60; time to allow motors to come to rest in float_broadcast
tnhnrl 28:16c83a2fdefa 8 _pitchTolerance = 5.0; // pitch angle tolerance for FLOAT_LEVEL state
tnhnrl 20:8987a9ae2bc7 9
tnhnrl 28:16c83a2fdefa 10 _bceFloatPosition = bce().getTravelLimit(); // bce position for "float" states (max travel limit for BCE is 320 mm)
tnhnrl 28:16c83a2fdefa 11 _battFloatPosition = batt().getTravelLimit(); // batt position tail high for "broadcast" state (max travel limit for battery is 75 mm)
joel_ssc 87:6d95f853dab3 12
joel_ssc 87:6d95f853dab3 13 _disconnect_batt_pos_mm = _battFloatPosition; // all the way forward
joel_ssc 87:6d95f853dab3 14 _batt_flying_pos_mm = 22.0;
joel_ssc 87:6d95f853dab3 15 _timeout_splashdown = 120; // two minutes??
joel_ssc 87:6d95f853dab3 16 _motorDisconnect_triggered = 0;
tnhnrl 32:f2f8ae34aadc 17 _depth_command = 2.0; // user keyboard depth (default)
tnhnrl 32:f2f8ae34aadc 18 _pitch_command = -20.0; // user keyboard pitch (default)
tnhnrl 58:94b7fd55185e 19 _heading_command = 0.0;
joel_ssc 87:6d95f853dab3 20 _start_swim_entry = SIT_IDLE;
joel_ssc 87:6d95f853dab3 21 _neutral_entry_state = SIT_IDLE;
tnhnrl 57:ec69651c8c21 22 //new commands
tnhnrl 58:94b7fd55185e 23 _BCE_dive_offset = 0.0; //starting at the limits
tnhnrl 58:94b7fd55185e 24 _BMM_dive_offset = 0.0;
tnhnrl 57:ec69651c8c21 25 //new commands
tnhnrl 57:ec69651c8c21 26
tnhnrl 28:16c83a2fdefa 27 _neutral_timer = 0; //timer used in FIND_NEUTRAL sub-FSM
joel_ssc 87:6d95f853dab3 28 _neutral_success = 0; // used if find neutral has not been run and neutral_setval is also not 1 from neutral.txt file
tnhnrl 73:f6f378311c8d 29 //////////////////////////////
joel_ssc 87:6d95f853dab3 30
tnhnrl 28:16c83a2fdefa 31 _state = SIT_IDLE; // select starting state here
tnhnrl 28:16c83a2fdefa 32 _isTimeoutRunning = false; // default timer to not running
tnhnrl 28:16c83a2fdefa 33 _isSubStateTimerRunning = false; // default timer to not running
tnhnrl 73:f6f378311c8d 34 /////////////////////////////
tnhnrl 24:c7d9b5bf3829 35 _multi_dive_counter = 0;
joel_ssc 82:0981b9ada820 36 _multi_leg_counter = 0;
tnhnrl 21:38c8544db6f4 37 _depth_KP = depthLoop().getControllerP(); // load current depth value
tnhnrl 21:38c8544db6f4 38 _depth_KI = depthLoop().getControllerI(); // load current depth value
tnhnrl 21:38c8544db6f4 39 _depth_KD = depthLoop().getControllerD(); // load current depth value
tnhnrl 21:38c8544db6f4 40
tnhnrl 21:38c8544db6f4 41 _pitch_KP = pitchLoop().getControllerP(); // load current pitch value
tnhnrl 21:38c8544db6f4 42 _pitch_KI = pitchLoop().getControllerI(); // load current pitch value
tnhnrl 21:38c8544db6f4 43 _pitch_KD = pitchLoop().getControllerD(); // load current pitch value
tnhnrl 21:38c8544db6f4 44
tnhnrl 21:38c8544db6f4 45 _neutral_bce_pos_mm = depthLoop().getOutputOffset(); //load current neutral buoyancy position offset
tnhnrl 21:38c8544db6f4 46 _neutral_batt_pos_mm = pitchLoop().getOutputOffset(); //load current neutral buoyancy position offset
tnhnrl 23:434f04ef1fad 47
tnhnrl 28:16c83a2fdefa 48 _state_array_counter = 1; //used to iterate through and record states
tnhnrl 28:16c83a2fdefa 49 _substate_array_counter = 0; //used to iterate through and record substates
tnhnrl 28:16c83a2fdefa 50
tnhnrl 28:16c83a2fdefa 51 _state_array[0] = SIT_IDLE; //system starts in the SIT_IDLE state, record this
tnhnrl 24:c7d9b5bf3829 52
tnhnrl 30:2964617e7676 53 _substate = NEUTRAL_SINKING; //start sub-FSM in NEUTRAL_SINKING
tnhnrl 28:16c83a2fdefa 54 _previous_substate = -1; //to start sub-FSM
tnhnrl 28:16c83a2fdefa 55 _previous_state = -1; //for tracking FSM states
tnhnrl 28:16c83a2fdefa 56
tnhnrl 28:16c83a2fdefa 57 _max_recorded_depth_neutral = -99; //float to record max depth
tnhnrl 28:16c83a2fdefa 58 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 32:f2f8ae34aadc 59
tnhnrl 32:f2f8ae34aadc 60 _max_recorded_auto_neutral_depth = -99;
tnhnrl 32:f2f8ae34aadc 61
tnhnrl 73:f6f378311c8d 62 _debug_menu_on = false; //toggle between debug and simple menu screens
tnhnrl 57:ec69651c8c21 63
tnhnrl 73:f6f378311c8d 64 //new file stuff
tnhnrl 73:f6f378311c8d 65 _pitch_filter_freq = pitchLoop().getFilterFrequency();
tnhnrl 73:f6f378311c8d 66 _pitch_deadband = pitchLoop().getDeadband();
tnhnrl 73:f6f378311c8d 67
tnhnrl 73:f6f378311c8d 68 _depth_filter_freq = depthLoop().getFilterFrequency();
tnhnrl 73:f6f378311c8d 69 _depth_deadband = depthLoop().getDeadband();
tnhnrl 16:3363b9f14913 70 }
tnhnrl 20:8987a9ae2bc7 71
tnhnrl 17:7c16b5671d0e 72 //Finite State Machine (FSM)
joel_ssc 85:dd8176285b6e 73 int StateMachine::runStateMachine() { // ends about line 1022
tnhnrl 16:3363b9f14913 74 // finite state machine ... each state has at least one exit criteria
joel_ssc 82:0981b9ada820 75 static int lpd_oneshots=0;
joel_ssc 82:0981b9ada820 76 static int lpr_oneshots=0;
joel_ssc 82:0981b9ada820 77 static int finish_leg=0; // allow a rise to complete ( if it takes less than a yo_time, then exit to FB)
joel_ssc 82:0981b9ada820 78 static float leg_max_depth = 0;
joel_ssc 82:0981b9ada820 79 static float leg_min_depth =0;
joel_ssc 82:0981b9ada820 80 static float leg_heading = 90; //go east!
joel_ssc 82:0981b9ada820 81 char buf[256];
tnhnrl 17:7c16b5671d0e 82 switch (_state) {
joel_ssc 85:dd8176285b6e 83 case ENDLEG_WAIT:
joel_ssc 85:dd8176285b6e 84 if(!_isTimeoutRunning) {
joel_ssc 85:dd8176285b6e 85 _yotimer.reset(); _yotimer.start(); _fsm_timer.reset(); _fsm_timer.start();
joel_ssc 85:dd8176285b6e 86 _isTimeoutRunning = 1;
joel_ssc 85:dd8176285b6e 87 }
joel_ssc 85:dd8176285b6e 88 keyboard(); //keyboard function now needs to know about this state and its timeout
joel_ssc 87:6d95f853dab3 89 break;
joel_ssc 87:6d95f853dab3 90 case FLYING_IDLE: // wait for some signal and the move the battery by a known amount
joel_ssc 87:6d95f853dab3 91 // then wait until you drop to START_SWIM on a timeout
joel_ssc 87:6d95f853dab3 92 if (!_isTimeoutRunning) { // start in the correct state, tell it once where to move
joel_ssc 87:6d95f853dab3 93 _fsm_timer.reset();
joel_ssc 87:6d95f853dab3 94 _fsm_timer.start();
joel_ssc 87:6d95f853dab3 95 _isTimeoutRunning = 1;
joel_ssc 87:6d95f853dab3 96 batt().unpause();
joel_ssc 87:6d95f853dab3 97 batt().setPosition_mm(_batt_flying_pos_mm); // do nothing - may include not bothering to log data - yes both SIT_IDLE and FLYING_IDLE do not log 27mar2019
joel_ssc 87:6d95f853dab3 98 sprintf(buf, "FLYING_IDLE started in statemachine\n\n\r");
joel_ssc 87:6d95f853dab3 99 mbedLogger().appendDiagFile(buf,0);
joel_ssc 87:6d95f853dab3 100 }
joel_ssc 87:6d95f853dab3 101 if (motorDisconnect().read() || (_fsm_timer > 13)) { // for real case, only test is motorDisconnect().read(), timeout check is for testing only
joel_ssc 87:6d95f853dab3 102 _motorDisconnect_triggered = 1; // once hardware has switched once, do not need it again
joel_ssc 87:6d95f853dab3 103 // move the battery
joel_ssc 87:6d95f853dab3 104 // reset and start the timer for post disconnect wait
joel_ssc 87:6d95f853dab3 105 sprintf(buf, "FLYING_IDLE: motorDisconnect.read() at timeout=13sec, to end\n\r");
joel_ssc 87:6d95f853dab3 106 mbedLogger().appendDiagFile(buf,0);
joel_ssc 87:6d95f853dab3 107 _fsm_timer.reset();
joel_ssc 87:6d95f853dab3 108 _fsm_timer.start();
joel_ssc 87:6d95f853dab3 109 _isTimeoutRunning = 1;
joel_ssc 87:6d95f853dab3 110 batt().unpause(); //this is now active
joel_ssc 87:6d95f853dab3 111 batt().setPosition_mm(_disconnect_batt_pos_mm);
joel_ssc 87:6d95f853dab3 112
joel_ssc 87:6d95f853dab3 113 }
joel_ssc 87:6d95f853dab3 114 _timeout_splashdown = 6; // timeout_splashdown reduced to 6 seconds for testing, usually 120 sec
joel_ssc 87:6d95f853dab3 115 if( _motorDisconnect_triggered && _isTimeoutRunning) {
joel_ssc 87:6d95f853dab3 116 // how end - timeout
joel_ssc 87:6d95f853dab3 117 if (_fsm_timer > _timeout_splashdown) { // timeout_splashdown reduced to 6 seconds for testing, usually 120 sec
joel_ssc 87:6d95f853dab3 118 xbee().printf("FLYING_IDLE ended: timed out! Go to START_SWIM \r\n"); // go to start_swim
joel_ssc 87:6d95f853dab3 119 //tare the pressure sensor
joel_ssc 87:6d95f853dab3 120 depth().tare(); // tares to ambient (do on surface)
joel_ssc 87:6d95f853dab3 121 _state = START_SWIM; // alternately, go into the legStruct and set start_swim now? rather than leg, or leg_position_dive
joel_ssc 87:6d95f853dab3 122 _start_swim_entry = FLYING_IDLE;
joel_ssc 87:6d95f853dab3 123 sprintf(buf, "FLYING_IDLE ended on splashdown timeout go to Start_swim\n\n\r");
joel_ssc 87:6d95f853dab3 124 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 125
joel_ssc 87:6d95f853dab3 126 //sprintf(buf, "FLYING_IDLE ended on splashdown timeout. FOR testing go to ENDLEG_WAIT instead of go to Start_swim\n\n\r");
joel_ssc 87:6d95f853dab3 127 //mbedLogger().appendDiagFile(buf,0);
joel_ssc 87:6d95f853dab3 128 //_state = ENDLEG_WAIT;
joel_ssc 87:6d95f853dab3 129 _fsm_timer.reset();
joel_ssc 87:6d95f853dab3 130 _isTimeoutRunning = false;
joel_ssc 87:6d95f853dab3 131 }
joel_ssc 87:6d95f853dab3 132 }
joel_ssc 87:6d95f853dab3 133 break;
joel_ssc 85:dd8176285b6e 134 case SIT_IDLE: // sit_idle and fb_exit fall through to keyboard actions, since they do not have break statements
joel_ssc 85:dd8176285b6e 135 case FB_EXIT:
tnhnrl 73:f6f378311c8d 136 case KEYBOARD :
joel_ssc 85:dd8176285b6e 137 // there actually is no timeout test for SIT_IDLE, but this enables some one-shot actions
joel_ssc 85:dd8176285b6e 138 if (!_isTimeoutRunning) { // presumably the first time back in this block, whatever ended stopped the timeout_running.
tnhnrl 73:f6f378311c8d 139 //tare pressure sensor
tnhnrl 73:f6f378311c8d 140 depth().tare(); // tares to ambient (do on surface)
tnhnrl 57:ec69651c8c21 141
tnhnrl 57:ec69651c8c21 142 if (_debug_menu_on)
tnhnrl 69:919ac8d7e023 143 printDebugMenu();
tnhnrl 57:ec69651c8c21 144 else
tnhnrl 69:919ac8d7e023 145 printSimpleMenu();
tnhnrl 74:d281aaef9766 146 xbee().printf("\r\n\nstate: SIT_IDLE\r\n");
tnhnrl 28:16c83a2fdefa 147 _isTimeoutRunning = true;
tnhnrl 20:8987a9ae2bc7 148
tnhnrl 16:3363b9f14913 149 // what is active?
tnhnrl 16:3363b9f14913 150 bce().pause();
tnhnrl 16:3363b9f14913 151 batt().pause();
tnhnrl 32:f2f8ae34aadc 152
tnhnrl 17:7c16b5671d0e 153 //reset sub FSM
tnhnrl 28:16c83a2fdefa 154 _isSubStateTimerRunning = false;
tnhnrl 16:3363b9f14913 155 }
tnhnrl 20:8987a9ae2bc7 156
tnhnrl 16:3363b9f14913 157 // how exit?
tnhnrl 20:8987a9ae2bc7 158 keyboard(); // keyboard function will change the state if needed
tnhnrl 16:3363b9f14913 159 break;
tnhnrl 49:47ffa4feb6db 160
tnhnrl 49:47ffa4feb6db 161 case CHECK_TUNING : // state used to check the tuning of the pressure vessel
tnhnrl 49:47ffa4feb6db 162 // start local state timer and init any other one-shot actions
tnhnrl 49:47ffa4feb6db 163 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 164 xbee().printf("\r\n\nstate: CHECK_TUNING\r\n");
tnhnrl 73:f6f378311c8d 165 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 166 _fsm_timer.start(); // background timer starts running
tnhnrl 49:47ffa4feb6db 167 _isTimeoutRunning = true;
tnhnrl 49:47ffa4feb6db 168
tnhnrl 49:47ffa4feb6db 169 // what needs to be started?
tnhnrl 49:47ffa4feb6db 170 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 171 batt().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 172
tnhnrl 49:47ffa4feb6db 173 // what are the commands? (DRIVE THE MOTORS "DIRECTLY")
tnhnrl 49:47ffa4feb6db 174 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 175 batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 176
tnhnrl 49:47ffa4feb6db 177 // getSetPosition_mm is the commanded position in the LinearActuator class
tnhnrl 49:47ffa4feb6db 178
tnhnrl 74:d281aaef9766 179 xbee().printf("CHECK_TUNING: BCE cmd: %3.1f (BCE current position: %3.1f)\r\n", bce().getSetPosition_mm(), bce().getPosition_mm());
tnhnrl 74:d281aaef9766 180 xbee().printf("CHECK_TUNING: BATT cmd: %3.1f (BATT current position: %3.1f)\r\n", batt().getSetPosition_mm(), bce().getPosition_mm());
tnhnrl 49:47ffa4feb6db 181 }
tnhnrl 49:47ffa4feb6db 182
tnhnrl 49:47ffa4feb6db 183 // how exit?
tnhnrl 73:f6f378311c8d 184 if (_fsm_timer > _timeout) {
tnhnrl 74:d281aaef9766 185 xbee().printf("CHECK_TUNING: timed out!\r\n");
tnhnrl 49:47ffa4feb6db 186 _state = FLOAT_BROADCAST;
tnhnrl 73:f6f378311c8d 187 _fsm_timer.reset();
tnhnrl 49:47ffa4feb6db 188 _isTimeoutRunning = false;
tnhnrl 49:47ffa4feb6db 189 }
tnhnrl 49:47ffa4feb6db 190
tnhnrl 67:c86a4b464682 191 //WHAT IS ACTIVE?
tnhnrl 67:c86a4b464682 192 // the inner loop position controls are maintaining the positions of the linear actuators
tnhnrl 67:c86a4b464682 193
tnhnrl 49:47ffa4feb6db 194 //print status to screen continuously
joel_ssc 85:dd8176285b6e 195 xbee().printf("CHECK_TUNING: BCE_position: %0.1f, BATT_position: %0.1f (BCE_cmd: %0.1f, BATT_cmd: %0.1f)(depth: %0.1f m,pitch: %0.1f deg,heading: %0.1f) [%0.1f sec]\r",
joel_ssc 85:dd8176285b6e 196 bce().getPosition_mm(),batt().getPosition_mm(),bce().getSetPosition_mm(),batt().getSetPosition_mm(),depthLoop().getPosition(),
joel_ssc 85:dd8176285b6e 197 pitchLoop().getPosition(),imu().getHeading(),_fsm_timer.read());
tnhnrl 49:47ffa4feb6db 198
tnhnrl 49:47ffa4feb6db 199 break;
tnhnrl 20:8987a9ae2bc7 200
tnhnrl 16:3363b9f14913 201 case EMERGENCY_CLIMB :
tnhnrl 16:3363b9f14913 202 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 203 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 204 xbee().printf("\r\n\nstate: EMERGENCY_CLIMB\r\n");
tnhnrl 73:f6f378311c8d 205 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 206 _fsm_timer.start(); // background timer starts running
joel_ssc 84:eccd8e837134 207 _yotimer.reset();
joel_ssc 84:eccd8e837134 208 _yotimer.start();
tnhnrl 28:16c83a2fdefa 209 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 210
tnhnrl 16:3363b9f14913 211 // what needs to be started?
tnhnrl 16:3363b9f14913 212 bce().unpause();
tnhnrl 16:3363b9f14913 213 batt().unpause();
tnhnrl 20:8987a9ae2bc7 214
tnhnrl 20:8987a9ae2bc7 215 // what are the commands?
tnhnrl 16:3363b9f14913 216 bce().setPosition_mm(bce().getTravelLimit());
tnhnrl 73:f6f378311c8d 217 batt().setPosition_mm(10.0); //pull nose up (0.0 was sketchy)
tnhnrl 16:3363b9f14913 218 }
tnhnrl 20:8987a9ae2bc7 219
tnhnrl 16:3363b9f14913 220 // how exit?
joel_ssc 82:0981b9ada820 221 if ((_fsm_timer > _timeout ) || (_yotimer > _yo_time)) {
tnhnrl 74:d281aaef9766 222 xbee().printf("EC: timed out\r\n");
tnhnrl 21:38c8544db6f4 223 _state = FLOAT_BROADCAST;
joel_ssc 82:0981b9ada820 224 _fsm_timer.reset(); _yotimer.reset();
tnhnrl 28:16c83a2fdefa 225 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 226 }
tnhnrl 26:7e118fc02eea 227 else if (depthLoop().getPosition() < 2.0) { //if the depth is greater than 0.2 feet, go to float broadcast
tnhnrl 21:38c8544db6f4 228 _state = FLOAT_BROADCAST;
joel_ssc 84:eccd8e837134 229 _fsm_timer.reset(); _yotimer.reset();
tnhnrl 28:16c83a2fdefa 230 _isTimeoutRunning = false;
joel_ssc 87:6d95f853dab3 231 sprintf(buf, "EMERGENCY_CLIMB - at surface ... now go to FLOAT_BROADCAST\n\n\r");
joel_ssc 87:6d95f853dab3 232 mbedLogger().appendDiagFile(buf,3);
tnhnrl 16:3363b9f14913 233 }
tnhnrl 32:f2f8ae34aadc 234
tnhnrl 67:c86a4b464682 235 //WHAT IS ACTIVE?
tnhnrl 34:9b66c5188051 236 //print status to screen continuously
tnhnrl 74:d281aaef9766 237 xbee().printf("EC: depth: %3.1f, pitch: %0.1f deg [BCE:%0.1f (cmd: %0.1f) BMM:%0.1f (cmd: %0.1f)] [%0.1f sec]\r",depthLoop().getPosition(),pitchLoop().getPosition(),bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(),_fsm_timer.read());
tnhnrl 32:f2f8ae34aadc 238
tnhnrl 16:3363b9f14913 239 break;
tnhnrl 20:8987a9ae2bc7 240
tnhnrl 16:3363b9f14913 241 case FIND_NEUTRAL :
tnhnrl 20:8987a9ae2bc7 242 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 243 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 244 xbee().printf("\r\n\nstate: FIND_NEUTRAL\r\n");
tnhnrl 73:f6f378311c8d 245 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 246 _fsm_timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 247 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 248
tnhnrl 16:3363b9f14913 249 // what needs to be started?
tnhnrl 16:3363b9f14913 250 bce().unpause();
tnhnrl 16:3363b9f14913 251 batt().unpause();
tnhnrl 74:d281aaef9766 252
tnhnrl 74:d281aaef9766 253 //start with a small offset from MANUAL neutral positions on the BCE only, BMM was pitching too much
tnhnrl 74:d281aaef9766 254 float bce_find_neutral_mm = _neutral_bce_pos_mm + 10.0;
tnhnrl 74:d281aaef9766 255 //float batt_find_neutral_mm = _neutral_batt_pos_mm + 10.0;
tnhnrl 74:d281aaef9766 256
tnhnrl 74:d281aaef9766 257 bce().setPosition_mm(bce_find_neutral_mm);
tnhnrl 74:d281aaef9766 258 batt().setPosition_mm(_neutral_batt_pos_mm); //set battery to the same neutral position
tnhnrl 17:7c16b5671d0e 259
tnhnrl 24:c7d9b5bf3829 260 //first iteration goes into Neutral Finding Sub-FSM
tnhnrl 24:c7d9b5bf3829 261 //set the first state of the FSM, and start the sub-FSM
tnhnrl 30:2964617e7676 262 _substate = NEUTRAL_SINKING; //first state in neutral sub-FSM is the pressure vessel sinking
tnhnrl 28:16c83a2fdefa 263 _previous_substate = -1;
tnhnrl 28:16c83a2fdefa 264
tnhnrl 28:16c83a2fdefa 265 //save this state to the array
tnhnrl 30:2964617e7676 266 _substate_array[_substate_array_counter] = NEUTRAL_SINKING; //save to state array
tnhnrl 28:16c83a2fdefa 267 _substate_array_counter++;
tnhnrl 28:16c83a2fdefa 268
tnhnrl 32:f2f8ae34aadc 269 runNeutralStateMachine();
tnhnrl 16:3363b9f14913 270 }
tnhnrl 20:8987a9ae2bc7 271
tnhnrl 20:8987a9ae2bc7 272 // how exit? (exit with the timer, if timer still running continue processing sub FSM)
tnhnrl 73:f6f378311c8d 273 if (_fsm_timer > _timeout) {
tnhnrl 74:d281aaef9766 274 xbee().printf("FN: timed out [time: %0.1f sec]\r\n", _fsm_timer.read());
joel_ssc 87:6d95f853dab3 275 sprintf(buf, "FIND_NEUTRAL - timed out roll=%f depth = %f now exit to EMERG_CLIMB\n\n", imu().getRoll(), depthLoop().getPosition());
joel_ssc 87:6d95f853dab3 276 mbedLogger().appendDiagFile(buf,3);
tnhnrl 21:38c8544db6f4 277 _state = EMERGENCY_CLIMB; //new behavior (if this times out it emergency surfaces)
tnhnrl 73:f6f378311c8d 278 _fsm_timer.reset();
joel_ssc 87:6d95f853dab3 279 sprintf(buf, "FN: timed out [time:%0.1f sec], _neutral_success = -1 for info to float_broadcast\n", _fsm_timer.read());
joel_ssc 87:6d95f853dab3 280 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 281 _fsm_timer.reset();
joel_ssc 87:6d95f853dab3 282 _neutral_success = -1;
tnhnrl 28:16c83a2fdefa 283 _isTimeoutRunning = false;
tnhnrl 24:c7d9b5bf3829 284
tnhnrl 24:c7d9b5bf3829 285 //record this to the NEUTRAL sub-FSM tracker
tnhnrl 28:16c83a2fdefa 286 _substate_array[_substate_array_counter] = EMERGENCY_CLIMB; //save to state array
tnhnrl 28:16c83a2fdefa 287 _substate_array_counter++;
tnhnrl 16:3363b9f14913 288 }
tnhnrl 21:38c8544db6f4 289
joel_ssc 87:6d95f853dab3 290
tnhnrl 24:c7d9b5bf3829 291 //what is active? (neutral finding sub-function runs until completion)
tnhnrl 24:c7d9b5bf3829 292 //check if substate returned exit state, if so stop running the sub-FSM
joel_ssc 87:6d95f853dab3 293
joel_ssc 87:6d95f853dab3 294 else if (runNeutralStateMachine() == NEUTRAL_EXIT) { // but this line will repeatedly runneutralstatemachine as long as the overall state is FIND_NEUTRAL
tnhnrl 21:38c8544db6f4 295 //if successful, FIND_NEUTRAL then goes to RISE
tnhnrl 74:d281aaef9766 296 xbee().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\r\n\n");
joel_ssc 87:6d95f853dab3 297 sprintf(buf, "FIND_NEUTRAL - at Neutral EXIT now go to RISE\n\n");
joel_ssc 87:6d95f853dab3 298 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 299 if(_neutral_success == 0 ) { // failed in finding neutral - bad exit states
joel_ssc 87:6d95f853dab3 300 configFileIO().report_no_neutral_found( bce().getPosition_mm(), batt().getPosition_mm()); // this is a message "no_float.txt"
joel_ssc 87:6d95f853dab3 301 //to the raspberry Pi control computer
joel_ssc 87:6d95f853dab3 302 }
joel_ssc 87:6d95f853dab3 303 _state = RISE; // adjust this to leg_position_dive with an if statement - if not sent via keyboard
joel_ssc 87:6d95f853dab3 304 // also set flags/structures that neutral was found successfully.
joel_ssc 87:6d95f853dab3 305 if (_neutral_entry_state == START_SWIM ) {
joel_ssc 87:6d95f853dab3 306 _state = EMERGENCY_CLIMB; finish_leg =1;
joel_ssc 87:6d95f853dab3 307 sprintf(buf, "FN neutral_EXIT: but entry via start_swim, so radio call is next\n\n\r");
joel_ssc 87:6d95f853dab3 308 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 309 } //exit case when not entered by keyboard
tnhnrl 30:2964617e7676 310 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 311 }
tnhnrl 17:7c16b5671d0e 312 break;
tnhnrl 17:7c16b5671d0e 313
tnhnrl 16:3363b9f14913 314 case DIVE :
tnhnrl 16:3363b9f14913 315 // start local state timer and init any other one-shot actions
tnhnrl 32:f2f8ae34aadc 316
tnhnrl 28:16c83a2fdefa 317 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 318 xbee().printf("\r\n\nstate: DIVE\r\n");
tnhnrl 73:f6f378311c8d 319 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 320 _fsm_timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 321 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 322
tnhnrl 16:3363b9f14913 323 // what needs to be started?
tnhnrl 16:3363b9f14913 324 bce().unpause();
tnhnrl 16:3363b9f14913 325 batt().unpause();
tnhnrl 20:8987a9ae2bc7 326
tnhnrl 16:3363b9f14913 327 // what are the commands?
tnhnrl 32:f2f8ae34aadc 328 depthLoop().setCommand(_depth_command);
tnhnrl 32:f2f8ae34aadc 329 pitchLoop().setCommand(_pitch_command);
tnhnrl 32:f2f8ae34aadc 330
tnhnrl 74:d281aaef9766 331 headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
tnhnrl 74:d281aaef9766 332
tnhnrl 74:d281aaef9766 333 xbee().printf("DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand());
tnhnrl 74:d281aaef9766 334 xbee().printf("DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 74:d281aaef9766 335 xbee().printf("DIVE: heading cmd: %3.1f\r\n",headingLoop().getCommand());
tnhnrl 28:16c83a2fdefa 336
tnhnrl 28:16c83a2fdefa 337 //reset max dive depth
tnhnrl 28:16c83a2fdefa 338 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 16:3363b9f14913 339 }
tnhnrl 20:8987a9ae2bc7 340
tnhnrl 16:3363b9f14913 341 // how exit?
tnhnrl 73:f6f378311c8d 342 if (_fsm_timer.read() > _timeout) {
tnhnrl 74:d281aaef9766 343 xbee().printf("DIVE: timed out\r\n\n");
tnhnrl 21:38c8544db6f4 344 _state = RISE; //new behavior 11/17/2017
tnhnrl 73:f6f378311c8d 345 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 346 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 347 }
tnhnrl 32:f2f8ae34aadc 348 else if (depthLoop().getPosition() > depthLoop().getCommand() - 0.5) { // including offset for low momentum approaches
tnhnrl 74:d281aaef9766 349 xbee().printf("DIVE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 21:38c8544db6f4 350 _state = RISE;
tnhnrl 73:f6f378311c8d 351 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 352 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 353 }
tnhnrl 20:8987a9ae2bc7 354
tnhnrl 67:c86a4b464682 355 // WHAT IS ACTIVE?
joel_ssc 85:dd8176285b6e 356 xbee().printf("DIVE: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg [cmd:%6.1f], heading_imu:%6.1f deg>>[%0.2f sec] \r",
joel_ssc 85:dd8176285b6e 357 bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_deg(),
joel_ssc 85:dd8176285b6e 358 depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),pitchLoop().getCommand(),imu().getHeading(),_fsm_timer.read());
tnhnrl 32:f2f8ae34aadc 359 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 16:3363b9f14913 360 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 28:16c83a2fdefa 361
tnhnrl 74:d281aaef9766 362 // ACTIVE RUDDER CONTROL
tnhnrl 74:d281aaef9766 363 rudder().setPosition_deg(headingLoop().getOutput());
tnhnrl 74:d281aaef9766 364
tnhnrl 28:16c83a2fdefa 365 if (depthLoop().getPosition() > _max_recorded_depth_dive) { //debug
tnhnrl 28:16c83a2fdefa 366 _max_recorded_depth_dive = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 367 }
tnhnrl 32:f2f8ae34aadc 368
tnhnrl 16:3363b9f14913 369 break;
joel_ssc 87:6d95f853dab3 370 case START_SWIM :
joel_ssc 87:6d95f853dab3 371 // start local state timer and init any other one-shot actions
joel_ssc 87:6d95f853dab3 372 if (!_isTimeoutRunning) {
joel_ssc 87:6d95f853dab3 373 xbee().printf("\r\n\nstate: START_SWIM\r\n");
joel_ssc 87:6d95f853dab3 374 _fsm_timer.reset(); // timer goes back to zero
joel_ssc 87:6d95f853dab3 375 _fsm_timer.start(); // background timer starts running
joel_ssc 87:6d95f853dab3 376 sprintf(buf, "START_SWIM begun \n\r");
joel_ssc 87:6d95f853dab3 377 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 378 _yotimer.reset();
joel_ssc 87:6d95f853dab3 379 _yotimer.start();
joel_ssc 87:6d95f853dab3 380 _isTimeoutRunning = true;
joel_ssc 87:6d95f853dab3 381 //tare the pressure sensor
joel_ssc 87:6d95f853dab3 382 depth().tare(); // tares to ambient (do on surface) HOW certain am I that this tare only happens at surface?
joel_ssc 87:6d95f853dab3 383 if( fabs(imu().getRoll()) > 70 && depthLoop().getPosition() < 9.0) { //inversion operations
joel_ssc 87:6d95f853dab3 384 // what needs to be started?
joel_ssc 87:6d95f853dab3 385 bce().unpause();
joel_ssc 87:6d95f853dab3 386 batt().unpause();
joel_ssc 87:6d95f853dab3 387 rudder().unpause();
joel_ssc 87:6d95f853dab3 388 _depth_command = 10; // this should be meters depth
joel_ssc 87:6d95f853dab3 389 _heading_command = 90;
joel_ssc 87:6d95f853dab3 390 _timeout = 180;
joel_ssc 87:6d95f853dab3 391 sprintf(buf, "START_SWIM found upside down, starting to descend imu().getRoll()= %f \n\r", imu().getRoll());
joel_ssc 87:6d95f853dab3 392 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 393
joel_ssc 87:6d95f853dab3 394 // what are the commands? (using inner loops except for heading outer loop)
joel_ssc 87:6d95f853dab3 395 // These actions happen ONCE in the POSITION_DIVE sequence
joel_ssc 87:6d95f853dab3 396
joel_ssc 87:6d95f853dab3 397 //DEPTH and HEADING COMMANDS first
joel_ssc 87:6d95f853dab3 398 depthLoop().setCommand(_depth_command);
joel_ssc 87:6d95f853dab3 399 headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 87:6d95f853dab3 400
joel_ssc 87:6d95f853dab3 401 // move BCE and batt to new target positions
joel_ssc 87:6d95f853dab3 402 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
joel_ssc 87:6d95f853dab3 403 // depthloop().getOutput() will tell bce() to basically hold at neutral if you are at the desired depth ( assuming that zeroOffset for depth is correct)
joel_ssc 87:6d95f853dab3 404
joel_ssc 87:6d95f853dab3 405 batt().setPosition_mm(pitchLoop().getOutput()); //these together should hold a depth, rather than turn around and rise
joel_ssc 87:6d95f853dab3 406 //batt().setPosition_mm(_neutral_batt_pos_mm + _BMM_dive_offset); // dare not use neutral postions before knowing what neutral is
joel_ssc 87:6d95f853dab3 407 //bce().setPosition_mm(_neutral_bce_pos_mm - _BCE_dive_offset); // dare not use neutral postions before knowing what neutral is
joel_ssc 87:6d95f853dab3 408
joel_ssc 87:6d95f853dab3 409
joel_ssc 87:6d95f853dab3 410
joel_ssc 87:6d95f853dab3 411 xbee().printf("START_SWIM: inversion BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //g
joel_ssc 87:6d95f853dab3 412 xbee().printf("START_SWIM: inversion BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //g
joel_ssc 87:6d95f853dab3 413 xbee().printf("START_SWIM: inversion starting roll = : %3.1f\r\n",imu().getRoll() ); //g
joel_ssc 87:6d95f853dab3 414 } //end of inversion section
joel_ssc 87:6d95f853dab3 415 if( fabs(imu().getRoll()) >70 && depthLoop().getPosition() > 9.0) { //still inverted
joel_ssc 87:6d95f853dab3 416 // what needs to be started?
joel_ssc 87:6d95f853dab3 417 bce().unpause();
joel_ssc 87:6d95f853dab3 418 batt().unpause();
joel_ssc 87:6d95f853dab3 419 rudder().unpause();
joel_ssc 87:6d95f853dab3 420 _depth_command = 9;
joel_ssc 87:6d95f853dab3 421 _heading_command = 90;
joel_ssc 87:6d95f853dab3 422 _timeout = 120;
joel_ssc 87:6d95f853dab3 423 _pitch_command = 0;
joel_ssc 87:6d95f853dab3 424
joel_ssc 87:6d95f853dab3 425 //DEPTH COMMAND
joel_ssc 87:6d95f853dab3 426 depthLoop().setCommand(_depth_command);
joel_ssc 87:6d95f853dab3 427 headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 87:6d95f853dab3 428
joel_ssc 87:6d95f853dab3 429 // what are the commands? (using inner loops except for heading outer loop)
joel_ssc 87:6d95f853dab3 430 // These actions happen ONCE in the POSITION_DIVE sequence
joel_ssc 87:6d95f853dab3 431 //batt().setPosition_mm(_neutral_batt_pos_mm - _BMM_dive_offset); // is this right ?? looks like climb commands
joel_ssc 87:6d95f853dab3 432 //bce().setPosition_mm(_neutral_bce_pos_mm + _BCE_dive_offset); // dare not use neutral postions before knowing what neutral is
joel_ssc 87:6d95f853dab3 433 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
joel_ssc 87:6d95f853dab3 434 batt().setPosition_mm(pitchLoop().getOutput()); //these together should hold a depth, rather than turn around and rise
joel_ssc 87:6d95f853dab3 435
joel_ssc 87:6d95f853dab3 436
joel_ssc 87:6d95f853dab3 437 xbee().printf("START_SWIM: inversion BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //g
joel_ssc 87:6d95f853dab3 438 xbee().printf("START_SWIM: inversion BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //g
joel_ssc 87:6d95f853dab3 439 xbee().printf("START_SWIM: inversion starting roll = : %3.1f\r\n",imu().getRoll() ); //g
joel_ssc 87:6d95f853dab3 440 sprintf(buf, "START_SWIM still upside down but depth>9m, set up to limit descent: depth = %f roll=%f\n\n\r", depthLoop().getPosition(), imu().getRoll());
joel_ssc 87:6d95f853dab3 441 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 442 }
joel_ssc 87:6d95f853dab3 443 if( fabs(imu().getRoll()) <70 && _start_swim_entry == FLYING_IDLE ) { // starts out right side up, still inside first call to start_swim
joel_ssc 87:6d95f853dab3 444 finish_leg = 1; //this works to get to FB_EXIT,
joel_ssc 87:6d95f853dab3 445 _state = FLOAT_BROADCAST;
joel_ssc 87:6d95f853dab3 446 _fsm_timer.reset(); _yotimer.reset();
joel_ssc 87:6d95f853dab3 447 _isTimeoutRunning = false;
joel_ssc 87:6d95f853dab3 448 sprintf(buf, "START_SWIM now upright, called by flying_idle, so end now. roll= %f depth = %f\n\r", imu().getRoll(), depthLoop().getPosition());
joel_ssc 87:6d95f853dab3 449 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 450 }
joel_ssc 87:6d95f853dab3 451 if( fabs(imu().getRoll()) <70 && _start_swim_entry != FLYING_IDLE ) { // starts out right side up, on second on/off cycle
joel_ssc 87:6d95f853dab3 452 _state = FIND_NEUTRAL;
joel_ssc 87:6d95f853dab3 453 _neutral_entry_state = START_SWIM;
joel_ssc 87:6d95f853dab3 454 _fsm_timer.reset(); _yotimer.reset();
joel_ssc 87:6d95f853dab3 455 _isTimeoutRunning = false;
joel_ssc 87:6d95f853dab3 456 sprintf(buf, "START_SWIM not called by FLYING_IDLE, so go to find_neutral: depth= %f\n\r", depthLoop().getPosition());
joel_ssc 87:6d95f853dab3 457 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 458 }
joel_ssc 87:6d95f853dab3 459 } //end of timeout running ==0
joel_ssc 87:6d95f853dab3 460
joel_ssc 87:6d95f853dab3 461 // how exit? keep diving and watching depth while also watching imu().getRoll()
joel_ssc 87:6d95f853dab3 462 if ((_fsm_timer > _timeout ) || (_yotimer > _yo_time)) { // this is bad, still upside down.
joel_ssc 87:6d95f853dab3 463 xbee().printf("start_swim: timed out\r\n");
joel_ssc 87:6d95f853dab3 464 sprintf(buf, "start_swim timed out on yo_time or timeout - still upside down bad - go to FLOAT_BROADCAST roll=%f \n\n\r", imu().getRoll());
joel_ssc 87:6d95f853dab3 465 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 466 configFileIO().report_still_inverted( fabs(imu().getRoll()), _yo_time); // tells how long you waited, puts in file "inverted.txt"
joel_ssc 87:6d95f853dab3 467 _state = FLOAT_BROADCAST;
joel_ssc 87:6d95f853dab3 468 _fsm_timer.reset(); _yotimer.reset();
joel_ssc 87:6d95f853dab3 469 _isTimeoutRunning = false;
joel_ssc 87:6d95f853dab3 470 }
joel_ssc 87:6d95f853dab3 471 else if (fabs(imu().getRoll()) <30 ) { // pretty much righted itself go to FIND_NEUTRAL
joel_ssc 87:6d95f853dab3 472 _state = FIND_NEUTRAL;
joel_ssc 87:6d95f853dab3 473 _neutral_entry_state = START_SWIM;
joel_ssc 87:6d95f853dab3 474 sprintf(buf, "START_SWIM - turned self upright roll=%f depth = %f , now go toFIND_NEUTRAL\n\n\r", imu().getRoll(), depthLoop().getPosition());
joel_ssc 87:6d95f853dab3 475 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 476 // print message ?
joel_ssc 87:6d95f853dab3 477 _fsm_timer.reset(); _yotimer.reset();
joel_ssc 87:6d95f853dab3 478 _isTimeoutRunning = false;
joel_ssc 87:6d95f853dab3 479 }
joel_ssc 87:6d95f853dab3 480
joel_ssc 87:6d95f853dab3 481 //WHAT IS ACTIVE?
joel_ssc 87:6d95f853dab3 482 //print status to screen continuously
joel_ssc 87:6d95f853dab3 483 xbee().printf("START_SWIM: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg [cmd:%6.1f], heading_imu:%6.1f deg>>[%0.2f sec] \r",
joel_ssc 87:6d95f853dab3 484 bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_deg(),
joel_ssc 87:6d95f853dab3 485 depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),pitchLoop().getCommand(),imu().getHeading(),_fsm_timer.read());
joel_ssc 87:6d95f853dab3 486
joel_ssc 87:6d95f853dab3 487 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
joel_ssc 87:6d95f853dab3 488 batt().setPosition_mm(pitchLoop().getOutput()); //these together should hold a depth, rather than turn around and rise
joel_ssc 87:6d95f853dab3 489
joel_ssc 87:6d95f853dab3 490 break;
joel_ssc 87:6d95f853dab3 491
tnhnrl 16:3363b9f14913 492 case RISE :
tnhnrl 16:3363b9f14913 493 // start local state timer and init any other one-shot actions
tnhnrl 32:f2f8ae34aadc 494
tnhnrl 28:16c83a2fdefa 495 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 496 xbee().printf("\r\n\nstate: RISE\r\n");
tnhnrl 73:f6f378311c8d 497 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 498 _fsm_timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 499 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 500
tnhnrl 16:3363b9f14913 501 // what needs to be started?
tnhnrl 16:3363b9f14913 502 bce().unpause();
tnhnrl 16:3363b9f14913 503 batt().unpause();
tnhnrl 16:3363b9f14913 504
tnhnrl 16:3363b9f14913 505 // what are the commands?
tnhnrl 28:16c83a2fdefa 506 depthLoop().setCommand(-1.0); //make sure to get towards the surface (saw issues at LASR pool)
tnhnrl 32:f2f8ae34aadc 507 pitchLoop().setCommand(-_pitch_command);
tnhnrl 74:d281aaef9766 508
tnhnrl 74:d281aaef9766 509 headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
tnhnrl 74:d281aaef9766 510
tnhnrl 74:d281aaef9766 511 xbee().printf("RISE: depth cmd: %3.1f\r\n",depthLoop().getCommand());
tnhnrl 74:d281aaef9766 512 xbee().printf("RISE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 74:d281aaef9766 513 xbee().printf("RISE: heading cmd: %3.1f\r\n",headingLoop().getCommand());
tnhnrl 16:3363b9f14913 514 }
tnhnrl 20:8987a9ae2bc7 515
tnhnrl 16:3363b9f14913 516 // how exit?
tnhnrl 73:f6f378311c8d 517 if (_fsm_timer.read() > _timeout) {
tnhnrl 74:d281aaef9766 518 xbee().printf("RISE: timed out\r\n");
tnhnrl 21:38c8544db6f4 519 _state = EMERGENCY_CLIMB;
tnhnrl 73:f6f378311c8d 520 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 521 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 522 }
tnhnrl 32:f2f8ae34aadc 523
tnhnrl 32:f2f8ae34aadc 524 //modified from (depthLoop().getPosition() < depthLoop().getCommand() + 0.5)
tnhnrl 32:f2f8ae34aadc 525 //did not work correctly in bench test (stuck in rise state)
tnhnrl 32:f2f8ae34aadc 526 else if (depthLoop().getPosition() < 0.5) {
tnhnrl 74:d281aaef9766 527 xbee().printf("RISE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
joel_ssc 87:6d95f853dab3 528 sprintf(buf, "in RISE: at surface ... depth =%g ... now go to FLOAT_BROADCAST\n\n\r", depthLoop().getPosition());
joel_ssc 87:6d95f853dab3 529 mbedLogger().appendDiagFile(buf,3);
tnhnrl 28:16c83a2fdefa 530 _state = FLOAT_BROADCAST;
tnhnrl 73:f6f378311c8d 531 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 532 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 533 }
tnhnrl 20:8987a9ae2bc7 534
tnhnrl 67:c86a4b464682 535 // WHAT IS ACTIVE?
joel_ssc 85:dd8176285b6e 536 xbee().printf("RISE: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg [cmd:%6.1f], heading_imu:%6.1f deg>>[%0.2f sec] \r",
joel_ssc 85:dd8176285b6e 537 bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_deg(),
joel_ssc 85:dd8176285b6e 538 depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),pitchLoop().getCommand(),imu().getHeading(),_fsm_timer.read());
tnhnrl 17:7c16b5671d0e 539 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 32:f2f8ae34aadc 540 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 74:d281aaef9766 541
tnhnrl 74:d281aaef9766 542 // ACTIVE RUDDER CONTROL
tnhnrl 74:d281aaef9766 543 rudder().setPosition_deg(headingLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 544
tnhnrl 58:94b7fd55185e 545 break;
tnhnrl 58:94b7fd55185e 546
tnhnrl 58:94b7fd55185e 547 // NEW DIVE AND RISE SEQUENCES
tnhnrl 58:94b7fd55185e 548 case POSITION_DIVE :
tnhnrl 58:94b7fd55185e 549 // start local state timer and init any other one-shot actions
tnhnrl 58:94b7fd55185e 550 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 551 xbee().printf("\r\n\nstate: POSITION DIVE\r\n");
tnhnrl 73:f6f378311c8d 552 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 553 _fsm_timer.start(); // background timer starts running
tnhnrl 58:94b7fd55185e 554 _isTimeoutRunning = true;
tnhnrl 58:94b7fd55185e 555
tnhnrl 58:94b7fd55185e 556 // what needs to be started?
tnhnrl 58:94b7fd55185e 557 bce().unpause();
tnhnrl 58:94b7fd55185e 558 batt().unpause();
tnhnrl 58:94b7fd55185e 559 rudder().unpause();
tnhnrl 58:94b7fd55185e 560
tnhnrl 58:94b7fd55185e 561 // what are the commands? (using inner loops except for heading outer loop)
tnhnrl 58:94b7fd55185e 562 // These actions happen ONCE in the POSITION_DIVE sequence
tnhnrl 74:d281aaef9766 563 batt().setPosition_mm(_neutral_batt_pos_mm + _BMM_dive_offset);
tnhnrl 74:d281aaef9766 564 bce().setPosition_mm(_neutral_bce_pos_mm - _BCE_dive_offset);
tnhnrl 58:94b7fd55185e 565
tnhnrl 74:d281aaef9766 566 //DEPTH COMMAND
tnhnrl 74:d281aaef9766 567 depthLoop().setCommand(_depth_command);
tnhnrl 74:d281aaef9766 568
tnhnrl 58:94b7fd55185e 569 headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
tnhnrl 58:94b7fd55185e 570
tnhnrl 74:d281aaef9766 571 xbee().printf("POS DIVE: BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //get the actual commanded position
tnhnrl 74:d281aaef9766 572 xbee().printf("POS DIVE: BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //get the actual commanded position
tnhnrl 74:d281aaef9766 573 xbee().printf("POS DIVE: heading cmd: %3.1f\r\n",headingLoop().getCommand());
tnhnrl 58:94b7fd55185e 574
tnhnrl 58:94b7fd55185e 575 //reset max dive depth
tnhnrl 58:94b7fd55185e 576 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 58:94b7fd55185e 577 }
tnhnrl 58:94b7fd55185e 578
tnhnrl 58:94b7fd55185e 579 // how exit?
tnhnrl 58:94b7fd55185e 580 // timer runs out goes to POSITION_RISE
tnhnrl 73:f6f378311c8d 581 if (_fsm_timer.read() > _timeout) {
tnhnrl 74:d281aaef9766 582 xbee().printf("POS DIVE timed out\r\n\n");
tnhnrl 58:94b7fd55185e 583 _state = POSITION_RISE; //new behavior 11/17/2017
tnhnrl 73:f6f378311c8d 584 _fsm_timer.reset();
tnhnrl 58:94b7fd55185e 585 _isTimeoutRunning = false;
tnhnrl 58:94b7fd55185e 586 }
tnhnrl 58:94b7fd55185e 587
tnhnrl 58:94b7fd55185e 588 // when you reach the dive threshold, surface
tnhnrl 58:94b7fd55185e 589 else if (depthLoop().getPosition() > depthLoop().getCommand() - 0.5) { // including offset for low momentum approaches
tnhnrl 74:d281aaef9766 590 xbee().printf("POS DIVE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 58:94b7fd55185e 591 _state = POSITION_RISE;
tnhnrl 73:f6f378311c8d 592 _fsm_timer.reset();
tnhnrl 58:94b7fd55185e 593 _isTimeoutRunning = false;
tnhnrl 58:94b7fd55185e 594 }
tnhnrl 58:94b7fd55185e 595
tnhnrl 58:94b7fd55185e 596 // what is active?
joel_ssc 85:dd8176285b6e 597 xbee().printf("POS DIVE: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg, heading_imu:%6.1f deg>>[%0.2f sec] \r",
joel_ssc 85:dd8176285b6e 598 bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_deg(),
joel_ssc 85:dd8176285b6e 599 depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),imu().getHeading(),_fsm_timer.read());
tnhnrl 58:94b7fd55185e 600
tnhnrl 58:94b7fd55185e 601 if (depthLoop().getPosition() > _max_recorded_depth_dive) {
tnhnrl 58:94b7fd55185e 602 _max_recorded_depth_dive = depthLoop().getPosition(); //new max depth recorded when it is larger than previous values
tnhnrl 58:94b7fd55185e 603 }
tnhnrl 58:94b7fd55185e 604
tnhnrl 58:94b7fd55185e 605 // ACTIVE RUDDER CONTROL
tnhnrl 58:94b7fd55185e 606 rudder().setPosition_deg(headingLoop().getOutput());
tnhnrl 58:94b7fd55185e 607
tnhnrl 58:94b7fd55185e 608 break;
joel_ssc 82:0981b9ada820 609
joel_ssc 82:0981b9ada820 610 case LEG_POSITION_DIVE :
joel_ssc 82:0981b9ada820 611 // start local state timer and init any other one-shot actions
joel_ssc 82:0981b9ada820 612 if (!_isTimeoutRunning) {
joel_ssc 82:0981b9ada820 613 xbee().printf("\r\n\nstate: LEG POSITION DIVE first time - start timer\r\n");
joel_ssc 82:0981b9ada820 614 sprintf(buf, "LEG POSITION DIVE start first dive start timer\n\n\r");
joel_ssc 84:eccd8e837134 615 mbedLogger().appendDiagFile(buf,0);
joel_ssc 82:0981b9ada820 616 _fsm_timer.reset(); // timer goes back to zero I am not sure about this reset jcw
joel_ssc 82:0981b9ada820 617 _fsm_timer.start(); // background timer starts running
joel_ssc 82:0981b9ada820 618 _isTimeoutRunning = true;
joel_ssc 82:0981b9ada820 619 _yotimer.reset();
joel_ssc 82:0981b9ada820 620 _yotimer.start(); //sets the yo_timer running on the dive
joel_ssc 82:0981b9ada820 621
joel_ssc 82:0981b9ada820 622 // what needs to be started?
joel_ssc 82:0981b9ada820 623 bce().unpause();
joel_ssc 82:0981b9ada820 624 batt().unpause();
joel_ssc 82:0981b9ada820 625 rudder().unpause();
joel_ssc 82:0981b9ada820 626
joel_ssc 82:0981b9ada820 627 // what are the commands? (using inner loops except for heading outer loop)
joel_ssc 82:0981b9ada820 628 // These actions happen ONCE in the POSITION_DIVE sequence
joel_ssc 82:0981b9ada820 629 batt().setPosition_mm(_neutral_batt_pos_mm + _BMM_dive_offset);
joel_ssc 82:0981b9ada820 630 bce().setPosition_mm(_neutral_bce_pos_mm - _BCE_dive_offset);
joel_ssc 85:dd8176285b6e 631
joel_ssc 85:dd8176285b6e 632
joel_ssc 82:0981b9ada820 633 //retrieve commands from structs (loaded from legfile.txt file)
joel_ssc 82:0981b9ada820 634 stateMachine().getLegParams();
joel_ssc 82:0981b9ada820 635
joel_ssc 82:0981b9ada820 636 leg_max_depth = currentLegStateStruct.max_depth;
joel_ssc 82:0981b9ada820 637 leg_heading = currentLegStateStruct.heading;
joel_ssc 82:0981b9ada820 638
joel_ssc 82:0981b9ada820 639
joel_ssc 82:0981b9ada820 640 //DEPTH COMMAND
joel_ssc 82:0981b9ada820 641 //depthLoop().setCommand(_depth_command); // I want this to be the max_depth in the legStruct
joel_ssc 82:0981b9ada820 642 depthLoop().setCommand(leg_max_depth); // I want this to be the max_depth in the legStruct
joel_ssc 82:0981b9ada820 643
joel_ssc 82:0981b9ada820 644 //headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 82:0981b9ada820 645 headingLoop().setCommand(leg_heading); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 82:0981b9ada820 646
joel_ssc 82:0981b9ada820 647 xbee().printf("LEG POS DIVE: BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //get the actual commanded position
joel_ssc 82:0981b9ada820 648 xbee().printf("LEG POS DIVE: BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //get the actual commanded position
joel_ssc 82:0981b9ada820 649 xbee().printf("LEG POS DIVE: heading cmd: %3.1f\r\n",headingLoop().getCommand());
joel_ssc 82:0981b9ada820 650
joel_ssc 82:0981b9ada820 651 //reset max dive depth
joel_ssc 82:0981b9ada820 652 _max_recorded_depth_dive = -99; //float to record max depth
joel_ssc 82:0981b9ada820 653 lpd_oneshots = 1;
joel_ssc 82:0981b9ada820 654 }
joel_ssc 82:0981b9ada820 655
joel_ssc 82:0981b9ada820 656 if (!lpd_oneshots) {
joel_ssc 82:0981b9ada820 657 xbee().printf("\r\n\nstate: LEG POSITION DIVE oneshots\r\n");
joel_ssc 82:0981b9ada820 658 // _fsm_timer.reset(); // timer goes back to zero I am not sure about this reset jcw
joel_ssc 82:0981b9ada820 659 // sm_timer.start(); // background timer starts running
joel_ssc 82:0981b9ada820 660 _yotimer.reset(); //sets the yo_timer running on the NEW dive
joel_ssc 82:0981b9ada820 661 _yotimer.start();
joel_ssc 82:0981b9ada820 662 //_isTimeoutRunning = true;
joel_ssc 82:0981b9ada820 663
joel_ssc 82:0981b9ada820 664 // what needs to be started?
joel_ssc 82:0981b9ada820 665 bce().unpause();
joel_ssc 82:0981b9ada820 666 batt().unpause();
joel_ssc 82:0981b9ada820 667 rudder().unpause();
joel_ssc 82:0981b9ada820 668
joel_ssc 82:0981b9ada820 669 // what are the commands? (using inner loops except for heading outer loop)
joel_ssc 82:0981b9ada820 670 // These actions happen ONCE in the POSITION_DIVE sequence
joel_ssc 82:0981b9ada820 671 batt().setPosition_mm(_neutral_batt_pos_mm + _BMM_dive_offset);
joel_ssc 82:0981b9ada820 672 bce().setPosition_mm(_neutral_bce_pos_mm - _BCE_dive_offset);
joel_ssc 82:0981b9ada820 673 //retrieve commands from structs (loaded from missionleg.cfg file)
joel_ssc 82:0981b9ada820 674 leg_max_depth = currentLegStateStruct.max_depth;
joel_ssc 82:0981b9ada820 675 leg_heading = currentLegStateStruct.heading;
joel_ssc 84:eccd8e837134 676 sprintf(buf, "LEG POSITION DIVE entered via Leg_pos_rise - lpd_oneshots, only restart _yotimer\n\n\r");
joel_ssc 84:eccd8e837134 677 mbedLogger().appendDiagFile(buf,0);
joel_ssc 82:0981b9ada820 678
joel_ssc 82:0981b9ada820 679 //DEPTH COMMAND
joel_ssc 82:0981b9ada820 680 //depthLoop().setCommand(_depth_command); // I want this to be the max_depth in the legStruct
joel_ssc 82:0981b9ada820 681 depthLoop().setCommand(leg_max_depth); // I want this to be the max_depth in the legStruct
joel_ssc 82:0981b9ada820 682
joel_ssc 82:0981b9ada820 683 //headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 82:0981b9ada820 684 headingLoop().setCommand(leg_heading); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 82:0981b9ada820 685
joel_ssc 82:0981b9ada820 686 xbee().printf("LEG POS DIVE: BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //get the actual commanded position
joel_ssc 82:0981b9ada820 687 xbee().printf("LEG POS DIVE: BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //get the actual commanded position
joel_ssc 82:0981b9ada820 688 xbee().printf("LEG POS DIVE: heading cmd: %3.1f\r\n",headingLoop().getCommand());
joel_ssc 82:0981b9ada820 689
joel_ssc 82:0981b9ada820 690 lpd_oneshots = 1;
joel_ssc 82:0981b9ada820 691 //reset max dive depth
joel_ssc 82:0981b9ada820 692 //_max_recorded_depth_dive = -99; //float to record max depth
joel_ssc 82:0981b9ada820 693 } // lpd_oneshots if timer is already running
joel_ssc 82:0981b9ada820 694
joel_ssc 82:0981b9ada820 695 // how exit?
joel_ssc 82:0981b9ada820 696 // timer runs out goes to LEG_POSITION_RISE but finish_leg flag is turned on.
joel_ssc 82:0981b9ada820 697 if (_fsm_timer.read() > _timeout) {
joel_ssc 82:0981b9ada820 698 xbee().printf("LEG POSITION DIVE timed out for overall leg time\r\n\n");
joel_ssc 82:0981b9ada820 699 _state = LEG_POSITION_RISE; // now start a timer on yo_timer for rise
joel_ssc 82:0981b9ada820 700 finish_leg =1;
joel_ssc 82:0981b9ada820 701 lpr_oneshots=0;
joel_ssc 84:eccd8e837134 702 sprintf(buf, "go to LEG POSITION DIVE on overall leg timeout, set finish_leg=1\n\n\r");
joel_ssc 84:eccd8e837134 703 mbedLogger().appendDiagFile(buf,0);
joel_ssc 82:0981b9ada820 704 // _fsm_timer.reset();
joel_ssc 82:0981b9ada820 705 // _isTimeoutRunning = false;
joel_ssc 82:0981b9ada820 706 }
joel_ssc 82:0981b9ada820 707
joel_ssc 82:0981b9ada820 708 // when you reach the dive threshold, surface
joel_ssc 82:0981b9ada820 709 else if (depthLoop().getPosition() > depthLoop().getCommand() - 0.5) { // including offset for low momentum approaches
joel_ssc 82:0981b9ada820 710 xbee().printf("LEG POS DIVE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
joel_ssc 82:0981b9ada820 711 _state = LEG_POSITION_RISE;
joel_ssc 82:0981b9ada820 712 lpr_oneshots = 0;
joel_ssc 84:eccd8e837134 713
joel_ssc 82:0981b9ada820 714 // _fsm_timer.reset(); // reset time if still inside legg long timeout?
joel_ssc 82:0981b9ada820 715 // _isTimeoutRunning = false;
joel_ssc 82:0981b9ada820 716 } else if(_yotimer.read() > _yo_time ) {
joel_ssc 82:0981b9ada820 717 xbee().printf("LEG POS DIVE: yo_time timed out - go to LEG_POSITION_RISE actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
joel_ssc 82:0981b9ada820 718 sprintf(buf, "LEG POS DIVE: yo_time timed out - go to LEG_POSITION_RISE\n\n\r");
joel_ssc 82:0981b9ada820 719 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 720 _state = LEG_POSITION_RISE;
joel_ssc 84:eccd8e837134 721 finish_leg =1;
joel_ssc 82:0981b9ada820 722 //_yotimer.reset();
joel_ssc 82:0981b9ada820 723 //_yotimer.start(); // restart the yo timer for next yo in rise mode -- test if depth is small fraction of max_depth and exit to EC if small enough??
joel_ssc 82:0981b9ada820 724 lpr_oneshots=0;
joel_ssc 82:0981b9ada820 725 if (depthLoop().getPosition() < 0.7* depthLoop().getCommand()) { //too slow - exit
joel_ssc 84:eccd8e837134 726 _state = EMERGENCY_CLIMB; // overridden here for testing
joel_ssc 82:0981b9ada820 727 finish_leg = 1;
joel_ssc 82:0981b9ada820 728 xbee().printf("LEG POS DIVE:descent too slow (<0.7*maxdepth) - go to EMERGENCY_CLIMB actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
joel_ssc 84:eccd8e837134 729 sprintf(buf, "LEG POS DIVE: descent too slow yo_time timed out - go to emergency climb but not really for testing\n\n\r");
joel_ssc 82:0981b9ada820 730 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 731 _state = LEG_POSITION_RISE;
joel_ssc 84:eccd8e837134 732 // _fsm_timer.reset();
joel_ssc 84:eccd8e837134 733 // _isTimeoutRunning = false;
joel_ssc 82:0981b9ada820 734 }
joel_ssc 82:0981b9ada820 735 }
joel_ssc 82:0981b9ada820 736
joel_ssc 82:0981b9ada820 737 // what is active?
joel_ssc 85:dd8176285b6e 738 xbee().printf("LEG POS DIVE: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg, heading_imu:%6.1f deg>>[%0.2f sec] \r",
joel_ssc 82:0981b9ada820 739 bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_deg(),
joel_ssc 82:0981b9ada820 740 depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),imu().getHeading(),_fsm_timer.read());
joel_ssc 82:0981b9ada820 741
joel_ssc 82:0981b9ada820 742 if (depthLoop().getPosition() > _max_recorded_depth_dive) {
joel_ssc 82:0981b9ada820 743 _max_recorded_depth_dive = depthLoop().getPosition(); //new max depth recorded when it is larger than previous values
joel_ssc 82:0981b9ada820 744 }
joel_ssc 82:0981b9ada820 745
joel_ssc 82:0981b9ada820 746 // ACTIVE RUDDER CONTROL
joel_ssc 82:0981b9ada820 747 rudder().setPosition_deg(headingLoop().getOutput());
joel_ssc 82:0981b9ada820 748
joel_ssc 82:0981b9ada820 749 break; // end LEG POSITION DIVE
tnhnrl 58:94b7fd55185e 750
tnhnrl 58:94b7fd55185e 751 case POSITION_RISE :
tnhnrl 58:94b7fd55185e 752 // start local state timer and init any other one-shot actions
tnhnrl 58:94b7fd55185e 753
tnhnrl 58:94b7fd55185e 754 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 755 xbee().printf("\r\n\nstate: POSITION RISE\r\n");
tnhnrl 73:f6f378311c8d 756 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 757 _fsm_timer.start(); // background timer starts running
tnhnrl 58:94b7fd55185e 758 _isTimeoutRunning = true;
tnhnrl 58:94b7fd55185e 759
tnhnrl 58:94b7fd55185e 760 // what needs to be started?
tnhnrl 58:94b7fd55185e 761 bce().unpause();
tnhnrl 58:94b7fd55185e 762 batt().unpause();
tnhnrl 58:94b7fd55185e 763
tnhnrl 74:d281aaef9766 764 // what are the commands? (using inner loops except for heading outer loop)
tnhnrl 74:d281aaef9766 765 batt().setPosition_mm(_neutral_batt_pos_mm - _BMM_dive_offset); //reversing the BCE and BATT positions
tnhnrl 58:94b7fd55185e 766 bce().setPosition_mm(_neutral_bce_pos_mm + _BCE_dive_offset); //reversing the BCE and BATT positions
tnhnrl 58:94b7fd55185e 767
tnhnrl 58:94b7fd55185e 768 headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
tnhnrl 58:94b7fd55185e 769
tnhnrl 74:d281aaef9766 770 xbee().printf("POS RISE: BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //get the actual commanded position
tnhnrl 74:d281aaef9766 771 xbee().printf("POS RISE: BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //get the actual commanded position
tnhnrl 74:d281aaef9766 772 xbee().printf("POS RISE: heading cmd: %3.1f\r\n",headingLoop().getCommand());
tnhnrl 58:94b7fd55185e 773 }
tnhnrl 58:94b7fd55185e 774
tnhnrl 58:94b7fd55185e 775 // how exit?
tnhnrl 73:f6f378311c8d 776 if (_fsm_timer.read() > _timeout) {
tnhnrl 74:d281aaef9766 777 xbee().printf("POS RISE: timed out\r\n");
tnhnrl 58:94b7fd55185e 778 _state = EMERGENCY_CLIMB;
tnhnrl 73:f6f378311c8d 779 _fsm_timer.reset();
tnhnrl 58:94b7fd55185e 780 _isTimeoutRunning = false;
tnhnrl 58:94b7fd55185e 781 }
tnhnrl 58:94b7fd55185e 782 else if (depthLoop().getPosition() < 0.5) {
tnhnrl 74:d281aaef9766 783 xbee().printf("POS RISE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 58:94b7fd55185e 784 _state = FLOAT_BROADCAST;
tnhnrl 73:f6f378311c8d 785 _fsm_timer.reset();
tnhnrl 58:94b7fd55185e 786 _isTimeoutRunning = false;
tnhnrl 58:94b7fd55185e 787 }
tnhnrl 58:94b7fd55185e 788
tnhnrl 58:94b7fd55185e 789 // what is active?
joel_ssc 85:dd8176285b6e 790 xbee().printf("POS RISE: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg, heading_imu:%6.1f deg>>[%0.2f sec] \r",
joel_ssc 82:0981b9ada820 791 bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),
joel_ssc 82:0981b9ada820 792 rudder().getSetPosition_deg(),depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),imu().getHeading(),_fsm_timer.read());
tnhnrl 58:94b7fd55185e 793
tnhnrl 58:94b7fd55185e 794 // ACTIVE RUDDER CONTROL
tnhnrl 58:94b7fd55185e 795 rudder().setPosition_deg(headingLoop().getOutput());
tnhnrl 58:94b7fd55185e 796
joel_ssc 82:0981b9ada820 797 break; //end of POS RISE
joel_ssc 82:0981b9ada820 798
joel_ssc 82:0981b9ada820 799 case LEG_POSITION_RISE :
joel_ssc 82:0981b9ada820 800 // start local state timer and init any other one-shot actions
joel_ssc 82:0981b9ada820 801 leg_min_depth = currentLegStateStruct.min_depth;
joel_ssc 82:0981b9ada820 802 leg_heading = currentLegStateStruct.heading;
joel_ssc 82:0981b9ada820 803
joel_ssc 82:0981b9ada820 804 if (!_isTimeoutRunning) { // this should never occur. the only entry into this case is from LEG_POSITION_DIVE exit
joel_ssc 82:0981b9ada820 805 xbee().printf("\r\n\nstate: LEG POSITION RISE\r\n");
joel_ssc 82:0981b9ada820 806 sprintf(buf, "state: LEG POSITION RISE entered with timeout NOT running should not happen\n\n\r");
joel_ssc 82:0981b9ada820 807 mbedLogger().appendDiagFile(buf,3);
joel_ssc 85:dd8176285b6e 808 // go and read the legparams just in case
joel_ssc 85:dd8176285b6e 809
joel_ssc 85:dd8176285b6e 810 //retrieve commands from structs (loaded from legfile.txt file)
joel_ssc 85:dd8176285b6e 811 stateMachine().getLegParams();
joel_ssc 85:dd8176285b6e 812 leg_min_depth = currentLegStateStruct.min_depth;
joel_ssc 85:dd8176285b6e 813 leg_heading = currentLegStateStruct.heading;
joel_ssc 85:dd8176285b6e 814
joel_ssc 82:0981b9ada820 815 //_fsm_timer.reset(); // timer goes back to zero --no
joel_ssc 82:0981b9ada820 816 _fsm_timer.start(); // background timer starts running
joel_ssc 82:0981b9ada820 817 _isTimeoutRunning = true;
joel_ssc 82:0981b9ada820 818 _yotimer.reset(); //reset yo_timer;
joel_ssc 82:0981b9ada820 819 _yotimer.start(); //and start it
joel_ssc 82:0981b9ada820 820
joel_ssc 82:0981b9ada820 821 // what needs to be started?
joel_ssc 82:0981b9ada820 822 bce().unpause();
joel_ssc 82:0981b9ada820 823 batt().unpause();
joel_ssc 82:0981b9ada820 824 stateMachine().getLegParams();
joel_ssc 82:0981b9ada820 825
joel_ssc 82:0981b9ada820 826 // what are the commands? (using inner loops except for heading outer loop)
joel_ssc 82:0981b9ada820 827 batt().setPosition_mm(_neutral_batt_pos_mm - _BMM_dive_offset); //reversing the BCE and BATT positions
joel_ssc 82:0981b9ada820 828 bce().setPosition_mm(_neutral_bce_pos_mm + _BCE_dive_offset); //reversing the BCE and BATT positions
joel_ssc 82:0981b9ada820 829 //retrieve commands from structs (loaded from missionleg.cfg file)
joel_ssc 82:0981b9ada820 830
joel_ssc 82:0981b9ada820 831 headingLoop().setCommand(leg_heading); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 82:0981b9ada820 832
joel_ssc 82:0981b9ada820 833 xbee().printf("LEG POS RISE: BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //get the actual commanded position
joel_ssc 82:0981b9ada820 834 xbee().printf("LEG POS RISE: BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //get the actual commanded position
joel_ssc 82:0981b9ada820 835 xbee().printf("LEG POS RISE: heading cmd: %3.1f\r\n",headingLoop().getCommand());
joel_ssc 82:0981b9ada820 836 lpr_oneshots = 1;
joel_ssc 82:0981b9ada820 837 }
joel_ssc 82:0981b9ada820 838 if (!lpr_oneshots) {
joel_ssc 82:0981b9ada820 839 xbee().printf("\r\n\nstate: LEG POSITION RISE entered via LEG_POSiTION_DIVE finish\r\n");
joel_ssc 84:eccd8e837134 840 sprintf(buf, "state: LEG POSITION RISE entered via LEG_POSITION_DIVE\n\n\r");
joel_ssc 84:eccd8e837134 841 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 842 //_fsm_timer.reset(); // timer goes back to zero --no
joel_ssc 82:0981b9ada820 843 _yotimer.reset(); //reset yo_timer;
joel_ssc 82:0981b9ada820 844 _yotimer.start(); //and start it
joel_ssc 82:0981b9ada820 845 //_fsm_timer.start(); timer is already running // background timer starts running
joel_ssc 82:0981b9ada820 846 // _isTimeoutRunning = true;
joel_ssc 82:0981b9ada820 847
joel_ssc 82:0981b9ada820 848 // what needs to be started?
joel_ssc 82:0981b9ada820 849 bce().unpause();
joel_ssc 82:0981b9ada820 850 batt().unpause();
joel_ssc 82:0981b9ada820 851
joel_ssc 82:0981b9ada820 852 // what are the commands? (using inner loops except for heading outer loop)
joel_ssc 82:0981b9ada820 853 batt().setPosition_mm(_neutral_batt_pos_mm - _BMM_dive_offset); //reversing the BCE and BATT positions
joel_ssc 82:0981b9ada820 854 bce().setPosition_mm(_neutral_bce_pos_mm + _BCE_dive_offset); //reversing the BCE and BATT positions
joel_ssc 82:0981b9ada820 855 //retrieve commands from structs (loaded from missionleg.cfg file)
joel_ssc 82:0981b9ada820 856
joel_ssc 82:0981b9ada820 857 headingLoop().setCommand(leg_heading); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 82:0981b9ada820 858
joel_ssc 82:0981b9ada820 859 xbee().printf("LEG POS RISE: BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //get the actual commanded position
joel_ssc 82:0981b9ada820 860 xbee().printf("LEG POS RISE: BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //get the actual commanded position
joel_ssc 82:0981b9ada820 861 xbee().printf("LEG POS RISE: heading cmd: %3.1f\r\n",headingLoop().getCommand());
joel_ssc 82:0981b9ada820 862 lpr_oneshots = 1;
joel_ssc 84:eccd8e837134 863 } // end if(!lpr_oneshots)
joel_ssc 82:0981b9ada820 864
joel_ssc 82:0981b9ada820 865 // how exit?
joel_ssc 82:0981b9ada820 866 if (_fsm_timer.read() > _timeout) {
joel_ssc 82:0981b9ada820 867 xbee().printf("LEG POS RISE: timed out on overall timeout\r\n");
joel_ssc 84:eccd8e837134 868 // _state = EMERGENCY_CLIMB;
joel_ssc 84:eccd8e837134 869 finish_leg = 1; // not going immediately to Emergency_climb and not resetting timers means it will take 1 yo-time of rising first
joel_ssc 84:eccd8e837134 870 // _fsm_timer.reset();
joel_ssc 84:eccd8e837134 871 sprintf(buf, "LEG POSITION DIVE Ended via overall timeout\n\n\r");
joel_ssc 84:eccd8e837134 872 mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 873 // _isTimeoutRunning = false;
joel_ssc 82:0981b9ada820 874 }
joel_ssc 84:eccd8e837134 875 else if (depthLoop().getPosition() < leg_min_depth - 0.5 ) { // add in check for finish_leg. Don't flip flop between states in that case
joel_ssc 82:0981b9ada820 876 xbee().printf("LEG POS RISE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
joel_ssc 82:0981b9ada820 877 _state = LEG_POSITION_DIVE;
joel_ssc 84:eccd8e837134 878 sprintf(buf, "LEG POSITION RISE flip-flops to LEG_POSITION_DIVE on near surface pressure \n\n\r");
joel_ssc 84:eccd8e837134 879 mbedLogger().appendDiagFile(buf,0);
joel_ssc 82:0981b9ada820 880 //_fsm_timer.reset();
joel_ssc 82:0981b9ada820 881 //_isTimeoutRunning = false;
joel_ssc 82:0981b9ada820 882 lpd_oneshots=0;
joel_ssc 84:eccd8e837134 883 if(finish_leg ==1) {
joel_ssc 84:eccd8e837134 884 _state = EMERGENCY_CLIMB;
joel_ssc 84:eccd8e837134 885 _fsm_timer.reset(); _yotimer.reset();
joel_ssc 84:eccd8e837134 886 _isTimeoutRunning = false;
joel_ssc 84:eccd8e837134 887 sprintf(buf, "LEG POSITION RISE ... ENDS, and since finish_leg=1, go to Emergency climb\n\n\r");
joel_ssc 84:eccd8e837134 888 mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 889 }
joel_ssc 82:0981b9ada820 890 }
joel_ssc 82:0981b9ada820 891 else if(_yotimer.read() > _yo_time ) {
joel_ssc 82:0981b9ada820 892 xbee().printf("LEG POS RISE: yo_time timed out - go to Emergency_CLIMB. Actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
joel_ssc 84:eccd8e837134 893 _state = EMERGENCY_CLIMB; // add diagnostics message
joel_ssc 82:0981b9ada820 894 finish_leg = 1;
joel_ssc 84:eccd8e837134 895 _fsm_timer.reset(); _yotimer.reset();
joel_ssc 82:0981b9ada820 896 _isTimeoutRunning = false;
joel_ssc 84:eccd8e837134 897 sprintf(buf, "LEG POSITION DIVE ... ENDS on yo_time too long, exit to emergnecy climb + finish_leg=1\n\n\r");
joel_ssc 84:eccd8e837134 898 mbedLogger().appendDiagFile(buf,0);
joel_ssc 82:0981b9ada820 899 }
joel_ssc 82:0981b9ada820 900
joel_ssc 82:0981b9ada820 901 // what is active?
joel_ssc 85:dd8176285b6e 902 xbee().printf("LEG POS RISE: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg, heading_imu:%6.1f deg>>[%0.2f sec] \r",
joel_ssc 82:0981b9ada820 903 bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),
joel_ssc 82:0981b9ada820 904 rudder().getSetPosition_deg(),depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),imu().getHeading(),_fsm_timer.read());
joel_ssc 82:0981b9ada820 905
joel_ssc 82:0981b9ada820 906 // ACTIVE RUDDER CONTROL
joel_ssc 82:0981b9ada820 907 rudder().setPosition_deg(headingLoop().getOutput());
joel_ssc 82:0981b9ada820 908
joel_ssc 82:0981b9ada820 909 break; // end LEG POS RISE
tnhnrl 58:94b7fd55185e 910 // NEW DIVE AND RISE SEQUENCES
tnhnrl 16:3363b9f14913 911
tnhnrl 16:3363b9f14913 912 case FLOAT_LEVEL :
tnhnrl 16:3363b9f14913 913 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 914 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 915 xbee().printf("\r\n\nstate: FLOAT_LEVEL\r\n");
tnhnrl 73:f6f378311c8d 916 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 917 _fsm_timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 918 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 919
tnhnrl 16:3363b9f14913 920 // what needs to be started?
tnhnrl 16:3363b9f14913 921 bce().unpause();
tnhnrl 16:3363b9f14913 922 batt().unpause();
tnhnrl 16:3363b9f14913 923
tnhnrl 20:8987a9ae2bc7 924 // what are the commands?
tnhnrl 28:16c83a2fdefa 925 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 16:3363b9f14913 926 pitchLoop().setCommand(0.0);
tnhnrl 16:3363b9f14913 927 }
tnhnrl 20:8987a9ae2bc7 928
tnhnrl 16:3363b9f14913 929 // how exit?
tnhnrl 73:f6f378311c8d 930 if (_fsm_timer > _timeout) {
tnhnrl 74:d281aaef9766 931 xbee().printf("FL: timed out\r\n");
tnhnrl 21:38c8544db6f4 932 _state = FLOAT_BROADCAST;
tnhnrl 73:f6f378311c8d 933 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 934 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 935 }
tnhnrl 28:16c83a2fdefa 936 else if (fabs(imu().getPitch() - pitchLoop().getCommand()) < fabs(_pitchTolerance)) { //current tolerance is 5 degrees
tnhnrl 74:d281aaef9766 937 xbee().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 938 _state = FLOAT_BROADCAST;
tnhnrl 73:f6f378311c8d 939 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 940 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 941 }
tnhnrl 20:8987a9ae2bc7 942
tnhnrl 16:3363b9f14913 943 // what is active?
tnhnrl 74:d281aaef9766 944 xbee().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(), _fsm_timer.read());
tnhnrl 16:3363b9f14913 945 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 946
tnhnrl 16:3363b9f14913 947 break;
tnhnrl 16:3363b9f14913 948
tnhnrl 16:3363b9f14913 949 case FLOAT_BROADCAST :
tnhnrl 16:3363b9f14913 950 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 951 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 952 xbee().printf("\r\n\nstate: FLOAT_BROADCAST\r\n");
tnhnrl 73:f6f378311c8d 953 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 954 _fsm_timer.start(); // background timer starts running
joel_ssc 84:eccd8e837134 955 _yotimer.reset();
joel_ssc 84:eccd8e837134 956 _yotimer.start();
tnhnrl 28:16c83a2fdefa 957 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 958
tnhnrl 16:3363b9f14913 959 // what needs to be started?
tnhnrl 16:3363b9f14913 960 bce().unpause();
tnhnrl 16:3363b9f14913 961 batt().unpause();
tnhnrl 20:8987a9ae2bc7 962
tnhnrl 16:3363b9f14913 963 // what are the commands?
tnhnrl 55:f4ec445c42fe 964 bce().setPosition_mm(_bceFloatPosition); // 320.0
tnhnrl 55:f4ec445c42fe 965 batt().setPosition_mm(_battFloatPosition); // 73.0
tnhnrl 74:d281aaef9766 966
tnhnrl 74:d281aaef9766 967 //set rudder to center
tnhnrl 74:d281aaef9766 968 rudder().setPosition_deg(0.0); //set rudder to center, zero degrees
tnhnrl 16:3363b9f14913 969 }
tnhnrl 20:8987a9ae2bc7 970
tnhnrl 16:3363b9f14913 971 // how exit?
joel_ssc 82:0981b9ada820 972
joel_ssc 84:eccd8e837134 973 if (_fsm_timer > _timeout || ( _yotimer > _state_transition_time)) {
tnhnrl 74:d281aaef9766 974 xbee().printf("FB: timed out\r\n");
joel_ssc 87:6d95f853dab3 975 sprintf(buf, "in FLOAT_BRADCAST timed out ... go to SIT_IDLE\n\n");
joel_ssc 87:6d95f853dab3 976 mbedLogger().appendDiagFile(buf,3);
tnhnrl 21:38c8544db6f4 977 _state = SIT_IDLE;
tnhnrl 73:f6f378311c8d 978 _fsm_timer.reset();
tnhnrl 32:f2f8ae34aadc 979
tnhnrl 32:f2f8ae34aadc 980 //stop recording data
tnhnrl 35:2f66ea4863d5 981 //mbedLogger().closeFile();
tnhnrl 32:f2f8ae34aadc 982
tnhnrl 28:16c83a2fdefa 983 _isTimeoutRunning = false;
joel_ssc 87:6d95f853dab3 984 if(finish_leg == 1) {
joel_ssc 87:6d95f853dab3 985 _state = ENDLEG_WAIT;
joel_ssc 87:6d95f853dab3 986 sprintf(buf, "in FLOAT_BROADCAST still timed out .... but also finish_leg==1 so go to ENDLEG_WAIT\n\n");
joel_ssc 87:6d95f853dab3 987 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 988 } // allows wait at surface for xbee messaging to not close program
tnhnrl 16:3363b9f14913 989 }
tnhnrl 74:d281aaef9766 990
tnhnrl 74:d281aaef9766 991 //fix on float_broadcast to account for BCE stopping early in current hardware
tnhnrl 74:d281aaef9766 992 else if ( (fabs(bce().getPosition_mm() - bce().getSetPosition_mm()) < 4.0 ) and
tnhnrl 20:8987a9ae2bc7 993 (fabs(batt().getPosition_mm() - batt().getSetPosition_mm()) < batt().getDeadband()) ) {
tnhnrl 74:d281aaef9766 994 xbee().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 995 _state = SIT_IDLE;
tnhnrl 73:f6f378311c8d 996 _fsm_timer.reset();
joel_ssc 85:dd8176285b6e 997 _isTimeoutRunning = false;
tnhnrl 32:f2f8ae34aadc 998
tnhnrl 32:f2f8ae34aadc 999 //stop recording data
tnhnrl 35:2f66ea4863d5 1000 //mbedLogger().closeFile();
joel_ssc 85:dd8176285b6e 1001 if(finish_leg == 1) { _state = ENDLEG_WAIT; } // allows exit via wait at surface first
joel_ssc 85:dd8176285b6e 1002 }
tnhnrl 20:8987a9ae2bc7 1003
tnhnrl 20:8987a9ae2bc7 1004 // what is active?
joel_ssc 85:dd8176285b6e 1005 xbee().printf("FB: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg, heading_imu:%6.1f deg>>[%0.2f sec] \r",
joel_ssc 82:0981b9ada820 1006 bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_deg(),
joel_ssc 82:0981b9ada820 1007 depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),imu().getHeading(),_fsm_timer.read());
tnhnrl 32:f2f8ae34aadc 1008
joel_ssc 85:dd8176285b6e 1009 // if (finish_leg == 1) { _state = FB_EXIT;}
tnhnrl 16:3363b9f14913 1010 break;
tnhnrl 17:7c16b5671d0e 1011
tnhnrl 17:7c16b5671d0e 1012 case MULTI_DIVE :
tnhnrl 17:7c16b5671d0e 1013 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 1014 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 1015 xbee().printf("\r\n\nstate: MULTI-DIVE\r\n");
tnhnrl 73:f6f378311c8d 1016 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 1017 _fsm_timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 1018 _isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 1019
tnhnrl 17:7c16b5671d0e 1020 // what needs to be started?
tnhnrl 17:7c16b5671d0e 1021 bce().unpause();
tnhnrl 17:7c16b5671d0e 1022 batt().unpause();
tnhnrl 17:7c16b5671d0e 1023
tnhnrl 21:38c8544db6f4 1024 //retrieve commands from structs (loaded from sequence.cfg file)
tnhnrl 32:f2f8ae34aadc 1025 float sequence_depth_command = currentStateStruct.depth;
tnhnrl 32:f2f8ae34aadc 1026 float sequence_pitch_command = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 1027
tnhnrl 17:7c16b5671d0e 1028 // what are the commands?
tnhnrl 32:f2f8ae34aadc 1029 depthLoop().setCommand(sequence_depth_command);
tnhnrl 32:f2f8ae34aadc 1030 pitchLoop().setCommand(sequence_pitch_command);
tnhnrl 74:d281aaef9766 1031
tnhnrl 74:d281aaef9766 1032
tnhnrl 74:d281aaef9766 1033 headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 85:dd8176285b6e 1034 xbee().printf("MULTI-DIVE: depth cmd: %3.1f m, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 1035
tnhnrl 32:f2f8ae34aadc 1036 //no max depth recording right now
tnhnrl 17:7c16b5671d0e 1037 }
tnhnrl 20:8987a9ae2bc7 1038
tnhnrl 17:7c16b5671d0e 1039 // how exit?
tnhnrl 73:f6f378311c8d 1040 if (_fsm_timer > _timeout) {
tnhnrl 74:d281aaef9766 1041 xbee().printf("\r\n\nMULTI-DIVE: timed out [time: %0.1f]\r\n\n", _fsm_timer.read());
tnhnrl 21:38c8544db6f4 1042 _state = MULTI_RISE; //new behavior 11/17/2017
tnhnrl 73:f6f378311c8d 1043 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 1044 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 1045 }
tnhnrl 17:7c16b5671d0e 1046 else if (depthLoop().getPosition() > depthLoop().getCommand()) {
tnhnrl 74:d281aaef9766 1047 xbee().printf("MULTI-DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 21:38c8544db6f4 1048 _state = MULTI_RISE;
tnhnrl 73:f6f378311c8d 1049 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 1050 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 1051 }
tnhnrl 20:8987a9ae2bc7 1052
tnhnrl 67:c86a4b464682 1053 // WHAT IS ACTIVE?
joel_ssc 85:dd8176285b6e 1054 xbee().printf("MD: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg [cmd:%6.1f], heading_imu:%6.1f deg>>[%0.2f sec] \r", bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),pitchLoop().getCommand(),imu().getHeading(),_fsm_timer.read());
tnhnrl 17:7c16b5671d0e 1055 bce().setPosition_mm(depthLoop().getOutput());
tnhnrl 17:7c16b5671d0e 1056 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 1057
tnhnrl 74:d281aaef9766 1058 // ACTIVE RUDDER CONTROL
tnhnrl 74:d281aaef9766 1059 rudder().setPosition_deg(headingLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 1060
tnhnrl 17:7c16b5671d0e 1061 break;
tnhnrl 17:7c16b5671d0e 1062
tnhnrl 17:7c16b5671d0e 1063 case MULTI_RISE :
tnhnrl 17:7c16b5671d0e 1064 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 1065 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 1066 xbee().printf("\r\n\nstate: MULTI-RISE\r\n");
tnhnrl 73:f6f378311c8d 1067 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 1068 _fsm_timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 1069 _isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 1070
tnhnrl 17:7c16b5671d0e 1071 // what needs to be started?
tnhnrl 17:7c16b5671d0e 1072 bce().unpause();
tnhnrl 17:7c16b5671d0e 1073 batt().unpause();
tnhnrl 17:7c16b5671d0e 1074
tnhnrl 17:7c16b5671d0e 1075 //NEW: retrieve depth and pitch commands from config file struct
tnhnrl 17:7c16b5671d0e 1076 // concept is to load this each time the multi-dive restarts
tnhnrl 17:7c16b5671d0e 1077 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 1078
tnhnrl 17:7c16b5671d0e 1079 //retrieve just pitch command from struct
tnhnrl 32:f2f8ae34aadc 1080 float sequence_pitch_command = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 1081
tnhnrl 17:7c16b5671d0e 1082 // what are the commands? (send back to 0.5 feet, not surface) // 11/21/2017
tnhnrl 17:7c16b5671d0e 1083 depthLoop().setCommand(0.5);
tnhnrl 32:f2f8ae34aadc 1084 pitchLoop().setCommand(-sequence_pitch_command);
tnhnrl 74:d281aaef9766 1085
tnhnrl 74:d281aaef9766 1086 headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code)
joel_ssc 85:dd8176285b6e 1087 xbee().printf("MULTI-RISE: depth cmd: 0.0 m, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand());
tnhnrl 17:7c16b5671d0e 1088 }
tnhnrl 20:8987a9ae2bc7 1089
tnhnrl 17:7c16b5671d0e 1090 // how exit?
tnhnrl 73:f6f378311c8d 1091 if (_fsm_timer > _timeout) {
tnhnrl 74:d281aaef9766 1092 xbee().printf("MULTI-RISE: timed out [time: %0.1f]\r\n\n", _fsm_timer.read());
tnhnrl 21:38c8544db6f4 1093 _state = EMERGENCY_CLIMB;
tnhnrl 73:f6f378311c8d 1094 _fsm_timer.reset();
tnhnrl 28:16c83a2fdefa 1095 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 1096
tnhnrl 17:7c16b5671d0e 1097 //reset multi-dive sequence to start
tnhnrl 24:c7d9b5bf3829 1098 _multi_dive_counter = 0;
tnhnrl 63:6cb0405fc6e6 1099
tnhnrl 68:8f549749b8ce 1100 // //Reload the dive sequence on exit
tnhnrl 68:8f549749b8ce 1101 // sequenceController().loadSequence();
tnhnrl 17:7c16b5671d0e 1102 }
tnhnrl 20:8987a9ae2bc7 1103 else if (depthLoop().getPosition() < 0.5) { // depth is less than 0.5 (zero is surface level)
tnhnrl 74:d281aaef9766 1104 xbee().printf("MULTI-RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 1105
tnhnrl 17:7c16b5671d0e 1106 //going to next state
tnhnrl 28:16c83a2fdefa 1107 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 1108
tnhnrl 17:7c16b5671d0e 1109 //successful dive-rise sequence CONTINUES the multi-dive sequence
tnhnrl 24:c7d9b5bf3829 1110 _multi_dive_counter++;
tnhnrl 17:7c16b5671d0e 1111
tnhnrl 17:7c16b5671d0e 1112 //UPDATE THE SEQUENCE DATA HERE
tnhnrl 17:7c16b5671d0e 1113 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 1114
tnhnrl 17:7c16b5671d0e 1115 //check if this is the end of the dive sequence
tnhnrl 30:2964617e7676 1116 //CHECK BEFORE ANYTHING ELSE that you have reached the "exit" state (FLOAT_BROADCAST)
tnhnrl 30:2964617e7676 1117 if (currentStateStruct.state == FLOAT_BROADCAST) {
tnhnrl 68:8f549749b8ce 1118 // //Reload the dive sequence on exit
tnhnrl 68:8f549749b8ce 1119 // sequenceController().loadSequence();
tnhnrl 63:6cb0405fc6e6 1120
tnhnrl 28:16c83a2fdefa 1121 _state = FLOAT_BROADCAST;
tnhnrl 17:7c16b5671d0e 1122 }
tnhnrl 17:7c16b5671d0e 1123
tnhnrl 17:7c16b5671d0e 1124 else
tnhnrl 63:6cb0405fc6e6 1125 _state = MULTI_DIVE; //Note: need to test if this else statement is necessary
tnhnrl 17:7c16b5671d0e 1126
tnhnrl 24:c7d9b5bf3829 1127 //have to stop this with the _multi_dive_counter variable!
tnhnrl 17:7c16b5671d0e 1128 }
tnhnrl 20:8987a9ae2bc7 1129
tnhnrl 67:c86a4b464682 1130 // WHAT IS ACTIVE?
joel_ssc 85:dd8176285b6e 1131 xbee().printf("MR: BcePos (cmd):%6.1f mm(%0.1f), BattPos:%6.1f mm(%0.1f), RUD_deg_cmd: %5.1f <<current depth:%6.1f m [cmd:%6.1f]), pitch:%6.1f deg [cmd:%6.1f], heading_imu:%6.1f deg>>[%0.2f sec] \r", bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),depthLoop().getCommand(),pitchLoop().getPosition(),pitchLoop().getCommand(),imu().getHeading(),_fsm_timer.read());
tnhnrl 17:7c16b5671d0e 1132 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 17:7c16b5671d0e 1133 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 1134
tnhnrl 74:d281aaef9766 1135 // ACTIVE RUDDER CONTROL
tnhnrl 74:d281aaef9766 1136 rudder().setPosition_deg(headingLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 1137
tnhnrl 17:7c16b5671d0e 1138 break;
tnhnrl 32:f2f8ae34aadc 1139
tnhnrl 71:939d179478c4 1140 case TX_MBED_LOG:
tnhnrl 63:6cb0405fc6e6 1141 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 1142 xbee().printf("\r\n\nstate: TX_MBED_LOG\r\n");
tnhnrl 73:f6f378311c8d 1143 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 1144 _fsm_timer.start(); // background timer starts running
tnhnrl 32:f2f8ae34aadc 1145 _isTimeoutRunning = true;
tnhnrl 32:f2f8ae34aadc 1146
tnhnrl 68:8f549749b8ce 1147 //mbedLogger().openFileForTransmit(); //starts _fp file pointer at the beginning of the file
tnhnrl 68:8f549749b8ce 1148
tnhnrl 74:d281aaef9766 1149 xbee().printf("TX_MBED_LOG set to zero\n\r");
tnhnrl 45:16b8162188ca 1150
tnhnrl 69:919ac8d7e023 1151 //function to read the file and get number of lines (packets to transmit)
tnhnrl 69:919ac8d7e023 1152 _timeout = mbedLogger().getNumberOfPacketsInCurrentLog();
tnhnrl 68:8f549749b8ce 1153
tnhnrl 68:8f549749b8ce 1154 mbedLogger().setTransmitPacketNumber(0); //reset to zero
tnhnrl 45:16b8162188ca 1155 }
tnhnrl 45:16b8162188ca 1156
tnhnrl 69:919ac8d7e023 1157 //TIMED OUT
tnhnrl 73:f6f378311c8d 1158 if (_fsm_timer.read() > _timeout) {
tnhnrl 74:d281aaef9766 1159 xbee().printf("\r\nTX_MBED_LOG: timed out!\r\n");
tnhnrl 63:6cb0405fc6e6 1160
tnhnrl 63:6cb0405fc6e6 1161 //STATE
tnhnrl 45:16b8162188ca 1162 _state = SIT_IDLE;
tnhnrl 63:6cb0405fc6e6 1163
tnhnrl 73:f6f378311c8d 1164 _fsm_timer.reset();
tnhnrl 63:6cb0405fc6e6 1165 _isTimeoutRunning = false;
tnhnrl 63:6cb0405fc6e6 1166
tnhnrl 73:f6f378311c8d 1167 //mbedLogger().closeLogFile();
tnhnrl 74:d281aaef9766 1168 xbee().printf("\r\n\nstate: TX_MBED_LOG (log filed closed)\r\n");
tnhnrl 63:6cb0405fc6e6 1169 }
tnhnrl 63:6cb0405fc6e6 1170
tnhnrl 73:f6f378311c8d 1171 //received end transmission packet (if this is true)
tnhnrl 73:f6f378311c8d 1172 if (mbedLogger().endTransmitPacket()) {
tnhnrl 73:f6f378311c8d 1173 //STATE
tnhnrl 73:f6f378311c8d 1174 led3() = !led3();
tnhnrl 73:f6f378311c8d 1175
tnhnrl 73:f6f378311c8d 1176 _state = SIT_IDLE;
tnhnrl 73:f6f378311c8d 1177
tnhnrl 73:f6f378311c8d 1178 //reset timer
tnhnrl 73:f6f378311c8d 1179 _fsm_timer.reset();
tnhnrl 73:f6f378311c8d 1180
tnhnrl 73:f6f378311c8d 1181 //mbedLogger().closeLogFile();
tnhnrl 73:f6f378311c8d 1182
tnhnrl 74:d281aaef9766 1183 xbee().printf("\r\n\nTX_MBED_LOG: Received end transmission packet.)\r\n");
tnhnrl 73:f6f378311c8d 1184 }
tnhnrl 73:f6f378311c8d 1185
tnhnrl 68:8f549749b8ce 1186 //What is active?
tnhnrl 73:f6f378311c8d 1187 //mbedLogger().fsmTransmitData();
tnhnrl 69:919ac8d7e023 1188 mbedLogger().checkForPythonTransmitRequest();
tnhnrl 73:f6f378311c8d 1189 led1() = !led1();
tnhnrl 68:8f549749b8ce 1190
tnhnrl 45:16b8162188ca 1191 break;
tnhnrl 45:16b8162188ca 1192
tnhnrl 74:d281aaef9766 1193 case RX_SEQUENCE :
tnhnrl 74:d281aaef9766 1194 xbee().printf("state: RX_SEQUENCE\r\n");
tnhnrl 45:16b8162188ca 1195
tnhnrl 45:16b8162188ca 1196 if (!_isTimeoutRunning) {
tnhnrl 74:d281aaef9766 1197 xbee().printf("RX_SEQUENCE _isTimeoutRunning\r\n");
tnhnrl 73:f6f378311c8d 1198 _fsm_timer.reset(); // timer goes back to zero
tnhnrl 73:f6f378311c8d 1199 _fsm_timer.start(); // background timer starts running
tnhnrl 45:16b8162188ca 1200 _isTimeoutRunning = true;
tnhnrl 45:16b8162188ca 1201 }
tnhnrl 45:16b8162188ca 1202
tnhnrl 73:f6f378311c8d 1203 if (_fsm_timer.read() > _timeout) {
tnhnrl 74:d281aaef9766 1204 xbee().printf("RX_SEQUENCE: timed out!\r\n");
tnhnrl 34:9b66c5188051 1205 _state = SIT_IDLE;
tnhnrl 73:f6f378311c8d 1206 _fsm_timer.reset();
tnhnrl 32:f2f8ae34aadc 1207 _isTimeoutRunning = false;
tnhnrl 32:f2f8ae34aadc 1208 }
tnhnrl 32:f2f8ae34aadc 1209
tnhnrl 45:16b8162188ca 1210 // what is active?
tnhnrl 74:d281aaef9766 1211 xbee().printf("Receive sequence active?\r\n");
tnhnrl 32:f2f8ae34aadc 1212
tnhnrl 32:f2f8ae34aadc 1213 break;
tnhnrl 16:3363b9f14913 1214
tnhnrl 16:3363b9f14913 1215 default :
tnhnrl 74:d281aaef9766 1216 xbee().printf("DEBUG: SIT_IDLE\r\n");
tnhnrl 21:38c8544db6f4 1217 _state = SIT_IDLE;
tnhnrl 28:16c83a2fdefa 1218 }
tnhnrl 28:16c83a2fdefa 1219
tnhnrl 28:16c83a2fdefa 1220 //save the state to print to user
tnhnrl 28:16c83a2fdefa 1221 if (_previous_state != _state) {
tnhnrl 28:16c83a2fdefa 1222 _state_array[_state_array_counter] = _state; //save to state array
tnhnrl 28:16c83a2fdefa 1223 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 1224
tnhnrl 28:16c83a2fdefa 1225 _previous_state = _state;
tnhnrl 28:16c83a2fdefa 1226 }
tnhnrl 34:9b66c5188051 1227
tnhnrl 34:9b66c5188051 1228 return _state;
tnhnrl 73:f6f378311c8d 1229 } /* end of runStateMachine */
tnhnrl 20:8987a9ae2bc7 1230
tnhnrl 16:3363b9f14913 1231 // output the keyboard menu for user's reference
tnhnrl 69:919ac8d7e023 1232 void StateMachine::printSimpleMenu() {
tnhnrl 74:d281aaef9766 1233 xbee().printf("\r\r\n\nSIMPLE KEYBOARD MENU (08/13/2018):\r\r\n"); //make sure depth sensor tares itself on startup
tnhnrl 74:d281aaef9766 1234 xbee().printf(" Neutral Positions BCE: %0.1f BMM: %0.1f (LIMIT: BCE: %0.1f BATT: %0.1f)\r\n\n", _neutral_bce_pos_mm, _neutral_batt_pos_mm, bce().getTravelLimit(),batt().getTravelLimit());
tnhnrl 58:94b7fd55185e 1235
tnhnrl 74:d281aaef9766 1236 xbee().printf(" V to POSITION DIVE (initiate motor position-based dive cycle)\r\n");
tnhnrl 74:d281aaef9766 1237 xbee().printf(" J to float level\r\n");
tnhnrl 74:d281aaef9766 1238 xbee().printf(" B to float at broadcast pitch\r\n");
tnhnrl 74:d281aaef9766 1239 xbee().printf(" E to initiate emergency climb\r\n");
tnhnrl 74:d281aaef9766 1240 xbee().printf(" P to print the current log file.\r\n");
tnhnrl 74:d281aaef9766 1241 xbee().printf(" G to transmit MBED log file\r\n");
tnhnrl 74:d281aaef9766 1242 xbee().printf(" I to receive multi-dive sequence file\r\n");
tnhnrl 74:d281aaef9766 1243 xbee().printf(" ~ to erase mbed log file. (clear before logging more than a few runs)\r\n");
tnhnrl 66:0f20870117b7 1244
tnhnrl 74:d281aaef9766 1245 xbee().printf("Q to TYPE in the BMM offset: %0.1f (BMM Dive POS: %0.1f, Rise POS: %0.1f) (positive input offset = pitch down)\r\n",_BMM_dive_offset, _neutral_batt_pos_mm + _BMM_dive_offset, _neutral_batt_pos_mm - _BMM_dive_offset);
tnhnrl 74:d281aaef9766 1246 xbee().printf("A to TYPE in the BCE offset: %0.1f (BCE Dive POS: %0.1f, Rise POS: %0.1f) (positive input offset = dive)\r\n",_BCE_dive_offset, _neutral_bce_pos_mm - _BCE_dive_offset, _neutral_bce_pos_mm + _BCE_dive_offset);
tnhnrl 74:d281aaef9766 1247 xbee().printf("W to TYPE in the heading command: %0.1f deg (imu heading: %0.1f)\r\n",_heading_command,imu().getHeading());
joel_ssc 85:dd8176285b6e 1248 xbee().printf("S to TYPE in depth setpoint: %0.1f (Current depth: %0.1f m)\r\n",_depth_command, depthLoop().getPosition());
tnhnrl 74:d281aaef9766 1249 xbee().printf("T to TYPE in the timeout (default is 60 seconds): %d s\r\n",_timeout);
tnhnrl 58:94b7fd55185e 1250
tnhnrl 74:d281aaef9766 1251 xbee().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 74:d281aaef9766 1252 xbee().printf(" 8 STREAM SENSOR STATUS (and channel readings)\r\n");
tnhnrl 74:d281aaef9766 1253 xbee().printf(" ? to reset mbed\r\n");
tnhnrl 74:d281aaef9766 1254 xbee().printf(" * (asterisk) to go to DEBUG keyboard menu\r\n");
tnhnrl 57:ec69651c8c21 1255 }
tnhnrl 57:ec69651c8c21 1256
tnhnrl 69:919ac8d7e023 1257 void StateMachine::printDebugMenu() {
tnhnrl 74:d281aaef9766 1258 xbee().printf("\r\r\n\nDEBUG KEYBOARD MENU (08/08/2018):\r\r\n");
tnhnrl 74:d281aaef9766 1259 xbee().printf(" Y to go into CHECK NEUTRAL TUNING (This is on a timer! Uses NEUTRAL positions!)\r\n");
tnhnrl 74:d281aaef9766 1260 xbee().printf(" N to find neutral\r\n");
tnhnrl 74:d281aaef9766 1261 xbee().printf(" M to initiate multi-dive cycle\r\n");
tnhnrl 74:d281aaef9766 1262 xbee().printf(" D to initiate dive cycle\r\n");
tnhnrl 74:d281aaef9766 1263 xbee().printf(" R to initiate rise\r\n");
tnhnrl 74:d281aaef9766 1264 xbee().printf(" J to float level\r\n");
tnhnrl 74:d281aaef9766 1265 xbee().printf(" B to float at broadcast pitch\r\n");
tnhnrl 74:d281aaef9766 1266 xbee().printf(" E to initiate emergency climb\r\n");
tnhnrl 74:d281aaef9766 1267 xbee().printf(" '}' to HOME the BCE (5 second delay)\r\n");
tnhnrl 74:d281aaef9766 1268 xbee().printf(" '|' to HOME the BMM (5 second delay)\r\n");
tnhnrl 74:d281aaef9766 1269 xbee().printf(" Z to show FSM and sub-FSM states.\r\n");
tnhnrl 74:d281aaef9766 1270 xbee().printf(" P to print the current log file.\r\n");
tnhnrl 74:d281aaef9766 1271 xbee().printf(" X to print the list of log files.\r\n");
tnhnrl 74:d281aaef9766 1272 xbee().printf(" I to receive data.\r\n");
tnhnrl 74:d281aaef9766 1273 xbee().printf(" G to transmit MBED log file (60 second timeout)\r\n");
tnhnrl 74:d281aaef9766 1274 xbee().printf(" ~ to erase mbed log file. (clear before logging more than a few runs)\r\n");
tnhnrl 74:d281aaef9766 1275 xbee().printf("; or : to TYPE in the BCE neutral position: %0.1f\r\n", _neutral_bce_pos_mm);
tnhnrl 74:d281aaef9766 1276 xbee().printf("[ or { to TYPE in the BMM neutral position: %0.1f\r\n", _neutral_batt_pos_mm);
tnhnrl 74:d281aaef9766 1277 xbee().printf("Q to TYPE in pitch setpoint: %0.1f (Current IMU pitch: %0.1f deg)\r\n",_pitch_command,imu().getPitch());
joel_ssc 85:dd8176285b6e 1278 xbee().printf("A to TYPE in depth setpoint: %0.1f (Current depth: %0.1f m)\r\n",_depth_command, depthLoop().getPosition());
tnhnrl 74:d281aaef9766 1279 xbee().printf("W to TYPE in the heading command: %0.1f deg (imu heading: %0.1f)\r\n",_heading_command,imu().getHeading());
tnhnrl 74:d281aaef9766 1280 xbee().printf("T to enter in the timeout (default is 60 seconds): %d s\r\n",_timeout);
tnhnrl 58:94b7fd55185e 1281
tnhnrl 74:d281aaef9766 1282 xbee().printf(" 1 BCE PID sub-menu (type in inputs)\r\n");
tnhnrl 74:d281aaef9766 1283 xbee().printf(" 2 BATT PID sub-menu (type in inputs)\r\n");
tnhnrl 74:d281aaef9766 1284 xbee().printf(" 3 Depth PID sub-menu (type in inputs)\r\n");
tnhnrl 74:d281aaef9766 1285 xbee().printf(" 4 Pitch PID sub-menu (type in inputs)\r\n");
tnhnrl 74:d281aaef9766 1286 xbee().printf(" 5 Rudder (servo) sub-menu\r\n");
tnhnrl 74:d281aaef9766 1287 xbee().printf(" 6 HEADING PID sub-menu (type in inputs)\r\n");
tnhnrl 74:d281aaef9766 1288 xbee().printf(" 7 MANUAL_TUNING sub-menu (does not have a timer!) *** MOTORS ARE ACTIVE *** (bce 200, bmm 40, rudder 1640)\r\n");
tnhnrl 74:d281aaef9766 1289 xbee().printf(" 8 STREAM SENSOR STATUS (and channel readings)\r\n");
tnhnrl 53:c0586fe62b01 1290
tnhnrl 74:d281aaef9766 1291 xbee().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 74:d281aaef9766 1292 xbee().printf(" ? to reset mbed\r\n");
tnhnrl 74:d281aaef9766 1293 xbee().printf(" * (asterisk) to go to SIMPLE keyboard menu\r\n");
tnhnrl 16:3363b9f14913 1294 }
tnhnrl 20:8987a9ae2bc7 1295
tnhnrl 74:d281aaef9766 1296 //FIND_NEUTRAL sub-Finite State Machine (sub-FSM)
tnhnrl 67:c86a4b464682 1297 // Note: the sub-FSM only moves the pistons once at the start of each timer loop
tnhnrl 67:c86a4b464682 1298 // (timer completes, moves piston, timer completes, moves piston, etc)
joel_ssc 87:6d95f853dab3 1299 int StateMachine::runNeutralStateMachine() {
joel_ssc 87:6d95f853dab3 1300 char buf[256];
tnhnrl 24:c7d9b5bf3829 1301 switch (_substate) {
tnhnrl 20:8987a9ae2bc7 1302 case NEUTRAL_SINKING :
tnhnrl 17:7c16b5671d0e 1303 //start the 10 second timer
tnhnrl 28:16c83a2fdefa 1304 if (!_isSubStateTimerRunning) {
tnhnrl 73:f6f378311c8d 1305 _neutral_timer = _fsm_timer.read() + 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 1306
tnhnrl 74:d281aaef9766 1307 xbee().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f) (BCE getSetPosition: %0.1f)\r\n", _neutral_timer, _fsm_timer.read(), pitchLoop().getPosition(), bce().getSetPosition_mm());
tnhnrl 20:8987a9ae2bc7 1308
tnhnrl 74:d281aaef9766 1309 // what are the commands? (BCE linear actuator active, no BMM or pitch movement)
tnhnrl 74:d281aaef9766 1310 bce().setPosition_mm(bce().getSetPosition_mm() - 2.5);
tnhnrl 23:434f04ef1fad 1311
tnhnrl 74:d281aaef9766 1312 xbee().printf("NEUTRAL_SINKING: Retracting piston 2.5 mm [BCE CMD : %0.1f] (pitch: %0.1f)\r\n", bce().getSetPosition_mm(), pitchLoop().getPosition());
tnhnrl 17:7c16b5671d0e 1313
tnhnrl 28:16c83a2fdefa 1314 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 1315 }
tnhnrl 20:8987a9ae2bc7 1316
tnhnrl 20:8987a9ae2bc7 1317 // how exit?
tnhnrl 20:8987a9ae2bc7 1318 //once reached the travel limit, no need to keep trying, so exit
tnhnrl 25:249e4d56b27c 1319 if (bce().getPosition_mm() <= 0) {
tnhnrl 74:d281aaef9766 1320 xbee().printf("\r\nDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\r\n", bce().getPosition_mm());
joel_ssc 87:6d95f853dab3 1321 sprintf(buf, "in RUNneutral: state failed at bce.getPosition()\n\n");
joel_ssc 87:6d95f853dab3 1322 mbedLogger().appendDiagFile(buf,3);
tnhnrl 25:249e4d56b27c 1323 _substate = NEUTRAL_EXIT;
joel_ssc 87:6d95f853dab3 1324 _neutral_success = 0;
tnhnrl 28:16c83a2fdefa 1325 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 74:d281aaef9766 1326 }
joel_ssc 87:6d95f853dab3 1327
tnhnrl 74:d281aaef9766 1328 //Troy: Pressure vessel went beyond set depth limit, goes to next state
tnhnrl 20:8987a9ae2bc7 1329 //once deeper than the commanded setpoint...
tnhnrl 32:f2f8ae34aadc 1330 else if (depthLoop().getPosition() > _depth_command) {
tnhnrl 24:c7d9b5bf3829 1331 _substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 28:16c83a2fdefa 1332 _isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 20:8987a9ae2bc7 1333 }
joel_ssc 87:6d95f853dab3 1334 else if ( (depthLoop().getVelocity() > 0.05) && (depthLoop().getPosition() > 0.6 * _depth_command) ) { // need to code in FILTERED vertical velocity
joel_ssc 87:6d95f853dab3 1335 _substate = NEUTRAL_SLOWLY_RISE; // next state
joel_ssc 87:6d95f853dab3 1336 _isSubStateTimerRunning = false; //reset the sub state timer
joel_ssc 87:6d95f853dab3 1337 }
tnhnrl 20:8987a9ae2bc7 1338 // what is active?
tnhnrl 20:8987a9ae2bc7 1339 //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 73:f6f378311c8d 1340 if (_fsm_timer.read() >= _neutral_timer) {
tnhnrl 74:d281aaef9766 1341 xbee().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", _fsm_timer.read());
tnhnrl 17:7c16b5671d0e 1342
tnhnrl 28:16c83a2fdefa 1343 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 1344 }
tnhnrl 39:58375ca6b6ff 1345
tnhnrl 39:58375ca6b6ff 1346 // what is active? (only the buoyancy engine moved every 5 seconds at start)
joel_ssc 85:dd8176285b6e 1347 xbee().printf("BCE current pos: %0.1f mm (BCE setpoint: %0.1f mm) (current depth: %0.1f m)\r", bce().getPosition_mm(),bce().getSetPosition_mm(),depthLoop().getPosition()); //debug
tnhnrl 67:c86a4b464682 1348
tnhnrl 74:d281aaef9766 1349 //the BCE moves every 5 seconds. No BMM or rudder movement.
tnhnrl 74:d281aaef9766 1350
tnhnrl 17:7c16b5671d0e 1351 break;
tnhnrl 17:7c16b5671d0e 1352
tnhnrl 17:7c16b5671d0e 1353 case NEUTRAL_SLOWLY_RISE:
tnhnrl 28:16c83a2fdefa 1354 if (!_isSubStateTimerRunning) {
tnhnrl 73:f6f378311c8d 1355 _neutral_timer = _fsm_timer.read()+ 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 1356
tnhnrl 74:d281aaef9766 1357 xbee().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Next extension at %0.1f sec) [current time: %0.1f]\r\n",_neutral_timer,_fsm_timer.read());
tnhnrl 17:7c16b5671d0e 1358
tnhnrl 20:8987a9ae2bc7 1359 // what are the commands?
tnhnrl 32:f2f8ae34aadc 1360 //move piston at start of sequence (default: extend 2.0 mm)
tnhnrl 74:d281aaef9766 1361 //Pressure vessel should slowly rise
tnhnrl 74:d281aaef9766 1362 bce().setPosition_mm(bce().getSetPosition_mm() + 2.0); //no depth command, only motor position
tnhnrl 23:434f04ef1fad 1363
tnhnrl 74:d281aaef9766 1364 //Troy: I commented out this command, we're finding pitch in the next state.
tnhnrl 23:434f04ef1fad 1365 // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state
tnhnrl 74:d281aaef9766 1366 //pitchLoop().setCommand(0.0);
tnhnrl 24:c7d9b5bf3829 1367
tnhnrl 74:d281aaef9766 1368 xbee().printf("NEUTRAL_SLOWLY_RISE: Extending BCE piston 2.0 mm [BCE CMD : %0.1f] (pitch: %0.1f)\r\n", bce().getSetPosition_mm(), pitchLoop().getPosition());
tnhnrl 24:c7d9b5bf3829 1369
tnhnrl 28:16c83a2fdefa 1370 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 1371 }
tnhnrl 17:7c16b5671d0e 1372
tnhnrl 20:8987a9ae2bc7 1373 // how exit?
tnhnrl 24:c7d9b5bf3829 1374 //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit
tnhnrl 24:c7d9b5bf3829 1375 if (bce().getSetPosition_mm() >= bce().getTravelLimit()) {
joel_ssc 87:6d95f853dab3 1376 _substate = NEUTRAL_EXIT;
joel_ssc 87:6d95f853dab3 1377 sprintf(buf, "in RUNneutral: SLOWLY RISE state failed at bce.getsetPosition()\n\n");
joel_ssc 87:6d95f853dab3 1378 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 1379 _neutral_success =0; //another failed state
tnhnrl 28:16c83a2fdefa 1380 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 1381 }
tnhnrl 74:d281aaef9766 1382 //Troy: Depth rate will go negative as the pressure vessel starts rising
tnhnrl 17:7c16b5671d0e 1383 //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration
joel_ssc 85:dd8176285b6e 1384 else if (depthLoop().getVelocity() < 0) { //less than zero ft/s should now be m/s
joel_ssc 85:dd8176285b6e 1385 xbee().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 m/s [time: %0.1f]\r\n", _fsm_timer.read());
tnhnrl 24:c7d9b5bf3829 1386 _substate = NEUTRAL_CHECK_PITCH;
tnhnrl 28:16c83a2fdefa 1387 _isSubStateTimerRunning = false; // reset the sub state timer
joel_ssc 87:6d95f853dab3 1388 }
tnhnrl 20:8987a9ae2bc7 1389 // what is active?
tnhnrl 20:8987a9ae2bc7 1390 //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 73:f6f378311c8d 1391 if (_fsm_timer.read() >= _neutral_timer) {
tnhnrl 74:d281aaef9766 1392 xbee().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! [timer: %0.1f]\r\n", _fsm_timer.read());
tnhnrl 20:8987a9ae2bc7 1393
tnhnrl 28:16c83a2fdefa 1394 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 1395 }
tnhnrl 23:434f04ef1fad 1396
tnhnrl 32:f2f8ae34aadc 1397 // what is active? (only the buoyancy engine moved every 5 seconds)
tnhnrl 74:d281aaef9766 1398 xbee().printf("depthLoop getOutput: %0.1f\r", depthLoop().getOutput()); //debug
tnhnrl 67:c86a4b464682 1399
tnhnrl 17:7c16b5671d0e 1400 break;
tnhnrl 17:7c16b5671d0e 1401
danstrider 22:a10ee088403b 1402 case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired
danstrider 22:a10ee088403b 1403 // start local state timer and init any other one-shot actions
tnhnrl 23:434f04ef1fad 1404
tnhnrl 28:16c83a2fdefa 1405 if (!_isSubStateTimerRunning) {
tnhnrl 73:f6f378311c8d 1406 _neutral_timer = _fsm_timer.read() + 10; // record time when this block is entered and add several seconds
tnhnrl 74:d281aaef9766 1407 xbee().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - _fsm_timer.read());
danstrider 22:a10ee088403b 1408
tnhnrl 32:f2f8ae34aadc 1409 // what are the commands? (default: retract or extend 0.5 mm)
tnhnrl 74:d281aaef9766 1410 if (pitchLoop().getPosition() > 2) { // nose is high (extend batteries)
tnhnrl 39:58375ca6b6ff 1411 batt().setPosition_mm(batt().getSetPosition_mm() + 0.5); // move battery forward (using setpoint from linear actuator)
tnhnrl 74:d281aaef9766 1412 xbee().printf("\r\nNeutral Check Pitch: moving battery FWD in 0.5 mm increments\r\n\n");
danstrider 22:a10ee088403b 1413 }
tnhnrl 74:d281aaef9766 1414 else if (pitchLoop().getPosition() < -2) { // nose is low (retract batteries)
tnhnrl 39:58375ca6b6ff 1415 batt().setPosition_mm(batt().getSetPosition_mm() - 0.5); // move battery aft (using setpoint from linear actuator)
tnhnrl 74:d281aaef9766 1416 xbee().printf("\r\nNeutral Check Pitch: moving battery AFT in 0.5 mm increments\r\n\n");
danstrider 22:a10ee088403b 1417 }
tnhnrl 24:c7d9b5bf3829 1418
tnhnrl 28:16c83a2fdefa 1419 _isSubStateTimerRunning = true; //disable this block after one iteration
danstrider 22:a10ee088403b 1420 }
tnhnrl 20:8987a9ae2bc7 1421
tnhnrl 28:16c83a2fdefa 1422 // how exit?
tnhnrl 20:8987a9ae2bc7 1423 //pitch angle and pitch rate within small tolerance
tnhnrl 20:8987a9ae2bc7 1424 //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 23:434f04ef1fad 1425 if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) {
tnhnrl 74:d281aaef9766 1426 xbee().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\r\n"); //debug
danstrider 22:a10ee088403b 1427 // found level, but don't need to save anything this time
tnhnrl 23:434f04ef1fad 1428
tnhnrl 28:16c83a2fdefa 1429 if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug
tnhnrl 28:16c83a2fdefa 1430 _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 1431 }
tnhnrl 28:16c83a2fdefa 1432
tnhnrl 23:434f04ef1fad 1433 // found level and at depth too, so save it all now
tnhnrl 32:f2f8ae34aadc 1434 if (_substate == NEUTRAL_CHECK_PITCH) {
danstrider 22:a10ee088403b 1435 //save positions locally
danstrider 22:a10ee088403b 1436 _neutral_batt_pos_mm = batt().getPosition_mm();
danstrider 22:a10ee088403b 1437 _neutral_bce_pos_mm = bce().getPosition_mm();
danstrider 22:a10ee088403b 1438
danstrider 22:a10ee088403b 1439 //set the neutral positions in each outer loop
danstrider 22:a10ee088403b 1440 depthLoop().setOutputOffset(_neutral_bce_pos_mm);
danstrider 22:a10ee088403b 1441 pitchLoop().setOutputOffset(_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 1442
danstrider 22:a10ee088403b 1443 // save into the depth.txt and pitch.txt files
tnhnrl 73:f6f378311c8d 1444
tnhnrl 73:f6f378311c8d 1445 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm, _pitch_filter_freq, _pitch_deadband); //P,I,D,batt zeroOffset
tnhnrl 73:f6f378311c8d 1446 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm, _depth_filter_freq, _depth_deadband); //P,I,D, bce zeroOffset
tnhnrl 73:f6f378311c8d 1447
tnhnrl 74:d281aaef9766 1448 xbee().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 1449
joel_ssc 87:6d95f853dab3 1450 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 24:c7d9b5bf3829 1451 _substate = NEUTRAL_EXIT;
joel_ssc 87:6d95f853dab3 1452 sprintf(buf, "in RUNneutral: state succeeded! at NEUTRAL_CHECK PITCH line 1452\n\n");
joel_ssc 87:6d95f853dab3 1453 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 1454 _neutral_success = 1;
joel_ssc 87:6d95f853dab3 1455 configFileIO().saveNeutralStatus(_neutral_success, _neutral_bce_pos_mm, _neutral_batt_pos_mm); // saves to tell re-started program neutral has been found
joel_ssc 87:6d95f853dab3 1456
tnhnrl 24:c7d9b5bf3829 1457 }
tnhnrl 24:c7d9b5bf3829 1458
tnhnrl 24:c7d9b5bf3829 1459 else {
tnhnrl 74:d281aaef9766 1460 xbee().printf("\r\nDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\r\n");
tnhnrl 24:c7d9b5bf3829 1461 _substate = NEUTRAL_EXIT;
joel_ssc 87:6d95f853dab3 1462 sprintf(buf, "in RUNneutral: state failed at final fallthrough point line 1462\n\n");
joel_ssc 87:6d95f853dab3 1463 mbedLogger().appendDiagFile(buf,3);
danstrider 22:a10ee088403b 1464 }
tnhnrl 17:7c16b5671d0e 1465 }
danstrider 22:a10ee088403b 1466
danstrider 22:a10ee088403b 1467 // what is active?
danstrider 22:a10ee088403b 1468 //once timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 73:f6f378311c8d 1469 if (_fsm_timer.read() >= _neutral_timer) {
tnhnrl 74:d281aaef9766 1470 xbee().printf("\r\n\nlevel timer COMPLETE!");
tnhnrl 74:d281aaef9766 1471 xbee().printf("\r\n\n (BATT POS: %0.1f) moving 1 mm [timer: %0.1f]\r\n", batt().getPosition_mm(), _fsm_timer.read());
tnhnrl 28:16c83a2fdefa 1472 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
danstrider 22:a10ee088403b 1473 }
tnhnrl 74:d281aaef9766 1474
tnhnrl 17:7c16b5671d0e 1475 break;
danstrider 22:a10ee088403b 1476
danstrider 22:a10ee088403b 1477 //this state could be removed, it is only used as a transition but is needed to stop entering this function
danstrider 22:a10ee088403b 1478 case NEUTRAL_EXIT :
tnhnrl 74:d281aaef9766 1479 xbee().printf("substate: NEUTRAL_EXIT\r\n");
tnhnrl 20:8987a9ae2bc7 1480 break;
tnhnrl 21:38c8544db6f4 1481
danstrider 22:a10ee088403b 1482 default :
tnhnrl 74:d281aaef9766 1483 xbee().printf("how did we get to substate: default?\r\n"); //debug
tnhnrl 23:434f04ef1fad 1484 //a default within the sub-state machine
tnhnrl 24:c7d9b5bf3829 1485 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 1486 break;
tnhnrl 17:7c16b5671d0e 1487 }
tnhnrl 20:8987a9ae2bc7 1488
tnhnrl 30:2964617e7676 1489 // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence)
tnhnrl 24:c7d9b5bf3829 1490 if (_substate == NEUTRAL_EXIT) {
tnhnrl 74:d281aaef9766 1491 xbee().printf("******************************** EXITING sub-FSM! *******************************\r\n\n");
tnhnrl 24:c7d9b5bf3829 1492
tnhnrl 30:2964617e7676 1493 //reset internal sub-state back to first entry conditions (first state is immediately sinking)
tnhnrl 30:2964617e7676 1494 _substate = NEUTRAL_SINKING;
tnhnrl 28:16c83a2fdefa 1495 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 21:38c8544db6f4 1496
tnhnrl 24:c7d9b5bf3829 1497 //record sub-states to view after sequence
tnhnrl 30:2964617e7676 1498 _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array
tnhnrl 28:16c83a2fdefa 1499 _substate_array_counter++;
tnhnrl 23:434f04ef1fad 1500
tnhnrl 24:c7d9b5bf3829 1501 //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit)
tnhnrl 24:c7d9b5bf3829 1502 _previous_substate = -1;
tnhnrl 24:c7d9b5bf3829 1503
tnhnrl 24:c7d9b5bf3829 1504 //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 24:c7d9b5bf3829 1505 return NEUTRAL_EXIT; // message to calling function we just exited
tnhnrl 21:38c8544db6f4 1506 }
tnhnrl 23:434f04ef1fad 1507 else {
tnhnrl 24:c7d9b5bf3829 1508 //record sub-states to view after sequence (when changed)
tnhnrl 24:c7d9b5bf3829 1509 if (_previous_substate != _substate) {
tnhnrl 28:16c83a2fdefa 1510 _substate_array[_substate_array_counter] = _substate; //save current state to state array
tnhnrl 28:16c83a2fdefa 1511 _substate_array_counter++;
tnhnrl 24:c7d9b5bf3829 1512
tnhnrl 24:c7d9b5bf3829 1513 //record the current substate for comparison
tnhnrl 24:c7d9b5bf3829 1514 _previous_substate = _substate;
tnhnrl 24:c7d9b5bf3829 1515 }
tnhnrl 24:c7d9b5bf3829 1516
tnhnrl 24:c7d9b5bf3829 1517 return _substate; // message to calling function of what sub-state it's in
tnhnrl 23:434f04ef1fad 1518 }
tnhnrl 17:7c16b5671d0e 1519 }
tnhnrl 20:8987a9ae2bc7 1520
tnhnrl 57:ec69651c8c21 1521 /* keyboard runs independently of the state machine, handling one key at a time
tnhnrl 57:ec69651c8c21 1522 keyboard updates the desired _keyboard_state that is used in the state machine
tnhnrl 57:ec69651c8c21 1523 and only allows input when the state is "idle" */
tnhnrl 57:ec69651c8c21 1524
tnhnrl 17:7c16b5671d0e 1525 void StateMachine::keyboard() {
tnhnrl 57:ec69651c8c21 1526 char user_input;
joel_ssc 85:dd8176285b6e 1527 char buf[256];
tnhnrl 20:8987a9ae2bc7 1528
tnhnrl 16:3363b9f14913 1529 // check keyboard and make settings changes as requested
tnhnrl 17:7c16b5671d0e 1530 // states can be changed only at the start of a sequence (when the system is in SIT_IDLE)
tnhnrl 21:38c8544db6f4 1531
tnhnrl 37:357e98a929cc 1532 //TEST
tnhnrl 37:357e98a929cc 1533 int _keyboard_state = 0; //made this a local variable because it was retaining the last keyboard state
joel_ssc 85:dd8176285b6e 1534 static int yocount = -3;
joel_ssc 85:dd8176285b6e 1535 static float htime = 0.0;
joel_ssc 85:dd8176285b6e 1536 if(_state == ENDLEG_WAIT) {
joel_ssc 85:dd8176285b6e 1537 yocount = int(_yotimer.read());
joel_ssc 85:dd8176285b6e 1538 if ( ( (yocount % 5) == 0 ) && ( _yotimer.read() > 2.5+htime) ) { // send out a heartbeat message, need to cast as an int from float
joel_ssc 85:dd8176285b6e 1539 xbee().printf("FSG on surface at end of leg, hit c to wake up\r\n");
joel_ssc 85:dd8176285b6e 1540 sprintf(buf, "ENDLEG WAIT heartbeat message sent yotimer=%f \n\n\r", _yotimer.read());
joel_ssc 85:dd8176285b6e 1541 mbedLogger().appendDiagFile(buf,0);
joel_ssc 85:dd8176285b6e 1542 htime = _yotimer.read();
joel_ssc 85:dd8176285b6e 1543 }
joel_ssc 85:dd8176285b6e 1544 if( _yotimer > _state_transition_time) {
joel_ssc 85:dd8176285b6e 1545 _keyboard_state = FB_EXIT;
joel_ssc 85:dd8176285b6e 1546 _state = FB_EXIT;
joel_ssc 85:dd8176285b6e 1547 }
joel_ssc 85:dd8176285b6e 1548 }
joel_ssc 87:6d95f853dab3 1549 if (xbee().readable() && (_state == SIT_IDLE || _state == KEYBOARD || _state == ENDLEG_WAIT)) { //ends at very end of function 1740
joel_ssc 85:dd8176285b6e 1550 // then get the key
joel_ssc 85:dd8176285b6e 1551 user_input = xbee().getc();
joel_ssc 85:dd8176285b6e 1552 // and exit from ENDLEG_WAIT - you got a keyboard input
joel_ssc 85:dd8176285b6e 1553 if(_state == ENDLEG_WAIT) {
joel_ssc 85:dd8176285b6e 1554 _keyboard_state = SIT_IDLE; // this is installed in _state at end of function
joel_ssc 85:dd8176285b6e 1555 }
tnhnrl 17:7c16b5671d0e 1556
tnhnrl 28:16c83a2fdefa 1557 //record that the keyboard was used
tnhnrl 28:16c83a2fdefa 1558 _state_array[_state_array_counter] = KEYBOARD;
tnhnrl 28:16c83a2fdefa 1559 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 1560
tnhnrl 21:38c8544db6f4 1561 // keyboard has to reset timer each time it's used
tnhnrl 28:16c83a2fdefa 1562 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 1563
tnhnrl 16:3363b9f14913 1564 // check command against desired control buttons
tnhnrl 72:250b2665755c 1565
tnhnrl 72:250b2665755c 1566 /***************************** COMMON COMMANDS *****************************/
tnhnrl 72:250b2665755c 1567 if (user_input == 'W') {
tnhnrl 74:d281aaef9766 1568 xbee().printf(">> Please enter the heading (deg).\r\n");
tnhnrl 72:250b2665755c 1569 _heading_command = getFloatUserInput();
tnhnrl 72:250b2665755c 1570 }
tnhnrl 73:f6f378311c8d 1571
tnhnrl 73:f6f378311c8d 1572 //////////////////// TEST
tnhnrl 73:f6f378311c8d 1573 // else if (user_input == 'l') {
tnhnrl 74:d281aaef9766 1574 // xbee().printf("CUT OFF RUDDER SERVO????\n\r");
tnhnrl 73:f6f378311c8d 1575 // rudder().pwm_pulse_off();
tnhnrl 73:f6f378311c8d 1576 // }
tnhnrl 73:f6f378311c8d 1577 //////////////////// TEST
tnhnrl 73:f6f378311c8d 1578
tnhnrl 74:d281aaef9766 1579 else if (user_input == 'K') {
tnhnrl 74:d281aaef9766 1580 xbee().printf("(K) BLAST DATA AND EXIT! \n\r");
tnhnrl 74:d281aaef9766 1581 mbedLogger().blastData();
tnhnrl 74:d281aaef9766 1582 }
tnhnrl 74:d281aaef9766 1583
tnhnrl 73:f6f378311c8d 1584 else if (user_input == 'U') {
tnhnrl 74:d281aaef9766 1585 xbee().printf("(U) TRANSMIT MULTIPLE PACKETS \n\r");
tnhnrl 73:f6f378311c8d 1586
tnhnrl 73:f6f378311c8d 1587 mbedLogger().transmitMultiplePackets();
tnhnrl 73:f6f378311c8d 1588 }
tnhnrl 73:f6f378311c8d 1589
tnhnrl 73:f6f378311c8d 1590 else if (user_input == 'H') {
tnhnrl 74:d281aaef9766 1591 xbee().printf("(H) BLAST DATA AND MULTIPLE PACKETS! \n\r");
tnhnrl 73:f6f378311c8d 1592 wait(1);
tnhnrl 73:f6f378311c8d 1593 mbedLogger().blastData();
tnhnrl 73:f6f378311c8d 1594
tnhnrl 73:f6f378311c8d 1595 mbedLogger().transmitMultiplePackets();
tnhnrl 73:f6f378311c8d 1596 }
tnhnrl 73:f6f378311c8d 1597
tnhnrl 73:f6f378311c8d 1598 else if (user_input == 'G') {
tnhnrl 74:d281aaef9766 1599 xbee().printf("(G) BLAST DATA TEST and use FSM! \n\r");
tnhnrl 73:f6f378311c8d 1600 wait(1);
tnhnrl 73:f6f378311c8d 1601 mbedLogger().blastData();
tnhnrl 73:f6f378311c8d 1602
tnhnrl 73:f6f378311c8d 1603 _keyboard_state = TX_MBED_LOG;
tnhnrl 73:f6f378311c8d 1604 }
tnhnrl 73:f6f378311c8d 1605
tnhnrl 74:d281aaef9766 1606 else if (user_input == 'I') {
tnhnrl 74:d281aaef9766 1607 xbee().printf("(I) Receive Multi-Dive Sequence! \n\r");
tnhnrl 74:d281aaef9766 1608 mbedLogger().receiveSequenceFile(); //receive sequence.txt files
tnhnrl 74:d281aaef9766 1609 }
tnhnrl 74:d281aaef9766 1610
tnhnrl 72:250b2665755c 1611 else if (user_input == '8') {
tnhnrl 72:250b2665755c 1612 keyboard_menu_STREAM_STATUS();
tnhnrl 72:250b2665755c 1613 }
tnhnrl 74:d281aaef9766 1614
tnhnrl 72:250b2665755c 1615 else if (user_input == '?') {
tnhnrl 74:d281aaef9766 1616 xbee().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 72:250b2665755c 1617 wait(0.5);
tnhnrl 72:250b2665755c 1618 mbed_reset();
tnhnrl 72:250b2665755c 1619 }
tnhnrl 72:250b2665755c 1620
tnhnrl 72:250b2665755c 1621 else if (user_input == 'T') {
tnhnrl 74:d281aaef9766 1622 xbee().printf("Please enter the timeout (timer) value below: \n\r");
tnhnrl 72:250b2665755c 1623 _timeout = fabs(getFloatUserInput());
tnhnrl 72:250b2665755c 1624 }
tnhnrl 72:250b2665755c 1625
tnhnrl 80:4e5d306d695b 1626 else if (user_input == '~') {
tnhnrl 80:4e5d306d695b 1627 xbee().printf("MBED LOG FILE MENU!\r\n");
tnhnrl 80:4e5d306d695b 1628 stateMachine().logFileMenu();
tnhnrl 80:4e5d306d695b 1629
tnhnrl 80:4e5d306d695b 1630 //xbee().printf("ERASING MBED LOG FILE\r\n"); //legacy method
tnhnrl 80:4e5d306d695b 1631 //mbedLogger().eraseFile();
tnhnrl 80:4e5d306d695b 1632 }
tnhnrl 80:4e5d306d695b 1633
tnhnrl 73:f6f378311c8d 1634 else if (user_input == 'C' or user_input == 'c') {
tnhnrl 73:f6f378311c8d 1635
tnhnrl 74:d281aaef9766 1636 xbee().printf("\r\n\nCURRENT STATUS AND PARAMETERS:\r\n");
tnhnrl 73:f6f378311c8d 1637
tnhnrl 74:d281aaef9766 1638 xbee().printf("raw BCE pos: %d \r\n",adc().readCh0());
tnhnrl 74:d281aaef9766 1639 xbee().printf("raw BMM pos: %d \r\n",adc().readCh1());
tnhnrl 74:d281aaef9766 1640 xbee().printf("raw BCE current sense: %d \r\n",adc().readCh2());
tnhnrl 74:d281aaef9766 1641 xbee().printf("raw BMM current sense: %d \r\n",adc().readCh3());
tnhnrl 74:d281aaef9766 1642 xbee().printf("raw depth pressure: %d \r\n",adc().readCh4());
tnhnrl 74:d281aaef9766 1643 xbee().printf("raw vessel pressure %d (internal psi: %0.1f)\r\n", adc().readCh5(),sensors().getInternalPressurePSI());
tnhnrl 74:d281aaef9766 1644 //xbee().printf("raw vessel pressure: %d \r\n",adc().readCh5());
tnhnrl 74:d281aaef9766 1645 xbee().printf("raw board voltage: %d (%0.1f volts)\r\n",adc().readCh6(),sensors().getVoltageInput());
tnhnrl 74:d281aaef9766 1646 xbee().printf("raw board current: %d (%0.3f amps)\r\n",adc().readCh7(), sensors().getCurrentInput());
tnhnrl 74:d281aaef9766 1647 xbee().printf("raw BCE limit switch: %d \r\n",bce().getSwitch());
tnhnrl 74:d281aaef9766 1648 xbee().printf("raw BMM limit switch: %d \r\n",batt().getSwitch());
tnhnrl 74:d281aaef9766 1649 xbee().printf("calc vessel pressure: %f (counts: %d) \r\n",sensors().getInternalPressurePSI(),adc().readCh5());
tnhnrl 73:f6f378311c8d 1650 // End of ADC Test
tnhnrl 73:f6f378311c8d 1651
joel_ssc 85:dd8176285b6e 1652 xbee().printf("depth: %3.1f m\r\n",depthLoop().getPosition());
tnhnrl 74:d281aaef9766 1653 xbee().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 74:d281aaef9766 1654 xbee().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 74:d281aaef9766 1655 xbee().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 74:d281aaef9766 1656 xbee().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 74:d281aaef9766 1657 xbee().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 74:d281aaef9766 1658 xbee().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 74:d281aaef9766 1659 xbee().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 73:f6f378311c8d 1660
tnhnrl 74:d281aaef9766 1661 xbee().printf("\r\nNeutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
tnhnrl 74:d281aaef9766 1662 xbee().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 74:d281aaef9766 1663 xbee().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 74:d281aaef9766 1664 xbee().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 73:f6f378311c8d 1665
tnhnrl 74:d281aaef9766 1666 xbee().printf("\r\n");
tnhnrl 76:c802e1da4179 1667 xbee().printf("bce P:%6.2f, I:%6.2f, D:%6.2f, zero offset: %3i, limit %6.1f mm, slope %0.5f, filter_freq: %0.1f, deadband: %0.1f\r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope(), bce().getFilterFrequency(), bce().getDeadband());
tnhnrl 76:c802e1da4179 1668 xbee().printf("batt P:%6.2f, I:%6.2f, D:%6.2f, zero offset: %3i, limit %6.1f mm, slope %0.5f, filter_freq: %0.1f, deadband: %0.1f\r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope(), batt().getFilterFrequency(), batt().getDeadband());
tnhnrl 74:d281aaef9766 1669 xbee().printf("rudder min_pwm:%6.1f, center_pwm:%6.1f, max_pwm:%6.1f (min_deg:%6.1f max_deg:%6.1f)\r\n",rudder().getMinPWM(),rudder().getCenterPWM(),rudder().getMaxPWM(),rudder().getMinDeg(),rudder().getMaxDeg());
tnhnrl 74:d281aaef9766 1670 xbee().printf("depth P:%6.2f, I:%6.2f, D:%6.2f, output offset: %6.1f mm, filter_freq: %0.1f, deadband: %0.1f \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset(),depthLoop().getFilterFrequency(),depthLoop().getDeadband());
tnhnrl 74:d281aaef9766 1671 xbee().printf("pitch P:%6.2f, I:%6.2f, D:%6.2f, output offset: %6.1f mm, filter_freq: %0.1f, deadband: %0.1f \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset(),pitchLoop().getFilterFrequency(),pitchLoop().getDeadband());
tnhnrl 74:d281aaef9766 1672 xbee().printf("heading P:%6.2f, I:%6.2f, D:%6.2f, output offset: %6.1f mm, filter_freq: %0.1f, deadband: %0.1f \r\n", headingLoop().getControllerP(), headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(),headingLoop().getFilterFrequency(),headingLoop().getDeadband());
tnhnrl 73:f6f378311c8d 1673 }
tnhnrl 73:f6f378311c8d 1674
tnhnrl 72:250b2665755c 1675 /***************************** COMMON COMMANDS *****************************/
tnhnrl 57:ec69651c8c21 1676
tnhnrl 57:ec69651c8c21 1677 /***************************** DEBUG MENU *****************************/
joel_ssc 85:dd8176285b6e 1678 if (_debug_menu_on) { // ends around +175 lines
tnhnrl 57:ec69651c8c21 1679 if (user_input == 'D') {
tnhnrl 57:ec69651c8c21 1680 _keyboard_state = DIVE;
tnhnrl 57:ec69651c8c21 1681 }
tnhnrl 74:d281aaef9766 1682
tnhnrl 74:d281aaef9766 1683 else if (user_input == '}') {
tnhnrl 74:d281aaef9766 1684 xbee().printf("HOMING the BCE (5 second delay)\r\n");
tnhnrl 74:d281aaef9766 1685 wait(5);
tnhnrl 74:d281aaef9766 1686 bce().homePiston();
tnhnrl 74:d281aaef9766 1687 }
tnhnrl 74:d281aaef9766 1688
tnhnrl 74:d281aaef9766 1689 else if (user_input == '|') {
tnhnrl 74:d281aaef9766 1690 xbee().printf("HOMING the BMM (5 second delay)\r\n");
tnhnrl 74:d281aaef9766 1691 wait(5);
tnhnrl 74:d281aaef9766 1692 batt().homePiston();
tnhnrl 74:d281aaef9766 1693 }
tnhnrl 74:d281aaef9766 1694
tnhnrl 57:ec69651c8c21 1695 else if (user_input == 'N') {
tnhnrl 57:ec69651c8c21 1696 _keyboard_state = FIND_NEUTRAL;
tnhnrl 57:ec69651c8c21 1697 }
tnhnrl 57:ec69651c8c21 1698 else if (user_input == 'M') {
tnhnrl 57:ec69651c8c21 1699 //currently does not run if there is no file.
tnhnrl 57:ec69651c8c21 1700
tnhnrl 57:ec69651c8c21 1701 //need to add method to Sequence Controller that returns -1
tnhnrl 57:ec69651c8c21 1702 // or some check that insures you cannot run the dive sequence without a file
tnhnrl 57:ec69651c8c21 1703
tnhnrl 68:8f549749b8ce 1704 //load sequence from file
tnhnrl 68:8f549749b8ce 1705 _multi_dive_counter = 0;
tnhnrl 68:8f549749b8ce 1706 sequenceController().loadSequence();
tnhnrl 68:8f549749b8ce 1707 wait(1); //test if this resets the sequence
tnhnrl 68:8f549749b8ce 1708
tnhnrl 57:ec69651c8c21 1709 stateMachine().getDiveSequence(); //get first sequence on keyboard press
tnhnrl 57:ec69651c8c21 1710 _keyboard_state = currentStateStruct.state;
tnhnrl 57:ec69651c8c21 1711
tnhnrl 74:d281aaef9766 1712 xbee().printf("Starting Dive Sequence Controller! (state: %d)\r\n", _keyboard_state); //neutral sequence and dive cycles
tnhnrl 57:ec69651c8c21 1713 }
tnhnrl 57:ec69651c8c21 1714 else if (user_input == 'R') {
tnhnrl 57:ec69651c8c21 1715 _keyboard_state = RISE;
tnhnrl 57:ec69651c8c21 1716 }
tnhnrl 57:ec69651c8c21 1717 else if (user_input == 'J') {
tnhnrl 57:ec69651c8c21 1718 _keyboard_state = FLOAT_LEVEL;
tnhnrl 57:ec69651c8c21 1719 }
tnhnrl 57:ec69651c8c21 1720 else if (user_input == 'B') {
tnhnrl 57:ec69651c8c21 1721 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 57:ec69651c8c21 1722 }
tnhnrl 57:ec69651c8c21 1723 else if (user_input == 'E') {
tnhnrl 57:ec69651c8c21 1724 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 57:ec69651c8c21 1725 }
tnhnrl 17:7c16b5671d0e 1726
tnhnrl 65:2ac186553959 1727 else if (user_input == 'Y') {
tnhnrl 57:ec69651c8c21 1728 _keyboard_state = CHECK_TUNING;
tnhnrl 57:ec69651c8c21 1729 }
tnhnrl 17:7c16b5671d0e 1730
tnhnrl 57:ec69651c8c21 1731 // some debug tools below
tnhnrl 57:ec69651c8c21 1732 else if (user_input == 'P') {
tnhnrl 57:ec69651c8c21 1733 //Print current SD card log file
tnhnrl 57:ec69651c8c21 1734 //printCurrentSdLog();
tnhnrl 57:ec69651c8c21 1735 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 57:ec69651c8c21 1736 }
tnhnrl 57:ec69651c8c21 1737 else if (user_input == 'X') {
tnhnrl 57:ec69651c8c21 1738 mbedLogger().printMbedDirectory(); //print all log files to the screen
tnhnrl 57:ec69651c8c21 1739 }
tnhnrl 57:ec69651c8c21 1740 else if (user_input == 'Z') {
tnhnrl 74:d281aaef9766 1741 xbee().printf("FSG FSM States: \r\n");
tnhnrl 57:ec69651c8c21 1742 string string_state;
tnhnrl 57:ec69651c8c21 1743
tnhnrl 57:ec69651c8c21 1744 for (int i = 0; i < _state_array_counter; i++) {
tnhnrl 57:ec69651c8c21 1745 if (_state_array[i] == SIT_IDLE)
tnhnrl 57:ec69651c8c21 1746 string_state = "SIT_IDLE <END>";
tnhnrl 57:ec69651c8c21 1747 else if (_state_array[i] == FIND_NEUTRAL)
tnhnrl 57:ec69651c8c21 1748 string_state = "FIND_NEUTRAL";
tnhnrl 57:ec69651c8c21 1749 else if (_state_array[i] == DIVE)
tnhnrl 57:ec69651c8c21 1750 string_state = "DIVE";
tnhnrl 57:ec69651c8c21 1751 else if (_state_array[i] == RISE)
tnhnrl 57:ec69651c8c21 1752 string_state = "RISE";
tnhnrl 57:ec69651c8c21 1753 else if (_state_array[i] == FLOAT_LEVEL)
tnhnrl 57:ec69651c8c21 1754 string_state = "FLOAT_LEVEL";
tnhnrl 57:ec69651c8c21 1755 else if (_state_array[i] == FLOAT_BROADCAST)
tnhnrl 57:ec69651c8c21 1756 string_state = "FLOAT_BROADCAST";
tnhnrl 57:ec69651c8c21 1757 else if (_state_array[i] == EMERGENCY_CLIMB)
tnhnrl 57:ec69651c8c21 1758 string_state = "EMERGENCY_CLIMB";
tnhnrl 57:ec69651c8c21 1759 else if (_state_array[i] == MULTI_DIVE)
tnhnrl 57:ec69651c8c21 1760 string_state = "MULTI_DIVE";
tnhnrl 57:ec69651c8c21 1761 else if (_state_array[i] == MULTI_RISE)
tnhnrl 57:ec69651c8c21 1762 string_state = "MULTI_RISE";
tnhnrl 57:ec69651c8c21 1763 else if (_state_array[i] == KEYBOARD)
joel_ssc 85:dd8176285b6e 1764 string_state = "KEYBOARD";
joel_ssc 85:dd8176285b6e 1765 else if (_state_array[i] == LEG_POSITION_DIVE)
joel_ssc 85:dd8176285b6e 1766 string_state = "LEG_POS_DIVE";
joel_ssc 85:dd8176285b6e 1767 else if (_state_array[i] == LEG_POSITION_RISE)
joel_ssc 85:dd8176285b6e 1768 string_state = "LEG_POS_RISE";
joel_ssc 85:dd8176285b6e 1769 else if (_state_array[i] == FB_EXIT)
joel_ssc 85:dd8176285b6e 1770 string_state = "FB_EXIT";
joel_ssc 85:dd8176285b6e 1771 else if (_state_array[i] == ENDLEG_WAIT)
joel_ssc 85:dd8176285b6e 1772 string_state = "ENDLEG_WAIT";
tnhnrl 74:d281aaef9766 1773 xbee().printf("State #%d: %d (%s)\r\n", i, _state_array[i], string_state.c_str());
tnhnrl 57:ec69651c8c21 1774 }
tnhnrl 57:ec69651c8c21 1775
tnhnrl 74:d281aaef9766 1776 xbee().printf("\r\nNeutral sub-FSM States: \r\n");
tnhnrl 57:ec69651c8c21 1777 string string_substate;
tnhnrl 57:ec69651c8c21 1778
tnhnrl 57:ec69651c8c21 1779 for (int i = 0; i < _substate_array_counter; i++) {
tnhnrl 57:ec69651c8c21 1780 if (_substate_array[i] == NEUTRAL_SINKING)
tnhnrl 57:ec69651c8c21 1781 string_substate = "NEUTRAL_SINKING";
tnhnrl 57:ec69651c8c21 1782 else if (_substate_array[i] == NEUTRAL_SLOWLY_RISE)
tnhnrl 57:ec69651c8c21 1783 string_substate = "NEUTRAL_SLOWLY_RISE";
tnhnrl 57:ec69651c8c21 1784 else if (_substate_array[i] == NEUTRAL_CHECK_PITCH)
tnhnrl 57:ec69651c8c21 1785 string_substate = "NEUTRAL_CHECK_PITCH";
tnhnrl 57:ec69651c8c21 1786 else if (_substate_array[i] == NEUTRAL_EXIT)
tnhnrl 57:ec69651c8c21 1787 string_substate = "NEUTRAL_EXIT <-- ";
tnhnrl 57:ec69651c8c21 1788 else if (_substate_array[i] == EMERGENCY_CLIMB)
tnhnrl 57:ec69651c8c21 1789 string_substate = " -- > EMERGENCY_CLIMB <-- ";
tnhnrl 74:d281aaef9766 1790 xbee().printf("Neutral Substate #%d: %d (%s)\r\n", i, _state_array[i], string_substate.c_str());
tnhnrl 57:ec69651c8c21 1791 }
tnhnrl 74:d281aaef9766 1792 xbee().printf("\r\n"); //make space between printouts
tnhnrl 72:250b2665755c 1793 }
tnhnrl 72:250b2665755c 1794 //BATTERY/PITCH
tnhnrl 57:ec69651c8c21 1795 else if (user_input == '[' or user_input == '{') {
tnhnrl 74:d281aaef9766 1796 xbee().printf("Please TYPE in the new BATT neutral position.\n\r");
tnhnrl 72:250b2665755c 1797 _neutral_batt_pos_mm = getFloatUserInput();
tnhnrl 57:ec69651c8c21 1798 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // decrease the batt neutral setpoint
tnhnrl 74:d281aaef9766 1799 xbee().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1800 // save neutral pitch value to config file
tnhnrl 73:f6f378311c8d 1801 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm, _pitch_filter_freq, _pitch_deadband); //P,I,D,batt zeroOffset
tnhnrl 57:ec69651c8c21 1802 }
tnhnrl 28:16c83a2fdefa 1803
tnhnrl 72:250b2665755c 1804 //BCE/DEPTH
tnhnrl 72:250b2665755c 1805 else if (user_input == ';' or user_input == ':') {
tnhnrl 74:d281aaef9766 1806 xbee().printf("Please TYPE in the new BCE neutral position.\n\r");
tnhnrl 72:250b2665755c 1807 _neutral_bce_pos_mm = getFloatUserInput();
tnhnrl 72:250b2665755c 1808 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // decrease the bce neutral setpoint
tnhnrl 74:d281aaef9766 1809 xbee().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 72:250b2665755c 1810 // save neutral depth value to config file
tnhnrl 73:f6f378311c8d 1811 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm, _depth_filter_freq, _depth_deadband);
tnhnrl 57:ec69651c8c21 1812 }
tnhnrl 57:ec69651c8c21 1813
tnhnrl 72:250b2665755c 1814 // change settings
tnhnrl 72:250b2665755c 1815 //heading is in the common controls
tnhnrl 72:250b2665755c 1816 else if (user_input == 'Q') {
tnhnrl 74:d281aaef9766 1817 xbee().printf(">> Please enter the desired PITCH (deg).\r\n");
tnhnrl 72:250b2665755c 1818 _pitch_command = getFloatUserInput();
tnhnrl 57:ec69651c8c21 1819 }
tnhnrl 72:250b2665755c 1820 else if (user_input == 'A') {
joel_ssc 85:dd8176285b6e 1821 xbee().printf(">> Please enter the desired DEPTH (m).\r\n");
tnhnrl 72:250b2665755c 1822 _depth_command = getFloatUserInput();
tnhnrl 57:ec69651c8c21 1823 }
tnhnrl 72:250b2665755c 1824
tnhnrl 57:ec69651c8c21 1825 else if (user_input == '5') {
tnhnrl 58:94b7fd55185e 1826 keyboard_menu_RUDDER_SERVO_settings();
tnhnrl 57:ec69651c8c21 1827 }
tnhnrl 57:ec69651c8c21 1828
tnhnrl 57:ec69651c8c21 1829 else if (user_input == '6') {
tnhnrl 58:94b7fd55185e 1830 keyboard_menu_HEADING_PID_settings();
tnhnrl 58:94b7fd55185e 1831 }
tnhnrl 57:ec69651c8c21 1832
tnhnrl 58:94b7fd55185e 1833 // go to tuning sub-menu
tnhnrl 57:ec69651c8c21 1834 else if (user_input == '7') {
tnhnrl 58:94b7fd55185e 1835 keyboard_menu_MANUAL_TUNING();
tnhnrl 57:ec69651c8c21 1836 }
tnhnrl 57:ec69651c8c21 1837
tnhnrl 73:f6f378311c8d 1838 // else if (user_input == 'U') {
tnhnrl 73:f6f378311c8d 1839 // keyboard_menu_POSITION_READINGS();
tnhnrl 73:f6f378311c8d 1840 // }
tnhnrl 57:ec69651c8c21 1841
tnhnrl 57:ec69651c8c21 1842 // go to sub-menus for the PID gains (this is blocking)
tnhnrl 57:ec69651c8c21 1843 else if (user_input == '1') {
tnhnrl 57:ec69651c8c21 1844 keyboard_menu_BCE_PID_settings();
tnhnrl 57:ec69651c8c21 1845 }
tnhnrl 57:ec69651c8c21 1846 else if (user_input == '2') {
tnhnrl 57:ec69651c8c21 1847 keyboard_menu_BATT_PID_settings();
tnhnrl 57:ec69651c8c21 1848 }
tnhnrl 57:ec69651c8c21 1849 else if (user_input == '3') {
tnhnrl 57:ec69651c8c21 1850 keyboard_menu_DEPTH_PID_settings();
tnhnrl 57:ec69651c8c21 1851 }
tnhnrl 57:ec69651c8c21 1852 else if (user_input == '4') {
tnhnrl 57:ec69651c8c21 1853 keyboard_menu_PITCH_PID_settings();
tnhnrl 28:16c83a2fdefa 1854 }
tnhnrl 73:f6f378311c8d 1855
tnhnrl 57:ec69651c8c21 1856 else if (user_input == '*') {
tnhnrl 74:d281aaef9766 1857 xbee().printf("SWITCHING TO SIMPLE MENU!\r\n");
tnhnrl 74:d281aaef9766 1858 wait(1);
tnhnrl 57:ec69651c8c21 1859 _debug_menu_on = false;
tnhnrl 57:ec69651c8c21 1860 }
joel_ssc 85:dd8176285b6e 1861 } //end of debug menu matches line 1445 approx -180
tnhnrl 57:ec69651c8c21 1862 /***************************** DEBUG MENU *****************************/
tnhnrl 57:ec69651c8c21 1863
tnhnrl 57:ec69651c8c21 1864 /***************************** SIMPLE MENU *****************************/
tnhnrl 72:250b2665755c 1865 else {
tnhnrl 58:94b7fd55185e 1866 if (user_input == 'V') {
tnhnrl 58:94b7fd55185e 1867 _keyboard_state = POSITION_DIVE;
tnhnrl 57:ec69651c8c21 1868 }
tnhnrl 57:ec69651c8c21 1869 else if (user_input == 'N') {
tnhnrl 57:ec69651c8c21 1870 _keyboard_state = FIND_NEUTRAL;
tnhnrl 57:ec69651c8c21 1871 }
tnhnrl 57:ec69651c8c21 1872 else if (user_input == 'J') {
tnhnrl 57:ec69651c8c21 1873 _keyboard_state = FLOAT_LEVEL;
tnhnrl 57:ec69651c8c21 1874 }
tnhnrl 57:ec69651c8c21 1875 else if (user_input == 'B') {
tnhnrl 57:ec69651c8c21 1876 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 57:ec69651c8c21 1877 }
tnhnrl 57:ec69651c8c21 1878 else if (user_input == 'E') {
tnhnrl 57:ec69651c8c21 1879 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 57:ec69651c8c21 1880 }
tnhnrl 57:ec69651c8c21 1881
tnhnrl 57:ec69651c8c21 1882 // some debug tools below
tnhnrl 57:ec69651c8c21 1883 else if (user_input == 'P') {
tnhnrl 57:ec69651c8c21 1884 //Print current SD card log file
tnhnrl 57:ec69651c8c21 1885 //printCurrentSdLog();
tnhnrl 57:ec69651c8c21 1886 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 72:250b2665755c 1887 }
tnhnrl 57:ec69651c8c21 1888
tnhnrl 57:ec69651c8c21 1889 //POSITION DIVE COMMANDS
tnhnrl 72:250b2665755c 1890 else if (user_input == 'Q') {
tnhnrl 74:d281aaef9766 1891 xbee().printf(">> Please enter the desired BMM offset (mm).\r\n");
tnhnrl 72:250b2665755c 1892 _BMM_dive_offset = getFloatUserInput();
tnhnrl 57:ec69651c8c21 1893 }
tnhnrl 72:250b2665755c 1894 else if (user_input == 'A') {
tnhnrl 74:d281aaef9766 1895 xbee().printf(">> Please enter the desired BCE offset (mm).\r\n");
tnhnrl 72:250b2665755c 1896 _BCE_dive_offset = getFloatUserInput();
tnhnrl 57:ec69651c8c21 1897 }
tnhnrl 73:f6f378311c8d 1898
tnhnrl 73:f6f378311c8d 1899 else if (user_input == 'S') {
joel_ssc 85:dd8176285b6e 1900 xbee().printf(">> Please enter the desired DEPTH (m).\r\n");
tnhnrl 73:f6f378311c8d 1901 _depth_command = getFloatUserInput();
tnhnrl 73:f6f378311c8d 1902 }
tnhnrl 57:ec69651c8c21 1903 //POSITION DIVE COMMANDS
tnhnrl 57:ec69651c8c21 1904
tnhnrl 57:ec69651c8c21 1905 else if (user_input == '*') {
tnhnrl 74:d281aaef9766 1906 xbee().printf("SWITCHING TO DEBUG MENU!\r\n");
tnhnrl 57:ec69651c8c21 1907 _debug_menu_on = true;
tnhnrl 74:d281aaef9766 1908 wait(1);
tnhnrl 57:ec69651c8c21 1909 }
tnhnrl 16:3363b9f14913 1910 }
joel_ssc 85:dd8176285b6e 1911 /***************************** END SIMPLE MENU *****************************/
tnhnrl 17:7c16b5671d0e 1912
tnhnrl 17:7c16b5671d0e 1913 //when you read the keyboard successfully, change the state
tnhnrl 28:16c83a2fdefa 1914 _state = _keyboard_state; //set state at the end of this function
tnhnrl 74:d281aaef9766 1915 //xbee().printf("\r\n\n ********* KEYBOARD STATE: %d *********\r\n\n", _state);
joel_ssc 85:dd8176285b6e 1916 } //matches very top of function - first if()
tnhnrl 16:3363b9f14913 1917 }
tnhnrl 52:f207567d3ea4 1918
tnhnrl 52:f207567d3ea4 1919 void StateMachine::keyboard_menu_STREAM_STATUS() {
tnhnrl 52:f207567d3ea4 1920 char STATUS_key;
tnhnrl 52:f207567d3ea4 1921
tnhnrl 52:f207567d3ea4 1922 // show the menu
tnhnrl 74:d281aaef9766 1923 xbee().printf("\r\n8: STATUS DEBUG MENU (EXIT WITH 'X' !)\r\n");
tnhnrl 52:f207567d3ea4 1924
tnhnrl 52:f207567d3ea4 1925 while (1) {
tnhnrl 74:d281aaef9766 1926 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 1927 STATUS_key = xbee().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1928 }
tnhnrl 52:f207567d3ea4 1929
tnhnrl 52:f207567d3ea4 1930 else {
tnhnrl 52:f207567d3ea4 1931
tnhnrl 52:f207567d3ea4 1932 wait(1);
tnhnrl 54:d4990fb68404 1933
tnhnrl 58:94b7fd55185e 1934
joel_ssc 85:dd8176285b6e 1935 // xbee().printf("BCE POS (CMD): %0.1f (%0.1f) BATT POS: %0.1f (%0.1f) PRESS_psi: %0.2f [depth_m: %0.2f], PITCH: %0.2f, HEADING: %0.2f, rudder_servo_pwm: %0.1f \n[FILT/RAW 0(%d,%d),1(%d),2(%d),6(%d),4(%d),5(%d),6(%d),7(%d)] Switch: BCE(%d) BMM(%d)\r",bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(),depth().getPsi(),depthLoop().getPosition(),imu().getPitch(),imu().getHeading(),rudder().getSetPosition_pwm(),adc().readCh0(),adc().readCh1(),adc().readCh2(),adc().readCh3(),adc().readCh4(),adc().readCh5(),adc().readCh6(),adc().readCh7(), bce().getHardwareSwitchStatus(),batt().getHardwareSwitchStatus());
joel_ssc 85:dd8176285b6e 1936 xbee().printf("BCE POS: %0.1f (cmd %0.1f) BATT POS: %0.1f (cmd %0.1f) PRESS_psi: %0.2f [depth_m: %0.2f], PITCH: %0.2f, HEADING: %0.2f, rdr_pwm: %0.1f [FILT/RAW 0(%d,%d),1(%d,%d),2(%d,%d),3(%d,%d),4(%d,%d),5(%d,%d),6(%d,%d),7(%d,%d)] Switch: BCE(%d) BMM(%d)\r",
joel_ssc 85:dd8176285b6e 1937 bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(),depth().getPsi(),
joel_ssc 85:dd8176285b6e 1938 depthLoop().getPosition(),imu().getPitch(),imu().getHeading(),rudder().getSetPosition_pwm(),adc().readCh0(),adc().readRawCh0(),
joel_ssc 85:dd8176285b6e 1939 adc().readCh1(),adc().readRawCh1(),adc().readCh2(),adc().readRawCh2(),adc().readCh3(),adc().readRawCh3(),adc().readCh4(),
joel_ssc 85:dd8176285b6e 1940 adc().readRawCh4(),adc().readCh5(),adc().readRawCh5(),adc().readCh6(),adc().readRawCh6(),adc().readCh7(),adc().readRawCh7(),
joel_ssc 85:dd8176285b6e 1941 bce().getHardwareSwitchStatus(),batt().getHardwareSwitchStatus());
tnhnrl 52:f207567d3ea4 1942
tnhnrl 52:f207567d3ea4 1943 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1944 }
tnhnrl 52:f207567d3ea4 1945
tnhnrl 52:f207567d3ea4 1946 // process the keys
tnhnrl 58:94b7fd55185e 1947 if (STATUS_key == 'X') {
tnhnrl 74:d281aaef9766 1948 xbee().printf("\r\nX: EXITING STATUS DEBUG MENU\r\n");
tnhnrl 52:f207567d3ea4 1949 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1950 }
tnhnrl 52:f207567d3ea4 1951
tnhnrl 52:f207567d3ea4 1952 else {
tnhnrl 74:d281aaef9766 1953 xbee().printf("\r\nThis key (%c) does nothing here. ", STATUS_key);
tnhnrl 52:f207567d3ea4 1954 }
tnhnrl 52:f207567d3ea4 1955 }
tnhnrl 52:f207567d3ea4 1956 }
tnhnrl 52:f207567d3ea4 1957
tnhnrl 52:f207567d3ea4 1958 void StateMachine::keyboard_menu_RUDDER_SERVO_settings() {
tnhnrl 52:f207567d3ea4 1959 //load current parameters from the rudder
tnhnrl 52:f207567d3ea4 1960 float rudder_min_pwm = rudder().getMinPWM();
tnhnrl 52:f207567d3ea4 1961 float rudder_max_pwm = rudder().getMaxPWM();
tnhnrl 52:f207567d3ea4 1962 float rudder_ctr_pwm = rudder().getCenterPWM();
tnhnrl 52:f207567d3ea4 1963 float rudder_min_deg = rudder().getMinDeg();
tnhnrl 52:f207567d3ea4 1964 float rudder_max_deg = rudder().getMaxDeg();
tnhnrl 52:f207567d3ea4 1965
tnhnrl 52:f207567d3ea4 1966 char RUDDER_PID_key;
tnhnrl 52:f207567d3ea4 1967
tnhnrl 52:f207567d3ea4 1968 // print the menu
tnhnrl 74:d281aaef9766 1969 xbee().printf("\r\nRUDDER (servo driver) settings (MENU)");
tnhnrl 74:d281aaef9766 1970 xbee().printf("\r\nAdjust min_pwm/max_pwm/center_pwm/min_deg/max_deg settings with the following keys: N, M, C, K, L");
tnhnrl 74:d281aaef9766 1971 xbee().printf("\r\nHit shift + X to exit w/o saving. Hit shift + S to save.\r\n");
tnhnrl 74:d281aaef9766 1972 xbee().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 1973
tnhnrl 52:f207567d3ea4 1974 // handle the key presses
tnhnrl 52:f207567d3ea4 1975 while(1) {
tnhnrl 52:f207567d3ea4 1976 // get the user's keystroke from either of the two inputs
tnhnrl 74:d281aaef9766 1977 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 1978 RUDDER_PID_key = xbee().getc();
tnhnrl 52:f207567d3ea4 1979 }
tnhnrl 52:f207567d3ea4 1980 else {
tnhnrl 52:f207567d3ea4 1981 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1982 }
tnhnrl 52:f207567d3ea4 1983
tnhnrl 52:f207567d3ea4 1984 // handle the user's key input
tnhnrl 52:f207567d3ea4 1985 if (RUDDER_PID_key == 'S') { // user wants to save the modified values
tnhnrl 52:f207567d3ea4 1986 // set global values
tnhnrl 52:f207567d3ea4 1987 rudder().setMinPWM(rudder_min_pwm);
tnhnrl 52:f207567d3ea4 1988 rudder().setMaxPWM(rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1989 rudder().setCenterPWM(rudder_ctr_pwm);
tnhnrl 52:f207567d3ea4 1990 rudder().setMinDeg(rudder_min_deg);
tnhnrl 52:f207567d3ea4 1991 rudder().setMaxDeg(rudder_max_deg);
tnhnrl 52:f207567d3ea4 1992
tnhnrl 52:f207567d3ea4 1993 // save rudder servo driver values for inner loop
tnhnrl 52:f207567d3ea4 1994 configFileIO().saveRudderData(rudder_min_deg, rudder_max_deg, rudder_ctr_pwm, rudder_min_pwm, rudder_max_pwm);
tnhnrl 74:d281aaef9766 1995 xbee().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 74:d281aaef9766 1996 xbee().printf("Adjust min_pwm/max_pwm/center_pwm/min_deg/max_deg settings with the following keys: N, M, C, K, L\r\n");
tnhnrl 52:f207567d3ea4 1997 }
tnhnrl 52:f207567d3ea4 1998 else if (RUDDER_PID_key == 'X') {
tnhnrl 52:f207567d3ea4 1999 break; //exit the while loop
tnhnrl 52:f207567d3ea4 2000 }
tnhnrl 52:f207567d3ea4 2001 // MIN PWM
tnhnrl 52:f207567d3ea4 2002 else if (RUDDER_PID_key == 'N') {
tnhnrl 74:d281aaef9766 2003 xbee().printf(">> Type in rudder_min_pwm with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 2004 rudder_min_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 2005 }
tnhnrl 52:f207567d3ea4 2006 // MAX PWM
tnhnrl 52:f207567d3ea4 2007 else if (RUDDER_PID_key == 'M') {
tnhnrl 74:d281aaef9766 2008 xbee().printf(">> Type in rudder_max_pwm with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 2009 rudder_max_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 2010 }
tnhnrl 52:f207567d3ea4 2011 // CENTER PWM
tnhnrl 52:f207567d3ea4 2012 else if (RUDDER_PID_key == 'C') {
tnhnrl 74:d281aaef9766 2013 xbee().printf(">> Type in rudder_ctr_pwm with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 2014 rudder_ctr_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 2015 }
tnhnrl 52:f207567d3ea4 2016 // MIN DEG
tnhnrl 52:f207567d3ea4 2017 else if (RUDDER_PID_key == 'K') {
tnhnrl 74:d281aaef9766 2018 xbee().printf(">> Type in rudder_min_deg with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 2019 rudder_min_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 2020 }
tnhnrl 52:f207567d3ea4 2021 // MAX DEG
tnhnrl 52:f207567d3ea4 2022 else if (RUDDER_PID_key == 'L') {
tnhnrl 74:d281aaef9766 2023 xbee().printf(">> Type in rudder_max_deg with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 2024 rudder_max_deg = getFloatUserInput();
tnhnrl 74:d281aaef9766 2025 }
tnhnrl 52:f207567d3ea4 2026 else {
tnhnrl 74:d281aaef9766 2027 xbee().printf("RUDDER SETUP: [%c] This key does nothing here. \r", RUDDER_PID_key);
tnhnrl 52:f207567d3ea4 2028 }
tnhnrl 52:f207567d3ea4 2029 }
tnhnrl 52:f207567d3ea4 2030 }
tnhnrl 54:d4990fb68404 2031
tnhnrl 54:d4990fb68404 2032 void StateMachine::keyboard_menu_COUNTS_STATUS() {
tnhnrl 73:f6f378311c8d 2033 // DELETE REMOVE
tnhnrl 54:d4990fb68404 2034 }
tnhnrl 56:48a8a5a65b82 2035
tnhnrl 49:47ffa4feb6db 2036 void StateMachine::keyboard_menu_MANUAL_TUNING() {
tnhnrl 49:47ffa4feb6db 2037 char TUNING_key;
tnhnrl 49:47ffa4feb6db 2038
tnhnrl 56:48a8a5a65b82 2039 //made these into internal parameters
tnhnrl 74:d281aaef9766 2040 float _tuning_bce_pos_mm = 200.0; //safe starting position
tnhnrl 74:d281aaef9766 2041 float _tuning_batt_pos_mm = 40.0; //safe starting position
tnhnrl 56:48a8a5a65b82 2042 float _tuning_rudder_pos_deg = 0.0; //safe starting position
tnhnrl 74:d281aaef9766 2043 float _tuning_rudder_pwm = 1640.0;
tnhnrl 56:48a8a5a65b82 2044
tnhnrl 56:48a8a5a65b82 2045 //immediately start at those positions
tnhnrl 56:48a8a5a65b82 2046 bce().setPosition_mm(_tuning_bce_pos_mm);
tnhnrl 56:48a8a5a65b82 2047 batt().setPosition_mm(_tuning_batt_pos_mm);
tnhnrl 56:48a8a5a65b82 2048 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 49:47ffa4feb6db 2049
tnhnrl 74:d281aaef9766 2050 // show the menu
tnhnrl 74:d281aaef9766 2051 xbee().printf("\r\n7: MANUAL TUNING MENU (EXIT WITH 'X' !) (Pause and Unpause rudder ticker with P and U\n");
tnhnrl 74:d281aaef9766 2052 xbee().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");
joel_ssc 85:dd8176285b6e 2053 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 74:d281aaef9766 2054
tnhnrl 49:47ffa4feb6db 2055 // what needs to be started?
tnhnrl 49:47ffa4feb6db 2056 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 2057 batt().unpause(); //this is now active
tnhnrl 56:48a8a5a65b82 2058 rudder().unpause();
tnhnrl 49:47ffa4feb6db 2059
tnhnrl 69:919ac8d7e023 2060 while (1) {
tnhnrl 69:919ac8d7e023 2061 wait(0.1);
tnhnrl 69:919ac8d7e023 2062
tnhnrl 74:d281aaef9766 2063 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 2064 TUNING_key = xbee().getc(); //get each keystroke
tnhnrl 49:47ffa4feb6db 2065 }
tnhnrl 49:47ffa4feb6db 2066
tnhnrl 58:94b7fd55185e 2067 else {
tnhnrl 74:d281aaef9766 2068 //xbee().printf("MT: POS (CMD) BCE %3.1f mm (%3.1f mm), BATT %3.1f mm (%3.1f mm) SERVO: %0.1f deg, (%0.1f pwm) PITCH: %0.1f,HEADING: %0.1f \r", bce().getPosition_mm(), bce().getSetPosition_mm(), batt().getPosition_mm(), batt().getSetPosition_mm(), rudder().getSetPosition_deg(), rudder().getSetPosition_pwm(),imu().getPitch(),imu().getHeading());
tnhnrl 49:47ffa4feb6db 2069 continue; // didn't get a user input, so keep waiting for it
tnhnrl 49:47ffa4feb6db 2070 }
tnhnrl 49:47ffa4feb6db 2071
tnhnrl 49:47ffa4feb6db 2072 // process the keys
tnhnrl 49:47ffa4feb6db 2073 if (TUNING_key == 'X') {
tnhnrl 49:47ffa4feb6db 2074 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 49:47ffa4feb6db 2075 bce().pause();
tnhnrl 49:47ffa4feb6db 2076 batt().pause();
tnhnrl 56:48a8a5a65b82 2077 rudder().pause();
tnhnrl 56:48a8a5a65b82 2078
tnhnrl 56:48a8a5a65b82 2079 //right now the rudder is always active................................................hmm
tnhnrl 56:48a8a5a65b82 2080 //deactivate the pin? new/delete?
tnhnrl 49:47ffa4feb6db 2081
tnhnrl 49:47ffa4feb6db 2082 break; //exit the while loop
tnhnrl 49:47ffa4feb6db 2083 }
tnhnrl 56:48a8a5a65b82 2084
tnhnrl 56:48a8a5a65b82 2085 //Buoyancy Engine
tnhnrl 74:d281aaef9766 2086 else if (TUNING_key == 'A' or TUNING_key == 'a') {
tnhnrl 56:48a8a5a65b82 2087 _tuning_bce_pos_mm = _tuning_bce_pos_mm - 1.0;
tnhnrl 56:48a8a5a65b82 2088 bce().setPosition_mm(_tuning_bce_pos_mm); //this variable is loaded from the file at initialization
joel_ssc 85:dd8176285b6e 2089 xbee().printf("\r\nMANUAL_TUNING: (BCE CHANGE: %0.1f)\r\n BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f m, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
joel_ssc 85:dd8176285b6e 2090 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 52:f207567d3ea4 2091 }
tnhnrl 52:f207567d3ea4 2092
tnhnrl 74:d281aaef9766 2093 else if (TUNING_key == 'S' or TUNING_key == 's') {
tnhnrl 56:48a8a5a65b82 2094 _tuning_bce_pos_mm = _tuning_bce_pos_mm + 1.0;
tnhnrl 56:48a8a5a65b82 2095 bce().setPosition_mm(_tuning_bce_pos_mm); //this variable is loaded from the file at initialization
joel_ssc 85:dd8176285b6e 2096 xbee().printf("\r\nMANUAL_TUNING: (BCE CHANGE: %0.1f)\r\n BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f m, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
joel_ssc 85:dd8176285b6e 2097 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 49:47ffa4feb6db 2098 }
tnhnrl 49:47ffa4feb6db 2099
tnhnrl 56:48a8a5a65b82 2100 //BATTERY
tnhnrl 74:d281aaef9766 2101 else if (TUNING_key == 'Q' or TUNING_key == 'q') {
tnhnrl 56:48a8a5a65b82 2102 _tuning_batt_pos_mm = _tuning_batt_pos_mm - 1.0;
tnhnrl 56:48a8a5a65b82 2103 batt().setPosition_mm(_tuning_batt_pos_mm); //this variable is loaded from the file at initialization
joel_ssc 85:dd8176285b6e 2104 xbee().printf("\r\nMANUAL_TUNING: (BATT CHANGE: %0.1f)\r\n BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f m, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
joel_ssc 85:dd8176285b6e 2105 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 49:47ffa4feb6db 2106 }
tnhnrl 49:47ffa4feb6db 2107
tnhnrl 74:d281aaef9766 2108 else if (TUNING_key == 'W' or TUNING_key == 'w') {
tnhnrl 56:48a8a5a65b82 2109 _tuning_batt_pos_mm = _tuning_batt_pos_mm + 1.0;
tnhnrl 56:48a8a5a65b82 2110 batt().setPosition_mm(_tuning_batt_pos_mm); //this variable is loaded from the file at initialization
joel_ssc 85:dd8176285b6e 2111 xbee().printf("\r\nMANUAL_TUNING: (BATT CHANGE: %0.1f)\r\n BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f m, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
joel_ssc 85:dd8176285b6e 2112 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 56:48a8a5a65b82 2113 }
tnhnrl 56:48a8a5a65b82 2114
tnhnrl 56:48a8a5a65b82 2115 else if (TUNING_key == 'c' or TUNING_key == 'C') {
joel_ssc 85:dd8176285b6e 2116 xbee().printf("\r\nMT: (CURRENT POSITIONS) BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 56:48a8a5a65b82 2117 }
tnhnrl 56:48a8a5a65b82 2118
tnhnrl 56:48a8a5a65b82 2119 //RUDER
tnhnrl 74:d281aaef9766 2120 else if (TUNING_key == 'R' or TUNING_key == 'r') {
tnhnrl 56:48a8a5a65b82 2121 _tuning_rudder_pos_deg = _tuning_rudder_pos_deg - 0.5;
tnhnrl 56:48a8a5a65b82 2122 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 74:d281aaef9766 2123 xbee().printf("MT: 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().getSetPosition_pwm(), rudder().getSetPosition_deg(), headingLoop().getPosition(), imu().getHeading());
joel_ssc 85:dd8176285b6e 2124 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 56:48a8a5a65b82 2125 }
tnhnrl 56:48a8a5a65b82 2126
tnhnrl 74:d281aaef9766 2127 else if (TUNING_key == 'E' or TUNING_key == 'e') {
tnhnrl 56:48a8a5a65b82 2128 _tuning_rudder_pos_deg = _tuning_rudder_pos_deg + 0.5;
tnhnrl 56:48a8a5a65b82 2129 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 74:d281aaef9766 2130 xbee().printf("MT: 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().getSetPosition_pwm(), rudder().getSetPosition_deg(), headingLoop().getPosition(), imu().getHeading());
joel_ssc 85:dd8176285b6e 2131 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 56:48a8a5a65b82 2132 }
tnhnrl 56:48a8a5a65b82 2133
tnhnrl 74:d281aaef9766 2134 else if (TUNING_key == '-' or TUNING_key == '_') {
tnhnrl 74:d281aaef9766 2135 _tuning_rudder_pwm -= 10.0;
tnhnrl 74:d281aaef9766 2136 rudder().setPWM(_tuning_rudder_pwm);
tnhnrl 74:d281aaef9766 2137 xbee().printf("MT: (-) RUDDER CHANGE %0.1f pwm\n\r", rudder().getSetPosition_pwm());
joel_ssc 85:dd8176285b6e 2138 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 56:48a8a5a65b82 2139 }
tnhnrl 74:d281aaef9766 2140
tnhnrl 74:d281aaef9766 2141 else if (TUNING_key == '=' or TUNING_key == '+') {
tnhnrl 74:d281aaef9766 2142 _tuning_rudder_pwm += 10.0;
tnhnrl 74:d281aaef9766 2143 rudder().setPWM(_tuning_rudder_pwm);
tnhnrl 74:d281aaef9766 2144 xbee().printf("MT: (+) RUDDER CHANGE %0.1f pwm\n\r", rudder().getSetPosition_pwm());
joel_ssc 85:dd8176285b6e 2145 xbee().printf("\r\nMT: BCE pos:%5.1f (cmd:%5.1f), BMM:%5.1f (cmd:%5.1f), RUDDER:%6.1f (%6.1f deg) (depth: %0.1f m, pitch: %0.1f deg, headingLoop heading: %0.1f deg, IMU heading: %0.1f deg) \n\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm(),rudder().getSetPosition_pwm(),rudder().getSetPosition_deg(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(),imu().getHeading());
tnhnrl 49:47ffa4feb6db 2146 }
tnhnrl 49:47ffa4feb6db 2147
tnhnrl 49:47ffa4feb6db 2148 else {
tnhnrl 74:d281aaef9766 2149 xbee().printf("\r\nMANUAL_TUNING: [%c] This key does nothing here. \r", TUNING_key);
tnhnrl 56:48a8a5a65b82 2150 }
tnhnrl 49:47ffa4feb6db 2151 }
tnhnrl 49:47ffa4feb6db 2152 }
tnhnrl 52:f207567d3ea4 2153
tnhnrl 52:f207567d3ea4 2154 void StateMachine::keyboard_menu_CHANNEL_READINGS() {
tnhnrl 52:f207567d3ea4 2155 char TUNING_key;
tnhnrl 52:f207567d3ea4 2156
tnhnrl 52:f207567d3ea4 2157 // show the menu
tnhnrl 74:d281aaef9766 2158 xbee().printf("\r\n8: CHANNEL READINGS (EXIT WITH 'X' !)");
tnhnrl 55:f4ec445c42fe 2159
tnhnrl 55:f4ec445c42fe 2160 while (1) {
tnhnrl 74:d281aaef9766 2161 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 2162 TUNING_key = xbee().getc(); //get each keystroke
tnhnrl 55:f4ec445c42fe 2163 }
tnhnrl 55:f4ec445c42fe 2164
tnhnrl 55:f4ec445c42fe 2165 // process the keys
tnhnrl 55:f4ec445c42fe 2166 if (TUNING_key == 'X') {
tnhnrl 55:f4ec445c42fe 2167 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 55:f4ec445c42fe 2168 bce().pause();
tnhnrl 55:f4ec445c42fe 2169 batt().pause();
tnhnrl 55:f4ec445c42fe 2170
tnhnrl 55:f4ec445c42fe 2171 break; //exit the while loop
tnhnrl 55:f4ec445c42fe 2172 }
tnhnrl 55:f4ec445c42fe 2173
tnhnrl 55:f4ec445c42fe 2174 else {
tnhnrl 55:f4ec445c42fe 2175 wait(0.5);
tnhnrl 74:d281aaef9766 2176 xbee().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 2177 continue; // didn't get a user input, so keep waiting for it
tnhnrl 55:f4ec445c42fe 2178 }
tnhnrl 55:f4ec445c42fe 2179 }
tnhnrl 55:f4ec445c42fe 2180 }
tnhnrl 49:47ffa4feb6db 2181
tnhnrl 16:3363b9f14913 2182 void StateMachine::keyboard_menu_BCE_PID_settings() {
tnhnrl 73:f6f378311c8d 2183 char BCE_PID_key;
tnhnrl 63:6cb0405fc6e6 2184
tnhnrl 76:c802e1da4179 2185 // load current values from files
tnhnrl 76:c802e1da4179 2186 float bce_KP = bce().getControllerP();
tnhnrl 76:c802e1da4179 2187 float bce_KI = bce().getControllerI();
tnhnrl 76:c802e1da4179 2188 float bce_KD = bce().getControllerD();
tnhnrl 76:c802e1da4179 2189
tnhnrl 76:c802e1da4179 2190 float bce_deadband = bce().getDeadband();
tnhnrl 76:c802e1da4179 2191 float bce_frequency = bce().getFilterFrequency();
tnhnrl 76:c802e1da4179 2192 int bce_zero_offset = bce().getZeroCounts();
tnhnrl 76:c802e1da4179 2193 //BCE frequency and deadband are hardcoded!
tnhnrl 16:3363b9f14913 2194
tnhnrl 16:3363b9f14913 2195 // show the menu
tnhnrl 76:c802e1da4179 2196 xbee().printf("\n\rBuoyancy Engine PID gain settings (MENU). ADJUST WITH CARE!");
tnhnrl 76:c802e1da4179 2197 xbee().printf("\n\rAdjust PID settings with the following keys: P I D. Filter = F, deadband = B, zero offset = Z\n\r");
tnhnrl 74:d281aaef9766 2198 xbee().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 76:c802e1da4179 2199 xbee().printf("bce P:%6.2f, I:%6.2f, D:%6.2f, zero offset: %3i, limit %6.1f mm, slope %0.5f, filter_freq: %0.1f, deadband: %0.1f\r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope(), bce().getFilterFrequency(), bce().getDeadband());
tnhnrl 63:6cb0405fc6e6 2200
tnhnrl 63:6cb0405fc6e6 2201 // handle the key presses
tnhnrl 63:6cb0405fc6e6 2202 while(1) {
tnhnrl 63:6cb0405fc6e6 2203 // get the user's keystroke from either of the two inputs
tnhnrl 74:d281aaef9766 2204 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 2205 BCE_PID_key = xbee().getc();
tnhnrl 63:6cb0405fc6e6 2206 }
tnhnrl 63:6cb0405fc6e6 2207 else {
tnhnrl 63:6cb0405fc6e6 2208 continue; // didn't get a user input, so keep waiting for it
tnhnrl 63:6cb0405fc6e6 2209 }
tnhnrl 63:6cb0405fc6e6 2210
tnhnrl 63:6cb0405fc6e6 2211 // handle the user's key input
tnhnrl 73:f6f378311c8d 2212 if (BCE_PID_key == 'S') { // user wants to save these modified values
tnhnrl 63:6cb0405fc6e6 2213 // set values
tnhnrl 63:6cb0405fc6e6 2214 bce().setControllerP(bce_KP);
tnhnrl 63:6cb0405fc6e6 2215 bce().setControllerI(bce_KI);
tnhnrl 76:c802e1da4179 2216 bce().setControllerD(bce_KD);
tnhnrl 76:c802e1da4179 2217
tnhnrl 76:c802e1da4179 2218 bce().setDeadband(bce_deadband);
tnhnrl 76:c802e1da4179 2219 bce().setFilterFrequency(bce_frequency);
tnhnrl 76:c802e1da4179 2220 bce().setZeroCounts(bce_zero_offset); //integer value
tnhnrl 63:6cb0405fc6e6 2221
tnhnrl 73:f6f378311c8d 2222 // save to "BCE.TXT" file
tnhnrl 76:c802e1da4179 2223 //saveBattData(float batt_p_gain, float batt_i_gain, float batt_d_gain, int batt_zeroOffset, float batt_filter_freq, float batt_deadband)
tnhnrl 76:c802e1da4179 2224 configFileIO().saveBCEData(bce_KP, bce_KI, bce_KD, bce_zero_offset, bce_frequency, bce_deadband);
tnhnrl 76:c802e1da4179 2225 xbee().printf("bce P:%6.2f, I:%6.2f, D:%6.2f, zero offset: %3i, limit %6.1f mm, slope %0.5f, filter_freq: %0.1f, deadband: %0.1f\r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope(), bce().getFilterFrequency(), bce().getDeadband());
tnhnrl 63:6cb0405fc6e6 2226 }
tnhnrl 73:f6f378311c8d 2227 else if (BCE_PID_key == 'X') {
tnhnrl 63:6cb0405fc6e6 2228 break; //exit the while loop
tnhnrl 63:6cb0405fc6e6 2229 }
tnhnrl 73:f6f378311c8d 2230 else if (BCE_PID_key == 'P') {
tnhnrl 74:d281aaef9766 2231 xbee().printf(">> Type in proportional gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2232 bce_KP = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2233 }
tnhnrl 73:f6f378311c8d 2234 else if (BCE_PID_key == 'I') {
tnhnrl 74:d281aaef9766 2235 xbee().printf(">> Type in integral gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2236 bce_KI = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2237 }
tnhnrl 73:f6f378311c8d 2238 else if (BCE_PID_key == 'D') {
tnhnrl 74:d281aaef9766 2239 xbee().printf(">> Type in derivative gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2240 bce_KD = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2241 }
tnhnrl 73:f6f378311c8d 2242 else if (BCE_PID_key == 'F') {
tnhnrl 74:d281aaef9766 2243 xbee().printf(">> Type in FILTER FREQUENCY with keyboard.\n\r");
tnhnrl 76:c802e1da4179 2244 bce_frequency = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2245 }
tnhnrl 73:f6f378311c8d 2246 else if (BCE_PID_key == 'B') {
tnhnrl 74:d281aaef9766 2247 xbee().printf(">> Type in DEADBAND with keyboard.\n\r");
tnhnrl 76:c802e1da4179 2248 bce_deadband = getFloatUserInput();
tnhnrl 76:c802e1da4179 2249 }
tnhnrl 76:c802e1da4179 2250 else if (BCE_PID_key == 'Z') {
tnhnrl 76:c802e1da4179 2251 xbee().printf(">> Type in zero count offset with keyboard.\n\r");
tnhnrl 76:c802e1da4179 2252 bce_zero_offset = (int)getFloatUserInput();
tnhnrl 73:f6f378311c8d 2253 }
tnhnrl 63:6cb0405fc6e6 2254 else {
tnhnrl 74:d281aaef9766 2255 xbee().printf("\n\rBCE: [%c] This key does nothing here. \r", BCE_PID_key);
tnhnrl 63:6cb0405fc6e6 2256 }
tnhnrl 63:6cb0405fc6e6 2257 }
tnhnrl 63:6cb0405fc6e6 2258 }
tnhnrl 63:6cb0405fc6e6 2259
tnhnrl 63:6cb0405fc6e6 2260 void StateMachine::keyboard_menu_BATT_PID_settings() {
tnhnrl 73:f6f378311c8d 2261 char BMM_PID_key;
tnhnrl 76:c802e1da4179 2262
tnhnrl 76:c802e1da4179 2263 // load current values from files
tnhnrl 76:c802e1da4179 2264 float batt_KP = batt().getControllerP();
tnhnrl 76:c802e1da4179 2265 float batt_KI = batt().getControllerI();
tnhnrl 76:c802e1da4179 2266 float batt_KD = batt().getControllerD();
tnhnrl 63:6cb0405fc6e6 2267
tnhnrl 76:c802e1da4179 2268 float batt_deadband = batt().getDeadband();
tnhnrl 76:c802e1da4179 2269 float batt_frequency = batt().getFilterFrequency();
tnhnrl 76:c802e1da4179 2270 int batt_zero_offset = batt().getZeroCounts();
tnhnrl 76:c802e1da4179 2271 //BATT frequency and deadband are hardcoded!
tnhnrl 63:6cb0405fc6e6 2272
tnhnrl 63:6cb0405fc6e6 2273 // print the menu
tnhnrl 74:d281aaef9766 2274 xbee().printf("\n\rBattery Motor PID gain settings (MENU)");
tnhnrl 74:d281aaef9766 2275 xbee().printf("\n\rAdjust PID settings with the following keys: P I D. Filter = F, deadband = B.\n\r");
tnhnrl 74:d281aaef9766 2276 xbee().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 76:c802e1da4179 2277 xbee().printf("batt P:%6.2f, I:%6.2f, D:%6.2f, zero offset: %3i, limit %6.1f mm, slope %0.5f, filter_freq: %0.1f, deadband: %0.1f\r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope(), batt().getFilterFrequency(), batt().getDeadband());
tnhnrl 76:c802e1da4179 2278
tnhnrl 16:3363b9f14913 2279 // handle the key presses
tnhnrl 16:3363b9f14913 2280 while(1) {
tnhnrl 16:3363b9f14913 2281 // get the user's keystroke from either of the two inputs
tnhnrl 74:d281aaef9766 2282 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 2283 BMM_PID_key = xbee().getc();
tnhnrl 16:3363b9f14913 2284 }
tnhnrl 16:3363b9f14913 2285 else {
tnhnrl 16:3363b9f14913 2286 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 2287 }
tnhnrl 16:3363b9f14913 2288
tnhnrl 16:3363b9f14913 2289 // handle the user's key input
tnhnrl 73:f6f378311c8d 2290 if (BMM_PID_key == 'S') { // user wants to save these modified values
tnhnrl 16:3363b9f14913 2291 // set values
tnhnrl 63:6cb0405fc6e6 2292 batt().setControllerP(batt_KP);
tnhnrl 63:6cb0405fc6e6 2293 batt().setControllerI(batt_KI);
tnhnrl 76:c802e1da4179 2294 batt().setControllerD(batt_KD);
tnhnrl 76:c802e1da4179 2295
tnhnrl 76:c802e1da4179 2296 batt().setDeadband(batt_deadband);
tnhnrl 76:c802e1da4179 2297 batt().setFilterFrequency(batt_frequency);
tnhnrl 76:c802e1da4179 2298 batt().setZeroCounts(batt_zero_offset); //integer value
tnhnrl 16:3363b9f14913 2299
tnhnrl 38:83d06c294807 2300 // save to "BATT.TXT" file
tnhnrl 76:c802e1da4179 2301 //saveBCEData(float bce_p_gain, float bce_i_gain, float bce_d_gain, int bce_zeroOffset, float bce_filter_freq, float bce_deadband)
tnhnrl 76:c802e1da4179 2302 configFileIO().saveBattData(batt_KP, batt_KI, batt_KD, batt_zero_offset, batt_frequency, batt_deadband);
tnhnrl 76:c802e1da4179 2303 xbee().printf("batt P:%6.2f, I:%6.2f, D:%6.2f, zero offset: %3i, limit %6.1f mm, slope %0.5f, filter_freq: %0.1f, deadband: %0.1f\r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope(), batt().getFilterFrequency(), batt().getDeadband());
tnhnrl 16:3363b9f14913 2304 }
tnhnrl 73:f6f378311c8d 2305 else if (BMM_PID_key == 'X') {
tnhnrl 16:3363b9f14913 2306 break; //exit the while loop
tnhnrl 16:3363b9f14913 2307 }
tnhnrl 73:f6f378311c8d 2308 else if (BMM_PID_key == 'P') {
tnhnrl 74:d281aaef9766 2309 xbee().printf(">> Type in proportional gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2310 batt_KP = getFloatUserInput();
tnhnrl 16:3363b9f14913 2311 }
tnhnrl 73:f6f378311c8d 2312 else if (BMM_PID_key == 'I') {
tnhnrl 74:d281aaef9766 2313 xbee().printf(">> Type in integral gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2314 batt_KI = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2315 }
tnhnrl 73:f6f378311c8d 2316 else if (BMM_PID_key == 'D') {
tnhnrl 74:d281aaef9766 2317 xbee().printf(">> Type in derivative gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2318 batt_KD = getFloatUserInput();
tnhnrl 16:3363b9f14913 2319 }
tnhnrl 73:f6f378311c8d 2320 else if (BMM_PID_key == 'F') {
tnhnrl 74:d281aaef9766 2321 xbee().printf(">> Type in FILTER FREQUENCY with keyboard.\n\r");
tnhnrl 76:c802e1da4179 2322 batt_frequency = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2323 }
tnhnrl 73:f6f378311c8d 2324 else if (BMM_PID_key == 'B') {
tnhnrl 74:d281aaef9766 2325 xbee().printf(">> Type in DEADBAND with keyboard.\n\r");
tnhnrl 76:c802e1da4179 2326 batt_deadband = getFloatUserInput();
tnhnrl 76:c802e1da4179 2327 }
tnhnrl 76:c802e1da4179 2328 else if (BMM_PID_key == 'Z') {
tnhnrl 76:c802e1da4179 2329 xbee().printf(">> Type in zero count offset with keyboard.\n\r");
tnhnrl 76:c802e1da4179 2330 batt_zero_offset = (int)getFloatUserInput();
tnhnrl 73:f6f378311c8d 2331 }
tnhnrl 16:3363b9f14913 2332 else {
tnhnrl 74:d281aaef9766 2333 xbee().printf("\n\rBATT: [%c] This key does nothing here. \r", BMM_PID_key);
tnhnrl 16:3363b9f14913 2334 }
tnhnrl 16:3363b9f14913 2335 }
tnhnrl 16:3363b9f14913 2336 }
tnhnrl 20:8987a9ae2bc7 2337
tnhnrl 16:3363b9f14913 2338 void StateMachine::keyboard_menu_DEPTH_PID_settings() {
tnhnrl 73:f6f378311c8d 2339 char DEPTH_PID_key;
tnhnrl 63:6cb0405fc6e6 2340
tnhnrl 63:6cb0405fc6e6 2341 float depth_KP = depthLoop().getControllerP(); // load current depth value
tnhnrl 63:6cb0405fc6e6 2342 float depth_KI = depthLoop().getControllerI(); // load current depth value
tnhnrl 63:6cb0405fc6e6 2343 float depth_KD = depthLoop().getControllerD(); // load current depth value
tnhnrl 76:c802e1da4179 2344
tnhnrl 73:f6f378311c8d 2345 float depth_freq = depthLoop().getFilterFrequency();
tnhnrl 73:f6f378311c8d 2346 float depth_deadband = depthLoop().getDeadband();
tnhnrl 16:3363b9f14913 2347
tnhnrl 63:6cb0405fc6e6 2348 // print the menu
tnhnrl 74:d281aaef9766 2349 xbee().printf("\n\rDEPTH (Buoyancy Engine O.L.) PID gain settings (MENU)");
tnhnrl 74:d281aaef9766 2350 xbee().printf("\n\rAdjust PID settings with the following keys: P I D. Filter = F, deadband = B.\n\r");
tnhnrl 74:d281aaef9766 2351 xbee().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 74:d281aaef9766 2352 xbee().printf("DEPTH P: %3.3f, I: %3.3f, D %3.3f, offset: %3.1f mm (filter: %0.2f, deadband: %0.2f)\r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(),depthLoop().getOutputOffset(),depthLoop().getFilterFrequency(),depthLoop().getDeadband());
tnhnrl 16:3363b9f14913 2353
tnhnrl 16:3363b9f14913 2354 // handle the key presses
tnhnrl 16:3363b9f14913 2355 while(1) {
tnhnrl 16:3363b9f14913 2356 // get the user's keystroke from either of the two inputs
tnhnrl 74:d281aaef9766 2357 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 2358 DEPTH_PID_key = xbee().getc();
tnhnrl 16:3363b9f14913 2359 }
tnhnrl 16:3363b9f14913 2360 else {
tnhnrl 16:3363b9f14913 2361 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 2362 }
tnhnrl 16:3363b9f14913 2363
tnhnrl 16:3363b9f14913 2364 // handle the user's key input
tnhnrl 73:f6f378311c8d 2365 if (DEPTH_PID_key == 'S') { // user wants to save these modified values
tnhnrl 63:6cb0405fc6e6 2366 // set values
tnhnrl 63:6cb0405fc6e6 2367 depthLoop().setControllerP(depth_KP);
tnhnrl 63:6cb0405fc6e6 2368 depthLoop().setControllerI(depth_KI);
tnhnrl 73:f6f378311c8d 2369 depthLoop().setControllerD(depth_KD);
tnhnrl 76:c802e1da4179 2370
tnhnrl 73:f6f378311c8d 2371 depthLoop().setFilterFrequency(depth_freq);
tnhnrl 73:f6f378311c8d 2372 depthLoop().setDeadband(depth_deadband);
tnhnrl 63:6cb0405fc6e6 2373
tnhnrl 63:6cb0405fc6e6 2374 // save to "DEPTH.TXT" file
tnhnrl 73:f6f378311c8d 2375 configFileIO().saveDepthData(depth_KP, depth_KI, depth_KD, _neutral_bce_pos_mm, depth_freq, depth_deadband); //P,I,D, bce zeroOffset
tnhnrl 73:f6f378311c8d 2376
tnhnrl 74:d281aaef9766 2377 xbee().printf("DEPTH P: %3.3f, I: %3.3f, D %3.3f, offset: %3.1f mm (filter: %0.2f, deadband: %0.2f)\r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(),depthLoop().getOutputOffset(),depthLoop().getFilterFrequency(),depthLoop().getDeadband());
tnhnrl 16:3363b9f14913 2378
tnhnrl 63:6cb0405fc6e6 2379 //set class variables that will be used in find neutral sequence
tnhnrl 63:6cb0405fc6e6 2380 _depth_KP = depthLoop().getControllerP(); // load current depth value
tnhnrl 63:6cb0405fc6e6 2381 _depth_KI = depthLoop().getControllerI(); // load current depth value
tnhnrl 63:6cb0405fc6e6 2382 _depth_KD = depthLoop().getControllerD(); // load current depth value
tnhnrl 16:3363b9f14913 2383 }
tnhnrl 73:f6f378311c8d 2384 else if (DEPTH_PID_key == 'X') {
tnhnrl 16:3363b9f14913 2385 break; //exit the while loop
tnhnrl 16:3363b9f14913 2386 }
tnhnrl 73:f6f378311c8d 2387 else if (DEPTH_PID_key == 'P') {
tnhnrl 74:d281aaef9766 2388 xbee().printf(">> Type in proportional gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2389 depth_KP = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2390 }
tnhnrl 73:f6f378311c8d 2391 else if (DEPTH_PID_key == 'I') {
tnhnrl 74:d281aaef9766 2392 xbee().printf(">> Type in integral gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2393 depth_KI = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2394 }
tnhnrl 73:f6f378311c8d 2395 else if (DEPTH_PID_key == 'D') {
tnhnrl 74:d281aaef9766 2396 xbee().printf(">> Type in derivative gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2397 depth_KD = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2398 }
tnhnrl 73:f6f378311c8d 2399 else if (DEPTH_PID_key == 'F') {
tnhnrl 74:d281aaef9766 2400 xbee().printf(">> Type in FILTER FREQUENCY with keyboard.\n\r");
tnhnrl 73:f6f378311c8d 2401 depth_freq = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2402 }
tnhnrl 73:f6f378311c8d 2403 else if (DEPTH_PID_key == 'B') {
tnhnrl 74:d281aaef9766 2404 xbee().printf(">> Type in DEADBAND with keyboard.\n\r");
tnhnrl 73:f6f378311c8d 2405 depth_deadband = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2406 }
tnhnrl 16:3363b9f14913 2407 else {
tnhnrl 74:d281aaef9766 2408 xbee().printf("\n\rDEPTH: [%c] This key does nothing here. \r", DEPTH_PID_key);
tnhnrl 16:3363b9f14913 2409 }
tnhnrl 16:3363b9f14913 2410 }
tnhnrl 16:3363b9f14913 2411 }
tnhnrl 16:3363b9f14913 2412
tnhnrl 16:3363b9f14913 2413 void StateMachine::keyboard_menu_PITCH_PID_settings() {
tnhnrl 73:f6f378311c8d 2414 char PITCH_PID_key;
tnhnrl 63:6cb0405fc6e6 2415
tnhnrl 63:6cb0405fc6e6 2416 float pitch_KP = pitchLoop().getControllerP(); // load current pitch value
tnhnrl 63:6cb0405fc6e6 2417 float pitch_KI = pitchLoop().getControllerI(); // load current pitch value
tnhnrl 63:6cb0405fc6e6 2418 float pitch_KD = pitchLoop().getControllerD(); // load current pitch value
tnhnrl 76:c802e1da4179 2419
tnhnrl 73:f6f378311c8d 2420 float pitch_freq = pitchLoop().getFilterFrequency();
tnhnrl 73:f6f378311c8d 2421 float pitch_deadband = pitchLoop().getDeadband();
tnhnrl 16:3363b9f14913 2422
tnhnrl 16:3363b9f14913 2423 // print the menu
tnhnrl 74:d281aaef9766 2424 xbee().printf("\n\rPITCH (Battery Motor O.L.) PID gain settings (MENU)");
tnhnrl 74:d281aaef9766 2425 xbee().printf("\n\rAdjust PID settings with the following keys: P I D. Filter = F, deadband = B.\n\r");
tnhnrl 74:d281aaef9766 2426 xbee().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 74:d281aaef9766 2427 xbee().printf("PITCH P: %3.3f, I: %3.3f, D %3.3f, offset: %3.1f mm (filter: %0.2f, deadband: %0.2f)\r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset(),pitchLoop().getFilterFrequency(),pitchLoop().getDeadband());
tnhnrl 63:6cb0405fc6e6 2428
tnhnrl 16:3363b9f14913 2429 // handle the key presses
tnhnrl 16:3363b9f14913 2430 while(1) {
tnhnrl 16:3363b9f14913 2431 // get the user's keystroke from either of the two inputs
tnhnrl 74:d281aaef9766 2432 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 2433 PITCH_PID_key = xbee().getc();
tnhnrl 16:3363b9f14913 2434 }
tnhnrl 16:3363b9f14913 2435 else {
tnhnrl 16:3363b9f14913 2436 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 2437 }
tnhnrl 63:6cb0405fc6e6 2438
tnhnrl 16:3363b9f14913 2439 // handle the user's key input
tnhnrl 73:f6f378311c8d 2440 if (PITCH_PID_key == 'S') { // user wants to save these modified values
tnhnrl 63:6cb0405fc6e6 2441 // set values
tnhnrl 63:6cb0405fc6e6 2442 pitchLoop().setControllerP(pitch_KP);
tnhnrl 63:6cb0405fc6e6 2443 pitchLoop().setControllerI(pitch_KI);
tnhnrl 73:f6f378311c8d 2444 pitchLoop().setControllerD(pitch_KD);
tnhnrl 76:c802e1da4179 2445
tnhnrl 73:f6f378311c8d 2446 pitchLoop().setFilterFrequency(pitch_freq);
tnhnrl 73:f6f378311c8d 2447 pitchLoop().setDeadband(pitch_deadband);
tnhnrl 63:6cb0405fc6e6 2448
tnhnrl 73:f6f378311c8d 2449 // save to "PITCH.TXT" file (doesn't modify neutral position)
tnhnrl 73:f6f378311c8d 2450 configFileIO().savePitchData(pitch_KP, pitch_KI, pitch_KD, _neutral_batt_pos_mm, pitch_freq, pitch_deadband);
tnhnrl 73:f6f378311c8d 2451
tnhnrl 74:d281aaef9766 2452 xbee().printf("PITCH P: %3.3f, I: %3.3f, D %3.3f, zeroOffset: %3.1f mm (filter: %0.2f, deadband: %0.2f)\r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset(),pitchLoop().getFilterFrequency(),pitchLoop().getDeadband());
tnhnrl 73:f6f378311c8d 2453
tnhnrl 63:6cb0405fc6e6 2454 _pitch_KP = pitchLoop().getControllerP(); // load current pitch value
tnhnrl 63:6cb0405fc6e6 2455 _pitch_KI = pitchLoop().getControllerI(); // load current pitch value
tnhnrl 63:6cb0405fc6e6 2456 _pitch_KD = pitchLoop().getControllerD(); // load current pitch value
tnhnrl 16:3363b9f14913 2457 }
tnhnrl 73:f6f378311c8d 2458 else if (PITCH_PID_key == 'X') {
tnhnrl 16:3363b9f14913 2459 break; //exit the while loop
tnhnrl 16:3363b9f14913 2460 }
tnhnrl 73:f6f378311c8d 2461 else if (PITCH_PID_key == 'P') {
tnhnrl 74:d281aaef9766 2462 xbee().printf(">> Type in proportional gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2463 pitch_KP = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2464 }
tnhnrl 73:f6f378311c8d 2465 else if (PITCH_PID_key == 'I') {
tnhnrl 74:d281aaef9766 2466 xbee().printf(">> Type in integral gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2467 pitch_KI = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2468 }
tnhnrl 73:f6f378311c8d 2469 else if (PITCH_PID_key == 'D') {
tnhnrl 74:d281aaef9766 2470 xbee().printf(">> Type in derivative gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2471 pitch_KD = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2472 }
tnhnrl 73:f6f378311c8d 2473 else if (PITCH_PID_key == 'F') {
tnhnrl 74:d281aaef9766 2474 xbee().printf(">> Type in FILTER FREQUENCY with keyboard.\n\r");
tnhnrl 73:f6f378311c8d 2475 pitch_freq = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2476 }
tnhnrl 73:f6f378311c8d 2477 else if (PITCH_PID_key == 'B') {
tnhnrl 74:d281aaef9766 2478 xbee().printf(">> Type in DEADBAND with keyboard.\n\r");
tnhnrl 73:f6f378311c8d 2479 pitch_deadband = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2480 }
tnhnrl 16:3363b9f14913 2481 else {
tnhnrl 74:d281aaef9766 2482 xbee().printf("\n\rPITCH: [%c] This key does nothing here. \r", PITCH_PID_key);
tnhnrl 73:f6f378311c8d 2483 }
tnhnrl 73:f6f378311c8d 2484 }
tnhnrl 73:f6f378311c8d 2485 }
tnhnrl 73:f6f378311c8d 2486
tnhnrl 73:f6f378311c8d 2487 void StateMachine::keyboard_menu_HEADING_PID_settings() {
tnhnrl 73:f6f378311c8d 2488 char HEADING_PID_key;
tnhnrl 73:f6f378311c8d 2489
tnhnrl 73:f6f378311c8d 2490 float heading_KP = headingLoop().getControllerP();
tnhnrl 73:f6f378311c8d 2491 float heading_KI = headingLoop().getControllerI();
tnhnrl 76:c802e1da4179 2492 float heading_KD = headingLoop().getControllerD();
tnhnrl 76:c802e1da4179 2493
tnhnrl 73:f6f378311c8d 2494 float heading_offset_deg = headingLoop().getOutputOffset();
tnhnrl 73:f6f378311c8d 2495 float heading_freq = headingLoop().getFilterFrequency();
tnhnrl 73:f6f378311c8d 2496 float heading_deadband = headingLoop().getDeadband();
tnhnrl 73:f6f378311c8d 2497
tnhnrl 73:f6f378311c8d 2498 // print the menu
tnhnrl 74:d281aaef9766 2499 xbee().printf("\n\rHEADING (rudder outer loop) PID gain settings (MENU)");
tnhnrl 74:d281aaef9766 2500 xbee().printf("\n\rAdjust PID settings with the following keys: P I D. Filter = F, deadband = B.\n\r");
tnhnrl 74:d281aaef9766 2501 xbee().printf("\n\r Adjust zero offset with O (oh).");
tnhnrl 74:d281aaef9766 2502 xbee().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 74:d281aaef9766 2503 xbee().printf("HEADING P: %3.3f, I: %3.3f, D %3.3f, zeroOffset: %3.1f mm (filter: %0.2f, deadband: %0.2f)\r\n\r\n", headingLoop().getControllerP(),headingLoop().getControllerI(),headingLoop().getControllerD(),headingLoop().getOutputOffset(),headingLoop().getFilterFrequency(),headingLoop().getDeadband());
tnhnrl 73:f6f378311c8d 2504
tnhnrl 73:f6f378311c8d 2505 // handle the key presses
tnhnrl 73:f6f378311c8d 2506 while(1) {
tnhnrl 73:f6f378311c8d 2507 // get the user's keystroke from either of the two inputs
tnhnrl 74:d281aaef9766 2508 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 2509 HEADING_PID_key = xbee().getc();
tnhnrl 73:f6f378311c8d 2510 }
tnhnrl 73:f6f378311c8d 2511 else {
tnhnrl 73:f6f378311c8d 2512 continue; // didn't get a user input, so keep waiting for it
tnhnrl 73:f6f378311c8d 2513 }
tnhnrl 73:f6f378311c8d 2514
tnhnrl 73:f6f378311c8d 2515 // handle the user's key input
tnhnrl 73:f6f378311c8d 2516 if (HEADING_PID_key == 'S') { // user wants to save the modified values
tnhnrl 73:f6f378311c8d 2517 // set global values
tnhnrl 73:f6f378311c8d 2518 headingLoop().setControllerP(heading_KP);
tnhnrl 73:f6f378311c8d 2519 headingLoop().setControllerI(heading_KI);
tnhnrl 73:f6f378311c8d 2520 headingLoop().setControllerD(heading_KD);
tnhnrl 73:f6f378311c8d 2521 headingLoop().setOutputOffset(heading_offset_deg);
tnhnrl 74:d281aaef9766 2522 headingLoop().setFilterFrequency(heading_freq);
tnhnrl 74:d281aaef9766 2523 headingLoop().setDeadband(heading_deadband);
tnhnrl 74:d281aaef9766 2524
tnhnrl 73:f6f378311c8d 2525 // save pitch PID values for outer loop (must save neutral position also)
tnhnrl 74:d281aaef9766 2526 configFileIO().saveHeadingData(heading_KP, heading_KI, heading_KD, heading_offset_deg, heading_freq, heading_deadband); //_neutral_heading_pos_deg);
tnhnrl 74:d281aaef9766 2527 xbee().printf("HEADING P: %3.3f, I: %3.3f, D %3.3f, zeroOffset: %3.1f mm (filter: %0.2f, deadband: %0.2f)\r\n\r\n", headingLoop().getControllerP(),headingLoop().getControllerI(),headingLoop().getControllerD(),headingLoop().getOutputOffset(),headingLoop().getFilterFrequency(),headingLoop().getDeadband());
tnhnrl 73:f6f378311c8d 2528 }
tnhnrl 73:f6f378311c8d 2529 else if (HEADING_PID_key == 'X') {
tnhnrl 73:f6f378311c8d 2530 break; //exit the while loop
tnhnrl 73:f6f378311c8d 2531 }
tnhnrl 73:f6f378311c8d 2532
tnhnrl 73:f6f378311c8d 2533 else if (HEADING_PID_key == 'P') {
tnhnrl 73:f6f378311c8d 2534 heading_KP = getFloatUserInput();;
tnhnrl 73:f6f378311c8d 2535 }
tnhnrl 73:f6f378311c8d 2536 else if (HEADING_PID_key == 'I') {
tnhnrl 73:f6f378311c8d 2537 heading_KI = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2538 }
tnhnrl 73:f6f378311c8d 2539 else if (HEADING_PID_key == 'D') {
tnhnrl 73:f6f378311c8d 2540 heading_KD = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2541 }
tnhnrl 73:f6f378311c8d 2542 else if (HEADING_PID_key == 'F') {
tnhnrl 74:d281aaef9766 2543 xbee().printf(">> Type in FILTER FREQUENCY with keyboard.\n\r");
tnhnrl 73:f6f378311c8d 2544 heading_freq = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2545 }
tnhnrl 73:f6f378311c8d 2546 else if (HEADING_PID_key == 'B') {
tnhnrl 74:d281aaef9766 2547 xbee().printf(">> Type in DEADBAND with keyboard.\n\r");
tnhnrl 73:f6f378311c8d 2548 heading_deadband = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2549 }
tnhnrl 73:f6f378311c8d 2550 else if (HEADING_PID_key == 'O') {
tnhnrl 73:f6f378311c8d 2551 heading_offset_deg = getFloatUserInput();
tnhnrl 73:f6f378311c8d 2552 }
tnhnrl 73:f6f378311c8d 2553 else {
tnhnrl 74:d281aaef9766 2554 xbee().printf("HEADING SETUP: [%c] This key does nothing here. \r", HEADING_PID_key);
tnhnrl 16:3363b9f14913 2555 }
tnhnrl 16:3363b9f14913 2556 }
tnhnrl 16:3363b9f14913 2557 }
tnhnrl 20:8987a9ae2bc7 2558
tnhnrl 16:3363b9f14913 2559 float StateMachine::getDepthCommand() {
tnhnrl 32:f2f8ae34aadc 2560 return _depth_command;
tnhnrl 16:3363b9f14913 2561 }
tnhnrl 20:8987a9ae2bc7 2562
tnhnrl 16:3363b9f14913 2563 float StateMachine::getPitchCommand() {
tnhnrl 32:f2f8ae34aadc 2564 return _pitch_command;
tnhnrl 32:f2f8ae34aadc 2565 }
tnhnrl 32:f2f8ae34aadc 2566
tnhnrl 32:f2f8ae34aadc 2567 float StateMachine::getDepthReading() {
tnhnrl 32:f2f8ae34aadc 2568 return _depth_reading;
tnhnrl 32:f2f8ae34aadc 2569 }
tnhnrl 32:f2f8ae34aadc 2570
tnhnrl 32:f2f8ae34aadc 2571 float StateMachine::getPitchReading() {
tnhnrl 32:f2f8ae34aadc 2572 return _pitch_reading;
tnhnrl 32:f2f8ae34aadc 2573 }
tnhnrl 32:f2f8ae34aadc 2574
tnhnrl 32:f2f8ae34aadc 2575 float StateMachine::getTimerReading() {
tnhnrl 32:f2f8ae34aadc 2576 return _timer_reading;
tnhnrl 17:7c16b5671d0e 2577 }
tnhnrl 28:16c83a2fdefa 2578
tnhnrl 17:7c16b5671d0e 2579 void StateMachine::setState(int input_state) {
tnhnrl 21:38c8544db6f4 2580 _state = input_state;
tnhnrl 73:f6f378311c8d 2581
tnhnrl 73:f6f378311c8d 2582 _isTimeoutRunning = false; //to start each state you have to reset this
tnhnrl 17:7c16b5671d0e 2583 }
tnhnrl 20:8987a9ae2bc7 2584
tnhnrl 17:7c16b5671d0e 2585 int StateMachine::getState() {
tnhnrl 17:7c16b5671d0e 2586 return _state; //return the current state of the system
tnhnrl 17:7c16b5671d0e 2587 }
tnhnrl 20:8987a9ae2bc7 2588
tnhnrl 17:7c16b5671d0e 2589 void StateMachine::setTimeout(float input_timeout) {
tnhnrl 17:7c16b5671d0e 2590 _timeout = input_timeout;
tnhnrl 17:7c16b5671d0e 2591 }
tnhnrl 20:8987a9ae2bc7 2592
tnhnrl 17:7c16b5671d0e 2593 void StateMachine::setDepthCommand(float input_depth_command) {
tnhnrl 32:f2f8ae34aadc 2594 _depth_command = input_depth_command;
tnhnrl 17:7c16b5671d0e 2595 }
tnhnrl 20:8987a9ae2bc7 2596
tnhnrl 17:7c16b5671d0e 2597 void StateMachine::setPitchCommand(float input_pitch_command) {
tnhnrl 32:f2f8ae34aadc 2598 _pitch_command = input_pitch_command;
tnhnrl 17:7c16b5671d0e 2599 }
tnhnrl 20:8987a9ae2bc7 2600
tnhnrl 17:7c16b5671d0e 2601 void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) {
tnhnrl 21:38c8544db6f4 2602 _neutral_batt_pos_mm = batt_pos_mm;
tnhnrl 21:38c8544db6f4 2603 _neutral_bce_pos_mm = bce_pos_mm;
tnhnrl 17:7c16b5671d0e 2604
tnhnrl 74:d281aaef9766 2605 xbee().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\r\n",_neutral_batt_pos_mm,_neutral_bce_pos_mm);
tnhnrl 17:7c16b5671d0e 2606 }
tnhnrl 20:8987a9ae2bc7 2607
tnhnrl 17:7c16b5671d0e 2608 //process one state at a time
tnhnrl 17:7c16b5671d0e 2609 void StateMachine::getDiveSequence() {
tnhnrl 17:7c16b5671d0e 2610 //iterate through this sequence using the FSM
tnhnrl 24:c7d9b5bf3829 2611 currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state;
tnhnrl 24:c7d9b5bf3829 2612 currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout;
tnhnrl 24:c7d9b5bf3829 2613 currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth;
tnhnrl 24:c7d9b5bf3829 2614 currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch;
tnhnrl 17:7c16b5671d0e 2615
tnhnrl 17:7c16b5671d0e 2616 _timeout = currentStateStruct.timeout; //set timeout before exiting this function
tnhnrl 32:f2f8ae34aadc 2617 }
joel_ssc 82:0981b9ada820 2618 //process one state at a time
joel_ssc 82:0981b9ada820 2619 void StateMachine::getLegParams() {
joel_ssc 82:0981b9ada820 2620 //iterate through this sequence using the FSM
joel_ssc 82:0981b9ada820 2621 currentLegStateStruct.state = legController().legStructLoaded[_multi_leg_counter].state;
joel_ssc 82:0981b9ada820 2622 currentLegStateStruct.timeout = legController().legStructLoaded[_multi_leg_counter].timeout;
joel_ssc 82:0981b9ada820 2623 currentLegStateStruct.yo_time = legController().legStructLoaded[_multi_leg_counter].yo_time; //timer for each up/down segment
joel_ssc 82:0981b9ada820 2624 currentLegStateStruct.min_depth = legController().legStructLoaded[_multi_leg_counter].min_depth;
joel_ssc 82:0981b9ada820 2625 currentLegStateStruct.max_depth = legController().legStructLoaded[_multi_leg_counter].max_depth;
joel_ssc 82:0981b9ada820 2626 currentLegStateStruct.heading = legController().legStructLoaded[_multi_leg_counter].heading;
joel_ssc 82:0981b9ada820 2627
joel_ssc 82:0981b9ada820 2628 _timeout = currentLegStateStruct.timeout; //set timeout before exiting this function
joel_ssc 82:0981b9ada820 2629 _yo_time = currentLegStateStruct.yo_time;
joel_ssc 82:0981b9ada820 2630 _state = currentLegStateStruct.state;
joel_ssc 85:dd8176285b6e 2631 // _multi_leg_counter += 1; // this is wrong, because this subroutine is called multiple times on the same leg, I need indicator of 'next_leg', first.
joel_ssc 82:0981b9ada820 2632 }
joel_ssc 88:1813f583cee9 2633 void StateMachine::setstate_frommain(int set_state_val, int new_timeout) { // used for setting state from main, when find_neutral has not been run
joel_ssc 88:1813f583cee9 2634 _state = set_state_val;
joel_ssc 88:1813f583cee9 2635 _timeout = new_timeout;
joel_ssc 88:1813f583cee9 2636 }
joel_ssc 88:1813f583cee9 2637
tnhnrl 32:f2f8ae34aadc 2638
tnhnrl 32:f2f8ae34aadc 2639 void StateMachine::printCurrentSdLog() {
tnhnrl 74:d281aaef9766 2640 xbee().printf("SD card log work in progress\r\n");
tnhnrl 32:f2f8ae34aadc 2641 //might be worth saving the last few logs to the MBED...
tnhnrl 32:f2f8ae34aadc 2642 }
tnhnrl 32:f2f8ae34aadc 2643
tnhnrl 52:f207567d3ea4 2644 // 06/06/2018
tnhnrl 52:f207567d3ea4 2645 float StateMachine::getFloatUserInput() {
tnhnrl 52:f207567d3ea4 2646 float float_conversion = 0.0;
tnhnrl 52:f207567d3ea4 2647
tnhnrl 52:f207567d3ea4 2648 while(1) {
tnhnrl 52:f207567d3ea4 2649 bool valid_input = false; //flag for valid or invalid input
tnhnrl 52:f207567d3ea4 2650
tnhnrl 74:d281aaef9766 2651 xbee().printf("\n\rPlease enter your number below and press ENTER:\r\n");
tnhnrl 52:f207567d3ea4 2652 char user_string [80]; //variable to store input as a character array
tnhnrl 52:f207567d3ea4 2653
tnhnrl 74:d281aaef9766 2654 xbee().scanf("%s", user_string); //read formatted data from stdin
tnhnrl 74:d281aaef9766 2655 xbee().printf("\n\n\ruser_string was <%s>\r\n", user_string);
tnhnrl 52:f207567d3ea4 2656
tnhnrl 52:f207567d3ea4 2657 //check through the string for invalid characters (decimal values 43 through 57)
tnhnrl 52:f207567d3ea4 2658 for (int c = 0; c < strlen(user_string); c++) {
tnhnrl 74:d281aaef9766 2659 //xbee().printf("character is [%c]\r\n", user_string[c]); //debug
tnhnrl 52:f207567d3ea4 2660 if (user_string[c] >= 43 and user_string[c] <= 57) {
tnhnrl 74:d281aaef9766 2661 //xbee().printf("VALID CHARACTER!\r\n"); //debug
tnhnrl 52:f207567d3ea4 2662 ;
tnhnrl 52:f207567d3ea4 2663 }
tnhnrl 52:f207567d3ea4 2664 else {
tnhnrl 74:d281aaef9766 2665 xbee().printf("INVALID INPUT!\r\n");
tnhnrl 52:f207567d3ea4 2666 break;
tnhnrl 52:f207567d3ea4 2667 }
tnhnrl 52:f207567d3ea4 2668
tnhnrl 52:f207567d3ea4 2669 if (c == (strlen(user_string) - 1)) {
tnhnrl 52:f207567d3ea4 2670 valid_input = true;
tnhnrl 52:f207567d3ea4 2671 }
tnhnrl 52:f207567d3ea4 2672 }
tnhnrl 52:f207567d3ea4 2673
tnhnrl 52:f207567d3ea4 2674 if (valid_input) {
tnhnrl 52:f207567d3ea4 2675 float_conversion = atof(user_string);
tnhnrl 74:d281aaef9766 2676 xbee().printf("VALID INPUT! Your input was: %3.3f (PRESS \"S\" (shift + S) to save!)\r\n", float_conversion);
tnhnrl 52:f207567d3ea4 2677 break;
tnhnrl 52:f207567d3ea4 2678 }
tnhnrl 52:f207567d3ea4 2679 }
tnhnrl 52:f207567d3ea4 2680
tnhnrl 52:f207567d3ea4 2681 return float_conversion;
tnhnrl 67:c86a4b464682 2682 }
tnhnrl 67:c86a4b464682 2683
tnhnrl 73:f6f378311c8d 2684 float StateMachine::getTimerValue() {
tnhnrl 73:f6f378311c8d 2685 return _fsm_timer;
tnhnrl 80:4e5d306d695b 2686 }
tnhnrl 80:4e5d306d695b 2687
tnhnrl 80:4e5d306d695b 2688 void StateMachine::logFileMenu() {
tnhnrl 80:4e5d306d695b 2689 char FILE_MENU_key;
tnhnrl 80:4e5d306d695b 2690
tnhnrl 80:4e5d306d695b 2691 // print the menu
tnhnrl 80:4e5d306d695b 2692 xbee().printf("\n\r>>> LOG FILE MENU. Y = Yes, erase file (and exit). N = No, keep file (and exit). P = Print file size.<<<\n\r");
tnhnrl 80:4e5d306d695b 2693
tnhnrl 80:4e5d306d695b 2694 // handle the key presses
tnhnrl 80:4e5d306d695b 2695 while(1) {
tnhnrl 80:4e5d306d695b 2696 // get the user's keystroke from either of the two inputs
tnhnrl 80:4e5d306d695b 2697 if (xbee().readable()) {
tnhnrl 80:4e5d306d695b 2698 xbee().printf("\n\r>>> LOG FILE MENU. Y = Yes, erase file (and exit). N = No, keep file (and exit). P = Print file size.<<<\n\r");
tnhnrl 80:4e5d306d695b 2699 FILE_MENU_key = xbee().getc();
tnhnrl 80:4e5d306d695b 2700 }
tnhnrl 80:4e5d306d695b 2701 else {
tnhnrl 80:4e5d306d695b 2702 continue; // didn't get a user input, so keep waiting for it
tnhnrl 80:4e5d306d695b 2703 }
tnhnrl 80:4e5d306d695b 2704
tnhnrl 80:4e5d306d695b 2705 // handle the user's key input
tnhnrl 80:4e5d306d695b 2706 if (FILE_MENU_key == 'P') { // user wants to save these modified values
tnhnrl 80:4e5d306d695b 2707 xbee().printf("\n\r>> Printing log file size!\n\r");
tnhnrl 80:4e5d306d695b 2708 wait(2);
tnhnrl 80:4e5d306d695b 2709 mbedLogger().getFileSize("/local/LOG000.csv");
tnhnrl 80:4e5d306d695b 2710 }
tnhnrl 80:4e5d306d695b 2711 else if (FILE_MENU_key == 'Y') {
tnhnrl 80:4e5d306d695b 2712 xbee().printf("\n\r>> Erasing MBED LOG FILE!\n\r");
tnhnrl 80:4e5d306d695b 2713 wait(2);
tnhnrl 80:4e5d306d695b 2714 mbedLogger().eraseFile();
tnhnrl 80:4e5d306d695b 2715 break;
tnhnrl 80:4e5d306d695b 2716 }
tnhnrl 80:4e5d306d695b 2717 else if (FILE_MENU_key == 'N') {
tnhnrl 80:4e5d306d695b 2718 xbee().printf("\n\r>> EXITING MENU. Log file intact.\n\r");
tnhnrl 80:4e5d306d695b 2719 wait(2);
tnhnrl 80:4e5d306d695b 2720 break;
tnhnrl 80:4e5d306d695b 2721 }
tnhnrl 80:4e5d306d695b 2722 else {
tnhnrl 80:4e5d306d695b 2723 xbee().printf("\n\r[%c] This key does nothing here. \r", FILE_MENU_key);
tnhnrl 80:4e5d306d695b 2724 }
tnhnrl 80:4e5d306d695b 2725 }
tnhnrl 16:3363b9f14913 2726 }