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:
Mon Jun 25 15:44:00 2018 +0000
Revision:
67:c86a4b464682
Parent:
66:0f20870117b7
Child:
68:8f549749b8ce
fix timing

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 28:16c83a2fdefa 72 case KEYBOARD:
tnhnrl 16:3363b9f14913 73 // there actually is no timeout for SIT_IDLE, but this enables some one-shot actions
tnhnrl 28:16c83a2fdefa 74 if (!_isTimeoutRunning) {
tnhnrl 57:ec69651c8c21 75
tnhnrl 57:ec69651c8c21 76 if (_debug_menu_on)
tnhnrl 57:ec69651c8c21 77 showDebugMenu();
tnhnrl 57:ec69651c8c21 78 else
tnhnrl 57:ec69651c8c21 79 showSimpleMenu();
tnhnrl 16:3363b9f14913 80 pc().printf("\r\n\nstate: SIT_IDLE\r\n");
tnhnrl 28:16c83a2fdefa 81 _isTimeoutRunning = true;
tnhnrl 20:8987a9ae2bc7 82
tnhnrl 16:3363b9f14913 83 // what is active?
tnhnrl 16:3363b9f14913 84 bce().pause();
tnhnrl 16:3363b9f14913 85 batt().pause();
tnhnrl 32:f2f8ae34aadc 86
tnhnrl 17:7c16b5671d0e 87 //reset sub FSM
tnhnrl 28:16c83a2fdefa 88 _isSubStateTimerRunning = false;
tnhnrl 32:f2f8ae34aadc 89
tnhnrl 32:f2f8ae34aadc 90 //close the MBED file
tnhnrl 32:f2f8ae34aadc 91 _file_closed = true;
tnhnrl 16:3363b9f14913 92 }
tnhnrl 20:8987a9ae2bc7 93
tnhnrl 16:3363b9f14913 94 // how exit?
tnhnrl 20:8987a9ae2bc7 95 keyboard(); // keyboard function will change the state if needed
tnhnrl 16:3363b9f14913 96 break;
tnhnrl 49:47ffa4feb6db 97
tnhnrl 49:47ffa4feb6db 98 case CHECK_TUNING : // state used to check the tuning of the pressure vessel
tnhnrl 49:47ffa4feb6db 99 // start local state timer and init any other one-shot actions
tnhnrl 49:47ffa4feb6db 100 if (!_isTimeoutRunning) {
tnhnrl 49:47ffa4feb6db 101 pc().printf("\r\n\nstate: CHECK_TUNING\r\n");
tnhnrl 49:47ffa4feb6db 102 timer.reset(); // timer goes back to zero
tnhnrl 49:47ffa4feb6db 103 timer.start(); // background timer starts running
tnhnrl 49:47ffa4feb6db 104 _isTimeoutRunning = true;
tnhnrl 49:47ffa4feb6db 105
tnhnrl 49:47ffa4feb6db 106 // what needs to be started?
tnhnrl 49:47ffa4feb6db 107 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 108 batt().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 109
tnhnrl 49:47ffa4feb6db 110 // what are the commands? (DRIVE THE MOTORS "DIRECTLY")
tnhnrl 49:47ffa4feb6db 111 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 112 batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 113
tnhnrl 49:47ffa4feb6db 114 // getSetPosition_mm is the commanded position in the LinearActuator class
tnhnrl 49:47ffa4feb6db 115
tnhnrl 49:47ffa4feb6db 116 pc().printf("CHECK_TUNING: BCE cmd: %3.1f (BCE current position: %3.1f)\r\n", bce().getSetPosition_mm(), bce().getPosition_mm());
tnhnrl 49:47ffa4feb6db 117 pc().printf("CHECK_TUNING: BATT cmd: %3.1f (BATT current position: %3.1f)\r\n", batt().getSetPosition_mm(), bce().getPosition_mm());
tnhnrl 49:47ffa4feb6db 118
tnhnrl 49:47ffa4feb6db 119 //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 63:6cb0405fc6e6 646 //Reload the dive sequence on exit
tnhnrl 63:6cb0405fc6e6 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 63:6cb0405fc6e6 664 //Reload the dive sequence on exit
tnhnrl 63:6cb0405fc6e6 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 45:16b8162188ca 692 mbedLogger().getNumberOfPacketsInCurrentLog(); //open the file, read the number of lines in the log file
tnhnrl 45:16b8162188ca 693 transmit_packet_number = mbedLogger().getNumberOfPackets();
tnhnrl 45:16b8162188ca 694
tnhnrl 63:6cb0405fc6e6 695 //set the timeout internally (integer value)!
tnhnrl 63:6cb0405fc6e6 696 _timeout = mbedLogger().getLogSize(); //works after current log function above
tnhnrl 63:6cb0405fc6e6 697 _timeout = _timeout - (_timeout % 10) + 10; //remove the last digit and keep it as multiples of 10 (add a buffer of 10 seconds just in case it's below 10)
tnhnrl 63:6cb0405fc6e6 698
tnhnrl 63:6cb0405fc6e6 699 //STATE
tnhnrl 63:6cb0405fc6e6 700 //TRANSMIT_MBED_LOG
tnhnrl 45:16b8162188ca 701 }
tnhnrl 45:16b8162188ca 702
tnhnrl 45:16b8162188ca 703 if (timer.read() > _timeout) {
tnhnrl 63:6cb0405fc6e6 704 mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading
tnhnrl 64:a8939bc127ab 705 pc().printf("\r\nTRANSMIT_MBED_LOG: timed out!\r\n");
tnhnrl 63:6cb0405fc6e6 706
tnhnrl 63:6cb0405fc6e6 707 //STATE
tnhnrl 45:16b8162188ca 708 _state = SIT_IDLE;
tnhnrl 63:6cb0405fc6e6 709
tnhnrl 63:6cb0405fc6e6 710 timer.reset();
tnhnrl 63:6cb0405fc6e6 711 _isTimeoutRunning = false;
tnhnrl 63:6cb0405fc6e6 712
tnhnrl 63:6cb0405fc6e6 713 //send a command to the Python GUI to stop looking for data
tnhnrl 63:6cb0405fc6e6 714 //SEND COMMAND WHEN TRANSMISSION ENDS PREMATURELY
tnhnrl 63:6cb0405fc6e6 715 pc().printf("%c%c%c%c",0x15,0x15,0x15,0x15);
tnhnrl 63:6cb0405fc6e6 716 }
tnhnrl 63:6cb0405fc6e6 717
tnhnrl 63:6cb0405fc6e6 718 //IF THIS IS ZERO
tnhnrl 63:6cb0405fc6e6 719 if (mbedLogger().currentPacketNumber() > transmit_packet_number) {
tnhnrl 63:6cb0405fc6e6 720 pc().printf("mbedLogger().currentPacketNumber() > transmit_packet_number");
tnhnrl 63:6cb0405fc6e6 721
tnhnrl 63:6cb0405fc6e6 722 //STATE
tnhnrl 63:6cb0405fc6e6 723 _state = SIT_IDLE;
tnhnrl 63:6cb0405fc6e6 724
tnhnrl 45:16b8162188ca 725 timer.reset();
tnhnrl 45:16b8162188ca 726 _isTimeoutRunning = false;
tnhnrl 45:16b8162188ca 727
tnhnrl 45:16b8162188ca 728 mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading
tnhnrl 45:16b8162188ca 729 }
tnhnrl 45:16b8162188ca 730
tnhnrl 63:6cb0405fc6e6 731 //What is active?
tnhnrl 45:16b8162188ca 732 mbedLogger().readTransmitPacketOneChar(); //led2 shows you pc readable
tnhnrl 45:16b8162188ca 733
tnhnrl 45:16b8162188ca 734 break;
tnhnrl 45:16b8162188ca 735
tnhnrl 45:16b8162188ca 736 case RECEIVE_SEQUENCE :
tnhnrl 58:94b7fd55185e 737 pc().printf("state: RECEIVE_SEQUENCE\r\n");
tnhnrl 45:16b8162188ca 738
tnhnrl 45:16b8162188ca 739 if (!_isTimeoutRunning) {
tnhnrl 45:16b8162188ca 740 pc().printf("RECEIVE_SEQUENCE _isTimeoutRunning\r\n");
tnhnrl 45:16b8162188ca 741 timer.reset(); // timer goes back to zero
tnhnrl 45:16b8162188ca 742 timer.start(); // background timer starts running
tnhnrl 45:16b8162188ca 743 _isTimeoutRunning = true;
tnhnrl 45:16b8162188ca 744 }
tnhnrl 45:16b8162188ca 745
tnhnrl 34:9b66c5188051 746 if (timer.read() > _timeout) {
tnhnrl 58:94b7fd55185e 747 pc().printf("RECEIVE_SEQUENCE: timed out!\r\n");
tnhnrl 34:9b66c5188051 748 _state = SIT_IDLE;
tnhnrl 34:9b66c5188051 749 timer.reset();
tnhnrl 32:f2f8ae34aadc 750 _isTimeoutRunning = false;
tnhnrl 32:f2f8ae34aadc 751 }
tnhnrl 32:f2f8ae34aadc 752
tnhnrl 45:16b8162188ca 753 // what is active?
tnhnrl 58:94b7fd55185e 754 pc().printf("Receive sequence active?\r\n");
tnhnrl 32:f2f8ae34aadc 755
tnhnrl 32:f2f8ae34aadc 756 break;
tnhnrl 16:3363b9f14913 757
tnhnrl 16:3363b9f14913 758 default :
tnhnrl 58:94b7fd55185e 759 pc().printf("DEBUG: SIT_IDLE\r\n");
tnhnrl 21:38c8544db6f4 760 _state = SIT_IDLE;
tnhnrl 28:16c83a2fdefa 761 }
tnhnrl 28:16c83a2fdefa 762
tnhnrl 28:16c83a2fdefa 763 //save the state to print to user
tnhnrl 28:16c83a2fdefa 764 if (_previous_state != _state) {
tnhnrl 28:16c83a2fdefa 765 _state_array[_state_array_counter] = _state; //save to state array
tnhnrl 28:16c83a2fdefa 766 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 767
tnhnrl 28:16c83a2fdefa 768 _previous_state = _state;
tnhnrl 28:16c83a2fdefa 769 }
tnhnrl 34:9b66c5188051 770
tnhnrl 34:9b66c5188051 771 return _state;
tnhnrl 34:9b66c5188051 772 // //if the state is SIT_IDLE, return 0 / false (for recording)
tnhnrl 34:9b66c5188051 773 // if (_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 774 // return 0;
tnhnrl 34:9b66c5188051 775 // else
tnhnrl 34:9b66c5188051 776 // return 1; //return true to indicate that you're recording
tnhnrl 16:3363b9f14913 777 }
tnhnrl 20:8987a9ae2bc7 778
tnhnrl 16:3363b9f14913 779 // output the keyboard menu for user's reference
tnhnrl 58:94b7fd55185e 780 void StateMachine::showSimpleMenu() {
tnhnrl 67:c86a4b464682 781 pc().printf("\r\r\n\nSIMPLE KEYBOARD MENU (06/22/2018):\r\r\n"); //make sure depth sensor tares itself on startup
tnhnrl 63:6cb0405fc6e6 782 pc().printf(" Neutral Positions BCE: %0.1f BMM: %0.1f \r\n", _neutral_bce_pos_mm, _neutral_batt_pos_mm);
tnhnrl 58:94b7fd55185e 783
tnhnrl 58:94b7fd55185e 784 pc().printf(" V to POSITION DIVE (initiate motor position-based dive cycle)\r\n");
tnhnrl 57:ec69651c8c21 785 pc().printf(" J to float level\r\n");
tnhnrl 57:ec69651c8c21 786 pc().printf(" B to float at broadcast pitch\r\n");
tnhnrl 57:ec69651c8c21 787 pc().printf(" E to initiate emergency climb\r\n");
tnhnrl 57:ec69651c8c21 788 pc().printf(" P to print the current log file.\r\n");
tnhnrl 57:ec69651c8c21 789 pc().printf(" O to transmit current log file.\r\n");
tnhnrl 57:ec69651c8c21 790 pc().printf(" ~ to erase mbed log file. (clear before logging more than a few runs)\r\n");
tnhnrl 58:94b7fd55185e 791
tnhnrl 58:94b7fd55185e 792 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 793 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 794 pc().printf("9/0 to decrease/increase heading setpoint: %0.1f deg\r\n",_heading_command);
tnhnrl 67:c86a4b464682 795 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 796
tnhnrl 66:0f20870117b7 797 pc().printf("T to enter in the timeout (default is 60 seconds): %d s\r\n",_timeout);
tnhnrl 66:0f20870117b7 798
tnhnrl 57:ec69651c8c21 799 pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 58:94b7fd55185e 800 pc().printf(" 8 STREAM SENSOR STATUS (and channel readings)\r\n");
tnhnrl 58:94b7fd55185e 801
tnhnrl 67:c86a4b464682 802 pc().printf(" '|' to tare the depth sensor (vertical bar)\r\n");
tnhnrl 67:c86a4b464682 803
tnhnrl 57:ec69651c8c21 804 pc().printf(" ? to reset mbed\r\n");
tnhnrl 58:94b7fd55185e 805 pc().printf(" * (asterisk) to go to DEBUG keyboard menu\r\n");
tnhnrl 57:ec69651c8c21 806 }
tnhnrl 57:ec69651c8c21 807
tnhnrl 57:ec69651c8c21 808 void StateMachine::showDebugMenu() {
tnhnrl 67:c86a4b464682 809 pc().printf("\r\r\n\nDEBUG KEYBOARD MENU (06/22/2018):\r\r\n");
tnhnrl 65:2ac186553959 810 pc().printf(" Y to go into CHECK NEUTRAL TUNING (This is on a timer! Uses NEUTRAL positions!)\r\n");
tnhnrl 58:94b7fd55185e 811 pc().printf(" V to POSITION DIVE (initiate motor position-based dive cycle)\r\n");
tnhnrl 16:3363b9f14913 812 pc().printf(" N to find neutral\r\n");
tnhnrl 17:7c16b5671d0e 813 pc().printf(" M to initiate multi-dive cycle\r\n");
tnhnrl 16:3363b9f14913 814 pc().printf(" D to initiate dive cycle\r\n");
tnhnrl 16:3363b9f14913 815 pc().printf(" R to initiate rise\r\n");
tnhnrl 57:ec69651c8c21 816 pc().printf(" J to float level\r\n");
tnhnrl 16:3363b9f14913 817 pc().printf(" B to float at broadcast pitch\r\n");
tnhnrl 16:3363b9f14913 818 pc().printf(" E to initiate emergency climb\r\n");
tnhnrl 49:47ffa4feb6db 819 pc().printf(" '|' to tare the depth sensor (vertical bar)\r\n");
tnhnrl 28:16c83a2fdefa 820 pc().printf(" Z to show FSM and sub-FSM states.\r\n");
tnhnrl 32:f2f8ae34aadc 821 pc().printf(" P to print the current log file.\r\n");
tnhnrl 32:f2f8ae34aadc 822 pc().printf(" X to print the list of log files.\r\n");
tnhnrl 49:47ffa4feb6db 823 pc().printf(" I to receive data.\r\n");
tnhnrl 49:47ffa4feb6db 824 pc().printf(" O to transmit data.\r\n");
tnhnrl 49:47ffa4feb6db 825 pc().printf(" ~ to erase mbed log file. (clear before logging more than a few runs)\r\n");
tnhnrl 32:f2f8ae34aadc 826 pc().printf("[/] to change bce neutral position: %0.1f\r\n", _neutral_bce_pos_mm);
tnhnrl 32:f2f8ae34aadc 827 pc().printf("</> to change batt neutral position: %0.1f\r\n", _neutral_batt_pos_mm);
tnhnrl 32:f2f8ae34aadc 828 pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",_pitch_command);
tnhnrl 67:c86a4b464682 829 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 830 pc().printf("9/0 to decrease/increase heading setpoint: %0.1f deg\r\n",_heading_command);
tnhnrl 58:94b7fd55185e 831 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 832 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 833 pc().printf("T to enter in the timeout (default is 60 seconds): %d s\r\n",_timeout);
tnhnrl 58:94b7fd55185e 834
tnhnrl 58:94b7fd55185e 835 // FIXED WITH NEW CODE
tnhnrl 16:3363b9f14913 836 pc().printf(" 1 BCE PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 837 pc().printf(" 2 BATT PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 838 pc().printf(" 3 Depth PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 839 pc().printf(" 4 Pitch PID sub-menu\r\n");
tnhnrl 58:94b7fd55185e 840 pc().printf(" 5 Rudder (servo) sub-menu\r\n");
tnhnrl 58:94b7fd55185e 841 pc().printf(" 6 HEADING PID sub-menu\r\n");
tnhnrl 58:94b7fd55185e 842 pc().printf(" 7 MANUAL_TUNING sub-menu (does not have a timer!) *** MOTORS ARE ACTIVE ***\r\n");
tnhnrl 58:94b7fd55185e 843 //pc().printf(" 8 VIEW_OUTPUTS sub-menu (does not a have a timer!)\r\n");
tnhnrl 58:94b7fd55185e 844 pc().printf(" 8 STREAM SENSOR STATUS (and channel readings)\r\n");
tnhnrl 53:c0586fe62b01 845
tnhnrl 58:94b7fd55185e 846 // FIXED WITH NEW CODE
tnhnrl 58:94b7fd55185e 847
tnhnrl 53:c0586fe62b01 848
tnhnrl 28:16c83a2fdefa 849 pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 16:3363b9f14913 850 pc().printf(" ? to reset mbed\r\n");
tnhnrl 58:94b7fd55185e 851 pc().printf(" * (asterisk) to go to SIMPLE keyboard menu\r\n");
tnhnrl 16:3363b9f14913 852 }
tnhnrl 20:8987a9ae2bc7 853
tnhnrl 67:c86a4b464682 854 //FIND_NEUTRAL Sub-Finite State Machine (sub-FSM)
tnhnrl 67:c86a4b464682 855 // Note: the sub-FSM only moves the pistons once at the start of each timer loop
tnhnrl 67:c86a4b464682 856 // (timer completes, moves piston, timer completes, moves piston, etc)
tnhnrl 28:16c83a2fdefa 857 int StateMachine::runNeutralStateMachine() {
tnhnrl 24:c7d9b5bf3829 858 switch (_substate) {
tnhnrl 20:8987a9ae2bc7 859 case NEUTRAL_SINKING :
tnhnrl 17:7c16b5671d0e 860 //start the 10 second timer
tnhnrl 28:16c83a2fdefa 861 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 862 _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 863
tnhnrl 58:94b7fd55185e 864 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 865
tnhnrl 32:f2f8ae34aadc 866 // what are the commands? (BCE linear actuator active, no pitch movement)
tnhnrl 39:58375ca6b6ff 867 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 868
tnhnrl 58:94b7fd55185e 869 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 870
tnhnrl 28:16c83a2fdefa 871 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 872 }
tnhnrl 20:8987a9ae2bc7 873
tnhnrl 20:8987a9ae2bc7 874 // how exit?
tnhnrl 20:8987a9ae2bc7 875 //once reached the travel limit, no need to keep trying, so exit
tnhnrl 25:249e4d56b27c 876 if (bce().getPosition_mm() <= 0) {
tnhnrl 58:94b7fd55185e 877 pc().printf("\r\nDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\r\n", bce().getPosition_mm());
tnhnrl 25:249e4d56b27c 878 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 879 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 25:249e4d56b27c 880 }
tnhnrl 20:8987a9ae2bc7 881 //once deeper than the commanded setpoint...
tnhnrl 32:f2f8ae34aadc 882 else if (depthLoop().getPosition() > _depth_command) {
tnhnrl 24:c7d9b5bf3829 883 _substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 28:16c83a2fdefa 884 _isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 20:8987a9ae2bc7 885 }
tnhnrl 20:8987a9ae2bc7 886
tnhnrl 20:8987a9ae2bc7 887 // what is active?
tnhnrl 20:8987a9ae2bc7 888 //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 889 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 890 pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", timer.read());
tnhnrl 17:7c16b5671d0e 891
tnhnrl 28:16c83a2fdefa 892 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 893 }
tnhnrl 39:58375ca6b6ff 894
tnhnrl 39:58375ca6b6ff 895 // what is active? (only the buoyancy engine moved every 5 seconds at start)
tnhnrl 39:58375ca6b6ff 896 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 897
tnhnrl 67:c86a4b464682 898 checkMotorPositions(); //failsafe
tnhnrl 17:7c16b5671d0e 899 break;
tnhnrl 17:7c16b5671d0e 900
tnhnrl 17:7c16b5671d0e 901 case NEUTRAL_SLOWLY_RISE:
tnhnrl 28:16c83a2fdefa 902 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 903 _neutral_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 904
tnhnrl 24:c7d9b5bf3829 905 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 906
tnhnrl 20:8987a9ae2bc7 907 // what are the commands?
tnhnrl 32:f2f8ae34aadc 908 //move piston at start of sequence (default: extend 2.0 mm)
tnhnrl 39:58375ca6b6ff 909 bce().setPosition_mm(bce().getSetPosition_mm() + 2.0); //no depth command
tnhnrl 23:434f04ef1fad 910
tnhnrl 23:434f04ef1fad 911 // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state
tnhnrl 23:434f04ef1fad 912 pitchLoop().setCommand(0.0);
tnhnrl 24:c7d9b5bf3829 913
tnhnrl 58:94b7fd55185e 914 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 915
tnhnrl 28:16c83a2fdefa 916 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 917 }
tnhnrl 17:7c16b5671d0e 918
tnhnrl 20:8987a9ae2bc7 919 // how exit?
tnhnrl 24:c7d9b5bf3829 920 //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit
tnhnrl 24:c7d9b5bf3829 921 if (bce().getSetPosition_mm() >= bce().getTravelLimit()) {
tnhnrl 24:c7d9b5bf3829 922 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 923 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 924 }
tnhnrl 17:7c16b5671d0e 925 //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration
tnhnrl 20:8987a9ae2bc7 926 else if (depthLoop().getVelocity() < 0) { //less than zero ft/s
tnhnrl 24:c7d9b5bf3829 927 pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 ft/s [time: %0.1f]\r\n", timer.read());
tnhnrl 24:c7d9b5bf3829 928 _substate = NEUTRAL_CHECK_PITCH;
tnhnrl 28:16c83a2fdefa 929 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 930 }
tnhnrl 17:7c16b5671d0e 931
tnhnrl 20:8987a9ae2bc7 932 // what is active?
tnhnrl 20:8987a9ae2bc7 933 //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 934 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 935 pc().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! [timer: %0.1f]\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 936
tnhnrl 28:16c83a2fdefa 937 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 938 }
tnhnrl 23:434f04ef1fad 939
tnhnrl 32:f2f8ae34aadc 940 // what is active? (only the buoyancy engine moved every 5 seconds)
tnhnrl 32:f2f8ae34aadc 941 pc().printf("depthLoop getOutput: %0.1f\r", depthLoop().getOutput()); //debug
tnhnrl 67:c86a4b464682 942 bce().setPosition_mm(depthLoop().getOutput());
tnhnrl 67:c86a4b464682 943
tnhnrl 67:c86a4b464682 944 checkMotorPositions(); //failsafe
tnhnrl 17:7c16b5671d0e 945 break;
tnhnrl 17:7c16b5671d0e 946
danstrider 22:a10ee088403b 947 case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired
danstrider 22:a10ee088403b 948 // start local state timer and init any other one-shot actions
tnhnrl 23:434f04ef1fad 949
tnhnrl 28:16c83a2fdefa 950 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 951 _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds
tnhnrl 24:c7d9b5bf3829 952 pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read());
danstrider 22:a10ee088403b 953
tnhnrl 32:f2f8ae34aadc 954 // what are the commands? (default: retract or extend 0.5 mm)
tnhnrl 24:c7d9b5bf3829 955 if (pitchLoop().getPosition() > 2) { // nose is high
tnhnrl 39:58375ca6b6ff 956 batt().setPosition_mm(batt().getSetPosition_mm() + 0.5); // move battery forward (using setpoint from linear actuator)
tnhnrl 58:94b7fd55185e 957 pc().printf("\r\nNeutral Check Pitch: moving battery FWD in %0.1f mm increments\r\n\n", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 958 }
tnhnrl 24:c7d9b5bf3829 959 else if (pitchLoop().getPosition() < -2) { // nose is low
tnhnrl 39:58375ca6b6ff 960 batt().setPosition_mm(batt().getSetPosition_mm() - 0.5); // move battery aft (using setpoint from linear actuator)
tnhnrl 58:94b7fd55185e 961 pc().printf("\r\nNeutral Check Pitch: moving battery AFT in %0.1f mm increments\r\n\n", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 962 }
tnhnrl 24:c7d9b5bf3829 963
tnhnrl 28:16c83a2fdefa 964 _isSubStateTimerRunning = true; //disable this block after one iteration
danstrider 22:a10ee088403b 965 }
tnhnrl 20:8987a9ae2bc7 966
tnhnrl 28:16c83a2fdefa 967 // how exit?
tnhnrl 20:8987a9ae2bc7 968 //pitch angle and pitch rate within small tolerance
tnhnrl 20:8987a9ae2bc7 969 //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 23:434f04ef1fad 970 if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) {
tnhnrl 58:94b7fd55185e 971 pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\r\n"); //debug
danstrider 22:a10ee088403b 972 // found level, but don't need to save anything this time
tnhnrl 23:434f04ef1fad 973
tnhnrl 28:16c83a2fdefa 974 if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug
tnhnrl 28:16c83a2fdefa 975 _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 976 }
tnhnrl 28:16c83a2fdefa 977
tnhnrl 23:434f04ef1fad 978 // found level and at depth too, so save it all now
tnhnrl 32:f2f8ae34aadc 979 if (_substate == NEUTRAL_CHECK_PITCH) {
danstrider 22:a10ee088403b 980 //save positions locally
danstrider 22:a10ee088403b 981 _neutral_batt_pos_mm = batt().getPosition_mm();
danstrider 22:a10ee088403b 982 _neutral_bce_pos_mm = bce().getPosition_mm();
danstrider 22:a10ee088403b 983
danstrider 22:a10ee088403b 984 //set the neutral positions in each outer loop
danstrider 22:a10ee088403b 985 depthLoop().setOutputOffset(_neutral_bce_pos_mm);
danstrider 22:a10ee088403b 986 pitchLoop().setOutputOffset(_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 987
danstrider 22:a10ee088403b 988 // save into the depth.txt and pitch.txt files
danstrider 22:a10ee088403b 989 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset
danstrider 22:a10ee088403b 990 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset
danstrider 22:a10ee088403b 991
tnhnrl 58:94b7fd55185e 992 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 993
tnhnrl 24:c7d9b5bf3829 994 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 995 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 24:c7d9b5bf3829 996 }
tnhnrl 24:c7d9b5bf3829 997
tnhnrl 24:c7d9b5bf3829 998 else {
tnhnrl 58:94b7fd55185e 999 pc().printf("\r\nDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\r\n");
tnhnrl 24:c7d9b5bf3829 1000 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 1001 }
tnhnrl 17:7c16b5671d0e 1002 }
danstrider 22:a10ee088403b 1003
danstrider 22:a10ee088403b 1004 // what is active?
danstrider 22:a10ee088403b 1005 //once timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 1006 if (timer.read() >= _neutral_timer) {
danstrider 22:a10ee088403b 1007 pc().printf("\r\n\nlevel timer COMPLETE!");
danstrider 22:a10ee088403b 1008 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 1009 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
danstrider 22:a10ee088403b 1010 }
tnhnrl 67:c86a4b464682 1011
tnhnrl 67:c86a4b464682 1012 checkMotorPositions(); //failsafe
tnhnrl 17:7c16b5671d0e 1013 break;
danstrider 22:a10ee088403b 1014
danstrider 22:a10ee088403b 1015 //this state could be removed, it is only used as a transition but is needed to stop entering this function
danstrider 22:a10ee088403b 1016 case NEUTRAL_EXIT :
tnhnrl 58:94b7fd55185e 1017 pc().printf("substate: NEUTRAL_EXIT\r\n");
tnhnrl 20:8987a9ae2bc7 1018 break;
tnhnrl 21:38c8544db6f4 1019
danstrider 22:a10ee088403b 1020 default :
tnhnrl 58:94b7fd55185e 1021 pc().printf("how did we get to substate: default?\r\n"); //debug
tnhnrl 23:434f04ef1fad 1022 //a default within the sub-state machine
tnhnrl 24:c7d9b5bf3829 1023 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 1024 break;
tnhnrl 17:7c16b5671d0e 1025 }
tnhnrl 20:8987a9ae2bc7 1026
tnhnrl 30:2964617e7676 1027 // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence)
tnhnrl 24:c7d9b5bf3829 1028 if (_substate == NEUTRAL_EXIT) {
tnhnrl 58:94b7fd55185e 1029 pc().printf("******************************** EXITING sub-FSM! *******************************\r\n\n");
tnhnrl 24:c7d9b5bf3829 1030
tnhnrl 30:2964617e7676 1031 //reset internal sub-state back to first entry conditions (first state is immediately sinking)
tnhnrl 30:2964617e7676 1032 _substate = NEUTRAL_SINKING;
tnhnrl 28:16c83a2fdefa 1033 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 21:38c8544db6f4 1034
tnhnrl 24:c7d9b5bf3829 1035 //record sub-states to view after sequence
tnhnrl 30:2964617e7676 1036 _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array
tnhnrl 28:16c83a2fdefa 1037 _substate_array_counter++;
tnhnrl 23:434f04ef1fad 1038
tnhnrl 24:c7d9b5bf3829 1039 //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit)
tnhnrl 24:c7d9b5bf3829 1040 _previous_substate = -1;
tnhnrl 24:c7d9b5bf3829 1041
tnhnrl 24:c7d9b5bf3829 1042 //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 24:c7d9b5bf3829 1043 return NEUTRAL_EXIT; // message to calling function we just exited
tnhnrl 21:38c8544db6f4 1044 }
tnhnrl 23:434f04ef1fad 1045 else {
tnhnrl 24:c7d9b5bf3829 1046 //record sub-states to view after sequence (when changed)
tnhnrl 24:c7d9b5bf3829 1047 if (_previous_substate != _substate) {
tnhnrl 28:16c83a2fdefa 1048 _substate_array[_substate_array_counter] = _substate; //save current state to state array
tnhnrl 28:16c83a2fdefa 1049 _substate_array_counter++;
tnhnrl 24:c7d9b5bf3829 1050
tnhnrl 24:c7d9b5bf3829 1051 //record the current substate for comparison
tnhnrl 24:c7d9b5bf3829 1052 _previous_substate = _substate;
tnhnrl 24:c7d9b5bf3829 1053 }
tnhnrl 24:c7d9b5bf3829 1054
tnhnrl 24:c7d9b5bf3829 1055 return _substate; // message to calling function of what sub-state it's in
tnhnrl 23:434f04ef1fad 1056 }
tnhnrl 17:7c16b5671d0e 1057 }
tnhnrl 20:8987a9ae2bc7 1058
tnhnrl 57:ec69651c8c21 1059 /* keyboard runs independently of the state machine, handling one key at a time
tnhnrl 57:ec69651c8c21 1060 keyboard updates the desired _keyboard_state that is used in the state machine
tnhnrl 57:ec69651c8c21 1061 and only allows input when the state is "idle" */
tnhnrl 57:ec69651c8c21 1062
tnhnrl 17:7c16b5671d0e 1063 void StateMachine::keyboard() {
tnhnrl 57:ec69651c8c21 1064 char user_input;
tnhnrl 20:8987a9ae2bc7 1065
tnhnrl 16:3363b9f14913 1066 // check keyboard and make settings changes as requested
tnhnrl 17:7c16b5671d0e 1067 // states can be changed only at the start of a sequence (when the system is in SIT_IDLE)
tnhnrl 21:38c8544db6f4 1068
tnhnrl 37:357e98a929cc 1069 //TEST
tnhnrl 37:357e98a929cc 1070 int _keyboard_state = 0; //made this a local variable because it was retaining the last keyboard state
tnhnrl 21:38c8544db6f4 1071
tnhnrl 28:16c83a2fdefa 1072 if (pc().readable() && (_state == SIT_IDLE || _state == KEYBOARD)) {
tnhnrl 16:3363b9f14913 1073 // get the key
tnhnrl 57:ec69651c8c21 1074 user_input = pc().getc();
tnhnrl 17:7c16b5671d0e 1075
tnhnrl 28:16c83a2fdefa 1076 //record that the keyboard was used
tnhnrl 28:16c83a2fdefa 1077 _state_array[_state_array_counter] = KEYBOARD;
tnhnrl 28:16c83a2fdefa 1078 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 1079
tnhnrl 21:38c8544db6f4 1080 // keyboard has to reset timer each time it's used
tnhnrl 28:16c83a2fdefa 1081 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 1082
tnhnrl 16:3363b9f14913 1083 // check command against desired control buttons
tnhnrl 57:ec69651c8c21 1084
tnhnrl 57:ec69651c8c21 1085 /***************************** DEBUG MENU *****************************/
tnhnrl 57:ec69651c8c21 1086 if (_debug_menu_on) {
tnhnrl 17:7c16b5671d0e 1087
tnhnrl 65:2ac186553959 1088 if (user_input == 'T') {
tnhnrl 65:2ac186553959 1089 pc().printf("Please enter the timeout (timer) value below: \n\r");
tnhnrl 65:2ac186553959 1090 _timeout = fabs(getFloatUserInput());
tnhnrl 65:2ac186553959 1091 }
tnhnrl 65:2ac186553959 1092
tnhnrl 57:ec69651c8c21 1093 if (user_input == 'D') {
tnhnrl 57:ec69651c8c21 1094 _keyboard_state = DIVE;
tnhnrl 57:ec69651c8c21 1095 }
tnhnrl 57:ec69651c8c21 1096 else if (user_input == 'N') {
tnhnrl 57:ec69651c8c21 1097 _keyboard_state = FIND_NEUTRAL;
tnhnrl 57:ec69651c8c21 1098 }
tnhnrl 57:ec69651c8c21 1099 else if (user_input == 'M') {
tnhnrl 57:ec69651c8c21 1100 //currently does not run if there is no file.
tnhnrl 57:ec69651c8c21 1101
tnhnrl 57:ec69651c8c21 1102 //need to add method to Sequence Controller that returns -1
tnhnrl 57:ec69651c8c21 1103 // or some check that insures you cannot run the dive sequence without a file
tnhnrl 57:ec69651c8c21 1104
tnhnrl 57:ec69651c8c21 1105 stateMachine().getDiveSequence(); //get first sequence on keyboard press
tnhnrl 57:ec69651c8c21 1106 _keyboard_state = currentStateStruct.state;
tnhnrl 57:ec69651c8c21 1107
tnhnrl 58:94b7fd55185e 1108 pc().printf("Starting Dive Sequence Controller! (state: %d)\r\n", _keyboard_state); //neutral sequence and dive cycles
tnhnrl 57:ec69651c8c21 1109 }
tnhnrl 57:ec69651c8c21 1110 else if (user_input == 'R') {
tnhnrl 57:ec69651c8c21 1111 _keyboard_state = RISE;
tnhnrl 57:ec69651c8c21 1112 }
tnhnrl 57:ec69651c8c21 1113 else if (user_input == 'J') {
tnhnrl 57:ec69651c8c21 1114 _keyboard_state = FLOAT_LEVEL;
tnhnrl 57:ec69651c8c21 1115 }
tnhnrl 57:ec69651c8c21 1116 else if (user_input == 'B') {
tnhnrl 57:ec69651c8c21 1117 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 57:ec69651c8c21 1118 }
tnhnrl 57:ec69651c8c21 1119 else if (user_input == 'E') {
tnhnrl 57:ec69651c8c21 1120 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 57:ec69651c8c21 1121 }
tnhnrl 17:7c16b5671d0e 1122
tnhnrl 65:2ac186553959 1123 else if (user_input == 'Y') {
tnhnrl 57:ec69651c8c21 1124 _keyboard_state = CHECK_TUNING;
tnhnrl 57:ec69651c8c21 1125 }
tnhnrl 17:7c16b5671d0e 1126
tnhnrl 58:94b7fd55185e 1127 else if (user_input == 'V') {
tnhnrl 58:94b7fd55185e 1128 _keyboard_state = POSITION_DIVE;
tnhnrl 58:94b7fd55185e 1129 }
tnhnrl 58:94b7fd55185e 1130
tnhnrl 57:ec69651c8c21 1131 // some debug tools below
tnhnrl 57:ec69651c8c21 1132 else if (user_input == 'P') {
tnhnrl 57:ec69651c8c21 1133 //Print current SD card log file
tnhnrl 57:ec69651c8c21 1134 //printCurrentSdLog();
tnhnrl 57:ec69651c8c21 1135 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 57:ec69651c8c21 1136 }
tnhnrl 57:ec69651c8c21 1137 else if (user_input == 'X') {
tnhnrl 57:ec69651c8c21 1138 mbedLogger().printMbedDirectory(); //print all log files to the screen
tnhnrl 57:ec69651c8c21 1139 }
tnhnrl 57:ec69651c8c21 1140 else if (user_input == 'O') {
tnhnrl 63:6cb0405fc6e6 1141 _keyboard_state = TRANSMIT_MBED_LOG; //Transmit data (work in progress)
tnhnrl 57:ec69651c8c21 1142 }
tnhnrl 57:ec69651c8c21 1143 else if (user_input == 'I') {
tnhnrl 57:ec69651c8c21 1144 mbedLogger().receiveMissionDataWithTicker(); //receive sequence.txt files
tnhnrl 57:ec69651c8c21 1145 }
tnhnrl 57:ec69651c8c21 1146 else if (user_input == '~') {
tnhnrl 58:94b7fd55185e 1147 pc().printf("ERASING MBED LOG FILE\r\n");
tnhnrl 57:ec69651c8c21 1148 mbedLogger().eraseFile();
tnhnrl 57:ec69651c8c21 1149 }
tnhnrl 57:ec69651c8c21 1150 else if (user_input == 'Z') {
tnhnrl 58:94b7fd55185e 1151 pc().printf("FSG FSM States: \r\n");
tnhnrl 57:ec69651c8c21 1152 string string_state;
tnhnrl 57:ec69651c8c21 1153
tnhnrl 57:ec69651c8c21 1154 for (int i = 0; i < _state_array_counter; i++) {
tnhnrl 57:ec69651c8c21 1155 if (_state_array[i] == SIT_IDLE)
tnhnrl 57:ec69651c8c21 1156 string_state = "SIT_IDLE <END>";
tnhnrl 57:ec69651c8c21 1157 else if (_state_array[i] == FIND_NEUTRAL)
tnhnrl 57:ec69651c8c21 1158 string_state = "FIND_NEUTRAL";
tnhnrl 57:ec69651c8c21 1159 else if (_state_array[i] == DIVE)
tnhnrl 57:ec69651c8c21 1160 string_state = "DIVE";
tnhnrl 57:ec69651c8c21 1161 else if (_state_array[i] == RISE)
tnhnrl 57:ec69651c8c21 1162 string_state = "RISE";
tnhnrl 57:ec69651c8c21 1163 else if (_state_array[i] == FLOAT_LEVEL)
tnhnrl 57:ec69651c8c21 1164 string_state = "FLOAT_LEVEL";
tnhnrl 57:ec69651c8c21 1165 else if (_state_array[i] == FLOAT_BROADCAST)
tnhnrl 57:ec69651c8c21 1166 string_state = "FLOAT_BROADCAST";
tnhnrl 57:ec69651c8c21 1167 else if (_state_array[i] == EMERGENCY_CLIMB)
tnhnrl 57:ec69651c8c21 1168 string_state = "EMERGENCY_CLIMB";
tnhnrl 57:ec69651c8c21 1169 else if (_state_array[i] == MULTI_DIVE)
tnhnrl 57:ec69651c8c21 1170 string_state = "MULTI_DIVE";
tnhnrl 57:ec69651c8c21 1171 else if (_state_array[i] == MULTI_RISE)
tnhnrl 57:ec69651c8c21 1172 string_state = "MULTI_RISE";
tnhnrl 57:ec69651c8c21 1173 else if (_state_array[i] == KEYBOARD)
tnhnrl 57:ec69651c8c21 1174 string_state = "KEYBOARD";
tnhnrl 58:94b7fd55185e 1175 pc().printf("State #%d: %d (%s)\r\n", i, _state_array[i], string_state.c_str());
tnhnrl 57:ec69651c8c21 1176 }
tnhnrl 57:ec69651c8c21 1177
tnhnrl 58:94b7fd55185e 1178 pc().printf("\r\nNeutral sub-FSM States: \r\n");
tnhnrl 57:ec69651c8c21 1179 string string_substate;
tnhnrl 57:ec69651c8c21 1180
tnhnrl 57:ec69651c8c21 1181 for (int i = 0; i < _substate_array_counter; i++) {
tnhnrl 57:ec69651c8c21 1182 if (_substate_array[i] == NEUTRAL_SINKING)
tnhnrl 57:ec69651c8c21 1183 string_substate = "NEUTRAL_SINKING";
tnhnrl 57:ec69651c8c21 1184 else if (_substate_array[i] == NEUTRAL_SLOWLY_RISE)
tnhnrl 57:ec69651c8c21 1185 string_substate = "NEUTRAL_SLOWLY_RISE";
tnhnrl 57:ec69651c8c21 1186 else if (_substate_array[i] == NEUTRAL_CHECK_PITCH)
tnhnrl 57:ec69651c8c21 1187 string_substate = "NEUTRAL_CHECK_PITCH";
tnhnrl 57:ec69651c8c21 1188 else if (_substate_array[i] == NEUTRAL_EXIT)
tnhnrl 57:ec69651c8c21 1189 string_substate = "NEUTRAL_EXIT <-- ";
tnhnrl 57:ec69651c8c21 1190 else if (_substate_array[i] == EMERGENCY_CLIMB)
tnhnrl 57:ec69651c8c21 1191 string_substate = " -- > EMERGENCY_CLIMB <-- ";
tnhnrl 58:94b7fd55185e 1192 pc().printf("Neutral Substate #%d: %d (%s)\r\n", i, _state_array[i], string_substate.c_str());
tnhnrl 57:ec69651c8c21 1193 }
tnhnrl 58:94b7fd55185e 1194 pc().printf("\r\n"); //make space between printouts
tnhnrl 57:ec69651c8c21 1195 }
tnhnrl 57:ec69651c8c21 1196 else if (user_input == '|') {
tnhnrl 57:ec69651c8c21 1197 pc().printf("taring depth sensor\r\n");
tnhnrl 57:ec69651c8c21 1198 pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 57:ec69651c8c21 1199 wait(0.1);
tnhnrl 57:ec69651c8c21 1200 depth().tare(); // tares to ambient (do on surface)
tnhnrl 57:ec69651c8c21 1201 pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 57:ec69651c8c21 1202 }
tnhnrl 28:16c83a2fdefa 1203
tnhnrl 57:ec69651c8c21 1204 else if (user_input == '[' or user_input == '{') {
tnhnrl 57:ec69651c8c21 1205 _neutral_bce_pos_mm = depthLoop().getOutputOffset() - 1;
tnhnrl 57:ec69651c8c21 1206 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // decrease the bce neutral setpoint
tnhnrl 57:ec69651c8c21 1207 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1208 // save neutral depth value to config file
tnhnrl 57:ec69651c8c21 1209 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 57:ec69651c8c21 1210 }
tnhnrl 57:ec69651c8c21 1211 else if (user_input == ']' or user_input == '}') {
tnhnrl 57:ec69651c8c21 1212 _neutral_bce_pos_mm = depthLoop().getOutputOffset() + 1;
tnhnrl 57:ec69651c8c21 1213 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // increase the bce neutral setpoint
tnhnrl 57:ec69651c8c21 1214 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1215 // save neutral depth value to config file
tnhnrl 57:ec69651c8c21 1216 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 57:ec69651c8c21 1217 }
tnhnrl 57:ec69651c8c21 1218 else if (user_input == '<' or user_input == ',') {
tnhnrl 57:ec69651c8c21 1219 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() - 1;
tnhnrl 57:ec69651c8c21 1220 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // decrease the batt neutral setpoint
tnhnrl 57:ec69651c8c21 1221 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1222 // save neutral pitch value to config file
tnhnrl 57:ec69651c8c21 1223 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 57:ec69651c8c21 1224 }
tnhnrl 57:ec69651c8c21 1225 else if (user_input == '>' or user_input == '.') {
tnhnrl 57:ec69651c8c21 1226 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() + 1;
tnhnrl 57:ec69651c8c21 1227 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // increase the batt neutral setpoint
tnhnrl 57:ec69651c8c21 1228 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1229 // save neutral pitch value to config file
tnhnrl 57:ec69651c8c21 1230 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 28:16c83a2fdefa 1231 }
tnhnrl 28:16c83a2fdefa 1232
tnhnrl 57:ec69651c8c21 1233 else if (user_input == '?') {
tnhnrl 57:ec69651c8c21 1234 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 57:ec69651c8c21 1235 wait(0.5);
tnhnrl 57:ec69651c8c21 1236 mbed_reset();
tnhnrl 57:ec69651c8c21 1237 }
tnhnrl 57:ec69651c8c21 1238
tnhnrl 57:ec69651c8c21 1239 // change settings
tnhnrl 57:ec69651c8c21 1240 else if (user_input == 'Q' or user_input == 'q') {
tnhnrl 57:ec69651c8c21 1241 _pitch_command -= 0.5; //decrement the pitch setpoint
tnhnrl 57:ec69651c8c21 1242 pitchLoop().setCommand(_pitch_command);
tnhnrl 57:ec69651c8c21 1243 pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand());
tnhnrl 57:ec69651c8c21 1244 }
tnhnrl 57:ec69651c8c21 1245 else if (user_input == 'W' or user_input == 'w') {
tnhnrl 57:ec69651c8c21 1246 _pitch_command += 0.5; //increment the pitch setpoint
tnhnrl 57:ec69651c8c21 1247 pitchLoop().setCommand(_pitch_command);
tnhnrl 57:ec69651c8c21 1248 pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand());
tnhnrl 57:ec69651c8c21 1249 }
tnhnrl 57:ec69651c8c21 1250 else if (user_input == 'A' or user_input == 'a') {
tnhnrl 57:ec69651c8c21 1251 _depth_command -= 0.5; //decrement the depth setpoint
tnhnrl 57:ec69651c8c21 1252 depthLoop().setCommand(_depth_command);
tnhnrl 57:ec69651c8c21 1253 pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand());
tnhnrl 57:ec69651c8c21 1254 }
tnhnrl 57:ec69651c8c21 1255 else if (user_input == 'S' or user_input == 's') {
tnhnrl 57:ec69651c8c21 1256 _depth_command += 0.5; //increment the depth setpoint
tnhnrl 57:ec69651c8c21 1257 depthLoop().setCommand(_depth_command);
tnhnrl 57:ec69651c8c21 1258 pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand());
tnhnrl 65:2ac186553959 1259 }
tnhnrl 57:ec69651c8c21 1260 else if (user_input == '5') {
tnhnrl 58:94b7fd55185e 1261 keyboard_menu_RUDDER_SERVO_settings();
tnhnrl 57:ec69651c8c21 1262 }
tnhnrl 57:ec69651c8c21 1263
tnhnrl 57:ec69651c8c21 1264 else if (user_input == '6') {
tnhnrl 58:94b7fd55185e 1265 keyboard_menu_HEADING_PID_settings();
tnhnrl 58:94b7fd55185e 1266 }
tnhnrl 57:ec69651c8c21 1267
tnhnrl 58:94b7fd55185e 1268 // go to tuning sub-menu
tnhnrl 57:ec69651c8c21 1269 else if (user_input == '7') {
tnhnrl 58:94b7fd55185e 1270 keyboard_menu_MANUAL_TUNING();
tnhnrl 57:ec69651c8c21 1271 }
tnhnrl 57:ec69651c8c21 1272
tnhnrl 57:ec69651c8c21 1273 else if (user_input == '8') {
tnhnrl 58:94b7fd55185e 1274 keyboard_menu_STREAM_STATUS(); //and channel readings for debugging
tnhnrl 57:ec69651c8c21 1275 }
tnhnrl 58:94b7fd55185e 1276
tnhnrl 57:ec69651c8c21 1277 else if (user_input == '9') {
tnhnrl 58:94b7fd55185e 1278 _heading_command -= 5.0; //decrement the rudder setpoint
tnhnrl 58:94b7fd55185e 1279 headingLoop().setCommand(_heading_command);
tnhnrl 58:94b7fd55185e 1280 pc().printf(">>> (-) new HEADING setpoint: %0.3f deg (-)\r\n", headingLoop().getCommand());
tnhnrl 58:94b7fd55185e 1281 }
tnhnrl 58:94b7fd55185e 1282 else if (user_input == '0') {
tnhnrl 58:94b7fd55185e 1283 _heading_command += 5.0; //increment the rudder setpoint
tnhnrl 58:94b7fd55185e 1284 headingLoop().setCommand(_heading_command);
tnhnrl 58:94b7fd55185e 1285 pc().printf(">>> (+) new HEADING setpoint: %0.3f deg (+)\r\n", headingLoop().getCommand());
tnhnrl 58:94b7fd55185e 1286 }
tnhnrl 65:2ac186553959 1287 else if (user_input == 'U') {
tnhnrl 57:ec69651c8c21 1288 keyboard_menu_POSITION_READINGS();
tnhnrl 57:ec69651c8c21 1289 }
tnhnrl 57:ec69651c8c21 1290
tnhnrl 57:ec69651c8c21 1291 // go to sub-menus for the PID gains (this is blocking)
tnhnrl 57:ec69651c8c21 1292 else if (user_input == '1') {
tnhnrl 57:ec69651c8c21 1293 keyboard_menu_BCE_PID_settings();
tnhnrl 57:ec69651c8c21 1294 }
tnhnrl 57:ec69651c8c21 1295 else if (user_input == '2') {
tnhnrl 57:ec69651c8c21 1296 keyboard_menu_BATT_PID_settings();
tnhnrl 57:ec69651c8c21 1297 }
tnhnrl 57:ec69651c8c21 1298 else if (user_input == '3') {
tnhnrl 57:ec69651c8c21 1299 keyboard_menu_DEPTH_PID_settings();
tnhnrl 57:ec69651c8c21 1300 }
tnhnrl 57:ec69651c8c21 1301 else if (user_input == '4') {
tnhnrl 57:ec69651c8c21 1302 keyboard_menu_PITCH_PID_settings();
tnhnrl 28:16c83a2fdefa 1303 }
tnhnrl 57:ec69651c8c21 1304
tnhnrl 57:ec69651c8c21 1305 else if (user_input == 'C' or user_input == 'c') {
tnhnrl 57:ec69651c8c21 1306
tnhnrl 58:94b7fd55185e 1307 pc().printf("\r\n\nCURRENT STATUS AND PARAMETERS:\r\n");
tnhnrl 57:ec69651c8c21 1308
tnhnrl 57:ec69651c8c21 1309 // Testing out ADC
tnhnrl 57:ec69651c8c21 1310 float vref = 5.6;
tnhnrl 57:ec69651c8c21 1311 float vmeasured = 0;
tnhnrl 57:ec69651c8c21 1312 unsigned int raw = adc().readCh5();
tnhnrl 57:ec69651c8c21 1313 vmeasured = ((float)raw)/4095.0*vref;
tnhnrl 57:ec69651c8c21 1314 pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
tnhnrl 57:ec69651c8c21 1315 pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
tnhnrl 57:ec69651c8c21 1316 pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
tnhnrl 57:ec69651c8c21 1317 pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
tnhnrl 57:ec69651c8c21 1318 pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
tnhnrl 57:ec69651c8c21 1319 pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
tnhnrl 57:ec69651c8c21 1320 pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
tnhnrl 57:ec69651c8c21 1321 pc().printf("raw board current: %d \r\n",adc().readCh7());
tnhnrl 57:ec69651c8c21 1322 pc().printf("raw BCE limit switch: %d \r\n",bce().getSwitch());
tnhnrl 57:ec69651c8c21 1323 pc().printf("raw BMM limit switch: %d \r\n",batt().getSwitch());
tnhnrl 57:ec69651c8c21 1324 pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
tnhnrl 57:ec69651c8c21 1325 // End of ADC Test
tnhnrl 57:ec69651c8c21 1326
tnhnrl 57:ec69651c8c21 1327 pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 57:ec69651c8c21 1328 pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 57:ec69651c8c21 1329 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 57:ec69651c8c21 1330 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 57:ec69651c8c21 1331 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 57:ec69651c8c21 1332 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 57:ec69651c8c21 1333 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 57:ec69651c8c21 1334 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 57:ec69651c8c21 1335
tnhnrl 58:94b7fd55185e 1336 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 1337 pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 57:ec69651c8c21 1338 pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 58:94b7fd55185e 1339 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 1340
tnhnrl 58:94b7fd55185e 1341 pc().printf("\r\n");
tnhnrl 57:ec69651c8c21 1342 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 1343 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 1344 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 1345 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 1346 }
tnhnrl 57:ec69651c8c21 1347
tnhnrl 57:ec69651c8c21 1348 //POSITION DIVE COMMANDS
tnhnrl 57:ec69651c8c21 1349 else if (user_input == 'k') {
tnhnrl 58:94b7fd55185e 1350 _BCE_dive_offset -= 1.0;
tnhnrl 58:94b7fd55185e 1351 pc().printf("Decreased BCE dive offset to %0.1f\r\n", _BCE_dive_offset);
tnhnrl 57:ec69651c8c21 1352 }
tnhnrl 57:ec69651c8c21 1353 else if (user_input == 'l') {
tnhnrl 58:94b7fd55185e 1354 _BCE_dive_offset += 1.0;
tnhnrl 58:94b7fd55185e 1355 pc().printf("Increased BCE dive offset to %0.1f\r\n", _BCE_dive_offset);
tnhnrl 57:ec69651c8c21 1356 }
tnhnrl 57:ec69651c8c21 1357 else if (user_input == ';') {
tnhnrl 58:94b7fd55185e 1358 _BMM_dive_offset -= 1.0;
tnhnrl 58:94b7fd55185e 1359 pc().printf("Decreased BMM dive offset to %0.1f\r\n", _BMM_dive_offset);
tnhnrl 57:ec69651c8c21 1360 }
tnhnrl 57:ec69651c8c21 1361 else if (user_input == '\'') {
tnhnrl 58:94b7fd55185e 1362 _BMM_dive_offset += 1.0;
tnhnrl 58:94b7fd55185e 1363 pc().printf("Increased BMM dive offset to %0.1f\r\n", _BMM_dive_offset);
tnhnrl 57:ec69651c8c21 1364 }
tnhnrl 57:ec69651c8c21 1365 //POSITION DIVE COMMANDS
tnhnrl 57:ec69651c8c21 1366
tnhnrl 57:ec69651c8c21 1367 else if (user_input == '*') {
tnhnrl 58:94b7fd55185e 1368 pc().printf("SWITCHING TO SIMPLE MENU!\r\n");
tnhnrl 57:ec69651c8c21 1369 wait(2);
tnhnrl 57:ec69651c8c21 1370 _debug_menu_on = false;
tnhnrl 57:ec69651c8c21 1371 }
tnhnrl 57:ec69651c8c21 1372 } //end of debug menu
tnhnrl 57:ec69651c8c21 1373 /***************************** DEBUG MENU *****************************/
tnhnrl 57:ec69651c8c21 1374
tnhnrl 57:ec69651c8c21 1375 /***************************** SIMPLE MENU *****************************/
tnhnrl 65:2ac186553959 1376 else {
tnhnrl 65:2ac186553959 1377 if (user_input == 'T') {
tnhnrl 65:2ac186553959 1378 pc().printf("Please enter the timeout (timer) value below: \n\r");
tnhnrl 65:2ac186553959 1379 _timeout = fabs(getFloatUserInput());
tnhnrl 65:2ac186553959 1380 }
tnhnrl 58:94b7fd55185e 1381 if (user_input == 'V') {
tnhnrl 58:94b7fd55185e 1382 _keyboard_state = POSITION_DIVE;
tnhnrl 57:ec69651c8c21 1383 }
tnhnrl 57:ec69651c8c21 1384 else if (user_input == 'N') {
tnhnrl 57:ec69651c8c21 1385 _keyboard_state = FIND_NEUTRAL;
tnhnrl 57:ec69651c8c21 1386 }
tnhnrl 57:ec69651c8c21 1387 else if (user_input == 'J') {
tnhnrl 57:ec69651c8c21 1388 _keyboard_state = FLOAT_LEVEL;
tnhnrl 57:ec69651c8c21 1389 }
tnhnrl 57:ec69651c8c21 1390 else if (user_input == 'B') {
tnhnrl 57:ec69651c8c21 1391 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 57:ec69651c8c21 1392 }
tnhnrl 57:ec69651c8c21 1393 else if (user_input == 'E') {
tnhnrl 57:ec69651c8c21 1394 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 57:ec69651c8c21 1395 }
tnhnrl 57:ec69651c8c21 1396
tnhnrl 57:ec69651c8c21 1397 // some debug tools below
tnhnrl 57:ec69651c8c21 1398 else if (user_input == 'P') {
tnhnrl 57:ec69651c8c21 1399 //Print current SD card log file
tnhnrl 57:ec69651c8c21 1400 //printCurrentSdLog();
tnhnrl 57:ec69651c8c21 1401 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 57:ec69651c8c21 1402 }
tnhnrl 57:ec69651c8c21 1403 else if (user_input == 'O') {
tnhnrl 63:6cb0405fc6e6 1404 _keyboard_state = TRANSMIT_MBED_LOG; //Transmit data (work in progress)
tnhnrl 57:ec69651c8c21 1405 }
tnhnrl 57:ec69651c8c21 1406 else if (user_input == '~') {
tnhnrl 58:94b7fd55185e 1407 pc().printf("ERASING MBED LOG FILE\r\n");
tnhnrl 57:ec69651c8c21 1408 mbedLogger().eraseFile();
tnhnrl 57:ec69651c8c21 1409 }
tnhnrl 57:ec69651c8c21 1410
tnhnrl 58:94b7fd55185e 1411 else if (user_input == 'C' or user_input == 'c') {
tnhnrl 58:94b7fd55185e 1412
tnhnrl 58:94b7fd55185e 1413 pc().printf("\r\n\nCURRENT STATUS AND PARAMETERS:\r\n");
tnhnrl 58:94b7fd55185e 1414
tnhnrl 58:94b7fd55185e 1415 // Testing out ADC
tnhnrl 58:94b7fd55185e 1416 float vref = 5.6;
tnhnrl 58:94b7fd55185e 1417 float vmeasured = 0;
tnhnrl 58:94b7fd55185e 1418 unsigned int raw = adc().readCh5();
tnhnrl 58:94b7fd55185e 1419 vmeasured = ((float)raw)/4095.0*vref;
tnhnrl 58:94b7fd55185e 1420 pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
tnhnrl 58:94b7fd55185e 1421 pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
tnhnrl 58:94b7fd55185e 1422 pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
tnhnrl 58:94b7fd55185e 1423 pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
tnhnrl 58:94b7fd55185e 1424 pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
tnhnrl 58:94b7fd55185e 1425 pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
tnhnrl 58:94b7fd55185e 1426 pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
tnhnrl 58:94b7fd55185e 1427 pc().printf("raw board current: %d \r\n",adc().readCh7());
tnhnrl 58:94b7fd55185e 1428 pc().printf("raw BCE limit switch: %d \r\n",bce().getSwitch());
tnhnrl 58:94b7fd55185e 1429 pc().printf("raw BMM limit switch: %d \r\n",batt().getSwitch());
tnhnrl 58:94b7fd55185e 1430 pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
tnhnrl 58:94b7fd55185e 1431 // End of ADC Test
tnhnrl 58:94b7fd55185e 1432
tnhnrl 58:94b7fd55185e 1433 pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 58:94b7fd55185e 1434 pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 58:94b7fd55185e 1435 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 58:94b7fd55185e 1436 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 58:94b7fd55185e 1437 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 58:94b7fd55185e 1438 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 58:94b7fd55185e 1439 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 58:94b7fd55185e 1440 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 58:94b7fd55185e 1441
tnhnrl 58:94b7fd55185e 1442 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 1443 pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 58:94b7fd55185e 1444 pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 58:94b7fd55185e 1445 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 1446
tnhnrl 58:94b7fd55185e 1447 pc().printf("\r\n");
tnhnrl 58:94b7fd55185e 1448 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 1449 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 1450 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 1451 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 1452 }
tnhnrl 67:c86a4b464682 1453
tnhnrl 67:c86a4b464682 1454 else if (user_input == 'A' or user_input == 'a') {
tnhnrl 67:c86a4b464682 1455 _depth_command -= 0.5; //decrement the depth setpoint
tnhnrl 67:c86a4b464682 1456 depthLoop().setCommand(_depth_command);
tnhnrl 67:c86a4b464682 1457 pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand());
tnhnrl 67:c86a4b464682 1458 }
tnhnrl 67:c86a4b464682 1459 else if (user_input == 'S' or user_input == 's') {
tnhnrl 67:c86a4b464682 1460 _depth_command += 0.5; //increment the depth setpoint
tnhnrl 67:c86a4b464682 1461 depthLoop().setCommand(_depth_command);
tnhnrl 67:c86a4b464682 1462 pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand());
tnhnrl 67:c86a4b464682 1463 }
tnhnrl 65:2ac186553959 1464
tnhnrl 57:ec69651c8c21 1465 else if (user_input == '?') {
tnhnrl 57:ec69651c8c21 1466 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 57:ec69651c8c21 1467 wait(0.5);
tnhnrl 57:ec69651c8c21 1468 mbed_reset();
tnhnrl 57:ec69651c8c21 1469 }
tnhnrl 57:ec69651c8c21 1470
tnhnrl 67:c86a4b464682 1471 else if (user_input == '|') {
tnhnrl 67:c86a4b464682 1472 pc().printf("taring depth sensor\r\n");
tnhnrl 67:c86a4b464682 1473 pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 67:c86a4b464682 1474 wait(0.1);
tnhnrl 67:c86a4b464682 1475 depth().tare(); // tares to ambient (do on surface)
tnhnrl 67:c86a4b464682 1476 pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 67:c86a4b464682 1477 }
tnhnrl 67:c86a4b464682 1478
tnhnrl 57:ec69651c8c21 1479 //POSITION DIVE COMMANDS
tnhnrl 57:ec69651c8c21 1480 else if (user_input == 'k') {
tnhnrl 58:94b7fd55185e 1481 _BCE_dive_offset -= 1.0;
tnhnrl 58:94b7fd55185e 1482 pc().printf("Decreased BCE dive offset to %0.1f\r\n", _BCE_dive_offset);
tnhnrl 57:ec69651c8c21 1483 }
tnhnrl 57:ec69651c8c21 1484 else if (user_input == 'l') {
tnhnrl 58:94b7fd55185e 1485 _BCE_dive_offset += 1.0;
tnhnrl 58:94b7fd55185e 1486 pc().printf("Increased BCE dive offset to %0.1f\r\n", _BCE_dive_offset);
tnhnrl 57:ec69651c8c21 1487 }
tnhnrl 57:ec69651c8c21 1488 else if (user_input == ';') {
tnhnrl 58:94b7fd55185e 1489 _BMM_dive_offset -= 1.0;
tnhnrl 58:94b7fd55185e 1490 pc().printf("Decreased BMM dive offset to %0.1f\r\n", _BMM_dive_offset);
tnhnrl 57:ec69651c8c21 1491 }
tnhnrl 57:ec69651c8c21 1492 else if (user_input == '\'') {
tnhnrl 58:94b7fd55185e 1493 _BMM_dive_offset += 1.0;
tnhnrl 58:94b7fd55185e 1494 pc().printf("Increased BMM dive offset to %0.1f\r\n", _BMM_dive_offset);
tnhnrl 57:ec69651c8c21 1495 }
tnhnrl 57:ec69651c8c21 1496 //POSITION DIVE COMMANDS
tnhnrl 58:94b7fd55185e 1497
tnhnrl 58:94b7fd55185e 1498 else if (user_input == '9') {
tnhnrl 58:94b7fd55185e 1499 _heading_command -= 5.0; //decrement the rudder setpoint
tnhnrl 58:94b7fd55185e 1500 headingLoop().setCommand(_heading_command);
tnhnrl 58:94b7fd55185e 1501 pc().printf(">>> (-) new HEADING setpoint: %0.3f deg (-)\r\n", headingLoop().getCommand());
tnhnrl 58:94b7fd55185e 1502 }
tnhnrl 58:94b7fd55185e 1503 else if (user_input == '0') {
tnhnrl 58:94b7fd55185e 1504 _heading_command += 5.0; //increment the rudder setpoint
tnhnrl 58:94b7fd55185e 1505 headingLoop().setCommand(_heading_command);
tnhnrl 58:94b7fd55185e 1506 pc().printf(">>> (+) new HEADING setpoint: %0.3f deg (+)\r\n", headingLoop().getCommand());
tnhnrl 58:94b7fd55185e 1507 }
tnhnrl 58:94b7fd55185e 1508
tnhnrl 58:94b7fd55185e 1509 else if (user_input == '8') {
tnhnrl 58:94b7fd55185e 1510 keyboard_menu_STREAM_STATUS();
tnhnrl 58:94b7fd55185e 1511 }
tnhnrl 57:ec69651c8c21 1512
tnhnrl 57:ec69651c8c21 1513 else if (user_input == '*') {
tnhnrl 58:94b7fd55185e 1514 pc().printf("SWITCHING TO DEBUG MENU!\r\n");
tnhnrl 57:ec69651c8c21 1515 _debug_menu_on = true;
tnhnrl 57:ec69651c8c21 1516 wait(2);
tnhnrl 57:ec69651c8c21 1517 }
tnhnrl 16:3363b9f14913 1518 }
tnhnrl 57:ec69651c8c21 1519 /***************************** SIMPLE MENU *****************************/
tnhnrl 17:7c16b5671d0e 1520
tnhnrl 17:7c16b5671d0e 1521 //when you read the keyboard successfully, change the state
tnhnrl 28:16c83a2fdefa 1522 _state = _keyboard_state; //set state at the end of this function
tnhnrl 58:94b7fd55185e 1523 //pc().printf("\r\n\n ********* KEYBOARD STATE: %d *********\r\n\n", _state);
tnhnrl 16:3363b9f14913 1524 }
tnhnrl 16:3363b9f14913 1525 }
tnhnrl 52:f207567d3ea4 1526
tnhnrl 52:f207567d3ea4 1527 void StateMachine::keyboard_menu_STREAM_STATUS() {
tnhnrl 52:f207567d3ea4 1528 char STATUS_key;
tnhnrl 52:f207567d3ea4 1529
tnhnrl 52:f207567d3ea4 1530 // show the menu
tnhnrl 58:94b7fd55185e 1531 pc().printf("\r\n8: STATUS DEBUG MENU (EXIT WITH 'X' !)\r\n");
tnhnrl 52:f207567d3ea4 1532
tnhnrl 52:f207567d3ea4 1533 while (1) {
tnhnrl 52:f207567d3ea4 1534 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1535 STATUS_key = pc().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1536 }
tnhnrl 52:f207567d3ea4 1537
tnhnrl 52:f207567d3ea4 1538 else {
tnhnrl 52:f207567d3ea4 1539
tnhnrl 52:f207567d3ea4 1540 wait(1);
tnhnrl 54:d4990fb68404 1541
tnhnrl 58:94b7fd55185e 1542
tnhnrl 65:2ac186553959 1543 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 1544 //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 1545 //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 1546
tnhnrl 54:d4990fb68404 1547
tnhnrl 56:48a8a5a65b82 1548 //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 1549
tnhnrl 54:d4990fb68404 1550 //pc().printf("Neutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
tnhnrl 54:d4990fb68404 1551 // pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 54:d4990fb68404 1552 // pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 54:d4990fb68404 1553 // pc().printf("heading (rudder): %3.1f deg\r\n",rudderLoop().getPosition()); //for heading
tnhnrl 52:f207567d3ea4 1554
tnhnrl 54:d4990fb68404 1555 // pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 54:d4990fb68404 1556 // pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 54:d4990fb68404 1557 //
tnhnrl 54:d4990fb68404 1558 // pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 54:d4990fb68404 1559 // pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 52:f207567d3ea4 1560
tnhnrl 54:d4990fb68404 1561 // pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 54:d4990fb68404 1562 // pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 54:d4990fb68404 1563 //
tnhnrl 54:d4990fb68404 1564 // pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 54:d4990fb68404 1565 // pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 52:f207567d3ea4 1566
tnhnrl 52:f207567d3ea4 1567 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1568 }
tnhnrl 52:f207567d3ea4 1569
tnhnrl 52:f207567d3ea4 1570 // process the keys
tnhnrl 58:94b7fd55185e 1571 if (STATUS_key == 'X') {
tnhnrl 58:94b7fd55185e 1572 pc().printf("\r\nX: EXITING STATUS DEBUG MENU\r\n");
tnhnrl 52:f207567d3ea4 1573 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1574 }
tnhnrl 52:f207567d3ea4 1575
tnhnrl 52:f207567d3ea4 1576 else {
tnhnrl 58:94b7fd55185e 1577 pc().printf("\r\nThis key (%c) does nothing here. ", STATUS_key);
tnhnrl 52:f207567d3ea4 1578 }
tnhnrl 52:f207567d3ea4 1579 }
tnhnrl 52:f207567d3ea4 1580 }
tnhnrl 52:f207567d3ea4 1581
tnhnrl 52:f207567d3ea4 1582 void StateMachine::keyboard_menu_RUDDER_SERVO_settings() {
tnhnrl 52:f207567d3ea4 1583 //load current parameters from the rudder
tnhnrl 52:f207567d3ea4 1584 float rudder_min_pwm = rudder().getMinPWM();
tnhnrl 52:f207567d3ea4 1585 float rudder_max_pwm = rudder().getMaxPWM();
tnhnrl 52:f207567d3ea4 1586 float rudder_ctr_pwm = rudder().getCenterPWM();
tnhnrl 52:f207567d3ea4 1587 float rudder_min_deg = rudder().getMinDeg();
tnhnrl 52:f207567d3ea4 1588 float rudder_max_deg = rudder().getMaxDeg();
tnhnrl 52:f207567d3ea4 1589
tnhnrl 52:f207567d3ea4 1590 char RUDDER_PID_key;
tnhnrl 52:f207567d3ea4 1591
tnhnrl 52:f207567d3ea4 1592 // print the menu
tnhnrl 58:94b7fd55185e 1593 pc().printf("\r\nRUDDER (servo driver) settings (MENU)");
tnhnrl 58:94b7fd55185e 1594 pc().printf("\r\n(Adjust min/max/center PWM settings with the following keys: N and M and C");
tnhnrl 58:94b7fd55185e 1595 pc().printf("\r\n(Adjust DEGREE limit settings with the following keys: min = K, max = L");
tnhnrl 58:94b7fd55185e 1596 pc().printf("\r\n(Hit shift + X to exit w/o saving. Hit shift + S to save.\r\n");
tnhnrl 52:f207567d3ea4 1597 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 1598
tnhnrl 52:f207567d3ea4 1599 // handle the key presses
tnhnrl 52:f207567d3ea4 1600 while(1) {
tnhnrl 52:f207567d3ea4 1601 // get the user's keystroke from either of the two inputs
tnhnrl 52:f207567d3ea4 1602 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1603 RUDDER_PID_key = pc().getc();
tnhnrl 52:f207567d3ea4 1604 }
tnhnrl 52:f207567d3ea4 1605 else {
tnhnrl 52:f207567d3ea4 1606 wait(0.5);
tnhnrl 52:f207567d3ea4 1607 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 1608 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1609 }
tnhnrl 52:f207567d3ea4 1610
tnhnrl 52:f207567d3ea4 1611 // handle the user's key input
tnhnrl 52:f207567d3ea4 1612 if (RUDDER_PID_key == 'S') { // user wants to save the modified values
tnhnrl 52:f207567d3ea4 1613 // set global values
tnhnrl 52:f207567d3ea4 1614 rudder().setMinPWM(rudder_min_pwm);
tnhnrl 52:f207567d3ea4 1615 rudder().setMaxPWM(rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1616 rudder().setCenterPWM(rudder_ctr_pwm);
tnhnrl 52:f207567d3ea4 1617 rudder().setMinDeg(rudder_min_deg);
tnhnrl 52:f207567d3ea4 1618 rudder().setMaxDeg(rudder_max_deg);
tnhnrl 52:f207567d3ea4 1619
tnhnrl 52:f207567d3ea4 1620 // save rudder servo driver values for inner loop
tnhnrl 52:f207567d3ea4 1621 configFileIO().saveRudderData(rudder_min_deg, rudder_max_deg, rudder_ctr_pwm, rudder_min_pwm, rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1622 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 1623 }
tnhnrl 52:f207567d3ea4 1624 else if (RUDDER_PID_key == 'X') {
tnhnrl 52:f207567d3ea4 1625 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1626 }
tnhnrl 52:f207567d3ea4 1627 // MIN PWM
tnhnrl 52:f207567d3ea4 1628 else if (RUDDER_PID_key == 'N') {
tnhnrl 58:94b7fd55185e 1629 pc().printf(">> Type in rudder_min_pwm with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 1630 rudder_min_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1631 }
tnhnrl 52:f207567d3ea4 1632 // MAX PWM
tnhnrl 52:f207567d3ea4 1633 else if (RUDDER_PID_key == 'M') {
tnhnrl 58:94b7fd55185e 1634 pc().printf(">> Type in rudder_max_pwm with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 1635 rudder_max_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1636 }
tnhnrl 52:f207567d3ea4 1637 // CENTER PWM
tnhnrl 52:f207567d3ea4 1638 else if (RUDDER_PID_key == 'C') {
tnhnrl 58:94b7fd55185e 1639 pc().printf(">> Type in rudder_ctr_pwm with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 1640 rudder_ctr_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1641 }
tnhnrl 52:f207567d3ea4 1642 // MIN DEG
tnhnrl 52:f207567d3ea4 1643 else if (RUDDER_PID_key == 'K') {
tnhnrl 58:94b7fd55185e 1644 pc().printf(">> Type in rudder_min_deg with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 1645 rudder_min_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1646 }
tnhnrl 52:f207567d3ea4 1647 // MAX DEG
tnhnrl 52:f207567d3ea4 1648 else if (RUDDER_PID_key == 'L') {
tnhnrl 58:94b7fd55185e 1649 pc().printf(">> Type in rudder_max_deg with keyboard.\r\n");
tnhnrl 52:f207567d3ea4 1650 rudder_max_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1651 }
tnhnrl 52:f207567d3ea4 1652
tnhnrl 52:f207567d3ea4 1653 else {
tnhnrl 52:f207567d3ea4 1654 pc().printf("RUDDER SETUP: [%c] This key does nothing here. \r", RUDDER_PID_key);
tnhnrl 52:f207567d3ea4 1655 }
tnhnrl 52:f207567d3ea4 1656 }
tnhnrl 52:f207567d3ea4 1657 }
tnhnrl 54:d4990fb68404 1658
tnhnrl 58:94b7fd55185e 1659 void StateMachine::keyboard_menu_HEADING_PID_settings() {
tnhnrl 58:94b7fd55185e 1660 char HEADING_PID_key;
tnhnrl 58:94b7fd55185e 1661
tnhnrl 58:94b7fd55185e 1662 float heading_KP = headingLoop().getControllerP();
tnhnrl 58:94b7fd55185e 1663 float heading_KI = headingLoop().getControllerI();
tnhnrl 58:94b7fd55185e 1664 float heading_KD = headingLoop().getControllerD();
tnhnrl 58:94b7fd55185e 1665 float heading_offset_deg = headingLoop().getOutputOffset();
tnhnrl 58:94b7fd55185e 1666
tnhnrl 58:94b7fd55185e 1667 // print the menu
tnhnrl 58:94b7fd55185e 1668 pc().printf("\n\rHEADING (rudder outer loop) PID gain settings (MENU)");
tnhnrl 58:94b7fd55185e 1669 pc().printf("\n\r Adjust PID settings with the following keys: P and I and D");
tnhnrl 58:94b7fd55185e 1670 pc().printf("\n\r Adjust zero offset with O (oh)");
tnhnrl 58:94b7fd55185e 1671 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 63:6cb0405fc6e6 1672 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 1673
tnhnrl 58:94b7fd55185e 1674 // handle the key presses
tnhnrl 58:94b7fd55185e 1675 while(1) {
tnhnrl 58:94b7fd55185e 1676 // get the user's keystroke from either of the two inputs
tnhnrl 58:94b7fd55185e 1677 if (pc().readable()) {
tnhnrl 58:94b7fd55185e 1678 HEADING_PID_key = pc().getc();
tnhnrl 58:94b7fd55185e 1679 }
tnhnrl 58:94b7fd55185e 1680 else {
tnhnrl 58:94b7fd55185e 1681 continue; // didn't get a user input, so keep waiting for it
tnhnrl 58:94b7fd55185e 1682 }
tnhnrl 58:94b7fd55185e 1683
tnhnrl 58:94b7fd55185e 1684 // handle the user's key input
tnhnrl 58:94b7fd55185e 1685 if (HEADING_PID_key == 'S') { // user wants to save the modified values
tnhnrl 58:94b7fd55185e 1686 // set global values
tnhnrl 58:94b7fd55185e 1687 headingLoop().setControllerP(heading_KP);
tnhnrl 58:94b7fd55185e 1688 headingLoop().setControllerI(heading_KI);
tnhnrl 58:94b7fd55185e 1689 headingLoop().setControllerD(heading_KD);
tnhnrl 58:94b7fd55185e 1690 headingLoop().setOutputOffset(heading_offset_deg);
tnhnrl 58:94b7fd55185e 1691
tnhnrl 58:94b7fd55185e 1692 // save pitch PID values for outer loop (must save neutral position also)
tnhnrl 58:94b7fd55185e 1693 configFileIO().saveHeadingData(heading_KP, heading_KI, heading_KD, heading_offset_deg); //_neutral_heading_pos_deg);
tnhnrl 63:6cb0405fc6e6 1694 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 1695 }
tnhnrl 58:94b7fd55185e 1696 else if (HEADING_PID_key == 'X') {
tnhnrl 58:94b7fd55185e 1697 break; //exit the while loop
tnhnrl 58:94b7fd55185e 1698 }
tnhnrl 58:94b7fd55185e 1699
tnhnrl 58:94b7fd55185e 1700 else if (HEADING_PID_key == 'P') {
tnhnrl 58:94b7fd55185e 1701 heading_KP = getFloatUserInput();;
tnhnrl 58:94b7fd55185e 1702 }
tnhnrl 58:94b7fd55185e 1703 else if (HEADING_PID_key == 'I') {
tnhnrl 58:94b7fd55185e 1704 heading_KI = getFloatUserInput();
tnhnrl 58:94b7fd55185e 1705 }
tnhnrl 58:94b7fd55185e 1706 else if (HEADING_PID_key == 'D') {
tnhnrl 58:94b7fd55185e 1707 heading_KD = getFloatUserInput();
tnhnrl 58:94b7fd55185e 1708 }
tnhnrl 58:94b7fd55185e 1709 else if (HEADING_PID_key == 'O') {
tnhnrl 58:94b7fd55185e 1710 heading_offset_deg = getFloatUserInput();
tnhnrl 58:94b7fd55185e 1711 }
tnhnrl 58:94b7fd55185e 1712
tnhnrl 58:94b7fd55185e 1713 else {
tnhnrl 58:94b7fd55185e 1714 pc().printf("HEADING SETUP: [%c] This key does nothing here. \r", HEADING_PID_key);
tnhnrl 58:94b7fd55185e 1715 }
tnhnrl 58:94b7fd55185e 1716 }
tnhnrl 58:94b7fd55185e 1717 }
tnhnrl 58:94b7fd55185e 1718
tnhnrl 54:d4990fb68404 1719 void StateMachine::keyboard_menu_COUNTS_STATUS() {
tnhnrl 54:d4990fb68404 1720
tnhnrl 54:d4990fb68404 1721 }
tnhnrl 56:48a8a5a65b82 1722
tnhnrl 49:47ffa4feb6db 1723 void StateMachine::keyboard_menu_MANUAL_TUNING() {
tnhnrl 49:47ffa4feb6db 1724 char TUNING_key;
tnhnrl 49:47ffa4feb6db 1725
tnhnrl 49:47ffa4feb6db 1726 // show the menu
tnhnrl 58:94b7fd55185e 1727 pc().printf("\r\n7: MANUAL TUNING MENU (EXIT WITH 'X' !) (Pause and Unpause rudder ticker with P and U\n");
tnhnrl 58:94b7fd55185e 1728 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 1729 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 1730
tnhnrl 56:48a8a5a65b82 1731 //made these into internal parameters
tnhnrl 56:48a8a5a65b82 1732 float _tuning_bce_pos_mm = 300.0; //safe starting position
tnhnrl 56:48a8a5a65b82 1733 float _tuning_batt_pos_mm = 60.0; //safe starting position
tnhnrl 56:48a8a5a65b82 1734 float _tuning_rudder_pos_deg = 0.0; //safe starting position
tnhnrl 56:48a8a5a65b82 1735
tnhnrl 56:48a8a5a65b82 1736 //immediately start at those positions
tnhnrl 56:48a8a5a65b82 1737 bce().setPosition_mm(_tuning_bce_pos_mm);
tnhnrl 56:48a8a5a65b82 1738 batt().setPosition_mm(_tuning_batt_pos_mm);
tnhnrl 56:48a8a5a65b82 1739 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 49:47ffa4feb6db 1740
tnhnrl 49:47ffa4feb6db 1741 // what needs to be started?
tnhnrl 49:47ffa4feb6db 1742 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 1743 batt().unpause(); //this is now active
tnhnrl 56:48a8a5a65b82 1744 rudder().unpause();
tnhnrl 49:47ffa4feb6db 1745
tnhnrl 56:48a8a5a65b82 1746 while (1) {
tnhnrl 49:47ffa4feb6db 1747 if (pc().readable()) {
tnhnrl 49:47ffa4feb6db 1748 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 49:47ffa4feb6db 1749 }
tnhnrl 49:47ffa4feb6db 1750
tnhnrl 58:94b7fd55185e 1751 else {
tnhnrl 67:c86a4b464682 1752 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 1753 continue; // didn't get a user input, so keep waiting for it
tnhnrl 49:47ffa4feb6db 1754 }
tnhnrl 49:47ffa4feb6db 1755
tnhnrl 49:47ffa4feb6db 1756 // process the keys
tnhnrl 49:47ffa4feb6db 1757 if (TUNING_key == 'X') {
tnhnrl 49:47ffa4feb6db 1758 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 49:47ffa4feb6db 1759 bce().pause();
tnhnrl 49:47ffa4feb6db 1760 batt().pause();
tnhnrl 56:48a8a5a65b82 1761 rudder().pause();
tnhnrl 56:48a8a5a65b82 1762
tnhnrl 56:48a8a5a65b82 1763 //right now the rudder is always active................................................hmm
tnhnrl 56:48a8a5a65b82 1764 //deactivate the pin? new/delete?
tnhnrl 49:47ffa4feb6db 1765
tnhnrl 49:47ffa4feb6db 1766 break; //exit the while loop
tnhnrl 49:47ffa4feb6db 1767 }
tnhnrl 56:48a8a5a65b82 1768
tnhnrl 56:48a8a5a65b82 1769 //Buoyancy Engine
tnhnrl 52:f207567d3ea4 1770 else if (TUNING_key == 'A') {
tnhnrl 56:48a8a5a65b82 1771 _tuning_bce_pos_mm = _tuning_bce_pos_mm - 1.0;
tnhnrl 56:48a8a5a65b82 1772 bce().setPosition_mm(_tuning_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 58:94b7fd55185e 1773 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 1774 }
tnhnrl 52:f207567d3ea4 1775
tnhnrl 49:47ffa4feb6db 1776 else if (TUNING_key == 'S') {
tnhnrl 56:48a8a5a65b82 1777 _tuning_bce_pos_mm = _tuning_bce_pos_mm + 1.0;
tnhnrl 56:48a8a5a65b82 1778 bce().setPosition_mm(_tuning_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 58:94b7fd55185e 1779 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 1780 }
tnhnrl 49:47ffa4feb6db 1781
tnhnrl 56:48a8a5a65b82 1782 //BATTERY
tnhnrl 49:47ffa4feb6db 1783 else if (TUNING_key == 'Q') {
tnhnrl 56:48a8a5a65b82 1784 _tuning_batt_pos_mm = _tuning_batt_pos_mm - 1.0;
tnhnrl 56:48a8a5a65b82 1785 batt().setPosition_mm(_tuning_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 58:94b7fd55185e 1786 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 1787 }
tnhnrl 49:47ffa4feb6db 1788
tnhnrl 49:47ffa4feb6db 1789 else if (TUNING_key == 'W') {
tnhnrl 56:48a8a5a65b82 1790 _tuning_batt_pos_mm = _tuning_batt_pos_mm + 1.0;
tnhnrl 56:48a8a5a65b82 1791 batt().setPosition_mm(_tuning_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 58:94b7fd55185e 1792 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 1793 }
tnhnrl 56:48a8a5a65b82 1794
tnhnrl 56:48a8a5a65b82 1795 else if (TUNING_key == 'c' or TUNING_key == 'C') {
tnhnrl 56:48a8a5a65b82 1796 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 1797 }
tnhnrl 56:48a8a5a65b82 1798
tnhnrl 56:48a8a5a65b82 1799 //RUDER
tnhnrl 56:48a8a5a65b82 1800 else if (TUNING_key == 'E') {
tnhnrl 56:48a8a5a65b82 1801 _tuning_rudder_pos_deg = _tuning_rudder_pos_deg - 0.5;
tnhnrl 56:48a8a5a65b82 1802 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 58:94b7fd55185e 1803 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 1804 }
tnhnrl 56:48a8a5a65b82 1805
tnhnrl 56:48a8a5a65b82 1806 else if (TUNING_key == 'R') {
tnhnrl 56:48a8a5a65b82 1807 _tuning_rudder_pos_deg = _tuning_rudder_pos_deg + 0.5;
tnhnrl 56:48a8a5a65b82 1808 rudder().setPosition_deg(_tuning_rudder_pos_deg);
tnhnrl 58:94b7fd55185e 1809 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 1810 }
tnhnrl 56:48a8a5a65b82 1811
tnhnrl 56:48a8a5a65b82 1812 else if (TUNING_key == 'P') {
tnhnrl 56:48a8a5a65b82 1813 rudder().pause();
tnhnrl 56:48a8a5a65b82 1814 }
tnhnrl 56:48a8a5a65b82 1815
tnhnrl 56:48a8a5a65b82 1816 else if (TUNING_key == 'U') {
tnhnrl 56:48a8a5a65b82 1817 rudder().unpause();
tnhnrl 49:47ffa4feb6db 1818 }
tnhnrl 49:47ffa4feb6db 1819
tnhnrl 49:47ffa4feb6db 1820 else {
tnhnrl 58:94b7fd55185e 1821 pc().printf("\r\nMANUAL_TUNING: [%c] This key does nothing here. \r", TUNING_key);
tnhnrl 56:48a8a5a65b82 1822 }
tnhnrl 49:47ffa4feb6db 1823 }
tnhnrl 49:47ffa4feb6db 1824 }
tnhnrl 52:f207567d3ea4 1825
tnhnrl 52:f207567d3ea4 1826 void StateMachine::keyboard_menu_CHANNEL_READINGS() {
tnhnrl 52:f207567d3ea4 1827 char TUNING_key;
tnhnrl 52:f207567d3ea4 1828
tnhnrl 52:f207567d3ea4 1829 // show the menu
tnhnrl 58:94b7fd55185e 1830 pc().printf("\r\n8: CHANNEL READINGS (EXIT WITH 'X' !)");
tnhnrl 55:f4ec445c42fe 1831
tnhnrl 55:f4ec445c42fe 1832 while (1) {
tnhnrl 55:f4ec445c42fe 1833 if (pc().readable()) {
tnhnrl 55:f4ec445c42fe 1834 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 55:f4ec445c42fe 1835 }
tnhnrl 55:f4ec445c42fe 1836
tnhnrl 55:f4ec445c42fe 1837 // process the keys
tnhnrl 55:f4ec445c42fe 1838 if (TUNING_key == 'X') {
tnhnrl 55:f4ec445c42fe 1839 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 55:f4ec445c42fe 1840 bce().pause();
tnhnrl 55:f4ec445c42fe 1841 batt().pause();
tnhnrl 55:f4ec445c42fe 1842
tnhnrl 55:f4ec445c42fe 1843 break; //exit the while loop
tnhnrl 55:f4ec445c42fe 1844 }
tnhnrl 55:f4ec445c42fe 1845
tnhnrl 55:f4ec445c42fe 1846 else {
tnhnrl 55:f4ec445c42fe 1847 wait(0.5);
tnhnrl 55:f4ec445c42fe 1848 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 1849 continue; // didn't get a user input, so keep waiting for it
tnhnrl 55:f4ec445c42fe 1850 }
tnhnrl 55:f4ec445c42fe 1851 }
tnhnrl 55:f4ec445c42fe 1852 }
tnhnrl 55:f4ec445c42fe 1853
tnhnrl 55:f4ec445c42fe 1854 void StateMachine::keyboard_menu_POSITION_READINGS() {
tnhnrl 55:f4ec445c42fe 1855 char TUNING_key;
tnhnrl 55:f4ec445c42fe 1856
tnhnrl 55:f4ec445c42fe 1857 // show the menu
tnhnrl 58:94b7fd55185e 1858 pc().printf("\r\n9: BCE and BMM POSITION READINGS (EXIT WITH 'X' !)");
tnhnrl 52:f207567d3ea4 1859
tnhnrl 52:f207567d3ea4 1860 while (1) {
tnhnrl 52:f207567d3ea4 1861 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1862 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1863 }
tnhnrl 52:f207567d3ea4 1864
tnhnrl 54:d4990fb68404 1865 // process the keys
tnhnrl 54:d4990fb68404 1866 if (TUNING_key == 'X') {
tnhnrl 54:d4990fb68404 1867 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 54:d4990fb68404 1868 bce().pause();
tnhnrl 54:d4990fb68404 1869 batt().pause();
tnhnrl 54:d4990fb68404 1870
tnhnrl 54:d4990fb68404 1871 break; //exit the while loop
tnhnrl 54:d4990fb68404 1872 }
tnhnrl 54:d4990fb68404 1873
tnhnrl 52:f207567d3ea4 1874 else {
tnhnrl 52:f207567d3ea4 1875 // Testing out ADC
tnhnrl 54:d4990fb68404 1876 wait(0.5);
tnhnrl 52:f207567d3ea4 1877 float vref = 5.6;
tnhnrl 52:f207567d3ea4 1878 float vmeasured = 0;
tnhnrl 52:f207567d3ea4 1879 unsigned int raw = adc().readCh5();
tnhnrl 52:f207567d3ea4 1880 vmeasured = ((float)raw)/4095.0*vref;
tnhnrl 52:f207567d3ea4 1881
tnhnrl 54:d4990fb68404 1882
tnhnrl 55:f4ec445c42fe 1883 //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 1884
tnhnrl 55:f4ec445c42fe 1885 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 1886
tnhnrl 54:d4990fb68404 1887 //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 1888 //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 1889 //pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
tnhnrl 52:f207567d3ea4 1890 // End of ADC Test
tnhnrl 52:f207567d3ea4 1891
tnhnrl 54:d4990fb68404 1892 // pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
tnhnrl 54:d4990fb68404 1893 // pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
tnhnrl 54:d4990fb68404 1894 // pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
tnhnrl 54:d4990fb68404 1895 // pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
tnhnrl 54:d4990fb68404 1896 // pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
tnhnrl 54:d4990fb68404 1897 // pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
tnhnrl 54:d4990fb68404 1898 // pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
tnhnrl 54:d4990fb68404 1899 // pc().printf("raw board current: %d \r\n",adc().readCh7());
tnhnrl 54:d4990fb68404 1900
tnhnrl 52:f207567d3ea4 1901 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1902 }
tnhnrl 52:f207567d3ea4 1903 }
tnhnrl 52:f207567d3ea4 1904 }
tnhnrl 49:47ffa4feb6db 1905
tnhnrl 16:3363b9f14913 1906 void StateMachine::keyboard_menu_BCE_PID_settings() {
tnhnrl 16:3363b9f14913 1907 char PID_key;
tnhnrl 63:6cb0405fc6e6 1908
tnhnrl 63:6cb0405fc6e6 1909 float bce_KP = bce().getControllerP(); // load current value
tnhnrl 63:6cb0405fc6e6 1910 float bce_KI = bce().getControllerI(); // load current global value
tnhnrl 63:6cb0405fc6e6 1911 float bce_KD = bce().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1912
tnhnrl 16:3363b9f14913 1913 // show the menu
tnhnrl 63:6cb0405fc6e6 1914 pc().printf("\n\rBuoyancy Engine PID gain settings (MENU)");
tnhnrl 63:6cb0405fc6e6 1915 pc().printf("\n\r(Adjust PID settings with the following keys: P and I and D");
tnhnrl 63:6cb0405fc6e6 1916 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 63:6cb0405fc6e6 1917 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 1918
tnhnrl 63:6cb0405fc6e6 1919 // handle the key presses
tnhnrl 63:6cb0405fc6e6 1920 while(1) {
tnhnrl 63:6cb0405fc6e6 1921 // get the user's keystroke from either of the two inputs
tnhnrl 63:6cb0405fc6e6 1922 if (pc().readable()) {
tnhnrl 63:6cb0405fc6e6 1923 PID_key = pc().getc();
tnhnrl 63:6cb0405fc6e6 1924 }
tnhnrl 63:6cb0405fc6e6 1925 else {
tnhnrl 63:6cb0405fc6e6 1926 continue; // didn't get a user input, so keep waiting for it
tnhnrl 63:6cb0405fc6e6 1927 }
tnhnrl 63:6cb0405fc6e6 1928
tnhnrl 63:6cb0405fc6e6 1929 // handle the user's key input
tnhnrl 63:6cb0405fc6e6 1930 if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 63:6cb0405fc6e6 1931 // set values
tnhnrl 63:6cb0405fc6e6 1932 bce().setControllerP(bce_KP);
tnhnrl 63:6cb0405fc6e6 1933 bce().setControllerI(bce_KI);
tnhnrl 63:6cb0405fc6e6 1934 bce().setControllerD(bce_KD);
tnhnrl 63:6cb0405fc6e6 1935
tnhnrl 63:6cb0405fc6e6 1936 // save to "BATT.TXT" file
tnhnrl 63:6cb0405fc6e6 1937 configFileIO().saveBCEData(bce_KP, bce_KI, bce_KD);
tnhnrl 63:6cb0405fc6e6 1938 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 1939 }
tnhnrl 63:6cb0405fc6e6 1940 else if (PID_key == 'X') {
tnhnrl 63:6cb0405fc6e6 1941 break; //exit the while loop
tnhnrl 63:6cb0405fc6e6 1942 }
tnhnrl 63:6cb0405fc6e6 1943 else if (PID_key == 'P') {
tnhnrl 63:6cb0405fc6e6 1944 pc().printf(">> Type in proportional gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 1945 bce_KP = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 1946 }
tnhnrl 63:6cb0405fc6e6 1947 else if (PID_key == 'I') {
tnhnrl 63:6cb0405fc6e6 1948 pc().printf(">> Type in integral gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 1949 bce_KI = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 1950 }
tnhnrl 63:6cb0405fc6e6 1951 else if (PID_key == 'D') {
tnhnrl 63:6cb0405fc6e6 1952 pc().printf(">> Type in derivative gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 1953 bce_KD = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 1954 }
tnhnrl 63:6cb0405fc6e6 1955 else {
tnhnrl 63:6cb0405fc6e6 1956 pc().printf("\n\rBCE: [%c] This key does nothing here. \r", PID_key);
tnhnrl 63:6cb0405fc6e6 1957 }
tnhnrl 63:6cb0405fc6e6 1958 }
tnhnrl 63:6cb0405fc6e6 1959 }
tnhnrl 63:6cb0405fc6e6 1960
tnhnrl 63:6cb0405fc6e6 1961 void StateMachine::keyboard_menu_BATT_PID_settings() {
tnhnrl 63:6cb0405fc6e6 1962 char PID_key;
tnhnrl 63:6cb0405fc6e6 1963
tnhnrl 63:6cb0405fc6e6 1964 float batt_KP = batt().getControllerP(); // load current global value
tnhnrl 63:6cb0405fc6e6 1965 float batt_KI = batt().getControllerI(); // load current global value
tnhnrl 63:6cb0405fc6e6 1966 float batt_KD = batt().getControllerD(); // load current global value
tnhnrl 63:6cb0405fc6e6 1967
tnhnrl 63:6cb0405fc6e6 1968 // print the menu
tnhnrl 63:6cb0405fc6e6 1969 pc().printf("\n\rBattery Motor PID gain settings (MENU)");
tnhnrl 63:6cb0405fc6e6 1970 pc().printf("\n\r(Adjust PID settings with the following keys: P and I and D");
tnhnrl 63:6cb0405fc6e6 1971 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 63:6cb0405fc6e6 1972 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 1973
tnhnrl 16:3363b9f14913 1974 // handle the key presses
tnhnrl 16:3363b9f14913 1975 while(1) {
tnhnrl 16:3363b9f14913 1976 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1977 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1978 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1979 }
tnhnrl 16:3363b9f14913 1980 else {
tnhnrl 16:3363b9f14913 1981 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1982 }
tnhnrl 16:3363b9f14913 1983
tnhnrl 16:3363b9f14913 1984 // handle the user's key input
tnhnrl 63:6cb0405fc6e6 1985 if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 16:3363b9f14913 1986 // set values
tnhnrl 63:6cb0405fc6e6 1987 batt().setControllerP(batt_KP);
tnhnrl 63:6cb0405fc6e6 1988 batt().setControllerI(batt_KI);
tnhnrl 63:6cb0405fc6e6 1989 batt().setControllerD(batt_KD);
tnhnrl 16:3363b9f14913 1990
tnhnrl 38:83d06c294807 1991 // save to "BATT.TXT" file
tnhnrl 63:6cb0405fc6e6 1992 configFileIO().saveBattData(batt_KP, batt_KI, batt_KD);
tnhnrl 63:6cb0405fc6e6 1993 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 1994 }
tnhnrl 16:3363b9f14913 1995 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1996 break; //exit the while loop
tnhnrl 16:3363b9f14913 1997 }
tnhnrl 63:6cb0405fc6e6 1998 else if (PID_key == 'P') {
tnhnrl 63:6cb0405fc6e6 1999 pc().printf(">> Type in proportional gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2000 batt_KP = getFloatUserInput();
tnhnrl 16:3363b9f14913 2001 }
tnhnrl 63:6cb0405fc6e6 2002 else if (PID_key == 'I') {
tnhnrl 63:6cb0405fc6e6 2003 pc().printf(">> Type in integral gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2004 batt_KI = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2005 }
tnhnrl 63:6cb0405fc6e6 2006 else if (PID_key == 'D') {
tnhnrl 63:6cb0405fc6e6 2007 pc().printf(">> Type in derivative gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2008 batt_KD = getFloatUserInput();
tnhnrl 16:3363b9f14913 2009 }
tnhnrl 16:3363b9f14913 2010 else {
tnhnrl 63:6cb0405fc6e6 2011 pc().printf("\n\rBATT: [%c] This key does nothing here. \r", PID_key);
tnhnrl 16:3363b9f14913 2012 }
tnhnrl 16:3363b9f14913 2013 }
tnhnrl 16:3363b9f14913 2014 }
tnhnrl 20:8987a9ae2bc7 2015
tnhnrl 16:3363b9f14913 2016 void StateMachine::keyboard_menu_DEPTH_PID_settings() {
tnhnrl 16:3363b9f14913 2017 char PID_key;
tnhnrl 63:6cb0405fc6e6 2018
tnhnrl 63:6cb0405fc6e6 2019 float depth_KP = depthLoop().getControllerP(); // load current depth value
tnhnrl 63:6cb0405fc6e6 2020 float depth_KI = depthLoop().getControllerI(); // load current depth value
tnhnrl 63:6cb0405fc6e6 2021 float depth_KD = depthLoop().getControllerD(); // load current depth value
tnhnrl 16:3363b9f14913 2022
tnhnrl 63:6cb0405fc6e6 2023 // print the menu
tnhnrl 63:6cb0405fc6e6 2024 pc().printf("\n\rDEPTH (Buoyancy Engine O.L.) PID gain settings (MENU)");
tnhnrl 63:6cb0405fc6e6 2025 pc().printf("\n\r(Adjust PID settings with the following keys: P and I and D");
tnhnrl 63:6cb0405fc6e6 2026 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 63:6cb0405fc6e6 2027 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 2028
tnhnrl 16:3363b9f14913 2029 // handle the key presses
tnhnrl 16:3363b9f14913 2030 while(1) {
tnhnrl 16:3363b9f14913 2031 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 2032 if (pc().readable()) {
tnhnrl 16:3363b9f14913 2033 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 2034 }
tnhnrl 16:3363b9f14913 2035 else {
tnhnrl 16:3363b9f14913 2036 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 2037 }
tnhnrl 16:3363b9f14913 2038
tnhnrl 16:3363b9f14913 2039 // handle the user's key input
tnhnrl 63:6cb0405fc6e6 2040 if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 63:6cb0405fc6e6 2041 // set values
tnhnrl 63:6cb0405fc6e6 2042 depthLoop().setControllerP(depth_KP);
tnhnrl 63:6cb0405fc6e6 2043 depthLoop().setControllerI(depth_KI);
tnhnrl 63:6cb0405fc6e6 2044 depthLoop().setControllerD(depth_KD);
tnhnrl 63:6cb0405fc6e6 2045
tnhnrl 63:6cb0405fc6e6 2046 // save to "DEPTH.TXT" file
tnhnrl 63:6cb0405fc6e6 2047 configFileIO().saveDepthData(depth_KP, depth_KI, depth_KD, _neutral_bce_pos_mm);
tnhnrl 63:6cb0405fc6e6 2048 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 2049
tnhnrl 63:6cb0405fc6e6 2050 //set class variables that will be used in find neutral sequence
tnhnrl 63:6cb0405fc6e6 2051 _depth_KP = depthLoop().getControllerP(); // load current depth value
tnhnrl 63:6cb0405fc6e6 2052 _depth_KI = depthLoop().getControllerI(); // load current depth value
tnhnrl 63:6cb0405fc6e6 2053 _depth_KD = depthLoop().getControllerD(); // load current depth value
tnhnrl 16:3363b9f14913 2054 }
tnhnrl 16:3363b9f14913 2055 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 2056 break; //exit the while loop
tnhnrl 16:3363b9f14913 2057 }
tnhnrl 63:6cb0405fc6e6 2058 else if (PID_key == 'P') {
tnhnrl 63:6cb0405fc6e6 2059 pc().printf(">> Type in proportional gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2060 depth_KP = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2061 }
tnhnrl 63:6cb0405fc6e6 2062 else if (PID_key == 'I') {
tnhnrl 63:6cb0405fc6e6 2063 pc().printf(">> Type in integral gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2064 depth_KI = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2065 }
tnhnrl 63:6cb0405fc6e6 2066 else if (PID_key == 'D') {
tnhnrl 63:6cb0405fc6e6 2067 pc().printf(">> Type in derivative gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2068 depth_KD = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2069 }
tnhnrl 16:3363b9f14913 2070 else {
tnhnrl 63:6cb0405fc6e6 2071 pc().printf("\n\rDEPTH: [%c] This key does nothing here. \r", PID_key);
tnhnrl 16:3363b9f14913 2072 }
tnhnrl 16:3363b9f14913 2073 }
tnhnrl 16:3363b9f14913 2074 }
tnhnrl 16:3363b9f14913 2075
tnhnrl 16:3363b9f14913 2076 void StateMachine::keyboard_menu_PITCH_PID_settings() {
tnhnrl 16:3363b9f14913 2077 char PID_key;
tnhnrl 63:6cb0405fc6e6 2078
tnhnrl 63:6cb0405fc6e6 2079 float pitch_KP = pitchLoop().getControllerP(); // load current pitch value
tnhnrl 63:6cb0405fc6e6 2080 float pitch_KI = pitchLoop().getControllerI(); // load current pitch value
tnhnrl 63:6cb0405fc6e6 2081 float pitch_KD = pitchLoop().getControllerD(); // load current pitch value
tnhnrl 16:3363b9f14913 2082
tnhnrl 16:3363b9f14913 2083 // print the menu
tnhnrl 63:6cb0405fc6e6 2084 pc().printf("\n\rPITCH (Battery Motor O.L.) PID gain settings (MENU)");
tnhnrl 63:6cb0405fc6e6 2085 pc().printf("\n\r(Adjust PID settings with the following keys: P and I and D");
tnhnrl 63:6cb0405fc6e6 2086 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 63:6cb0405fc6e6 2087 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 2088
tnhnrl 16:3363b9f14913 2089 // handle the key presses
tnhnrl 16:3363b9f14913 2090 while(1) {
tnhnrl 16:3363b9f14913 2091 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 2092 if (pc().readable()) {
tnhnrl 16:3363b9f14913 2093 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 2094 }
tnhnrl 16:3363b9f14913 2095 else {
tnhnrl 16:3363b9f14913 2096 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 2097 }
tnhnrl 63:6cb0405fc6e6 2098
tnhnrl 16:3363b9f14913 2099 // handle the user's key input
tnhnrl 63:6cb0405fc6e6 2100 if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 63:6cb0405fc6e6 2101 // set values
tnhnrl 63:6cb0405fc6e6 2102 pitchLoop().setControllerP(pitch_KP);
tnhnrl 63:6cb0405fc6e6 2103 pitchLoop().setControllerI(pitch_KI);
tnhnrl 63:6cb0405fc6e6 2104 pitchLoop().setControllerD(pitch_KD);
tnhnrl 63:6cb0405fc6e6 2105
tnhnrl 63:6cb0405fc6e6 2106 // save to "PITCH.TXT" file
tnhnrl 63:6cb0405fc6e6 2107 configFileIO().savePitchData(pitch_KP, pitch_KI, pitch_KD, _neutral_batt_pos_mm);
tnhnrl 63:6cb0405fc6e6 2108 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 2109
tnhnrl 63:6cb0405fc6e6 2110 _pitch_KP = pitchLoop().getControllerP(); // load current pitch value
tnhnrl 63:6cb0405fc6e6 2111 _pitch_KI = pitchLoop().getControllerI(); // load current pitch value
tnhnrl 63:6cb0405fc6e6 2112 _pitch_KD = pitchLoop().getControllerD(); // load current pitch value
tnhnrl 16:3363b9f14913 2113 }
tnhnrl 16:3363b9f14913 2114 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 2115 break; //exit the while loop
tnhnrl 16:3363b9f14913 2116 }
tnhnrl 63:6cb0405fc6e6 2117 else if (PID_key == 'P') {
tnhnrl 63:6cb0405fc6e6 2118 pc().printf(">> Type in proportional gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2119 pitch_KP = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2120 }
tnhnrl 63:6cb0405fc6e6 2121 else if (PID_key == 'I') {
tnhnrl 63:6cb0405fc6e6 2122 pc().printf(">> Type in integral gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2123 pitch_KI = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2124 }
tnhnrl 63:6cb0405fc6e6 2125 else if (PID_key == 'D') {
tnhnrl 63:6cb0405fc6e6 2126 pc().printf(">> Type in derivative gain with keyboard.\n\r");
tnhnrl 63:6cb0405fc6e6 2127 pitch_KD = getFloatUserInput();
tnhnrl 63:6cb0405fc6e6 2128 }
tnhnrl 16:3363b9f14913 2129 else {
tnhnrl 63:6cb0405fc6e6 2130 pc().printf("\n\rPITCH: [%c] This key does nothing here. \r", PID_key);
tnhnrl 16:3363b9f14913 2131 }
tnhnrl 16:3363b9f14913 2132 }
tnhnrl 16:3363b9f14913 2133 }
tnhnrl 20:8987a9ae2bc7 2134
tnhnrl 16:3363b9f14913 2135 float StateMachine::getDepthCommand() {
tnhnrl 32:f2f8ae34aadc 2136 return _depth_command;
tnhnrl 16:3363b9f14913 2137 }
tnhnrl 20:8987a9ae2bc7 2138
tnhnrl 16:3363b9f14913 2139 float StateMachine::getPitchCommand() {
tnhnrl 32:f2f8ae34aadc 2140 return _pitch_command;
tnhnrl 32:f2f8ae34aadc 2141 }
tnhnrl 32:f2f8ae34aadc 2142
tnhnrl 32:f2f8ae34aadc 2143 float StateMachine::getDepthReading() {
tnhnrl 32:f2f8ae34aadc 2144 return _depth_reading;
tnhnrl 32:f2f8ae34aadc 2145 }
tnhnrl 32:f2f8ae34aadc 2146
tnhnrl 32:f2f8ae34aadc 2147 float StateMachine::getPitchReading() {
tnhnrl 32:f2f8ae34aadc 2148 return _pitch_reading;
tnhnrl 32:f2f8ae34aadc 2149 }
tnhnrl 32:f2f8ae34aadc 2150
tnhnrl 32:f2f8ae34aadc 2151 float StateMachine::getTimerReading() {
tnhnrl 32:f2f8ae34aadc 2152 return _timer_reading;
tnhnrl 17:7c16b5671d0e 2153 }
tnhnrl 28:16c83a2fdefa 2154
tnhnrl 17:7c16b5671d0e 2155 void StateMachine::setState(int input_state) {
tnhnrl 21:38c8544db6f4 2156 _state = input_state;
tnhnrl 17:7c16b5671d0e 2157 }
tnhnrl 20:8987a9ae2bc7 2158
tnhnrl 17:7c16b5671d0e 2159 int StateMachine::getState() {
tnhnrl 17:7c16b5671d0e 2160 return _state; //return the current state of the system
tnhnrl 17:7c16b5671d0e 2161 }
tnhnrl 20:8987a9ae2bc7 2162
tnhnrl 17:7c16b5671d0e 2163 void StateMachine::setTimeout(float input_timeout) {
tnhnrl 17:7c16b5671d0e 2164 _timeout = input_timeout;
tnhnrl 17:7c16b5671d0e 2165 }
tnhnrl 20:8987a9ae2bc7 2166
tnhnrl 17:7c16b5671d0e 2167 void StateMachine::setDepthCommand(float input_depth_command) {
tnhnrl 32:f2f8ae34aadc 2168 _depth_command = input_depth_command;
tnhnrl 17:7c16b5671d0e 2169 }
tnhnrl 20:8987a9ae2bc7 2170
tnhnrl 17:7c16b5671d0e 2171 void StateMachine::setPitchCommand(float input_pitch_command) {
tnhnrl 32:f2f8ae34aadc 2172 _pitch_command = input_pitch_command;
tnhnrl 17:7c16b5671d0e 2173 }
tnhnrl 20:8987a9ae2bc7 2174
tnhnrl 17:7c16b5671d0e 2175 void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) {
tnhnrl 21:38c8544db6f4 2176 _neutral_batt_pos_mm = batt_pos_mm;
tnhnrl 21:38c8544db6f4 2177 _neutral_bce_pos_mm = bce_pos_mm;
tnhnrl 17:7c16b5671d0e 2178
tnhnrl 58:94b7fd55185e 2179 pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\r\n",_neutral_batt_pos_mm,_neutral_bce_pos_mm);
tnhnrl 17:7c16b5671d0e 2180 }
tnhnrl 20:8987a9ae2bc7 2181
tnhnrl 17:7c16b5671d0e 2182 int StateMachine::timeoutRunning() {
tnhnrl 28:16c83a2fdefa 2183 return _isTimeoutRunning;
tnhnrl 17:7c16b5671d0e 2184 }
tnhnrl 20:8987a9ae2bc7 2185
tnhnrl 17:7c16b5671d0e 2186 //process one state at a time
tnhnrl 17:7c16b5671d0e 2187 void StateMachine::getDiveSequence() {
tnhnrl 17:7c16b5671d0e 2188 //iterate through this sequence using the FSM
tnhnrl 24:c7d9b5bf3829 2189 currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state;
tnhnrl 24:c7d9b5bf3829 2190 currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout;
tnhnrl 24:c7d9b5bf3829 2191 currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth;
tnhnrl 24:c7d9b5bf3829 2192 currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch;
tnhnrl 17:7c16b5671d0e 2193
tnhnrl 17:7c16b5671d0e 2194 _timeout = currentStateStruct.timeout; //set timeout before exiting this function
tnhnrl 32:f2f8ae34aadc 2195 }
tnhnrl 32:f2f8ae34aadc 2196
tnhnrl 32:f2f8ae34aadc 2197 void StateMachine::printCurrentSdLog() {
tnhnrl 58:94b7fd55185e 2198 pc().printf("SD card log work in progress\r\n");
tnhnrl 32:f2f8ae34aadc 2199 //might be worth saving the last few logs to the MBED...
tnhnrl 32:f2f8ae34aadc 2200 }
tnhnrl 32:f2f8ae34aadc 2201
tnhnrl 32:f2f8ae34aadc 2202 //check if the file is still opened
tnhnrl 32:f2f8ae34aadc 2203 void StateMachine::createNewFile() {
tnhnrl 32:f2f8ae34aadc 2204 if (_file_closed) {
tnhnrl 34:9b66c5188051 2205 //mbedLogger().createFile(); //create a new MBED file
tnhnrl 32:f2f8ae34aadc 2206
tnhnrl 32:f2f8ae34aadc 2207 _file_closed = false; //file is still open until you get to SIT_IDLE
tnhnrl 32:f2f8ae34aadc 2208 }
tnhnrl 32:f2f8ae34aadc 2209 }
tnhnrl 32:f2f8ae34aadc 2210
tnhnrl 32:f2f8ae34aadc 2211 void StateMachine::transmitData() {
tnhnrl 32:f2f8ae34aadc 2212 static float transmit_timer = 0;
tnhnrl 32:f2f8ae34aadc 2213 static bool is_transmit_timer_running = false;
tnhnrl 32:f2f8ae34aadc 2214
tnhnrl 32:f2f8ae34aadc 2215 if (!is_transmit_timer_running) {
tnhnrl 58:94b7fd55185e 2216 //pc().printf("\r\n\nTRANSMIT timer running...\r\n\n"); //debug
tnhnrl 32:f2f8ae34aadc 2217
tnhnrl 32:f2f8ae34aadc 2218 transmit_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 32:f2f8ae34aadc 2219 is_transmit_timer_running = true; //disable this block after one iteration
tnhnrl 32:f2f8ae34aadc 2220
tnhnrl 58:94b7fd55185e 2221 pc().printf("TESTING to see if this transmits once a second. (timer: %0.1f)\r\n", timer.read());
tnhnrl 32:f2f8ae34aadc 2222 }
tnhnrl 32:f2f8ae34aadc 2223 if (timer.read() >= transmit_timer) {
tnhnrl 32:f2f8ae34aadc 2224 is_transmit_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 32:f2f8ae34aadc 2225 }
tnhnrl 34:9b66c5188051 2226 }
tnhnrl 34:9b66c5188051 2227
tnhnrl 36:966a86937e17 2228 float * StateMachine::dataArray() {
tnhnrl 36:966a86937e17 2229 //return the array to a calling function
tnhnrl 36:966a86937e17 2230 return _data_log;
tnhnrl 52:f207567d3ea4 2231 }
tnhnrl 52:f207567d3ea4 2232
tnhnrl 52:f207567d3ea4 2233 // 06/06/2018
tnhnrl 52:f207567d3ea4 2234 float StateMachine::getFloatUserInput() {
tnhnrl 52:f207567d3ea4 2235 float float_conversion = 0.0;
tnhnrl 52:f207567d3ea4 2236
tnhnrl 52:f207567d3ea4 2237 while(1) {
tnhnrl 52:f207567d3ea4 2238 bool valid_input = false; //flag for valid or invalid input
tnhnrl 52:f207567d3ea4 2239
tnhnrl 58:94b7fd55185e 2240 pc().printf("\n\rPlease enter your number below and press ENTER:\r\n");
tnhnrl 52:f207567d3ea4 2241 char user_string [80]; //variable to store input as a character array
tnhnrl 52:f207567d3ea4 2242
tnhnrl 52:f207567d3ea4 2243 pc().scanf("%s", user_string); //read formatted data from stdin
tnhnrl 58:94b7fd55185e 2244 pc().printf("\n\n\ruser_string was <%s>\r\n", user_string);
tnhnrl 52:f207567d3ea4 2245
tnhnrl 52:f207567d3ea4 2246 //check through the string for invalid characters (decimal values 43 through 57)
tnhnrl 52:f207567d3ea4 2247 for (int c = 0; c < strlen(user_string); c++) {
tnhnrl 58:94b7fd55185e 2248 //pc().printf("character is [%c]\r\n", user_string[c]); //debug
tnhnrl 52:f207567d3ea4 2249 if (user_string[c] >= 43 and user_string[c] <= 57) {
tnhnrl 58:94b7fd55185e 2250 //pc().printf("VALID CHARACTER!\r\n"); //debug
tnhnrl 52:f207567d3ea4 2251 ;
tnhnrl 52:f207567d3ea4 2252 }
tnhnrl 52:f207567d3ea4 2253 else {
tnhnrl 58:94b7fd55185e 2254 pc().printf("INVALID INPUT!\r\n");
tnhnrl 52:f207567d3ea4 2255 break;
tnhnrl 52:f207567d3ea4 2256 }
tnhnrl 52:f207567d3ea4 2257
tnhnrl 52:f207567d3ea4 2258 if (c == (strlen(user_string) - 1)) {
tnhnrl 52:f207567d3ea4 2259 valid_input = true;
tnhnrl 52:f207567d3ea4 2260 }
tnhnrl 52:f207567d3ea4 2261 }
tnhnrl 52:f207567d3ea4 2262
tnhnrl 52:f207567d3ea4 2263 if (valid_input) {
tnhnrl 52:f207567d3ea4 2264 float_conversion = atof(user_string);
tnhnrl 63:6cb0405fc6e6 2265 pc().printf("VALID INPUT! Your input was: %3.3f (PRESS \"S\" (shift + S) to save!)\r\n", float_conversion);
tnhnrl 52:f207567d3ea4 2266 break;
tnhnrl 52:f207567d3ea4 2267 }
tnhnrl 52:f207567d3ea4 2268 }
tnhnrl 52:f207567d3ea4 2269
tnhnrl 52:f207567d3ea4 2270 return float_conversion;
tnhnrl 67:c86a4b464682 2271 }
tnhnrl 67:c86a4b464682 2272
tnhnrl 67:c86a4b464682 2273 //added because channel readings failed multiple times (if they fail, motors could break parts)
tnhnrl 67:c86a4b464682 2274 void StateMachine::checkMotorPositions() {
tnhnrl 67:c86a4b464682 2275
tnhnrl 67:c86a4b464682 2276 /* 1. If channel readings fail, calculations will give negative positions
tnhnrl 67:c86a4b464682 2277 2. If the potentiometers are reporting negative positions, send the system into float broadcast
tnhnrl 67:c86a4b464682 2278 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 2279
tnhnrl 67:c86a4b464682 2280 if (bce().getPosition_mm() < 0 or batt().getPosition_mm() < 0 ) {
tnhnrl 67:c86a4b464682 2281 //_state = FLOAT_BROADCAST;
tnhnrl 67:c86a4b464682 2282 }
tnhnrl 16:3363b9f14913 2283 }