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 Nov 22 14:32:06 2017 +0000
Revision:
20:8987a9ae2bc7
Parent:
19:aaa0f4f29d50
Child:
21:38c8544db6f4
Re-Publish of Dan's mods

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 20:8987a9ae2bc7 5 _timeout = 20; // generic timeout for every state, seconds
tnhnrl 20:8987a9ae2bc7 6
tnhnrl 16:3363b9f14913 7 depthTolerance = 0.25; // depth tolerance for neutral finding exit critera
tnhnrl 16:3363b9f14913 8 pitchTolerance = 1.0; // pitch angle tolerance for neutral finding exit criteria
tnhnrl 20:8987a9ae2bc7 9
tnhnrl 16:3363b9f14913 10 bceFloatPosition = 300; // bce position for "float" states
tnhnrl 16:3363b9f14913 11 battFloatPosition = 50; // batt position for "broadcast" state
tnhnrl 20:8987a9ae2bc7 12
tnhnrl 16:3363b9f14913 13 depthCommand = 3.5; // user keyboard depth
tnhnrl 16:3363b9f14913 14 pitchCommand = -20.0; // user keyboard depth
tnhnrl 17:7c16b5671d0e 15
tnhnrl 20:8987a9ae2bc7 16 _neutral_sink_timer = 0;
tnhnrl 20:8987a9ae2bc7 17 _neutral_rise_timer = 0;
tnhnrl 17:7c16b5671d0e 18
tnhnrl 20:8987a9ae2bc7 19 previousPosition_mm = 220; // Troy: centered, overwritten by the state machine (LOAD THIS FROM CONFIG?)
tnhnrl 20:8987a9ae2bc7 20 // Dan: no, think - where do you Want the bce to start from? Full extend limt probably.
tnhnrl 20:8987a9ae2bc7 21
tnhnrl 20:8987a9ae2bc7 22 _state = SIT_IDLE; // select starting state here
tnhnrl 17:7c16b5671d0e 23 isTimeoutRunning = false; // default timer to not running
tnhnrl 20:8987a9ae2bc7 24 isSubStateTimerRunning = false; // default timer to not running
tnhnrl 17:7c16b5671d0e 25
tnhnrl 17:7c16b5671d0e 26 _neutral_buoyancy_bce_pos_mm = 0;
tnhnrl 17:7c16b5671d0e 27 _neutral_buoyancy_batt_pos_mm = 0;
tnhnrl 17:7c16b5671d0e 28
tnhnrl 17:7c16b5671d0e 29 //new
tnhnrl 17:7c16b5671d0e 30 _next_state = -1; //next state is used to prevent states from changing as the FSM executes
tnhnrl 17:7c16b5671d0e 31 _state_counter = 0;
tnhnrl 16:3363b9f14913 32 }
tnhnrl 20:8987a9ae2bc7 33
tnhnrl 17:7c16b5671d0e 34 //Finite State Machine (FSM)
tnhnrl 17:7c16b5671d0e 35 void StateMachine::runStateMachine() {
tnhnrl 20:8987a9ae2bc7 36
tnhnrl 17:7c16b5671d0e 37 static bool runFirstNeutral = false;
tnhnrl 17:7c16b5671d0e 38
tnhnrl 17:7c16b5671d0e 39 //use the _next_state when the state machine is run again (so that it cannot change states while the FSM executes)
tnhnrl 17:7c16b5671d0e 40 if (_next_state > -1)
tnhnrl 17:7c16b5671d0e 41 _state = _next_state; //current state comes from the state at the completion of the FSM
tnhnrl 17:7c16b5671d0e 42
tnhnrl 16:3363b9f14913 43 // finite state machine ... each state has at least one exit criteria
tnhnrl 17:7c16b5671d0e 44 switch (_state) {
tnhnrl 16:3363b9f14913 45 case SIT_IDLE :
tnhnrl 16:3363b9f14913 46 // there actually is no timeout for SIT_IDLE, but this enables some one-shot actions
tnhnrl 16:3363b9f14913 47 if (!isTimeoutRunning) {
tnhnrl 16:3363b9f14913 48 showMenu();
tnhnrl 16:3363b9f14913 49 pc().printf("\r\n\nstate: SIT_IDLE\r\n");
tnhnrl 17:7c16b5671d0e 50 isTimeoutRunning = true;
tnhnrl 20:8987a9ae2bc7 51
tnhnrl 16:3363b9f14913 52 // what is active?
tnhnrl 16:3363b9f14913 53 bce().pause();
tnhnrl 16:3363b9f14913 54 batt().pause();
tnhnrl 17:7c16b5671d0e 55
tnhnrl 17:7c16b5671d0e 56 //reset sub FSM
tnhnrl 20:8987a9ae2bc7 57 isSubStateTimerRunning = false;
tnhnrl 16:3363b9f14913 58 }
tnhnrl 20:8987a9ae2bc7 59
tnhnrl 16:3363b9f14913 60 // how exit?
tnhnrl 20:8987a9ae2bc7 61 keyboard(); // keyboard function will change the state if needed
tnhnrl 16:3363b9f14913 62 break;
tnhnrl 20:8987a9ae2bc7 63
tnhnrl 16:3363b9f14913 64 case EMERGENCY_CLIMB :
tnhnrl 16:3363b9f14913 65 // start local state timer and init any other one-shot actions
tnhnrl 16:3363b9f14913 66 if (!isTimeoutRunning) {
tnhnrl 16:3363b9f14913 67 pc().printf("\r\n\nstate: EMERGENCY_CLIMB\r\n");
tnhnrl 16:3363b9f14913 68 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 69 timer.start(); // background timer starts running
tnhnrl 17:7c16b5671d0e 70 isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 71
tnhnrl 16:3363b9f14913 72 // what needs to be started?
tnhnrl 16:3363b9f14913 73 bce().unpause();
tnhnrl 16:3363b9f14913 74 batt().unpause();
tnhnrl 20:8987a9ae2bc7 75
tnhnrl 20:8987a9ae2bc7 76 // what are the commands?
tnhnrl 16:3363b9f14913 77 bce().setPosition_mm(bce().getTravelLimit());
tnhnrl 16:3363b9f14913 78 batt().setPosition_mm(0.0);
tnhnrl 16:3363b9f14913 79 }
tnhnrl 20:8987a9ae2bc7 80
tnhnrl 16:3363b9f14913 81 // how exit?
tnhnrl 17:7c16b5671d0e 82 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 83 pc().printf("EC: timed out\r\n");
tnhnrl 17:7c16b5671d0e 84 _next_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 85 timer.reset();
tnhnrl 16:3363b9f14913 86 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 87 }
tnhnrl 17:7c16b5671d0e 88 else if (depthLoop().getPosition() < 0.2) { //if the depth is greater than 0.2 feet, go to float broadcast
tnhnrl 16:3363b9f14913 89 pc().printf("EC: depth: %3.1f, cmd: 0.5 [%0.1f sec]\r",depthLoop().getPosition(), timer.read());
tnhnrl 17:7c16b5671d0e 90 _next_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 91 timer.reset();
tnhnrl 16:3363b9f14913 92 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 93 }
tnhnrl 16:3363b9f14913 94 break;
tnhnrl 20:8987a9ae2bc7 95
tnhnrl 16:3363b9f14913 96 case FIND_NEUTRAL :
tnhnrl 20:8987a9ae2bc7 97 // start local state timer and init any other one-shot actions
tnhnrl 16:3363b9f14913 98 if (!isTimeoutRunning) {
tnhnrl 16:3363b9f14913 99 pc().printf("\r\n\nstate: FIND_NEUTRAL\r\n");
tnhnrl 16:3363b9f14913 100 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 101 timer.start(); // background timer starts running
tnhnrl 20:8987a9ae2bc7 102 isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 103
tnhnrl 16:3363b9f14913 104 // what needs to be started?
tnhnrl 16:3363b9f14913 105 bce().unpause();
tnhnrl 16:3363b9f14913 106 batt().unpause();
tnhnrl 17:7c16b5671d0e 107
tnhnrl 17:7c16b5671d0e 108 //first iteration sends SINKING sub-state
tnhnrl 20:8987a9ae2bc7 109 runFirstNeutral = true;
tnhnrl 16:3363b9f14913 110 }
tnhnrl 20:8987a9ae2bc7 111
tnhnrl 20:8987a9ae2bc7 112 // how exit? (exit with the timer, if timer still running continue processing sub FSM)
tnhnrl 17:7c16b5671d0e 113 if (timer > _timeout) {
tnhnrl 17:7c16b5671d0e 114 pc().printf("FN: timed out [time: %0.1f sec]\r\n", timer.read());
tnhnrl 17:7c16b5671d0e 115 _next_state = EMERGENCY_CLIMB; //new behavior (if this times out it emergency surfaces)
tnhnrl 16:3363b9f14913 116 timer.reset();
tnhnrl 16:3363b9f14913 117 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 118 }
tnhnrl 20:8987a9ae2bc7 119
tnhnrl 17:7c16b5671d0e 120 // what is active? (call neutral finding sub-function every iteration)
tnhnrl 17:7c16b5671d0e 121 if (runFirstNeutral) {
tnhnrl 17:7c16b5671d0e 122 //pc().printf("First iteration of find neutral\n\r"); // debug (confirmed working)
tnhnrl 17:7c16b5671d0e 123 _sub_state = findNeutralSubState(NEUTRAL_SINKING);
tnhnrl 20:8987a9ae2bc7 124 _previous_sub_state = _sub_state; //save previous sub-state
tnhnrl 17:7c16b5671d0e 125 runFirstNeutral = false;
tnhnrl 16:3363b9f14913 126 }
tnhnrl 20:8987a9ae2bc7 127 else { //second and further iterations, this is running
tnhnrl 17:7c16b5671d0e 128 _sub_state = findNeutralSubState(_previous_sub_state);
tnhnrl 17:7c16b5671d0e 129 _previous_sub_state = _sub_state; //save previous sub-state
tnhnrl 17:7c16b5671d0e 130
tnhnrl 17:7c16b5671d0e 131 //if sub-FSM has completed its state machine
tnhnrl 17:7c16b5671d0e 132 if (_sub_state == NEUTRAL_EXIT) {
tnhnrl 17:7c16b5671d0e 133 pc().printf("DEBUG: FIND_NEUTRAL sub FSM completed!\n\r");
tnhnrl 17:7c16b5671d0e 134 _next_state = RISE; //your next state will be RISE in the FSM
tnhnrl 17:7c16b5671d0e 135 }
tnhnrl 17:7c16b5671d0e 136 }
tnhnrl 17:7c16b5671d0e 137 break;
tnhnrl 17:7c16b5671d0e 138
tnhnrl 16:3363b9f14913 139 case DIVE :
tnhnrl 16:3363b9f14913 140 // start local state timer and init any other one-shot actions
tnhnrl 16:3363b9f14913 141 if (!isTimeoutRunning) {
tnhnrl 16:3363b9f14913 142 pc().printf("\r\n\nstate: DIVE\r\n");
tnhnrl 16:3363b9f14913 143 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 144 timer.start(); // background timer starts running
tnhnrl 17:7c16b5671d0e 145 isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 146
tnhnrl 16:3363b9f14913 147 // what needs to be started?
tnhnrl 16:3363b9f14913 148 bce().unpause();
tnhnrl 16:3363b9f14913 149 batt().unpause();
tnhnrl 20:8987a9ae2bc7 150
tnhnrl 16:3363b9f14913 151 // what are the commands?
tnhnrl 16:3363b9f14913 152 depthLoop().setCommand(depthCommand);
tnhnrl 16:3363b9f14913 153 pitchLoop().setCommand(pitchCommand);
tnhnrl 16:3363b9f14913 154 pc().printf("DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand());
tnhnrl 16:3363b9f14913 155 pc().printf("DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 156 }
tnhnrl 20:8987a9ae2bc7 157
tnhnrl 16:3363b9f14913 158 // how exit?
tnhnrl 17:7c16b5671d0e 159 if (timer > _timeout) {
tnhnrl 17:7c16b5671d0e 160 pc().printf("DIVE: timed out\n\n\r");
tnhnrl 17:7c16b5671d0e 161 _next_state = RISE; //new behavior 11/17/2017
tnhnrl 16:3363b9f14913 162 timer.reset();
tnhnrl 16:3363b9f14913 163 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 164 }
tnhnrl 16:3363b9f14913 165 else if (depthLoop().getPosition() > depthLoop().getCommand()) {
tnhnrl 16:3363b9f14913 166 pc().printf("DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 167 _next_state = RISE;
tnhnrl 16:3363b9f14913 168 timer.reset();
tnhnrl 16:3363b9f14913 169 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 170 }
tnhnrl 20:8987a9ae2bc7 171
tnhnrl 16:3363b9f14913 172 // what is active?
tnhnrl 17:7c16b5671d0e 173 pc().printf("DIVE: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) [%0.2f sec]\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read());
tnhnrl 16:3363b9f14913 174 bce().setPosition_mm(depthLoop().getOutput());
tnhnrl 16:3363b9f14913 175 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 16:3363b9f14913 176 break;
tnhnrl 16:3363b9f14913 177
tnhnrl 16:3363b9f14913 178 case RISE :
tnhnrl 16:3363b9f14913 179 // start local state timer and init any other one-shot actions
tnhnrl 16:3363b9f14913 180 if (!isTimeoutRunning) {
tnhnrl 16:3363b9f14913 181 pc().printf("\r\n\nstate: RISE\r\n");
tnhnrl 16:3363b9f14913 182 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 183 timer.start(); // background timer starts running
tnhnrl 17:7c16b5671d0e 184 isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 185
tnhnrl 16:3363b9f14913 186 // what needs to be started?
tnhnrl 16:3363b9f14913 187 bce().unpause();
tnhnrl 16:3363b9f14913 188 batt().unpause();
tnhnrl 16:3363b9f14913 189
tnhnrl 16:3363b9f14913 190 // what are the commands?
tnhnrl 16:3363b9f14913 191 depthLoop().setCommand(0.0);
tnhnrl 16:3363b9f14913 192 pitchLoop().setCommand(-pitchCommand);
tnhnrl 16:3363b9f14913 193 pc().printf("RISE: depth cmd: 0.0\r\n");
tnhnrl 16:3363b9f14913 194 pc().printf("RISE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 195 }
tnhnrl 20:8987a9ae2bc7 196
tnhnrl 16:3363b9f14913 197 // how exit?
tnhnrl 17:7c16b5671d0e 198 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 199 pc().printf("RISE: timed out\r\n");
tnhnrl 17:7c16b5671d0e 200 _next_state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 201 timer.reset();
tnhnrl 16:3363b9f14913 202 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 203 }
tnhnrl 16:3363b9f14913 204 else if (depthLoop().getPosition() < depthLoop().getCommand()) {
tnhnrl 16:3363b9f14913 205 pc().printf("RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 206 _next_state = FLOAT_LEVEL;
tnhnrl 16:3363b9f14913 207 timer.reset();
tnhnrl 16:3363b9f14913 208 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 209 }
tnhnrl 20:8987a9ae2bc7 210
tnhnrl 20:8987a9ae2bc7 211 // what is active?
tnhnrl 16:3363b9f14913 212 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 213 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 17:7c16b5671d0e 214 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 16:3363b9f14913 215 break;
tnhnrl 16:3363b9f14913 216
tnhnrl 16:3363b9f14913 217 case FLOAT_LEVEL :
tnhnrl 16:3363b9f14913 218 // start local state timer and init any other one-shot actions
tnhnrl 16:3363b9f14913 219 if (!isTimeoutRunning) {
tnhnrl 16:3363b9f14913 220 pc().printf("\r\n\nstate: FLOAT_LEVEL\r\n");
tnhnrl 16:3363b9f14913 221 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 222 timer.start(); // background timer starts running
tnhnrl 17:7c16b5671d0e 223 isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 224
tnhnrl 16:3363b9f14913 225 // what needs to be started?
tnhnrl 16:3363b9f14913 226 bce().unpause();
tnhnrl 16:3363b9f14913 227 batt().unpause();
tnhnrl 16:3363b9f14913 228
tnhnrl 20:8987a9ae2bc7 229 // what are the commands?
tnhnrl 16:3363b9f14913 230 bce().setPosition_mm(bceFloatPosition);
tnhnrl 16:3363b9f14913 231 pitchLoop().setCommand(0.0);
tnhnrl 16:3363b9f14913 232 }
tnhnrl 20:8987a9ae2bc7 233
tnhnrl 16:3363b9f14913 234 // how exit?
tnhnrl 17:7c16b5671d0e 235 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 236 pc().printf("FL: timed out\r\n");
tnhnrl 17:7c16b5671d0e 237 _next_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 238 timer.reset();
tnhnrl 16:3363b9f14913 239 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 240 }
tnhnrl 17:7c16b5671d0e 241 else if (fabs(imu().getPitch() - pitchLoop().getCommand()) < fabs(pitchTolerance)) {
tnhnrl 16:3363b9f14913 242 pc().printf("FL: pitch: %3.1f mm, set pos: %3.1f mm, deadband: %3.1f mm\r\n",imu().getPitch(), pitchLoop().getCommand(), pitchTolerance);
tnhnrl 17:7c16b5671d0e 243 _next_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 244 timer.reset();
tnhnrl 16:3363b9f14913 245 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 246 }
tnhnrl 20:8987a9ae2bc7 247
tnhnrl 16:3363b9f14913 248 // what is active?
tnhnrl 16:3363b9f14913 249 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 250 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 16:3363b9f14913 251 break;
tnhnrl 16:3363b9f14913 252
tnhnrl 16:3363b9f14913 253 case FLOAT_BROADCAST :
tnhnrl 16:3363b9f14913 254 // start local state timer and init any other one-shot actions
tnhnrl 16:3363b9f14913 255 if (!isTimeoutRunning) {
tnhnrl 16:3363b9f14913 256 pc().printf("\r\n\nstate: FLOAT_BROADCAST\r\n");
tnhnrl 16:3363b9f14913 257 timer.reset(); // timer goes back to zero
tnhnrl 16:3363b9f14913 258 timer.start(); // background timer starts running
tnhnrl 17:7c16b5671d0e 259 isTimeoutRunning = true;
tnhnrl 16:3363b9f14913 260
tnhnrl 16:3363b9f14913 261 // what needs to be started?
tnhnrl 16:3363b9f14913 262 bce().unpause();
tnhnrl 16:3363b9f14913 263 batt().unpause();
tnhnrl 20:8987a9ae2bc7 264
tnhnrl 16:3363b9f14913 265 // what are the commands?
tnhnrl 16:3363b9f14913 266 bce().setPosition_mm(bceFloatPosition);
tnhnrl 16:3363b9f14913 267 batt().setPosition_mm(battFloatPosition);
tnhnrl 16:3363b9f14913 268 }
tnhnrl 20:8987a9ae2bc7 269
tnhnrl 16:3363b9f14913 270 // how exit?
tnhnrl 17:7c16b5671d0e 271 if (timer > _timeout) {
tnhnrl 16:3363b9f14913 272 pc().printf("FB: timed out\r\n");
tnhnrl 17:7c16b5671d0e 273 _next_state = SIT_IDLE;
tnhnrl 16:3363b9f14913 274 timer.reset();
tnhnrl 16:3363b9f14913 275 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 276 }
tnhnrl 20:8987a9ae2bc7 277 else if ( (fabs(bce().getPosition_mm() - bce().getSetPosition_mm()) < bce().getDeadband()) and
tnhnrl 20:8987a9ae2bc7 278 (fabs(batt().getPosition_mm() - batt().getSetPosition_mm()) < batt().getDeadband()) ) {
tnhnrl 16:3363b9f14913 279 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 17:7c16b5671d0e 280 _next_state = SIT_IDLE;
tnhnrl 16:3363b9f14913 281 timer.reset();
tnhnrl 16:3363b9f14913 282 isTimeoutRunning = false;
tnhnrl 16:3363b9f14913 283 }
tnhnrl 20:8987a9ae2bc7 284
tnhnrl 20:8987a9ae2bc7 285 // what is active?
tnhnrl 16:3363b9f14913 286 pc().printf("FB: 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 16:3363b9f14913 287 break;
tnhnrl 17:7c16b5671d0e 288
tnhnrl 17:7c16b5671d0e 289 case MULTI_DIVE :
tnhnrl 17:7c16b5671d0e 290 // start local state timer and init any other one-shot actions
tnhnrl 17:7c16b5671d0e 291 if (!isTimeoutRunning) {
tnhnrl 17:7c16b5671d0e 292 pc().printf("\r\n\nstate: MULTI-DIVE\r\n");
tnhnrl 17:7c16b5671d0e 293 timer.reset(); // timer goes back to zero
tnhnrl 17:7c16b5671d0e 294 timer.start(); // background timer starts running
tnhnrl 17:7c16b5671d0e 295 isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 296
tnhnrl 17:7c16b5671d0e 297 // what needs to be started?
tnhnrl 17:7c16b5671d0e 298 bce().unpause();
tnhnrl 17:7c16b5671d0e 299 batt().unpause();
tnhnrl 20:8987a9ae2bc7 300
tnhnrl 17:7c16b5671d0e 301 //NEW: retrieve depth and pitch commands from config file struct
tnhnrl 17:7c16b5671d0e 302 // concept is to load this each time the multi-dive restarts
tnhnrl 17:7c16b5671d0e 303
tnhnrl 17:7c16b5671d0e 304 //keyboard starts sequence at 0 from keyboard function
tnhnrl 17:7c16b5671d0e 305 //stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 306
tnhnrl 17:7c16b5671d0e 307 //retrieve commands from structs
tnhnrl 17:7c16b5671d0e 308 float sequenceDepthCommand = currentStateStruct.depth;
tnhnrl 17:7c16b5671d0e 309 float sequencePitchCommand = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 310
tnhnrl 17:7c16b5671d0e 311 // what are the commands?
tnhnrl 17:7c16b5671d0e 312 depthLoop().setCommand(sequenceDepthCommand);
tnhnrl 17:7c16b5671d0e 313 pitchLoop().setCommand(sequencePitchCommand);
tnhnrl 17:7c16b5671d0e 314 pc().printf("MULTI-DIVE: depth cmd: %3.1f\r\n",depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 315 pc().printf("MULTI-DIVE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 17:7c16b5671d0e 316 }
tnhnrl 20:8987a9ae2bc7 317
tnhnrl 17:7c16b5671d0e 318 // how exit?
tnhnrl 17:7c16b5671d0e 319 if (timer > _timeout) {
tnhnrl 17:7c16b5671d0e 320 pc().printf("MULTI-DIVE: timed out\n\n\r");
tnhnrl 17:7c16b5671d0e 321 _next_state = MULTI_RISE; //new behavior 11/17/2017
tnhnrl 17:7c16b5671d0e 322 timer.reset();
tnhnrl 17:7c16b5671d0e 323 isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 324 }
tnhnrl 17:7c16b5671d0e 325 else if (depthLoop().getPosition() > depthLoop().getCommand()) {
tnhnrl 17:7c16b5671d0e 326 pc().printf("MULTI-DIVE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 327 _next_state = MULTI_RISE;
tnhnrl 17:7c16b5671d0e 328 timer.reset();
tnhnrl 17:7c16b5671d0e 329 isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 330 }
tnhnrl 20:8987a9ae2bc7 331
tnhnrl 17:7c16b5671d0e 332 // what is active?
tnhnrl 17:7c16b5671d0e 333 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 334 bce().setPosition_mm(depthLoop().getOutput());
tnhnrl 17:7c16b5671d0e 335 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 17:7c16b5671d0e 336 break;
tnhnrl 17:7c16b5671d0e 337
tnhnrl 17:7c16b5671d0e 338 case MULTI_RISE :
tnhnrl 17:7c16b5671d0e 339 // start local state timer and init any other one-shot actions
tnhnrl 17:7c16b5671d0e 340 if (!isTimeoutRunning) {
tnhnrl 17:7c16b5671d0e 341 pc().printf("\r\n\nstate: MULTI-RISE\r\n");
tnhnrl 17:7c16b5671d0e 342 timer.reset(); // timer goes back to zero
tnhnrl 17:7c16b5671d0e 343 timer.start(); // background timer starts running
tnhnrl 17:7c16b5671d0e 344 isTimeoutRunning = true;
tnhnrl 17:7c16b5671d0e 345
tnhnrl 17:7c16b5671d0e 346 // what needs to be started?
tnhnrl 17:7c16b5671d0e 347 bce().unpause();
tnhnrl 17:7c16b5671d0e 348 batt().unpause();
tnhnrl 17:7c16b5671d0e 349
tnhnrl 17:7c16b5671d0e 350 //NEW: retrieve depth and pitch commands from config file struct
tnhnrl 17:7c16b5671d0e 351 // concept is to load this each time the multi-dive restarts
tnhnrl 17:7c16b5671d0e 352 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 353
tnhnrl 17:7c16b5671d0e 354 //retrieve just pitch command from struct
tnhnrl 17:7c16b5671d0e 355 float sequencePitchCommand = currentStateStruct.pitch;
tnhnrl 17:7c16b5671d0e 356
tnhnrl 17:7c16b5671d0e 357 // what are the commands? (send back to 0.5 feet, not surface) // 11/21/2017
tnhnrl 17:7c16b5671d0e 358 depthLoop().setCommand(0.5);
tnhnrl 17:7c16b5671d0e 359 pitchLoop().setCommand(-sequencePitchCommand);
tnhnrl 17:7c16b5671d0e 360 pc().printf("MULTI-RISE: depth cmd: 0.0\r\n");
tnhnrl 17:7c16b5671d0e 361 pc().printf("MULTI-RISE: pitch cmd: %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 17:7c16b5671d0e 362 }
tnhnrl 20:8987a9ae2bc7 363
tnhnrl 17:7c16b5671d0e 364 // how exit?
tnhnrl 17:7c16b5671d0e 365 if (timer > _timeout) {
tnhnrl 17:7c16b5671d0e 366 pc().printf("MULTI-RISE: timed out\r\n");
tnhnrl 17:7c16b5671d0e 367 _next_state = EMERGENCY_CLIMB;
tnhnrl 17:7c16b5671d0e 368 timer.reset();
tnhnrl 17:7c16b5671d0e 369 isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 370
tnhnrl 17:7c16b5671d0e 371 //reset multi-dive sequence to start
tnhnrl 17:7c16b5671d0e 372 _state_counter = 0;
tnhnrl 17:7c16b5671d0e 373 }
tnhnrl 20:8987a9ae2bc7 374 else if (depthLoop().getPosition() < 0.5) { // depth is less than 0.5 (zero is surface level)
tnhnrl 17:7c16b5671d0e 375 pc().printf("MULTI-RISE: depth: %3.1f, cmd: %3.1f\r\n", depthLoop().getPosition(), depthLoop().getCommand());
tnhnrl 17:7c16b5671d0e 376
tnhnrl 17:7c16b5671d0e 377 //going to next state
tnhnrl 17:7c16b5671d0e 378 isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 379
tnhnrl 17:7c16b5671d0e 380 //successful dive-rise sequence CONTINUES the multi-dive sequence
tnhnrl 17:7c16b5671d0e 381 _state_counter++;
tnhnrl 17:7c16b5671d0e 382
tnhnrl 17:7c16b5671d0e 383 //UPDATE THE SEQUENCE DATA HERE
tnhnrl 17:7c16b5671d0e 384 stateMachine().getDiveSequence();
tnhnrl 17:7c16b5671d0e 385
tnhnrl 17:7c16b5671d0e 386 //check if this is the end of the dive sequence
tnhnrl 17:7c16b5671d0e 387 //CHECK BEFORE ANYTHING ELSE that you have reached the "exit" state (Float_Level)
tnhnrl 17:7c16b5671d0e 388 if (currentStateStruct.state == FLOAT_LEVEL) {
tnhnrl 17:7c16b5671d0e 389 _next_state = FLOAT_LEVEL;
tnhnrl 17:7c16b5671d0e 390 return;
tnhnrl 17:7c16b5671d0e 391 }
tnhnrl 17:7c16b5671d0e 392
tnhnrl 17:7c16b5671d0e 393 else
tnhnrl 17:7c16b5671d0e 394 _next_state = MULTI_DIVE;
tnhnrl 17:7c16b5671d0e 395
tnhnrl 17:7c16b5671d0e 396 //have to stop this with the _state_counter variable!
tnhnrl 17:7c16b5671d0e 397 }
tnhnrl 20:8987a9ae2bc7 398
tnhnrl 20:8987a9ae2bc7 399 // what is active?
tnhnrl 17:7c16b5671d0e 400 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 401 bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors
tnhnrl 17:7c16b5671d0e 402 batt().setPosition_mm(pitchLoop().getOutput());
tnhnrl 17:7c16b5671d0e 403 break;
tnhnrl 16:3363b9f14913 404
tnhnrl 16:3363b9f14913 405 default :
tnhnrl 17:7c16b5671d0e 406 pc().printf("DEBUG: SIT_IDLE\n\r");
tnhnrl 17:7c16b5671d0e 407 _next_state = SIT_IDLE;
tnhnrl 17:7c16b5671d0e 408 }
tnhnrl 16:3363b9f14913 409 }
tnhnrl 20:8987a9ae2bc7 410
tnhnrl 16:3363b9f14913 411 // output the keyboard menu for user's reference
tnhnrl 16:3363b9f14913 412 void StateMachine::showMenu() {
tnhnrl 16:3363b9f14913 413 pc().printf("\r\r\n\nKEYBOARD MENU:\r\r\n");
tnhnrl 16:3363b9f14913 414 pc().printf(" N to find neutral\r\n");
tnhnrl 17:7c16b5671d0e 415 pc().printf(" M to initiate multi-dive cycle\r\n");
tnhnrl 16:3363b9f14913 416 pc().printf(" D to initiate dive cycle\r\n");
tnhnrl 16:3363b9f14913 417 pc().printf(" R to initiate rise\r\n");
tnhnrl 16:3363b9f14913 418 pc().printf(" L to float level\r\n");
tnhnrl 16:3363b9f14913 419 pc().printf(" B to float at broadcast pitch\r\n");
tnhnrl 16:3363b9f14913 420 pc().printf(" E to initiate emergency climb\r\n");
tnhnrl 16:3363b9f14913 421 pc().printf(" H to run homing sequence on both BCE and Batt\r\n");
tnhnrl 16:3363b9f14913 422 pc().printf(" T to tare the depth sensor\r\n");
tnhnrl 16:3363b9f14913 423 pc().printf("[/] to change bce neutral position\r\n");
tnhnrl 16:3363b9f14913 424 pc().printf("</> to change batt neutral position\r\n");
tnhnrl 16:3363b9f14913 425 pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",pitchCommand);
tnhnrl 16:3363b9f14913 426 pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",depthCommand);
tnhnrl 17:7c16b5671d0e 427 pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout);
tnhnrl 16:3363b9f14913 428 pc().printf(" 1 BCE PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 429 pc().printf(" 2 BATT PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 430 pc().printf(" 3 Depth PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 431 pc().printf(" 4 Pitch PID sub-menu\r\n");
tnhnrl 16:3363b9f14913 432 pc().printf(" C See sensor readings\r\n");
tnhnrl 16:3363b9f14913 433 pc().printf(" ? to reset mbed\r\n");
tnhnrl 16:3363b9f14913 434 }
tnhnrl 20:8987a9ae2bc7 435
tnhnrl 17:7c16b5671d0e 436 //Find Neutral sub finite state machine
tnhnrl 17:7c16b5671d0e 437 // Note: the sub-fsm only moves the pistons once at the start of each timer loop
tnhnrl 17:7c16b5671d0e 438 // (timer completes, move piston, timer completes, move piston, etc)
tnhnrl 17:7c16b5671d0e 439 int StateMachine::findNeutralSubState(int input_substate) {
tnhnrl 20:8987a9ae2bc7 440
tnhnrl 17:7c16b5671d0e 441 //output substate is the input substate unless it is changed in the sub FSM
tnhnrl 17:7c16b5671d0e 442 static int output_substate = NEUTRAL_SINKING; //to start
tnhnrl 17:7c16b5671d0e 443
tnhnrl 17:7c16b5671d0e 444 switch (input_substate) {
tnhnrl 20:8987a9ae2bc7 445 case NEUTRAL_SINKING :
tnhnrl 17:7c16b5671d0e 446 //start the 10 second timer
tnhnrl 20:8987a9ae2bc7 447 if (!isSubStateTimerRunning) {
tnhnrl 17:7c16b5671d0e 448 pc().printf(" input_substate: %d\n\r", input_substate); //debug
tnhnrl 20:8987a9ae2bc7 449 _neutral_sink_timer = timer.read() + 10; //record the time when this block is first entered and add 10 seconds
tnhnrl 17:7c16b5671d0e 450
tnhnrl 20:8987a9ae2bc7 451 pc().printf("\r\n\n START NEUTRAL_SINKING (_neutral_sink_timer will go off at %0.1f sec) %3.1f\r\n", _neutral_sink_timer, timer.read());
tnhnrl 20:8987a9ae2bc7 452
tnhnrl 20:8987a9ae2bc7 453 // what are the commands?
tnhnrl 17:7c16b5671d0e 454 //move piston at start of sequence (retract 10 mm)
tnhnrl 17:7c16b5671d0e 455 pc().printf("Neutral Sinking: Retracting piston 10 mm\n\r");
tnhnrl 17:7c16b5671d0e 456 previousPosition_mm -= 10;
tnhnrl 17:7c16b5671d0e 457 bce().setPosition_mm(previousPosition_mm); //no depth command
tnhnrl 17:7c16b5671d0e 458
tnhnrl 20:8987a9ae2bc7 459 isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 460 }
tnhnrl 20:8987a9ae2bc7 461
tnhnrl 20:8987a9ae2bc7 462 // how exit?
tnhnrl 20:8987a9ae2bc7 463 //once reached the travel limit, no need to keep trying, so exit
tnhnrl 20:8987a9ae2bc7 464 if (bce().getPosition_mm() <= 0) {
tnhnrl 20:8987a9ae2bc7 465 output_substate = NEUTRAL_EXIT;
tnhnrl 17:7c16b5671d0e 466 }
tnhnrl 20:8987a9ae2bc7 467 //once deeper than the commanded setpoint...
tnhnrl 20:8987a9ae2bc7 468 else if (depthLoop().getPosition() > depthCommand) {
tnhnrl 20:8987a9ae2bc7 469 output_substate = NEUTRAL_SLOWLY_RISE; // next state
tnhnrl 20:8987a9ae2bc7 470 isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 20:8987a9ae2bc7 471 }
tnhnrl 20:8987a9ae2bc7 472
tnhnrl 20:8987a9ae2bc7 473 // what is active?
tnhnrl 20:8987a9ae2bc7 474 //once the 10 second timer is complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 20:8987a9ae2bc7 475 if (timer.read() >= _neutral_sink_timer) {
tnhnrl 20:8987a9ae2bc7 476 pc().printf("\r\n\n NEUTRAL_SINKING TIMER COMPLETE! %3.1f\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 477 pc().printf("\r\n\n (BATT POS: %0.1f) retract 10 mm (time: %3.1f)\r\n", previousPosition_mm, timer.read());
tnhnrl 17:7c16b5671d0e 478
tnhnrl 20:8987a9ae2bc7 479 isSubStateTimerRunning = false; //reset the sub state timer
tnhnrl 17:7c16b5671d0e 480 }
tnhnrl 17:7c16b5671d0e 481 break;
tnhnrl 17:7c16b5671d0e 482
tnhnrl 17:7c16b5671d0e 483 case NEUTRAL_SLOWLY_RISE:
tnhnrl 20:8987a9ae2bc7 484 if (!isSubStateTimerRunning) {
tnhnrl 17:7c16b5671d0e 485 pc().printf("\r\n\n(9) START NEUTRAL_SLOWLY_RISE (will go off at %0.1f sec)\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 486 _neutral_rise_timer = timer.read()+ 5; //record the time when this block is first entered and add 5 seconds
tnhnrl 17:7c16b5671d0e 487
tnhnrl 20:8987a9ae2bc7 488 isSubStateTimerRunning = true; //disable this block after one iteration
tnhnrl 17:7c16b5671d0e 489
tnhnrl 20:8987a9ae2bc7 490 // what are the commands?
tnhnrl 20:8987a9ae2bc7 491 //move piston at start of sequence (extend)
tnhnrl 17:7c16b5671d0e 492 pc().printf("Neutral Slowly Rise: Extending piston 1 mm\n\r");
tnhnrl 17:7c16b5671d0e 493 previousPosition_mm += 1;
tnhnrl 17:7c16b5671d0e 494 bce().setPosition_mm(previousPosition_mm); //no depth command
tnhnrl 17:7c16b5671d0e 495 }
tnhnrl 17:7c16b5671d0e 496
tnhnrl 20:8987a9ae2bc7 497 // how exit?
tnhnrl 20:8987a9ae2bc7 498 //once at full travel limit and haven't yet risen, time to give up and exit
tnhnrl 20:8987a9ae2bc7 499 if (bce().getPosition_mm() >= bce().getTravelLimit()) {
tnhnrl 20:8987a9ae2bc7 500 output_substate = NEUTRAL_EXIT;
tnhnrl 17:7c16b5671d0e 501 }
tnhnrl 17:7c16b5671d0e 502 //depth rate or sink rate < 0 ft/s, go to the next substate the next iteration
tnhnrl 20:8987a9ae2bc7 503 else if (depthLoop().getVelocity() < 0) { //less than zero ft/s
tnhnrl 17:7c16b5671d0e 504 pc().printf("\r\n\n(9) NEUTRAL_SLOWLY_RISE: Sink Rate below 0 ft/s [time: %0.1f]\r\n", timer.read());
tnhnrl 17:7c16b5671d0e 505 output_substate = NEUTRAL_CHECK_PITCH;
tnhnrl 17:7c16b5671d0e 506 }
tnhnrl 17:7c16b5671d0e 507
tnhnrl 20:8987a9ae2bc7 508 // what is active?
tnhnrl 20:8987a9ae2bc7 509 //once 5 second timer complete, reset the timeout so the state one-shot entry will move the setpoint
tnhnrl 20:8987a9ae2bc7 510 if (timer.read() >= _neutral_sink_timer) {
tnhnrl 20:8987a9ae2bc7 511 pc().printf("\r\n\n(9)NEUTRAL_SLOWLY_RISE TIMER COMPLETE! (time: %3.1f)\r\n", timer.read());
tnhnrl 20:8987a9ae2bc7 512 pc().printf("\r\n\n (BATT POS: %0.1f) extend 1 mm %3.1f\r\n", previousPosition_mm, timer.read());
tnhnrl 20:8987a9ae2bc7 513
tnhnrl 20:8987a9ae2bc7 514 isSubStateTimerRunning = false; // reset the sub state timer
tnhnrl 17:7c16b5671d0e 515 }
tnhnrl 17:7c16b5671d0e 516 break;
tnhnrl 17:7c16b5671d0e 517
tnhnrl 20:8987a9ae2bc7 518 case NEUTRAL_CHECK_PITCH :
tnhnrl 20:8987a9ae2bc7 519 pc().printf("NEUTRAL_CHECK_PITCH (time: %3.1f) [%0.1f deg, %0.1f deg/s] \r", timer.read(), pitchLoop().getPosition(), pitchLoop().getVelocity()); //debug
tnhnrl 20:8987a9ae2bc7 520 //no timeout in this sub FSM state (find_neutral timer still running)
tnhnrl 20:8987a9ae2bc7 521
tnhnrl 20:8987a9ae2bc7 522 //what are commands?
tnhnrl 17:7c16b5671d0e 523 //the BCE piston is maintaining its position
tnhnrl 17:7c16b5671d0e 524 //the battery is maintaining the pitch at zero degrees
tnhnrl 17:7c16b5671d0e 525
tnhnrl 20:8987a9ae2bc7 526 // how exit?
tnhnrl 20:8987a9ae2bc7 527 //pitch angle and pitch rate within small tolerance
tnhnrl 20:8987a9ae2bc7 528 //benchtop tests confirm angle needs to be around 2 degrees
tnhnrl 20:8987a9ae2bc7 529 if ((fabs(pitchLoop().getPosition()) < 0.5) and (fabs(pitchLoop().getVelocity()) < 2.0)) {
tnhnrl 20:8987a9ae2bc7 530 pc().printf("Saving Positions: BCE: %0.1f mm, BATT: %0.1f\n\r",bce().getPosition_mm(),batt().getPosition_mm());
tnhnrl 20:8987a9ae2bc7 531
tnhnrl 20:8987a9ae2bc7 532 //save positions locally
tnhnrl 20:8987a9ae2bc7 533 depthLoop().setOutputOffset(bce().getPosition_mm());
tnhnrl 20:8987a9ae2bc7 534 pitchLoop().setOutputOffset(batt().getPosition_mm());
tnhnrl 17:7c16b5671d0e 535
tnhnrl 17:7c16b5671d0e 536 //save to neutral.cfg
tnhnrl 20:8987a9ae2bc7 537 // Troy: save into the depth.txt and pitch.txt files, not a separate file.
tnhnrl 20:8987a9ae2bc7 538 ConfigFileIO().saveNeutralPositions(bce().getPosition_mm(),batt().getPosition_mm()); //BCE, BATT
tnhnrl 17:7c16b5671d0e 539
tnhnrl 17:7c16b5671d0e 540 output_substate = NEUTRAL_EXIT; //the "case" NEUTRAL_EXIT is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 17:7c16b5671d0e 541 }
tnhnrl 17:7c16b5671d0e 542 break;
tnhnrl 20:8987a9ae2bc7 543
tnhnrl 20:8987a9ae2bc7 544 case NEUTRAL_EXIT :
tnhnrl 20:8987a9ae2bc7 545 //NEUTRAL_EXIT state is used to tell the greater FSM that this sub-FSM has completed
tnhnrl 20:8987a9ae2bc7 546 pc().printf("substate: NEUTRAL_EXIT\n\r");
tnhnrl 20:8987a9ae2bc7 547 break;
tnhnrl 20:8987a9ae2bc7 548
tnhnrl 20:8987a9ae2bc7 549 default :
tnhnrl 20:8987a9ae2bc7 550 pc().printf("how did we get to substate default?\n\r"); //debug
tnhnrl 20:8987a9ae2bc7 551 output_substate = NEUTRAL_EXIT; //a default within the sub-state machine
tnhnrl 17:7c16b5671d0e 552 break;
tnhnrl 17:7c16b5671d0e 553 }
tnhnrl 20:8987a9ae2bc7 554
tnhnrl 17:7c16b5671d0e 555 return output_substate;
tnhnrl 17:7c16b5671d0e 556 }
tnhnrl 20:8987a9ae2bc7 557
tnhnrl 20:8987a9ae2bc7 558 // keyboard runs independently of the state machine, handling one key at a time
tnhnrl 20:8987a9ae2bc7 559 //keyboard updates the desired _keyboard_state that is used in the state machine
tnhnrl 20:8987a9ae2bc7 560 //and only allows input when the state is "idle"
tnhnrl 17:7c16b5671d0e 561 void StateMachine::keyboard() {
tnhnrl 16:3363b9f14913 562 char userInput;
tnhnrl 20:8987a9ae2bc7 563
tnhnrl 16:3363b9f14913 564 // check keyboard and make settings changes as requested
tnhnrl 17:7c16b5671d0e 565 // states can be changed only at the start of a sequence (when the system is in SIT_IDLE)
tnhnrl 17:7c16b5671d0e 566 if (pc().readable() && (_state == SIT_IDLE)) {
tnhnrl 16:3363b9f14913 567 // get the key
tnhnrl 17:7c16b5671d0e 568 userInput = pc().getc();
tnhnrl 17:7c16b5671d0e 569
tnhnrl 20:8987a9ae2bc7 570 // keyboard resets timer each time it's used
tnhnrl 20:8987a9ae2bc7 571 isTimeoutRunning = false;
tnhnrl 17:7c16b5671d0e 572
tnhnrl 17:7c16b5671d0e 573 pc().printf("KEYBOARD isTimeoutRunning: %d\n\r", isTimeoutRunning);
tnhnrl 17:7c16b5671d0e 574
tnhnrl 16:3363b9f14913 575 // check command against desired control buttons
tnhnrl 16:3363b9f14913 576 if (userInput == 'D' or userInput == 'd') {
tnhnrl 17:7c16b5671d0e 577 _keyboard_state = DIVE;
tnhnrl 16:3363b9f14913 578 }
tnhnrl 16:3363b9f14913 579 else if (userInput == 'N' or userInput == 'n') {
tnhnrl 17:7c16b5671d0e 580 _keyboard_state = FIND_NEUTRAL;
tnhnrl 17:7c16b5671d0e 581 }
tnhnrl 17:7c16b5671d0e 582 else if (userInput == 'M' or userInput == 'm') {
tnhnrl 17:7c16b5671d0e 583 //currently does not run if there is no file.
tnhnrl 17:7c16b5671d0e 584
tnhnrl 17:7c16b5671d0e 585 //need to add method to Sequence Controller that returns -1
tnhnrl 17:7c16b5671d0e 586 // or some check that insures you cannot run the dive sequence without a file
tnhnrl 17:7c16b5671d0e 587
tnhnrl 17:7c16b5671d0e 588 stateMachine().getDiveSequence(); //get first sequence on keyboard press
tnhnrl 17:7c16b5671d0e 589 _keyboard_state = currentStateStruct.state;
tnhnrl 17:7c16b5671d0e 590
tnhnrl 17:7c16b5671d0e 591 pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles
tnhnrl 16:3363b9f14913 592 }
tnhnrl 16:3363b9f14913 593 else if (userInput == 'R' or userInput == 'r') {
tnhnrl 17:7c16b5671d0e 594 _keyboard_state = RISE;
tnhnrl 16:3363b9f14913 595 }
tnhnrl 16:3363b9f14913 596 else if (userInput == 'L' or userInput == 'l') {
tnhnrl 17:7c16b5671d0e 597 _keyboard_state = FLOAT_LEVEL;
tnhnrl 16:3363b9f14913 598 }
tnhnrl 16:3363b9f14913 599 else if (userInput == 'B' or userInput == 'b') {
tnhnrl 17:7c16b5671d0e 600 _keyboard_state = FLOAT_BROADCAST;
tnhnrl 16:3363b9f14913 601 }
tnhnrl 16:3363b9f14913 602 else if (userInput == 'E' or userInput == 'e') {
tnhnrl 17:7c16b5671d0e 603 _keyboard_state = EMERGENCY_CLIMB;
tnhnrl 16:3363b9f14913 604 }
tnhnrl 16:3363b9f14913 605 else if (userInput == 'H' or userInput == 'h') {
tnhnrl 16:3363b9f14913 606 pc().printf("running homing procedure\r\n");
tnhnrl 16:3363b9f14913 607 bce().unpause(); bce().homePiston(); bce().pause();
tnhnrl 16:3363b9f14913 608 batt().unpause(); batt().homePiston(); batt().pause();
tnhnrl 16:3363b9f14913 609 }
tnhnrl 16:3363b9f14913 610 else if (userInput == 'T' or userInput == 't') {
tnhnrl 16:3363b9f14913 611 pc().printf("taring depth sensor\r\n");
tnhnrl 16:3363b9f14913 612 pc().printf("Pre-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 613 wait(0.1);
tnhnrl 16:3363b9f14913 614 depth().tare(); // tares to ambient (do on surface)
tnhnrl 16:3363b9f14913 615 pc().printf("Post-tare: press: %3.3f psi, depth: %3.3f ft\r\n", depth().getPsi(), depth().getDepthFt());
tnhnrl 16:3363b9f14913 616 }
tnhnrl 16:3363b9f14913 617
tnhnrl 16:3363b9f14913 618 else if (userInput == '[' or userInput == '{') {
tnhnrl 16:3363b9f14913 619 depthLoop().setOutputOffset(depthLoop().getOutputOffset() - 1); // decrease the bce neutral setpoint
tnhnrl 16:3363b9f14913 620 pc().printf("Adjusting bce neutral position. new: %3.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 621 }
tnhnrl 16:3363b9f14913 622 else if (userInput == ']' or userInput == '}') {
tnhnrl 16:3363b9f14913 623 depthLoop().setOutputOffset(depthLoop().getOutputOffset() + 1); // increase the bce neutral setpoint
tnhnrl 16:3363b9f14913 624 pc().printf("Adjusting bce neutral position. new: %3.1f\r\n",depthLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 625 }
tnhnrl 16:3363b9f14913 626 else if (userInput == '<' or userInput == ',') {
tnhnrl 16:3363b9f14913 627 pitchLoop().setOutputOffset(pitchLoop().getOutputOffset() - 1); // decrease the batt neutral setpoint
tnhnrl 16:3363b9f14913 628 pc().printf("Adjusting batt neutral position. new: %3.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 629 }
tnhnrl 16:3363b9f14913 630 else if (userInput == '>' or userInput == '.') {
tnhnrl 16:3363b9f14913 631 pitchLoop().setOutputOffset(pitchLoop().getOutputOffset() + 1); // increase the batt neutral setpoint
tnhnrl 16:3363b9f14913 632 pc().printf("Adjusting batt neutral position. new: %3.1f\r\n",pitchLoop().getOutputOffset());
tnhnrl 16:3363b9f14913 633 }
tnhnrl 16:3363b9f14913 634
tnhnrl 16:3363b9f14913 635 else if (userInput == '?') {
tnhnrl 16:3363b9f14913 636 pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n");
tnhnrl 16:3363b9f14913 637 wait(0.5);
tnhnrl 16:3363b9f14913 638 mbed_reset();
tnhnrl 16:3363b9f14913 639 }
tnhnrl 20:8987a9ae2bc7 640
tnhnrl 16:3363b9f14913 641 // change settings
tnhnrl 16:3363b9f14913 642 else if (userInput == 'Q' or userInput == 'q') {
tnhnrl 16:3363b9f14913 643 pitchCommand -= 0.5; //decrement the pitch setpoint
tnhnrl 16:3363b9f14913 644 pitchLoop().setCommand(pitchCommand);
tnhnrl 16:3363b9f14913 645 pc().printf(">>> new pitch angle setpoint: %0.3f deg (decreased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 646 }
tnhnrl 16:3363b9f14913 647 else if (userInput == 'W' or userInput == 'w') {
tnhnrl 16:3363b9f14913 648 pitchCommand += 0.5; //increment the pitch setpoint
tnhnrl 16:3363b9f14913 649 pitchLoop().setCommand(pitchCommand);
tnhnrl 16:3363b9f14913 650 pc().printf(">>> new pitch angle setpoint: %0.3f deg (increased)\r\n", pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 651 }
tnhnrl 16:3363b9f14913 652 else if (userInput == 'A' or userInput == 'a') {
tnhnrl 16:3363b9f14913 653 depthCommand -= 0.5; //decrement the depth setpoint
tnhnrl 16:3363b9f14913 654 depthLoop().setCommand(depthCommand);
tnhnrl 16:3363b9f14913 655 pc().printf(">>> new depth (ft) setpoint: %0.3f ft (sink)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 656 }
tnhnrl 16:3363b9f14913 657 else if (userInput == 'S' or userInput == 's') {
tnhnrl 16:3363b9f14913 658 depthCommand += 0.5; //increment the depth setpoint
tnhnrl 16:3363b9f14913 659 depthLoop().setCommand(depthCommand);
tnhnrl 16:3363b9f14913 660 pc().printf(">>> new depth setpoint: %0.3f ft (rise)\r\n", depthLoop().getCommand());
tnhnrl 16:3363b9f14913 661 }
tnhnrl 20:8987a9ae2bc7 662
tnhnrl 16:3363b9f14913 663 else if (userInput == '-') {
tnhnrl 17:7c16b5671d0e 664 _timeout -= 10.0; //decrement the timeout
tnhnrl 17:7c16b5671d0e 665 pc().printf(">>> timeout decreased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 666 }
tnhnrl 16:3363b9f14913 667 else if (userInput == '=' or userInput == '+') {
tnhnrl 17:7c16b5671d0e 668 _timeout += 10.0; //increment the timeout
tnhnrl 17:7c16b5671d0e 669 pc().printf(">>> timeout increased: %d\r\n", _timeout);
tnhnrl 16:3363b9f14913 670 }
tnhnrl 16:3363b9f14913 671
tnhnrl 16:3363b9f14913 672 // go to sub-menus for the PID gains (this is blocking)
tnhnrl 16:3363b9f14913 673 else if (userInput == '1') {
tnhnrl 16:3363b9f14913 674 keyboard_menu_BCE_PID_settings();
tnhnrl 16:3363b9f14913 675 }
tnhnrl 16:3363b9f14913 676 else if (userInput == '2') {
tnhnrl 16:3363b9f14913 677 keyboard_menu_BATT_PID_settings();
tnhnrl 16:3363b9f14913 678 }
tnhnrl 16:3363b9f14913 679 else if (userInput == '3') {
tnhnrl 16:3363b9f14913 680 keyboard_menu_DEPTH_PID_settings();
tnhnrl 16:3363b9f14913 681 }
tnhnrl 16:3363b9f14913 682 else if (userInput == '4') {
tnhnrl 16:3363b9f14913 683 keyboard_menu_PITCH_PID_settings();
tnhnrl 16:3363b9f14913 684 }
tnhnrl 16:3363b9f14913 685
tnhnrl 16:3363b9f14913 686 else if (userInput == 'C' or userInput == 'c') {
tnhnrl 16:3363b9f14913 687 pc().printf("depth: %3.1f\r\n",depthLoop().getPosition());
tnhnrl 16:3363b9f14913 688 pc().printf("pitch: %3.1f\r\n",imu().getPitch());
tnhnrl 16:3363b9f14913 689 pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm());
tnhnrl 16:3363b9f14913 690 pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm());
tnhnrl 16:3363b9f14913 691 pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm());
tnhnrl 16:3363b9f14913 692 pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm());
tnhnrl 16:3363b9f14913 693 pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand());
tnhnrl 16:3363b9f14913 694 pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand());
tnhnrl 16:3363b9f14913 695 }
tnhnrl 16:3363b9f14913 696 else {
tnhnrl 17:7c16b5671d0e 697 _keyboard_state = -1;
tnhnrl 16:3363b9f14913 698 }
tnhnrl 17:7c16b5671d0e 699
tnhnrl 17:7c16b5671d0e 700 //when you read the keyboard successfully, change the state
tnhnrl 17:7c16b5671d0e 701 _next_state = _keyboard_state; //set state at the end of this function
tnhnrl 16:3363b9f14913 702 }
tnhnrl 16:3363b9f14913 703 }
tnhnrl 20:8987a9ae2bc7 704
tnhnrl 20:8987a9ae2bc7 705
tnhnrl 16:3363b9f14913 706 void StateMachine::keyboard_menu_BCE_PID_settings() {
tnhnrl 16:3363b9f14913 707 char PID_key;
tnhnrl 16:3363b9f14913 708 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 709 float KP = bce().getControllerP(); // load current value
tnhnrl 16:3363b9f14913 710 float KI = bce().getControllerI(); // load current global value
tnhnrl 16:3363b9f14913 711 float KD = bce().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 712
tnhnrl 16:3363b9f14913 713 // show the menu
tnhnrl 16:3363b9f14913 714 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 715 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 716 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r");
tnhnrl 16:3363b9f14913 717 pc().printf("bce P: %3.2f, I: %3.2f, D %3.2f, zero %3i, limit %3.0f mm, slope %3.3f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
tnhnrl 16:3363b9f14913 718
tnhnrl 16:3363b9f14913 719 // handle the key presses
tnhnrl 16:3363b9f14913 720 while(1) {
tnhnrl 16:3363b9f14913 721 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 722 if (pc().readable()) {
tnhnrl 16:3363b9f14913 723 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 724 }
tnhnrl 16:3363b9f14913 725 else {
tnhnrl 16:3363b9f14913 726 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 727 }
tnhnrl 16:3363b9f14913 728
tnhnrl 16:3363b9f14913 729 // handle the user's key input
tnhnrl 16:3363b9f14913 730 if (PID_key == '-') {
tnhnrl 16:3363b9f14913 731 KP -= gain_step_size;
tnhnrl 16:3363b9f14913 732 pc().printf("P gain: %0.5f \r\n", KP);
tnhnrl 16:3363b9f14913 733 }
tnhnrl 16:3363b9f14913 734 else if (PID_key == '=') {
tnhnrl 16:3363b9f14913 735 KP += gain_step_size;
tnhnrl 16:3363b9f14913 736 pc().printf("P gain: %0.5f \r\n", KP);
tnhnrl 16:3363b9f14913 737 }
tnhnrl 16:3363b9f14913 738 else if (PID_key == '[') {
tnhnrl 16:3363b9f14913 739 KI -= gain_step_size;
tnhnrl 16:3363b9f14913 740 pc().printf("I gain: %0.5f \r\n", KI);
tnhnrl 16:3363b9f14913 741 }
tnhnrl 16:3363b9f14913 742 else if (PID_key == ']') {
tnhnrl 16:3363b9f14913 743 KI += gain_step_size;
tnhnrl 16:3363b9f14913 744 pc().printf("I gain: %0.5f \r\n", KI);
tnhnrl 16:3363b9f14913 745 }
tnhnrl 16:3363b9f14913 746 else if (PID_key == ';') {
tnhnrl 16:3363b9f14913 747 KD -= gain_step_size;
tnhnrl 16:3363b9f14913 748 pc().printf("D gain: %0.5f \r\n", KD);
tnhnrl 16:3363b9f14913 749 }
tnhnrl 16:3363b9f14913 750 else if (PID_key == '\'') {
tnhnrl 16:3363b9f14913 751 KD += gain_step_size;
tnhnrl 16:3363b9f14913 752 pc().printf("D gain: %0.5f \r\n", KD);
tnhnrl 16:3363b9f14913 753 }
tnhnrl 16:3363b9f14913 754 else if (PID_key == 'S') { // user wants to save these modified values
tnhnrl 16:3363b9f14913 755 // set values
tnhnrl 16:3363b9f14913 756 bce().setControllerP(KP);
tnhnrl 16:3363b9f14913 757 bce().setControllerI(KI);
tnhnrl 16:3363b9f14913 758 bce().setControllerD(KD);
tnhnrl 16:3363b9f14913 759
tnhnrl 16:3363b9f14913 760 // save into "PID.cfg"
tnhnrl 16:3363b9f14913 761 //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 762 break; //exit the while loop
tnhnrl 16:3363b9f14913 763 }
tnhnrl 16:3363b9f14913 764 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 765 break; //exit the while loop
tnhnrl 16:3363b9f14913 766 }
tnhnrl 16:3363b9f14913 767 else {
tnhnrl 16:3363b9f14913 768 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 769 }
tnhnrl 16:3363b9f14913 770 }
tnhnrl 16:3363b9f14913 771 }
tnhnrl 20:8987a9ae2bc7 772
tnhnrl 16:3363b9f14913 773 void StateMachine::keyboard_menu_BATT_PID_settings() {
tnhnrl 16:3363b9f14913 774 char PID_key;
tnhnrl 16:3363b9f14913 775 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 776 float KP = batt().getControllerP(); // load current global value
tnhnrl 16:3363b9f14913 777 float KI = batt().getControllerI(); // load current global value
tnhnrl 16:3363b9f14913 778 float KD = batt().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 779
tnhnrl 16:3363b9f14913 780 // print the menu
tnhnrl 16:3363b9f14913 781 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 782 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 783 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 16:3363b9f14913 784 pc().printf("batt P: %3.2f, I: %3.2f, D %3.2f, zero %3i, limit %3.0f mm, slope %3.3f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
tnhnrl 20:8987a9ae2bc7 785
tnhnrl 16:3363b9f14913 786 // handle the key presses
tnhnrl 16:3363b9f14913 787 while(1) {
tnhnrl 16:3363b9f14913 788 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 789 if (pc().readable()) {
tnhnrl 16:3363b9f14913 790 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 791 }
tnhnrl 16:3363b9f14913 792 else {
tnhnrl 16:3363b9f14913 793 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 794 }
tnhnrl 16:3363b9f14913 795
tnhnrl 16:3363b9f14913 796 // handle the user's key input
tnhnrl 16:3363b9f14913 797 if (PID_key == '-') {
tnhnrl 16:3363b9f14913 798 KP -= gain_step_size;
tnhnrl 16:3363b9f14913 799 pc().printf("\rP gain: %0.5f ", KP);
tnhnrl 16:3363b9f14913 800 }
tnhnrl 16:3363b9f14913 801 else if (PID_key == '=') {
tnhnrl 16:3363b9f14913 802 KP += gain_step_size;
tnhnrl 16:3363b9f14913 803 pc().printf("\rP gain: %0.5f ", KP);
tnhnrl 16:3363b9f14913 804 }
tnhnrl 16:3363b9f14913 805 else if (PID_key == '[') {
tnhnrl 16:3363b9f14913 806 KI -= gain_step_size;
tnhnrl 16:3363b9f14913 807 pc().printf("\rI gain: %0.5f ", KI);
tnhnrl 16:3363b9f14913 808 }
tnhnrl 16:3363b9f14913 809 else if (PID_key == ']') {
tnhnrl 16:3363b9f14913 810 KI += gain_step_size;
tnhnrl 16:3363b9f14913 811 pc().printf("\rI gain: %0.5f ", KI);
tnhnrl 16:3363b9f14913 812 }
tnhnrl 16:3363b9f14913 813 else if (PID_key == ';') {
tnhnrl 16:3363b9f14913 814 KD -= gain_step_size;
tnhnrl 16:3363b9f14913 815 pc().printf("\rD gain: %0.5f ", KD);
tnhnrl 16:3363b9f14913 816 }
tnhnrl 16:3363b9f14913 817 else if (PID_key == '\'') {
tnhnrl 16:3363b9f14913 818 KD += gain_step_size;
tnhnrl 16:3363b9f14913 819 pc().printf("\rD gain: %0.5f ", KD);
tnhnrl 16:3363b9f14913 820 }
tnhnrl 16:3363b9f14913 821 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 822 // set global values
tnhnrl 16:3363b9f14913 823 batt().setControllerP(KP);
tnhnrl 16:3363b9f14913 824 batt().setControllerI(KI);
tnhnrl 16:3363b9f14913 825 batt().setControllerD(KD);
tnhnrl 16:3363b9f14913 826
tnhnrl 16:3363b9f14913 827 // save to "PID.cfg" file
tnhnrl 16:3363b9f14913 828 //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 829 break; //exit the while loop
tnhnrl 16:3363b9f14913 830 }
tnhnrl 16:3363b9f14913 831 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 832 break; //exit the while loop
tnhnrl 16:3363b9f14913 833 }
tnhnrl 16:3363b9f14913 834 else {
tnhnrl 16:3363b9f14913 835 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 836 }
tnhnrl 16:3363b9f14913 837 }
tnhnrl 16:3363b9f14913 838 }
tnhnrl 20:8987a9ae2bc7 839
tnhnrl 16:3363b9f14913 840 void StateMachine::keyboard_menu_DEPTH_PID_settings() {
tnhnrl 16:3363b9f14913 841 char PID_key;
tnhnrl 16:3363b9f14913 842 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 843 float KP = depthLoop().getControllerP(); // load current global value
tnhnrl 16:3363b9f14913 844 float KI = depthLoop().getControllerI(); // load current global value
tnhnrl 16:3363b9f14913 845 float KD = depthLoop().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 846
tnhnrl 16:3363b9f14913 847 // show the menu
tnhnrl 16:3363b9f14913 848 pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 849 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 850 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r");
tnhnrl 16:3363b9f14913 851 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 852
tnhnrl 16:3363b9f14913 853 // handle the key presses
tnhnrl 16:3363b9f14913 854 while(1) {
tnhnrl 16:3363b9f14913 855 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 856 if (pc().readable()) {
tnhnrl 16:3363b9f14913 857 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 858 }
tnhnrl 16:3363b9f14913 859 else {
tnhnrl 16:3363b9f14913 860 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 861 }
tnhnrl 16:3363b9f14913 862
tnhnrl 16:3363b9f14913 863 // handle the user's key input
tnhnrl 16:3363b9f14913 864 if (PID_key == '-') {
tnhnrl 16:3363b9f14913 865 KP -= gain_step_size;
tnhnrl 16:3363b9f14913 866 pc().printf("P gain: %0.5f \r\n", KP);
tnhnrl 16:3363b9f14913 867 }
tnhnrl 16:3363b9f14913 868 else if (PID_key == '=') {
tnhnrl 16:3363b9f14913 869 KP += gain_step_size;
tnhnrl 16:3363b9f14913 870 pc().printf("P gain: %0.5f \r\n", KP);
tnhnrl 16:3363b9f14913 871 }
tnhnrl 16:3363b9f14913 872 else if (PID_key == '[') {
tnhnrl 16:3363b9f14913 873 KI -= gain_step_size;
tnhnrl 16:3363b9f14913 874 pc().printf("I gain: %0.5f \r\n", KI);
tnhnrl 16:3363b9f14913 875 }
tnhnrl 16:3363b9f14913 876 else if (PID_key == ']') {
tnhnrl 16:3363b9f14913 877 KI += gain_step_size;
tnhnrl 16:3363b9f14913 878 pc().printf("I gain: %0.5f \r\n", KI);
tnhnrl 16:3363b9f14913 879 }
tnhnrl 16:3363b9f14913 880 else if (PID_key == ';') {
tnhnrl 16:3363b9f14913 881 KD -= gain_step_size;
tnhnrl 16:3363b9f14913 882 pc().printf("D gain: %0.5f \r\n", KD);
tnhnrl 16:3363b9f14913 883 }
tnhnrl 16:3363b9f14913 884 else if (PID_key == '\'') {
tnhnrl 16:3363b9f14913 885 KD += gain_step_size;
tnhnrl 16:3363b9f14913 886 pc().printf("D gain: %0.5f \r\n", KD);
tnhnrl 16:3363b9f14913 887 }
tnhnrl 16:3363b9f14913 888 else if (PID_key == 'S') { // user wants to save these settings
tnhnrl 16:3363b9f14913 889 // set global values
tnhnrl 16:3363b9f14913 890 depthLoop().setControllerP(KP);
tnhnrl 16:3363b9f14913 891 depthLoop().setControllerI(KI);
tnhnrl 16:3363b9f14913 892 depthLoop().setControllerD(KD);
tnhnrl 16:3363b9f14913 893
tnhnrl 16:3363b9f14913 894 // save into "PID.cfg"
tnhnrl 16:3363b9f14913 895 //Config_File_IO().write_auto_PID_values_to_config(pitch_controller_P,pitch_controller_I,pitch_controller_D,depth_controller_P,depth_controller_I,depth_controller_D);
tnhnrl 16:3363b9f14913 896 break; //exit the while loop
tnhnrl 16:3363b9f14913 897 }
tnhnrl 16:3363b9f14913 898 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 899 break; //exit the while loop
tnhnrl 16:3363b9f14913 900 }
tnhnrl 16:3363b9f14913 901 else {
tnhnrl 16:3363b9f14913 902 pc().printf("\n\rThis key does nothing here. ");
tnhnrl 16:3363b9f14913 903 }
tnhnrl 16:3363b9f14913 904 }
tnhnrl 16:3363b9f14913 905 }
tnhnrl 16:3363b9f14913 906
tnhnrl 16:3363b9f14913 907 void StateMachine::keyboard_menu_PITCH_PID_settings() {
tnhnrl 16:3363b9f14913 908 char PID_key;
tnhnrl 16:3363b9f14913 909 float gain_step_size = 0.01; // modify this to change gain step size
tnhnrl 16:3363b9f14913 910 float KP = pitchLoop().getControllerP(); // load current global value
tnhnrl 16:3363b9f14913 911 float KI = pitchLoop().getControllerI(); // load current global value
tnhnrl 16:3363b9f14913 912 float KD = pitchLoop().getControllerD(); // load current global value
tnhnrl 16:3363b9f14913 913
tnhnrl 16:3363b9f14913 914 // print the menu
tnhnrl 16:3363b9f14913 915 pc().printf("\n\r2: Battery Motor PID gain settings (MENU)");
tnhnrl 16:3363b9f14913 916 pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'");
tnhnrl 16:3363b9f14913 917 pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r");
tnhnrl 16:3363b9f14913 918 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 919
tnhnrl 16:3363b9f14913 920 // handle the key presses
tnhnrl 16:3363b9f14913 921 while(1) {
tnhnrl 16:3363b9f14913 922 // get the user's keystroke from either of the two inputs
tnhnrl 16:3363b9f14913 923 if (pc().readable()) {
tnhnrl 16:3363b9f14913 924 PID_key = pc().getc();
tnhnrl 16:3363b9f14913 925 }
tnhnrl 16:3363b9f14913 926 else {
tnhnrl 16:3363b9f14913 927 continue; // didn't get a user input, so keep waiting for it
tnhnrl 16:3363b9f14913 928 }
tnhnrl 16:3363b9f14913 929
tnhnrl 16:3363b9f14913 930 // handle the user's key input
tnhnrl 16:3363b9f14913 931 if (PID_key == '-') {
tnhnrl 16:3363b9f14913 932 KP -= gain_step_size;
tnhnrl 16:3363b9f14913 933 pc().printf("\rP gain: %0.5f ", KP);
tnhnrl 16:3363b9f14913 934 }
tnhnrl 16:3363b9f14913 935 else if (PID_key == '=') {
tnhnrl 16:3363b9f14913 936 KP += gain_step_size;
tnhnrl 16:3363b9f14913 937 pc().printf("\rP gain: %0.5f ", KP);
tnhnrl 16:3363b9f14913 938 }
tnhnrl 16:3363b9f14913 939 else if (PID_key == '[') {
tnhnrl 16:3363b9f14913 940 KI -= gain_step_size;
tnhnrl 16:3363b9f14913 941 pc().printf("\rI gain: %0.5f ", KI);
tnhnrl 16:3363b9f14913 942 }
tnhnrl 16:3363b9f14913 943 else if (PID_key == ']') {
tnhnrl 16:3363b9f14913 944 KI += gain_step_size;
tnhnrl 16:3363b9f14913 945 pc().printf("\rI gain: %0.5f ", KI);
tnhnrl 16:3363b9f14913 946 }
tnhnrl 16:3363b9f14913 947 else if (PID_key == ';') {
tnhnrl 16:3363b9f14913 948 KD -= gain_step_size;
tnhnrl 16:3363b9f14913 949 pc().printf("\rD gain: %0.5f ", KD);
tnhnrl 16:3363b9f14913 950 }
tnhnrl 16:3363b9f14913 951 else if (PID_key == '\'') {
tnhnrl 16:3363b9f14913 952 KD += gain_step_size;
tnhnrl 16:3363b9f14913 953 pc().printf("\rD gain: %0.5f ", KD);
tnhnrl 16:3363b9f14913 954 }
tnhnrl 16:3363b9f14913 955 else if (PID_key == 'S') { // user wants to save the modified values
tnhnrl 16:3363b9f14913 956 // set global values
tnhnrl 16:3363b9f14913 957 pitchLoop().setControllerP(KP);
tnhnrl 16:3363b9f14913 958 pitchLoop().setControllerI(KI);
tnhnrl 16:3363b9f14913 959 pitchLoop().setControllerD(KD);
tnhnrl 16:3363b9f14913 960
tnhnrl 16:3363b9f14913 961 //Config_File_IO().write_auto_PID_values_to_config(pitch_controller_P,pitch_controller_I,pitch_controller_D,depth_controller_P,depth_controller_I,depth_controller_D);
tnhnrl 16:3363b9f14913 962 break; //exit the while loop
tnhnrl 16:3363b9f14913 963 }
tnhnrl 16:3363b9f14913 964 else if (PID_key == 'X') {
tnhnrl 16:3363b9f14913 965 break; //exit the while loop
tnhnrl 16:3363b9f14913 966 }
tnhnrl 16:3363b9f14913 967 else {
tnhnrl 16:3363b9f14913 968 pc().printf("This key does nothing here.\r");
tnhnrl 16:3363b9f14913 969 }
tnhnrl 16:3363b9f14913 970 }
tnhnrl 16:3363b9f14913 971 }
tnhnrl 20:8987a9ae2bc7 972
tnhnrl 16:3363b9f14913 973 float StateMachine::getDepthCommand() {
tnhnrl 16:3363b9f14913 974 return depthCommand;
tnhnrl 16:3363b9f14913 975 }
tnhnrl 20:8987a9ae2bc7 976
tnhnrl 16:3363b9f14913 977 float StateMachine::getPitchCommand() {
tnhnrl 16:3363b9f14913 978 return pitchCommand;
tnhnrl 17:7c16b5671d0e 979 }
tnhnrl 20:8987a9ae2bc7 980
tnhnrl 17:7c16b5671d0e 981 void StateMachine::setState(int input_state) {
tnhnrl 17:7c16b5671d0e 982 //pc().printf("input_state: %d\n\r", input_state); //debug
tnhnrl 17:7c16b5671d0e 983 //_state = input_state; //changing wrong variable
tnhnrl 17:7c16b5671d0e 984 _next_state = input_state;
tnhnrl 17:7c16b5671d0e 985 }
tnhnrl 20:8987a9ae2bc7 986
tnhnrl 17:7c16b5671d0e 987 int StateMachine::getState() {
tnhnrl 17:7c16b5671d0e 988 return _state; //return the current state of the system
tnhnrl 17:7c16b5671d0e 989 }
tnhnrl 20:8987a9ae2bc7 990
tnhnrl 17:7c16b5671d0e 991 void StateMachine::setTimeout(float input_timeout) {
tnhnrl 17:7c16b5671d0e 992 _timeout = input_timeout;
tnhnrl 17:7c16b5671d0e 993 }
tnhnrl 20:8987a9ae2bc7 994
tnhnrl 17:7c16b5671d0e 995 void StateMachine::setDepthCommand(float input_depth_command) {
tnhnrl 17:7c16b5671d0e 996 depthCommand = input_depth_command;
tnhnrl 17:7c16b5671d0e 997 }
tnhnrl 20:8987a9ae2bc7 998
tnhnrl 17:7c16b5671d0e 999 void StateMachine::setPitchCommand(float input_pitch_command) {
tnhnrl 17:7c16b5671d0e 1000 pitchCommand = input_pitch_command;
tnhnrl 17:7c16b5671d0e 1001 }
tnhnrl 20:8987a9ae2bc7 1002
tnhnrl 17:7c16b5671d0e 1003 void StateMachine::setNeutralPositions(float batt_pos_mm, float bce_pos_mm) {
tnhnrl 17:7c16b5671d0e 1004 _neutral_buoyancy_batt_pos_mm = batt_pos_mm;
tnhnrl 17:7c16b5671d0e 1005 _neutral_buoyancy_bce_pos_mm = bce_pos_mm;
tnhnrl 17:7c16b5671d0e 1006
tnhnrl 17:7c16b5671d0e 1007 pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_buoyancy_batt_pos_mm,_neutral_buoyancy_bce_pos_mm);
tnhnrl 17:7c16b5671d0e 1008 }
tnhnrl 20:8987a9ae2bc7 1009
tnhnrl 17:7c16b5671d0e 1010 int StateMachine::timeoutRunning() {
tnhnrl 17:7c16b5671d0e 1011 return isTimeoutRunning;
tnhnrl 17:7c16b5671d0e 1012 }
tnhnrl 20:8987a9ae2bc7 1013
tnhnrl 17:7c16b5671d0e 1014 //process one state at a time
tnhnrl 17:7c16b5671d0e 1015 void StateMachine::getDiveSequence() {
tnhnrl 17:7c16b5671d0e 1016 //iterate through this sequence using the FSM
tnhnrl 17:7c16b5671d0e 1017 currentStateStruct.state = sequenceController().sequenceStructLoaded[_state_counter].state;
tnhnrl 17:7c16b5671d0e 1018 currentStateStruct.timeout = sequenceController().sequenceStructLoaded[_state_counter].timeout;
tnhnrl 17:7c16b5671d0e 1019 currentStateStruct.depth = sequenceController().sequenceStructLoaded[_state_counter].depth;
tnhnrl 17:7c16b5671d0e 1020 currentStateStruct.pitch = sequenceController().sequenceStructLoaded[_state_counter].pitch;
tnhnrl 17:7c16b5671d0e 1021
tnhnrl 17:7c16b5671d0e 1022 _timeout = currentStateStruct.timeout; //set timeout before exiting this function
tnhnrl 16:3363b9f14913 1023 }