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:
danstrider
Date:
Thu Nov 23 02:20:08 2017 +0000
Revision:
22:a10ee088403b
Parent:
21:38c8544db6f4
Child:
23:434f04ef1fad
Added ability for battery to move to find the pitch level.  Added a sub state (uses fall-thru) to find level before sinking.

Who changed what in which revision?

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