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:
Thu Jun 07 13:02:08 2018 +0000
Revision:
53:c0586fe62b01
Parent:
52:f207567d3ea4
Child:
54:d4990fb68404
motors working in correct directions

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 17:7c16b5671d0e 14
tnhnrl 28:16c83a2fdefa 15 _neutral_timer = 0; //timer used in FIND_NEUTRAL sub-FSM
tnhnrl 20:8987a9ae2bc7 16
tnhnrl 28:16c83a2fdefa 17 _state = SIT_IDLE; // select starting state here
tnhnrl 28:16c83a2fdefa 18 _isTimeoutRunning = false; // default timer to not running
tnhnrl 28:16c83a2fdefa 19 _isSubStateTimerRunning = false; // default timer to not running
tnhnrl 17:7c16b5671d0e 20
tnhnrl 24:c7d9b5bf3829 21 _multi_dive_counter = 0;
tnhnrl 21:38c8544db6f4 22
tnhnrl 21:38c8544db6f4 23 _neutral_sub_state_active = false;
tnhnrl 17:7c16b5671d0e 24
tnhnrl 21:38c8544db6f4 25 _depth_KP = depthLoop().getControllerP(); // load current depth value
tnhnrl 21:38c8544db6f4 26 _depth_KI = depthLoop().getControllerI(); // load current depth value
tnhnrl 21:38c8544db6f4 27 _depth_KD = depthLoop().getControllerD(); // load current depth value
tnhnrl 21:38c8544db6f4 28
tnhnrl 21:38c8544db6f4 29 _pitch_KP = pitchLoop().getControllerP(); // load current pitch value
tnhnrl 21:38c8544db6f4 30 _pitch_KI = pitchLoop().getControllerI(); // load current pitch value
tnhnrl 21:38c8544db6f4 31 _pitch_KD = pitchLoop().getControllerD(); // load current pitch value
tnhnrl 21:38c8544db6f4 32
tnhnrl 21:38c8544db6f4 33 _neutral_bce_pos_mm = depthLoop().getOutputOffset(); //load current neutral buoyancy position offset
tnhnrl 21:38c8544db6f4 34 _neutral_batt_pos_mm = pitchLoop().getOutputOffset(); //load current neutral buoyancy position offset
tnhnrl 23:434f04ef1fad 35
tnhnrl 28:16c83a2fdefa 36 _state_array_counter = 1; //used to iterate through and record states
tnhnrl 28:16c83a2fdefa 37 _substate_array_counter = 0; //used to iterate through and record substates
tnhnrl 28:16c83a2fdefa 38
tnhnrl 28:16c83a2fdefa 39 _state_array[0] = SIT_IDLE; //system starts in the SIT_IDLE state, record this
tnhnrl 24:c7d9b5bf3829 40
tnhnrl 30:2964617e7676 41 _substate = NEUTRAL_SINKING; //start sub-FSM in NEUTRAL_SINKING
tnhnrl 28:16c83a2fdefa 42 _previous_substate = -1; //to start sub-FSM
tnhnrl 28:16c83a2fdefa 43 _previous_state = -1; //for tracking FSM states
tnhnrl 28:16c83a2fdefa 44
tnhnrl 28:16c83a2fdefa 45 _max_recorded_depth_neutral = -99; //float to record max depth
tnhnrl 28:16c83a2fdefa 46 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 32:f2f8ae34aadc 47
tnhnrl 32:f2f8ae34aadc 48 _neutral_sink_command_mm = -2.5; //defaults for neutral finding sub-FSM
tnhnrl 32:f2f8ae34aadc 49 _neutral_rise_command_mm = 2.0;
tnhnrl 32:f2f8ae34aadc 50 _neutral_pitch_command_mm = 0.5;
tnhnrl 32:f2f8ae34aadc 51
tnhnrl 32:f2f8ae34aadc 52 _max_recorded_auto_neutral_depth = -99;
tnhnrl 32:f2f8ae34aadc 53
tnhnrl 32:f2f8ae34aadc 54 _file_closed = true;
tnhnrl 16:3363b9f14913 55 }
tnhnrl 20:8987a9ae2bc7 56
tnhnrl 17:7c16b5671d0e 57 //Finite State Machine (FSM)
tnhnrl 45:16b8162188ca 58 int StateMachine::runStateMachine() {
tnhnrl 45:16b8162188ca 59 static int transmit_packet_number = 1; //for data transmission
tnhnrl 45:16b8162188ca 60
tnhnrl 16:3363b9f14913 61 // finite state machine ... each state has at least one exit criteria
tnhnrl 17:7c16b5671d0e 62 switch (_state) {
tnhnrl 16:3363b9f14913 63 case SIT_IDLE :
tnhnrl 28:16c83a2fdefa 64 case KEYBOARD:
tnhnrl 16:3363b9f14913 65 // there actually is no timeout for SIT_IDLE, but this enables some one-shot actions
tnhnrl 28:16c83a2fdefa 66 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 67 showMenu();
tnhnrl 16:3363b9f14913 68 pc().printf("\r\n\nstate: SIT_IDLE\r\n");
tnhnrl 28:16c83a2fdefa 69 _isTimeoutRunning = true;
tnhnrl 20:8987a9ae2bc7 70
tnhnrl 16:3363b9f14913 71 // what is active?
tnhnrl 16:3363b9f14913 72 bce().pause();
tnhnrl 16:3363b9f14913 73 batt().pause();
tnhnrl 32:f2f8ae34aadc 74
tnhnrl 17:7c16b5671d0e 75 //reset sub FSM
tnhnrl 28:16c83a2fdefa 76 _isSubStateTimerRunning = false;
tnhnrl 32:f2f8ae34aadc 77
tnhnrl 32:f2f8ae34aadc 78 //close the MBED file
tnhnrl 32:f2f8ae34aadc 79 _file_closed = true;
tnhnrl 16:3363b9f14913 80 }
tnhnrl 20:8987a9ae2bc7 81
tnhnrl 16:3363b9f14913 82 // how exit?
tnhnrl 20:8987a9ae2bc7 83 keyboard(); // keyboard function will change the state if needed
tnhnrl 16:3363b9f14913 84 break;
tnhnrl 49:47ffa4feb6db 85
tnhnrl 49:47ffa4feb6db 86 case CHECK_TUNING : // state used to check the tuning of the pressure vessel
tnhnrl 49:47ffa4feb6db 87 // start local state timer and init any other one-shot actions
tnhnrl 49:47ffa4feb6db 88 if (!_isTimeoutRunning) {
tnhnrl 49:47ffa4feb6db 89 pc().printf("\r\n\nstate: CHECK_TUNING\r\n");
tnhnrl 49:47ffa4feb6db 90 timer.reset(); // timer goes back to zero
tnhnrl 49:47ffa4feb6db 91 timer.start(); // background timer starts running
tnhnrl 49:47ffa4feb6db 92 _isTimeoutRunning = true;
tnhnrl 49:47ffa4feb6db 93
tnhnrl 49:47ffa4feb6db 94 // what needs to be started?
tnhnrl 49:47ffa4feb6db 95 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 96 batt().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 97
tnhnrl 49:47ffa4feb6db 98 // what are the commands? (DRIVE THE MOTORS "DIRECTLY")
tnhnrl 49:47ffa4feb6db 99 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 100 batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 101
tnhnrl 49:47ffa4feb6db 102 // getSetPosition_mm is the commanded position in the LinearActuator class
tnhnrl 49:47ffa4feb6db 103
tnhnrl 49:47ffa4feb6db 104 pc().printf("CHECK_TUNING: BCE cmd: %3.1f (BCE current position: %3.1f)\r\n", bce().getSetPosition_mm(), bce().getPosition_mm());
tnhnrl 49:47ffa4feb6db 105 pc().printf("CHECK_TUNING: BATT cmd: %3.1f (BATT current position: %3.1f)\r\n", batt().getSetPosition_mm(), bce().getPosition_mm());
tnhnrl 49:47ffa4feb6db 106
tnhnrl 49:47ffa4feb6db 107 //show that this is the start of new dive sequence
tnhnrl 49:47ffa4feb6db 108 recordState(_state);
tnhnrl 49:47ffa4feb6db 109
tnhnrl 49:47ffa4feb6db 110 //triggers logger array
tnhnrl 49:47ffa4feb6db 111 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 49:47ffa4feb6db 112 recordData(_state);
tnhnrl 49:47ffa4feb6db 113 }
tnhnrl 49:47ffa4feb6db 114
tnhnrl 49:47ffa4feb6db 115 // how exit?
tnhnrl 49:47ffa4feb6db 116 if (timer > _timeout) {
tnhnrl 49:47ffa4feb6db 117 pc().printf("CHECK_TUNING: timed out!\r\n");
tnhnrl 49:47ffa4feb6db 118 _state = FLOAT_BROADCAST;
tnhnrl 49:47ffa4feb6db 119 timer.reset();
tnhnrl 49:47ffa4feb6db 120 _isTimeoutRunning = false;
tnhnrl 49:47ffa4feb6db 121 }
tnhnrl 49:47ffa4feb6db 122
tnhnrl 49:47ffa4feb6db 123 //print status to screen continuously
tnhnrl 49:47ffa4feb6db 124 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 125
tnhnrl 49:47ffa4feb6db 126 //record data every 5 seconds
tnhnrl 49:47ffa4feb6db 127 recordData(_state);
tnhnrl 49:47ffa4feb6db 128
tnhnrl 49:47ffa4feb6db 129 break;
tnhnrl 20:8987a9ae2bc7 130
tnhnrl 16:3363b9f14913 131 case EMERGENCY_CLIMB :
tnhnrl 16:3363b9f14913 132 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 133 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 134 pc().printf("\r\n\nstate: EMERGENCY_CLIMB\r\n");
tnhnrl 16:3363b9f14913 135 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 136 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 137 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 138
tnhnrl 16:3363b9f14913 139 // what needs to be started?
tnhnrl 16:3363b9f14913 140 bce().unpause();
tnhnrl 16:3363b9f14913 141 batt().unpause();
tnhnrl 20:8987a9ae2bc7 142
tnhnrl 20:8987a9ae2bc7 143 // what are the commands?
tnhnrl 16:3363b9f14913 144 bce().setPosition_mm(bce().getTravelLimit());
tnhnrl 16:3363b9f14913 145 batt().setPosition_mm(0.0);
tnhnrl 32:f2f8ae34aadc 146
tnhnrl 32:f2f8ae34aadc 147 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 148 ////////createNewFile();
tnhnrl 32:f2f8ae34aadc 149
tnhnrl 32:f2f8ae34aadc 150 //show that this is the start of a new EMERGENCY_CLIMB sequence
tnhnrl 32:f2f8ae34aadc 151 recordState(_state);
tnhnrl 32:f2f8ae34aadc 152
tnhnrl 49:47ffa4feb6db 153 //triggers logger array
tnhnrl 32:f2f8ae34aadc 154 _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 155 recordData(_state);
tnhnrl 16:3363b9f14913 156 }
tnhnrl 20:8987a9ae2bc7 157
tnhnrl 16:3363b9f14913 158 // how exit?
tnhnrl 17:7c16b5671d0e 159 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 160 pc().printf("EC: timed out\r\n");
tnhnrl 21:38c8544db6f4 161 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 162 timer.reset();
tnhnrl 28:16c83a2fdefa 163 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 164 }
tnhnrl 26:7e118fc02eea 165 else if (depthLoop().getPosition() < 2.0) { //if the depth is greater than 0.2 feet, go to float broadcast
tnhnrl 21:38c8544db6f4 166 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 167 timer.reset();
tnhnrl 28:16c83a2fdefa 168 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 169 }
tnhnrl 32:f2f8ae34aadc 170
tnhnrl 34:9b66c5188051 171 //print status to screen continuously
tnhnrl 34:9b66c5188051 172 pc().printf("EC: depth: %3.1f (BCE_cmd: %0.1f), pitch: %0.1f deg [%0.1f sec]\r",depthLoop().getPosition(),bce().getPosition_mm(),pitchLoop().getPosition(),timer.read());
tnhnrl 34:9b66c5188051 173
tnhnrl 32:f2f8ae34aadc 174 //record data every 5 seconds
tnhnrl 34:9b66c5188051 175 recordData(_state);
tnhnrl 32:f2f8ae34aadc 176
tnhnrl 16:3363b9f14913 177 break;
tnhnrl 20:8987a9ae2bc7 178
tnhnrl 16:3363b9f14913 179 case FIND_NEUTRAL :
tnhnrl 20:8987a9ae2bc7 180 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 181 if (!_isTimeoutRunning) {
tnhnrl 21:38c8544db6f4 182 pc().printf("\r\n\nstate: FIND_NEUTRAL\n\r");
tnhnrl 16:3363b9f14913 183 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 184 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 185 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 186
tnhnrl 16:3363b9f14913 187 // what needs to be started?
tnhnrl 16:3363b9f14913 188 bce().unpause();
tnhnrl 16:3363b9f14913 189 batt().unpause();
tnhnrl 28:16c83a2fdefa 190 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 24:c7d9b5bf3829 191 batt().setPosition_mm(_neutral_batt_pos_mm); //set battery to close-to-neutral setting from config file
tnhnrl 17:7c16b5671d0e 192
tnhnrl 24:c7d9b5bf3829 193 //first iteration goes into Neutral Finding Sub-FSM
tnhnrl 24:c7d9b5bf3829 194 //set the first state of the FSM, and start the sub-FSM
tnhnrl 30:2964617e7676 195 _substate = NEUTRAL_SINKING; //first state in neutral sub-FSM is the pressure vessel sinking
tnhnrl 28:16c83a2fdefa 196 _previous_substate = -1;
tnhnrl 28:16c83a2fdefa 197
tnhnrl 28:16c83a2fdefa 198 //save this state to the array
tnhnrl 30:2964617e7676 199 _substate_array[_substate_array_counter] = NEUTRAL_SINKING; //save to state array
tnhnrl 28:16c83a2fdefa 200 _substate_array_counter++;
tnhnrl 28:16c83a2fdefa 201
tnhnrl 32:f2f8ae34aadc 202 runNeutralStateMachine();
tnhnrl 32:f2f8ae34aadc 203
tnhnrl 32:f2f8ae34aadc 204 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 205 //createNewFile();
tnhnrl 32:f2f8ae34aadc 206
tnhnrl 32:f2f8ae34aadc 207 //show that this is the start of a new FIND_NEUTRAL sequence
tnhnrl 32:f2f8ae34aadc 208 recordState(_state);
tnhnrl 32:f2f8ae34aadc 209
tnhnrl 49:47ffa4feb6db 210 //triggers logger array
tnhnrl 32:f2f8ae34aadc 211 _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 212 recordData(_state);
tnhnrl 16:3363b9f14913 213 }
tnhnrl 20:8987a9ae2bc7 214
tnhnrl 20:8987a9ae2bc7 215 // how exit? (exit with the timer, if timer still running continue processing sub FSM)
tnhnrl 17:7c16b5671d0e 216 if (timer > _timeout) {
tnhnrl 17:7c16b5671d0e 217 pc().printf("FN: timed out [time: %0.1f sec]\r\n", timer.read());
tnhnrl 21:38c8544db6f4 218 _state = EMERGENCY_CLIMB; //new behavior (if this times out it emergency surfaces)
tnhnrl 16:3363b9f14913 219 timer.reset();
tnhnrl 28:16c83a2fdefa 220 _isTimeoutRunning = false;
tnhnrl 24:c7d9b5bf3829 221
tnhnrl 24:c7d9b5bf3829 222 //record this to the NEUTRAL sub-FSM tracker
tnhnrl 28:16c83a2fdefa 223 _substate_array[_substate_array_counter] = EMERGENCY_CLIMB; //save to state array
tnhnrl 28:16c83a2fdefa 224 _substate_array_counter++;
tnhnrl 16:3363b9f14913 225 }
tnhnrl 21:38c8544db6f4 226
tnhnrl 24:c7d9b5bf3829 227 //what is active? (neutral finding sub-function runs until completion)
tnhnrl 24:c7d9b5bf3829 228 //check if substate returned exit state, if so stop running the sub-FSM
tnhnrl 26:7e118fc02eea 229 else if (runNeutralStateMachine() == NEUTRAL_EXIT) {
tnhnrl 21:38c8544db6f4 230 //if successful, FIND_NEUTRAL then goes to RISE
tnhnrl 21:38c8544db6f4 231 pc().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\n\n\r");
tnhnrl 21:38c8544db6f4 232 _state = RISE;
tnhnrl 30:2964617e7676 233 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 234 }
tnhnrl 32:f2f8ae34aadc 235
tnhnrl 32:f2f8ae34aadc 236 //record data every 5 seconds
tnhnrl 34:9b66c5188051 237 recordData(_state);
tnhnrl 32:f2f8ae34aadc 238
tnhnrl 17:7c16b5671d0e 239 break;
tnhnrl 17:7c16b5671d0e 240
tnhnrl 16:3363b9f14913 241 case DIVE :
tnhnrl 16:3363b9f14913 242 // start local state timer and init any other one-shot actions
tnhnrl 32:f2f8ae34aadc 243
tnhnrl 28:16c83a2fdefa 244 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 245 pc().printf("\r\n\nstate: DIVE\r\n");
tnhnrl 16:3363b9f14913 246 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 247 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 248 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 249
tnhnrl 16:3363b9f14913 250 // what needs to be started?
tnhnrl 16:3363b9f14913 251 bce().unpause();
tnhnrl 16:3363b9f14913 252 batt().unpause();
tnhnrl 20:8987a9ae2bc7 253
tnhnrl 16:3363b9f14913 254 // what are the commands?
tnhnrl 32:f2f8ae34aadc 255 depthLoop().setCommand(_depth_command);
tnhnrl 32:f2f8ae34aadc 256 pitchLoop().setCommand(_pitch_command);
tnhnrl 32:f2f8ae34aadc 257
tnhnrl 34:9b66c5188051 258 pc().printf("DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand());
tnhnrl 34:9b66c5188051 259 pc().printf("DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 28:16c83a2fdefa 260
tnhnrl 28:16c83a2fdefa 261 //reset max dive depth
tnhnrl 28:16c83a2fdefa 262 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 32:f2f8ae34aadc 263
tnhnrl 32:f2f8ae34aadc 264 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 265 //createNewFile();
tnhnrl 32:f2f8ae34aadc 266
tnhnrl 32:f2f8ae34aadc 267 //show that this is the start of new dive sequence
tnhnrl 32:f2f8ae34aadc 268 recordState(_state);
tnhnrl 32:f2f8ae34aadc 269
tnhnrl 49:47ffa4feb6db 270 //triggers logger array
tnhnrl 32:f2f8ae34aadc 271 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 272 recordData(_state);
tnhnrl 16:3363b9f14913 273 }
tnhnrl 20:8987a9ae2bc7 274
tnhnrl 16:3363b9f14913 275 // how exit?
tnhnrl 32:f2f8ae34aadc 276 if (timer.read() > _timeout) {
tnhnrl 17:7c16b5671d0e 277 pc().printf("DIVE: timed out\n\n\r");
tnhnrl 21:38c8544db6f4 278 _state = RISE; //new behavior 11/17/2017
tnhnrl 16:3363b9f14913 279 timer.reset();
tnhnrl 28:16c83a2fdefa 280 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 281 }
tnhnrl 32:f2f8ae34aadc 282 else if (depthLoop().getPosition() > depthLoop().getCommand() - 0.5) { // including offset for low momentum approaches
tnhnrl 32:f2f8ae34aadc 283 pc().printf("DIVE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 21:38c8544db6f4 284 _state = RISE;
tnhnrl 16:3363b9f14913 285 timer.reset();
tnhnrl 28:16c83a2fdefa 286 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 287 }
tnhnrl 20:8987a9ae2bc7 288
tnhnrl 16:3363b9f14913 289 // what is active?
tnhnrl 21:38c8544db6f4 290 pc().printf("DIVE: bce pos: %3.1f mm, batt pos: %3.1f mm (depth: %3.1f ft) (pitch: %3.1f deg)[%0.2f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), pitchLoop().getPosition(), timer.read());
tnhnrl 32:f2f8ae34aadc 291 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 16:3363b9f14913 292 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 28:16c83a2fdefa 293
tnhnrl 28:16c83a2fdefa 294 if (depthLoop().getPosition() > _max_recorded_depth_dive) { //debug
tnhnrl 28:16c83a2fdefa 295 _max_recorded_depth_dive = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 296 }
tnhnrl 32:f2f8ae34aadc 297
tnhnrl 32:f2f8ae34aadc 298 //record data every 5 seconds
tnhnrl 34:9b66c5188051 299 recordData(_state);
tnhnrl 32:f2f8ae34aadc 300
tnhnrl 16:3363b9f14913 301 break;
tnhnrl 16:3363b9f14913 302
tnhnrl 16:3363b9f14913 303 case RISE :
tnhnrl 16:3363b9f14913 304 // start local state timer and init any other one-shot actions
tnhnrl 32:f2f8ae34aadc 305
tnhnrl 28:16c83a2fdefa 306 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 307 pc().printf("\r\n\nstate: RISE\r\n");
tnhnrl 16:3363b9f14913 308 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 309 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 310 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 311
tnhnrl 16:3363b9f14913 312 // what needs to be started?
tnhnrl 16:3363b9f14913 313 bce().unpause();
tnhnrl 16:3363b9f14913 314 batt().unpause();
tnhnrl 16:3363b9f14913 315
tnhnrl 16:3363b9f14913 316 // what are the commands?
tnhnrl 28:16c83a2fdefa 317 depthLoop().setCommand(-1.0); //make sure to get towards the surface (saw issues at LASR pool)
tnhnrl 32:f2f8ae34aadc 318 pitchLoop().setCommand(-_pitch_command);
tnhnrl 34:9b66c5188051 319
tnhnrl 34:9b66c5188051 320 pc().printf("RISE: depth cmd: %0.1f\r\n",depthLoop().getCommand());
tnhnrl 34:9b66c5188051 321 pc().printf("RISE: pitch cmd: %0.1f\r\n",pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 322
tnhnrl 32:f2f8ae34aadc 323 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 324 //createNewFile();
tnhnrl 32:f2f8ae34aadc 325
tnhnrl 32:f2f8ae34aadc 326 //show that this is the start of new rise sequence
tnhnrl 32:f2f8ae34aadc 327 recordState(_state);
tnhnrl 32:f2f8ae34aadc 328
tnhnrl 49:47ffa4feb6db 329 //triggers logger array
tnhnrl 32:f2f8ae34aadc 330 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 331 recordData(_state);
tnhnrl 16:3363b9f14913 332 }
tnhnrl 20:8987a9ae2bc7 333
tnhnrl 16:3363b9f14913 334 // how exit?
tnhnrl 32:f2f8ae34aadc 335 if (timer.read() > _timeout) {
tnhnrl 16:3363b9f14913 336 pc().printf("RISE: timed out\r\n");
tnhnrl 21:38c8544db6f4 337 _state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 338 timer.reset();
tnhnrl 28:16c83a2fdefa 339 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 340 }
tnhnrl 32:f2f8ae34aadc 341
tnhnrl 32:f2f8ae34aadc 342 //modified from (depthLoop().getPosition() < depthLoop().getCommand() + 0.5)
tnhnrl 32:f2f8ae34aadc 343 //did not work correctly in bench test (stuck in rise state)
tnhnrl 32:f2f8ae34aadc 344 else if (depthLoop().getPosition() < 0.5) {
tnhnrl 32:f2f8ae34aadc 345 pc().printf("RISE: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 28:16c83a2fdefa 346 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 347 timer.reset();
tnhnrl 28:16c83a2fdefa 348 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 349 }
tnhnrl 20:8987a9ae2bc7 350
tnhnrl 20:8987a9ae2bc7 351 // what is active?
tnhnrl 16:3363b9f14913 352 pc().printf("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 353 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 32:f2f8ae34aadc 354 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 355
tnhnrl 32:f2f8ae34aadc 356 //record data every 5 seconds
tnhnrl 34:9b66c5188051 357 recordData(_state);
tnhnrl 32:f2f8ae34aadc 358
tnhnrl 16:3363b9f14913 359 break;
tnhnrl 16:3363b9f14913 360
tnhnrl 16:3363b9f14913 361 case FLOAT_LEVEL :
tnhnrl 16:3363b9f14913 362 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 363 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 364 pc().printf("\r\n\nstate: FLOAT_LEVEL\r\n");
tnhnrl 16:3363b9f14913 365 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 366 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 367 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 368
tnhnrl 16:3363b9f14913 369 // what needs to be started?
tnhnrl 16:3363b9f14913 370 bce().unpause();
tnhnrl 16:3363b9f14913 371 batt().unpause();
tnhnrl 16:3363b9f14913 372
tnhnrl 20:8987a9ae2bc7 373 // what are the commands?
tnhnrl 28:16c83a2fdefa 374 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 16:3363b9f14913 375 pitchLoop().setCommand(0.0);
tnhnrl 32:f2f8ae34aadc 376
tnhnrl 32:f2f8ae34aadc 377 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 378 //createNewFile();
tnhnrl 32:f2f8ae34aadc 379
tnhnrl 32:f2f8ae34aadc 380 //show that this is the start of a new float level sequence
tnhnrl 32:f2f8ae34aadc 381 recordState(_state);
tnhnrl 32:f2f8ae34aadc 382
tnhnrl 49:47ffa4feb6db 383 //triggers logger array
tnhnrl 32:f2f8ae34aadc 384 _is_log_timer_running = true; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 385 recordData(_state);
tnhnrl 16:3363b9f14913 386 }
tnhnrl 20:8987a9ae2bc7 387
tnhnrl 16:3363b9f14913 388 // how exit?
tnhnrl 17:7c16b5671d0e 389 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 390 pc().printf("FL: timed out\r\n");
tnhnrl 21:38c8544db6f4 391 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 392 timer.reset();
tnhnrl 28:16c83a2fdefa 393 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 394 }
tnhnrl 28:16c83a2fdefa 395 else if (fabs(imu().getPitch() - pitchLoop().getCommand()) < fabs(_pitchTolerance)) { //current tolerance is 5 degrees
tnhnrl 28:16c83a2fdefa 396 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 397 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 398 timer.reset();
tnhnrl 28:16c83a2fdefa 399 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 400 }
tnhnrl 20:8987a9ae2bc7 401
tnhnrl 16:3363b9f14913 402 // what is active?
tnhnrl 16:3363b9f14913 403 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 404 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 405
tnhnrl 32:f2f8ae34aadc 406 //record data every 5 seconds
tnhnrl 34:9b66c5188051 407 recordData(_state);
tnhnrl 32:f2f8ae34aadc 408
tnhnrl 16:3363b9f14913 409 break;
tnhnrl 16:3363b9f14913 410
tnhnrl 16:3363b9f14913 411 case FLOAT_BROADCAST :
tnhnrl 16:3363b9f14913 412 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 413 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 414 pc().printf("\r\n\nstate: FLOAT_BROADCAST\r\n");
tnhnrl 16:3363b9f14913 415 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 416 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 417 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 418
tnhnrl 16:3363b9f14913 419 // what needs to be started?
tnhnrl 16:3363b9f14913 420 bce().unpause();
tnhnrl 16:3363b9f14913 421 batt().unpause();
tnhnrl 20:8987a9ae2bc7 422
tnhnrl 16:3363b9f14913 423 // what are the commands?
tnhnrl 28:16c83a2fdefa 424 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 28:16c83a2fdefa 425 batt().setPosition_mm(_battFloatPosition);
tnhnrl 32:f2f8ae34aadc 426
tnhnrl 32:f2f8ae34aadc 427 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 428 //createNewFile();
tnhnrl 32:f2f8ae34aadc 429
tnhnrl 32:f2f8ae34aadc 430 //show that this is the start of a new float broadcast sequence
tnhnrl 32:f2f8ae34aadc 431 recordState(_state);
tnhnrl 32:f2f8ae34aadc 432
tnhnrl 49:47ffa4feb6db 433 //triggers logger array
tnhnrl 32:f2f8ae34aadc 434 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 435 recordData(_state);
tnhnrl 16:3363b9f14913 436 }
tnhnrl 20:8987a9ae2bc7 437
tnhnrl 16:3363b9f14913 438 // how exit?
tnhnrl 17:7c16b5671d0e 439 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 440 pc().printf("FB: timed out\r\n");
tnhnrl 21:38c8544db6f4 441 _state = SIT_IDLE;
tnhnrl 16:3363b9f14913 442 timer.reset();
tnhnrl 32:f2f8ae34aadc 443
tnhnrl 32:f2f8ae34aadc 444 //stop recording data
tnhnrl 35:2f66ea4863d5 445 //mbedLogger().closeFile();
tnhnrl 32:f2f8ae34aadc 446
tnhnrl 28:16c83a2fdefa 447 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 448 }
tnhnrl 20:8987a9ae2bc7 449 else if ( (fabs(bce().getPosition_mm() - bce().getSetPosition_mm()) < bce().getDeadband()) and
tnhnrl 20:8987a9ae2bc7 450 (fabs(batt().getPosition_mm() - batt().getSetPosition_mm()) < batt().getDeadband()) ) {
tnhnrl 16:3363b9f14913 451 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 452 _state = SIT_IDLE;
tnhnrl 16:3363b9f14913 453 timer.reset();
tnhnrl 32:f2f8ae34aadc 454
tnhnrl 32:f2f8ae34aadc 455 //stop recording data
tnhnrl 35:2f66ea4863d5 456 //mbedLogger().closeFile();
tnhnrl 32:f2f8ae34aadc 457
tnhnrl 28:16c83a2fdefa 458 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 459 }
tnhnrl 20:8987a9ae2bc7 460
tnhnrl 20:8987a9ae2bc7 461 // what is active?
tnhnrl 49:47ffa4feb6db 462 pc().printf("FB: bce pos: %0.1f mm, batt pos: %0.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec] (CMD BCE: %0.1f BATT: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read(), bce().getSetPosition_mm(),batt().getSetPosition_mm());
tnhnrl 32:f2f8ae34aadc 463
tnhnrl 32:f2f8ae34aadc 464 //record data every 5 seconds
tnhnrl 34:9b66c5188051 465 recordData(_state);
tnhnrl 32:f2f8ae34aadc 466
tnhnrl 16:3363b9f14913 467 break;
tnhnrl 17:7c16b5671d0e 468
tnhnrl 17:7c16b5671d0e 469 case MULTI_DIVE :
tnhnrl 17:7c16b5671d0e 470 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 471 if (!_isTimeoutRunning) {
tnhnrl 17:7c16b5671d0e 472 pc().printf("\r\n\nstate: MULTI-DIVE\r\n");
tnhnrl 17:7c16b5671d0e 473 timer.reset(); // timer goes back to zero
tnhnrl 17:7c16b5671d0e 474 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 475 _isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 476
tnhnrl 17:7c16b5671d0e 477 // what needs to be started?
tnhnrl 17:7c16b5671d0e 478 bce().unpause();
tnhnrl 17:7c16b5671d0e 479 batt().unpause();
tnhnrl 17:7c16b5671d0e 480
tnhnrl 21:38c8544db6f4 481 //retrieve commands from structs (loaded from sequence.cfg file)
tnhnrl 32:f2f8ae34aadc 482 float sequence_depth_command = currentStateStruct.depth;
tnhnrl 32:f2f8ae34aadc 483 float sequence_pitch_command = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 484
tnhnrl 17:7c16b5671d0e 485 // what are the commands?
tnhnrl 32:f2f8ae34aadc 486 depthLoop().setCommand(sequence_depth_command);
tnhnrl 32:f2f8ae34aadc 487 pitchLoop().setCommand(sequence_pitch_command);
tnhnrl 21:38c8544db6f4 488 pc().printf("MULTI-DIVE: depth cmd: %3.1f ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 489
tnhnrl 32:f2f8ae34aadc 490 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 491 //createNewFile();
tnhnrl 32:f2f8ae34aadc 492
tnhnrl 32:f2f8ae34aadc 493 //show that this is the start of a new MULTI_DIVE sequence
tnhnrl 32:f2f8ae34aadc 494 recordState(_state);
tnhnrl 32:f2f8ae34aadc 495
tnhnrl 32:f2f8ae34aadc 496 //no max depth recording right now
tnhnrl 32:f2f8ae34aadc 497
tnhnrl 49:47ffa4feb6db 498 //triggers logger array
tnhnrl 32:f2f8ae34aadc 499 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 500 recordData(_state);
tnhnrl 17:7c16b5671d0e 501 }
tnhnrl 20:8987a9ae2bc7 502
tnhnrl 17:7c16b5671d0e 503 // how exit?
tnhnrl 17:7c16b5671d0e 504 if (timer > _timeout) {
tnhnrl 21:38c8544db6f4 505 pc().printf("\n\n\rMULTI-DIVE: timed out [time: %0.1f]\n\n\r", timer.read());
tnhnrl 21:38c8544db6f4 506 _state = MULTI_RISE; //new behavior 11/17/2017
tnhnrl 17:7c16b5671d0e 507 timer.reset();
tnhnrl 28:16c83a2fdefa 508 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 509 }
tnhnrl 17:7c16b5671d0e 510 else if (depthLoop().getPosition() > depthLoop().getCommand()) {
tnhnrl 17:7c16b5671d0e 511 pc().printf("MULTI-DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 21:38c8544db6f4 512 _state = MULTI_RISE;
tnhnrl 17:7c16b5671d0e 513 timer.reset();
tnhnrl 28:16c83a2fdefa 514 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 515 }
tnhnrl 20:8987a9ae2bc7 516
tnhnrl 17:7c16b5671d0e 517 // what is active?
tnhnrl 17:7c16b5671d0e 518 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 519 bce().setPosition_mm(depthLoop().getOutput());
tnhnrl 17:7c16b5671d0e 520 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 521
tnhnrl 32:f2f8ae34aadc 522 //record data every 5 seconds
tnhnrl 34:9b66c5188051 523 recordData(_state);
tnhnrl 32:f2f8ae34aadc 524
tnhnrl 17:7c16b5671d0e 525 break;
tnhnrl 17:7c16b5671d0e 526
tnhnrl 17:7c16b5671d0e 527 case MULTI_RISE :
tnhnrl 17:7c16b5671d0e 528 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 529 if (!_isTimeoutRunning) {
tnhnrl 17:7c16b5671d0e 530 pc().printf("\r\n\nstate: MULTI-RISE\r\n");
tnhnrl 17:7c16b5671d0e 531 timer.reset(); // timer goes back to zero
tnhnrl 17:7c16b5671d0e 532 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 533 _isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 534
tnhnrl 17:7c16b5671d0e 535 // what needs to be started?
tnhnrl 17:7c16b5671d0e 536 bce().unpause();
tnhnrl 17:7c16b5671d0e 537 batt().unpause();
tnhnrl 17:7c16b5671d0e 538
tnhnrl 17:7c16b5671d0e 539 //NEW: retrieve depth and pitch commands from config file struct
tnhnrl 17:7c16b5671d0e 540 // concept is to load this each time the multi-dive restarts
tnhnrl 17:7c16b5671d0e 541 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 542
tnhnrl 17:7c16b5671d0e 543 //retrieve just pitch command from struct
tnhnrl 32:f2f8ae34aadc 544 float sequence_pitch_command = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 545
tnhnrl 17:7c16b5671d0e 546 // what are the commands? (send back to 0.5 feet, not surface) // 11/21/2017
tnhnrl 17:7c16b5671d0e 547 depthLoop().setCommand(0.5);
tnhnrl 32:f2f8ae34aadc 548 pitchLoop().setCommand(-sequence_pitch_command);
tnhnrl 21:38c8544db6f4 549 pc().printf("MULTI-RISE: depth cmd: 0.0 ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 550
tnhnrl 32:f2f8ae34aadc 551 //create the log file (works only if the file is closed)
tnhnrl 34:9b66c5188051 552 //createNewFile();
tnhnrl 32:f2f8ae34aadc 553
tnhnrl 32:f2f8ae34aadc 554 //show that this is the start of a new MULTI_DIVE sequence
tnhnrl 32:f2f8ae34aadc 555 recordState(_state);
tnhnrl 32:f2f8ae34aadc 556
tnhnrl 49:47ffa4feb6db 557 //triggers logger array
tnhnrl 32:f2f8ae34aadc 558 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 559 recordData(_state);
tnhnrl 17:7c16b5671d0e 560 }
tnhnrl 20:8987a9ae2bc7 561
tnhnrl 17:7c16b5671d0e 562 // how exit?
tnhnrl 17:7c16b5671d0e 563 if (timer > _timeout) {
tnhnrl 21:38c8544db6f4 564 pc().printf("MULTI-RISE: timed out [time: %0.1f]\n\n\r", timer.read());
tnhnrl 21:38c8544db6f4 565 _state = EMERGENCY_CLIMB;
tnhnrl 17:7c16b5671d0e 566 timer.reset();
tnhnrl 28:16c83a2fdefa 567 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 568
tnhnrl 17:7c16b5671d0e 569 //reset multi-dive sequence to start
tnhnrl 24:c7d9b5bf3829 570 _multi_dive_counter = 0;
tnhnrl 17:7c16b5671d0e 571 }
tnhnrl 20:8987a9ae2bc7 572 else if (depthLoop().getPosition() < 0.5) { // depth is less than 0.5 (zero is surface level)
tnhnrl 17:7c16b5671d0e 573 pc().printf("MULTI-RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 574
tnhnrl 17:7c16b5671d0e 575 //going to next state
tnhnrl 28:16c83a2fdefa 576 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 577
tnhnrl 17:7c16b5671d0e 578 //successful dive-rise sequence CONTINUES the multi-dive sequence
tnhnrl 24:c7d9b5bf3829 579 _multi_dive_counter++;
tnhnrl 17:7c16b5671d0e 580
tnhnrl 17:7c16b5671d0e 581 //UPDATE THE SEQUENCE DATA HERE
tnhnrl 17:7c16b5671d0e 582 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 583
tnhnrl 17:7c16b5671d0e 584 //check if this is the end of the dive sequence
tnhnrl 30:2964617e7676 585 //CHECK BEFORE ANYTHING ELSE that you have reached the "exit" state (FLOAT_BROADCAST)
tnhnrl 30:2964617e7676 586 if (currentStateStruct.state == FLOAT_BROADCAST) {
tnhnrl 28:16c83a2fdefa 587 _state = FLOAT_BROADCAST;
tnhnrl 17:7c16b5671d0e 588 }
tnhnrl 17:7c16b5671d0e 589
tnhnrl 17:7c16b5671d0e 590 else
tnhnrl 21:38c8544db6f4 591 _state = MULTI_DIVE;
tnhnrl 17:7c16b5671d0e 592
tnhnrl 24:c7d9b5bf3829 593 //have to stop this with the _multi_dive_counter variable!
tnhnrl 17:7c16b5671d0e 594 }
tnhnrl 20:8987a9ae2bc7 595
tnhnrl 20:8987a9ae2bc7 596 // what is active?
tnhnrl 17:7c16b5671d0e 597 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 598 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 17:7c16b5671d0e 599 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 32:f2f8ae34aadc 600
tnhnrl 32:f2f8ae34aadc 601 //record data every 5 seconds
tnhnrl 34:9b66c5188051 602 recordData(_state);
tnhnrl 32:f2f8ae34aadc 603
tnhnrl 17:7c16b5671d0e 604 break;
tnhnrl 32:f2f8ae34aadc 605
tnhnrl 45:16b8162188ca 606 case TRANSMIT_LOG:
tnhnrl 32:f2f8ae34aadc 607 if (!_isTimeoutRunning) {
tnhnrl 45:16b8162188ca 608 pc().printf("\r\n\nstate: TRANSMIT_LOG\r\n");
tnhnrl 32:f2f8ae34aadc 609 timer.reset(); // timer goes back to zero
tnhnrl 32:f2f8ae34aadc 610 timer.start(); // background timer starts running
tnhnrl 32:f2f8ae34aadc 611 _isTimeoutRunning = true;
tnhnrl 32:f2f8ae34aadc 612
tnhnrl 45:16b8162188ca 613 mbedLogger().getNumberOfPacketsInCurrentLog(); //open the file, read the number of lines in the log file
tnhnrl 45:16b8162188ca 614 transmit_packet_number = mbedLogger().getNumberOfPackets();
tnhnrl 45:16b8162188ca 615
tnhnrl 45:16b8162188ca 616 //pc().printf("getNumberOfPacketsInCurrentLog is %d\n\r", transmit_packet_number);
tnhnrl 45:16b8162188ca 617 }
tnhnrl 45:16b8162188ca 618
tnhnrl 45:16b8162188ca 619 if (timer.read() > _timeout) {
tnhnrl 45:16b8162188ca 620 pc().printf("\n\rTRANSMIT_LOG: timed out!\n\r");
tnhnrl 45:16b8162188ca 621 _state = SIT_IDLE;
tnhnrl 45:16b8162188ca 622 timer.reset();
tnhnrl 45:16b8162188ca 623 _isTimeoutRunning = false;
tnhnrl 45:16b8162188ca 624
tnhnrl 45:16b8162188ca 625 mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading
tnhnrl 45:16b8162188ca 626 }
tnhnrl 45:16b8162188ca 627
tnhnrl 45:16b8162188ca 628 //IF THIS IS ZERO
tnhnrl 45:16b8162188ca 629 if (mbedLogger().currentPacketNumber() > transmit_packet_number) {
tnhnrl 45:16b8162188ca 630 pc().printf("mbedLogger().currentPacketNumber() > transmit_packet_number");
tnhnrl 45:16b8162188ca 631 _state = SIT_IDLE;
tnhnrl 45:16b8162188ca 632 timer.reset();
tnhnrl 45:16b8162188ca 633 _isTimeoutRunning = false;
tnhnrl 45:16b8162188ca 634
tnhnrl 45:16b8162188ca 635 mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading
tnhnrl 32:f2f8ae34aadc 636 }
tnhnrl 45:16b8162188ca 637
tnhnrl 45:16b8162188ca 638 if (mbedLogger().isTransmissionComplete()) {
tnhnrl 45:16b8162188ca 639 pc().printf("StateMachine isTransmissionComplete (true)\n\r");
tnhnrl 45:16b8162188ca 640 _state = SIT_IDLE;
tnhnrl 45:16b8162188ca 641 timer.reset();
tnhnrl 45:16b8162188ca 642 _isTimeoutRunning = false;
tnhnrl 45:16b8162188ca 643
tnhnrl 45:16b8162188ca 644 mbedLogger().closeLogFile(); //on timeout close the log file that was opened for reading
tnhnrl 45:16b8162188ca 645 }
tnhnrl 45:16b8162188ca 646
tnhnrl 32:f2f8ae34aadc 647
tnhnrl 45:16b8162188ca 648 // what is active? (no hardware should be active)
tnhnrl 45:16b8162188ca 649 mbedLogger().readTransmitPacketOneChar(); //led2 shows you pc readable
tnhnrl 45:16b8162188ca 650
tnhnrl 45:16b8162188ca 651 break;
tnhnrl 45:16b8162188ca 652
tnhnrl 45:16b8162188ca 653 case RECEIVE_SEQUENCE :
tnhnrl 45:16b8162188ca 654 pc().printf("state: RECEIVE_SEQUENCE\n\r");
tnhnrl 45:16b8162188ca 655
tnhnrl 45:16b8162188ca 656 if (!_isTimeoutRunning) {
tnhnrl 45:16b8162188ca 657 pc().printf("RECEIVE_SEQUENCE _isTimeoutRunning\r\n");
tnhnrl 45:16b8162188ca 658 timer.reset(); // timer goes back to zero
tnhnrl 45:16b8162188ca 659 timer.start(); // background timer starts running
tnhnrl 45:16b8162188ca 660 _isTimeoutRunning = true;
tnhnrl 45:16b8162188ca 661 }
tnhnrl 45:16b8162188ca 662
tnhnrl 34:9b66c5188051 663 if (timer.read() > _timeout) {
tnhnrl 45:16b8162188ca 664 pc().printf("RECEIVE_SEQUENCE: timed out!\n\r");
tnhnrl 34:9b66c5188051 665 _state = SIT_IDLE;
tnhnrl 34:9b66c5188051 666 timer.reset();
tnhnrl 32:f2f8ae34aadc 667 _isTimeoutRunning = false;
tnhnrl 32:f2f8ae34aadc 668 }
tnhnrl 32:f2f8ae34aadc 669
tnhnrl 45:16b8162188ca 670 // what is active?
tnhnrl 45:16b8162188ca 671 pc().printf("Receive sequence active?\n\r");
tnhnrl 32:f2f8ae34aadc 672
tnhnrl 32:f2f8ae34aadc 673 break;
tnhnrl 16:3363b9f14913 674
tnhnrl 16:3363b9f14913 675 default :
tnhnrl 17:7c16b5671d0e 676 pc().printf("DEBUG: SIT_IDLE\n\r");
tnhnrl 21:38c8544db6f4 677 _state = SIT_IDLE;
tnhnrl 28:16c83a2fdefa 678 }
tnhnrl 28:16c83a2fdefa 679
tnhnrl 28:16c83a2fdefa 680 //save the state to print to user
tnhnrl 28:16c83a2fdefa 681 if (_previous_state != _state) {
tnhnrl 28:16c83a2fdefa 682 _state_array[_state_array_counter] = _state; //save to state array
tnhnrl 28:16c83a2fdefa 683 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 684
tnhnrl 28:16c83a2fdefa 685 _previous_state = _state;
tnhnrl 28:16c83a2fdefa 686 }
tnhnrl 34:9b66c5188051 687
tnhnrl 34:9b66c5188051 688 return _state;
tnhnrl 34:9b66c5188051 689 // //if the state is SIT_IDLE, return 0 / false (for recording)
tnhnrl 34:9b66c5188051 690 // if (_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 691 // return 0;
tnhnrl 34:9b66c5188051 692 // else
tnhnrl 34:9b66c5188051 693 // return 1; //return true to indicate that you're recording
tnhnrl 16:3363b9f14913 694 }
tnhnrl 20:8987a9ae2bc7 695
tnhnrl 16:3363b9f14913 696 // output the keyboard menu for user's reference
tnhnrl 16:3363b9f14913 697 void StateMachine::showMenu() {
tnhnrl 49:47ffa4feb6db 698 pc().printf("\r\r\n\nKEYBOARD MENU (02/15/2018):\r\r\n");
tnhnrl 52:f207567d3ea4 699 pc().printf(" T to go into manual tuning mode (This is on a timer!)\r\n");
tnhnrl 16:3363b9f14913 700 pc().printf(" N to find neutral\r\n");
tnhnrl 17:7c16b5671d0e 701 pc().printf(" M to initiate multi-dive cycle\r\n");
tnhnrl 16:3363b9f14913 702 pc().printf(" D to initiate dive cycle\r\n");
tnhnrl 16:3363b9f14913 703 pc().printf(" R to initiate rise\r\n");
tnhnrl 16:3363b9f14913 704 pc().printf(" L to float level\r\n");
tnhnrl 16:3363b9f14913 705 pc().printf(" B to float at broadcast pitch\r\n");
tnhnrl 16:3363b9f14913 706 pc().printf(" E to initiate emergency climb\r\n");
tnhnrl 49:47ffa4feb6db 707 pc().printf(" '|' to tare the depth sensor (vertical bar)\r\n");
tnhnrl 28:16c83a2fdefa 708 pc().printf(" Z to show FSM and sub-FSM states.\r\n");
tnhnrl 32:f2f8ae34aadc 709 pc().printf(" P to print the current log file.\r\n");
tnhnrl 32:f2f8ae34aadc 710 pc().printf(" X to print the list of log files.\r\n");
tnhnrl 49:47ffa4feb6db 711 pc().printf(" I to receive data.\r\n");
tnhnrl 49:47ffa4feb6db 712 pc().printf(" O to transmit data.\r\n");
tnhnrl 49:47ffa4feb6db 713 pc().printf(" ~ to erase mbed log file. (clear before logging more than a few runs)\r\n");
tnhnrl 32:f2f8ae34aadc 714 pc().printf("[/] to change bce neutral position: %0.1f\r\n", _neutral_bce_pos_mm);
tnhnrl 32:f2f8ae34aadc 715 pc().printf("</> to change batt neutral position: %0.1f\r\n", _neutral_batt_pos_mm);
tnhnrl 32:f2f8ae34aadc 716 pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",_pitch_command);
tnhnrl 32:f2f8ae34aadc 717 pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",_depth_command);
tnhnrl 17:7c16b5671d0e 718 pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout);
tnhnrl 16:3363b9f14913 719 pc().printf(" 1 BCE PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 720 pc().printf(" 2 BATT PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 721 pc().printf(" 3 Depth PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 722 pc().printf(" 4 Pitch PID sub-menu\r\n");
tnhnrl 49:47ffa4feb6db 723 pc().printf(" 5 MANUAL_TUNING sub-menu (does not have a timer!)\r\n");
tnhnrl 53:c0586fe62b01 724
tnhnrl 53:c0586fe62b01 725 pc().printf(" 6 STREAM STATUS)\r\n");
tnhnrl 53:c0586fe62b01 726 pc().printf(" 7 RUDDER SERVO settings menu\r\n");
tnhnrl 53:c0586fe62b01 727
tnhnrl 28:16c83a2fdefa 728 pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 16:3363b9f14913 729 pc().printf(" ? to reset mbed\r\n");
tnhnrl 16:3363b9f14913 730 }
tnhnrl 20:8987a9ae2bc7 731
tnhnrl 17:7c16b5671d0e 732 //Find Neutral sub finite state machine
tnhnrl 17:7c16b5671d0e 733 // Note: the sub-fsm only moves the pistons once at the start of each timer loop
tnhnrl 17:7c16b5671d0e 734 // (timer completes, move piston, timer completes, move piston, etc)
tnhnrl 28:16c83a2fdefa 735 int StateMachine::runNeutralStateMachine() {
tnhnrl 24:c7d9b5bf3829 736 switch (_substate) {
tnhnrl 20:8987a9ae2bc7 737 case NEUTRAL_SINKING :
tnhnrl 17:7c16b5671d0e 738 //start the 10 second timer
tnhnrl 28:16c83a2fdefa 739 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 740 _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 741
tnhnrl 39:58375ca6b6ff 742 pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f) (getSetPosition: %0.1f)\n\r", _neutral_timer, timer.read(), pitchLoop().getPosition(), bce().getSetPosition_mm());
tnhnrl 20:8987a9ae2bc7 743
tnhnrl 32:f2f8ae34aadc 744 // what are the commands? (BCE linear actuator active, no pitch movement)
tnhnrl 39:58375ca6b6ff 745 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 746
tnhnrl 32:f2f8ae34aadc 747 pc().printf("NEUTRAL_SINKING: Retracting piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f] (pitch: %0.1f)\n\r", _neutral_sink_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand(), pitchLoop().getPosition());
tnhnrl 17:7c16b5671d0e 748
tnhnrl 28:16c83a2fdefa 749 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 750 }
tnhnrl 20:8987a9ae2bc7 751
tnhnrl 20:8987a9ae2bc7 752 // how exit?
tnhnrl 20:8987a9ae2bc7 753 //once reached the travel limit, no need to keep trying, so exit
tnhnrl 25:249e4d56b27c 754 if (bce().getPosition_mm() <= 0) {
tnhnrl 25:249e4d56b27c 755 pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm());
tnhnrl 25:249e4d56b27c 756 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 757 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 25:249e4d56b27c 758 }
tnhnrl 20:8987a9ae2bc7 759 //once deeper than the commanded setpoint...
tnhnrl 32:f2f8ae34aadc 760 else if (depthLoop().getPosition() > _depth_command) {
tnhnrl 24:c7d9b5bf3829 761 _substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 28:16c83a2fdefa 762 _isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 20:8987a9ae2bc7 763 }
tnhnrl 20:8987a9ae2bc7 764
tnhnrl 20:8987a9ae2bc7 765 // what is active?
tnhnrl 20:8987a9ae2bc7 766 //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 767 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 768 pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", timer.read());
tnhnrl 17:7c16b5671d0e 769
tnhnrl 28:16c83a2fdefa 770 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 771 }
tnhnrl 39:58375ca6b6ff 772
tnhnrl 39:58375ca6b6ff 773 // what is active? (only the buoyancy engine moved every 5 seconds at start)
tnhnrl 39:58375ca6b6ff 774 pc().printf("BCE current pos: %0.1f mm (BCE setpoint: %0.1f mm) (current depth: %0.1f ft)\r", bce().getPosition_mm(),bce().getSetPosition_mm(),depthLoop().getPosition()); //debug
tnhnrl 17:7c16b5671d0e 775 break;
tnhnrl 17:7c16b5671d0e 776
tnhnrl 17:7c16b5671d0e 777 case NEUTRAL_SLOWLY_RISE:
tnhnrl 28:16c83a2fdefa 778 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 779 _neutral_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 780
tnhnrl 24:c7d9b5bf3829 781 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 782
tnhnrl 20:8987a9ae2bc7 783 // what are the commands?
tnhnrl 32:f2f8ae34aadc 784 //move piston at start of sequence (default: extend 2.0 mm)
tnhnrl 39:58375ca6b6ff 785 bce().setPosition_mm(bce().getSetPosition_mm() + 2.0); //no depth command
tnhnrl 23:434f04ef1fad 786
tnhnrl 23:434f04ef1fad 787 // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state
tnhnrl 23:434f04ef1fad 788 pitchLoop().setCommand(0.0);
tnhnrl 24:c7d9b5bf3829 789
tnhnrl 32:f2f8ae34aadc 790 pc().printf("NEUTRAL_SLOWLY_RISE: Extending BCE piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f]\n\r", _neutral_rise_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand());
tnhnrl 24:c7d9b5bf3829 791
tnhnrl 28:16c83a2fdefa 792 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 793 }
tnhnrl 17:7c16b5671d0e 794
tnhnrl 20:8987a9ae2bc7 795 // how exit?
tnhnrl 24:c7d9b5bf3829 796 //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit
tnhnrl 24:c7d9b5bf3829 797 if (bce().getSetPosition_mm() >= bce().getTravelLimit()) {
tnhnrl 24:c7d9b5bf3829 798 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 799 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 800 }
tnhnrl 17:7c16b5671d0e 801 //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration
tnhnrl 20:8987a9ae2bc7 802 else if (depthLoop().getVelocity() < 0) { //less than zero ft/s
tnhnrl 24:c7d9b5bf3829 803 pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 ft/s [time: %0.1f]\r\n", timer.read());
tnhnrl 24:c7d9b5bf3829 804 _substate = NEUTRAL_CHECK_PITCH;
tnhnrl 28:16c83a2fdefa 805 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 806 }
tnhnrl 17:7c16b5671d0e 807
tnhnrl 20:8987a9ae2bc7 808 // what is active?
tnhnrl 20:8987a9ae2bc7 809 //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 810 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 811 pc().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! [timer: %0.1f]\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 812
tnhnrl 28:16c83a2fdefa 813 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 814 }
tnhnrl 23:434f04ef1fad 815
tnhnrl 32:f2f8ae34aadc 816 // what is active? (only the buoyancy engine moved every 5 seconds)
tnhnrl 32:f2f8ae34aadc 817 pc().printf("depthLoop getOutput: %0.1f\r", depthLoop().getOutput()); //debug
tnhnrl 32:f2f8ae34aadc 818 bce().setPosition_mm(depthLoop().getOutput()); // (DID NOT WORK ON BENCH)
tnhnrl 17:7c16b5671d0e 819 break;
tnhnrl 17:7c16b5671d0e 820
danstrider 22:a10ee088403b 821 case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired
danstrider 22:a10ee088403b 822 // start local state timer and init any other one-shot actions
tnhnrl 23:434f04ef1fad 823
tnhnrl 28:16c83a2fdefa 824 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 825 _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds
tnhnrl 24:c7d9b5bf3829 826 pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read());
danstrider 22:a10ee088403b 827
tnhnrl 32:f2f8ae34aadc 828 // what are the commands? (default: retract or extend 0.5 mm)
tnhnrl 24:c7d9b5bf3829 829 if (pitchLoop().getPosition() > 2) { // nose is high
tnhnrl 39:58375ca6b6ff 830 batt().setPosition_mm(batt().getSetPosition_mm() + 0.5); // move battery forward (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 831 pc().printf("\n\rNeutral Check Pitch: moving battery FWD in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 832 }
tnhnrl 24:c7d9b5bf3829 833 else if (pitchLoop().getPosition() < -2) { // nose is low
tnhnrl 39:58375ca6b6ff 834 batt().setPosition_mm(batt().getSetPosition_mm() - 0.5); // move battery aft (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 835 pc().printf("\n\rNeutral Check Pitch: moving battery AFT in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 836 }
tnhnrl 24:c7d9b5bf3829 837
tnhnrl 28:16c83a2fdefa 838 _isSubStateTimerRunning = true; //disable this block after one iteration
danstrider 22:a10ee088403b 839 }
tnhnrl 20:8987a9ae2bc7 840
tnhnrl 28:16c83a2fdefa 841 // how exit?
tnhnrl 20:8987a9ae2bc7 842 //pitch angle and pitch rate within small tolerance
tnhnrl 20:8987a9ae2bc7 843 //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 23:434f04ef1fad 844 if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) {
tnhnrl 24:c7d9b5bf3829 845 pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug
danstrider 22:a10ee088403b 846 // found level, but don't need to save anything this time
tnhnrl 23:434f04ef1fad 847
tnhnrl 28:16c83a2fdefa 848 if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug
tnhnrl 28:16c83a2fdefa 849 _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 850 }
tnhnrl 28:16c83a2fdefa 851
tnhnrl 23:434f04ef1fad 852 // found level and at depth too, so save it all now
tnhnrl 32:f2f8ae34aadc 853 if (_substate == NEUTRAL_CHECK_PITCH) {
danstrider 22:a10ee088403b 854 //save positions locally
danstrider 22:a10ee088403b 855 _neutral_batt_pos_mm = batt().getPosition_mm();
danstrider 22:a10ee088403b 856 _neutral_bce_pos_mm = bce().getPosition_mm();
danstrider 22:a10ee088403b 857
danstrider 22:a10ee088403b 858 //set the neutral positions in each outer loop
danstrider 22:a10ee088403b 859 depthLoop().setOutputOffset(_neutral_bce_pos_mm);
danstrider 22:a10ee088403b 860 pitchLoop().setOutputOffset(_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 861
danstrider 22:a10ee088403b 862 // save into the depth.txt and pitch.txt files
danstrider 22:a10ee088403b 863 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset
danstrider 22:a10ee088403b 864 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset
danstrider 22:a10ee088403b 865
tnhnrl 32:f2f8ae34aadc 866 pc().printf("\n\n\r>>> Saving Positions: BCE: %0.1f mm, BATT: %0.1f <<<\n\n\r",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 867
tnhnrl 24:c7d9b5bf3829 868 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 869 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 24:c7d9b5bf3829 870 }
tnhnrl 24:c7d9b5bf3829 871
tnhnrl 24:c7d9b5bf3829 872 else {
tnhnrl 24:c7d9b5bf3829 873 pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r");
tnhnrl 24:c7d9b5bf3829 874 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 875 }
tnhnrl 17:7c16b5671d0e 876 }
danstrider 22:a10ee088403b 877
danstrider 22:a10ee088403b 878 // what is active?
danstrider 22:a10ee088403b 879 //once timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 880 if (timer.read() >= _neutral_timer) {
danstrider 22:a10ee088403b 881 pc().printf("\r\n\nlevel timer COMPLETE!");
danstrider 22:a10ee088403b 882 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 883 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
danstrider 22:a10ee088403b 884 }
tnhnrl 17:7c16b5671d0e 885 break;
danstrider 22:a10ee088403b 886
danstrider 22:a10ee088403b 887 //this state could be removed, it is only used as a transition but is needed to stop entering this function
danstrider 22:a10ee088403b 888 case NEUTRAL_EXIT :
tnhnrl 23:434f04ef1fad 889 pc().printf("substate: NEUTRAL_EXIT\n\r");
tnhnrl 20:8987a9ae2bc7 890 break;
tnhnrl 21:38c8544db6f4 891
danstrider 22:a10ee088403b 892 default :
tnhnrl 24:c7d9b5bf3829 893 pc().printf("how did we get to substate: default?\n\r"); //debug
tnhnrl 23:434f04ef1fad 894 //a default within the sub-state machine
tnhnrl 24:c7d9b5bf3829 895 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 896 break;
tnhnrl 17:7c16b5671d0e 897 }
tnhnrl 20:8987a9ae2bc7 898
tnhnrl 30:2964617e7676 899 // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence)
tnhnrl 24:c7d9b5bf3829 900 if (_substate == NEUTRAL_EXIT) {
tnhnrl 24:c7d9b5bf3829 901 pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r");
tnhnrl 24:c7d9b5bf3829 902
tnhnrl 30:2964617e7676 903 //reset internal sub-state back to first entry conditions (first state is immediately sinking)
tnhnrl 30:2964617e7676 904 _substate = NEUTRAL_SINKING;
tnhnrl 28:16c83a2fdefa 905 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 21:38c8544db6f4 906
tnhnrl 24:c7d9b5bf3829 907 //record sub-states to view after sequence
tnhnrl 30:2964617e7676 908 _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array
tnhnrl 28:16c83a2fdefa 909 _substate_array_counter++;
tnhnrl 23:434f04ef1fad 910
tnhnrl 24:c7d9b5bf3829 911 //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit)
tnhnrl 24:c7d9b5bf3829 912 _previous_substate = -1;
tnhnrl 24:c7d9b5bf3829 913
tnhnrl 24:c7d9b5bf3829 914 //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 24:c7d9b5bf3829 915 return NEUTRAL_EXIT; // message to calling function we just exited
tnhnrl 21:38c8544db6f4 916 }
tnhnrl 23:434f04ef1fad 917 else {
tnhnrl 24:c7d9b5bf3829 918 //record sub-states to view after sequence (when changed)
tnhnrl 24:c7d9b5bf3829 919 if (_previous_substate != _substate) {
tnhnrl 28:16c83a2fdefa 920 _substate_array[_substate_array_counter] = _substate; //save current state to state array
tnhnrl 28:16c83a2fdefa 921 _substate_array_counter++;
tnhnrl 24:c7d9b5bf3829 922
tnhnrl 24:c7d9b5bf3829 923 //record the current substate for comparison
tnhnrl 24:c7d9b5bf3829 924 _previous_substate = _substate;
tnhnrl 24:c7d9b5bf3829 925 }
tnhnrl 24:c7d9b5bf3829 926
tnhnrl 24:c7d9b5bf3829 927 return _substate; // message to calling function of what sub-state it's in
tnhnrl 23:434f04ef1fad 928 }
tnhnrl 17:7c16b5671d0e 929 }
tnhnrl 20:8987a9ae2bc7 930
tnhnrl 20:8987a9ae2bc7 931 // keyboard runs independently of the state machine, handling one key at a time
tnhnrl 20:8987a9ae2bc7 932 //keyboard updates the desired _keyboard_state that is used in the state machine
tnhnrl 20:8987a9ae2bc7 933 //and only allows input when the state is "idle"
tnhnrl 17:7c16b5671d0e 934 void StateMachine::keyboard() {
tnhnrl 16:3363b9f14913 935 char userInput;
tnhnrl 20:8987a9ae2bc7 936
tnhnrl 16:3363b9f14913 937 // check keyboard and make settings changes as requested
tnhnrl 17:7c16b5671d0e 938 // states can be changed only at the start of a sequence (when the system is in SIT_IDLE)
tnhnrl 21:38c8544db6f4 939
tnhnrl 37:357e98a929cc 940 //TEST
tnhnrl 37:357e98a929cc 941 int _keyboard_state = 0; //made this a local variable because it was retaining the last keyboard state
tnhnrl 21:38c8544db6f4 942
tnhnrl 28:16c83a2fdefa 943 if (pc().readable() && (_state == SIT_IDLE || _state == KEYBOARD)) {
tnhnrl 16:3363b9f14913 944 // get the key
tnhnrl 17:7c16b5671d0e 945 userInput = pc().getc();
tnhnrl 17:7c16b5671d0e 946
tnhnrl 28:16c83a2fdefa 947 //record that the keyboard was used
tnhnrl 28:16c83a2fdefa 948 _state_array[_state_array_counter] = KEYBOARD;
tnhnrl 28:16c83a2fdefa 949 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 950
tnhnrl 21:38c8544db6f4 951 // keyboard has to reset timer each time it's used
tnhnrl 28:16c83a2fdefa 952 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 953
tnhnrl 16:3363b9f14913 954 // check command against desired control buttons
tnhnrl 49:47ffa4feb6db 955 if (userInput == 'D') {
tnhnrl 17:7c16b5671d0e 956 _keyboard_state = DIVE;
tnhnrl 16:3363b9f14913 957 }
tnhnrl 49:47ffa4feb6db 958 else if (userInput == 'N') {
tnhnrl 17:7c16b5671d0e 959 _keyboard_state = FIND_NEUTRAL;
tnhnrl 17:7c16b5671d0e 960 }
tnhnrl 49:47ffa4feb6db 961 else if (userInput == 'M') {
tnhnrl 17:7c16b5671d0e 962 //currently does not run if there is no file.
tnhnrl 17:7c16b5671d0e 963
tnhnrl 17:7c16b5671d0e 964 //need to add method to Sequence Controller that returns -1
tnhnrl 17:7c16b5671d0e 965 // or some check that insures you cannot run the dive sequence without a file
tnhnrl 17:7c16b5671d0e 966
tnhnrl 17:7c16b5671d0e 967 stateMachine().getDiveSequence(); //get first sequence on keyboard press
tnhnrl 17:7c16b5671d0e 968 _keyboard_state = currentStateStruct.state;
tnhnrl 17:7c16b5671d0e 969
tnhnrl 17:7c16b5671d0e 970 pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles
tnhnrl 16:3363b9f14913 971 }
tnhnrl 45:16b8162188ca 972 else if (userInput == 'R') {
tnhnrl 17:7c16b5671d0e 973 _keyboard_state = RISE;
tnhnrl 16:3363b9f14913 974 }
tnhnrl 45:16b8162188ca 975 else if (userInput == 'L') {
tnhnrl 17:7c16b5671d0e 976 _keyboard_state = FLOAT_LEVEL;
tnhnrl 16:3363b9f14913 977 }
tnhnrl 45:16b8162188ca 978 else if (userInput == 'B') {
tnhnrl 17:7c16b5671d0e 979 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 980 }
tnhnrl 45:16b8162188ca 981 else if (userInput == 'E') {
tnhnrl 17:7c16b5671d0e 982 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 983 }
tnhnrl 49:47ffa4feb6db 984
tnhnrl 49:47ffa4feb6db 985 else if (userInput == 'T') {
tnhnrl 49:47ffa4feb6db 986 _keyboard_state = CHECK_TUNING;
tnhnrl 49:47ffa4feb6db 987 }
tnhnrl 49:47ffa4feb6db 988
tnhnrl 49:47ffa4feb6db 989 // some debug tools below
tnhnrl 32:f2f8ae34aadc 990 else if (userInput == 'P') {
tnhnrl 32:f2f8ae34aadc 991 //Print current SD card log file
tnhnrl 36:966a86937e17 992 //printCurrentSdLog();
tnhnrl 36:966a86937e17 993 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 32:f2f8ae34aadc 994 }
tnhnrl 32:f2f8ae34aadc 995 else if (userInput == 'X') {
tnhnrl 45:16b8162188ca 996 mbedLogger().printMbedDirectory(); //print all log files to the screen
tnhnrl 32:f2f8ae34aadc 997 }
tnhnrl 45:16b8162188ca 998 else if (userInput == 'O') {
tnhnrl 45:16b8162188ca 999 _keyboard_state = TRANSMIT_LOG; //Transmit data (work in progress)
tnhnrl 45:16b8162188ca 1000 }
tnhnrl 49:47ffa4feb6db 1001 else if (userInput == 'I') {
tnhnrl 49:47ffa4feb6db 1002 mbedLogger().receiveMissionDataWithTicker(); //receive sequence.txt files
tnhnrl 49:47ffa4feb6db 1003 }
tnhnrl 49:47ffa4feb6db 1004 else if (userInput == '~') {
tnhnrl 49:47ffa4feb6db 1005 pc().printf("ERASING MBED LOG FILE\n\r");
tnhnrl 49:47ffa4feb6db 1006 mbedLogger().eraseFile();
tnhnrl 49:47ffa4feb6db 1007 }
tnhnrl 45:16b8162188ca 1008 else if (userInput == 'Z') {
tnhnrl 28:16c83a2fdefa 1009 pc().printf("FSG FSM States: \n\r");
tnhnrl 28:16c83a2fdefa 1010 string string_state;
tnhnrl 28:16c83a2fdefa 1011
tnhnrl 28:16c83a2fdefa 1012 for (int i = 0; i < _state_array_counter; i++) {
tnhnrl 28:16c83a2fdefa 1013 if (_state_array[i] == SIT_IDLE)
tnhnrl 28:16c83a2fdefa 1014 string_state = "SIT_IDLE <END>";
tnhnrl 28:16c83a2fdefa 1015 else if (_state_array[i] == FIND_NEUTRAL)
tnhnrl 28:16c83a2fdefa 1016 string_state = "FIND_NEUTRAL";
tnhnrl 28:16c83a2fdefa 1017 else if (_state_array[i] == DIVE)
tnhnrl 28:16c83a2fdefa 1018 string_state = "DIVE";
tnhnrl 28:16c83a2fdefa 1019 else if (_state_array[i] == RISE)
tnhnrl 28:16c83a2fdefa 1020 string_state = "RISE";
tnhnrl 28:16c83a2fdefa 1021 else if (_state_array[i] == FLOAT_LEVEL)
tnhnrl 28:16c83a2fdefa 1022 string_state = "FLOAT_LEVEL";
tnhnrl 28:16c83a2fdefa 1023 else if (_state_array[i] == FLOAT_BROADCAST)
tnhnrl 28:16c83a2fdefa 1024 string_state = "FLOAT_BROADCAST";
tnhnrl 28:16c83a2fdefa 1025 else if (_state_array[i] == EMERGENCY_CLIMB)
tnhnrl 28:16c83a2fdefa 1026 string_state = "EMERGENCY_CLIMB";
tnhnrl 28:16c83a2fdefa 1027 else if (_state_array[i] == MULTI_DIVE)
tnhnrl 28:16c83a2fdefa 1028 string_state = "MULTI_DIVE";
tnhnrl 28:16c83a2fdefa 1029 else if (_state_array[i] == MULTI_RISE)
tnhnrl 28:16c83a2fdefa 1030 string_state = "MULTI_RISE";
tnhnrl 28:16c83a2fdefa 1031 else if (_state_array[i] == KEYBOARD)
tnhnrl 28:16c83a2fdefa 1032 string_state = "KEYBOARD";
tnhnrl 28:16c83a2fdefa 1033 pc().printf("State #%d: %d (%s)\n\r", i, _state_array[i], string_state.c_str());
tnhnrl 28:16c83a2fdefa 1034 }
tnhnrl 28:16c83a2fdefa 1035
tnhnrl 28:16c83a2fdefa 1036 pc().printf("\n\rNeutral sub-FSM States: \n\r");
tnhnrl 28:16c83a2fdefa 1037 string string_substate;
tnhnrl 28:16c83a2fdefa 1038
tnhnrl 28:16c83a2fdefa 1039 for (int i = 0; i < _substate_array_counter; i++) {
tnhnrl 32:f2f8ae34aadc 1040 if (_substate_array[i] == NEUTRAL_SINKING)
tnhnrl 28:16c83a2fdefa 1041 string_substate = "NEUTRAL_SINKING";
tnhnrl 28:16c83a2fdefa 1042 else if (_substate_array[i] == NEUTRAL_SLOWLY_RISE)
tnhnrl 28:16c83a2fdefa 1043 string_substate = "NEUTRAL_SLOWLY_RISE";
tnhnrl 28:16c83a2fdefa 1044 else if (_substate_array[i] == NEUTRAL_CHECK_PITCH)
tnhnrl 28:16c83a2fdefa 1045 string_substate = "NEUTRAL_CHECK_PITCH";
tnhnrl 28:16c83a2fdefa 1046 else if (_substate_array[i] == NEUTRAL_EXIT)
tnhnrl 28:16c83a2fdefa 1047 string_substate = "NEUTRAL_EXIT <-- ";
tnhnrl 28:16c83a2fdefa 1048 else if (_substate_array[i] == EMERGENCY_CLIMB)
tnhnrl 28:16c83a2fdefa 1049 string_substate = " -- > EMERGENCY_CLIMB <-- ";
tnhnrl 28:16c83a2fdefa 1050 pc().printf("Neutral Substate #%d: %d (%s)\n\r", i, _state_array[i], string_substate.c_str());
tnhnrl 28:16c83a2fdefa 1051 }
tnhnrl 28:16c83a2fdefa 1052 pc().printf("\n\r"); //make space between printouts
tnhnrl 16:3363b9f14913 1053 }
tnhnrl 49:47ffa4feb6db 1054 else if (userInput == '|') {
tnhnrl 16:3363b9f14913 1055 pc().printf("taring depth sensor\r\n");
tnhnrl 16:3363b9f14913 1056 pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 1057 wait(0.1);
tnhnrl 16:3363b9f14913 1058 depth().tare(); // tares to ambient (do on surface)
tnhnrl 16:3363b9f14913 1059 pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 1060 }
tnhnrl 16:3363b9f14913 1061
tnhnrl 16:3363b9f14913 1062 else if (userInput == '[' or userInput == '{') {
tnhnrl 32:f2f8ae34aadc 1063 _neutral_bce_pos_mm = depthLoop().getOutputOffset() - 1;
tnhnrl 32:f2f8ae34aadc 1064 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // decrease the bce neutral setpoint
tnhnrl 32:f2f8ae34aadc 1065 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1066 // save neutral depth value to config file
tnhnrl 32:f2f8ae34aadc 1067 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1068 }
tnhnrl 16:3363b9f14913 1069 else if (userInput == ']' or userInput == '}') {
tnhnrl 32:f2f8ae34aadc 1070 _neutral_bce_pos_mm = depthLoop().getOutputOffset() + 1;
tnhnrl 32:f2f8ae34aadc 1071 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // increase the bce neutral setpoint
tnhnrl 32:f2f8ae34aadc 1072 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1073 // save neutral depth value to config file
tnhnrl 32:f2f8ae34aadc 1074 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1075 }
tnhnrl 16:3363b9f14913 1076 else if (userInput == '<' or userInput == ',') {
tnhnrl 32:f2f8ae34aadc 1077 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() - 1;
tnhnrl 32:f2f8ae34aadc 1078 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // decrease the batt neutral setpoint
tnhnrl 32:f2f8ae34aadc 1079 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1080 // save neutral pitch value to config file
tnhnrl 32:f2f8ae34aadc 1081 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1082 }
tnhnrl 16:3363b9f14913 1083 else if (userInput == '>' or userInput == '.') {
tnhnrl 32:f2f8ae34aadc 1084 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() + 1;
tnhnrl 32:f2f8ae34aadc 1085 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // increase the batt neutral setpoint
tnhnrl 32:f2f8ae34aadc 1086 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1087 // save neutral pitch value to config file
tnhnrl 32:f2f8ae34aadc 1088 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1089 }
tnhnrl 16:3363b9f14913 1090
tnhnrl 16:3363b9f14913 1091 else if (userInput == '?') {
tnhnrl 16:3363b9f14913 1092 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 16:3363b9f14913 1093 wait(0.5);
tnhnrl 16:3363b9f14913 1094 mbed_reset();
tnhnrl 16:3363b9f14913 1095 }
tnhnrl 20:8987a9ae2bc7 1096
tnhnrl 16:3363b9f14913 1097 // change settings
tnhnrl 16:3363b9f14913 1098 else if (userInput == 'Q' or userInput == 'q') {
tnhnrl 32:f2f8ae34aadc 1099 _pitch_command -= 0.5; //decrement the pitch setpoint
tnhnrl 32:f2f8ae34aadc 1100 pitchLoop().setCommand(_pitch_command);
tnhnrl 16:3363b9f14913 1101 pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 1102 }
tnhnrl 16:3363b9f14913 1103 else if (userInput == 'W' or userInput == 'w') {
tnhnrl 32:f2f8ae34aadc 1104 _pitch_command += 0.5; //increment the pitch setpoint
tnhnrl 32:f2f8ae34aadc 1105 pitchLoop().setCommand(_pitch_command);
tnhnrl 16:3363b9f14913 1106 pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 1107 }
tnhnrl 16:3363b9f14913 1108 else if (userInput == 'A' or userInput == 'a') {
tnhnrl 32:f2f8ae34aadc 1109 _depth_command -= 0.5; //decrement the depth setpoint
tnhnrl 32:f2f8ae34aadc 1110 depthLoop().setCommand(_depth_command);
tnhnrl 16:3363b9f14913 1111 pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1112 }
tnhnrl 16:3363b9f14913 1113 else if (userInput == 'S' or userInput == 's') {
tnhnrl 32:f2f8ae34aadc 1114 _depth_command += 0.5; //increment the depth setpoint
tnhnrl 32:f2f8ae34aadc 1115 depthLoop().setCommand(_depth_command);
tnhnrl 16:3363b9f14913 1116 pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1117 }
tnhnrl 16:3363b9f14913 1118 else if (userInput == '-') {
tnhnrl 17:7c16b5671d0e 1119 _timeout -= 10.0; //decrement the timeout
tnhnrl 17:7c16b5671d0e 1120 pc().printf(">>> timeout decreased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 1121 }
tnhnrl 16:3363b9f14913 1122 else if (userInput == '=' or userInput == '+') {
tnhnrl 17:7c16b5671d0e 1123 _timeout += 10.0; //increment the timeout
tnhnrl 17:7c16b5671d0e 1124 pc().printf(">>> timeout increased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 1125 }
tnhnrl 16:3363b9f14913 1126
tnhnrl 49:47ffa4feb6db 1127 // go to tuning sub-menu
tnhnrl 49:47ffa4feb6db 1128 else if (userInput == '5') {
tnhnrl 49:47ffa4feb6db 1129 keyboard_menu_MANUAL_TUNING();
tnhnrl 49:47ffa4feb6db 1130 }
tnhnrl 49:47ffa4feb6db 1131
tnhnrl 52:f207567d3ea4 1132 else if (userInput == '6') {
tnhnrl 52:f207567d3ea4 1133 keyboard_menu_STREAM_STATUS();
tnhnrl 52:f207567d3ea4 1134 }
tnhnrl 52:f207567d3ea4 1135
tnhnrl 52:f207567d3ea4 1136 else if (userInput == '8') {
tnhnrl 52:f207567d3ea4 1137 keyboard_menu_RUDDER_SERVO_settings();
tnhnrl 52:f207567d3ea4 1138 }
tnhnrl 52:f207567d3ea4 1139
tnhnrl 16:3363b9f14913 1140 // go to sub-menus for the PID gains (this is blocking)
tnhnrl 16:3363b9f14913 1141 else if (userInput == '1') {
tnhnrl 16:3363b9f14913 1142 keyboard_menu_BCE_PID_settings();
tnhnrl 16:3363b9f14913 1143 }
tnhnrl 16:3363b9f14913 1144 else if (userInput == '2') {
tnhnrl 16:3363b9f14913 1145 keyboard_menu_BATT_PID_settings();
tnhnrl 16:3363b9f14913 1146 }
tnhnrl 16:3363b9f14913 1147 else if (userInput == '3') {
tnhnrl 16:3363b9f14913 1148 keyboard_menu_DEPTH_PID_settings();
tnhnrl 16:3363b9f14913 1149 }
tnhnrl 16:3363b9f14913 1150 else if (userInput == '4') {
tnhnrl 16:3363b9f14913 1151 keyboard_menu_PITCH_PID_settings();
tnhnrl 16:3363b9f14913 1152 }
tnhnrl 16:3363b9f14913 1153
tnhnrl 16:3363b9f14913 1154 else if (userInput == 'C' or userInput == 'c') {
tnhnrl 32:f2f8ae34aadc 1155
tnhnrl 32:f2f8ae34aadc 1156 pc().printf("\n\n\rCURRENT STATUS AND PARAMETERS:\n\r");
mkelly10 51:c5c40272ecc3 1157
mkelly10 51:c5c40272ecc3 1158 // Testing out ADC
mkelly10 51:c5c40272ecc3 1159 float vref = 5.6;
mkelly10 51:c5c40272ecc3 1160 float vmeasured = 0;
mkelly10 51:c5c40272ecc3 1161 unsigned int raw = adc().readCh5();
mkelly10 51:c5c40272ecc3 1162 vmeasured = ((float)raw)/4095.0*vref;
mkelly10 51:c5c40272ecc3 1163 pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
mkelly10 51:c5c40272ecc3 1164 pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
mkelly10 51:c5c40272ecc3 1165 pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
mkelly10 51:c5c40272ecc3 1166 pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
mkelly10 51:c5c40272ecc3 1167 pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
mkelly10 51:c5c40272ecc3 1168 pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
mkelly10 51:c5c40272ecc3 1169 pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
mkelly10 51:c5c40272ecc3 1170 pc().printf("raw board current: %d \r\n",adc().readCh7());
mkelly10 51:c5c40272ecc3 1171 pc().printf("raw BCE limit switch: %d \r\n",bce().getSwitch());
mkelly10 51:c5c40272ecc3 1172 pc().printf("raw BMM limit switch: %d \r\n",batt().getSwitch());
mkelly10 51:c5c40272ecc3 1173 pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
mkelly10 51:c5c40272ecc3 1174 // End of ADC Test
mkelly10 51:c5c40272ecc3 1175
tnhnrl 32:f2f8ae34aadc 1176 pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 32:f2f8ae34aadc 1177 pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 16:3363b9f14913 1178 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 16:3363b9f14913 1179 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 16:3363b9f14913 1180 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 16:3363b9f14913 1181 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 16:3363b9f14913 1182 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1183 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 1184
tnhnrl 32:f2f8ae34aadc 1185 pc().printf("\n\rNeutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
tnhnrl 28:16c83a2fdefa 1186 pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 28:16c83a2fdefa 1187 pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1188 pc().printf("Max recorded depth: neutral: %0.1f, dive: %0.1f, auto_neutral_depth: %0.1f\n\n\r",_max_recorded_depth_neutral, _max_recorded_depth_dive, _max_recorded_auto_neutral_depth);
tnhnrl 38:83d06c294807 1189
tnhnrl 38:83d06c294807 1190 pc().printf("\n\r");
tnhnrl 38:83d06c294807 1191 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 38:83d06c294807 1192 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 38:83d06c294807 1193 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 38:83d06c294807 1194 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 16:3363b9f14913 1195 }
tnhnrl 17:7c16b5671d0e 1196
tnhnrl 17:7c16b5671d0e 1197 //when you read the keyboard successfully, change the state
tnhnrl 28:16c83a2fdefa 1198 _state = _keyboard_state; //set state at the end of this function
tnhnrl 38:83d06c294807 1199 //pc().printf("\n\n\r ********* KEYBOARD STATE: %d *********\n\n\r", _state);
tnhnrl 16:3363b9f14913 1200 }
tnhnrl 16:3363b9f14913 1201 }
tnhnrl 52:f207567d3ea4 1202
tnhnrl 52:f207567d3ea4 1203 void StateMachine::keyboard_menu_STREAM_STATUS() {
tnhnrl 52:f207567d3ea4 1204 char STATUS_key;
tnhnrl 52:f207567d3ea4 1205
tnhnrl 52:f207567d3ea4 1206 // show the menu
tnhnrl 52:f207567d3ea4 1207 pc().printf("\n\r1: STATUS DEBUG MENU (EXIT WITH 'X' !)");
tnhnrl 52:f207567d3ea4 1208
tnhnrl 52:f207567d3ea4 1209 while (1) {
tnhnrl 52:f207567d3ea4 1210 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1211 STATUS_key = pc().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1212 }
tnhnrl 52:f207567d3ea4 1213
tnhnrl 52:f207567d3ea4 1214 else {
tnhnrl 52:f207567d3ea4 1215
tnhnrl 52:f207567d3ea4 1216 wait(1);
tnhnrl 52:f207567d3ea4 1217 pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 52:f207567d3ea4 1218 pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 52:f207567d3ea4 1219 pc().printf("heading (rudder): %3.1f deg\r\n",rudderLoop().getPosition()); //for heading
tnhnrl 52:f207567d3ea4 1220
tnhnrl 52:f207567d3ea4 1221 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 52:f207567d3ea4 1222 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 52:f207567d3ea4 1223
tnhnrl 52:f207567d3ea4 1224 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 52:f207567d3ea4 1225 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 52:f207567d3ea4 1226
tnhnrl 52:f207567d3ea4 1227 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 52:f207567d3ea4 1228 pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 52:f207567d3ea4 1229
tnhnrl 52:f207567d3ea4 1230 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 52:f207567d3ea4 1231 pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 52:f207567d3ea4 1232
tnhnrl 52:f207567d3ea4 1233 pc().printf("\n\rNeutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
tnhnrl 52:f207567d3ea4 1234
tnhnrl 52:f207567d3ea4 1235 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1236 }
tnhnrl 52:f207567d3ea4 1237
tnhnrl 52:f207567d3ea4 1238 // process the keys
tnhnrl 52:f207567d3ea4 1239 if (STATUS_key == 'X') {
tnhnrl 52:f207567d3ea4 1240 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1241 }
tnhnrl 52:f207567d3ea4 1242
tnhnrl 52:f207567d3ea4 1243 else {
tnhnrl 52:f207567d3ea4 1244 pc().printf("\n\rThis key (%c) does nothing here. ", STATUS_key);
tnhnrl 52:f207567d3ea4 1245 }
tnhnrl 52:f207567d3ea4 1246 }
tnhnrl 52:f207567d3ea4 1247 }
tnhnrl 52:f207567d3ea4 1248
tnhnrl 52:f207567d3ea4 1249 void StateMachine::keyboard_menu_RUDDER_SERVO_settings() {
tnhnrl 52:f207567d3ea4 1250 //load current parameters from the rudder
tnhnrl 52:f207567d3ea4 1251 float rudder_min_pwm = rudder().getMinPWM();
tnhnrl 52:f207567d3ea4 1252 float rudder_max_pwm = rudder().getMaxPWM();
tnhnrl 52:f207567d3ea4 1253 float rudder_ctr_pwm = rudder().getCenterPWM();
tnhnrl 52:f207567d3ea4 1254 float rudder_min_deg = rudder().getMinDeg();
tnhnrl 52:f207567d3ea4 1255 float rudder_max_deg = rudder().getMaxDeg();
tnhnrl 52:f207567d3ea4 1256
tnhnrl 52:f207567d3ea4 1257 char RUDDER_PID_key;
tnhnrl 52:f207567d3ea4 1258
tnhnrl 52:f207567d3ea4 1259 // print the menu
tnhnrl 52:f207567d3ea4 1260 pc().printf("\n\rRUDDER (servo driver) settings (MENU)");
tnhnrl 52:f207567d3ea4 1261 pc().printf("\n\r(Adjust min/max/center PWM settings with the following keys: N and M and C");
tnhnrl 52:f207567d3ea4 1262 pc().printf("\n\r(Adjust DEGREE limit settings with the following keys: min = K, max = L");
tnhnrl 52:f207567d3ea4 1263 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 52:f207567d3ea4 1264 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 1265
tnhnrl 52:f207567d3ea4 1266 // handle the key presses
tnhnrl 52:f207567d3ea4 1267 while(1) {
tnhnrl 52:f207567d3ea4 1268 // get the user's keystroke from either of the two inputs
tnhnrl 52:f207567d3ea4 1269 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1270 RUDDER_PID_key = pc().getc();
tnhnrl 52:f207567d3ea4 1271 }
tnhnrl 52:f207567d3ea4 1272 else {
tnhnrl 52:f207567d3ea4 1273 wait(0.5);
tnhnrl 52:f207567d3ea4 1274 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 1275 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1276 }
tnhnrl 52:f207567d3ea4 1277
tnhnrl 52:f207567d3ea4 1278 // handle the user's key input
tnhnrl 52:f207567d3ea4 1279 if (RUDDER_PID_key == 'S') { // user wants to save the modified values
tnhnrl 52:f207567d3ea4 1280 // set global values
tnhnrl 52:f207567d3ea4 1281 rudder().setMinPWM(rudder_min_pwm);
tnhnrl 52:f207567d3ea4 1282 rudder().setMaxPWM(rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1283 rudder().setCenterPWM(rudder_ctr_pwm);
tnhnrl 52:f207567d3ea4 1284 rudder().setMinDeg(rudder_min_deg);
tnhnrl 52:f207567d3ea4 1285 rudder().setMaxDeg(rudder_max_deg);
tnhnrl 52:f207567d3ea4 1286
tnhnrl 52:f207567d3ea4 1287 // save rudder servo driver values for inner loop
tnhnrl 52:f207567d3ea4 1288 configFileIO().saveRudderData(rudder_min_deg, rudder_max_deg, rudder_ctr_pwm, rudder_min_pwm, rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1289 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 1290 }
tnhnrl 52:f207567d3ea4 1291 else if (RUDDER_PID_key == 'X') {
tnhnrl 52:f207567d3ea4 1292 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1293 }
tnhnrl 52:f207567d3ea4 1294 // MIN PWM
tnhnrl 52:f207567d3ea4 1295 else if (RUDDER_PID_key == 'N') {
tnhnrl 52:f207567d3ea4 1296 pc().printf(">> Type in rudder_min_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1297 rudder_min_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1298 }
tnhnrl 52:f207567d3ea4 1299 // MAX PWM
tnhnrl 52:f207567d3ea4 1300 else if (RUDDER_PID_key == 'M') {
tnhnrl 52:f207567d3ea4 1301 pc().printf(">> Type in rudder_max_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1302 rudder_max_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1303 }
tnhnrl 52:f207567d3ea4 1304 // CENTER PWM
tnhnrl 52:f207567d3ea4 1305 else if (RUDDER_PID_key == 'C') {
tnhnrl 52:f207567d3ea4 1306 pc().printf(">> Type in rudder_ctr_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1307 rudder_ctr_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1308 }
tnhnrl 52:f207567d3ea4 1309 // MIN DEG
tnhnrl 52:f207567d3ea4 1310 else if (RUDDER_PID_key == 'K') {
tnhnrl 52:f207567d3ea4 1311 pc().printf(">> Type in rudder_min_deg with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1312 rudder_min_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1313 }
tnhnrl 52:f207567d3ea4 1314 // MAX DEG
tnhnrl 52:f207567d3ea4 1315 else if (RUDDER_PID_key == 'L') {
tnhnrl 52:f207567d3ea4 1316 pc().printf(">> Type in rudder_max_deg with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1317 rudder_max_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1318 }
tnhnrl 52:f207567d3ea4 1319
tnhnrl 52:f207567d3ea4 1320 else {
tnhnrl 52:f207567d3ea4 1321 pc().printf("RUDDER SETUP: [%c] This key does nothing here. \r", RUDDER_PID_key);
tnhnrl 52:f207567d3ea4 1322 }
tnhnrl 52:f207567d3ea4 1323 }
tnhnrl 52:f207567d3ea4 1324 }
tnhnrl 20:8987a9ae2bc7 1325
tnhnrl 49:47ffa4feb6db 1326 void StateMachine::keyboard_menu_MANUAL_TUNING() {
tnhnrl 49:47ffa4feb6db 1327 char TUNING_key;
tnhnrl 49:47ffa4feb6db 1328
tnhnrl 49:47ffa4feb6db 1329 // show the menu
tnhnrl 49:47ffa4feb6db 1330 pc().printf("\n\r1: MANUAL TUNING MENU (EXIT WITH 'X' !)");
tnhnrl 49:47ffa4feb6db 1331 pc().printf("\n\r(Adjust BCE and BATT positions in real-time. Timeout NOT running! (decrease/increase BCE with A/S, BATT with Q/W)\n\r");
tnhnrl 49:47ffa4feb6db 1332 pc().printf("MANUAL_TUNING: BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 49:47ffa4feb6db 1333
tnhnrl 49:47ffa4feb6db 1334 // what needs to be started?
tnhnrl 49:47ffa4feb6db 1335 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 1336 batt().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 1337
tnhnrl 49:47ffa4feb6db 1338 while (1) {
tnhnrl 49:47ffa4feb6db 1339 if (pc().readable()) {
tnhnrl 49:47ffa4feb6db 1340 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 49:47ffa4feb6db 1341 }
tnhnrl 49:47ffa4feb6db 1342
tnhnrl 49:47ffa4feb6db 1343 else {
tnhnrl 52:f207567d3ea4 1344 pc().printf("MT: ACTUAL POS (SET POS): BCE: %0.1f (%0.1f), BATT: %0.1f (%0.1f)\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm());
tnhnrl 49:47ffa4feb6db 1345 continue; // didn't get a user input, so keep waiting for it
tnhnrl 49:47ffa4feb6db 1346 }
tnhnrl 49:47ffa4feb6db 1347
tnhnrl 49:47ffa4feb6db 1348 // process the keys
tnhnrl 49:47ffa4feb6db 1349 if (TUNING_key == 'X') {
tnhnrl 49:47ffa4feb6db 1350 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 49:47ffa4feb6db 1351 bce().pause();
tnhnrl 49:47ffa4feb6db 1352 batt().pause();
tnhnrl 49:47ffa4feb6db 1353
tnhnrl 49:47ffa4feb6db 1354 break; //exit the while loop
tnhnrl 49:47ffa4feb6db 1355 }
tnhnrl 49:47ffa4feb6db 1356
tnhnrl 49:47ffa4feb6db 1357 else if (TUNING_key == 'A') {
tnhnrl 49:47ffa4feb6db 1358 _neutral_bce_pos_mm = _neutral_bce_pos_mm - 1;
tnhnrl 49:47ffa4feb6db 1359 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 1360 pc().printf("MANUAL_TUNING: (BCE CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 49:47ffa4feb6db 1361 }
tnhnrl 49:47ffa4feb6db 1362
tnhnrl 52:f207567d3ea4 1363 else if (TUNING_key == 'A') {
tnhnrl 52:f207567d3ea4 1364 _neutral_bce_pos_mm = _neutral_bce_pos_mm - 1;
tnhnrl 52:f207567d3ea4 1365 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 52:f207567d3ea4 1366 pc().printf("MANUAL_TUNING: (BCE CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 52:f207567d3ea4 1367 }
tnhnrl 52:f207567d3ea4 1368
tnhnrl 49:47ffa4feb6db 1369 else if (TUNING_key == 'S') {
tnhnrl 49:47ffa4feb6db 1370 _neutral_bce_pos_mm = _neutral_bce_pos_mm + 1;
tnhnrl 49:47ffa4feb6db 1371 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 1372 pc().printf("MANUAL_TUNING: (BCE CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 49:47ffa4feb6db 1373 }
tnhnrl 49:47ffa4feb6db 1374
tnhnrl 49:47ffa4feb6db 1375 else if (TUNING_key == 'Q') {
tnhnrl 49:47ffa4feb6db 1376 _neutral_batt_pos_mm = _neutral_batt_pos_mm - 1;
tnhnrl 49:47ffa4feb6db 1377 batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 1378 pc().printf("MANUAL_TUNING: (BATT CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 49:47ffa4feb6db 1379 }
tnhnrl 49:47ffa4feb6db 1380
tnhnrl 49:47ffa4feb6db 1381 else if (TUNING_key == 'W') {
tnhnrl 49:47ffa4feb6db 1382 _neutral_batt_pos_mm = _neutral_batt_pos_mm + 1;
tnhnrl 49:47ffa4feb6db 1383 batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 1384 pc().printf("MANUAL_TUNING: (BATT CHANGE: %0.1f) BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition());
tnhnrl 49:47ffa4feb6db 1385 }
tnhnrl 49:47ffa4feb6db 1386
tnhnrl 49:47ffa4feb6db 1387 else {
tnhnrl 49:47ffa4feb6db 1388 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 49:47ffa4feb6db 1389 }
tnhnrl 49:47ffa4feb6db 1390
tnhnrl 49:47ffa4feb6db 1391 }
tnhnrl 49:47ffa4feb6db 1392 }
tnhnrl 52:f207567d3ea4 1393
tnhnrl 52:f207567d3ea4 1394 void StateMachine::keyboard_menu_CHANNEL_READINGS() {
tnhnrl 52:f207567d3ea4 1395 char TUNING_key;
tnhnrl 52:f207567d3ea4 1396
tnhnrl 52:f207567d3ea4 1397 // show the menu
tnhnrl 52:f207567d3ea4 1398 pc().printf("\n\r1: CHANNEL READINGS (EXIT WITH 'X' !)");
tnhnrl 52:f207567d3ea4 1399
tnhnrl 52:f207567d3ea4 1400 while (1) {
tnhnrl 52:f207567d3ea4 1401 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1402 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1403 }
tnhnrl 52:f207567d3ea4 1404
tnhnrl 52:f207567d3ea4 1405 else {
tnhnrl 52:f207567d3ea4 1406 // Testing out ADC
tnhnrl 52:f207567d3ea4 1407 float vref = 5.6;
tnhnrl 52:f207567d3ea4 1408 float vmeasured = 0;
tnhnrl 52:f207567d3ea4 1409 unsigned int raw = adc().readCh5();
tnhnrl 52:f207567d3ea4 1410 vmeasured = ((float)raw)/4095.0*vref;
tnhnrl 52:f207567d3ea4 1411
tnhnrl 52:f207567d3ea4 1412 pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
tnhnrl 52:f207567d3ea4 1413 pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
tnhnrl 52:f207567d3ea4 1414 pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
tnhnrl 52:f207567d3ea4 1415 pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
tnhnrl 52:f207567d3ea4 1416 pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
tnhnrl 52:f207567d3ea4 1417 pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
tnhnrl 52:f207567d3ea4 1418 pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
tnhnrl 52:f207567d3ea4 1419 pc().printf("raw board current: %d \r\n",adc().readCh7());
tnhnrl 52:f207567d3ea4 1420 pc().printf("raw BCE limit switch: %d \r\n",bce().getSwitch());
tnhnrl 52:f207567d3ea4 1421 pc().printf("raw BMM limit switch: %d \r\n",batt().getSwitch());
tnhnrl 52:f207567d3ea4 1422 pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
tnhnrl 52:f207567d3ea4 1423 // End of ADC Test
tnhnrl 52:f207567d3ea4 1424
tnhnrl 52:f207567d3ea4 1425 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1426 }
tnhnrl 52:f207567d3ea4 1427 }
tnhnrl 52:f207567d3ea4 1428 }
tnhnrl 49:47ffa4feb6db 1429
tnhnrl 16:3363b9f14913 1430 void StateMachine::keyboard_menu_BCE_PID_settings() {
tnhnrl 16:3363b9f14913 1431 char PID_key;
tnhnrl 16:3363b9f14913 1432 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 38:83d06c294807 1433 float BCE_KP = bce().getControllerP(); // load current value
tnhnrl 38:83d06c294807 1434 float BCE_KI = bce().getControllerI(); // load current global value
tnhnrl 38:83d06c294807 1435 float BCE_KD = bce().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1436
tnhnrl 16:3363b9f14913 1437 // show the menu
tnhnrl 16:3363b9f14913 1438 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1439 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1440 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 21:38c8544db6f4 1441 pc().printf("bce P: %3.2f, I: %3.2f, D %3.2f, zero %d, limit %3.0f mm, slope %3.3f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
tnhnrl 16:3363b9f14913 1442
tnhnrl 16:3363b9f14913 1443 // handle the key presses
tnhnrl 16:3363b9f14913 1444 while(1) {
tnhnrl 16:3363b9f14913 1445 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1446 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1447 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1448 }
tnhnrl 16:3363b9f14913 1449 else {
tnhnrl 16:3363b9f14913 1450 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1451 }
tnhnrl 16:3363b9f14913 1452
tnhnrl 16:3363b9f14913 1453 // handle the user's key input
tnhnrl 16:3363b9f14913 1454 if (PID_key == '-') {
tnhnrl 38:83d06c294807 1455 BCE_KP -= gain_step_size;
tnhnrl 38:83d06c294807 1456 pc().printf("P gain: %0.5f \r\n", BCE_KP);
tnhnrl 16:3363b9f14913 1457 }
tnhnrl 16:3363b9f14913 1458 else if (PID_key == '=') {
tnhnrl 38:83d06c294807 1459 BCE_KP += gain_step_size;
tnhnrl 38:83d06c294807 1460 pc().printf("P gain: %0.5f \r\n", BCE_KP);
tnhnrl 16:3363b9f14913 1461 }
tnhnrl 16:3363b9f14913 1462 else if (PID_key == '[') {
tnhnrl 38:83d06c294807 1463 BCE_KI -= gain_step_size;
tnhnrl 38:83d06c294807 1464 pc().printf("I gain: %0.5f \r\n", BCE_KI);
tnhnrl 16:3363b9f14913 1465 }
tnhnrl 16:3363b9f14913 1466 else if (PID_key == ']') {
tnhnrl 38:83d06c294807 1467 BCE_KI += gain_step_size;
tnhnrl 38:83d06c294807 1468 pc().printf("I gain: %0.5f \r\n", BCE_KI);
tnhnrl 16:3363b9f14913 1469 }
tnhnrl 16:3363b9f14913 1470 else if (PID_key == ';') {
tnhnrl 38:83d06c294807 1471 BCE_KD -= gain_step_size;
tnhnrl 38:83d06c294807 1472 pc().printf("D gain: %0.5f \r\n", BCE_KD);
tnhnrl 16:3363b9f14913 1473 }
tnhnrl 16:3363b9f14913 1474 else if (PID_key == '\'') {
tnhnrl 38:83d06c294807 1475 BCE_KD += gain_step_size;
tnhnrl 38:83d06c294807 1476 pc().printf("D gain: %0.5f \r\n", BCE_KD);
tnhnrl 16:3363b9f14913 1477 }
tnhnrl 16:3363b9f14913 1478 else if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 16:3363b9f14913 1479 // set values
tnhnrl 38:83d06c294807 1480 bce().setControllerP(BCE_KP);
tnhnrl 38:83d06c294807 1481 bce().setControllerI(BCE_KI);
tnhnrl 38:83d06c294807 1482 bce().setControllerD(BCE_KD);
tnhnrl 16:3363b9f14913 1483
tnhnrl 38:83d06c294807 1484 // save to "BATT.TXT" file
tnhnrl 38:83d06c294807 1485 configFileIO().saveBCEData(BCE_KP, BCE_KI, BCE_KD);
tnhnrl 38:83d06c294807 1486
tnhnrl 16:3363b9f14913 1487 break; //exit the while loop
tnhnrl 16:3363b9f14913 1488 }
tnhnrl 16:3363b9f14913 1489 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1490 break; //exit the while loop
tnhnrl 16:3363b9f14913 1491 }
tnhnrl 16:3363b9f14913 1492 else {
tnhnrl 16:3363b9f14913 1493 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1494 }
tnhnrl 16:3363b9f14913 1495 }
tnhnrl 16:3363b9f14913 1496 }
tnhnrl 20:8987a9ae2bc7 1497
tnhnrl 16:3363b9f14913 1498 void StateMachine::keyboard_menu_BATT_PID_settings() {
tnhnrl 16:3363b9f14913 1499 char PID_key;
tnhnrl 16:3363b9f14913 1500 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 38:83d06c294807 1501 float batt_KP = batt().getControllerP(); // load current global value
tnhnrl 38:83d06c294807 1502 float batt_KI = batt().getControllerI(); // load current global value
tnhnrl 38:83d06c294807 1503 float batt_KD = batt().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1504
tnhnrl 16:3363b9f14913 1505 // print the menu
tnhnrl 16:3363b9f14913 1506 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1507 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1508 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 21:38c8544db6f4 1509 pc().printf("batt P: %3.2f, I: %3.2f, D %3.2f, zero %d, limit %3.0f mm, slope %3.3f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
tnhnrl 20:8987a9ae2bc7 1510
tnhnrl 16:3363b9f14913 1511 // handle the key presses
tnhnrl 16:3363b9f14913 1512 while(1) {
tnhnrl 16:3363b9f14913 1513 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1514 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1515 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1516 }
tnhnrl 16:3363b9f14913 1517 else {
tnhnrl 16:3363b9f14913 1518 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1519 }
tnhnrl 16:3363b9f14913 1520
tnhnrl 16:3363b9f14913 1521 // handle the user's key input
tnhnrl 16:3363b9f14913 1522 if (PID_key == '-') {
tnhnrl 38:83d06c294807 1523 batt_KP -= gain_step_size;
tnhnrl 38:83d06c294807 1524 pc().printf("\rP gain: %0.5f ", batt_KP);
tnhnrl 16:3363b9f14913 1525 }
tnhnrl 16:3363b9f14913 1526 else if (PID_key == '=') {
tnhnrl 38:83d06c294807 1527 batt_KP += gain_step_size;
tnhnrl 38:83d06c294807 1528 pc().printf("\rP gain: %0.5f ", batt_KP);
tnhnrl 16:3363b9f14913 1529 }
tnhnrl 16:3363b9f14913 1530 else if (PID_key == '[') {
tnhnrl 38:83d06c294807 1531 batt_KI -= gain_step_size;
tnhnrl 38:83d06c294807 1532 pc().printf("\rI gain: %0.5f ", batt_KI);
tnhnrl 16:3363b9f14913 1533 }
tnhnrl 16:3363b9f14913 1534 else if (PID_key == ']') {
tnhnrl 38:83d06c294807 1535 batt_KI += gain_step_size;
tnhnrl 38:83d06c294807 1536 pc().printf("\rI gain: %0.5f ", batt_KI);
tnhnrl 16:3363b9f14913 1537 }
tnhnrl 16:3363b9f14913 1538 else if (PID_key == ';') {
tnhnrl 38:83d06c294807 1539 batt_KD -= gain_step_size;
tnhnrl 38:83d06c294807 1540 pc().printf("\rD gain: %0.5f ", batt_KD);
tnhnrl 16:3363b9f14913 1541 }
tnhnrl 16:3363b9f14913 1542 else if (PID_key == '\'') {
tnhnrl 38:83d06c294807 1543 batt_KD += gain_step_size;
tnhnrl 38:83d06c294807 1544 pc().printf("\rD gain: %0.5f ", batt_KD);
tnhnrl 16:3363b9f14913 1545 }
tnhnrl 16:3363b9f14913 1546 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1547 // set global values
tnhnrl 38:83d06c294807 1548 batt().setControllerP(batt_KP);
tnhnrl 38:83d06c294807 1549 batt().setControllerI(batt_KI);
tnhnrl 38:83d06c294807 1550 batt().setControllerD(batt_KD);
tnhnrl 16:3363b9f14913 1551
tnhnrl 38:83d06c294807 1552 // save to "BATT.TXT" file
tnhnrl 38:83d06c294807 1553 configFileIO().saveBattData(batt_KP, batt_KI, batt_KD);
tnhnrl 38:83d06c294807 1554
tnhnrl 16:3363b9f14913 1555 break; //exit the while loop
tnhnrl 16:3363b9f14913 1556 }
tnhnrl 16:3363b9f14913 1557 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1558 break; //exit the while loop
tnhnrl 16:3363b9f14913 1559 }
tnhnrl 16:3363b9f14913 1560 else {
tnhnrl 16:3363b9f14913 1561 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1562 }
tnhnrl 16:3363b9f14913 1563 }
tnhnrl 16:3363b9f14913 1564 }
tnhnrl 20:8987a9ae2bc7 1565
tnhnrl 16:3363b9f14913 1566 void StateMachine::keyboard_menu_DEPTH_PID_settings() {
tnhnrl 16:3363b9f14913 1567 char PID_key;
tnhnrl 16:3363b9f14913 1568 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1569
tnhnrl 16:3363b9f14913 1570 // show the menu
tnhnrl 16:3363b9f14913 1571 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1572 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1573 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r");
tnhnrl 16:3363b9f14913 1574 pc().printf("depth P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 1575
tnhnrl 16:3363b9f14913 1576 // handle the key presses
tnhnrl 16:3363b9f14913 1577 while(1) {
tnhnrl 16:3363b9f14913 1578 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1579 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1580 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1581 }
tnhnrl 16:3363b9f14913 1582 else {
tnhnrl 16:3363b9f14913 1583 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1584 }
tnhnrl 16:3363b9f14913 1585
tnhnrl 16:3363b9f14913 1586 // handle the user's key input
tnhnrl 16:3363b9f14913 1587 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1588 _depth_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1589 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1590 }
tnhnrl 16:3363b9f14913 1591 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1592 _depth_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1593 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1594 }
tnhnrl 16:3363b9f14913 1595 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1596 _depth_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1597 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1598 }
tnhnrl 16:3363b9f14913 1599 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1600 _depth_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1601 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1602 }
tnhnrl 16:3363b9f14913 1603 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1604 _depth_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1605 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1606 }
tnhnrl 16:3363b9f14913 1607 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1608 _depth_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1609 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1610 }
tnhnrl 16:3363b9f14913 1611 else if (PID_key == 'S') { // user wants to save these settings
tnhnrl 16:3363b9f14913 1612 // set global values
tnhnrl 21:38c8544db6f4 1613 depthLoop().setControllerP(_depth_KP);
tnhnrl 21:38c8544db6f4 1614 depthLoop().setControllerI(_depth_KI);
tnhnrl 21:38c8544db6f4 1615 depthLoop().setControllerD(_depth_KD);
tnhnrl 16:3363b9f14913 1616
tnhnrl 21:38c8544db6f4 1617 // save depth PID values for outer loop
tnhnrl 21:38c8544db6f4 1618 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1619 break; //exit the while loop
tnhnrl 16:3363b9f14913 1620 }
tnhnrl 16:3363b9f14913 1621 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1622 break; //exit the while loop
tnhnrl 16:3363b9f14913 1623 }
tnhnrl 16:3363b9f14913 1624 else {
tnhnrl 16:3363b9f14913 1625 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1626 }
tnhnrl 16:3363b9f14913 1627 }
tnhnrl 16:3363b9f14913 1628 }
tnhnrl 16:3363b9f14913 1629
tnhnrl 16:3363b9f14913 1630 void StateMachine::keyboard_menu_PITCH_PID_settings() {
tnhnrl 16:3363b9f14913 1631 char PID_key;
tnhnrl 16:3363b9f14913 1632 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1633
tnhnrl 16:3363b9f14913 1634 // print the menu
tnhnrl 16:3363b9f14913 1635 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1636 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1637 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 16:3363b9f14913 1638 pc().printf("pitch P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
tnhnrl 20:8987a9ae2bc7 1639
tnhnrl 16:3363b9f14913 1640 // handle the key presses
tnhnrl 16:3363b9f14913 1641 while(1) {
tnhnrl 16:3363b9f14913 1642 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1643 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1644 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1645 }
tnhnrl 16:3363b9f14913 1646 else {
tnhnrl 16:3363b9f14913 1647 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1648 }
tnhnrl 16:3363b9f14913 1649
tnhnrl 16:3363b9f14913 1650 // handle the user's key input
tnhnrl 16:3363b9f14913 1651 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1652 _pitch_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1653 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1654 }
tnhnrl 16:3363b9f14913 1655 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1656 _pitch_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1657 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1658 }
tnhnrl 16:3363b9f14913 1659 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1660 _pitch_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1661 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1662 }
tnhnrl 16:3363b9f14913 1663 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1664 _pitch_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1665 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1666 }
tnhnrl 16:3363b9f14913 1667 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1668 _pitch_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1669 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1670 }
tnhnrl 16:3363b9f14913 1671 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1672 _pitch_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1673 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1674 }
tnhnrl 16:3363b9f14913 1675 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1676 // set global values
tnhnrl 21:38c8544db6f4 1677 pitchLoop().setControllerP(_pitch_KP);
tnhnrl 21:38c8544db6f4 1678 pitchLoop().setControllerI(_pitch_KI);
tnhnrl 21:38c8544db6f4 1679 pitchLoop().setControllerD(_pitch_KD);
tnhnrl 16:3363b9f14913 1680
tnhnrl 32:f2f8ae34aadc 1681 // save pitch PID values for outer loop (must save neutral position also)
tnhnrl 21:38c8544db6f4 1682 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1683 break; //exit the while loop
tnhnrl 16:3363b9f14913 1684 }
tnhnrl 16:3363b9f14913 1685 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1686 break; //exit the while loop
tnhnrl 16:3363b9f14913 1687 }
tnhnrl 16:3363b9f14913 1688 else {
tnhnrl 16:3363b9f14913 1689 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1690 }
tnhnrl 16:3363b9f14913 1691 }
tnhnrl 16:3363b9f14913 1692 }
tnhnrl 20:8987a9ae2bc7 1693
tnhnrl 16:3363b9f14913 1694 float StateMachine::getDepthCommand() {
tnhnrl 32:f2f8ae34aadc 1695 return _depth_command;
tnhnrl 16:3363b9f14913 1696 }
tnhnrl 20:8987a9ae2bc7 1697
tnhnrl 16:3363b9f14913 1698 float StateMachine::getPitchCommand() {
tnhnrl 32:f2f8ae34aadc 1699 return _pitch_command;
tnhnrl 32:f2f8ae34aadc 1700 }
tnhnrl 32:f2f8ae34aadc 1701
tnhnrl 32:f2f8ae34aadc 1702 float StateMachine::getDepthReading() {
tnhnrl 32:f2f8ae34aadc 1703 return _depth_reading;
tnhnrl 32:f2f8ae34aadc 1704 }
tnhnrl 32:f2f8ae34aadc 1705
tnhnrl 32:f2f8ae34aadc 1706 float StateMachine::getPitchReading() {
tnhnrl 32:f2f8ae34aadc 1707 return _pitch_reading;
tnhnrl 32:f2f8ae34aadc 1708 }
tnhnrl 32:f2f8ae34aadc 1709
tnhnrl 32:f2f8ae34aadc 1710 float StateMachine::getTimerReading() {
tnhnrl 32:f2f8ae34aadc 1711 return _timer_reading;
tnhnrl 17:7c16b5671d0e 1712 }
tnhnrl 28:16c83a2fdefa 1713
tnhnrl 17:7c16b5671d0e 1714 void StateMachine::setState(int input_state) {
tnhnrl 21:38c8544db6f4 1715 _state = input_state;
tnhnrl 17:7c16b5671d0e 1716 }
tnhnrl 20:8987a9ae2bc7 1717
tnhnrl 17:7c16b5671d0e 1718 int StateMachine::getState() {
tnhnrl 17:7c16b5671d0e 1719 return _state; //return the current state of the system
tnhnrl 17:7c16b5671d0e 1720 }
tnhnrl 20:8987a9ae2bc7 1721
tnhnrl 17:7c16b5671d0e 1722 void StateMachine::setTimeout(float input_timeout) {
tnhnrl 17:7c16b5671d0e 1723 _timeout = input_timeout;
tnhnrl 17:7c16b5671d0e 1724 }
tnhnrl 20:8987a9ae2bc7 1725
tnhnrl 17:7c16b5671d0e 1726 void StateMachine::setDepthCommand(float input_depth_command) {
tnhnrl 32:f2f8ae34aadc 1727 _depth_command = input_depth_command;
tnhnrl 17:7c16b5671d0e 1728 }
tnhnrl 20:8987a9ae2bc7 1729
tnhnrl 17:7c16b5671d0e 1730 void StateMachine::setPitchCommand(float input_pitch_command) {
tnhnrl 32:f2f8ae34aadc 1731 _pitch_command = input_pitch_command;
tnhnrl 17:7c16b5671d0e 1732 }
tnhnrl 20:8987a9ae2bc7 1733
tnhnrl 17:7c16b5671d0e 1734 void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) {
tnhnrl 21:38c8544db6f4 1735 _neutral_batt_pos_mm = batt_pos_mm;
tnhnrl 21:38c8544db6f4 1736 _neutral_bce_pos_mm = bce_pos_mm;
tnhnrl 17:7c16b5671d0e 1737
tnhnrl 21:38c8544db6f4 1738 pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm);
tnhnrl 17:7c16b5671d0e 1739 }
tnhnrl 20:8987a9ae2bc7 1740
tnhnrl 17:7c16b5671d0e 1741 int StateMachine::timeoutRunning() {
tnhnrl 28:16c83a2fdefa 1742 return _isTimeoutRunning;
tnhnrl 17:7c16b5671d0e 1743 }
tnhnrl 20:8987a9ae2bc7 1744
tnhnrl 17:7c16b5671d0e 1745 //process one state at a time
tnhnrl 17:7c16b5671d0e 1746 void StateMachine::getDiveSequence() {
tnhnrl 17:7c16b5671d0e 1747 //iterate through this sequence using the FSM
tnhnrl 24:c7d9b5bf3829 1748 currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state;
tnhnrl 24:c7d9b5bf3829 1749 currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout;
tnhnrl 24:c7d9b5bf3829 1750 currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth;
tnhnrl 24:c7d9b5bf3829 1751 currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch;
tnhnrl 17:7c16b5671d0e 1752
tnhnrl 17:7c16b5671d0e 1753 _timeout = currentStateStruct.timeout; //set timeout before exiting this function
tnhnrl 32:f2f8ae34aadc 1754 }
tnhnrl 32:f2f8ae34aadc 1755
tnhnrl 32:f2f8ae34aadc 1756 void StateMachine::printCurrentSdLog() {
tnhnrl 32:f2f8ae34aadc 1757 pc().printf("SD card log work in progress\n\r");
tnhnrl 32:f2f8ae34aadc 1758 //might be worth saving the last few logs to the MBED...
tnhnrl 32:f2f8ae34aadc 1759 }
tnhnrl 32:f2f8ae34aadc 1760
tnhnrl 32:f2f8ae34aadc 1761 //check if the file is still opened
tnhnrl 32:f2f8ae34aadc 1762 void StateMachine::createNewFile() {
tnhnrl 32:f2f8ae34aadc 1763 if (_file_closed) {
tnhnrl 34:9b66c5188051 1764 //mbedLogger().createFile(); //create a new MBED file
tnhnrl 32:f2f8ae34aadc 1765
tnhnrl 32:f2f8ae34aadc 1766 _file_closed = false; //file is still open until you get to SIT_IDLE
tnhnrl 32:f2f8ae34aadc 1767 }
tnhnrl 32:f2f8ae34aadc 1768 }
tnhnrl 32:f2f8ae34aadc 1769
tnhnrl 32:f2f8ae34aadc 1770 void StateMachine::transmitData() {
tnhnrl 32:f2f8ae34aadc 1771 static float transmit_timer = 0;
tnhnrl 32:f2f8ae34aadc 1772 static bool is_transmit_timer_running = false;
tnhnrl 32:f2f8ae34aadc 1773
tnhnrl 32:f2f8ae34aadc 1774 if (!is_transmit_timer_running) {
tnhnrl 32:f2f8ae34aadc 1775 //pc().printf("\n\n\rTRANSMIT timer running...\n\n\r"); //debug
tnhnrl 32:f2f8ae34aadc 1776
tnhnrl 32:f2f8ae34aadc 1777 transmit_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 32:f2f8ae34aadc 1778 is_transmit_timer_running = true; //disable this block after one iteration
tnhnrl 32:f2f8ae34aadc 1779
tnhnrl 32:f2f8ae34aadc 1780 pc().printf("TESTING to see if this transmits once a second. (timer: %0.1f)\n\r", timer.read());
tnhnrl 32:f2f8ae34aadc 1781 }
tnhnrl 32:f2f8ae34aadc 1782 if (timer.read() >= transmit_timer) {
tnhnrl 32:f2f8ae34aadc 1783 is_transmit_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 32:f2f8ae34aadc 1784 }
tnhnrl 34:9b66c5188051 1785 }
tnhnrl 34:9b66c5188051 1786
tnhnrl 34:9b66c5188051 1787 void StateMachine::recordData(int input_state) {
tnhnrl 34:9b66c5188051 1788 string string_state;
tnhnrl 34:9b66c5188051 1789 if (input_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 1790 string_state = "SIT_IDLE";
tnhnrl 34:9b66c5188051 1791 else if (input_state == FIND_NEUTRAL)
tnhnrl 34:9b66c5188051 1792 string_state = "FIND_NEUTRAL";
tnhnrl 34:9b66c5188051 1793 else if (input_state == DIVE)
tnhnrl 34:9b66c5188051 1794 string_state = "DIVE";
tnhnrl 34:9b66c5188051 1795 else if (input_state == RISE)
tnhnrl 34:9b66c5188051 1796 string_state = "RISE";
tnhnrl 34:9b66c5188051 1797 else if (input_state == FLOAT_LEVEL)
tnhnrl 34:9b66c5188051 1798 string_state = "FLOAT_LEVEL";
tnhnrl 34:9b66c5188051 1799 else if (input_state == FLOAT_BROADCAST)
tnhnrl 34:9b66c5188051 1800 string_state = "FLOAT_BROADCAST";
tnhnrl 34:9b66c5188051 1801 else if (input_state == EMERGENCY_CLIMB)
tnhnrl 34:9b66c5188051 1802 string_state = "EMERGENCY_CLIMB";
tnhnrl 34:9b66c5188051 1803 else if (input_state == MULTI_DIVE)
tnhnrl 34:9b66c5188051 1804 string_state = "MULTI_DIVE";
tnhnrl 34:9b66c5188051 1805 else if (input_state == MULTI_RISE)
tnhnrl 34:9b66c5188051 1806 string_state = "MULTI_RISE";
tnhnrl 34:9b66c5188051 1807 else if (input_state == KEYBOARD)
tnhnrl 34:9b66c5188051 1808 string_state = "KEYBOARD";
tnhnrl 34:9b66c5188051 1809
tnhnrl 34:9b66c5188051 1810 if (!_is_log_timer_running) {
tnhnrl 34:9b66c5188051 1811 //pc().printf("\n\n\rlog timer running...\n\n\r"); //debug
tnhnrl 34:9b66c5188051 1812
tnhnrl 34:9b66c5188051 1813 _log_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 34:9b66c5188051 1814 _is_log_timer_running = true; //disable this block after one iteration
tnhnrl 34:9b66c5188051 1815
tnhnrl 37:357e98a929cc 1816 _data_log[0] = systemTime().read(); //system time reading
tnhnrl 34:9b66c5188051 1817 _data_log[1] = depthLoop().getCommand(); //depth command
tnhnrl 34:9b66c5188051 1818 _data_log[2] = depthLoop().getPosition(); //depth reading
tnhnrl 34:9b66c5188051 1819 _data_log[3] = pitchLoop().getCommand(); //pitch command
tnhnrl 34:9b66c5188051 1820 _data_log[4] = pitchLoop().getPosition(); //pitch reading
tnhnrl 34:9b66c5188051 1821 _data_log[5] = bce().getSetPosition_mm();
tnhnrl 34:9b66c5188051 1822 _data_log[6] = bce().getPosition_mm();
tnhnrl 34:9b66c5188051 1823 _data_log[7] = batt().getSetPosition_mm();
tnhnrl 34:9b66c5188051 1824 _data_log[8] = batt().getPosition_mm();
tnhnrl 34:9b66c5188051 1825
tnhnrl 34:9b66c5188051 1826 //record data to the MBED every 5 seconds
tnhnrl 35:2f66ea4863d5 1827 //mbedLogger().saveArrayToFile(string_state,input_state,_data_log);
tnhnrl 34:9b66c5188051 1828 }
tnhnrl 34:9b66c5188051 1829 if (timer.read() >= _log_timer) {
tnhnrl 34:9b66c5188051 1830 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 1831 }
tnhnrl 34:9b66c5188051 1832 }
tnhnrl 34:9b66c5188051 1833
tnhnrl 34:9b66c5188051 1834 void StateMachine::recordState(int input_state) {
tnhnrl 34:9b66c5188051 1835 string string_state;
tnhnrl 34:9b66c5188051 1836
tnhnrl 34:9b66c5188051 1837 if (input_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 1838 string_state = "SIT_IDLE";
tnhnrl 34:9b66c5188051 1839 else if (input_state == FIND_NEUTRAL)
tnhnrl 34:9b66c5188051 1840 string_state = "FIND_NEUTRAL";
tnhnrl 34:9b66c5188051 1841 else if (input_state == DIVE)
tnhnrl 34:9b66c5188051 1842 string_state = "DIVE";
tnhnrl 34:9b66c5188051 1843 else if (input_state == RISE)
tnhnrl 34:9b66c5188051 1844 string_state = "RISE";
tnhnrl 34:9b66c5188051 1845 else if (input_state == FLOAT_LEVEL)
tnhnrl 34:9b66c5188051 1846 string_state = "FLOAT_LEVEL";
tnhnrl 34:9b66c5188051 1847 else if (input_state == FLOAT_BROADCAST)
tnhnrl 34:9b66c5188051 1848 string_state = "FLOAT_BROADCAST";
tnhnrl 34:9b66c5188051 1849 else if (input_state == EMERGENCY_CLIMB)
tnhnrl 34:9b66c5188051 1850 string_state = "EMERGENCY_CLIMB";
tnhnrl 34:9b66c5188051 1851 else if (input_state == MULTI_DIVE)
tnhnrl 34:9b66c5188051 1852 string_state = "MULTI_DIVE";
tnhnrl 34:9b66c5188051 1853 else if (input_state == MULTI_RISE)
tnhnrl 34:9b66c5188051 1854 string_state = "MULTI_RISE";
tnhnrl 34:9b66c5188051 1855 else if (input_state == KEYBOARD)
tnhnrl 34:9b66c5188051 1856 string_state = "KEYBOARD";
tnhnrl 34:9b66c5188051 1857 //datalogger().printf("%s\n", string_state.c_str());
tnhnrl 36:966a86937e17 1858 }
tnhnrl 36:966a86937e17 1859
tnhnrl 36:966a86937e17 1860 float * StateMachine::dataArray() {
tnhnrl 36:966a86937e17 1861 //return the array to a calling function
tnhnrl 36:966a86937e17 1862 return _data_log;
tnhnrl 52:f207567d3ea4 1863 }
tnhnrl 52:f207567d3ea4 1864
tnhnrl 52:f207567d3ea4 1865 // 06/06/2018
tnhnrl 52:f207567d3ea4 1866 float StateMachine::getFloatUserInput() {
tnhnrl 52:f207567d3ea4 1867 float float_conversion = 0.0;
tnhnrl 52:f207567d3ea4 1868
tnhnrl 52:f207567d3ea4 1869 while(1) {
tnhnrl 52:f207567d3ea4 1870 bool valid_input = false; //flag for valid or invalid input
tnhnrl 52:f207567d3ea4 1871
tnhnrl 52:f207567d3ea4 1872 pc().printf("Please enter your number below and press ENTER:\n\r");
tnhnrl 52:f207567d3ea4 1873 char user_string [80]; //variable to store input as a character array
tnhnrl 52:f207567d3ea4 1874
tnhnrl 52:f207567d3ea4 1875 pc().scanf("%s", user_string); //read formatted data from stdin
tnhnrl 52:f207567d3ea4 1876 pc().printf("user_string was <%s>\n\r", user_string);
tnhnrl 52:f207567d3ea4 1877
tnhnrl 52:f207567d3ea4 1878 //check through the string for invalid characters (decimal values 43 through 57)
tnhnrl 52:f207567d3ea4 1879 for (int c = 0; c < strlen(user_string); c++) {
tnhnrl 52:f207567d3ea4 1880 //pc().printf("character is [%c]\n\r", user_string[c]); //debug
tnhnrl 52:f207567d3ea4 1881 if (user_string[c] >= 43 and user_string[c] <= 57) {
tnhnrl 52:f207567d3ea4 1882 //pc().printf("VALID CHARACTER!\n\r"); //debug
tnhnrl 52:f207567d3ea4 1883 ;
tnhnrl 52:f207567d3ea4 1884 }
tnhnrl 52:f207567d3ea4 1885 else {
tnhnrl 52:f207567d3ea4 1886 pc().printf("INVALID INPUT!\n\r");
tnhnrl 52:f207567d3ea4 1887 break;
tnhnrl 52:f207567d3ea4 1888 }
tnhnrl 52:f207567d3ea4 1889
tnhnrl 52:f207567d3ea4 1890 if (c == (strlen(user_string) - 1)) {
tnhnrl 52:f207567d3ea4 1891 valid_input = true;
tnhnrl 52:f207567d3ea4 1892 }
tnhnrl 52:f207567d3ea4 1893 }
tnhnrl 52:f207567d3ea4 1894
tnhnrl 52:f207567d3ea4 1895 if (valid_input) {
tnhnrl 52:f207567d3ea4 1896 float_conversion = atof(user_string);
tnhnrl 52:f207567d3ea4 1897 pc().printf("VALID INPUT! Your input was: %f\n\r", float_conversion);
tnhnrl 52:f207567d3ea4 1898 break;
tnhnrl 52:f207567d3ea4 1899 }
tnhnrl 52:f207567d3ea4 1900 }
tnhnrl 52:f207567d3ea4 1901
tnhnrl 52:f207567d3ea4 1902 return float_conversion;
tnhnrl 16:3363b9f14913 1903 }