update with altimeter, swimfile.txt endleg.txt, etc see changes_13sep.txt also reset_PI()

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
joel_ssc
Date:
Fri May 17 14:25:03 2019 +0000
Revision:
95:1aac4086928a
Parent:
94:7ea429bcbbae
Child:
97:2b4f78a54227
chnage logic for find_neutral in main.cpp

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