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
Diff: StateMachine/StateMachine.cpp
- Revision:
- 58:94b7fd55185e
- Parent:
- 57:ec69651c8c21
- Child:
- 63:6cb0405fc6e6
--- a/StateMachine/StateMachine.cpp Thu Jun 14 16:10:25 2018 +0000 +++ b/StateMachine/StateMachine.cpp Fri Jun 15 20:11:39 2018 +0000 @@ -11,10 +11,11 @@ _depth_command = 2.0; // user keyboard depth (default) _pitch_command = -20.0; // user keyboard pitch (default) + _heading_command = 0.0; //new commands - _BCE_position_command = bce().getTravelLimit(); //starting at the limits - _BMM_position_command = batt().getTravelLimit(); + _BCE_dive_offset = 0.0; //starting at the limits + _BMM_dive_offset = 0.0; //new commands _neutral_timer = 0; //timer used in FIND_NEUTRAL sub-FSM @@ -190,7 +191,7 @@ case FIND_NEUTRAL : // start local state timer and init any other one-shot actions if (!_isTimeoutRunning) { - pc().printf("\r\n\nstate: FIND_NEUTRAL\n\r"); + pc().printf("\r\n\nstate: FIND_NEUTRAL\r\n"); timer.reset(); // timer goes back to zero timer.start(); // background timer starts running _isTimeoutRunning = true; @@ -239,7 +240,7 @@ //check if substate returned exit state, if so stop running the sub-FSM else if (runNeutralStateMachine() == NEUTRAL_EXIT) { //if successful, FIND_NEUTRAL then goes to RISE - pc().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\n\n\r"); + pc().printf("*************************************** FIND_NEUTRAL sequence complete. Rising.\r\n\n"); _state = RISE; _isTimeoutRunning = false; } @@ -285,7 +286,7 @@ // how exit? if (timer.read() > _timeout) { - pc().printf("DIVE: timed out\n\n\r"); + pc().printf("DIVE: timed out\r\n\n"); _state = RISE; //new behavior 11/17/2017 timer.reset(); _isTimeoutRunning = false; @@ -298,7 +299,7 @@ } // what is active? - 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()); + pc().printf("DIVE: bce pos: %3.1f mm, batt pos: %3.1f mm (depth: %3.1f ft) (pitch: %3.1f deg)[%0.2f sec] (CMD BCE: %0.1f BATT: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), pitchLoop().getPosition(), timer.read(),bce().getSetPosition_mm(),batt().getSetPosition_mm()); bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors batt().setPosition_mm(pitchLoop().getOutput()); @@ -360,14 +361,143 @@ } // what is active? - 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()); + pc().printf("RISE: bce pos: %3.1f mm, batt pos: %3.1f mm (depthLoop POS: %3.1f ft) (pitch: %3.1f deg)[%0.2f sec] (CMD BCE: %0.1f BATT: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), pitchLoop().getPosition(), timer.read(),bce().getSetPosition_mm(),batt().getSetPosition_mm()); bce().setPosition_mm(depthLoop().getOutput()); //constantly checking the Outer Loop output to move the motors batt().setPosition_mm(pitchLoop().getOutput()); //record data every 5 seconds recordData(_state); - break; + break; + +// NEW DIVE AND RISE SEQUENCES + case POSITION_DIVE : + // start local state timer and init any other one-shot actions + if (!_isTimeoutRunning) { + pc().printf("\r\n\nstate: POSITION DIVE\r\n"); + timer.reset(); // timer goes back to zero + timer.start(); // background timer starts running + _isTimeoutRunning = true; + + // what needs to be started? + bce().unpause(); + batt().unpause(); + rudder().unpause(); + + // what are the commands? (using inner loops except for heading outer loop) + // These actions happen ONCE in the POSITION_DIVE sequence + batt().setPosition_mm(_neutral_batt_pos_mm - _BMM_dive_offset); + bce().setPosition_mm(_neutral_bce_pos_mm- _BMM_dive_offset); + //rudder().setPosition_deg(_heading_command); + + headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code) + + pc().printf("PD: BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //get the actual commanded position + pc().printf("PD: BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //get the actual commanded position + pc().printf("PD: heading cmd: %3.1f\r\n",headingLoop().getCommand()); + + //reset max dive depth + _max_recorded_depth_dive = -99; //float to record max depth + + //show that this is the start of new dive sequence + recordState(_state); + + //triggers logger array + _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again + recordData(_state); + } + + // how exit? + // timer runs out goes to POSITION_RISE + if (timer.read() > _timeout) { + pc().printf("PD: timed out\r\n\n"); + _state = POSITION_RISE; //new behavior 11/17/2017 + timer.reset(); + _isTimeoutRunning = false; + } + + // when you reach the dive threshold, surface + else if (depthLoop().getPosition() > depthLoop().getCommand() - 0.5) { // including offset for low momentum approaches + pc().printf("PD: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand()); + _state = POSITION_RISE; + timer.reset(); + _isTimeoutRunning = false; + } + + // what is active? + pc().printf("PD: bce pos (cmd pos): %3.1f mm (%0.1f), batt pos: %3.1f mm (%0.1f), rudder: %f (depth: %3.1f ft, pitch: %3.1f, heading: %3.1f [cmd: %0.1f])[%0.2f sec] [imu heading: %0.1f] \r", bce().getPosition_mm(),bce().getSetPosition_mm(), batt().getPosition_mm(),batt().getSetPosition_mm(), rudder().getPosition_pwm(), depthLoop().getPosition(), pitchLoop().getPosition(), headingLoop().getPosition(), headingLoop().getOutput(), timer.read(), imu().getHeading()); + + if (depthLoop().getPosition() > _max_recorded_depth_dive) { + _max_recorded_depth_dive = depthLoop().getPosition(); //new max depth recorded when it is larger than previous values + } + + // ACTIVE RUDDER CONTROL + rudder().setPosition_deg(headingLoop().getOutput()); + + //record data internally (for access by MbedLogger) + recordData(_state); + + break; + + case POSITION_RISE : + // start local state timer and init any other one-shot actions + + if (!_isTimeoutRunning) { + pc().printf("\r\n\nstate: POSITION RISE\r\n"); + timer.reset(); // timer goes back to zero + timer.start(); // background timer starts running + _isTimeoutRunning = true; + + // what needs to be started? + bce().unpause(); + batt().unpause(); + + // what are the commands? (using inner loops except for heading outer loop) + pc().printf("PR: <<DEBUG>> CMD BCE: %0.1f, CMD BMM: %0.1f)\r\n", _BCE_dive_offset, _BMM_dive_offset); + + batt().setPosition_mm(_neutral_batt_pos_mm + _BMM_dive_offset); //reversing the BCE and BATT positions + bce().setPosition_mm(_neutral_bce_pos_mm + _BCE_dive_offset); //reversing the BCE and BATT positions + //rudder().setPosition_deg(_heading_command); //heading stays the same + + headingLoop().setCommand(_heading_command); //ACTIVE HEADING (mimic of dive and rise code) + + pc().printf("PR: BATT cmd: %3.1f\r\n",batt().getSetPosition_mm()); //get the actual commanded position + pc().printf("PR: BCE cmd: %3.1f\r\n",bce().getSetPosition_mm()); //get the actual commanded position + pc().printf("PR: heading cmd: %3.1f\r\n",headingLoop().getCommand()); + + //show that this is the start of new rise sequence + recordState(_state); + + //triggers logger array + _is_log_timer_running = false; // reset the sub state timer to do one-shot actions again + recordData(_state); + } + + // how exit? + if (timer.read() > _timeout) { + pc().printf("PR: timed out\r\n"); + _state = EMERGENCY_CLIMB; + timer.reset(); + _isTimeoutRunning = false; + } + else if (depthLoop().getPosition() < 0.5) { + pc().printf("PR: actual depth: %3.1f (cmd: %3.1f)\r\n", depthLoop().getPosition(), depthLoop().getCommand()); + _state = FLOAT_BROADCAST; + timer.reset(); + _isTimeoutRunning = false; + } + + // what is active? + pc().printf("PR: bce pos (cmd pos): %3.1f mm (%0.1f), batt pos: %3.1f mm (%0.1f), rudder: %f (depth: %3.1f ft, pitch: %3.1f, heading: %3.1f [cmd: %0.1f])[%0.2f sec] [imu heading: %0.1f] \r", bce().getPosition_mm(),bce().getSetPosition_mm(), batt().getPosition_mm(),batt().getSetPosition_mm(), rudder().getPosition_pwm(), depthLoop().getPosition(), pitchLoop().getPosition(), headingLoop().getPosition(), headingLoop().getOutput(), timer.read(), imu().getHeading()); + + // ACTIVE RUDDER CONTROL + rudder().setPosition_deg(headingLoop().getOutput()); + + //record data internally (for access by MbedLogger) + recordData(_state); + + break; +// NEW DIVE AND RISE SEQUENCES case FLOAT_LEVEL : // start local state timer and init any other one-shot actions @@ -470,7 +600,7 @@ } // what is active? - pc().printf("FB: bce pos: %0.1f mm, batt pos: %0.1f mm (depthLoop POS: %3.1f ft) [%0.1f sec] (CMD BCE: %0.1f BATT: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), depthLoop().getPosition(), timer.read(), bce().getSetPosition_mm(),batt().getSetPosition_mm()); + pc().printf("FB: bce pos: %0.1f mm, batt pos: %0.1f mm, heading(IMU): %0.1f (depthLoop POS: %3.1f ft) [%0.1f sec] (CMD BCE: %0.1f BATT: %0.1f)\r", bce().getPosition_mm(), batt().getPosition_mm(), imu().getHeading(), depthLoop().getPosition(), timer.read(), bce().getSetPosition_mm(),batt().getSetPosition_mm()); //record data every 5 seconds recordData(_state); @@ -513,7 +643,7 @@ // how exit? if (timer > _timeout) { - pc().printf("\n\n\rMULTI-DIVE: timed out [time: %0.1f]\n\n\r", timer.read()); + pc().printf("\r\n\nMULTI-DIVE: timed out [time: %0.1f]\r\n\n", timer.read()); _state = MULTI_RISE; //new behavior 11/17/2017 timer.reset(); _isTimeoutRunning = false; @@ -572,7 +702,7 @@ // how exit? if (timer > _timeout) { - pc().printf("MULTI-RISE: timed out [time: %0.1f]\n\n\r", timer.read()); + pc().printf("MULTI-RISE: timed out [time: %0.1f]\r\n\n", timer.read()); _state = EMERGENCY_CLIMB; timer.reset(); _isTimeoutRunning = false; @@ -624,11 +754,11 @@ mbedLogger().getNumberOfPacketsInCurrentLog(); //open the file, read the number of lines in the log file transmit_packet_number = mbedLogger().getNumberOfPackets(); - //pc().printf("getNumberOfPacketsInCurrentLog is %d\n\r", transmit_packet_number); + //pc().printf("getNumberOfPacketsInCurrentLog is %d\r\n", transmit_packet_number); } if (timer.read() > _timeout) { - pc().printf("\n\rTRANSMIT_LOG: timed out!\n\r"); + pc().printf("\r\nTRANSMIT_LOG: timed out!\r\n"); _state = SIT_IDLE; timer.reset(); _isTimeoutRunning = false; @@ -647,7 +777,7 @@ } if (mbedLogger().isTransmissionComplete()) { - pc().printf("StateMachine isTransmissionComplete (true)\n\r"); + pc().printf("StateMachine isTransmissionComplete (true)\r\n"); _state = SIT_IDLE; timer.reset(); _isTimeoutRunning = false; @@ -662,7 +792,7 @@ break; case RECEIVE_SEQUENCE : - pc().printf("state: RECEIVE_SEQUENCE\n\r"); + pc().printf("state: RECEIVE_SEQUENCE\r\n"); if (!_isTimeoutRunning) { pc().printf("RECEIVE_SEQUENCE _isTimeoutRunning\r\n"); @@ -672,19 +802,19 @@ } if (timer.read() > _timeout) { - pc().printf("RECEIVE_SEQUENCE: timed out!\n\r"); + pc().printf("RECEIVE_SEQUENCE: timed out!\r\n"); _state = SIT_IDLE; timer.reset(); _isTimeoutRunning = false; } // what is active? - pc().printf("Receive sequence active?\n\r"); + pc().printf("Receive sequence active?\r\n"); break; default : - pc().printf("DEBUG: SIT_IDLE\n\r"); + pc().printf("DEBUG: SIT_IDLE\r\n"); _state = SIT_IDLE; } @@ -705,57 +835,34 @@ } // output the keyboard menu for user's reference -void StateMachine::showSimpleMenu() { -// if (user_input == 'D') { -// _keyboard_state = DIVE; -// } -// else if (user_input == 'N') { -// _keyboard_state = FIND_NEUTRAL; -// } -// else if (user_input == 'L') { -// _keyboard_state = FLOAT_LEVEL; -// } -// else if (user_input == 'B') { -// _keyboard_state = FLOAT_BROADCAST; -// } -// else if (user_input == 'E') { -// _keyboard_state = EMERGENCY_CLIMB; -// } -// -// // some debug tools below -// else if (user_input == 'P') { -// //Print current SD card log file -// //printCurrentSdLog(); -// mbedLogger().printCurrentLogFile(); //print the current log file to the screen -// } -// else if (user_input == 'O') { -// _keyboard_state = TRANSMIT_LOG; //Transmit data (work in progress) -// } -// else if (user_input == '~') { -// pc().printf("ERASING MBED LOG FILE\n\r"); -// mbedLogger().eraseFile(); - - pc().printf("\r\r\n\nSIMPLE KEYBOARD MENU (06/14/2018):\r\r\n"); //make sure depth sensor tares itself on startup - pc().printf(" D to initiate dive cycle\r\n"); +void StateMachine::showSimpleMenu() { + pc().printf("\r\r\n\nSIMPLE KEYBOARD MENU (06/15/2018):\r\r\n"); //make sure depth sensor tares itself on startup + pc().printf(" Neutral Position BCE: %0.1f BMM: %01.f \r\n", _neutral_bce_pos_mm, _neutral_batt_pos_mm); + + pc().printf(" V to POSITION DIVE (initiate motor position-based dive cycle)\r\n"); pc().printf(" J to float level\r\n"); pc().printf(" B to float at broadcast pitch\r\n"); pc().printf(" E to initiate emergency climb\r\n"); pc().printf(" P to print the current log file.\r\n"); pc().printf(" O to transmit current log file.\r\n"); pc().printf(" ~ to erase mbed log file. (clear before logging more than a few runs)\r\n"); - pc().printf("[/] to change bce neutral position: %0.1f\r\n", _neutral_bce_pos_mm); - pc().printf("</> to change batt neutral position: %0.1f\r\n", _neutral_batt_pos_mm); - pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",_pitch_command); - pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",_depth_command); - pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout); + + pc().printf("k/l to decrease/increase BCE offset: %3.1f (Dive POS: %0.1f, Rise POS: %0.1f)\r\n",_BCE_dive_offset, _neutral_bce_pos_mm - _BCE_dive_offset, _neutral_bce_pos_mm + _BCE_dive_offset); + pc().printf(";/' to decrease/increase BMM offset: %3.1f (Dive POS: %0.1f, Rise POS: %0.1f)\r\n",_BMM_dive_offset, _neutral_batt_pos_mm - _BMM_dive_offset, _neutral_batt_pos_mm + _BMM_dive_offset); + pc().printf("9/0 to decrease/increase heading setpoint: %0.1f deg\r\n",_heading_command); + + pc().printf("-/+ to decrease/increase timeout: %d s\r\n",_timeout); pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n"); + pc().printf(" 8 STREAM SENSOR STATUS (and channel readings)\r\n"); + pc().printf(" ? to reset mbed\r\n"); - pc().printf(" * (asterisk) to go to DEBUG menu\r\n"); + pc().printf(" * (asterisk) to go to DEBUG keyboard menu\r\n"); } void StateMachine::showDebugMenu() { - pc().printf("\r\r\n\nDEBUG MENU (06/14/2018):\r\r\n"); - pc().printf(" T to go into CHECK TUNING (This is on a timer! Uses NEUTRAL positions!)\r\n"); + pc().printf("\r\r\n\nDEBUG KEYBOARD MENU (06/15/2018):\r\r\n"); + pc().printf(" T to go into CHECK NEUTRAL TUNING (This is on a timer! Uses NEUTRAL positions!)\r\n"); + pc().printf(" V to POSITION DIVE (initiate motor position-based dive cycle)\r\n"); pc().printf(" N to find neutral\r\n"); pc().printf(" M to initiate multi-dive cycle\r\n"); pc().printf(" D to initiate dive cycle\r\n"); @@ -774,21 +881,28 @@ pc().printf("</> to change batt neutral position: %0.1f\r\n", _neutral_batt_pos_mm); pc().printf("Q/W to decrease/increase pitch setpoint: %3.1f\r\n",_pitch_command); pc().printf("A/S to decrease/increase depth setpoint: %3.1f\r\n",_depth_command); - pc().printf("+/- to decrease/increase timeout: %d s\r\n",_timeout); + pc().printf("9/0 to decrease/increase heading setpoint: %0.1f deg\r\n",_heading_command); + pc().printf("k/l to decrease/increase BCE offset: %3.1f (Dive POS: %0.1f, Rise POS: %0.1f)\r\n",_BCE_dive_offset, _neutral_bce_pos_mm - _BCE_dive_offset, _neutral_bce_pos_mm + _BCE_dive_offset); + pc().printf(";/' to decrease/increase BMM offset: %3.1f (Dive POS: %0.1f, Rise POS: %0.1f)\r\n",_BMM_dive_offset, _neutral_batt_pos_mm - _BMM_dive_offset, _neutral_batt_pos_mm + _BMM_dive_offset); + pc().printf("-/+ to decrease/increase timeout: %d s\r\n",_timeout); + +// FIXED WITH NEW CODE pc().printf(" 1 BCE PID sub-menu\r\n"); pc().printf(" 2 BATT PID sub-menu\r\n"); pc().printf(" 3 Depth PID sub-menu\r\n"); pc().printf(" 4 Pitch PID sub-menu\r\n"); - pc().printf(" 5 MANUAL_TUNING sub-menu (does not have a timer!)\r\n"); + pc().printf(" 5 Rudder (servo) sub-menu\r\n"); + pc().printf(" 6 HEADING PID sub-menu\r\n"); + pc().printf(" 7 MANUAL_TUNING sub-menu (does not have a timer!) *** MOTORS ARE ACTIVE ***\r\n"); + //pc().printf(" 8 VIEW_OUTPUTS sub-menu (does not a have a timer!)\r\n"); + pc().printf(" 8 STREAM SENSOR STATUS (and channel readings)\r\n"); - pc().printf(" 6 STREAM STATUS\r\n"); - pc().printf(" 7 RUDDER SERVO settings menu\r\n"); - pc().printf(" 8 CHANNEL READINGS\r\n"); - pc().printf(" 9 POSITION READINGS\r\n"); +// FIXED WITH NEW CODE + pc().printf(" C See sensor readings (and max recorded depth of dive & neutral sequences)\r\n"); pc().printf(" ? to reset mbed\r\n"); - pc().printf(" * (asterisk) go to SIMPLE menu\r\n"); + pc().printf(" * (asterisk) to go to SIMPLE keyboard menu\r\n"); } //Find Neutral sub finite state machine @@ -801,12 +915,12 @@ if (!_isSubStateTimerRunning) { _neutral_timer = timer.read() + 5; //record the time when this block is first entered and add 5 seconds - pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f) (getSetPosition: %0.1f)\n\r", _neutral_timer, timer.read(), pitchLoop().getPosition(), bce().getSetPosition_mm()); + pc().printf("\r\n\nNEUTRAL_SINKING: Next retraction at %0.1f sec [current time: %0.1f] (pitch: %0.1f) (getSetPosition: %0.1f)\r\n", _neutral_timer, timer.read(), pitchLoop().getPosition(), bce().getSetPosition_mm()); // what are the commands? (BCE linear actuator active, no pitch movement) bce().setPosition_mm(bce().getSetPosition_mm() - 2.5); //Troy: There is some strange error where this has to be a hardcoded number. - pc().printf("NEUTRAL_SINKING: Retracting piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f] (pitch: %0.1f)\n\r", _neutral_sink_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand(), pitchLoop().getPosition()); + pc().printf("NEUTRAL_SINKING: Retracting piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f] (pitch: %0.1f)\r\n", _neutral_sink_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand(), pitchLoop().getPosition()); _isSubStateTimerRunning = true; //disable this block after one iteration } @@ -814,7 +928,7 @@ // how exit? //once reached the travel limit, no need to keep trying, so exit if (bce().getPosition_mm() <= 0) { - pc().printf("\n\rDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\n\r", bce().getPosition_mm()); + pc().printf("\r\nDEBUG: BCE current position is %0.1f mm (NEXT SUBSTATE NEUTRAL EXIT)\r\n", bce().getPosition_mm()); _substate = NEUTRAL_EXIT; _isSubStateTimerRunning = false; // reset the sub state timer } @@ -849,7 +963,7 @@ // it's okay to run the pitch outer loop now since we've already found pitch level in the previous state pitchLoop().setCommand(0.0); - pc().printf("NEUTRAL_SLOWLY_RISE: Extending BCE piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f]\n\r", _neutral_rise_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand()); + pc().printf("NEUTRAL_SLOWLY_RISE: Extending BCE piston %0.1f mm [BCE CMD : %0.1f] [pitch cmd: %0.1f]\r\n", _neutral_rise_command_mm, bce().getSetPosition_mm(), pitchLoop().getCommand()); _isSubStateTimerRunning = true; //disable this block after one iteration } @@ -890,11 +1004,11 @@ // what are the commands? (default: retract or extend 0.5 mm) if (pitchLoop().getPosition() > 2) { // nose is high batt().setPosition_mm(batt().getSetPosition_mm() + 0.5); // move battery forward (using setpoint from linear actuator) - pc().printf("\n\rNeutral Check Pitch: moving battery FWD in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm); + pc().printf("\r\nNeutral Check Pitch: moving battery FWD in %0.1f mm increments\r\n\n", _neutral_pitch_command_mm); } else if (pitchLoop().getPosition() < -2) { // nose is low batt().setPosition_mm(batt().getSetPosition_mm() - 0.5); // move battery aft (using setpoint from linear actuator) - pc().printf("\n\rNeutral Check Pitch: moving battery AFT in %0.1f mm increments\n\n\r", _neutral_pitch_command_mm); + pc().printf("\r\nNeutral Check Pitch: moving battery AFT in %0.1f mm increments\r\n\n", _neutral_pitch_command_mm); } _isSubStateTimerRunning = true; //disable this block after one iteration @@ -904,7 +1018,7 @@ //pitch angle and pitch rate within small tolerance //benchtop tests confirm angle needs to be around 2 degrees if ((fabs(pitchLoop().getPosition()) < 2.0) and (fabs(pitchLoop().getVelocity()) < 5.0)) { - pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\n\r"); //debug + pc().printf("Debug: Found Level (NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH)\r\n"); //debug // found level, but don't need to save anything this time if (depthLoop().getPosition() > _max_recorded_depth_neutral) { //debug @@ -925,14 +1039,14 @@ configFileIO().savePitchData(_pitch_KP, _pitch_KI, _pitch_KD, _neutral_batt_pos_mm); //P,I,D,batt zeroOffset configFileIO().saveDepthData(_depth_KP, _depth_KI, _depth_KD, _neutral_bce_pos_mm); //P,I,D, bce zeroOffset - pc().printf("\n\n\r>>> Saving Positions: BCE: %0.1f mm, BATT: %0.1f <<<\n\n\r",_neutral_bce_pos_mm,_neutral_batt_pos_mm); + pc().printf("\r\n\n>>> Saving Positions: BCE: %0.1f mm, BATT: %0.1f <<<\r\n\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm); _substate = NEUTRAL_EXIT; _isSubStateTimerRunning = false; // reset the sub state timer to do one-shot actions again } else { - pc().printf("\n\rDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\n\r"); + pc().printf("\r\nDid not find NEUTRAL_CHECK_PITCH or NEUTRAL_FIRST_PITCH, how did I get here?!\r\n"); _substate = NEUTRAL_EXIT; } } @@ -948,11 +1062,11 @@ //this state could be removed, it is only used as a transition but is needed to stop entering this function case NEUTRAL_EXIT : - pc().printf("substate: NEUTRAL_EXIT\n\r"); + pc().printf("substate: NEUTRAL_EXIT\r\n"); break; default : - pc().printf("how did we get to substate: default?\n\r"); //debug + pc().printf("how did we get to substate: default?\r\n"); //debug //a default within the sub-state machine _substate = NEUTRAL_EXIT; break; @@ -960,7 +1074,7 @@ // reset the sub-FSM if needed (useful if you need to redo the neutral-finding sequence) if (_substate == NEUTRAL_EXIT) { - pc().printf("******************************** EXITING sub-FSM! *******************************\n\n\r"); + pc().printf("******************************** EXITING sub-FSM! *******************************\r\n\n"); //reset internal sub-state back to first entry conditions (first state is immediately sinking) _substate = NEUTRAL_SINKING; @@ -1034,7 +1148,7 @@ stateMachine().getDiveSequence(); //get first sequence on keyboard press _keyboard_state = currentStateStruct.state; - pc().printf("Starting Dive Sequence Controller! (state: %d)\n\r", _keyboard_state); //neutral sequence and dive cycles + pc().printf("Starting Dive Sequence Controller! (state: %d)\r\n", _keyboard_state); //neutral sequence and dive cycles } else if (user_input == 'R') { _keyboard_state = RISE; @@ -1053,6 +1167,10 @@ _keyboard_state = CHECK_TUNING; } + else if (user_input == 'V') { + _keyboard_state = POSITION_DIVE; + } + // some debug tools below else if (user_input == 'P') { //Print current SD card log file @@ -1069,11 +1187,11 @@ mbedLogger().receiveMissionDataWithTicker(); //receive sequence.txt files } else if (user_input == '~') { - pc().printf("ERASING MBED LOG FILE\n\r"); + pc().printf("ERASING MBED LOG FILE\r\n"); mbedLogger().eraseFile(); } else if (user_input == 'Z') { - pc().printf("FSG FSM States: \n\r"); + pc().printf("FSG FSM States: \r\n"); string string_state; for (int i = 0; i < _state_array_counter; i++) { @@ -1097,10 +1215,10 @@ string_state = "MULTI_RISE"; else if (_state_array[i] == KEYBOARD) string_state = "KEYBOARD"; - pc().printf("State #%d: %d (%s)\n\r", i, _state_array[i], string_state.c_str()); + pc().printf("State #%d: %d (%s)\r\n", i, _state_array[i], string_state.c_str()); } - pc().printf("\n\rNeutral sub-FSM States: \n\r"); + pc().printf("\r\nNeutral sub-FSM States: \r\n"); string string_substate; for (int i = 0; i < _substate_array_counter; i++) { @@ -1114,9 +1232,9 @@ string_substate = "NEUTRAL_EXIT <-- "; else if (_substate_array[i] == EMERGENCY_CLIMB) string_substate = " -- > EMERGENCY_CLIMB <-- "; - pc().printf("Neutral Substate #%d: %d (%s)\n\r", i, _state_array[i], string_substate.c_str()); + pc().printf("Neutral Substate #%d: %d (%s)\r\n", i, _state_array[i], string_substate.c_str()); } - pc().printf("\n\r"); //make space between printouts + pc().printf("\r\n"); //make space between printouts } else if (user_input == '|') { pc().printf("taring depth sensor\r\n"); @@ -1191,23 +1309,38 @@ pc().printf(">>> timeout increased: %d\r\n", _timeout); } - // go to tuning sub-menu else if (user_input == '5') { - keyboard_menu_MANUAL_TUNING(); + keyboard_menu_RUDDER_SERVO_settings(); } else if (user_input == '6') { - keyboard_menu_STREAM_STATUS(); - } + keyboard_menu_HEADING_PID_settings(); + } + // go to tuning sub-menu else if (user_input == '7') { - keyboard_menu_RUDDER_SERVO_settings(); + keyboard_menu_MANUAL_TUNING(); } else if (user_input == '8') { - keyboard_menu_CHANNEL_READINGS(); + keyboard_menu_STREAM_STATUS(); //and channel readings for debugging } + +// else if (user_input == '8') { +// keyboard_menu_CHANNEL_READINGS(); +// } + else if (user_input == '9') { + _heading_command -= 5.0; //decrement the rudder setpoint + headingLoop().setCommand(_heading_command); + pc().printf(">>> (-) new HEADING setpoint: %0.3f deg (-)\r\n", headingLoop().getCommand()); + } + else if (user_input == '0') { + _heading_command += 5.0; //increment the rudder setpoint + headingLoop().setCommand(_heading_command); + pc().printf(">>> (+) new HEADING setpoint: %0.3f deg (+)\r\n", headingLoop().getCommand()); + } + else if (user_input == 'Y') { keyboard_menu_POSITION_READINGS(); } @@ -1227,7 +1360,7 @@ else if (user_input == 'C' or user_input == 'c') { - pc().printf("\n\n\rCURRENT STATUS AND PARAMETERS:\n\r"); + pc().printf("\r\n\nCURRENT STATUS AND PARAMETERS:\r\n"); // Testing out ADC float vref = 5.6; @@ -1256,12 +1389,12 @@ pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand()); pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand()); - pc().printf("\n\rNeutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm); + pc().printf("\r\nNeutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm); pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset()); pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset()); - pc().printf("Max recorded depth: neutral: %0.1f, dive: %0.1f, auto_neutral_depth: %0.1f\n\n\r",_max_recorded_depth_neutral, _max_recorded_depth_dive, _max_recorded_auto_neutral_depth); + pc().printf("Max recorded depth: neutral: %0.1f, dive: %0.1f, auto_neutral_depth: %0.1f\r\n\n",_max_recorded_depth_neutral, _max_recorded_depth_dive, _max_recorded_auto_neutral_depth); - pc().printf("\n\r"); + pc().printf("\r\n"); pc().printf("bce P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %6.1f mm, slope %0.5f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope()); pc().printf("batt P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %6.1f mm, slope %0.5f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope()); pc().printf("depth P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset()); @@ -1270,25 +1403,25 @@ //POSITION DIVE COMMANDS else if (user_input == 'k') { - _BCE_position_command -= 1.0; - pc().printf("Decreased BCE CMD to %0.1f\n\r", _BCE_position_command); + _BCE_dive_offset -= 1.0; + pc().printf("Decreased BCE dive offset to %0.1f\r\n", _BCE_dive_offset); } else if (user_input == 'l') { - _BCE_position_command += 1.0; - pc().printf("Increased BCE CMD to %0.1f\n\r", _BCE_position_command); + _BCE_dive_offset += 1.0; + pc().printf("Increased BCE dive offset to %0.1f\r\n", _BCE_dive_offset); } else if (user_input == ';') { - _BMM_position_command -= 1.0; - pc().printf("Decreased BMM CMD to %0.1f\n\r", _BMM_position_command); + _BMM_dive_offset -= 1.0; + pc().printf("Decreased BMM dive offset to %0.1f\r\n", _BMM_dive_offset); } else if (user_input == '\'') { - _BMM_position_command += 1.0; - pc().printf("Increased BMM CMD to %0.1f\n\r", _BMM_position_command); + _BMM_dive_offset += 1.0; + pc().printf("Increased BMM dive offset to %0.1f\r\n", _BMM_dive_offset); } //POSITION DIVE COMMANDS else if (user_input == '*') { - pc().printf("SWITCHING TO SIMPLE MENU!\n\r"); + pc().printf("SWITCHING TO SIMPLE MENU!\r\n"); wait(2); _debug_menu_on = false; } @@ -1297,8 +1430,8 @@ /***************************** SIMPLE MENU *****************************/ else { - if (user_input == 'D') { - _keyboard_state = DIVE; + if (user_input == 'V') { + _keyboard_state = POSITION_DIVE; } else if (user_input == 'N') { _keyboard_state = FIND_NEUTRAL; @@ -1323,10 +1456,62 @@ _keyboard_state = TRANSMIT_LOG; //Transmit data (work in progress) } else if (user_input == '~') { - pc().printf("ERASING MBED LOG FILE\n\r"); + pc().printf("ERASING MBED LOG FILE\r\n"); mbedLogger().eraseFile(); } + else if (user_input == 'C' or user_input == 'c') { + + pc().printf("\r\n\nCURRENT STATUS AND PARAMETERS:\r\n"); + + // Testing out ADC + float vref = 5.6; + float vmeasured = 0; + unsigned int raw = adc().readCh5(); + vmeasured = ((float)raw)/4095.0*vref; + pc().printf("raw BCE pos: %d \r\n",adc().readCh0()); + pc().printf("raw BMM pos: %d \r\n",adc().readCh1()); + pc().printf("raw BCE current sense: %d \r\n",adc().readCh2()); + pc().printf("raw BMM current sense: %d \r\n",adc().readCh3()); + pc().printf("raw depth pressure: %d \r\n",adc().readCh4()); + pc().printf("raw vessel pressure: %d \r\n",adc().readCh5()); + pc().printf("raw battery voltage: %d \r\n",adc().readCh6()); + pc().printf("raw board current: %d \r\n",adc().readCh7()); + pc().printf("raw BCE limit switch: %d \r\n",bce().getSwitch()); + pc().printf("raw BMM limit switch: %d \r\n",batt().getSwitch()); + pc().printf("raw vessel pressure: %f %d \r\n",vmeasured,raw); + // End of ADC Test + + pc().printf("depth: %3.1f ft\r\n",depthLoop().getPosition()); + pc().printf("pitch: %3.1f deg\r\n",imu().getPitch()); + pc().printf("bce().getPosition_mm(): %3.1f\r\n",bce().getPosition_mm()); + pc().printf("bce().getSetPosition_mm(): %3.1f\r\n",bce().getSetPosition_mm()); + pc().printf("batt().getPosition_mm(): %3.1f\r\n",batt().getPosition_mm()); + pc().printf("batt().getSetPosition_mm(): %3.1f\r\n",batt().getSetPosition_mm()); + pc().printf("depthLoop().getCommand(): %3.1f\r\n",depthLoop().getCommand()); + pc().printf("pitchLoop().getCommand(): %3.1f\r\n",pitchLoop().getCommand()); + + pc().printf("\r\nNeutral Buoyancy Positions: bce: %0.1f, batt: %0.1f\r\n",_neutral_bce_pos_mm,_neutral_batt_pos_mm); + pc().printf("depthLoop().getOutputOffset(): %0.1f\r\n",depthLoop().getOutputOffset()); + pc().printf("pitchLoop().getOutputOffset(): %0.1f\r\n",pitchLoop().getOutputOffset()); + pc().printf("Max recorded depth: neutral: %0.1f, dive: %0.1f, auto_neutral_depth: %0.1f\r\n\n",_max_recorded_depth_neutral, _max_recorded_depth_dive, _max_recorded_auto_neutral_depth); + + pc().printf("\r\n"); + pc().printf("bce P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %6.1f mm, slope %0.5f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope()); + pc().printf("batt P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %6.1f mm, slope %0.5f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope()); + pc().printf("depth P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset()); + pc().printf("pitch P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset()); + } + + else if (user_input == '-') { + _timeout -= 10.0; //decrement the timeout + pc().printf(">>> timeout decreased: %d\r\n", _timeout); + } + else if (user_input == '=' or user_input == '+') { + _timeout += 10.0; //increment the timeout + pc().printf(">>> timeout increased: %d\r\n", _timeout); + } + else if (user_input == '?') { pc().printf("\n\n\n>>> Resetting MBED <<<\n\n\n"); wait(0.5); @@ -1335,25 +1520,40 @@ //POSITION DIVE COMMANDS else if (user_input == 'k') { - _BCE_position_command -= 1.0; - pc().printf("Decreased BCE CMD to %0.1f\n\r", _BCE_position_command); + _BCE_dive_offset -= 1.0; + pc().printf("Decreased BCE dive offset to %0.1f\r\n", _BCE_dive_offset); } else if (user_input == 'l') { - _BCE_position_command += 1.0; - pc().printf("Increased BCE CMD to %0.1f\n\r", _BCE_position_command); + _BCE_dive_offset += 1.0; + pc().printf("Increased BCE dive offset to %0.1f\r\n", _BCE_dive_offset); } else if (user_input == ';') { - _BMM_position_command -= 1.0; - pc().printf("Decreased BMM CMD to %0.1f\n\r", _BMM_position_command); + _BMM_dive_offset -= 1.0; + pc().printf("Decreased BMM dive offset to %0.1f\r\n", _BMM_dive_offset); } else if (user_input == '\'') { - _BMM_position_command += 1.0; - pc().printf("Increased BMM CMD to %0.1f\n\r", _BMM_position_command); + _BMM_dive_offset += 1.0; + pc().printf("Increased BMM dive offset to %0.1f\r\n", _BMM_dive_offset); } //POSITION DIVE COMMANDS + + else if (user_input == '9') { + _heading_command -= 5.0; //decrement the rudder setpoint + headingLoop().setCommand(_heading_command); + pc().printf(">>> (-) new HEADING setpoint: %0.3f deg (-)\r\n", headingLoop().getCommand()); + } + else if (user_input == '0') { + _heading_command += 5.0; //increment the rudder setpoint + headingLoop().setCommand(_heading_command); + pc().printf(">>> (+) new HEADING setpoint: %0.3f deg (+)\r\n", headingLoop().getCommand()); + } + + else if (user_input == '8') { + keyboard_menu_STREAM_STATUS(); + } else if (user_input == '*') { - pc().printf("SWITCHING TO DEBUG MENU!\n\r"); + pc().printf("SWITCHING TO DEBUG MENU!\r\n"); _debug_menu_on = true; wait(2); } @@ -1362,7 +1562,7 @@ //when you read the keyboard successfully, change the state _state = _keyboard_state; //set state at the end of this function - //pc().printf("\n\n\r ********* KEYBOARD STATE: %d *********\n\n\r", _state); + //pc().printf("\r\n\n ********* KEYBOARD STATE: %d *********\r\n\n", _state); } } @@ -1370,7 +1570,7 @@ char STATUS_key; // show the menu - pc().printf("\n\r1: STATUS DEBUG MENU (EXIT WITH 'X' !)\n\r"); + pc().printf("\r\n8: STATUS DEBUG MENU (EXIT WITH 'X' !)\r\n"); while (1) { if (pc().readable()) { @@ -1381,7 +1581,11 @@ wait(1); - pc().printf("BCE POS (CMD): %0.1f (%0.1f) BATT POS: %0.1f (%0.1f) PRESS_psi: %0.2f (unfiltered: %0.2f) [depth_ft: %0.2f], PITCH: %0.2f, HEADING: %0.2f, rudder_servo_pwm: %0.1f\r\n",bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(),depth().getPsi(),depth().getRawPSI(),depthLoop().getPosition(),imu().getPitch(),imu().getHeading(),rudder().getPosition_pwm()); + + pc().printf("BCE POS (CMD): %0.1f (%0.1f) BATT POS: %0.1f (%0.1f) PRESS_psi: %0.2f [depth_ft: %0.2f], PITCH: %0.2f, HEADING: %0.2f, rudder_servo_pwm: %0.1f [0(%d),1(%d),2(%d),6(%d),4(%d),5(%d),6(%d),7(%d)]\r",bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(),depth().getPsi(),depthLoop().getPosition(),imu().getPitch(),imu().getHeading(),rudder().getPosition_pwm(),adc().readCh0(),adc().readCh1(),adc().readCh2(),adc().readCh3(),adc().readCh4(),adc().readCh5(),adc().readCh6(),adc().readCh7()); + //pc().printf("BCE POS (CMD): %0.1f (%0.1f) BATT POS: %0.1f (%0.1f) PRESS_psi: %0.2f (unfiltered: %0.2f) [depth_ft: %0.2f], PITCH: %0.2f, HEADING: %0.2f, rudder_servo_pwm: %0.1f\r",bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(),depth().getPsi(),depth().getRawPSI(),depthLoop().getPosition(),imu().getPitch(),imu().getHeading(),rudder().getPosition_pwm()); + //pc().printf("\n0(%d),1(%d),2(%d),6(%d),4(%d),5(%d),6(%d),7(%d)\r\n",adc().readCh0(),adc().readCh1(),adc().readCh2(),adc().readCh3(),adc().readCh4(),adc().readCh5(),adc().readCh6(),adc().readCh7()); + //pc().printf("(set) BCE POS: %0.1f (%0.1f) BATT POS: %0.1f (%0.1f) PRESS: %0.2f )(channel reading: %d) (volt: %0.2f) << %0.2f >> (Pressure: %0.2f (raw: %0.2f) PITCH: %0.2f HEADING: %0.2f)\r\n",bce().getPosition_mm(), bce().getSetPosition_mm(),batt().getPosition_mm(), batt().getSetPosition_mm(), depthLoop().getPosition(),depth().readADCCounts(),depth().readVoltage(),depth().getPsi(),depth().getRawPSI(),depth().getRawPSI(),imu().getPitch(),imu().getHeading()); @@ -1406,12 +1610,13 @@ } // process the keys - if (STATUS_key == 'X') { + if (STATUS_key == 'X') { + pc().printf("\r\nX: EXITING STATUS DEBUG MENU\r\n"); break; //exit the while loop } else { - pc().printf("\n\rThis key (%c) does nothing here. ", STATUS_key); + pc().printf("\r\nThis key (%c) does nothing here. ", STATUS_key); } } } @@ -1427,10 +1632,10 @@ char RUDDER_PID_key; // print the menu - pc().printf("\n\rRUDDER (servo driver) settings (MENU)"); - pc().printf("\n\r(Adjust min/max/center PWM settings with the following keys: N and M and C"); - pc().printf("\n\r(Adjust DEGREE limit settings with the following keys: min = K, max = L"); - pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); + pc().printf("\r\nRUDDER (servo driver) settings (MENU)"); + pc().printf("\r\n(Adjust min/max/center PWM settings with the following keys: N and M and C"); + pc().printf("\r\n(Adjust DEGREE limit settings with the following keys: min = K, max = L"); + pc().printf("\r\n(Hit shift + X to exit w/o saving. Hit shift + S to save.\r\n"); pc().printf("RUDDER min pwm: %f, max pwm: %f, center pwm: %f, min deg: %f, max deg: %f\r\n", rudder().getMinPWM(), rudder().getMaxPWM(), rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg()); // handle the key presses @@ -1463,27 +1668,27 @@ } // MIN PWM else if (RUDDER_PID_key == 'N') { - pc().printf(">> Type in rudder_min_pwm with keyboard.\n\r"); + pc().printf(">> Type in rudder_min_pwm with keyboard.\r\n"); rudder_min_pwm = getFloatUserInput(); } // MAX PWM else if (RUDDER_PID_key == 'M') { - pc().printf(">> Type in rudder_max_pwm with keyboard.\n\r"); + pc().printf(">> Type in rudder_max_pwm with keyboard.\r\n"); rudder_max_pwm = getFloatUserInput(); } // CENTER PWM else if (RUDDER_PID_key == 'C') { - pc().printf(">> Type in rudder_ctr_pwm with keyboard.\n\r"); + pc().printf(">> Type in rudder_ctr_pwm with keyboard.\r\n"); rudder_ctr_pwm = getFloatUserInput(); } // MIN DEG else if (RUDDER_PID_key == 'K') { - pc().printf(">> Type in rudder_min_deg with keyboard.\n\r"); + pc().printf(">> Type in rudder_min_deg with keyboard.\r\n"); rudder_min_deg = getFloatUserInput(); } // MAX DEG else if (RUDDER_PID_key == 'L') { - pc().printf(">> Type in rudder_max_deg with keyboard.\n\r"); + pc().printf(">> Type in rudder_max_deg with keyboard.\r\n"); rudder_max_deg = getFloatUserInput(); } @@ -1493,6 +1698,66 @@ } } +void StateMachine::keyboard_menu_HEADING_PID_settings() { + char HEADING_PID_key; + + float heading_KP = headingLoop().getControllerP(); + float heading_KI = headingLoop().getControllerI(); + float heading_KD = headingLoop().getControllerD(); + float heading_offset_deg = headingLoop().getOutputOffset(); + + // print the menu + pc().printf("\n\rHEADING (rudder outer loop) PID gain settings (MENU)"); + pc().printf("\n\r Adjust PID settings with the following keys: P and I and D"); + pc().printf("\n\r Adjust zero offset with O (oh)"); + pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); + pc().printf("HEADING P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", headingLoop().getControllerP(), headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset()); + + // handle the key presses + while(1) { + // get the user's keystroke from either of the two inputs + if (pc().readable()) { + HEADING_PID_key = pc().getc(); + } + else { + continue; // didn't get a user input, so keep waiting for it + } + + // handle the user's key input + if (HEADING_PID_key == 'S') { // user wants to save the modified values + // set global values + headingLoop().setControllerP(heading_KP); + headingLoop().setControllerI(heading_KI); + headingLoop().setControllerD(heading_KD); + headingLoop().setOutputOffset(heading_offset_deg); + + // save pitch PID values for outer loop (must save neutral position also) + configFileIO().saveHeadingData(heading_KP, heading_KI, heading_KD, heading_offset_deg); //_neutral_heading_pos_deg); + pc().printf("HEADING P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", headingLoop().getControllerP(), headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset()); + } + else if (HEADING_PID_key == 'X') { + break; //exit the while loop + } + + else if (HEADING_PID_key == 'P') { + heading_KP = getFloatUserInput();; + } + else if (HEADING_PID_key == 'I') { + heading_KI = getFloatUserInput(); + } + else if (HEADING_PID_key == 'D') { + heading_KD = getFloatUserInput(); + } + else if (HEADING_PID_key == 'O') { + heading_offset_deg = getFloatUserInput(); + } + + else { + pc().printf("HEADING SETUP: [%c] This key does nothing here. \r", HEADING_PID_key); + } + } +} + void StateMachine::keyboard_menu_COUNTS_STATUS() { } @@ -1501,9 +1766,9 @@ char TUNING_key; // show the menu - pc().printf("\n\r1: MANUAL TUNING MENU (EXIT WITH 'X' !) (Pause and Unpause rudder ticker with P and U\n"); - pc().printf("\n\r(Adjust BCE and BATT positions in real-time. Timeout NOT running! (decrease/increase BCE with A/S, BATT with Q/W, RUDDER with E/R)\n\r"); - pc().printf("\n\rMANUAL_TUNING: BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg, headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\r",bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(), imu().getHeading()); + pc().printf("\r\n7: MANUAL TUNING MENU (EXIT WITH 'X' !) (Pause and Unpause rudder ticker with P and U\n"); + pc().printf("\r\n(Adjust BCE and BATT positions in real-time. Timeout NOT running! (decrease/increase BCE with A/S, BATT with Q/W, RUDDER with E/R)\r\n"); + pc().printf("\r\nMANUAL_TUNING: BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg, headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\r",bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition(), headingLoop().getPosition(), imu().getHeading()); //made these into internal parameters float _tuning_bce_pos_mm = 300.0; //safe starting position @@ -1525,7 +1790,8 @@ TUNING_key = pc().getc(); //get each keystroke } - else { + else { + pc().printf("MANUAL_TUNING: (current stats) BCE pos: %3.1f mm (cmd: %3.1f mm), BATT pos: %3.1f mm (cmd: %3.1f mm) SERVO: %0.1f deg, %f pwm\r", bce().getPosition_mm(), bce().getSetPosition_mm(), batt().getPosition_mm(), batt().getSetPosition_mm(), rudder().getPosition_deg(), rudder().getPosition_pwm()); continue; // didn't get a user input, so keep waiting for it } @@ -1546,28 +1812,26 @@ else if (TUNING_key == 'A') { _tuning_bce_pos_mm = _tuning_bce_pos_mm - 1.0; bce().setPosition_mm(_tuning_bce_pos_mm); //this variable is loaded from the file at initialization - pc().printf("BCE _tuning_bce_pos_mm now: %0.1f\n\r", _tuning_bce_pos_mm); - pc().printf("\n\rMANUAL_TUNING: (BCE CHANGE: %0.1f)\n\r BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); + pc().printf("\r\nMANUAL_TUNING: (BCE CHANGE: %0.1f)\r\n BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); } else if (TUNING_key == 'S') { _tuning_bce_pos_mm = _tuning_bce_pos_mm + 1.0; bce().setPosition_mm(_tuning_bce_pos_mm); //this variable is loaded from the file at initialization - pc().printf("BCE _tuning_bce_pos_mm now: %0.1f\n\r", _tuning_bce_pos_mm); - pc().printf("\n\rMANUAL_TUNING: (BCE CHANGE: %0.1f)\n\r BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); + pc().printf("\r\nMANUAL_TUNING: (BCE CHANGE: %0.1f)\r\n BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); } //BATTERY else if (TUNING_key == 'Q') { _tuning_batt_pos_mm = _tuning_batt_pos_mm - 1.0; batt().setPosition_mm(_tuning_batt_pos_mm); //this variable is loaded from the file at initialization - pc().printf("\n\rMANUAL_TUNING: (BATT CHANGE: %0.1f)\n\r BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); + pc().printf("\r\nMANUAL_TUNING: (BATT CHANGE: %0.1f)\r\n BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); } else if (TUNING_key == 'W') { _tuning_batt_pos_mm = _tuning_batt_pos_mm + 1.0; batt().setPosition_mm(_tuning_batt_pos_mm); //this variable is loaded from the file at initialization - pc().printf("\n\rMANUAL_TUNING: (BATT CHANGE: %0.1f)\n\r BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); + pc().printf("\r\nMANUAL_TUNING: (BATT CHANGE: %0.1f)\r\n BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",batt().getSetPosition_mm(),bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); } else if (TUNING_key == 'c' or TUNING_key == 'C') { @@ -1578,13 +1842,13 @@ else if (TUNING_key == 'E') { _tuning_rudder_pos_deg = _tuning_rudder_pos_deg - 0.5; rudder().setPosition_deg(_tuning_rudder_pos_deg); - pc().printf("MT: RUDDER %0.1f [servo pwm: %f, %0.1f deg] (headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\n\r", _tuning_rudder_pos_deg, rudder().getPosition_pwm(), rudder().getPosition_deg(), headingLoop().getPosition(), imu().getHeading()); + pc().printf("MANUAL_TUNING: RUDDER CHANGE %0.1f deg [servo pwm: %f, %0.1f deg] (headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\r\n", _tuning_rudder_pos_deg, rudder().getPosition_pwm(), rudder().getPosition_deg(), headingLoop().getPosition(), imu().getHeading()); } else if (TUNING_key == 'R') { _tuning_rudder_pos_deg = _tuning_rudder_pos_deg + 0.5; rudder().setPosition_deg(_tuning_rudder_pos_deg); - pc().printf("MT: RUDDER %0.1f [servo pwm: %f, %0.1f deg] (headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\n\r", _tuning_rudder_pos_deg, rudder().getPosition_pwm(), rudder().getPosition_deg(), headingLoop().getPosition(), imu().getHeading()); + pc().printf("MANUAL_TUNING: RUDDER CHANGE %0.1f deg [servo pwm: %f, %0.1f deg] (headingLoop heading: % 0.1f deg, IMU heading: %0.1f deg)\r\n", _tuning_rudder_pos_deg, rudder().getPosition_pwm(), rudder().getPosition_deg(), headingLoop().getPosition(), imu().getHeading()); } else if (TUNING_key == 'P') { @@ -1596,7 +1860,7 @@ } else { - pc().printf("\n\rMANUAL_TUNING: [%c] This key does nothing here. \r", TUNING_key); + pc().printf("\r\nMANUAL_TUNING: [%c] This key does nothing here. \r", TUNING_key); } } } @@ -1605,8 +1869,8 @@ // char TUNING_key; // // // show the menu -// pc().printf("\n\r1: MANUAL TUNING MENU (EXIT WITH 'X' !)"); -// pc().printf("\n\r(Adjust BCE and BATT positions in real-time. Timeout NOT running! (decrease/increase BCE with A/S, BATT with Q/W)\n\r"); +// pc().printf("\r\n1: MANUAL TUNING MENU (EXIT WITH 'X' !)"); +// pc().printf("\r\n(Adjust BCE and BATT positions in real-time. Timeout NOT running! (decrease/increase BCE with A/S, BATT with Q/W)\r\n"); // pc().printf("MANUAL_TUNING: BCE_position: %0.1f, BATT_position: %0.1f (depth: %0.1f ft, pitch: %0.1f deg)\r",bce().getPosition_mm(),batt().getPosition_mm(),depthLoop().getPosition(),pitchLoop().getPosition()); // // // what needs to be started? @@ -1623,7 +1887,7 @@ // } // // else { -// pc().printf("MT: ACTUAL POS (SET POS): BCE: %0.1f (%0.1f), BATT: %0.1f (%0.1f)\n\r",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm()); +// pc().printf("MT: ACTUAL POS (SET POS): BCE: %0.1f (%0.1f), BATT: %0.1f (%0.1f)\r\n",bce().getPosition_mm(),bce().getSetPosition_mm(),batt().getPosition_mm(),batt().getSetPosition_mm()); // continue; // didn't get a user input, so keep waiting for it // } // @@ -1661,7 +1925,7 @@ // } // // else { -// pc().printf("\n\rThis key does nothing here. "); +// pc().printf("\r\nThis key does nothing here. "); // } // // } @@ -1671,7 +1935,7 @@ char TUNING_key; // show the menu - pc().printf("\n\r8: CHANNEL READINGS (EXIT WITH 'X' !)"); + pc().printf("\r\n8: CHANNEL READINGS (EXIT WITH 'X' !)"); while (1) { if (pc().readable()) { @@ -1699,7 +1963,7 @@ char TUNING_key; // show the menu - pc().printf("\n\r9: BCE and BMM POSITION READINGS (EXIT WITH 'X' !)"); + pc().printf("\r\n9: BCE and BMM POSITION READINGS (EXIT WITH 'X' !)"); while (1) { if (pc().readable()) { @@ -1755,9 +2019,9 @@ float BCE_KD = bce().getControllerD(); // load current global value // show the menu - pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)"); - pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); - pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.)\n\n\n\r"); + pc().printf("\r\n1: Buoyancy Engine PID gain settings (MENU)"); + pc().printf("\r\n(Adjust PID settings with the following keys: -= and [] and ;'"); + pc().printf("\r\n(Hit shift + X to exit w/o saving. Hit shift + S to save.)\r\n\n\n"); 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()); // handle the key presses @@ -1810,7 +2074,7 @@ break; //exit the while loop } else { - pc().printf("\n\rThis key does nothing here. "); + pc().printf("\r\nThis key does nothing here. "); } } } @@ -1823,9 +2087,9 @@ float batt_KD = batt().getControllerD(); // load current global value // print the menu - pc().printf("\n\r2: Battery Motor PID gain settings (MENU)"); - pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); - pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); + pc().printf("\r\n2: Battery Motor PID gain settings (MENU)"); + pc().printf("\r\n(Adjust PID settings with the following keys: -= and [] and ;'"); + pc().printf("\r\n(Hit shift + X to exit w/o saving. Hit shift + S to save.\r\n"); 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()); // handle the key presses @@ -1888,9 +2152,9 @@ float gain_step_size = 0.01; // modify this to change gain step size // show the menu - pc().printf("\n\r1: Buoyancy Engine PID gain settings (MENU)"); - pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); - pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\n\n\r"); + pc().printf("\r\n3: DEPTH outer loop PID gain settings (MENU)"); + pc().printf("\r\n(Adjust PID settings with the following keys: -= and [] and ;'"); + pc().printf("\r\n(Hit shift + X to exit w/o saving. Hit shift + S to save.\r\n\n\n"); 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()); // handle the key presses @@ -1942,7 +2206,7 @@ break; //exit the while loop } else { - pc().printf("\n\rThis key does nothing here. "); + pc().printf("\r\nThis key does nothing here. "); } } } @@ -1952,9 +2216,9 @@ float gain_step_size = 0.01; // modify this to change gain step size // print the menu - pc().printf("\n\r2: Battery Motor PID gain settings (MENU)"); - pc().printf("\n\r(Adjust PID settings with the following keys: -= and [] and ;'"); - pc().printf("\n\r(Hit shift + X to exit w/o saving. Hit shift + S to save.\n\r"); + pc().printf("\r\n4: Pitch outer loop PID gain settings (MENU)"); + pc().printf("\r\n(Adjust PID settings with the following keys: -= and [] and ;'"); + pc().printf("\r\n(Hit shift + X to exit w/o saving. Hit shift + S to save.\r\n"); 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()); // handle the key presses @@ -2055,7 +2319,7 @@ _neutral_batt_pos_mm = batt_pos_mm; _neutral_bce_pos_mm = bce_pos_mm; - pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\n\r",_neutral_batt_pos_mm,_neutral_bce_pos_mm); + pc().printf("Neutral Buoyancy Positions: batt: %0.1f, bce: %0.1f\r\n",_neutral_batt_pos_mm,_neutral_bce_pos_mm); } int StateMachine::timeoutRunning() { @@ -2074,7 +2338,7 @@ } void StateMachine::printCurrentSdLog() { - pc().printf("SD card log work in progress\n\r"); + pc().printf("SD card log work in progress\r\n"); //might be worth saving the last few logs to the MBED... } @@ -2092,12 +2356,12 @@ static bool is_transmit_timer_running = false; if (!is_transmit_timer_running) { - //pc().printf("\n\n\rTRANSMIT timer running...\n\n\r"); //debug + //pc().printf("\r\n\nTRANSMIT timer running...\r\n\n"); //debug transmit_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds is_transmit_timer_running = true; //disable this block after one iteration - pc().printf("TESTING to see if this transmits once a second. (timer: %0.1f)\n\r", timer.read()); + pc().printf("TESTING to see if this transmits once a second. (timer: %0.1f)\r\n", timer.read()); } if (timer.read() >= transmit_timer) { is_transmit_timer_running = false; // reset the sub state timer to do one-shot actions again @@ -2128,7 +2392,7 @@ string_state = "KEYBOARD"; if (!_is_log_timer_running) { - //pc().printf("\n\n\rlog timer running...\n\n\r"); //debug + //pc().printf("\r\n\nlog timer running...\r\n\n"); //debug _log_timer = timer.read() + 1; //record the time when this block is first entered and add 5 seconds _is_log_timer_running = true; //disable this block after one iteration @@ -2189,21 +2453,21 @@ while(1) { bool valid_input = false; //flag for valid or invalid input - pc().printf("Please enter your number below and press ENTER:\n\r"); + pc().printf("\n\rPlease enter your number below and press ENTER:\r\n"); char user_string [80]; //variable to store input as a character array pc().scanf("%s", user_string); //read formatted data from stdin - pc().printf("user_string was <%s>\n\r", user_string); + pc().printf("\n\n\ruser_string was <%s>\r\n", user_string); //check through the string for invalid characters (decimal values 43 through 57) for (int c = 0; c < strlen(user_string); c++) { - //pc().printf("character is [%c]\n\r", user_string[c]); //debug + //pc().printf("character is [%c]\r\n", user_string[c]); //debug if (user_string[c] >= 43 and user_string[c] <= 57) { - //pc().printf("VALID CHARACTER!\n\r"); //debug + //pc().printf("VALID CHARACTER!\r\n"); //debug ; } else { - pc().printf("INVALID INPUT!\n\r"); + pc().printf("INVALID INPUT!\r\n"); break; } @@ -2214,7 +2478,7 @@ if (valid_input) { float_conversion = atof(user_string); - pc().printf("VALID INPUT! Your input was: %f\n\r", float_conversion); + pc().printf("VALID INPUT! Your input was: %f\r\n", float_conversion); break; } }