most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
tnhnrl
Date:
Wed Dec 06 22:08:44 2017 +0000
Revision:
30:2964617e7676
Parent:
28:16c83a2fdefa
Child:
32:f2f8ae34aadc
Used Friday 12/01/17 code with find_neutral exit fix and sub-FSM fix (starting in NEUTRAL_SINKING)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tnhnrl 16:3363b9f14913 1 #include "StateMachine.hpp"
tnhnrl 16:3363b9f14913 2 #include "StaticDefs.hpp"
tnhnrl 20:8987a9ae2bc7 3
tnhnrl 16:3363b9f14913 4 StateMachine::StateMachine() {
tnhnrl 24:c7d9b5bf3829 5 _timeout = 480; // generic timeout for every state, seconds
tnhnrl 20:8987a9ae2bc7 6
tnhnrl 28:16c83a2fdefa 7 _pitchTolerance = 5.0; // pitch angle tolerance for FLOAT_LEVEL state
tnhnrl 20:8987a9ae2bc7 8
tnhnrl 28:16c83a2fdefa 9 _bceFloatPosition = bce().getTravelLimit(); // bce position for "float" states (max travel limit for BCE is 320 mm)
tnhnrl 28:16c83a2fdefa 10 _battFloatPosition = batt().getTravelLimit(); // batt position tail high for "broadcast" state (max travel limit for battery is 75 mm)
tnhnrl 20:8987a9ae2bc7 11
tnhnrl 28:16c83a2fdefa 12 _depthCommand = 2.0; // user keyboard depth (default)
tnhnrl 28:16c83a2fdefa 13 _pitchCommand = -20.0; // user keyboard pitch (default)
tnhnrl 17:7c16b5671d0e 14
tnhnrl 28:16c83a2fdefa 15 _neutral_timer = 0; //timer used in FIND_NEUTRAL sub-FSM
tnhnrl 20:8987a9ae2bc7 16
tnhnrl 28:16c83a2fdefa 17 _state = SIT_IDLE; // select starting state here
tnhnrl 28:16c83a2fdefa 18 _isTimeoutRunning = false; // default timer to not running
tnhnrl 28:16c83a2fdefa 19 _isSubStateTimerRunning = false; // default timer to not running
tnhnrl 17:7c16b5671d0e 20
tnhnrl 24:c7d9b5bf3829 21 _multi_dive_counter = 0;
tnhnrl 21:38c8544db6f4 22
tnhnrl 21:38c8544db6f4 23 _neutral_sub_state_active = false;
tnhnrl 17:7c16b5671d0e 24
tnhnrl 21:38c8544db6f4 25 _depth_KP = depthLoop().getControllerP(); // load current depth value
tnhnrl 21:38c8544db6f4 26 _depth_KI = depthLoop().getControllerI(); // load current depth value
tnhnrl 21:38c8544db6f4 27 _depth_KD = depthLoop().getControllerD(); // load current depth value
tnhnrl 21:38c8544db6f4 28
tnhnrl 21:38c8544db6f4 29 _pitch_KP = pitchLoop().getControllerP(); // load current pitch value
tnhnrl 21:38c8544db6f4 30 _pitch_KI = pitchLoop().getControllerI(); // load current pitch value
tnhnrl 21:38c8544db6f4 31 _pitch_KD = pitchLoop().getControllerD(); // load current pitch value
tnhnrl 21:38c8544db6f4 32
tnhnrl 21:38c8544db6f4 33 _neutral_bce_pos_mm = depthLoop().getOutputOffset(); //load current neutral buoyancy position offset
tnhnrl 21:38c8544db6f4 34 _neutral_batt_pos_mm = pitchLoop().getOutputOffset(); //load current neutral buoyancy position offset
tnhnrl 23:434f04ef1fad 35
tnhnrl 28:16c83a2fdefa 36 _state_array_counter = 1; //used to iterate through and record states
tnhnrl 28:16c83a2fdefa 37 _substate_array_counter = 0; //used to iterate through and record substates
tnhnrl 28:16c83a2fdefa 38
tnhnrl 28:16c83a2fdefa 39 _state_array[0] = SIT_IDLE; //system starts in the SIT_IDLE state, record this
tnhnrl 24:c7d9b5bf3829 40
tnhnrl 30:2964617e7676 41 _substate = NEUTRAL_SINKING; //start sub-FSM in NEUTRAL_SINKING
tnhnrl 28:16c83a2fdefa 42 _previous_substate = -1; //to start sub-FSM
tnhnrl 28:16c83a2fdefa 43 _previous_state = -1; //for tracking FSM states
tnhnrl 28:16c83a2fdefa 44
tnhnrl 28:16c83a2fdefa 45 _max_recorded_depth_neutral = -99; //float to record max depth
tnhnrl 28:16c83a2fdefa 46 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 16:3363b9f14913 47 }
tnhnrl 20:8987a9ae2bc7 48
tnhnrl 17:7c16b5671d0e 49 //Finite State Machine (FSM)
tnhnrl 24:c7d9b5bf3829 50 void StateMachine::runStateMachine() {
tnhnrl 16:3363b9f14913 51 // finite state machine ... each state has at least one exit criteria
tnhnrl 17:7c16b5671d0e 52 switch (_state) {
tnhnrl 16:3363b9f14913 53 case SIT_IDLE :
tnhnrl 28:16c83a2fdefa 54 case KEYBOARD:
tnhnrl 16:3363b9f14913 55 // there actually is no timeout for SIT_IDLE, but this enables some one-shot actions
tnhnrl 28:16c83a2fdefa 56 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 57 showMenu();
tnhnrl 16:3363b9f14913 58 pc().printf("\r\n\nstate: SIT_IDLE\r\n");
tnhnrl 28:16c83a2fdefa 59 _isTimeoutRunning = true;
tnhnrl 20:8987a9ae2bc7 60
tnhnrl 16:3363b9f14913 61 // what is active?
tnhnrl 16:3363b9f14913 62 bce().pause();
tnhnrl 16:3363b9f14913 63 batt().pause();
tnhnrl 17:7c16b5671d0e 64
tnhnrl 17:7c16b5671d0e 65 //reset sub FSM
tnhnrl 28:16c83a2fdefa 66 _isSubStateTimerRunning = false;
tnhnrl 16:3363b9f14913 67 }
tnhnrl 20:8987a9ae2bc7 68
tnhnrl 16:3363b9f14913 69 // how exit?
tnhnrl 20:8987a9ae2bc7 70 keyboard(); // keyboard function will change the state if needed
tnhnrl 16:3363b9f14913 71 break;
tnhnrl 20:8987a9ae2bc7 72
tnhnrl 16:3363b9f14913 73 case EMERGENCY_CLIMB :
tnhnrl 16:3363b9f14913 74 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 75 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 76 pc().printf("\r\n\nstate: EMERGENCY_CLIMB\r\n");
tnhnrl 16:3363b9f14913 77 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 78 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 79 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 80
tnhnrl 16:3363b9f14913 81 // what needs to be started?
tnhnrl 16:3363b9f14913 82 bce().unpause();
tnhnrl 16:3363b9f14913 83 batt().unpause();
tnhnrl 20:8987a9ae2bc7 84
tnhnrl 20:8987a9ae2bc7 85 // what are the commands?
tnhnrl 16:3363b9f14913 86 bce().setPosition_mm(bce().getTravelLimit());
tnhnrl 16:3363b9f14913 87 batt().setPosition_mm(0.0);
tnhnrl 16:3363b9f14913 88 }
tnhnrl 20:8987a9ae2bc7 89
tnhnrl 16:3363b9f14913 90 // how exit?
tnhnrl 17:7c16b5671d0e 91 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 92 pc().printf("EC: timed out\r\n");
tnhnrl 21:38c8544db6f4 93 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 94 timer.reset();
tnhnrl 28:16c83a2fdefa 95 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 96 }
tnhnrl 26:7e118fc02eea 97 else if (depthLoop().getPosition() < 2.0) { //if the depth is greater than 0.2 feet, go to float broadcast
tnhnrl 16:3363b9f14913 98 pc().printf("EC: depth: %3.1f, cmd: 0.5 [%0.1f sec]\r",depthLoop().getPosition(), timer.read());
tnhnrl 21:38c8544db6f4 99 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 100 timer.reset();
tnhnrl 28:16c83a2fdefa 101 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 102 }
tnhnrl 16:3363b9f14913 103 break;
tnhnrl 20:8987a9ae2bc7 104
tnhnrl 16:3363b9f14913 105 case FIND_NEUTRAL :
tnhnrl 20:8987a9ae2bc7 106 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 107 if (!_isTimeoutRunning) {
tnhnrl 21:38c8544db6f4 108 pc().printf("\r\n\nstate: FIND_NEUTRAL\n\r");
tnhnrl 16:3363b9f14913 109 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 110 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 111 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 112
tnhnrl 16:3363b9f14913 113 // what needs to be started?
tnhnrl 16:3363b9f14913 114 bce().unpause();
tnhnrl 16:3363b9f14913 115 batt().unpause();
tnhnrl 28:16c83a2fdefa 116 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 24:c7d9b5bf3829 117 batt().setPosition_mm(_neutral_batt_pos_mm); //set battery to close-to-neutral setting from config file
tnhnrl 17:7c16b5671d0e 118
tnhnrl 24:c7d9b5bf3829 119 //first iteration goes into Neutral Finding Sub-FSM
tnhnrl 24:c7d9b5bf3829 120 //set the first state of the FSM, and start the sub-FSM
tnhnrl 30:2964617e7676 121 _substate = NEUTRAL_SINKING; //first state in neutral sub-FSM is the pressure vessel sinking
tnhnrl 28:16c83a2fdefa 122 _previous_substate = -1;
tnhnrl 28:16c83a2fdefa 123
tnhnrl 28:16c83a2fdefa 124 //save this state to the array
tnhnrl 30:2964617e7676 125 _substate_array[_substate_array_counter] = NEUTRAL_SINKING; //save to state array
tnhnrl 28:16c83a2fdefa 126 _substate_array_counter++;
tnhnrl 28:16c83a2fdefa 127
tnhnrl 24:c7d9b5bf3829 128 runNeutralStateMachine();
tnhnrl 16:3363b9f14913 129 }
tnhnrl 20:8987a9ae2bc7 130
tnhnrl 20:8987a9ae2bc7 131 // how exit? (exit with the timer, if timer still running continue processing sub FSM)
tnhnrl 17:7c16b5671d0e 132 if (timer > _timeout) {
tnhnrl 17:7c16b5671d0e 133 pc().printf("FN: timed out [time: %0.1f sec]\r\n", timer.read());
tnhnrl 21:38c8544db6f4 134 _state = EMERGENCY_CLIMB; //new behavior (if this times out it emergency surfaces)
tnhnrl 16:3363b9f14913 135 timer.reset();
tnhnrl 28:16c83a2fdefa 136 _isTimeoutRunning = false;
tnhnrl 24:c7d9b5bf3829 137
tnhnrl 24:c7d9b5bf3829 138 //record this to the NEUTRAL sub-FSM tracker
tnhnrl 28:16c83a2fdefa 139 _substate_array[_substate_array_counter] = EMERGENCY_CLIMB; //save to state array
tnhnrl 28:16c83a2fdefa 140 _substate_array_counter++;
tnhnrl 16:3363b9f14913 141 }
tnhnrl 21:38c8544db6f4 142
tnhnrl 24:c7d9b5bf3829 143 //what is active? (neutral finding sub-function runs until completion)
tnhnrl 24:c7d9b5bf3829 144 //check if substate returned exit state, if so stop running the sub-FSM
tnhnrl 26:7e118fc02eea 145 else if (runNeutralStateMachine() == NEUTRAL_EXIT) {
tnhnrl 21:38c8544db6f4 146 //if successful, FIND_NEUTRAL then goes to RISE
tnhnrl 21:38c8544db6f4 147 pc().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\n\n\r");
tnhnrl 21:38c8544db6f4 148 _state = RISE;
tnhnrl 30:2964617e7676 149 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 150 }
tnhnrl 17:7c16b5671d0e 151 break;
tnhnrl 17:7c16b5671d0e 152
tnhnrl 16:3363b9f14913 153 case DIVE :
tnhnrl 16:3363b9f14913 154 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 155 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 156 pc().printf("\r\n\nstate: DIVE\r\n");
tnhnrl 16:3363b9f14913 157 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 158 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 159 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 160
tnhnrl 16:3363b9f14913 161 // what needs to be started?
tnhnrl 16:3363b9f14913 162 bce().unpause();
tnhnrl 16:3363b9f14913 163 batt().unpause();
tnhnrl 20:8987a9ae2bc7 164
tnhnrl 16:3363b9f14913 165 // what are the commands?
tnhnrl 28:16c83a2fdefa 166 depthLoop().setCommand(_depthCommand);
tnhnrl 28:16c83a2fdefa 167 pitchLoop().setCommand(_pitchCommand);
tnhnrl 16:3363b9f14913 168 pc().printf("DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand());
tnhnrl 16:3363b9f14913 169 pc().printf("DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 28:16c83a2fdefa 170
tnhnrl 28:16c83a2fdefa 171 //reset max dive depth
tnhnrl 28:16c83a2fdefa 172 _max_recorded_depth_dive = -99; //float to record max depth
tnhnrl 16:3363b9f14913 173 }
tnhnrl 20:8987a9ae2bc7 174
tnhnrl 16:3363b9f14913 175 // how exit?
tnhnrl 17:7c16b5671d0e 176 if (timer > _timeout) {
tnhnrl 17:7c16b5671d0e 177 pc().printf("DIVE: timed out\n\n\r");
tnhnrl 21:38c8544db6f4 178 _state = RISE; //new behavior 11/17/2017
tnhnrl 16:3363b9f14913 179 timer.reset();
tnhnrl 28:16c83a2fdefa 180 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 181 }
tnhnrl 16:3363b9f14913 182 else if (depthLoop().getPosition() > depthLoop().getCommand()) {
tnhnrl 16:3363b9f14913 183 pc().printf("DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 21:38c8544db6f4 184 _state = RISE;
tnhnrl 16:3363b9f14913 185 timer.reset();
tnhnrl 28:16c83a2fdefa 186 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 187 }
tnhnrl 20:8987a9ae2bc7 188
tnhnrl 16:3363b9f14913 189 // what is active?
tnhnrl 21:38c8544db6f4 190 pc().printf("DIVE: bce pos: %3.1f mm, batt pos: %3.1f mm (depth: %3.1f ft) (pitch: %3.1f deg)[%0.2f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), pitchLoop().getPosition(), timer.read());
tnhnrl 16:3363b9f14913 191 bce().setPosition_mm(depthLoop().getOutput());
tnhnrl 16:3363b9f14913 192 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 28:16c83a2fdefa 193
tnhnrl 28:16c83a2fdefa 194 if (depthLoop().getPosition() > _max_recorded_depth_dive) { //debug
tnhnrl 28:16c83a2fdefa 195 _max_recorded_depth_dive = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 196 }
tnhnrl 16:3363b9f14913 197 break;
tnhnrl 16:3363b9f14913 198
tnhnrl 16:3363b9f14913 199 case RISE :
tnhnrl 16:3363b9f14913 200 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 201 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 202 pc().printf("\r\n\nstate: RISE\r\n");
tnhnrl 16:3363b9f14913 203 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 204 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 205 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 206
tnhnrl 16:3363b9f14913 207 // what needs to be started?
tnhnrl 16:3363b9f14913 208 bce().unpause();
tnhnrl 16:3363b9f14913 209 batt().unpause();
tnhnrl 16:3363b9f14913 210
tnhnrl 16:3363b9f14913 211 // what are the commands?
tnhnrl 28:16c83a2fdefa 212 depthLoop().setCommand(-1.0); //make sure to get towards the surface (saw issues at LASR pool)
tnhnrl 28:16c83a2fdefa 213 pitchLoop().setCommand(-_pitchCommand);
tnhnrl 16:3363b9f14913 214 pc().printf("RISE: depth cmd: 0.0\r\n");
tnhnrl 16:3363b9f14913 215 pc().printf("RISE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 216 }
tnhnrl 20:8987a9ae2bc7 217
tnhnrl 16:3363b9f14913 218 // how exit?
tnhnrl 17:7c16b5671d0e 219 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 220 pc().printf("RISE: timed out\r\n");
tnhnrl 21:38c8544db6f4 221 _state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 222 timer.reset();
tnhnrl 28:16c83a2fdefa 223 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 224 }
tnhnrl 28:16c83a2fdefa 225 else if (depthLoop().getPosition() < 0.5) { //removed depthLoop().getCommand()
tnhnrl 16:3363b9f14913 226 pc().printf("RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 28:16c83a2fdefa 227 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 228 timer.reset();
tnhnrl 28:16c83a2fdefa 229 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 230 }
tnhnrl 20:8987a9ae2bc7 231
tnhnrl 20:8987a9ae2bc7 232 // what is active?
tnhnrl 16:3363b9f14913 233 pc().printf("RISE: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read());
tnhnrl 17:7c16b5671d0e 234 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 17:7c16b5671d0e 235 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 16:3363b9f14913 236 break;
tnhnrl 16:3363b9f14913 237
tnhnrl 16:3363b9f14913 238 case FLOAT_LEVEL :
tnhnrl 16:3363b9f14913 239 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 240 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 241 pc().printf("\r\n\nstate: FLOAT_LEVEL\r\n");
tnhnrl 16:3363b9f14913 242 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 243 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 244 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 245
tnhnrl 16:3363b9f14913 246 // what needs to be started?
tnhnrl 16:3363b9f14913 247 bce().unpause();
tnhnrl 16:3363b9f14913 248 batt().unpause();
tnhnrl 16:3363b9f14913 249
tnhnrl 20:8987a9ae2bc7 250 // what are the commands?
tnhnrl 28:16c83a2fdefa 251 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 16:3363b9f14913 252 pitchLoop().setCommand(0.0);
tnhnrl 16:3363b9f14913 253 }
tnhnrl 20:8987a9ae2bc7 254
tnhnrl 16:3363b9f14913 255 // how exit?
tnhnrl 17:7c16b5671d0e 256 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 257 pc().printf("FL: timed out\r\n");
tnhnrl 21:38c8544db6f4 258 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 259 timer.reset();
tnhnrl 28:16c83a2fdefa 260 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 261 }
tnhnrl 28:16c83a2fdefa 262 else if (fabs(imu().getPitch() - pitchLoop().getCommand()) < fabs(_pitchTolerance)) { //current tolerance is 5 degrees
tnhnrl 28:16c83a2fdefa 263 pc().printf("FL: pitch: %3.1f mm, set pos: %3.1f mm, deadband: %3.1f mm\r\n",imu().getPitch(), pitchLoop().getCommand(), _pitchTolerance);
tnhnrl 21:38c8544db6f4 264 _state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 265 timer.reset();
tnhnrl 28:16c83a2fdefa 266 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 267 }
tnhnrl 20:8987a9ae2bc7 268
tnhnrl 16:3363b9f14913 269 // what is active?
tnhnrl 16:3363b9f14913 270 pc().printf("FL: pitchLoop output: %3.1f, batt pos: %3.1f, piston pos: %3.1f [%0.1f sec]\r", pitchLoop().getOutput(), batt().getPosition_mm(), bce().getPosition_mm(), timer.read());
tnhnrl 16:3363b9f14913 271 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 16:3363b9f14913 272 break;
tnhnrl 16:3363b9f14913 273
tnhnrl 16:3363b9f14913 274 case FLOAT_BROADCAST :
tnhnrl 16:3363b9f14913 275 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 276 if (!_isTimeoutRunning) {
tnhnrl 16:3363b9f14913 277 pc().printf("\r\n\nstate: FLOAT_BROADCAST\r\n");
tnhnrl 16:3363b9f14913 278 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 279 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 280 _isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 281
tnhnrl 16:3363b9f14913 282 // what needs to be started?
tnhnrl 16:3363b9f14913 283 bce().unpause();
tnhnrl 16:3363b9f14913 284 batt().unpause();
tnhnrl 20:8987a9ae2bc7 285
tnhnrl 16:3363b9f14913 286 // what are the commands?
tnhnrl 28:16c83a2fdefa 287 bce().setPosition_mm(_bceFloatPosition);
tnhnrl 28:16c83a2fdefa 288 batt().setPosition_mm(_battFloatPosition);
tnhnrl 16:3363b9f14913 289 }
tnhnrl 20:8987a9ae2bc7 290
tnhnrl 16:3363b9f14913 291 // how exit?
tnhnrl 17:7c16b5671d0e 292 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 293 pc().printf("FB: timed out\r\n");
tnhnrl 21:38c8544db6f4 294 _state = SIT_IDLE;
tnhnrl 16:3363b9f14913 295 timer.reset();
tnhnrl 28:16c83a2fdefa 296 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 297 }
tnhnrl 20:8987a9ae2bc7 298 else if ( (fabs(bce().getPosition_mm() - bce().getSetPosition_mm()) < bce().getDeadband()) and
tnhnrl 20:8987a9ae2bc7 299 (fabs(batt().getPosition_mm() - batt().getSetPosition_mm()) < batt().getDeadband()) ) {
tnhnrl 16:3363b9f14913 300 pc().printf("FB: position: %3.1f mm, set pos: %3.1f mm, deadband: %3.1f mm\r\n",bce().getPosition_mm(), bce().getSetPosition_mm(), bce().getDeadband());
tnhnrl 21:38c8544db6f4 301 _state = SIT_IDLE;
tnhnrl 16:3363b9f14913 302 timer.reset();
tnhnrl 28:16c83a2fdefa 303 _isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 304 }
tnhnrl 20:8987a9ae2bc7 305
tnhnrl 20:8987a9ae2bc7 306 // what is active?
tnhnrl 26:7e118fc02eea 307 pc().printf("FB: bce pos: %0.1f mm, batt pos: %0.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec] (setPos batt: %0.1f bce: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read(), bce().getSetPosition_mm(),batt().getSetPosition_mm());
tnhnrl 16:3363b9f14913 308 break;
tnhnrl 17:7c16b5671d0e 309
tnhnrl 17:7c16b5671d0e 310 case MULTI_DIVE :
tnhnrl 17:7c16b5671d0e 311 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 312 if (!_isTimeoutRunning) {
tnhnrl 17:7c16b5671d0e 313 pc().printf("\r\n\nstate: MULTI-DIVE\r\n");
tnhnrl 17:7c16b5671d0e 314 timer.reset(); // timer goes back to zero
tnhnrl 17:7c16b5671d0e 315 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 316 _isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 317
tnhnrl 17:7c16b5671d0e 318 // what needs to be started?
tnhnrl 17:7c16b5671d0e 319 bce().unpause();
tnhnrl 17:7c16b5671d0e 320 batt().unpause();
tnhnrl 17:7c16b5671d0e 321
tnhnrl 21:38c8544db6f4 322 //retrieve commands from structs (loaded from sequence.cfg file)
tnhnrl 28:16c83a2fdefa 323 float sequence_depthCommand = currentStateStruct.depth;
tnhnrl 28:16c83a2fdefa 324 float sequence_pitchCommand = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 325
tnhnrl 17:7c16b5671d0e 326 // what are the commands?
tnhnrl 28:16c83a2fdefa 327 depthLoop().setCommand(sequence_depthCommand);
tnhnrl 28:16c83a2fdefa 328 pitchLoop().setCommand(sequence_pitchCommand);
tnhnrl 21:38c8544db6f4 329 pc().printf("MULTI-DIVE: depth cmd: %3.1f ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand());
tnhnrl 17:7c16b5671d0e 330 }
tnhnrl 20:8987a9ae2bc7 331
tnhnrl 17:7c16b5671d0e 332 // how exit?
tnhnrl 17:7c16b5671d0e 333 if (timer > _timeout) {
tnhnrl 21:38c8544db6f4 334 pc().printf("\n\n\rMULTI-DIVE: timed out [time: %0.1f]\n\n\r", timer.read());
tnhnrl 21:38c8544db6f4 335 _state = MULTI_RISE; //new behavior 11/17/2017
tnhnrl 17:7c16b5671d0e 336 timer.reset();
tnhnrl 28:16c83a2fdefa 337 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 338 }
tnhnrl 17:7c16b5671d0e 339 else if (depthLoop().getPosition() > depthLoop().getCommand()) {
tnhnrl 17:7c16b5671d0e 340 pc().printf("MULTI-DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 21:38c8544db6f4 341 _state = MULTI_RISE;
tnhnrl 17:7c16b5671d0e 342 timer.reset();
tnhnrl 28:16c83a2fdefa 343 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 344 }
tnhnrl 20:8987a9ae2bc7 345
tnhnrl 17:7c16b5671d0e 346 // what is active?
tnhnrl 17:7c16b5671d0e 347 pc().printf("MULTI-DIVE: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read());
tnhnrl 17:7c16b5671d0e 348 bce().setPosition_mm(depthLoop().getOutput());
tnhnrl 17:7c16b5671d0e 349 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 17:7c16b5671d0e 350 break;
tnhnrl 17:7c16b5671d0e 351
tnhnrl 17:7c16b5671d0e 352 case MULTI_RISE :
tnhnrl 17:7c16b5671d0e 353 // start local state timer and init any other one-shot actions
tnhnrl 28:16c83a2fdefa 354 if (!_isTimeoutRunning) {
tnhnrl 17:7c16b5671d0e 355 pc().printf("\r\n\nstate: MULTI-RISE\r\n");
tnhnrl 17:7c16b5671d0e 356 timer.reset(); // timer goes back to zero
tnhnrl 17:7c16b5671d0e 357 timer.start(); // background timer starts running
tnhnrl 28:16c83a2fdefa 358 _isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 359
tnhnrl 17:7c16b5671d0e 360 // what needs to be started?
tnhnrl 17:7c16b5671d0e 361 bce().unpause();
tnhnrl 17:7c16b5671d0e 362 batt().unpause();
tnhnrl 17:7c16b5671d0e 363
tnhnrl 17:7c16b5671d0e 364 //NEW: retrieve depth and pitch commands from config file struct
tnhnrl 17:7c16b5671d0e 365 // concept is to load this each time the multi-dive restarts
tnhnrl 17:7c16b5671d0e 366 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 367
tnhnrl 17:7c16b5671d0e 368 //retrieve just pitch command from struct
tnhnrl 28:16c83a2fdefa 369 float sequence_pitchCommand = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 370
tnhnrl 17:7c16b5671d0e 371 // what are the commands? (send back to 0.5 feet, not surface) // 11/21/2017
tnhnrl 17:7c16b5671d0e 372 depthLoop().setCommand(0.5);
tnhnrl 28:16c83a2fdefa 373 pitchLoop().setCommand(-sequence_pitchCommand);
tnhnrl 21:38c8544db6f4 374 pc().printf("MULTI-RISE: depth cmd: 0.0 ft, pitch cmd: %3.1f deg\r\n",depthLoop().getCommand(), pitchLoop().getCommand());
tnhnrl 17:7c16b5671d0e 375 }
tnhnrl 20:8987a9ae2bc7 376
tnhnrl 17:7c16b5671d0e 377 // how exit?
tnhnrl 17:7c16b5671d0e 378 if (timer > _timeout) {
tnhnrl 21:38c8544db6f4 379 pc().printf("MULTI-RISE: timed out [time: %0.1f]\n\n\r", timer.read());
tnhnrl 21:38c8544db6f4 380 _state = EMERGENCY_CLIMB;
tnhnrl 17:7c16b5671d0e 381 timer.reset();
tnhnrl 28:16c83a2fdefa 382 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 383
tnhnrl 17:7c16b5671d0e 384 //reset multi-dive sequence to start
tnhnrl 24:c7d9b5bf3829 385 _multi_dive_counter = 0;
tnhnrl 17:7c16b5671d0e 386 }
tnhnrl 20:8987a9ae2bc7 387 else if (depthLoop().getPosition() < 0.5) { // depth is less than 0.5 (zero is surface level)
tnhnrl 17:7c16b5671d0e 388 pc().printf("MULTI-RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 389
tnhnrl 17:7c16b5671d0e 390 //going to next state
tnhnrl 28:16c83a2fdefa 391 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 392
tnhnrl 17:7c16b5671d0e 393 //successful dive-rise sequence CONTINUES the multi-dive sequence
tnhnrl 24:c7d9b5bf3829 394 _multi_dive_counter++;
tnhnrl 17:7c16b5671d0e 395
tnhnrl 17:7c16b5671d0e 396 //UPDATE THE SEQUENCE DATA HERE
tnhnrl 17:7c16b5671d0e 397 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 398
tnhnrl 17:7c16b5671d0e 399 //check if this is the end of the dive sequence
tnhnrl 30:2964617e7676 400 //CHECK BEFORE ANYTHING ELSE that you have reached the "exit" state (FLOAT_BROADCAST)
tnhnrl 30:2964617e7676 401 if (currentStateStruct.state == FLOAT_BROADCAST) {
tnhnrl 28:16c83a2fdefa 402 _state = FLOAT_BROADCAST;
tnhnrl 17:7c16b5671d0e 403 return;
tnhnrl 17:7c16b5671d0e 404 }
tnhnrl 17:7c16b5671d0e 405
tnhnrl 17:7c16b5671d0e 406 else
tnhnrl 21:38c8544db6f4 407 _state = MULTI_DIVE;
tnhnrl 17:7c16b5671d0e 408
tnhnrl 24:c7d9b5bf3829 409 //have to stop this with the _multi_dive_counter variable!
tnhnrl 17:7c16b5671d0e 410 }
tnhnrl 20:8987a9ae2bc7 411
tnhnrl 20:8987a9ae2bc7 412 // what is active?
tnhnrl 17:7c16b5671d0e 413 pc().printf("MULTI-RISE: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read());
tnhnrl 17:7c16b5671d0e 414 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 17:7c16b5671d0e 415 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 17:7c16b5671d0e 416 break;
tnhnrl 16:3363b9f14913 417
tnhnrl 16:3363b9f14913 418 default :
tnhnrl 17:7c16b5671d0e 419 pc().printf("DEBUG: SIT_IDLE\n\r");
tnhnrl 21:38c8544db6f4 420 _state = SIT_IDLE;
tnhnrl 28:16c83a2fdefa 421 }
tnhnrl 28:16c83a2fdefa 422
tnhnrl 28:16c83a2fdefa 423 //save the state to print to user
tnhnrl 28:16c83a2fdefa 424 if (_previous_state != _state) {
tnhnrl 28:16c83a2fdefa 425 _state_array[_state_array_counter] = _state; //save to state array
tnhnrl 28:16c83a2fdefa 426 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 427
tnhnrl 28:16c83a2fdefa 428 _previous_state = _state;
tnhnrl 28:16c83a2fdefa 429 }
tnhnrl 16:3363b9f14913 430 }
tnhnrl 20:8987a9ae2bc7 431
tnhnrl 16:3363b9f14913 432 // output the keyboard menu for user's reference
tnhnrl 16:3363b9f14913 433 void StateMachine::showMenu() {
tnhnrl 16:3363b9f14913 434 pc().printf("\r\r\n\nKEYBOARD MENU:\r\r\n");
tnhnrl 16:3363b9f14913 435 pc().printf(" N to find neutral\r\n");
tnhnrl 17:7c16b5671d0e 436 pc().printf(" M to initiate multi-dive cycle\r\n");
tnhnrl 16:3363b9f14913 437 pc().printf(" D to initiate dive cycle\r\n");
tnhnrl 16:3363b9f14913 438 pc().printf(" R to initiate rise\r\n");
tnhnrl 16:3363b9f14913 439 pc().printf(" L to float level\r\n");
tnhnrl 16:3363b9f14913 440 pc().printf(" B to float at broadcast pitch\r\n");
tnhnrl 16:3363b9f14913 441 pc().printf(" E to initiate emergency climb\r\n");
tnhnrl 28:16c83a2fdefa 442 //pc().printf(" H to run homing sequence on both BCE and Batt\r\n");
tnhnrl 16:3363b9f14913 443 pc().printf(" T to tare the depth sensor\r\n");
tnhnrl 28:16c83a2fdefa 444 pc().printf(" Z to show FSM and sub-FSM states.\r\n");
tnhnrl 16:3363b9f14913 445 pc().printf("[/] to change bce neutral position\r\n");
tnhnrl 16:3363b9f14913 446 pc().printf("</> to change batt neutral position\r\n");
tnhnrl 28:16c83a2fdefa 447 pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",_pitchCommand);
tnhnrl 28:16c83a2fdefa 448 pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",_depthCommand);
tnhnrl 17:7c16b5671d0e 449 pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout);
tnhnrl 16:3363b9f14913 450 pc().printf(" 1 BCE PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 451 pc().printf(" 2 BATT PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 452 pc().printf(" 3 Depth PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 453 pc().printf(" 4 Pitch PID sub-menu\r\n");
tnhnrl 28:16c83a2fdefa 454 pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n");
tnhnrl 16:3363b9f14913 455 pc().printf(" ? to reset mbed\r\n");
tnhnrl 16:3363b9f14913 456 }
tnhnrl 20:8987a9ae2bc7 457
tnhnrl 17:7c16b5671d0e 458 //Find Neutral sub finite state machine
tnhnrl 17:7c16b5671d0e 459 // Note: the sub-fsm only moves the pistons once at the start of each timer loop
tnhnrl 17:7c16b5671d0e 460 // (timer completes, move piston, timer completes, move piston, etc)
tnhnrl 28:16c83a2fdefa 461 int StateMachine::runNeutralStateMachine() {
tnhnrl 24:c7d9b5bf3829 462 switch (_substate) {
tnhnrl 20:8987a9ae2bc7 463 case NEUTRAL_SINKING :
tnhnrl 17:7c16b5671d0e 464 //start the 10 second timer
tnhnrl 28:16c83a2fdefa 465 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 466 _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 467
tnhnrl 28:16c83a2fdefa 468 pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f)\n\r", _neutral_timer, timer.read(), pitchLoop().getPosition());
tnhnrl 20:8987a9ae2bc7 469
tnhnrl 20:8987a9ae2bc7 470 // what are the commands?
tnhnrl 28:16c83a2fdefa 471 //move piston at start of sequence (retract 2.5 mm)
tnhnrl 28:16c83a2fdefa 472 bce().setPosition_mm(bce().getSetPosition_mm() - 2.5); //no depth command
tnhnrl 23:434f04ef1fad 473
tnhnrl 23:434f04ef1fad 474 // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state
tnhnrl 23:434f04ef1fad 475 pitchLoop().setCommand(0.0);
tnhnrl 24:c7d9b5bf3829 476
tnhnrl 28:16c83a2fdefa 477 pc().printf("NEUTRAL_SINKING: Retracting piston 5 mm [BCE CMD : %0.1f] [pitch cmd: %0.1f] (pitch: %0.1f)\n\r", bce().getSetPosition_mm(), pitchLoop().getCommand(), pitchLoop().getPosition());
tnhnrl 17:7c16b5671d0e 478
tnhnrl 28:16c83a2fdefa 479 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 480 }
tnhnrl 20:8987a9ae2bc7 481
tnhnrl 20:8987a9ae2bc7 482 // how exit?
tnhnrl 20:8987a9ae2bc7 483 //once reached the travel limit, no need to keep trying, so exit
tnhnrl 25:249e4d56b27c 484 if (bce().getPosition_mm() <= 0) {
tnhnrl 25:249e4d56b27c 485 pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm());
tnhnrl 25:249e4d56b27c 486 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 487 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 25:249e4d56b27c 488 }
tnhnrl 20:8987a9ae2bc7 489 //once deeper than the commanded setpoint...
tnhnrl 28:16c83a2fdefa 490 else if (depthLoop().getPosition() > _depthCommand) {
tnhnrl 24:c7d9b5bf3829 491 _substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 28:16c83a2fdefa 492 _isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 20:8987a9ae2bc7 493 }
tnhnrl 20:8987a9ae2bc7 494
tnhnrl 20:8987a9ae2bc7 495 // what is active?
tnhnrl 20:8987a9ae2bc7 496 //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 497 if (timer.read() >= _neutral_timer) {
tnhnrl 24:c7d9b5bf3829 498 pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! Retracting BCE piston 5 mm [current time: %0.1f]\r\n", timer.read());
tnhnrl 17:7c16b5671d0e 499
tnhnrl 28:16c83a2fdefa 500 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 501 }
tnhnrl 21:38c8544db6f4 502
tnhnrl 23:434f04ef1fad 503 // what is active?
tnhnrl 23:434f04ef1fad 504 batt().setPosition_mm(pitchLoop().getOutput()); // pitch outer loop is running
tnhnrl 17:7c16b5671d0e 505 break;
tnhnrl 17:7c16b5671d0e 506
tnhnrl 17:7c16b5671d0e 507 case NEUTRAL_SLOWLY_RISE:
tnhnrl 28:16c83a2fdefa 508 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 509 _neutral_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 510
tnhnrl 24:c7d9b5bf3829 511 pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Next extension at %0.1f sec) [current time: %0.1f]\r\n",_neutral_timer,timer.read());
tnhnrl 17:7c16b5671d0e 512
tnhnrl 20:8987a9ae2bc7 513 // what are the commands?
tnhnrl 20:8987a9ae2bc7 514 //move piston at start of sequence (extend)
tnhnrl 24:c7d9b5bf3829 515 bce().setPosition_mm(bce().getSetPosition_mm() + 2); //no depth command
tnhnrl 23:434f04ef1fad 516
tnhnrl 23:434f04ef1fad 517 // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state
tnhnrl 23:434f04ef1fad 518 pitchLoop().setCommand(0.0);
tnhnrl 24:c7d9b5bf3829 519
tnhnrl 24:c7d9b5bf3829 520 pc().printf("NEUTRAL_SLOWLY_RISE: Extending BCE piston 5 mm [BCE CMD : %0.1f] [pitch cmd: %0.1f]\n\r", bce().getSetPosition_mm(), pitchLoop().getCommand());
tnhnrl 24:c7d9b5bf3829 521
tnhnrl 28:16c83a2fdefa 522 _isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 523 }
tnhnrl 17:7c16b5671d0e 524
tnhnrl 20:8987a9ae2bc7 525 // how exit?
tnhnrl 24:c7d9b5bf3829 526 //once at full travel limit (setPosition) and haven't yet risen, time to give up and exit
tnhnrl 24:c7d9b5bf3829 527 if (bce().getSetPosition_mm() >= bce().getTravelLimit()) {
tnhnrl 24:c7d9b5bf3829 528 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 529 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 530 }
tnhnrl 17:7c16b5671d0e 531 //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration
tnhnrl 20:8987a9ae2bc7 532 else if (depthLoop().getVelocity() < 0) { //less than zero ft/s
tnhnrl 24:c7d9b5bf3829 533 pc().printf("\r\n\nNEUTRAL_SLOWLY_RISE: Sink Rate < 0 ft/s [time: %0.1f]\r\n", timer.read());
tnhnrl 24:c7d9b5bf3829 534 _substate = NEUTRAL_CHECK_PITCH;
tnhnrl 28:16c83a2fdefa 535 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 536 }
tnhnrl 17:7c16b5671d0e 537
tnhnrl 20:8987a9ae2bc7 538 // what is active?
tnhnrl 20:8987a9ae2bc7 539 //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 540 if (timer.read() >= _neutral_timer) {
tnhnrl 24:c7d9b5bf3829 541 pc().printf("\r\n\n NEUTRAL_SLOWLY_RISE TIMER COMPLETE! Extending 1 mm [timer: %0.1f]\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 542
tnhnrl 28:16c83a2fdefa 543 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 17:7c16b5671d0e 544 }
tnhnrl 23:434f04ef1fad 545
tnhnrl 23:434f04ef1fad 546 // what is active?
tnhnrl 23:434f04ef1fad 547 batt().setPosition_mm(pitchLoop().getOutput()); // pitch outer loop is running
tnhnrl 17:7c16b5671d0e 548 break;
tnhnrl 17:7c16b5671d0e 549
danstrider 22:a10ee088403b 550 case NEUTRAL_CHECK_PITCH : // fall thru to next state is desired
danstrider 22:a10ee088403b 551 case NEUTRAL_FIRST_PITCH :
danstrider 22:a10ee088403b 552 // start local state timer and init any other one-shot actions
tnhnrl 23:434f04ef1fad 553
tnhnrl 28:16c83a2fdefa 554 if (!_isSubStateTimerRunning) {
tnhnrl 24:c7d9b5bf3829 555 _neutral_timer = timer.read() + 10; // record time when this block is entered and add several seconds
tnhnrl 24:c7d9b5bf3829 556 pc().printf("\r\nNEUTRAL_CHECK_PITCH: Next move in %0.1f sec \r\n",_neutral_timer - timer.read());
danstrider 22:a10ee088403b 557
danstrider 22:a10ee088403b 558 // what are the commands?
tnhnrl 24:c7d9b5bf3829 559 if (pitchLoop().getPosition() > 2) { // nose is high
tnhnrl 24:c7d9b5bf3829 560 batt().setPosition_mm(batt().getSetPosition_mm() + 0.5); // move battery forward (using setpoint from linear actuator)
tnhnrl 23:434f04ef1fad 561 pc().printf("\n\rNeutral Check Pitch: moving battery FWD in 1mm increments\n\n\r");
danstrider 22:a10ee088403b 562 }
tnhnrl 24:c7d9b5bf3829 563 else if (pitchLoop().getPosition() < -2) { // nose is low
tnhnrl 24:c7d9b5bf3829 564 batt().setPosition_mm(batt().getSetPosition_mm() - 0.5); // move battery aft (using setpoint from linear actuator)
tnhnrl 23:434f04ef1fad 565 pc().printf("\n\rNeutral Check Pitch: moving battery AFT in 1mm increments\n\n\r");
danstrider 22:a10ee088403b 566 }
tnhnrl 24:c7d9b5bf3829 567
tnhnrl 28:16c83a2fdefa 568 _isSubStateTimerRunning = true; //disable this block after one iteration
danstrider 22:a10ee088403b 569 }
tnhnrl 20:8987a9ae2bc7 570
tnhnrl 28:16c83a2fdefa 571 // how exit?
tnhnrl 20:8987a9ae2bc7 572 //pitch angle and pitch rate within small tolerance
tnhnrl 20:8987a9ae2bc7 573 //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 23:434f04ef1fad 574 if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) {
tnhnrl 24:c7d9b5bf3829 575 pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug
danstrider 22:a10ee088403b 576 // found level, but don't need to save anything this time
tnhnrl 23:434f04ef1fad 577
tnhnrl 28:16c83a2fdefa 578 if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug
tnhnrl 28:16c83a2fdefa 579 _max_recorded_depth_neutral = depthLoop().getPosition(); //new max depth recorded
tnhnrl 28:16c83a2fdefa 580 }
tnhnrl 28:16c83a2fdefa 581
tnhnrl 24:c7d9b5bf3829 582 if (_substate == NEUTRAL_FIRST_PITCH) {
tnhnrl 24:c7d9b5bf3829 583 _substate = NEUTRAL_SINKING; // next state starts the sinking
tnhnrl 28:16c83a2fdefa 584 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 24:c7d9b5bf3829 585
tnhnrl 23:434f04ef1fad 586 // save this neutral (not in file) for pitch
tnhnrl 23:434f04ef1fad 587 pitchLoop().setOutputOffset(batt().getPosition_mm());
tnhnrl 23:434f04ef1fad 588
tnhnrl 24:c7d9b5bf3829 589 pc().printf("substate: NEUTRAL_FIRST_PITCH (next substate: NEUTRAL_SINKING)\n\r");
danstrider 22:a10ee088403b 590 }
tnhnrl 23:434f04ef1fad 591
tnhnrl 23:434f04ef1fad 592 // found level and at depth too, so save it all now
tnhnrl 24:c7d9b5bf3829 593 else if (_substate == NEUTRAL_CHECK_PITCH) {
danstrider 22:a10ee088403b 594 //save positions locally
danstrider 22:a10ee088403b 595 _neutral_batt_pos_mm = batt().getPosition_mm();
danstrider 22:a10ee088403b 596 _neutral_bce_pos_mm = bce().getPosition_mm();
danstrider 22:a10ee088403b 597
danstrider 22:a10ee088403b 598 //set the neutral positions in each outer loop
danstrider 22:a10ee088403b 599 depthLoop().setOutputOffset(_neutral_bce_pos_mm);
danstrider 22:a10ee088403b 600 pitchLoop().setOutputOffset(_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 601
danstrider 22:a10ee088403b 602 // save into the depth.txt and pitch.txt files
danstrider 22:a10ee088403b 603 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset
danstrider 22:a10ee088403b 604 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset
danstrider 22:a10ee088403b 605
danstrider 22:a10ee088403b 606 pc().printf("\n\rSaving Positions: BCE: %0.1f mm, BATT: %0.1f\n\n\r",_neutral_bce_pos_mm,_neutral_batt_pos_mm);
danstrider 22:a10ee088403b 607
tnhnrl 24:c7d9b5bf3829 608 _substate = NEUTRAL_EXIT;
tnhnrl 28:16c83a2fdefa 609 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
tnhnrl 24:c7d9b5bf3829 610 }
tnhnrl 24:c7d9b5bf3829 611
tnhnrl 24:c7d9b5bf3829 612 else {
tnhnrl 24:c7d9b5bf3829 613 pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r");
tnhnrl 24:c7d9b5bf3829 614 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 615 }
tnhnrl 17:7c16b5671d0e 616 }
danstrider 22:a10ee088403b 617
danstrider 22:a10ee088403b 618 // what is active?
danstrider 22:a10ee088403b 619 //once timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 24:c7d9b5bf3829 620 if (timer.read() >= _neutral_timer) {
danstrider 22:a10ee088403b 621 pc().printf("\r\n\nlevel timer COMPLETE!");
danstrider 22:a10ee088403b 622 pc().printf("\r\n\n (BATT POS: %0.1f) moving 1 mm [timer: %0.1f]\r\n", batt().getPosition_mm(), timer.read());
tnhnrl 28:16c83a2fdefa 623 _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again
danstrider 22:a10ee088403b 624 }
tnhnrl 17:7c16b5671d0e 625 break;
danstrider 22:a10ee088403b 626
danstrider 22:a10ee088403b 627 //this state could be removed, it is only used as a transition but is needed to stop entering this function
danstrider 22:a10ee088403b 628 case NEUTRAL_EXIT :
tnhnrl 23:434f04ef1fad 629 pc().printf("substate: NEUTRAL_EXIT\n\r");
tnhnrl 20:8987a9ae2bc7 630 break;
tnhnrl 21:38c8544db6f4 631
danstrider 22:a10ee088403b 632 default :
tnhnrl 24:c7d9b5bf3829 633 pc().printf("how did we get to substate: default?\n\r"); //debug
tnhnrl 23:434f04ef1fad 634 //a default within the sub-state machine
tnhnrl 24:c7d9b5bf3829 635 _substate = NEUTRAL_EXIT;
danstrider 22:a10ee088403b 636 break;
tnhnrl 17:7c16b5671d0e 637 }
tnhnrl 20:8987a9ae2bc7 638
tnhnrl 30:2964617e7676 639 // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence)
tnhnrl 24:c7d9b5bf3829 640 if (_substate == NEUTRAL_EXIT) {
tnhnrl 24:c7d9b5bf3829 641 pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r");
tnhnrl 24:c7d9b5bf3829 642
tnhnrl 30:2964617e7676 643 //reset internal sub-state back to first entry conditions (first state is immediately sinking)
tnhnrl 30:2964617e7676 644 _substate = NEUTRAL_SINKING;
tnhnrl 28:16c83a2fdefa 645 _isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 21:38c8544db6f4 646
tnhnrl 24:c7d9b5bf3829 647 //record sub-states to view after sequence
tnhnrl 30:2964617e7676 648 _substate_array[_substate_array_counter] = NEUTRAL_EXIT; //save exit to state array
tnhnrl 28:16c83a2fdefa 649 _substate_array_counter++;
tnhnrl 23:434f04ef1fad 650
tnhnrl 24:c7d9b5bf3829 651 //reset _previous_substate on exit (has to be done in FIND_NEUTRAL if emergency exit)
tnhnrl 24:c7d9b5bf3829 652 _previous_substate = -1;
tnhnrl 24:c7d9b5bf3829 653
tnhnrl 24:c7d9b5bf3829 654 //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 24:c7d9b5bf3829 655 return NEUTRAL_EXIT; // message to calling function we just exited
tnhnrl 21:38c8544db6f4 656 }
tnhnrl 23:434f04ef1fad 657 else {
tnhnrl 24:c7d9b5bf3829 658 //record sub-states to view after sequence (when changed)
tnhnrl 24:c7d9b5bf3829 659 if (_previous_substate != _substate) {
tnhnrl 28:16c83a2fdefa 660 _substate_array[_substate_array_counter] = _substate; //save current state to state array
tnhnrl 28:16c83a2fdefa 661 _substate_array_counter++;
tnhnrl 24:c7d9b5bf3829 662
tnhnrl 24:c7d9b5bf3829 663 //record the current substate for comparison
tnhnrl 24:c7d9b5bf3829 664 _previous_substate = _substate;
tnhnrl 24:c7d9b5bf3829 665 }
tnhnrl 24:c7d9b5bf3829 666
tnhnrl 24:c7d9b5bf3829 667 return _substate; // message to calling function of what sub-state it's in
tnhnrl 23:434f04ef1fad 668 }
tnhnrl 17:7c16b5671d0e 669 }
tnhnrl 20:8987a9ae2bc7 670
tnhnrl 20:8987a9ae2bc7 671 // keyboard runs independently of the state machine, handling one key at a time
tnhnrl 20:8987a9ae2bc7 672 //keyboard updates the desired _keyboard_state that is used in the state machine
tnhnrl 20:8987a9ae2bc7 673 //and only allows input when the state is "idle"
tnhnrl 17:7c16b5671d0e 674 void StateMachine::keyboard() {
tnhnrl 16:3363b9f14913 675 char userInput;
tnhnrl 20:8987a9ae2bc7 676
tnhnrl 16:3363b9f14913 677 // check keyboard and make settings changes as requested
tnhnrl 17:7c16b5671d0e 678 // states can be changed only at the start of a sequence (when the system is in SIT_IDLE)
tnhnrl 21:38c8544db6f4 679
tnhnrl 21:38c8544db6f4 680 int _keyboard_state = -1; //made this a local variable because it was retaining the last keyboard state
tnhnrl 21:38c8544db6f4 681
tnhnrl 28:16c83a2fdefa 682 if (pc().readable() && (_state == SIT_IDLE || _state == KEYBOARD)) {
tnhnrl 16:3363b9f14913 683 // get the key
tnhnrl 17:7c16b5671d0e 684 userInput = pc().getc();
tnhnrl 17:7c16b5671d0e 685
tnhnrl 28:16c83a2fdefa 686 //record that the keyboard was used
tnhnrl 28:16c83a2fdefa 687 _state_array[_state_array_counter] = KEYBOARD;
tnhnrl 28:16c83a2fdefa 688 _state_array_counter++;
tnhnrl 28:16c83a2fdefa 689
tnhnrl 21:38c8544db6f4 690 // keyboard has to reset timer each time it's used
tnhnrl 28:16c83a2fdefa 691 _isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 692
tnhnrl 16:3363b9f14913 693 // check command against desired control buttons
tnhnrl 16:3363b9f14913 694 if (userInput == 'D' or userInput == 'd') {
tnhnrl 17:7c16b5671d0e 695 _keyboard_state = DIVE;
tnhnrl 16:3363b9f14913 696 }
tnhnrl 16:3363b9f14913 697 else if (userInput == 'N' or userInput == 'n') {
tnhnrl 17:7c16b5671d0e 698 _keyboard_state = FIND_NEUTRAL;
tnhnrl 17:7c16b5671d0e 699 }
tnhnrl 17:7c16b5671d0e 700 else if (userInput == 'M' or userInput == 'm') {
tnhnrl 17:7c16b5671d0e 701 //currently does not run if there is no file.
tnhnrl 17:7c16b5671d0e 702
tnhnrl 17:7c16b5671d0e 703 //need to add method to Sequence Controller that returns -1
tnhnrl 17:7c16b5671d0e 704 // or some check that insures you cannot run the dive sequence without a file
tnhnrl 17:7c16b5671d0e 705
tnhnrl 17:7c16b5671d0e 706 stateMachine().getDiveSequence(); //get first sequence on keyboard press
tnhnrl 17:7c16b5671d0e 707 _keyboard_state = currentStateStruct.state;
tnhnrl 17:7c16b5671d0e 708
tnhnrl 17:7c16b5671d0e 709 pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles
tnhnrl 16:3363b9f14913 710 }
tnhnrl 16:3363b9f14913 711 else if (userInput == 'R' or userInput == 'r') {
tnhnrl 17:7c16b5671d0e 712 _keyboard_state = RISE;
tnhnrl 16:3363b9f14913 713 }
tnhnrl 16:3363b9f14913 714 else if (userInput == 'L' or userInput == 'l') {
tnhnrl 17:7c16b5671d0e 715 _keyboard_state = FLOAT_LEVEL;
tnhnrl 16:3363b9f14913 716 }
tnhnrl 16:3363b9f14913 717 else if (userInput == 'B' or userInput == 'b') {
tnhnrl 17:7c16b5671d0e 718 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 719 }
tnhnrl 16:3363b9f14913 720 else if (userInput == 'E' or userInput == 'e') {
tnhnrl 17:7c16b5671d0e 721 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 722 }
tnhnrl 28:16c83a2fdefa 723 // else if (userInput == 'H' or userInput == 'h') {
tnhnrl 28:16c83a2fdefa 724 // pc().printf("running homing procedure\r\n");
tnhnrl 28:16c83a2fdefa 725 // bce().unpause(); bce().homePiston(); bce().pause();
tnhnrl 28:16c83a2fdefa 726 // batt().unpause(); batt().homePiston(); batt().pause();
tnhnrl 28:16c83a2fdefa 727 // }
tnhnrl 28:16c83a2fdefa 728 else if (userInput == 'z' or userInput == 'Z') {
tnhnrl 28:16c83a2fdefa 729 pc().printf("FSG FSM States: \n\r");
tnhnrl 28:16c83a2fdefa 730 string string_state;
tnhnrl 28:16c83a2fdefa 731
tnhnrl 28:16c83a2fdefa 732 for (int i = 0; i < _state_array_counter; i++) {
tnhnrl 28:16c83a2fdefa 733 if (_state_array[i] == SIT_IDLE)
tnhnrl 28:16c83a2fdefa 734 string_state = "SIT_IDLE <END>";
tnhnrl 28:16c83a2fdefa 735 else if (_state_array[i] == FIND_NEUTRAL)
tnhnrl 28:16c83a2fdefa 736 string_state = "FIND_NEUTRAL";
tnhnrl 28:16c83a2fdefa 737 else if (_state_array[i] == DIVE)
tnhnrl 28:16c83a2fdefa 738 string_state = "DIVE";
tnhnrl 28:16c83a2fdefa 739 else if (_state_array[i] == RISE)
tnhnrl 28:16c83a2fdefa 740 string_state = "RISE";
tnhnrl 28:16c83a2fdefa 741 else if (_state_array[i] == FLOAT_LEVEL)
tnhnrl 28:16c83a2fdefa 742 string_state = "FLOAT_LEVEL";
tnhnrl 28:16c83a2fdefa 743 else if (_state_array[i] == FLOAT_BROADCAST)
tnhnrl 28:16c83a2fdefa 744 string_state = "FLOAT_BROADCAST";
tnhnrl 28:16c83a2fdefa 745 else if (_state_array[i] == EMERGENCY_CLIMB)
tnhnrl 28:16c83a2fdefa 746 string_state = "EMERGENCY_CLIMB";
tnhnrl 28:16c83a2fdefa 747 else if (_state_array[i] == MULTI_DIVE)
tnhnrl 28:16c83a2fdefa 748 string_state = "MULTI_DIVE";
tnhnrl 28:16c83a2fdefa 749 else if (_state_array[i] == MULTI_RISE)
tnhnrl 28:16c83a2fdefa 750 string_state = "MULTI_RISE";
tnhnrl 28:16c83a2fdefa 751 else if (_state_array[i] == KEYBOARD)
tnhnrl 28:16c83a2fdefa 752 string_state = "KEYBOARD";
tnhnrl 28:16c83a2fdefa 753 pc().printf("State #%d: %d (%s)\n\r", i, _state_array[i], string_state.c_str());
tnhnrl 28:16c83a2fdefa 754 }
tnhnrl 28:16c83a2fdefa 755
tnhnrl 28:16c83a2fdefa 756 pc().printf("\n\rNeutral sub-FSM States: \n\r");
tnhnrl 28:16c83a2fdefa 757 string string_substate;
tnhnrl 28:16c83a2fdefa 758
tnhnrl 28:16c83a2fdefa 759 for (int i = 0; i < _substate_array_counter; i++) {
tnhnrl 28:16c83a2fdefa 760 if (_substate_array[i] == NEUTRAL_FIRST_PITCH)
tnhnrl 28:16c83a2fdefa 761 string_substate = "NEUTRAL_FIRST_PITCH";
tnhnrl 28:16c83a2fdefa 762 else if (_substate_array[i] == NEUTRAL_SINKING)
tnhnrl 28:16c83a2fdefa 763 string_substate = "NEUTRAL_SINKING";
tnhnrl 28:16c83a2fdefa 764 else if (_substate_array[i] == NEUTRAL_SLOWLY_RISE)
tnhnrl 28:16c83a2fdefa 765 string_substate = "NEUTRAL_SLOWLY_RISE";
tnhnrl 28:16c83a2fdefa 766 else if (_substate_array[i] == NEUTRAL_CHECK_PITCH)
tnhnrl 28:16c83a2fdefa 767 string_substate = "NEUTRAL_CHECK_PITCH";
tnhnrl 28:16c83a2fdefa 768 else if (_substate_array[i] == NEUTRAL_EXIT)
tnhnrl 28:16c83a2fdefa 769 string_substate = "NEUTRAL_EXIT <-- ";
tnhnrl 28:16c83a2fdefa 770 else if (_substate_array[i] == EMERGENCY_CLIMB)
tnhnrl 28:16c83a2fdefa 771 string_substate = " -- > EMERGENCY_CLIMB <-- ";
tnhnrl 28:16c83a2fdefa 772 pc().printf("Neutral Substate #%d: %d (%s)\n\r", i, _state_array[i], string_substate.c_str());
tnhnrl 28:16c83a2fdefa 773 }
tnhnrl 28:16c83a2fdefa 774 pc().printf("\n\r"); //make space between printouts
tnhnrl 16:3363b9f14913 775 }
tnhnrl 16:3363b9f14913 776 else if (userInput == 'T' or userInput == 't') {
tnhnrl 16:3363b9f14913 777 pc().printf("taring depth sensor\r\n");
tnhnrl 16:3363b9f14913 778 pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 779 wait(0.1);
tnhnrl 16:3363b9f14913 780 depth().tare(); // tares to ambient (do on surface)
tnhnrl 16:3363b9f14913 781 pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 782 }
tnhnrl 16:3363b9f14913 783
tnhnrl 16:3363b9f14913 784 else if (userInput == '[' or userInput == '{') {
tnhnrl 16:3363b9f14913 785 depthLoop().setOutputOffset(depthLoop().getOutputOffset() - 1); // decrease the bce neutral setpoint
tnhnrl 16:3363b9f14913 786 pc().printf("Adjusting bce neutral position. new: %3.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 787 }
tnhnrl 16:3363b9f14913 788 else if (userInput == ']' or userInput == '}') {
tnhnrl 16:3363b9f14913 789 depthLoop().setOutputOffset(depthLoop().getOutputOffset() + 1); // increase the bce neutral setpoint
tnhnrl 16:3363b9f14913 790 pc().printf("Adjusting bce neutral position. new: %3.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 791 }
tnhnrl 16:3363b9f14913 792 else if (userInput == '<' or userInput == ',') {
tnhnrl 16:3363b9f14913 793 pitchLoop().setOutputOffset(pitchLoop().getOutputOffset() - 1); // decrease the batt neutral setpoint
tnhnrl 16:3363b9f14913 794 pc().printf("Adjusting batt neutral position. new: %3.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 795 }
tnhnrl 16:3363b9f14913 796 else if (userInput == '>' or userInput == '.') {
tnhnrl 16:3363b9f14913 797 pitchLoop().setOutputOffset(pitchLoop().getOutputOffset() + 1); // increase the batt neutral setpoint
tnhnrl 16:3363b9f14913 798 pc().printf("Adjusting batt neutral position. new: %3.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 799 }
tnhnrl 16:3363b9f14913 800
tnhnrl 16:3363b9f14913 801 else if (userInput == '?') {
tnhnrl 16:3363b9f14913 802 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 16:3363b9f14913 803 wait(0.5);
tnhnrl 16:3363b9f14913 804 mbed_reset();
tnhnrl 16:3363b9f14913 805 }
tnhnrl 20:8987a9ae2bc7 806
tnhnrl 16:3363b9f14913 807 // change settings
tnhnrl 16:3363b9f14913 808 else if (userInput == 'Q' or userInput == 'q') {
tnhnrl 28:16c83a2fdefa 809 _pitchCommand -= 0.5; //decrement the pitch setpoint
tnhnrl 28:16c83a2fdefa 810 pitchLoop().setCommand(_pitchCommand);
tnhnrl 16:3363b9f14913 811 pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 812 }
tnhnrl 16:3363b9f14913 813 else if (userInput == 'W' or userInput == 'w') {
tnhnrl 28:16c83a2fdefa 814 _pitchCommand += 0.5; //increment the pitch setpoint
tnhnrl 28:16c83a2fdefa 815 pitchLoop().setCommand(_pitchCommand);
tnhnrl 16:3363b9f14913 816 pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 817 }
tnhnrl 16:3363b9f14913 818 else if (userInput == 'A' or userInput == 'a') {
tnhnrl 28:16c83a2fdefa 819 _depthCommand -= 0.5; //decrement the depth setpoint
tnhnrl 28:16c83a2fdefa 820 depthLoop().setCommand(_depthCommand);
tnhnrl 16:3363b9f14913 821 pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 822 }
tnhnrl 16:3363b9f14913 823 else if (userInput == 'S' or userInput == 's') {
tnhnrl 28:16c83a2fdefa 824 _depthCommand += 0.5; //increment the depth setpoint
tnhnrl 28:16c83a2fdefa 825 depthLoop().setCommand(_depthCommand);
tnhnrl 16:3363b9f14913 826 pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 827 }
tnhnrl 16:3363b9f14913 828 else if (userInput == '-') {
tnhnrl 17:7c16b5671d0e 829 _timeout -= 10.0; //decrement the timeout
tnhnrl 17:7c16b5671d0e 830 pc().printf(">>> timeout decreased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 831 }
tnhnrl 16:3363b9f14913 832 else if (userInput == '=' or userInput == '+') {
tnhnrl 17:7c16b5671d0e 833 _timeout += 10.0; //increment the timeout
tnhnrl 17:7c16b5671d0e 834 pc().printf(">>> timeout increased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 835 }
tnhnrl 16:3363b9f14913 836
tnhnrl 16:3363b9f14913 837 // go to sub-menus for the PID gains (this is blocking)
tnhnrl 16:3363b9f14913 838 else if (userInput == '1') {
tnhnrl 16:3363b9f14913 839 keyboard_menu_BCE_PID_settings();
tnhnrl 16:3363b9f14913 840 }
tnhnrl 16:3363b9f14913 841 else if (userInput == '2') {
tnhnrl 16:3363b9f14913 842 keyboard_menu_BATT_PID_settings();
tnhnrl 16:3363b9f14913 843 }
tnhnrl 16:3363b9f14913 844 else if (userInput == '3') {
tnhnrl 16:3363b9f14913 845 keyboard_menu_DEPTH_PID_settings();
tnhnrl 16:3363b9f14913 846 }
tnhnrl 16:3363b9f14913 847 else if (userInput == '4') {
tnhnrl 16:3363b9f14913 848 keyboard_menu_PITCH_PID_settings();
tnhnrl 16:3363b9f14913 849 }
tnhnrl 16:3363b9f14913 850
tnhnrl 16:3363b9f14913 851 else if (userInput == 'C' or userInput == 'c') {
tnhnrl 16:3363b9f14913 852 pc().printf("depth: %3.1f\r\n",depthLoop().getPosition());
tnhnrl 16:3363b9f14913 853 pc().printf("pitch: %3.1f\r\n",imu().getPitch());
tnhnrl 16:3363b9f14913 854 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 16:3363b9f14913 855 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 16:3363b9f14913 856 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 16:3363b9f14913 857 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 16:3363b9f14913 858 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 16:3363b9f14913 859 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 28:16c83a2fdefa 860 pc().printf("Latest Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\r\n",_neutral_batt_pos_mm,_neutral_bce_pos_mm);
tnhnrl 28:16c83a2fdefa 861 pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 28:16c83a2fdefa 862 pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 28:16c83a2fdefa 863 pc().printf("Max recorded depth: neutral: %0.1f, dive: %0.1f\n\n\r",_max_recorded_depth_neutral, _max_recorded_depth_dive);
tnhnrl 16:3363b9f14913 864 }
tnhnrl 17:7c16b5671d0e 865
tnhnrl 17:7c16b5671d0e 866 //when you read the keyboard successfully, change the state
tnhnrl 28:16c83a2fdefa 867 _state = _keyboard_state; //set state at the end of this function
tnhnrl 16:3363b9f14913 868 }
tnhnrl 16:3363b9f14913 869 }
tnhnrl 20:8987a9ae2bc7 870
tnhnrl 20:8987a9ae2bc7 871
tnhnrl 16:3363b9f14913 872 void StateMachine::keyboard_menu_BCE_PID_settings() {
tnhnrl 16:3363b9f14913 873 char PID_key;
tnhnrl 16:3363b9f14913 874 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 875 float KP = bce().getControllerP(); // load current value
tnhnrl 16:3363b9f14913 876 float KI = bce().getControllerI(); // load current global value
tnhnrl 16:3363b9f14913 877 float KD = bce().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 878
tnhnrl 16:3363b9f14913 879 // show the menu
tnhnrl 16:3363b9f14913 880 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 881 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 882 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 21:38c8544db6f4 883 pc().printf("bce P: %3.2f, I: %3.2f, D %3.2f, zero %d, limit %3.0f mm, slope %3.3f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
tnhnrl 16:3363b9f14913 884
tnhnrl 16:3363b9f14913 885 // handle the key presses
tnhnrl 16:3363b9f14913 886 while(1) {
tnhnrl 16:3363b9f14913 887 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 888 if (pc().readable()) {
tnhnrl 16:3363b9f14913 889 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 890 }
tnhnrl 16:3363b9f14913 891 else {
tnhnrl 16:3363b9f14913 892 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 893 }
tnhnrl 16:3363b9f14913 894
tnhnrl 16:3363b9f14913 895 // handle the user's key input
tnhnrl 16:3363b9f14913 896 if (PID_key == '-') {
tnhnrl 16:3363b9f14913 897 KP -= gain_step_size;
tnhnrl 16:3363b9f14913 898 pc().printf("P gain: %0.5f \r\n", KP);
tnhnrl 16:3363b9f14913 899 }
tnhnrl 16:3363b9f14913 900 else if (PID_key == '=') {
tnhnrl 16:3363b9f14913 901 KP += gain_step_size;
tnhnrl 16:3363b9f14913 902 pc().printf("P gain: %0.5f \r\n", KP);
tnhnrl 16:3363b9f14913 903 }
tnhnrl 16:3363b9f14913 904 else if (PID_key == '[') {
tnhnrl 16:3363b9f14913 905 KI -= gain_step_size;
tnhnrl 16:3363b9f14913 906 pc().printf("I gain: %0.5f \r\n", KI);
tnhnrl 16:3363b9f14913 907 }
tnhnrl 16:3363b9f14913 908 else if (PID_key == ']') {
tnhnrl 16:3363b9f14913 909 KI += gain_step_size;
tnhnrl 16:3363b9f14913 910 pc().printf("I gain: %0.5f \r\n", KI);
tnhnrl 16:3363b9f14913 911 }
tnhnrl 16:3363b9f14913 912 else if (PID_key == ';') {
tnhnrl 16:3363b9f14913 913 KD -= gain_step_size;
tnhnrl 16:3363b9f14913 914 pc().printf("D gain: %0.5f \r\n", KD);
tnhnrl 16:3363b9f14913 915 }
tnhnrl 16:3363b9f14913 916 else if (PID_key == '\'') {
tnhnrl 16:3363b9f14913 917 KD += gain_step_size;
tnhnrl 16:3363b9f14913 918 pc().printf("D gain: %0.5f \r\n", KD);
tnhnrl 16:3363b9f14913 919 }
tnhnrl 16:3363b9f14913 920 else if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 16:3363b9f14913 921 // set values
tnhnrl 16:3363b9f14913 922 bce().setControllerP(KP);
tnhnrl 16:3363b9f14913 923 bce().setControllerI(KI);
tnhnrl 16:3363b9f14913 924 bce().setControllerD(KD);
tnhnrl 16:3363b9f14913 925
tnhnrl 16:3363b9f14913 926 // save into "PID.cfg"
tnhnrl 16:3363b9f14913 927 //Config_File_IO().write_manual_position_PID_values_to_config(batt_position_P,batt_position_I,batt_position_D,bce_position_P,bce_position_I,bce_position_D);
tnhnrl 16:3363b9f14913 928 break; //exit the while loop
tnhnrl 16:3363b9f14913 929 }
tnhnrl 16:3363b9f14913 930 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 931 break; //exit the while loop
tnhnrl 16:3363b9f14913 932 }
tnhnrl 16:3363b9f14913 933 else {
tnhnrl 16:3363b9f14913 934 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 935 }
tnhnrl 16:3363b9f14913 936 }
tnhnrl 16:3363b9f14913 937 }
tnhnrl 20:8987a9ae2bc7 938
tnhnrl 16:3363b9f14913 939 void StateMachine::keyboard_menu_BATT_PID_settings() {
tnhnrl 16:3363b9f14913 940 char PID_key;
tnhnrl 16:3363b9f14913 941 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 942 float KP = batt().getControllerP(); // load current global value
tnhnrl 16:3363b9f14913 943 float KI = batt().getControllerI(); // load current global value
tnhnrl 16:3363b9f14913 944 float KD = batt().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 945
tnhnrl 16:3363b9f14913 946 // print the menu
tnhnrl 16:3363b9f14913 947 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 948 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 949 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 21:38c8544db6f4 950 pc().printf("batt P: %3.2f, I: %3.2f, D %3.2f, zero %d, limit %3.0f mm, slope %3.3f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
tnhnrl 20:8987a9ae2bc7 951
tnhnrl 16:3363b9f14913 952 // handle the key presses
tnhnrl 16:3363b9f14913 953 while(1) {
tnhnrl 16:3363b9f14913 954 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 955 if (pc().readable()) {
tnhnrl 16:3363b9f14913 956 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 957 }
tnhnrl 16:3363b9f14913 958 else {
tnhnrl 16:3363b9f14913 959 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 960 }
tnhnrl 16:3363b9f14913 961
tnhnrl 16:3363b9f14913 962 // handle the user's key input
tnhnrl 16:3363b9f14913 963 if (PID_key == '-') {
tnhnrl 16:3363b9f14913 964 KP -= gain_step_size;
tnhnrl 16:3363b9f14913 965 pc().printf("\rP gain: %0.5f ", KP);
tnhnrl 16:3363b9f14913 966 }
tnhnrl 16:3363b9f14913 967 else if (PID_key == '=') {
tnhnrl 16:3363b9f14913 968 KP += gain_step_size;
tnhnrl 16:3363b9f14913 969 pc().printf("\rP gain: %0.5f ", KP);
tnhnrl 16:3363b9f14913 970 }
tnhnrl 16:3363b9f14913 971 else if (PID_key == '[') {
tnhnrl 16:3363b9f14913 972 KI -= gain_step_size;
tnhnrl 16:3363b9f14913 973 pc().printf("\rI gain: %0.5f ", KI);
tnhnrl 16:3363b9f14913 974 }
tnhnrl 16:3363b9f14913 975 else if (PID_key == ']') {
tnhnrl 16:3363b9f14913 976 KI += gain_step_size;
tnhnrl 16:3363b9f14913 977 pc().printf("\rI gain: %0.5f ", KI);
tnhnrl 16:3363b9f14913 978 }
tnhnrl 16:3363b9f14913 979 else if (PID_key == ';') {
tnhnrl 16:3363b9f14913 980 KD -= gain_step_size;
tnhnrl 16:3363b9f14913 981 pc().printf("\rD gain: %0.5f ", KD);
tnhnrl 16:3363b9f14913 982 }
tnhnrl 16:3363b9f14913 983 else if (PID_key == '\'') {
tnhnrl 16:3363b9f14913 984 KD += gain_step_size;
tnhnrl 16:3363b9f14913 985 pc().printf("\rD gain: %0.5f ", KD);
tnhnrl 16:3363b9f14913 986 }
tnhnrl 16:3363b9f14913 987 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 988 // set global values
tnhnrl 16:3363b9f14913 989 batt().setControllerP(KP);
tnhnrl 16:3363b9f14913 990 batt().setControllerI(KI);
tnhnrl 16:3363b9f14913 991 batt().setControllerD(KD);
tnhnrl 16:3363b9f14913 992
tnhnrl 16:3363b9f14913 993 // save to "PID.cfg" file
tnhnrl 16:3363b9f14913 994 //Config_File_IO().write_manual_position_PID_values_to_config(batt_position_P,batt_position_I,batt_position_D,bce_position_P,bce_position_I,bce_position_D);
tnhnrl 16:3363b9f14913 995 break; //exit the while loop
tnhnrl 16:3363b9f14913 996 }
tnhnrl 16:3363b9f14913 997 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 998 break; //exit the while loop
tnhnrl 16:3363b9f14913 999 }
tnhnrl 16:3363b9f14913 1000 else {
tnhnrl 16:3363b9f14913 1001 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1002 }
tnhnrl 16:3363b9f14913 1003 }
tnhnrl 16:3363b9f14913 1004 }
tnhnrl 20:8987a9ae2bc7 1005
tnhnrl 16:3363b9f14913 1006 void StateMachine::keyboard_menu_DEPTH_PID_settings() {
tnhnrl 16:3363b9f14913 1007 char PID_key;
tnhnrl 16:3363b9f14913 1008 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1009
tnhnrl 16:3363b9f14913 1010 // show the menu
tnhnrl 16:3363b9f14913 1011 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1012 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1013 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r");
tnhnrl 16:3363b9f14913 1014 pc().printf("depth P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 1015
tnhnrl 16:3363b9f14913 1016 // handle the key presses
tnhnrl 16:3363b9f14913 1017 while(1) {
tnhnrl 16:3363b9f14913 1018 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1019 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1020 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1021 }
tnhnrl 16:3363b9f14913 1022 else {
tnhnrl 16:3363b9f14913 1023 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1024 }
tnhnrl 16:3363b9f14913 1025
tnhnrl 16:3363b9f14913 1026 // handle the user's key input
tnhnrl 16:3363b9f14913 1027 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1028 _depth_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1029 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1030 }
tnhnrl 16:3363b9f14913 1031 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1032 _depth_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1033 pc().printf("P gain: %0.5f \r\n", _depth_KP);
tnhnrl 16:3363b9f14913 1034 }
tnhnrl 16:3363b9f14913 1035 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1036 _depth_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1037 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1038 }
tnhnrl 16:3363b9f14913 1039 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1040 _depth_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1041 pc().printf("I gain: %0.5f \r\n", _depth_KI);
tnhnrl 16:3363b9f14913 1042 }
tnhnrl 16:3363b9f14913 1043 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1044 _depth_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1045 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1046 }
tnhnrl 16:3363b9f14913 1047 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1048 _depth_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1049 pc().printf("D gain: %0.5f \r\n", _depth_KD);
tnhnrl 16:3363b9f14913 1050 }
tnhnrl 16:3363b9f14913 1051 else if (PID_key == 'S') { // user wants to save these settings
tnhnrl 16:3363b9f14913 1052 // set global values
tnhnrl 21:38c8544db6f4 1053 depthLoop().setControllerP(_depth_KP);
tnhnrl 21:38c8544db6f4 1054 depthLoop().setControllerI(_depth_KI);
tnhnrl 21:38c8544db6f4 1055 depthLoop().setControllerD(_depth_KD);
tnhnrl 16:3363b9f14913 1056
tnhnrl 21:38c8544db6f4 1057 // save depth PID values for outer loop
tnhnrl 21:38c8544db6f4 1058 configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm);
tnhnrl 16:3363b9f14913 1059 break; //exit the while loop
tnhnrl 16:3363b9f14913 1060 }
tnhnrl 16:3363b9f14913 1061 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1062 break; //exit the while loop
tnhnrl 16:3363b9f14913 1063 }
tnhnrl 16:3363b9f14913 1064 else {
tnhnrl 16:3363b9f14913 1065 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 1066 }
tnhnrl 16:3363b9f14913 1067 }
tnhnrl 16:3363b9f14913 1068 }
tnhnrl 16:3363b9f14913 1069
tnhnrl 16:3363b9f14913 1070 void StateMachine::keyboard_menu_PITCH_PID_settings() {
tnhnrl 16:3363b9f14913 1071 char PID_key;
tnhnrl 16:3363b9f14913 1072 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 1073
tnhnrl 16:3363b9f14913 1074 // print the menu
tnhnrl 16:3363b9f14913 1075 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 1076 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 1077 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 16:3363b9f14913 1078 pc().printf("pitch P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
tnhnrl 20:8987a9ae2bc7 1079
tnhnrl 16:3363b9f14913 1080 // handle the key presses
tnhnrl 16:3363b9f14913 1081 while(1) {
tnhnrl 16:3363b9f14913 1082 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 1083 if (pc().readable()) {
tnhnrl 16:3363b9f14913 1084 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 1085 }
tnhnrl 16:3363b9f14913 1086 else {
tnhnrl 16:3363b9f14913 1087 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 1088 }
tnhnrl 16:3363b9f14913 1089
tnhnrl 16:3363b9f14913 1090 // handle the user's key input
tnhnrl 16:3363b9f14913 1091 if (PID_key == '-') {
tnhnrl 21:38c8544db6f4 1092 _pitch_KP -= gain_step_size;
tnhnrl 21:38c8544db6f4 1093 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1094 }
tnhnrl 16:3363b9f14913 1095 else if (PID_key == '=') {
tnhnrl 21:38c8544db6f4 1096 _pitch_KP += gain_step_size;
tnhnrl 21:38c8544db6f4 1097 pc().printf("\rP gain: %0.5f ", _pitch_KP);
tnhnrl 16:3363b9f14913 1098 }
tnhnrl 16:3363b9f14913 1099 else if (PID_key == '[') {
tnhnrl 21:38c8544db6f4 1100 _pitch_KI -= gain_step_size;
tnhnrl 21:38c8544db6f4 1101 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1102 }
tnhnrl 16:3363b9f14913 1103 else if (PID_key == ']') {
tnhnrl 21:38c8544db6f4 1104 _pitch_KI += gain_step_size;
tnhnrl 21:38c8544db6f4 1105 pc().printf("\rI gain: %0.5f ", _pitch_KI);
tnhnrl 16:3363b9f14913 1106 }
tnhnrl 16:3363b9f14913 1107 else if (PID_key == ';') {
tnhnrl 21:38c8544db6f4 1108 _pitch_KD -= gain_step_size;
tnhnrl 21:38c8544db6f4 1109 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1110 }
tnhnrl 16:3363b9f14913 1111 else if (PID_key == '\'') {
tnhnrl 21:38c8544db6f4 1112 _pitch_KD += gain_step_size;
tnhnrl 21:38c8544db6f4 1113 pc().printf("\rD gain: %0.5f ", _pitch_KD);
tnhnrl 16:3363b9f14913 1114 }
tnhnrl 16:3363b9f14913 1115 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 1116 // set global values
tnhnrl 21:38c8544db6f4 1117 pitchLoop().setControllerP(_pitch_KP);
tnhnrl 21:38c8544db6f4 1118 pitchLoop().setControllerI(_pitch_KI);
tnhnrl 21:38c8544db6f4 1119 pitchLoop().setControllerD(_pitch_KD);
tnhnrl 16:3363b9f14913 1120
tnhnrl 21:38c8544db6f4 1121 // save pitch PID values for outer loop
tnhnrl 21:38c8544db6f4 1122 configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm);
tnhnrl 16:3363b9f14913 1123 break; //exit the while loop
tnhnrl 16:3363b9f14913 1124 }
tnhnrl 16:3363b9f14913 1125 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 1126 break; //exit the while loop
tnhnrl 16:3363b9f14913 1127 }
tnhnrl 16:3363b9f14913 1128 else {
tnhnrl 16:3363b9f14913 1129 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 1130 }
tnhnrl 16:3363b9f14913 1131 }
tnhnrl 16:3363b9f14913 1132 }
tnhnrl 20:8987a9ae2bc7 1133
tnhnrl 16:3363b9f14913 1134 float StateMachine::getDepthCommand() {
tnhnrl 28:16c83a2fdefa 1135 return _depthCommand;
tnhnrl 16:3363b9f14913 1136 }
tnhnrl 20:8987a9ae2bc7 1137
tnhnrl 16:3363b9f14913 1138 float StateMachine::getPitchCommand() {
tnhnrl 28:16c83a2fdefa 1139 return _pitchCommand;
tnhnrl 17:7c16b5671d0e 1140 }
tnhnrl 28:16c83a2fdefa 1141
tnhnrl 17:7c16b5671d0e 1142 void StateMachine::setState(int input_state) {
tnhnrl 21:38c8544db6f4 1143 _state = input_state;
tnhnrl 17:7c16b5671d0e 1144 }
tnhnrl 20:8987a9ae2bc7 1145
tnhnrl 17:7c16b5671d0e 1146 int StateMachine::getState() {
tnhnrl 17:7c16b5671d0e 1147 return _state; //return the current state of the system
tnhnrl 17:7c16b5671d0e 1148 }
tnhnrl 20:8987a9ae2bc7 1149
tnhnrl 17:7c16b5671d0e 1150 void StateMachine::setTimeout(float input_timeout) {
tnhnrl 17:7c16b5671d0e 1151 _timeout = input_timeout;
tnhnrl 17:7c16b5671d0e 1152 }
tnhnrl 20:8987a9ae2bc7 1153
tnhnrl 17:7c16b5671d0e 1154 void StateMachine::setDepthCommand(float input_depth_command) {
tnhnrl 28:16c83a2fdefa 1155 _depthCommand = input_depth_command;
tnhnrl 17:7c16b5671d0e 1156 }
tnhnrl 20:8987a9ae2bc7 1157
tnhnrl 17:7c16b5671d0e 1158 void StateMachine::setPitchCommand(float input_pitch_command) {
tnhnrl 28:16c83a2fdefa 1159 _pitchCommand = input_pitch_command;
tnhnrl 17:7c16b5671d0e 1160 }
tnhnrl 20:8987a9ae2bc7 1161
tnhnrl 17:7c16b5671d0e 1162 void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) {
tnhnrl 21:38c8544db6f4 1163 _neutral_batt_pos_mm = batt_pos_mm;
tnhnrl 21:38c8544db6f4 1164 _neutral_bce_pos_mm = bce_pos_mm;
tnhnrl 17:7c16b5671d0e 1165
tnhnrl 21:38c8544db6f4 1166 pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm);
tnhnrl 17:7c16b5671d0e 1167 }
tnhnrl 20:8987a9ae2bc7 1168
tnhnrl 17:7c16b5671d0e 1169 int StateMachine::timeoutRunning() {
tnhnrl 28:16c83a2fdefa 1170 return _isTimeoutRunning;
tnhnrl 17:7c16b5671d0e 1171 }
tnhnrl 20:8987a9ae2bc7 1172
tnhnrl 17:7c16b5671d0e 1173 //process one state at a time
tnhnrl 17:7c16b5671d0e 1174 void StateMachine::getDiveSequence() {
tnhnrl 17:7c16b5671d0e 1175 //iterate through this sequence using the FSM
tnhnrl 24:c7d9b5bf3829 1176 currentStateStruct.state = sequenceController().sequenceStructLoaded[_multi_dive_counter].state;
tnhnrl 24:c7d9b5bf3829 1177 currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_multi_dive_counter].timeout;
tnhnrl 24:c7d9b5bf3829 1178 currentStateStruct.depth = sequenceController().sequenceStructLoaded[_multi_dive_counter].depth;
tnhnrl 24:c7d9b5bf3829 1179 currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_multi_dive_counter].pitch;
tnhnrl 17:7c16b5671d0e 1180
tnhnrl 17:7c16b5671d0e 1181 _timeout = currentStateStruct.timeout; //set timeout before exiting this function
tnhnrl 16:3363b9f14913 1182 }