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