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

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
tnhnrl
Date:
Fri Dec 01 22:43:14 2017 +0000
Revision:
28:16c83a2fdefa
Parent:
27:0a5b90cd65d6
Child:
29:6c030ac0c362
Child:
30:2964617e7676
Pool tested code from Friday 12/1/17

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