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:
Fri Jun 08 13:56:30 2018 +0000
Revision:
54:d4990fb68404
Parent:
53:c0586fe62b01
Child:
55:f4ec445c42fe
heading not working

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 54:d4990fb68404 725 pc().printf(" 6 STREAM STATUS\r\n");
tnhnrl 53:c0586fe62b01 726 pc().printf(" 7 RUDDER SERVO settings menu\r\n");
tnhnrl 54:d4990fb68404 727 pc().printf(" 8 CHANNEL READINGS\r\n");
tnhnrl 53:c0586fe62b01 728
tnhnrl 28:16c83a2fdefa 729 pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 16:3363b9f14913 730 pc().printf(" ? to reset mbed\r\n");
tnhnrl 16:3363b9f14913 731 }
tnhnrl 20:8987a9ae2bc7 732
tnhnrl 17:7c16b5671d0e 733 //Find Neutral sub finite state machine
tnhnrl 17:7c16b5671d0e 734 // Note: the sub-fsm only moves the pistons once at the start of each timer loop
tnhnrl 17:7c16b5671d0e 735 // (timer completes, move piston, timer completes, move piston, etc)
tnhnrl 28:16c83a2fdefa 736 int StateMachine::runNeutralStateMachine() {
tnhnrl 24:c7d9b5bf3829 737 switch (_substate) {
tnhnrl 20:8987a9ae2bc7 738 case NEUTRAL_SINKING :
tnhnrl 17:7c16b5671d0e 739 //start the 10 second timer
tnhnrl 28:16c83a2fdefa 740 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 741 _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 742
tnhnrl 39:58375ca6b6ff 743 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 744
tnhnrl 32:f2f8ae34aadc 745 // what are the commands? (BCE linear actuator active, no pitch movement)
tnhnrl 39:58375ca6b6ff 746 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 747
tnhnrl 32:f2f8ae34aadc 748 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 749
tnhnrl 28:16c83a2fdefa 750 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 751 }
tnhnrl 20:8987a9ae2bc7 752
tnhnrl 20:8987a9ae2bc7 753 // how exit?
tnhnrl 20:8987a9ae2bc7 754 //once reached the travel limit, no need to keep trying, so exit
tnhnrl 25:249e4d56b27c 755 if (bce().getPosition_mm() <= 0) {
tnhnrl 25:249e4d56b27c 756 pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm());
tnhnrl 25:249e4d56b27c 757 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 758 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 25:249e4d56b27c 759 }
tnhnrl 20:8987a9ae2bc7 760 //once deeper than the commanded setpoint...
tnhnrl 32:f2f8ae34aadc 761 else if (depthLoop().getPosition() > _depth_command) {
tnhnrl 24:c7d9b5bf3829 762 _substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 28:16c83a2fdefa 763 _isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 20:8987a9ae2bc7 764 }
tnhnrl 20:8987a9ae2bc7 765
tnhnrl 20:8987a9ae2bc7 766 // what is active?
tnhnrl 20:8987a9ae2bc7 767 //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 768 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 769 pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! [current time: %0.1f]\r\n", timer.read());
tnhnrl 17:7c16b5671d0e 770
tnhnrl 28:16c83a2fdefa 771 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 772 }
tnhnrl 39:58375ca6b6ff 773
tnhnrl 39:58375ca6b6ff 774 // what is active? (only the buoyancy engine moved every 5 seconds at start)
tnhnrl 39:58375ca6b6ff 775 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 776 break;
tnhnrl 17:7c16b5671d0e 777
tnhnrl 17:7c16b5671d0e 778 case NEUTRAL_SLOWLY_RISE:
tnhnrl 28:16c83a2fdefa 779 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 780 _neutral_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 781
tnhnrl 24:c7d9b5bf3829 782 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 783
tnhnrl 20:8987a9ae2bc7 784 // what are the commands?
tnhnrl 32:f2f8ae34aadc 785 //move piston at start of sequence (default: extend 2.0 mm)
tnhnrl 39:58375ca6b6ff 786 bce().setPosition_mm(bce().getSetPosition_mm() + 2.0); //no depth command
tnhnrl 23:434f04ef1fad 787
tnhnrl 23:434f04ef1fad 788 // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state
tnhnrl 23:434f04ef1fad 789 pitchLoop().setCommand(0.0);
tnhnrl 24:c7d9b5bf3829 790
tnhnrl 32:f2f8ae34aadc 791 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 792
tnhnrl 28:16c83a2fdefa 793 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 794 }
tnhnrl 17:7c16b5671d0e 795
tnhnrl 20:8987a9ae2bc7 796 // how exit?
tnhnrl 24:c7d9b5bf3829 797 //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit
tnhnrl 24:c7d9b5bf3829 798 if (bce().getSetPosition_mm() >= bce().getTravelLimit()) {
tnhnrl 24:c7d9b5bf3829 799 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 800 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 801 }
tnhnrl 17:7c16b5671d0e 802 //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration
tnhnrl 20:8987a9ae2bc7 803 else if (depthLoop().getVelocity() < 0) { //less than zero ft/s
tnhnrl 24:c7d9b5bf3829 804 pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 ft/s [time: %0.1f]\r\n", timer.read());
tnhnrl 24:c7d9b5bf3829 805 _substate = NEUTRAL_CHECK_PITCH;
tnhnrl 28:16c83a2fdefa 806 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 807 }
tnhnrl 17:7c16b5671d0e 808
tnhnrl 20:8987a9ae2bc7 809 // what is active?
tnhnrl 20:8987a9ae2bc7 810 //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 811 if (timer.read() >= _neutral_timer) {
tnhnrl 32:f2f8ae34aadc 812 pc().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! [timer: %0.1f]\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 813
tnhnrl 28:16c83a2fdefa 814 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 815 }
tnhnrl 23:434f04ef1fad 816
tnhnrl 32:f2f8ae34aadc 817 // what is active? (only the buoyancy engine moved every 5 seconds)
tnhnrl 32:f2f8ae34aadc 818 pc().printf("depthLoop getOutput: %0.1f\r", depthLoop().getOutput()); //debug
tnhnrl 32:f2f8ae34aadc 819 bce().setPosition_mm(depthLoop().getOutput()); // (DID NOT WORK ON BENCH)
tnhnrl 17:7c16b5671d0e 820 break;
tnhnrl 17:7c16b5671d0e 821
danstrider 22:a10ee088403b 822 case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired
danstrider 22:a10ee088403b 823 // start local state timer and init any other one-shot actions
tnhnrl 23:434f04ef1fad 824
tnhnrl 28:16c83a2fdefa 825 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 826 _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds
tnhnrl 24:c7d9b5bf3829 827 pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read());
danstrider 22:a10ee088403b 828
tnhnrl 32:f2f8ae34aadc 829 // what are the commands? (default: retract or extend 0.5 mm)
tnhnrl 24:c7d9b5bf3829 830 if (pitchLoop().getPosition() > 2) { // nose is high
tnhnrl 39:58375ca6b6ff 831 batt().setPosition_mm(batt().getSetPosition_mm() + 0.5); // move battery forward (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 832 pc().printf("\n\rNeutral Check Pitch: moving battery FWD in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 833 }
tnhnrl 24:c7d9b5bf3829 834 else if (pitchLoop().getPosition() < -2) { // nose is low
tnhnrl 39:58375ca6b6ff 835 batt().setPosition_mm(batt().getSetPosition_mm() - 0.5); // move battery aft (using setpoint from linear actuator)
tnhnrl 32:f2f8ae34aadc 836 pc().printf("\n\rNeutral Check Pitch: moving battery AFT in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm);
danstrider 22:a10ee088403b 837 }
tnhnrl 24:c7d9b5bf3829 838
tnhnrl 28:16c83a2fdefa 839 _isSubStateTimerRunning = true; //disable this block after one iteration
danstrider 22:a10ee088403b 840 }
tnhnrl 20:8987a9ae2bc7 841
tnhnrl 28:16c83a2fdefa 842 // how exit?
tnhnrl 20:8987a9ae2bc7 843 //pitch angle and pitch rate within small tolerance
tnhnrl 20:8987a9ae2bc7 844 //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 23:434f04ef1fad 845 if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) {
tnhnrl 24:c7d9b5bf3829 846 pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug
danstrider 22:a10ee088403b 847 // found level, but don't need to save anything this time
tnhnrl 23:434f04ef1fad 848
tnhnrl 28:16c83a2fdefa 849 if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug
tnhnrl 28:16c83a2fdefa 850 _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 851 }
tnhnrl 28:16c83a2fdefa 852
tnhnrl 23:434f04ef1fad 853 // found level and at depth too, so save it all now
tnhnrl 32:f2f8ae34aadc 854 if (_substate == NEUTRAL_CHECK_PITCH) {
danstrider 22:a10ee088403b 855 //save positions locally
danstrider 22:a10ee088403b 856 _neutral_batt_pos_mm = batt().getPosition_mm();
danstrider 22:a10ee088403b 857 _neutral_bce_pos_mm = bce().getPosition_mm();
danstrider 22:a10ee088403b 858
danstrider 22:a10ee088403b 859 //set the neutral positions in each outer loop
danstrider 22:a10ee088403b 860 depthLoop().setOutputOffset(_neutral_bce_pos_mm);
danstrider 22:a10ee088403b 861 pitchLoop().setOutputOffset(_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 862
danstrider 22:a10ee088403b 863 // save into the depth.txt and pitch.txt files
danstrider 22:a10ee088403b 864 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset
danstrider 22:a10ee088403b 865 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset
danstrider 22:a10ee088403b 866
tnhnrl 32:f2f8ae34aadc 867 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 868
tnhnrl 24:c7d9b5bf3829 869 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 870 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 24:c7d9b5bf3829 871 }
tnhnrl 24:c7d9b5bf3829 872
tnhnrl 24:c7d9b5bf3829 873 else {
tnhnrl 24:c7d9b5bf3829 874 pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r");
tnhnrl 24:c7d9b5bf3829 875 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 876 }
tnhnrl 17:7c16b5671d0e 877 }
danstrider 22:a10ee088403b 878
danstrider 22:a10ee088403b 879 // what is active?
danstrider 22:a10ee088403b 880 //once timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 881 if (timer.read() >= _neutral_timer) {
danstrider 22:a10ee088403b 882 pc().printf("\r\n\nlevel timer COMPLETE!");
danstrider 22:a10ee088403b 883 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 884 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
danstrider 22:a10ee088403b 885 }
tnhnrl 17:7c16b5671d0e 886 break;
danstrider 22:a10ee088403b 887
danstrider 22:a10ee088403b 888 //this state could be removed, it is only used as a transition but is needed to stop entering this function
danstrider 22:a10ee088403b 889 case NEUTRAL_EXIT :
tnhnrl 23:434f04ef1fad 890 pc().printf("substate: NEUTRAL_EXIT\n\r");
tnhnrl 20:8987a9ae2bc7 891 break;
tnhnrl 21:38c8544db6f4 892
danstrider 22:a10ee088403b 893 default :
tnhnrl 24:c7d9b5bf3829 894 pc().printf("how did we get to substate: default?\n\r"); //debug
tnhnrl 23:434f04ef1fad 895 //a default within the sub-state machine
tnhnrl 24:c7d9b5bf3829 896 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 897 break;
tnhnrl 17:7c16b5671d0e 898 }
tnhnrl 20:8987a9ae2bc7 899
tnhnrl 30:2964617e7676 900 // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence)
tnhnrl 24:c7d9b5bf3829 901 if (_substate == NEUTRAL_EXIT) {
tnhnrl 24:c7d9b5bf3829 902 pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r");
tnhnrl 24:c7d9b5bf3829 903
tnhnrl 30:2964617e7676 904 //reset internal sub-state back to first entry conditions (first state is immediately sinking)
tnhnrl 30:2964617e7676 905 _substate = NEUTRAL_SINKING;
tnhnrl 28:16c83a2fdefa 906 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 21:38c8544db6f4 907
tnhnrl 24:c7d9b5bf3829 908 //record sub-states to view after sequence
tnhnrl 30:2964617e7676 909 _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array
tnhnrl 28:16c83a2fdefa 910 _substate_array_counter++;
tnhnrl 23:434f04ef1fad 911
tnhnrl 24:c7d9b5bf3829 912 //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit)
tnhnrl 24:c7d9b5bf3829 913 _previous_substate = -1;
tnhnrl 24:c7d9b5bf3829 914
tnhnrl 24:c7d9b5bf3829 915 //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 24:c7d9b5bf3829 916 return NEUTRAL_EXIT; // message to calling function we just exited
tnhnrl 21:38c8544db6f4 917 }
tnhnrl 23:434f04ef1fad 918 else {
tnhnrl 24:c7d9b5bf3829 919 //record sub-states to view after sequence (when changed)
tnhnrl 24:c7d9b5bf3829 920 if (_previous_substate != _substate) {
tnhnrl 28:16c83a2fdefa 921 _substate_array[_substate_array_counter] = _substate; //save current state to state array
tnhnrl 28:16c83a2fdefa 922 _substate_array_counter++;
tnhnrl 24:c7d9b5bf3829 923
tnhnrl 24:c7d9b5bf3829 924 //record the current substate for comparison
tnhnrl 24:c7d9b5bf3829 925 _previous_substate = _substate;
tnhnrl 24:c7d9b5bf3829 926 }
tnhnrl 24:c7d9b5bf3829 927
tnhnrl 24:c7d9b5bf3829 928 return _substate; // message to calling function of what sub-state it's in
tnhnrl 23:434f04ef1fad 929 }
tnhnrl 17:7c16b5671d0e 930 }
tnhnrl 20:8987a9ae2bc7 931
tnhnrl 20:8987a9ae2bc7 932 // keyboard runs independently of the state machine, handling one key at a time
tnhnrl 20:8987a9ae2bc7 933 //keyboard updates the desired _keyboard_state that is used in the state machine
tnhnrl 20:8987a9ae2bc7 934 //and only allows input when the state is "idle"
tnhnrl 17:7c16b5671d0e 935 void StateMachine::keyboard() {
tnhnrl 16:3363b9f14913 936 char userInput;
tnhnrl 20:8987a9ae2bc7 937
tnhnrl 16:3363b9f14913 938 // check keyboard and make settings changes as requested
tnhnrl 17:7c16b5671d0e 939 // states can be changed only at the start of a sequence (when the system is in SIT_IDLE)
tnhnrl 21:38c8544db6f4 940
tnhnrl 37:357e98a929cc 941 //TEST
tnhnrl 37:357e98a929cc 942 int _keyboard_state = 0; //made this a local variable because it was retaining the last keyboard state
tnhnrl 21:38c8544db6f4 943
tnhnrl 28:16c83a2fdefa 944 if (pc().readable() && (_state == SIT_IDLE || _state == KEYBOARD)) {
tnhnrl 16:3363b9f14913 945 // get the key
tnhnrl 17:7c16b5671d0e 946 userInput = pc().getc();
tnhnrl 17:7c16b5671d0e 947
tnhnrl 28:16c83a2fdefa 948 //record that the keyboard was used
tnhnrl 28:16c83a2fdefa 949 _state_array[_state_array_counter] = KEYBOARD;
tnhnrl 28:16c83a2fdefa 950 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 951
tnhnrl 21:38c8544db6f4 952 // keyboard has to reset timer each time it's used
tnhnrl 28:16c83a2fdefa 953 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 954
tnhnrl 16:3363b9f14913 955 // check command against desired control buttons
tnhnrl 49:47ffa4feb6db 956 if (userInput == 'D') {
tnhnrl 17:7c16b5671d0e 957 _keyboard_state = DIVE;
tnhnrl 16:3363b9f14913 958 }
tnhnrl 49:47ffa4feb6db 959 else if (userInput == 'N') {
tnhnrl 17:7c16b5671d0e 960 _keyboard_state = FIND_NEUTRAL;
tnhnrl 17:7c16b5671d0e 961 }
tnhnrl 49:47ffa4feb6db 962 else if (userInput == 'M') {
tnhnrl 17:7c16b5671d0e 963 //currently does not run if there is no file.
tnhnrl 17:7c16b5671d0e 964
tnhnrl 17:7c16b5671d0e 965 //need to add method to Sequence Controller that returns -1
tnhnrl 17:7c16b5671d0e 966 // or some check that insures you cannot run the dive sequence without a file
tnhnrl 17:7c16b5671d0e 967
tnhnrl 17:7c16b5671d0e 968 stateMachine().getDiveSequence(); //get first sequence on keyboard press
tnhnrl 17:7c16b5671d0e 969 _keyboard_state = currentStateStruct.state;
tnhnrl 17:7c16b5671d0e 970
tnhnrl 17:7c16b5671d0e 971 pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles
tnhnrl 16:3363b9f14913 972 }
tnhnrl 45:16b8162188ca 973 else if (userInput == 'R') {
tnhnrl 17:7c16b5671d0e 974 _keyboard_state = RISE;
tnhnrl 16:3363b9f14913 975 }
tnhnrl 45:16b8162188ca 976 else if (userInput == 'L') {
tnhnrl 17:7c16b5671d0e 977 _keyboard_state = FLOAT_LEVEL;
tnhnrl 16:3363b9f14913 978 }
tnhnrl 45:16b8162188ca 979 else if (userInput == 'B') {
tnhnrl 17:7c16b5671d0e 980 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 981 }
tnhnrl 45:16b8162188ca 982 else if (userInput == 'E') {
tnhnrl 17:7c16b5671d0e 983 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 984 }
tnhnrl 49:47ffa4feb6db 985
tnhnrl 49:47ffa4feb6db 986 else if (userInput == 'T') {
tnhnrl 49:47ffa4feb6db 987 _keyboard_state = CHECK_TUNING;
tnhnrl 49:47ffa4feb6db 988 }
tnhnrl 49:47ffa4feb6db 989
tnhnrl 49:47ffa4feb6db 990 // some debug tools below
tnhnrl 32:f2f8ae34aadc 991 else if (userInput == 'P') {
tnhnrl 32:f2f8ae34aadc 992 //Print current SD card log file
tnhnrl 36:966a86937e17 993 //printCurrentSdLog();
tnhnrl 36:966a86937e17 994 mbedLogger().printCurrentLogFile(); //print the current log file to the screen
tnhnrl 32:f2f8ae34aadc 995 }
tnhnrl 32:f2f8ae34aadc 996 else if (userInput == 'X') {
tnhnrl 45:16b8162188ca 997 mbedLogger().printMbedDirectory(); //print all log files to the screen
tnhnrl 32:f2f8ae34aadc 998 }
tnhnrl 45:16b8162188ca 999 else if (userInput == 'O') {
tnhnrl 45:16b8162188ca 1000 _keyboard_state = TRANSMIT_LOG; //Transmit data (work in progress)
tnhnrl 45:16b8162188ca 1001 }
tnhnrl 49:47ffa4feb6db 1002 else if (userInput == 'I') {
tnhnrl 49:47ffa4feb6db 1003 mbedLogger().receiveMissionDataWithTicker(); //receive sequence.txt files
tnhnrl 49:47ffa4feb6db 1004 }
tnhnrl 49:47ffa4feb6db 1005 else if (userInput == '~') {
tnhnrl 49:47ffa4feb6db 1006 pc().printf("ERASING MBED LOG FILE\n\r");
tnhnrl 49:47ffa4feb6db 1007 mbedLogger().eraseFile();
tnhnrl 49:47ffa4feb6db 1008 }
tnhnrl 45:16b8162188ca 1009 else if (userInput == 'Z') {
tnhnrl 28:16c83a2fdefa 1010 pc().printf("FSG FSM States: \n\r");
tnhnrl 28:16c83a2fdefa 1011 string string_state;
tnhnrl 28:16c83a2fdefa 1012
tnhnrl 28:16c83a2fdefa 1013 for (int i = 0; i < _state_array_counter; i++) {
tnhnrl 28:16c83a2fdefa 1014 if (_state_array[i] == SIT_IDLE)
tnhnrl 28:16c83a2fdefa 1015 string_state = "SIT_IDLE <END>";
tnhnrl 28:16c83a2fdefa 1016 else if (_state_array[i] == FIND_NEUTRAL)
tnhnrl 28:16c83a2fdefa 1017 string_state = "FIND_NEUTRAL";
tnhnrl 28:16c83a2fdefa 1018 else if (_state_array[i] == DIVE)
tnhnrl 28:16c83a2fdefa 1019 string_state = "DIVE";
tnhnrl 28:16c83a2fdefa 1020 else if (_state_array[i] == RISE)
tnhnrl 28:16c83a2fdefa 1021 string_state = "RISE";
tnhnrl 28:16c83a2fdefa 1022 else if (_state_array[i] == FLOAT_LEVEL)
tnhnrl 28:16c83a2fdefa 1023 string_state = "FLOAT_LEVEL";
tnhnrl 28:16c83a2fdefa 1024 else if (_state_array[i] == FLOAT_BROADCAST)
tnhnrl 28:16c83a2fdefa 1025 string_state = "FLOAT_BROADCAST";
tnhnrl 28:16c83a2fdefa 1026 else if (_state_array[i] == EMERGENCY_CLIMB)
tnhnrl 28:16c83a2fdefa 1027 string_state = "EMERGENCY_CLIMB";
tnhnrl 28:16c83a2fdefa 1028 else if (_state_array[i] == MULTI_DIVE)
tnhnrl 28:16c83a2fdefa 1029 string_state = "MULTI_DIVE";
tnhnrl 28:16c83a2fdefa 1030 else if (_state_array[i] == MULTI_RISE)
tnhnrl 28:16c83a2fdefa 1031 string_state = "MULTI_RISE";
tnhnrl 28:16c83a2fdefa 1032 else if (_state_array[i] == KEYBOARD)
tnhnrl 28:16c83a2fdefa 1033 string_state = "KEYBOARD";
tnhnrl 28:16c83a2fdefa 1034 pc().printf("State #%d: %d (%s)\n\r", i, _state_array[i], string_state.c_str());
tnhnrl 28:16c83a2fdefa 1035 }
tnhnrl 28:16c83a2fdefa 1036
tnhnrl 28:16c83a2fdefa 1037 pc().printf("\n\rNeutral sub-FSM States: \n\r");
tnhnrl 28:16c83a2fdefa 1038 string string_substate;
tnhnrl 28:16c83a2fdefa 1039
tnhnrl 28:16c83a2fdefa 1040 for (int i = 0; i < _substate_array_counter; i++) {
tnhnrl 32:f2f8ae34aadc 1041 if (_substate_array[i] == NEUTRAL_SINKING)
tnhnrl 28:16c83a2fdefa 1042 string_substate = "NEUTRAL_SINKING";
tnhnrl 28:16c83a2fdefa 1043 else if (_substate_array[i] == NEUTRAL_SLOWLY_RISE)
tnhnrl 28:16c83a2fdefa 1044 string_substate = "NEUTRAL_SLOWLY_RISE";
tnhnrl 28:16c83a2fdefa 1045 else if (_substate_array[i] == NEUTRAL_CHECK_PITCH)
tnhnrl 28:16c83a2fdefa 1046 string_substate = "NEUTRAL_CHECK_PITCH";
tnhnrl 28:16c83a2fdefa 1047 else if (_substate_array[i] == NEUTRAL_EXIT)
tnhnrl 28:16c83a2fdefa 1048 string_substate = "NEUTRAL_EXIT <-- ";
tnhnrl 28:16c83a2fdefa 1049 else if (_substate_array[i] == EMERGENCY_CLIMB)
tnhnrl 28:16c83a2fdefa 1050 string_substate = " -- > EMERGENCY_CLIMB <-- ";
tnhnrl 28:16c83a2fdefa 1051 pc().printf("Neutral Substate #%d: %d (%s)\n\r", i, _state_array[i], string_substate.c_str());
tnhnrl 28:16c83a2fdefa 1052 }
tnhnrl 28:16c83a2fdefa 1053 pc().printf("\n\r"); //make space between printouts
tnhnrl 16:3363b9f14913 1054 }
tnhnrl 49:47ffa4feb6db 1055 else if (userInput == '|') {
tnhnrl 16:3363b9f14913 1056 pc().printf("taring depth sensor\r\n");
tnhnrl 16:3363b9f14913 1057 pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 1058 wait(0.1);
tnhnrl 16:3363b9f14913 1059 depth().tare(); // tares to ambient (do on surface)
tnhnrl 16:3363b9f14913 1060 pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 1061 }
tnhnrl 16:3363b9f14913 1062
tnhnrl 16:3363b9f14913 1063 else if (userInput == '[' or userInput == '{') {
tnhnrl 32:f2f8ae34aadc 1064 _neutral_bce_pos_mm = depthLoop().getOutputOffset() - 1;
tnhnrl 32:f2f8ae34aadc 1065 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // decrease the bce neutral setpoint
tnhnrl 32:f2f8ae34aadc 1066 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1067 // save neutral depth value to config file
tnhnrl 32:f2f8ae34aadc 1068 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1069 }
tnhnrl 16:3363b9f14913 1070 else if (userInput == ']' or userInput == '}') {
tnhnrl 32:f2f8ae34aadc 1071 _neutral_bce_pos_mm = depthLoop().getOutputOffset() + 1;
tnhnrl 32:f2f8ae34aadc 1072 depthLoop().setOutputOffset(_neutral_bce_pos_mm); // increase the bce neutral setpoint
tnhnrl 32:f2f8ae34aadc 1073 pc().printf("Adjusting bce neutral position. new offset: %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1074 // save neutral depth value to config file
tnhnrl 32:f2f8ae34aadc 1075 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1076 }
tnhnrl 16:3363b9f14913 1077 else if (userInput == '<' or userInput == ',') {
tnhnrl 32:f2f8ae34aadc 1078 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() - 1;
tnhnrl 32:f2f8ae34aadc 1079 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // decrease the batt neutral setpoint
tnhnrl 32:f2f8ae34aadc 1080 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1081 // save neutral pitch value to config file
tnhnrl 32:f2f8ae34aadc 1082 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1083 }
tnhnrl 16:3363b9f14913 1084 else if (userInput == '>' or userInput == '.') {
tnhnrl 32:f2f8ae34aadc 1085 _neutral_batt_pos_mm = pitchLoop().getOutputOffset() + 1;
tnhnrl 32:f2f8ae34aadc 1086 pitchLoop().setOutputOffset(_neutral_batt_pos_mm); // increase the batt neutral setpoint
tnhnrl 32:f2f8ae34aadc 1087 pc().printf("Adjusting batt neutral position. new offset: %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1088 // save neutral pitch value to config file
tnhnrl 32:f2f8ae34aadc 1089 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1090 }
tnhnrl 16:3363b9f14913 1091
tnhnrl 16:3363b9f14913 1092 else if (userInput == '?') {
tnhnrl 16:3363b9f14913 1093 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 16:3363b9f14913 1094 wait(0.5);
tnhnrl 16:3363b9f14913 1095 mbed_reset();
tnhnrl 16:3363b9f14913 1096 }
tnhnrl 20:8987a9ae2bc7 1097
tnhnrl 16:3363b9f14913 1098 // change settings
tnhnrl 16:3363b9f14913 1099 else if (userInput == 'Q' or userInput == 'q') {
tnhnrl 32:f2f8ae34aadc 1100 _pitch_command -= 0.5; //decrement the pitch setpoint
tnhnrl 32:f2f8ae34aadc 1101 pitchLoop().setCommand(_pitch_command);
tnhnrl 16:3363b9f14913 1102 pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 1103 }
tnhnrl 16:3363b9f14913 1104 else if (userInput == 'W' or userInput == 'w') {
tnhnrl 32:f2f8ae34aadc 1105 _pitch_command += 0.5; //increment the pitch setpoint
tnhnrl 32:f2f8ae34aadc 1106 pitchLoop().setCommand(_pitch_command);
tnhnrl 16:3363b9f14913 1107 pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 1108 }
tnhnrl 16:3363b9f14913 1109 else if (userInput == 'A' or userInput == 'a') {
tnhnrl 32:f2f8ae34aadc 1110 _depth_command -= 0.5; //decrement the depth setpoint
tnhnrl 32:f2f8ae34aadc 1111 depthLoop().setCommand(_depth_command);
tnhnrl 16:3363b9f14913 1112 pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1113 }
tnhnrl 16:3363b9f14913 1114 else if (userInput == 'S' or userInput == 's') {
tnhnrl 32:f2f8ae34aadc 1115 _depth_command += 0.5; //increment the depth setpoint
tnhnrl 32:f2f8ae34aadc 1116 depthLoop().setCommand(_depth_command);
tnhnrl 16:3363b9f14913 1117 pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1118 }
tnhnrl 16:3363b9f14913 1119 else if (userInput == '-') {
tnhnrl 17:7c16b5671d0e 1120 _timeout -= 10.0; //decrement the timeout
tnhnrl 17:7c16b5671d0e 1121 pc().printf(">>> timeout decreased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 1122 }
tnhnrl 16:3363b9f14913 1123 else if (userInput == '=' or userInput == '+') {
tnhnrl 17:7c16b5671d0e 1124 _timeout += 10.0; //increment the timeout
tnhnrl 17:7c16b5671d0e 1125 pc().printf(">>> timeout increased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 1126 }
tnhnrl 16:3363b9f14913 1127
tnhnrl 49:47ffa4feb6db 1128 // go to tuning sub-menu
tnhnrl 49:47ffa4feb6db 1129 else if (userInput == '5') {
tnhnrl 49:47ffa4feb6db 1130 keyboard_menu_MANUAL_TUNING();
tnhnrl 49:47ffa4feb6db 1131 }
tnhnrl 49:47ffa4feb6db 1132
tnhnrl 52:f207567d3ea4 1133 else if (userInput == '6') {
tnhnrl 52:f207567d3ea4 1134 keyboard_menu_STREAM_STATUS();
tnhnrl 52:f207567d3ea4 1135 }
tnhnrl 52:f207567d3ea4 1136
tnhnrl 54:d4990fb68404 1137 else if (userInput == '7') {
tnhnrl 54:d4990fb68404 1138 keyboard_menu_RUDDER_SERVO_settings();
tnhnrl 54:d4990fb68404 1139 }
tnhnrl 54:d4990fb68404 1140
tnhnrl 52:f207567d3ea4 1141 else if (userInput == '8') {
tnhnrl 54:d4990fb68404 1142 keyboard_menu_CHANNEL_READINGS();
tnhnrl 52:f207567d3ea4 1143 }
tnhnrl 52:f207567d3ea4 1144
tnhnrl 16:3363b9f14913 1145 // go to sub-menus for the PID gains (this is blocking)
tnhnrl 16:3363b9f14913 1146 else if (userInput == '1') {
tnhnrl 16:3363b9f14913 1147 keyboard_menu_BCE_PID_settings();
tnhnrl 16:3363b9f14913 1148 }
tnhnrl 16:3363b9f14913 1149 else if (userInput == '2') {
tnhnrl 16:3363b9f14913 1150 keyboard_menu_BATT_PID_settings();
tnhnrl 16:3363b9f14913 1151 }
tnhnrl 16:3363b9f14913 1152 else if (userInput == '3') {
tnhnrl 16:3363b9f14913 1153 keyboard_menu_DEPTH_PID_settings();
tnhnrl 16:3363b9f14913 1154 }
tnhnrl 16:3363b9f14913 1155 else if (userInput == '4') {
tnhnrl 16:3363b9f14913 1156 keyboard_menu_PITCH_PID_settings();
tnhnrl 16:3363b9f14913 1157 }
tnhnrl 16:3363b9f14913 1158
tnhnrl 16:3363b9f14913 1159 else if (userInput == 'C' or userInput == 'c') {
tnhnrl 32:f2f8ae34aadc 1160
tnhnrl 32:f2f8ae34aadc 1161 pc().printf("\n\n\rCURRENT STATUS AND PARAMETERS:\n\r");
mkelly10 51:c5c40272ecc3 1162
mkelly10 51:c5c40272ecc3 1163 // Testing out ADC
mkelly10 51:c5c40272ecc3 1164 float vref = 5.6;
mkelly10 51:c5c40272ecc3 1165 float vmeasured = 0;
mkelly10 51:c5c40272ecc3 1166 unsigned int raw = adc().readCh5();
mkelly10 51:c5c40272ecc3 1167 vmeasured = ((float)raw)/4095.0*vref;
mkelly10 51:c5c40272ecc3 1168 pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
mkelly10 51:c5c40272ecc3 1169 pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
mkelly10 51:c5c40272ecc3 1170 pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
mkelly10 51:c5c40272ecc3 1171 pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
mkelly10 51:c5c40272ecc3 1172 pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
mkelly10 51:c5c40272ecc3 1173 pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
mkelly10 51:c5c40272ecc3 1174 pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
mkelly10 51:c5c40272ecc3 1175 pc().printf("raw board current: %d \r\n",adc().readCh7());
mkelly10 51:c5c40272ecc3 1176 pc().printf("raw BCE limit switch: %d \r\n",bce().getSwitch());
mkelly10 51:c5c40272ecc3 1177 pc().printf("raw BMM limit switch: %d \r\n",batt().getSwitch());
mkelly10 51:c5c40272ecc3 1178 pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
mkelly10 51:c5c40272ecc3 1179 // End of ADC Test
mkelly10 51:c5c40272ecc3 1180
tnhnrl 32:f2f8ae34aadc 1181 pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 32:f2f8ae34aadc 1182 pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 16:3363b9f14913 1183 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 16:3363b9f14913 1184 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 16:3363b9f14913 1185 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 16:3363b9f14913 1186 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 16:3363b9f14913 1187 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 16:3363b9f14913 1188 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 32:f2f8ae34aadc 1189
tnhnrl 32:f2f8ae34aadc 1190 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 1191 pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 28:16c83a2fdefa 1192 pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 32:f2f8ae34aadc 1193 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 1194
tnhnrl 38:83d06c294807 1195 pc().printf("\n\r");
tnhnrl 38:83d06c294807 1196 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 1197 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 1198 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 1199 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 1200 }
tnhnrl 17:7c16b5671d0e 1201
tnhnrl 17:7c16b5671d0e 1202 //when you read the keyboard successfully, change the state
tnhnrl 28:16c83a2fdefa 1203 _state = _keyboard_state; //set state at the end of this function
tnhnrl 38:83d06c294807 1204 //pc().printf("\n\n\r ********* KEYBOARD STATE: %d *********\n\n\r", _state);
tnhnrl 16:3363b9f14913 1205 }
tnhnrl 16:3363b9f14913 1206 }
tnhnrl 52:f207567d3ea4 1207
tnhnrl 52:f207567d3ea4 1208 void StateMachine::keyboard_menu_STREAM_STATUS() {
tnhnrl 52:f207567d3ea4 1209 char STATUS_key;
tnhnrl 52:f207567d3ea4 1210
tnhnrl 52:f207567d3ea4 1211 // show the menu
tnhnrl 54:d4990fb68404 1212 pc().printf("\n\r1: STATUS DEBUG MENU (EXIT WITH 'X' !)\n\r");
tnhnrl 52:f207567d3ea4 1213
tnhnrl 52:f207567d3ea4 1214 while (1) {
tnhnrl 52:f207567d3ea4 1215 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1216 STATUS_key = pc().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1217 }
tnhnrl 52:f207567d3ea4 1218
tnhnrl 52:f207567d3ea4 1219 else {
tnhnrl 52:f207567d3ea4 1220
tnhnrl 52:f207567d3ea4 1221 wait(1);
tnhnrl 54:d4990fb68404 1222
tnhnrl 54:d4990fb68404 1223 pc().printf("(set) BCE POS: %0.1f (%0.1f) BATT POS: %0.1f (%0.1f) PRESS: %0.2f )(channel reading: %d) (volt: %0.2f) << %0.2f >> PRESSURE: %0.2f PITCH: %0.2f HEADING: %0.2f\r\n",bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(), depthLoop().getPosition(),depth().readADCCounts(),depth().readVoltage(),depth().getRawPSI(),depth().getRawPSI(),imu().getPitch(),headingLoop().getPosition());
tnhnrl 54:d4990fb68404 1224
tnhnrl 52:f207567d3ea4 1225
tnhnrl 54:d4990fb68404 1226 //pc().printf("Neutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
tnhnrl 54:d4990fb68404 1227 // pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition());
tnhnrl 54:d4990fb68404 1228 // pc().printf("pitch: %3.1f deg\r\n",imu().getPitch());
tnhnrl 54:d4990fb68404 1229 // pc().printf("heading (rudder): %3.1f deg\r\n",rudderLoop().getPosition()); //for heading
tnhnrl 52:f207567d3ea4 1230
tnhnrl 54:d4990fb68404 1231 // pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 54:d4990fb68404 1232 // pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 54:d4990fb68404 1233 //
tnhnrl 54:d4990fb68404 1234 // pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 54:d4990fb68404 1235 // pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 52:f207567d3ea4 1236
tnhnrl 54:d4990fb68404 1237 // pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 54:d4990fb68404 1238 // pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 54:d4990fb68404 1239 //
tnhnrl 54:d4990fb68404 1240 // pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 54:d4990fb68404 1241 // pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 52:f207567d3ea4 1242
tnhnrl 52:f207567d3ea4 1243
tnhnrl 52:f207567d3ea4 1244 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1245 }
tnhnrl 52:f207567d3ea4 1246
tnhnrl 52:f207567d3ea4 1247 // process the keys
tnhnrl 52:f207567d3ea4 1248 if (STATUS_key == 'X') {
tnhnrl 52:f207567d3ea4 1249 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1250 }
tnhnrl 52:f207567d3ea4 1251
tnhnrl 52:f207567d3ea4 1252 else {
tnhnrl 52:f207567d3ea4 1253 pc().printf("\n\rThis key (%c) does nothing here. ", STATUS_key);
tnhnrl 52:f207567d3ea4 1254 }
tnhnrl 52:f207567d3ea4 1255 }
tnhnrl 52:f207567d3ea4 1256 }
tnhnrl 52:f207567d3ea4 1257
tnhnrl 52:f207567d3ea4 1258 void StateMachine::keyboard_menu_RUDDER_SERVO_settings() {
tnhnrl 52:f207567d3ea4 1259 //load current parameters from the rudder
tnhnrl 52:f207567d3ea4 1260 float rudder_min_pwm = rudder().getMinPWM();
tnhnrl 52:f207567d3ea4 1261 float rudder_max_pwm = rudder().getMaxPWM();
tnhnrl 52:f207567d3ea4 1262 float rudder_ctr_pwm = rudder().getCenterPWM();
tnhnrl 52:f207567d3ea4 1263 float rudder_min_deg = rudder().getMinDeg();
tnhnrl 52:f207567d3ea4 1264 float rudder_max_deg = rudder().getMaxDeg();
tnhnrl 52:f207567d3ea4 1265
tnhnrl 52:f207567d3ea4 1266 char RUDDER_PID_key;
tnhnrl 52:f207567d3ea4 1267
tnhnrl 52:f207567d3ea4 1268 // print the menu
tnhnrl 52:f207567d3ea4 1269 pc().printf("\n\rRUDDER (servo driver) settings (MENU)");
tnhnrl 52:f207567d3ea4 1270 pc().printf("\n\r(Adjust min/max/center PWM settings with the following keys: N and M and C");
tnhnrl 52:f207567d3ea4 1271 pc().printf("\n\r(Adjust DEGREE limit settings with the following keys: min = K, max = L");
tnhnrl 52:f207567d3ea4 1272 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 52:f207567d3ea4 1273 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 1274
tnhnrl 52:f207567d3ea4 1275 // handle the key presses
tnhnrl 52:f207567d3ea4 1276 while(1) {
tnhnrl 52:f207567d3ea4 1277 // get the user's keystroke from either of the two inputs
tnhnrl 52:f207567d3ea4 1278 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1279 RUDDER_PID_key = pc().getc();
tnhnrl 52:f207567d3ea4 1280 }
tnhnrl 52:f207567d3ea4 1281 else {
tnhnrl 52:f207567d3ea4 1282 wait(0.5);
tnhnrl 52:f207567d3ea4 1283 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 1284 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1285 }
tnhnrl 52:f207567d3ea4 1286
tnhnrl 52:f207567d3ea4 1287 // handle the user's key input
tnhnrl 52:f207567d3ea4 1288 if (RUDDER_PID_key == 'S') { // user wants to save the modified values
tnhnrl 52:f207567d3ea4 1289 // set global values
tnhnrl 52:f207567d3ea4 1290 rudder().setMinPWM(rudder_min_pwm);
tnhnrl 52:f207567d3ea4 1291 rudder().setMaxPWM(rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1292 rudder().setCenterPWM(rudder_ctr_pwm);
tnhnrl 52:f207567d3ea4 1293 rudder().setMinDeg(rudder_min_deg);
tnhnrl 52:f207567d3ea4 1294 rudder().setMaxDeg(rudder_max_deg);
tnhnrl 52:f207567d3ea4 1295
tnhnrl 52:f207567d3ea4 1296 // save rudder servo driver values for inner loop
tnhnrl 52:f207567d3ea4 1297 configFileIO().saveRudderData(rudder_min_deg, rudder_max_deg, rudder_ctr_pwm, rudder_min_pwm, rudder_max_pwm);
tnhnrl 52:f207567d3ea4 1298 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 1299 }
tnhnrl 52:f207567d3ea4 1300 else if (RUDDER_PID_key == 'X') {
tnhnrl 52:f207567d3ea4 1301 break; //exit the while loop
tnhnrl 52:f207567d3ea4 1302 }
tnhnrl 52:f207567d3ea4 1303 // MIN PWM
tnhnrl 52:f207567d3ea4 1304 else if (RUDDER_PID_key == 'N') {
tnhnrl 52:f207567d3ea4 1305 pc().printf(">> Type in rudder_min_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1306 rudder_min_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1307 }
tnhnrl 52:f207567d3ea4 1308 // MAX PWM
tnhnrl 52:f207567d3ea4 1309 else if (RUDDER_PID_key == 'M') {
tnhnrl 52:f207567d3ea4 1310 pc().printf(">> Type in rudder_max_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1311 rudder_max_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1312 }
tnhnrl 52:f207567d3ea4 1313 // CENTER PWM
tnhnrl 52:f207567d3ea4 1314 else if (RUDDER_PID_key == 'C') {
tnhnrl 52:f207567d3ea4 1315 pc().printf(">> Type in rudder_ctr_pwm with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1316 rudder_ctr_pwm = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1317 }
tnhnrl 52:f207567d3ea4 1318 // MIN DEG
tnhnrl 52:f207567d3ea4 1319 else if (RUDDER_PID_key == 'K') {
tnhnrl 52:f207567d3ea4 1320 pc().printf(">> Type in rudder_min_deg with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1321 rudder_min_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1322 }
tnhnrl 52:f207567d3ea4 1323 // MAX DEG
tnhnrl 52:f207567d3ea4 1324 else if (RUDDER_PID_key == 'L') {
tnhnrl 52:f207567d3ea4 1325 pc().printf(">> Type in rudder_max_deg with keyboard.\n\r");
tnhnrl 52:f207567d3ea4 1326 rudder_max_deg = getFloatUserInput();
tnhnrl 52:f207567d3ea4 1327 }
tnhnrl 52:f207567d3ea4 1328
tnhnrl 52:f207567d3ea4 1329 else {
tnhnrl 52:f207567d3ea4 1330 pc().printf("RUDDER SETUP: [%c] This key does nothing here. \r", RUDDER_PID_key);
tnhnrl 52:f207567d3ea4 1331 }
tnhnrl 52:f207567d3ea4 1332 }
tnhnrl 52:f207567d3ea4 1333 }
tnhnrl 54:d4990fb68404 1334
tnhnrl 54:d4990fb68404 1335 void StateMachine::keyboard_menu_COUNTS_STATUS() {
tnhnrl 54:d4990fb68404 1336
tnhnrl 54:d4990fb68404 1337 }
tnhnrl 20:8987a9ae2bc7 1338
tnhnrl 49:47ffa4feb6db 1339 void StateMachine::keyboard_menu_MANUAL_TUNING() {
tnhnrl 49:47ffa4feb6db 1340 char TUNING_key;
tnhnrl 49:47ffa4feb6db 1341
tnhnrl 49:47ffa4feb6db 1342 // show the menu
tnhnrl 49:47ffa4feb6db 1343 pc().printf("\n\r1: MANUAL TUNING MENU (EXIT WITH 'X' !)");
tnhnrl 49:47ffa4feb6db 1344 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 1345 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 1346
tnhnrl 49:47ffa4feb6db 1347 // what needs to be started?
tnhnrl 54:d4990fb68404 1348 bce().setPosition_mm(160);
tnhnrl 54:d4990fb68404 1349
tnhnrl 54:d4990fb68404 1350 batt().setPosition_mm(50);
tnhnrl 54:d4990fb68404 1351
tnhnrl 49:47ffa4feb6db 1352 bce().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 1353 batt().unpause(); //this is now active
tnhnrl 49:47ffa4feb6db 1354
tnhnrl 49:47ffa4feb6db 1355 while (1) {
tnhnrl 49:47ffa4feb6db 1356 if (pc().readable()) {
tnhnrl 49:47ffa4feb6db 1357 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 49:47ffa4feb6db 1358 }
tnhnrl 49:47ffa4feb6db 1359
tnhnrl 49:47ffa4feb6db 1360 else {
tnhnrl 52:f207567d3ea4 1361 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 1362 continue; // didn't get a user input, so keep waiting for it
tnhnrl 49:47ffa4feb6db 1363 }
tnhnrl 49:47ffa4feb6db 1364
tnhnrl 49:47ffa4feb6db 1365 // process the keys
tnhnrl 49:47ffa4feb6db 1366 if (TUNING_key == 'X') {
tnhnrl 49:47ffa4feb6db 1367 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 49:47ffa4feb6db 1368 bce().pause();
tnhnrl 49:47ffa4feb6db 1369 batt().pause();
tnhnrl 49:47ffa4feb6db 1370
tnhnrl 49:47ffa4feb6db 1371 break; //exit the while loop
tnhnrl 49:47ffa4feb6db 1372 }
tnhnrl 49:47ffa4feb6db 1373
tnhnrl 49:47ffa4feb6db 1374 else if (TUNING_key == 'A') {
tnhnrl 49:47ffa4feb6db 1375 _neutral_bce_pos_mm = _neutral_bce_pos_mm - 1;
tnhnrl 49:47ffa4feb6db 1376 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 1377 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 1378 }
tnhnrl 49:47ffa4feb6db 1379
tnhnrl 52:f207567d3ea4 1380 else if (TUNING_key == 'A') {
tnhnrl 52:f207567d3ea4 1381 _neutral_bce_pos_mm = _neutral_bce_pos_mm - 1;
tnhnrl 52:f207567d3ea4 1382 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 52:f207567d3ea4 1383 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 1384 }
tnhnrl 52:f207567d3ea4 1385
tnhnrl 49:47ffa4feb6db 1386 else if (TUNING_key == 'S') {
tnhnrl 49:47ffa4feb6db 1387 _neutral_bce_pos_mm = _neutral_bce_pos_mm + 1;
tnhnrl 49:47ffa4feb6db 1388 bce().setPosition_mm(_neutral_bce_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 1389 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 1390 }
tnhnrl 49:47ffa4feb6db 1391
tnhnrl 49:47ffa4feb6db 1392 else if (TUNING_key == 'Q') {
tnhnrl 49:47ffa4feb6db 1393 _neutral_batt_pos_mm = _neutral_batt_pos_mm - 1;
tnhnrl 49:47ffa4feb6db 1394 batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 1395 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 1396 }
tnhnrl 49:47ffa4feb6db 1397
tnhnrl 49:47ffa4feb6db 1398 else if (TUNING_key == 'W') {
tnhnrl 49:47ffa4feb6db 1399 _neutral_batt_pos_mm = _neutral_batt_pos_mm + 1;
tnhnrl 49:47ffa4feb6db 1400 batt().setPosition_mm(_neutral_batt_pos_mm); //this variable is loaded from the file at initialization
tnhnrl 49:47ffa4feb6db 1401 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 1402 }
tnhnrl 49:47ffa4feb6db 1403
tnhnrl 49:47ffa4feb6db 1404 else {
tnhnrl 49:47ffa4feb6db 1405 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 49:47ffa4feb6db 1406 }
tnhnrl 49:47ffa4feb6db 1407
tnhnrl 49:47ffa4feb6db 1408 }
tnhnrl 49:47ffa4feb6db 1409 }
tnhnrl 52:f207567d3ea4 1410
tnhnrl 52:f207567d3ea4 1411 void StateMachine::keyboard_menu_CHANNEL_READINGS() {
tnhnrl 52:f207567d3ea4 1412 char TUNING_key;
tnhnrl 52:f207567d3ea4 1413
tnhnrl 52:f207567d3ea4 1414 // show the menu
tnhnrl 52:f207567d3ea4 1415 pc().printf("\n\r1: CHANNEL READINGS (EXIT WITH 'X' !)");
tnhnrl 52:f207567d3ea4 1416
tnhnrl 52:f207567d3ea4 1417 while (1) {
tnhnrl 52:f207567d3ea4 1418 if (pc().readable()) {
tnhnrl 52:f207567d3ea4 1419 TUNING_key = pc().getc(); //get each keystroke
tnhnrl 52:f207567d3ea4 1420 }
tnhnrl 52:f207567d3ea4 1421
tnhnrl 54:d4990fb68404 1422 // process the keys
tnhnrl 54:d4990fb68404 1423 if (TUNING_key == 'X') {
tnhnrl 54:d4990fb68404 1424 // STOP THE MOTORS BEFORE LEAVING! (Just in case.)
tnhnrl 54:d4990fb68404 1425 bce().pause();
tnhnrl 54:d4990fb68404 1426 batt().pause();
tnhnrl 54:d4990fb68404 1427
tnhnrl 54:d4990fb68404 1428 break; //exit the while loop
tnhnrl 54:d4990fb68404 1429 }
tnhnrl 54:d4990fb68404 1430
tnhnrl 52:f207567d3ea4 1431 else {
tnhnrl 52:f207567d3ea4 1432 // Testing out ADC
tnhnrl 54:d4990fb68404 1433 wait(0.5);
tnhnrl 52:f207567d3ea4 1434 float vref = 5.6;
tnhnrl 52:f207567d3ea4 1435 float vmeasured = 0;
tnhnrl 52:f207567d3ea4 1436 unsigned int raw = adc().readCh5();
tnhnrl 52:f207567d3ea4 1437 vmeasured = ((float)raw)/4095.0*vref;
tnhnrl 52:f207567d3ea4 1438
tnhnrl 54:d4990fb68404 1439
tnhnrl 54:d4990fb68404 1440 pc().printf("BCE POS(%d),BMM POS(%d), BCE CUR(%d), BMM CUR(%d), Depth Pressure (%d) \r\n",adc().readCh0(),adc().readCh1(),adc().readCh2(),adc().readCh3(),adc().readCh4());
tnhnrl 54:d4990fb68404 1441
tnhnrl 54:d4990fb68404 1442 //pc().printf("vessel pressure(%d) batt voltage(%d) board current(%d) LIMIT: BCE(%d) BMM(%d) (HW reading limit: BCE(%d) BMM (%d) \r\n",adc().readCh5(),adc().readCh6(),adc().readCh7(),bce().getSwitch(), batt().getSwitch(),bce().getSwitchState(),batt().getSwitchState());
tnhnrl 54:d4990fb68404 1443 //pc().printf("LIMIT: BCE(%d) BMM(%d) << HW reading limit: BCE(%d) BMM (%d) >> \r\n",bce().getSwitch(), batt().getSwitch(),bce().getHWSwitchReading(),batt().getHWSwitchReading());
tnhnrl 54:d4990fb68404 1444 //pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw);
tnhnrl 52:f207567d3ea4 1445 // End of ADC Test
tnhnrl 52:f207567d3ea4 1446
tnhnrl 54:d4990fb68404 1447 // pc().printf("raw BCE pos: %d \r\n",adc().readCh0());
tnhnrl 54:d4990fb68404 1448 // pc().printf("raw BMM pos: %d \r\n",adc().readCh1());
tnhnrl 54:d4990fb68404 1449 // pc().printf("raw BCE current sense: %d \r\n",adc().readCh2());
tnhnrl 54:d4990fb68404 1450 // pc().printf("raw BMM current sense: %d \r\n",adc().readCh3());
tnhnrl 54:d4990fb68404 1451 // pc().printf("raw depth pressure: %d \r\n",adc().readCh4());
tnhnrl 54:d4990fb68404 1452 // pc().printf("raw vessel pressure: %d \r\n",adc().readCh5());
tnhnrl 54:d4990fb68404 1453 // pc().printf("raw battery voltage: %d \r\n",adc().readCh6());
tnhnrl 54:d4990fb68404 1454 // pc().printf("raw board current: %d \r\n",adc().readCh7());
tnhnrl 54:d4990fb68404 1455
tnhnrl 52:f207567d3ea4 1456 continue; // didn't get a user input, so keep waiting for it
tnhnrl 52:f207567d3ea4 1457 }
tnhnrl 52:f207567d3ea4 1458 }
tnhnrl 52:f207567d3ea4 1459 }
tnhnrl 49:47ffa4feb6db 1460
tnhnrl 16:3363b9f14913 1461 void StateMachine::keyboard_menu_BCE_PID_settings() {
tnhnrl 16:3363b9f14913 1462 char PID_key;
tnhnrl 16:3363b9f14913 1463 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 38:83d06c294807 1464 float BCE_KP = bce().getControllerP(); // load current value
tnhnrl 38:83d06c294807 1465 float BCE_KI = bce().getControllerI(); // load current global value
tnhnrl 38:83d06c294807 1466 float BCE_KD = bce().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1467
tnhnrl 16:3363b9f14913 1468 // show the menu
tnhnrl 16:3363b9f14913 1469 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1470 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1471 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 21:38c8544db6f4 1472 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 1473
tnhnrl 16:3363b9f14913 1474 // handle the key presses
tnhnrl 16:3363b9f14913 1475 while(1) {
tnhnrl 16:3363b9f14913 1476 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1477 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1478 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1479 }
tnhnrl 16:3363b9f14913 1480 else {
tnhnrl 16:3363b9f14913 1481 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1482 }
tnhnrl 16:3363b9f14913 1483
tnhnrl 16:3363b9f14913 1484 // handle the user's key input
tnhnrl 16:3363b9f14913 1485 if (PID_key == '-') {
tnhnrl 38:83d06c294807 1486 BCE_KP -= gain_step_size;
tnhnrl 38:83d06c294807 1487 pc().printf("P gain: %0.5f \r\n", BCE_KP);
tnhnrl 16:3363b9f14913 1488 }
tnhnrl 16:3363b9f14913 1489 else if (PID_key == '=') {
tnhnrl 38:83d06c294807 1490 BCE_KP += gain_step_size;
tnhnrl 38:83d06c294807 1491 pc().printf("P gain: %0.5f \r\n", BCE_KP);
tnhnrl 16:3363b9f14913 1492 }
tnhnrl 16:3363b9f14913 1493 else if (PID_key == '[') {
tnhnrl 38:83d06c294807 1494 BCE_KI -= gain_step_size;
tnhnrl 38:83d06c294807 1495 pc().printf("I gain: %0.5f \r\n", BCE_KI);
tnhnrl 16:3363b9f14913 1496 }
tnhnrl 16:3363b9f14913 1497 else if (PID_key == ']') {
tnhnrl 38:83d06c294807 1498 BCE_KI += gain_step_size;
tnhnrl 38:83d06c294807 1499 pc().printf("I gain: %0.5f \r\n", BCE_KI);
tnhnrl 16:3363b9f14913 1500 }
tnhnrl 16:3363b9f14913 1501 else if (PID_key == ';') {
tnhnrl 38:83d06c294807 1502 BCE_KD -= gain_step_size;
tnhnrl 38:83d06c294807 1503 pc().printf("D gain: %0.5f \r\n", BCE_KD);
tnhnrl 16:3363b9f14913 1504 }
tnhnrl 16:3363b9f14913 1505 else if (PID_key == '\'') {
tnhnrl 38:83d06c294807 1506 BCE_KD += gain_step_size;
tnhnrl 38:83d06c294807 1507 pc().printf("D gain: %0.5f \r\n", BCE_KD);
tnhnrl 16:3363b9f14913 1508 }
tnhnrl 16:3363b9f14913 1509 else if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 16:3363b9f14913 1510 // set values
tnhnrl 38:83d06c294807 1511 bce().setControllerP(BCE_KP);
tnhnrl 38:83d06c294807 1512 bce().setControllerI(BCE_KI);
tnhnrl 38:83d06c294807 1513 bce().setControllerD(BCE_KD);
tnhnrl 16:3363b9f14913 1514
tnhnrl 38:83d06c294807 1515 // save to "BATT.TXT" file
tnhnrl 38:83d06c294807 1516 configFileIO().saveBCEData(BCE_KP, BCE_KI, BCE_KD);
tnhnrl 38:83d06c294807 1517
tnhnrl 16:3363b9f14913 1518 break; //exit the while loop
tnhnrl 16:3363b9f14913 1519 }
tnhnrl 16:3363b9f14913 1520 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1521 break; //exit the while loop
tnhnrl 16:3363b9f14913 1522 }
tnhnrl 16:3363b9f14913 1523 else {
tnhnrl 16:3363b9f14913 1524 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1525 }
tnhnrl 16:3363b9f14913 1526 }
tnhnrl 16:3363b9f14913 1527 }
tnhnrl 20:8987a9ae2bc7 1528
tnhnrl 16:3363b9f14913 1529 void StateMachine::keyboard_menu_BATT_PID_settings() {
tnhnrl 16:3363b9f14913 1530 char PID_key;
tnhnrl 16:3363b9f14913 1531 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 38:83d06c294807 1532 float batt_KP = batt().getControllerP(); // load current global value
tnhnrl 38:83d06c294807 1533 float batt_KI = batt().getControllerI(); // load current global value
tnhnrl 38:83d06c294807 1534 float batt_KD = batt().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 1535
tnhnrl 16:3363b9f14913 1536 // print the menu
tnhnrl 16:3363b9f14913 1537 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1538 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1539 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 21:38c8544db6f4 1540 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 1541
tnhnrl 16:3363b9f14913 1542 // handle the key presses
tnhnrl 16:3363b9f14913 1543 while(1) {
tnhnrl 16:3363b9f14913 1544 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1545 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1546 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1547 }
tnhnrl 16:3363b9f14913 1548 else {
tnhnrl 16:3363b9f14913 1549 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1550 }
tnhnrl 16:3363b9f14913 1551
tnhnrl 16:3363b9f14913 1552 // handle the user's key input
tnhnrl 16:3363b9f14913 1553 if (PID_key == '-') {
tnhnrl 38:83d06c294807 1554 batt_KP -= gain_step_size;
tnhnrl 38:83d06c294807 1555 pc().printf("\rP gain: %0.5f ", batt_KP);
tnhnrl 16:3363b9f14913 1556 }
tnhnrl 16:3363b9f14913 1557 else if (PID_key == '=') {
tnhnrl 38:83d06c294807 1558 batt_KP += gain_step_size;
tnhnrl 38:83d06c294807 1559 pc().printf("\rP gain: %0.5f ", batt_KP);
tnhnrl 16:3363b9f14913 1560 }
tnhnrl 16:3363b9f14913 1561 else if (PID_key == '[') {
tnhnrl 38:83d06c294807 1562 batt_KI -= gain_step_size;
tnhnrl 38:83d06c294807 1563 pc().printf("\rI gain: %0.5f ", batt_KI);
tnhnrl 16:3363b9f14913 1564 }
tnhnrl 16:3363b9f14913 1565 else if (PID_key == ']') {
tnhnrl 38:83d06c294807 1566 batt_KI += gain_step_size;
tnhnrl 38:83d06c294807 1567 pc().printf("\rI gain: %0.5f ", batt_KI);
tnhnrl 16:3363b9f14913 1568 }
tnhnrl 16:3363b9f14913 1569 else if (PID_key == ';') {
tnhnrl 38:83d06c294807 1570 batt_KD -= gain_step_size;
tnhnrl 38:83d06c294807 1571 pc().printf("\rD gain: %0.5f ", batt_KD);
tnhnrl 16:3363b9f14913 1572 }
tnhnrl 16:3363b9f14913 1573 else if (PID_key == '\'') {
tnhnrl 38:83d06c294807 1574 batt_KD += gain_step_size;
tnhnrl 38:83d06c294807 1575 pc().printf("\rD gain: %0.5f ", batt_KD);
tnhnrl 16:3363b9f14913 1576 }
tnhnrl 16:3363b9f14913 1577 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1578 // set global values
tnhnrl 38:83d06c294807 1579 batt().setControllerP(batt_KP);
tnhnrl 38:83d06c294807 1580 batt().setControllerI(batt_KI);
tnhnrl 38:83d06c294807 1581 batt().setControllerD(batt_KD);
tnhnrl 16:3363b9f14913 1582
tnhnrl 38:83d06c294807 1583 // save to "BATT.TXT" file
tnhnrl 38:83d06c294807 1584 configFileIO().saveBattData(batt_KP, batt_KI, batt_KD);
tnhnrl 38:83d06c294807 1585
tnhnrl 16:3363b9f14913 1586 break; //exit the while loop
tnhnrl 16:3363b9f14913 1587 }
tnhnrl 16:3363b9f14913 1588 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1589 break; //exit the while loop
tnhnrl 16:3363b9f14913 1590 }
tnhnrl 16:3363b9f14913 1591 else {
tnhnrl 16:3363b9f14913 1592 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1593 }
tnhnrl 16:3363b9f14913 1594 }
tnhnrl 16:3363b9f14913 1595 }
tnhnrl 20:8987a9ae2bc7 1596
tnhnrl 16:3363b9f14913 1597 void StateMachine::keyboard_menu_DEPTH_PID_settings() {
tnhnrl 16:3363b9f14913 1598 char PID_key;
tnhnrl 16:3363b9f14913 1599 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1600
tnhnrl 16:3363b9f14913 1601 // show the menu
tnhnrl 16:3363b9f14913 1602 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1603 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1604 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r");
tnhnrl 16:3363b9f14913 1605 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 1606
tnhnrl 16:3363b9f14913 1607 // handle the key presses
tnhnrl 16:3363b9f14913 1608 while(1) {
tnhnrl 16:3363b9f14913 1609 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1610 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1611 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1612 }
tnhnrl 16:3363b9f14913 1613 else {
tnhnrl 16:3363b9f14913 1614 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1615 }
tnhnrl 16:3363b9f14913 1616
tnhnrl 16:3363b9f14913 1617 // handle the user's key input
tnhnrl 16:3363b9f14913 1618 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1619 _depth_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1620 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1621 }
tnhnrl 16:3363b9f14913 1622 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1623 _depth_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1624 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1625 }
tnhnrl 16:3363b9f14913 1626 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1627 _depth_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1628 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1629 }
tnhnrl 16:3363b9f14913 1630 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1631 _depth_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1632 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1633 }
tnhnrl 16:3363b9f14913 1634 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1635 _depth_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1636 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1637 }
tnhnrl 16:3363b9f14913 1638 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1639 _depth_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1640 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1641 }
tnhnrl 16:3363b9f14913 1642 else if (PID_key == 'S') { // user wants to save these settings
tnhnrl 16:3363b9f14913 1643 // set global values
tnhnrl 21:38c8544db6f4 1644 depthLoop().setControllerP(_depth_KP);
tnhnrl 21:38c8544db6f4 1645 depthLoop().setControllerI(_depth_KI);
tnhnrl 21:38c8544db6f4 1646 depthLoop().setControllerD(_depth_KD);
tnhnrl 16:3363b9f14913 1647
tnhnrl 21:38c8544db6f4 1648 // save depth PID values for outer loop
tnhnrl 21:38c8544db6f4 1649 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1650 break; //exit the while loop
tnhnrl 16:3363b9f14913 1651 }
tnhnrl 16:3363b9f14913 1652 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1653 break; //exit the while loop
tnhnrl 16:3363b9f14913 1654 }
tnhnrl 16:3363b9f14913 1655 else {
tnhnrl 16:3363b9f14913 1656 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1657 }
tnhnrl 16:3363b9f14913 1658 }
tnhnrl 16:3363b9f14913 1659 }
tnhnrl 16:3363b9f14913 1660
tnhnrl 16:3363b9f14913 1661 void StateMachine::keyboard_menu_PITCH_PID_settings() {
tnhnrl 16:3363b9f14913 1662 char PID_key;
tnhnrl 16:3363b9f14913 1663 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1664
tnhnrl 16:3363b9f14913 1665 // print the menu
tnhnrl 16:3363b9f14913 1666 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1667 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1668 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 16:3363b9f14913 1669 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 1670
tnhnrl 16:3363b9f14913 1671 // handle the key presses
tnhnrl 16:3363b9f14913 1672 while(1) {
tnhnrl 16:3363b9f14913 1673 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1674 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1675 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1676 }
tnhnrl 16:3363b9f14913 1677 else {
tnhnrl 16:3363b9f14913 1678 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1679 }
tnhnrl 16:3363b9f14913 1680
tnhnrl 16:3363b9f14913 1681 // handle the user's key input
tnhnrl 16:3363b9f14913 1682 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1683 _pitch_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1684 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1685 }
tnhnrl 16:3363b9f14913 1686 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1687 _pitch_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1688 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1689 }
tnhnrl 16:3363b9f14913 1690 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1691 _pitch_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1692 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1693 }
tnhnrl 16:3363b9f14913 1694 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1695 _pitch_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1696 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1697 }
tnhnrl 16:3363b9f14913 1698 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1699 _pitch_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1700 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1701 }
tnhnrl 16:3363b9f14913 1702 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1703 _pitch_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1704 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1705 }
tnhnrl 16:3363b9f14913 1706 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1707 // set global values
tnhnrl 21:38c8544db6f4 1708 pitchLoop().setControllerP(_pitch_KP);
tnhnrl 21:38c8544db6f4 1709 pitchLoop().setControllerI(_pitch_KI);
tnhnrl 21:38c8544db6f4 1710 pitchLoop().setControllerD(_pitch_KD);
tnhnrl 16:3363b9f14913 1711
tnhnrl 32:f2f8ae34aadc 1712 // save pitch PID values for outer loop (must save neutral position also)
tnhnrl 21:38c8544db6f4 1713 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1714 break; //exit the while loop
tnhnrl 16:3363b9f14913 1715 }
tnhnrl 16:3363b9f14913 1716 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1717 break; //exit the while loop
tnhnrl 16:3363b9f14913 1718 }
tnhnrl 16:3363b9f14913 1719 else {
tnhnrl 16:3363b9f14913 1720 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1721 }
tnhnrl 16:3363b9f14913 1722 }
tnhnrl 16:3363b9f14913 1723 }
tnhnrl 20:8987a9ae2bc7 1724
tnhnrl 16:3363b9f14913 1725 float StateMachine::getDepthCommand() {
tnhnrl 32:f2f8ae34aadc 1726 return _depth_command;
tnhnrl 16:3363b9f14913 1727 }
tnhnrl 20:8987a9ae2bc7 1728
tnhnrl 16:3363b9f14913 1729 float StateMachine::getPitchCommand() {
tnhnrl 32:f2f8ae34aadc 1730 return _pitch_command;
tnhnrl 32:f2f8ae34aadc 1731 }
tnhnrl 32:f2f8ae34aadc 1732
tnhnrl 32:f2f8ae34aadc 1733 float StateMachine::getDepthReading() {
tnhnrl 32:f2f8ae34aadc 1734 return _depth_reading;
tnhnrl 32:f2f8ae34aadc 1735 }
tnhnrl 32:f2f8ae34aadc 1736
tnhnrl 32:f2f8ae34aadc 1737 float StateMachine::getPitchReading() {
tnhnrl 32:f2f8ae34aadc 1738 return _pitch_reading;
tnhnrl 32:f2f8ae34aadc 1739 }
tnhnrl 32:f2f8ae34aadc 1740
tnhnrl 32:f2f8ae34aadc 1741 float StateMachine::getTimerReading() {
tnhnrl 32:f2f8ae34aadc 1742 return _timer_reading;
tnhnrl 17:7c16b5671d0e 1743 }
tnhnrl 28:16c83a2fdefa 1744
tnhnrl 17:7c16b5671d0e 1745 void StateMachine::setState(int input_state) {
tnhnrl 21:38c8544db6f4 1746 _state = input_state;
tnhnrl 17:7c16b5671d0e 1747 }
tnhnrl 20:8987a9ae2bc7 1748
tnhnrl 17:7c16b5671d0e 1749 int StateMachine::getState() {
tnhnrl 17:7c16b5671d0e 1750 return _state; //return the current state of the system
tnhnrl 17:7c16b5671d0e 1751 }
tnhnrl 20:8987a9ae2bc7 1752
tnhnrl 17:7c16b5671d0e 1753 void StateMachine::setTimeout(float input_timeout) {
tnhnrl 17:7c16b5671d0e 1754 _timeout = input_timeout;
tnhnrl 17:7c16b5671d0e 1755 }
tnhnrl 20:8987a9ae2bc7 1756
tnhnrl 17:7c16b5671d0e 1757 void StateMachine::setDepthCommand(float input_depth_command) {
tnhnrl 32:f2f8ae34aadc 1758 _depth_command = input_depth_command;
tnhnrl 17:7c16b5671d0e 1759 }
tnhnrl 20:8987a9ae2bc7 1760
tnhnrl 17:7c16b5671d0e 1761 void StateMachine::setPitchCommand(float input_pitch_command) {
tnhnrl 32:f2f8ae34aadc 1762 _pitch_command = input_pitch_command;
tnhnrl 17:7c16b5671d0e 1763 }
tnhnrl 20:8987a9ae2bc7 1764
tnhnrl 17:7c16b5671d0e 1765 void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) {
tnhnrl 21:38c8544db6f4 1766 _neutral_batt_pos_mm = batt_pos_mm;
tnhnrl 21:38c8544db6f4 1767 _neutral_bce_pos_mm = bce_pos_mm;
tnhnrl 17:7c16b5671d0e 1768
tnhnrl 21:38c8544db6f4 1769 pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm);
tnhnrl 17:7c16b5671d0e 1770 }
tnhnrl 20:8987a9ae2bc7 1771
tnhnrl 17:7c16b5671d0e 1772 int StateMachine::timeoutRunning() {
tnhnrl 28:16c83a2fdefa 1773 return _isTimeoutRunning;
tnhnrl 17:7c16b5671d0e 1774 }
tnhnrl 20:8987a9ae2bc7 1775
tnhnrl 17:7c16b5671d0e 1776 //process one state at a time
tnhnrl 17:7c16b5671d0e 1777 void StateMachine::getDiveSequence() {
tnhnrl 17:7c16b5671d0e 1778 //iterate through this sequence using the FSM
tnhnrl 24:c7d9b5bf3829 1779 currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state;
tnhnrl 24:c7d9b5bf3829 1780 currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout;
tnhnrl 24:c7d9b5bf3829 1781 currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth;
tnhnrl 24:c7d9b5bf3829 1782 currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch;
tnhnrl 17:7c16b5671d0e 1783
tnhnrl 17:7c16b5671d0e 1784 _timeout = currentStateStruct.timeout; //set timeout before exiting this function
tnhnrl 32:f2f8ae34aadc 1785 }
tnhnrl 32:f2f8ae34aadc 1786
tnhnrl 32:f2f8ae34aadc 1787 void StateMachine::printCurrentSdLog() {
tnhnrl 32:f2f8ae34aadc 1788 pc().printf("SD card log work in progress\n\r");
tnhnrl 32:f2f8ae34aadc 1789 //might be worth saving the last few logs to the MBED...
tnhnrl 32:f2f8ae34aadc 1790 }
tnhnrl 32:f2f8ae34aadc 1791
tnhnrl 32:f2f8ae34aadc 1792 //check if the file is still opened
tnhnrl 32:f2f8ae34aadc 1793 void StateMachine::createNewFile() {
tnhnrl 32:f2f8ae34aadc 1794 if (_file_closed) {
tnhnrl 34:9b66c5188051 1795 //mbedLogger().createFile(); //create a new MBED file
tnhnrl 32:f2f8ae34aadc 1796
tnhnrl 32:f2f8ae34aadc 1797 _file_closed = false; //file is still open until you get to SIT_IDLE
tnhnrl 32:f2f8ae34aadc 1798 }
tnhnrl 32:f2f8ae34aadc 1799 }
tnhnrl 32:f2f8ae34aadc 1800
tnhnrl 32:f2f8ae34aadc 1801 void StateMachine::transmitData() {
tnhnrl 32:f2f8ae34aadc 1802 static float transmit_timer = 0;
tnhnrl 32:f2f8ae34aadc 1803 static bool is_transmit_timer_running = false;
tnhnrl 32:f2f8ae34aadc 1804
tnhnrl 32:f2f8ae34aadc 1805 if (!is_transmit_timer_running) {
tnhnrl 32:f2f8ae34aadc 1806 //pc().printf("\n\n\rTRANSMIT timer running...\n\n\r"); //debug
tnhnrl 32:f2f8ae34aadc 1807
tnhnrl 32:f2f8ae34aadc 1808 transmit_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 32:f2f8ae34aadc 1809 is_transmit_timer_running = true; //disable this block after one iteration
tnhnrl 32:f2f8ae34aadc 1810
tnhnrl 32:f2f8ae34aadc 1811 pc().printf("TESTING to see if this transmits once a second. (timer: %0.1f)\n\r", timer.read());
tnhnrl 32:f2f8ae34aadc 1812 }
tnhnrl 32:f2f8ae34aadc 1813 if (timer.read() >= transmit_timer) {
tnhnrl 32:f2f8ae34aadc 1814 is_transmit_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 32:f2f8ae34aadc 1815 }
tnhnrl 34:9b66c5188051 1816 }
tnhnrl 34:9b66c5188051 1817
tnhnrl 34:9b66c5188051 1818 void StateMachine::recordData(int input_state) {
tnhnrl 34:9b66c5188051 1819 string string_state;
tnhnrl 34:9b66c5188051 1820 if (input_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 1821 string_state = "SIT_IDLE";
tnhnrl 34:9b66c5188051 1822 else if (input_state == FIND_NEUTRAL)
tnhnrl 34:9b66c5188051 1823 string_state = "FIND_NEUTRAL";
tnhnrl 34:9b66c5188051 1824 else if (input_state == DIVE)
tnhnrl 34:9b66c5188051 1825 string_state = "DIVE";
tnhnrl 34:9b66c5188051 1826 else if (input_state == RISE)
tnhnrl 34:9b66c5188051 1827 string_state = "RISE";
tnhnrl 34:9b66c5188051 1828 else if (input_state == FLOAT_LEVEL)
tnhnrl 34:9b66c5188051 1829 string_state = "FLOAT_LEVEL";
tnhnrl 34:9b66c5188051 1830 else if (input_state == FLOAT_BROADCAST)
tnhnrl 34:9b66c5188051 1831 string_state = "FLOAT_BROADCAST";
tnhnrl 34:9b66c5188051 1832 else if (input_state == EMERGENCY_CLIMB)
tnhnrl 34:9b66c5188051 1833 string_state = "EMERGENCY_CLIMB";
tnhnrl 34:9b66c5188051 1834 else if (input_state == MULTI_DIVE)
tnhnrl 34:9b66c5188051 1835 string_state = "MULTI_DIVE";
tnhnrl 34:9b66c5188051 1836 else if (input_state == MULTI_RISE)
tnhnrl 34:9b66c5188051 1837 string_state = "MULTI_RISE";
tnhnrl 34:9b66c5188051 1838 else if (input_state == KEYBOARD)
tnhnrl 34:9b66c5188051 1839 string_state = "KEYBOARD";
tnhnrl 34:9b66c5188051 1840
tnhnrl 34:9b66c5188051 1841 if (!_is_log_timer_running) {
tnhnrl 34:9b66c5188051 1842 //pc().printf("\n\n\rlog timer running...\n\n\r"); //debug
tnhnrl 34:9b66c5188051 1843
tnhnrl 34:9b66c5188051 1844 _log_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds
tnhnrl 34:9b66c5188051 1845 _is_log_timer_running = true; //disable this block after one iteration
tnhnrl 34:9b66c5188051 1846
tnhnrl 37:357e98a929cc 1847 _data_log[0] = systemTime().read(); //system time reading
tnhnrl 34:9b66c5188051 1848 _data_log[1] = depthLoop().getCommand(); //depth command
tnhnrl 34:9b66c5188051 1849 _data_log[2] = depthLoop().getPosition(); //depth reading
tnhnrl 34:9b66c5188051 1850 _data_log[3] = pitchLoop().getCommand(); //pitch command
tnhnrl 34:9b66c5188051 1851 _data_log[4] = pitchLoop().getPosition(); //pitch reading
tnhnrl 34:9b66c5188051 1852 _data_log[5] = bce().getSetPosition_mm();
tnhnrl 34:9b66c5188051 1853 _data_log[6] = bce().getPosition_mm();
tnhnrl 34:9b66c5188051 1854 _data_log[7] = batt().getSetPosition_mm();
tnhnrl 34:9b66c5188051 1855 _data_log[8] = batt().getPosition_mm();
tnhnrl 34:9b66c5188051 1856
tnhnrl 34:9b66c5188051 1857 //record data to the MBED every 5 seconds
tnhnrl 35:2f66ea4863d5 1858 //mbedLogger().saveArrayToFile(string_state,input_state,_data_log);
tnhnrl 34:9b66c5188051 1859 }
tnhnrl 34:9b66c5188051 1860 if (timer.read() >= _log_timer) {
tnhnrl 34:9b66c5188051 1861 _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again
tnhnrl 34:9b66c5188051 1862 }
tnhnrl 34:9b66c5188051 1863 }
tnhnrl 34:9b66c5188051 1864
tnhnrl 34:9b66c5188051 1865 void StateMachine::recordState(int input_state) {
tnhnrl 34:9b66c5188051 1866 string string_state;
tnhnrl 34:9b66c5188051 1867
tnhnrl 34:9b66c5188051 1868 if (input_state == SIT_IDLE)
tnhnrl 34:9b66c5188051 1869 string_state = "SIT_IDLE";
tnhnrl 34:9b66c5188051 1870 else if (input_state == FIND_NEUTRAL)
tnhnrl 34:9b66c5188051 1871 string_state = "FIND_NEUTRAL";
tnhnrl 34:9b66c5188051 1872 else if (input_state == DIVE)
tnhnrl 34:9b66c5188051 1873 string_state = "DIVE";
tnhnrl 34:9b66c5188051 1874 else if (input_state == RISE)
tnhnrl 34:9b66c5188051 1875 string_state = "RISE";
tnhnrl 34:9b66c5188051 1876 else if (input_state == FLOAT_LEVEL)
tnhnrl 34:9b66c5188051 1877 string_state = "FLOAT_LEVEL";
tnhnrl 34:9b66c5188051 1878 else if (input_state == FLOAT_BROADCAST)
tnhnrl 34:9b66c5188051 1879 string_state = "FLOAT_BROADCAST";
tnhnrl 34:9b66c5188051 1880 else if (input_state == EMERGENCY_CLIMB)
tnhnrl 34:9b66c5188051 1881 string_state = "EMERGENCY_CLIMB";
tnhnrl 34:9b66c5188051 1882 else if (input_state == MULTI_DIVE)
tnhnrl 34:9b66c5188051 1883 string_state = "MULTI_DIVE";
tnhnrl 34:9b66c5188051 1884 else if (input_state == MULTI_RISE)
tnhnrl 34:9b66c5188051 1885 string_state = "MULTI_RISE";
tnhnrl 34:9b66c5188051 1886 else if (input_state == KEYBOARD)
tnhnrl 34:9b66c5188051 1887 string_state = "KEYBOARD";
tnhnrl 34:9b66c5188051 1888 //datalogger().printf("%s\n", string_state.c_str());
tnhnrl 36:966a86937e17 1889 }
tnhnrl 36:966a86937e17 1890
tnhnrl 36:966a86937e17 1891 float * StateMachine::dataArray() {
tnhnrl 36:966a86937e17 1892 //return the array to a calling function
tnhnrl 36:966a86937e17 1893 return _data_log;
tnhnrl 52:f207567d3ea4 1894 }
tnhnrl 52:f207567d3ea4 1895
tnhnrl 52:f207567d3ea4 1896 // 06/06/2018
tnhnrl 52:f207567d3ea4 1897 float StateMachine::getFloatUserInput() {
tnhnrl 52:f207567d3ea4 1898 float float_conversion = 0.0;
tnhnrl 52:f207567d3ea4 1899
tnhnrl 52:f207567d3ea4 1900 while(1) {
tnhnrl 52:f207567d3ea4 1901 bool valid_input = false; //flag for valid or invalid input
tnhnrl 52:f207567d3ea4 1902
tnhnrl 52:f207567d3ea4 1903 pc().printf("Please enter your number below and press ENTER:\n\r");
tnhnrl 52:f207567d3ea4 1904 char user_string [80]; //variable to store input as a character array
tnhnrl 52:f207567d3ea4 1905
tnhnrl 52:f207567d3ea4 1906 pc().scanf("%s", user_string); //read formatted data from stdin
tnhnrl 52:f207567d3ea4 1907 pc().printf("user_string was <%s>\n\r", user_string);
tnhnrl 52:f207567d3ea4 1908
tnhnrl 52:f207567d3ea4 1909 //check through the string for invalid characters (decimal values 43 through 57)
tnhnrl 52:f207567d3ea4 1910 for (int c = 0; c < strlen(user_string); c++) {
tnhnrl 52:f207567d3ea4 1911 //pc().printf("character is [%c]\n\r", user_string[c]); //debug
tnhnrl 52:f207567d3ea4 1912 if (user_string[c] >= 43 and user_string[c] <= 57) {
tnhnrl 52:f207567d3ea4 1913 //pc().printf("VALID CHARACTER!\n\r"); //debug
tnhnrl 52:f207567d3ea4 1914 ;
tnhnrl 52:f207567d3ea4 1915 }
tnhnrl 52:f207567d3ea4 1916 else {
tnhnrl 52:f207567d3ea4 1917 pc().printf("INVALID INPUT!\n\r");
tnhnrl 52:f207567d3ea4 1918 break;
tnhnrl 52:f207567d3ea4 1919 }
tnhnrl 52:f207567d3ea4 1920
tnhnrl 52:f207567d3ea4 1921 if (c == (strlen(user_string) - 1)) {
tnhnrl 52:f207567d3ea4 1922 valid_input = true;
tnhnrl 52:f207567d3ea4 1923 }
tnhnrl 52:f207567d3ea4 1924 }
tnhnrl 52:f207567d3ea4 1925
tnhnrl 52:f207567d3ea4 1926 if (valid_input) {
tnhnrl 52:f207567d3ea4 1927 float_conversion = atof(user_string);
tnhnrl 52:f207567d3ea4 1928 pc().printf("VALID INPUT! Your input was: %f\n\r", float_conversion);
tnhnrl 52:f207567d3ea4 1929 break;
tnhnrl 52:f207567d3ea4 1930 }
tnhnrl 52:f207567d3ea4 1931 }
tnhnrl 52:f207567d3ea4 1932
tnhnrl 52:f207567d3ea4 1933 return float_conversion;
tnhnrl 16:3363b9f14913 1934 }