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

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
tnhnrl
Date:
Wed Jun 27 23:01:53 2018 +0000
Revision:
68:8f549749b8ce
Parent:
67:c86a4b464682
Child:
69:919ac8d7e023
file transmission partially repaired

Who changed what in which revision?

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