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:
Tue Nov 21 22:03:26 2017 +0000
Revision:
17:7c16b5671d0e
Parent:
16:3363b9f14913
Child:
18:85a7535af8fd
FSG code commit 11/21

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