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 23:04:11 2017 +0000
Revision:
21:38c8544db6f4
Parent:
20:8987a9ae2bc7
Child:
22:a10ee088403b
A few large changes and a few small changes.  Works on the bench.

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