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