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