most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
tnhnrl
Date:
Thu Dec 21 16:07:47 2017 +0000
Revision:
37:357e98a929cc
Parent:
36:966a86937e17
Child:
38:83d06c294807
RevA working without batt and BCE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danstrider 10:085ab7328054 1 /*
tnhnrl 24:c7d9b5bf3829 2 Modified 2017-11-29 revA by Troy
tnhnrl 24:c7d9b5bf3829 3 - changelog.txt carries previous changes
tnhnrl 24:c7d9b5bf3829 4 - Incorporated email changes and did quick bench test
tnhnrl 28:16c83a2fdefa 5 1) In FIND_NEUTRAL, add bce().setPosition_mm(bceFloatPosition) to the one-shot actions after the unpause().
tnhnrl 28:16c83a2fdefa 6 2) NEUTRAL_FIRST_PITCH, changed batt position by half from 1.0 to 0.5.
tnhnrl 28:16c83a2fdefa 7 3) NEUTRAL_SINKING and NEUTRAL_SLOWLY_RISE are using getSetPosition now
tnhnrl 28:16c83a2fdefa 8 4) previousPosition_mm deleted (and print statements changed)
tnhnrl 28:16c83a2fdefa 9 5) NEUTRAL_SLOWLY_RISE check is using bce().getSetPosition_mm()
tnhnrl 28:16c83a2fdefa 10 6) Timers replaced with _neutral_timer variable
tnhnrl 28:16c83a2fdefa 11 7) Default timeout is 8 minutes = 480 seconds
tnhnrl 24:c7d9b5bf3829 12 - Redid the sub-state recording method to only update on state changes
tnhnrl 27:0a5b90cd65d6 13 Modified 2017-11-29 revB by Troy
tnhnrl 27:0a5b90cd65d6 14 - minor print fixes
tnhnrl 28:16c83a2fdefa 15 Modified 2017-11-29 revC by Troy
tnhnrl 28:16c83a2fdefa 16 - removed depthTolerance variable that was not being used.
tnhnrl 28:16c83a2fdefa 17 Modified 2017-11-30 revA by Troy/Dan
tnhnrl 28:16c83a2fdefa 18 - Modified state RISE to setCommand to -1.0 feet (force out of water) and go to next state (float level) when above 0.5 feet
tnhnrl 28:16c83a2fdefa 19 - Modified sub-state NEUTRAL_SINKING to only move 2.5 mm per cycle instead of 5 mm
tnhnrl 28:16c83a2fdefa 20 - Commented out home function (accidentally hit home and got stuck while at LASR)
tnhnrl 28:16c83a2fdefa 21 - Added max depth neutral and dive variables
tnhnrl 28:16c83a2fdefa 22 - Print max recorded depths and current neutral buoyancy positions
tnhnrl 28:16c83a2fdefa 23 Modified 2017-11-30 revB by Troy/Dan
tnhnrl 28:16c83a2fdefa 24 - Dive and Multi-Dive cycles now exit to FLOAT_BROADCAST state
tnhnrl 28:16c83a2fdefa 25 - FLOAT_LEVEL pitchTolerance variable increased from 1.0 degree to 5.0 degrees (PV was difficult to level in LASR pool)
tnhnrl 28:16c83a2fdefa 26 - Added the ability to record the states of the FSM and print to user using keyboard "Z"
tnhnrl 28:16c83a2fdefa 27 Modified 2017-12-01 revA by Troy
tnhnrl 28:16c83a2fdefa 28 - Minor update to 2017-11-30 revB
tnhnrl 28:16c83a2fdefa 29 - Now printing current neutral battery and BCE setpoints and max depth in neutral and dive cycle with "C" (and current sensor data)
tnhnrl 28:16c83a2fdefa 30 - Pool-tested with successful dives.
tnhnrl 28:16c83a2fdefa 31 1) Dive working correctly. (And max dive tracker shows that it is reaching the correct commanded depth.)
tnhnrl 28:16c83a2fdefa 32 2) Float broadcast working correctly. (And we are now going directly from dive and rise to float broadcast.)
tnhnrl 28:16c83a2fdefa 33 3) Neutral sequence has PV diving with -10 to -20 degrees of pitch, battery command seems to be slow or inactive.
tnhnrl 28:16c83a2fdefa 34 3) Float broadcast working correctly.
tnhnrl 28:16c83a2fdefa 35 4) FSM tracker and sub-FSM tracker working.
tnhnrl 28:16c83a2fdefa 36 5) Antenna working until tail just under water (last depth recorded varies between 1.0 and 1.5 feet)
tnhnrl 30:2964617e7676 37 Modified 2017-12-06 revA by Troy
tnhnrl 30:2964617e7676 38 - Added "_isTimeoutRunning = false;" to the FIND_NEUTRAL successful exit case ("else if (runNeutralStateMachine() == NEUTRAL_EXIT)"
tnhnrl 30:2964617e7676 39 1) This will reset the timeout when the PV has successfully found and saved the neutral BCE and battery positions
tnhnrl 30:2964617e7676 40 - Successful dive and multi-dive sequences exit to FLOAT_BROADCAST state (fix was implemented in code that did not respond, reimplemented here)
tnhnrl 30:2964617e7676 41 1) Also made the change to the SequenceController (which loads a file that has multiple sequences)
tnhnrl 31:8616e397c22d 42 - FIND_NEUTRAL starts in NEUTRAL_SINKING with fix on sub-FSM to start with NEUTRAL_SINKING (verified via mbed on bench)
tnhnrl 32:f2f8ae34aadc 43 Modified 2017-12-07 revA by Troy
tnhnrl 32:f2f8ae34aadc 44 - Using new neutral finding sequence, dive slowly, rise slowly, then check pitch (pitch is not active at all until final state)
tnhnrl 32:f2f8ae34aadc 45 - Added additional print information (showing neutral parameters with "C")
tnhnrl 32:f2f8ae34aadc 46 Modified 2017-12-07 revB by Troy
tnhnrl 32:f2f8ae34aadc 47 - Momentum offsets reintroduced:
tnhnrl 32:f2f8ae34aadc 48 1) CASE DIVE uses "else if (depthLoop().getPosition() > depthLoop().getCommand() - 0.5) { // including offset for low momentum approaches"
tnhnrl 32:f2f8ae34aadc 49 2) CASE RISE uses "else if (depthLoop().getPosition() < depthLoop().getCommand() + 0.5) { // including offset for low momentum approaches"
tnhnrl 32:f2f8ae34aadc 50 - Momentum on rise command does not exit properly, removed
tnhnrl 32:f2f8ae34aadc 51 Modified 2017-12-11 revA by Troy
tnhnrl 32:f2f8ae34aadc 52 - Added a logger function the main while loop
tnhnrl 32:f2f8ae34aadc 53 - Added vectors (essentially resizable arrays) to hold the data in the State Machine because of memory limitations
tnhnrl 32:f2f8ae34aadc 54 Modified 2017-12-12 revA by Troy
tnhnrl 32:f2f8ae34aadc 55 - Logger directly implemented in StateMachine (records every 5 seconds)
tnhnrl 32:f2f8ae34aadc 56 Modified 2017-12-12 revB by Troy
tnhnrl 32:f2f8ae34aadc 57 - Logger fixes implemented in StateMachine, was not logging each state, fixed the reset
tnhnrl 32:f2f8ae34aadc 58 - Made logger into a function call
tnhnrl 32:f2f8ae34aadc 59 Modified 2017-12-13 revA by Troy
tnhnrl 32:f2f8ae34aadc 60 - Added function to print log file to screen (tested on bench with multiple dives and timers)
tnhnrl 32:f2f8ae34aadc 61 Modified 2017-12-18 revA by Troy
tnhnrl 32:f2f8ae34aadc 62 - Redo of the code to stop opening and closing files each time
tnhnrl 32:f2f8ae34aadc 63 - This should allow you to write to the open file, then close it once you exit the sequence back to SIT_IDLE
tnhnrl 32:f2f8ae34aadc 64 - Start record in dive, end when you exit float broadcast...
tnhnrl 32:f2f8ae34aadc 65 Modified 2017-12-19 revA by Troy
tnhnrl 32:f2f8ae34aadc 66 - Fixed OpenLog printing to include states and variable names. Currently logs to LOG#####.TXT files
tnhnrl 32:f2f8ae34aadc 67 1) Note: The OpenLog only starts a new log when it is power-cycled (with the MBED)
tnhnrl 34:9b66c5188051 68 Modified 2017-12-20 revA by Troy
tnhnrl 32:f2f8ae34aadc 69 - Modified code to log every 1 second in current iteration
tnhnrl 34:9b66c5188051 70 Modified 2017-12-20 revB by Troy
tnhnrl 34:9b66c5188051 71 - Fixed bug where Dive depth was resetting to rise depth
tnhnrl 37:357e98a929cc 72 Modified 2017-12-21 revA by Troy
tnhnrl 37:357e98a929cc 73 - 2 minute timeout default
tnhnrl 37:357e98a929cc 74 - add system time to MBED logger
tnhnrl 37:357e98a929cc 75 - Fixed bug where it was recording random keyboard presses (other FSM states)
tnhnrl 37:357e98a929cc 76 - WORK IN PROGRESS to fix the batt and BCE PID saving
danstrider 10:085ab7328054 77 */
tnhnrl 20:8987a9ae2bc7 78
tzyoung 0:ea293bbf9717 79 #include "mbed.h"
tzyoung 0:ea293bbf9717 80 #include "StaticDefs.hpp"
tnhnrl 20:8987a9ae2bc7 81
tnhnrl 32:f2f8ae34aadc 82 // loop rate used to determine how fast events trigger in the while loop
tnhnrl 20:8987a9ae2bc7 83 Ticker loop_rate_ticker;
tnhnrl 34:9b66c5188051 84 Ticker log_loop_rate_ticker;
tnhnrl 32:f2f8ae34aadc 85
tnhnrl 32:f2f8ae34aadc 86 volatile bool loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)
tnhnrl 34:9b66c5188051 87 volatile bool log_loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)
tnhnrl 32:f2f8ae34aadc 88
tnhnrl 32:f2f8ae34aadc 89 void loop_trigger() { loop = true;} // loop trigger (used in while loop)
tnhnrl 34:9b66c5188051 90 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
tnhnrl 32:f2f8ae34aadc 91
danstrider 10:085ab7328054 92 void setup() {
danstrider 11:3b241ecb75ed 93 pc().baud(57600);
tnhnrl 34:9b66c5188051 94 pc().printf("\n\n\rFSG POOL TEST 2017-12-20 revB (DESKTOP SAVE TEST)\n\n\r");
tnhnrl 32:f2f8ae34aadc 95
tnhnrl 32:f2f8ae34aadc 96 //setup data logger baud rate and write the start of the program (every time you reset)
tnhnrl 32:f2f8ae34aadc 97 datalogger().baud(57600);
tnhnrl 32:f2f8ae34aadc 98 datalogger().printf("DATA, LOGGER, START\n");
tnhnrl 20:8987a9ae2bc7 99
danstrider 10:085ab7328054 100 // start up the system timer
tzyoung 0:ea293bbf9717 101 systemTime().start();
tnhnrl 20:8987a9ae2bc7 102
danstrider 10:085ab7328054 103 // set up and start the adc. This runs on a fixed interval and is interrupt driven
tzyoung 0:ea293bbf9717 104 adc().initialize();
tzyoung 0:ea293bbf9717 105 adc().start();
danstrider 10:085ab7328054 106
danstrider 10:085ab7328054 107 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 108 imu().initialize();
danstrider 10:085ab7328054 109 imu().start();
danstrider 10:085ab7328054 110
danstrider 10:085ab7328054 111 // set up the depth sensor. This is an internal ADC read, but eventually will be on the ltc1298
danstrider 14:85b64a4d08e8 112 depth().init();
danstrider 14:85b64a4d08e8 113 depth().tare();
danstrider 10:085ab7328054 114
danstrider 10:085ab7328054 115 // construct a local file system
danstrider 10:085ab7328054 116 local();
tnhnrl 20:8987a9ae2bc7 117
danstrider 10:085ab7328054 118 // load config data from files
tnhnrl 21:38c8544db6f4 119 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 21:38c8544db6f4 120 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 21:38c8544db6f4 121 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 21:38c8544db6f4 122 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 20:8987a9ae2bc7 123
danstrider 10:085ab7328054 124 // set up the linear actuators. adc has to be running first.
danstrider 10:085ab7328054 125 bce().init();
danstrider 10:085ab7328054 126 bce().start();
mkelly10 12:a0519d11d2b6 127 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 128
danstrider 10:085ab7328054 129 batt().init();
tzyoung 2:892b58e56712 130 batt().start();
mkelly10 12:a0519d11d2b6 131 batt().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 132
danstrider 10:085ab7328054 133 // set up the depth and pitch outer loop controllers
danstrider 10:085ab7328054 134 depthLoop().init();
tnhnrl 13:84fcbe1dcd62 135 depthLoop().start();
tnhnrl 16:3363b9f14913 136 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 137
danstrider 10:085ab7328054 138 pitchLoop().init();
tnhnrl 13:84fcbe1dcd62 139 pitchLoop().start();
tnhnrl 16:3363b9f14913 140 pitchLoop().setCommand(stateMachine().getPitchCommand());
tnhnrl 20:8987a9ae2bc7 141
tnhnrl 20:8987a9ae2bc7 142 // show that the PID gains are loading from the file
tnhnrl 21:38c8544db6f4 143 pc().printf("bce P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %3.0f mm, slope %3.3f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
tnhnrl 21:38c8544db6f4 144 pc().printf("batt P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %3.0f mm, slope %3.3f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
tnhnrl 21:38c8544db6f4 145 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());
tnhnrl 21:38c8544db6f4 146 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());
danstrider 10:085ab7328054 147 pc().printf("\n\r");
tnhnrl 21:38c8544db6f4 148
tnhnrl 17:7c16b5671d0e 149 //load sequence from file
tnhnrl 17:7c16b5671d0e 150 sequenceController().loadSequence();
tnhnrl 20:8987a9ae2bc7 151
tnhnrl 20:8987a9ae2bc7 152 // establish the main loop rate
tnhnrl 20:8987a9ae2bc7 153 loop_rate_ticker.attach(&loop_trigger, 0.1); // fires the ticker at 10 Hz rate
tnhnrl 32:f2f8ae34aadc 154
tnhnrl 32:f2f8ae34aadc 155 // setup the data logger rate
tnhnrl 34:9b66c5188051 156 log_loop_rate_ticker.attach(&log_loop_trigger, 1.0); // fires the ticker at 1 Hz rate (every second)
tnhnrl 32:f2f8ae34aadc 157
tnhnrl 35:2f66ea4863d5 158 //create the MBED log file (current log file)
tnhnrl 36:966a86937e17 159 //mbedLogger().openFile();
tnhnrl 21:38c8544db6f4 160 }
tnhnrl 20:8987a9ae2bc7 161
danstrider 10:085ab7328054 162 int main() {
danstrider 10:085ab7328054 163 setup();
tnhnrl 16:3363b9f14913 164
danstrider 10:085ab7328054 165 while(1) {
tnhnrl 34:9b66c5188051 166 static int current_state = 0;
tnhnrl 36:966a86937e17 167
tnhnrl 36:966a86937e17 168 static bool file_opened = false;
tnhnrl 36:966a86937e17 169
tnhnrl 32:f2f8ae34aadc 170 // runs at 10 hz
tnhnrl 20:8987a9ae2bc7 171 if(loop) {
tnhnrl 32:f2f8ae34aadc 172 led1() = !led1(); // blink led 1
tnhnrl 34:9b66c5188051 173
tnhnrl 34:9b66c5188051 174 //running State Machine. Returns 0 if sitting idle (SIT_IDLE state).
tnhnrl 34:9b66c5188051 175 current_state = stateMachine().runStateMachine();
tnhnrl 34:9b66c5188051 176
tnhnrl 20:8987a9ae2bc7 177 loop = false; // wait until the loop rate timer fires again
tnhnrl 17:7c16b5671d0e 178 }
tnhnrl 32:f2f8ae34aadc 179
tnhnrl 34:9b66c5188051 180 //runs at 1 hz
tnhnrl 34:9b66c5188051 181 if (log_loop) {
tnhnrl 34:9b66c5188051 182 led3() = !led3(); // blink led 3
tnhnrl 34:9b66c5188051 183
tnhnrl 36:966a86937e17 184 //pc().printf("led 3 blinking once a second?\n\r"); //confirmed
tnhnrl 36:966a86937e17 185
tnhnrl 37:357e98a929cc 186 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
tnhnrl 37:357e98a929cc 187 // if(current_state != 0 or current_state != -1) {
tnhnrl 34:9b66c5188051 188 if(current_state != 0) {
tnhnrl 36:966a86937e17 189 //pc().printf("\n\rDEBUG: trying to record data \n\r");
tnhnrl 36:966a86937e17 190
tnhnrl 37:357e98a929cc 191 //if the log file is not open, open it
tnhnrl 36:966a86937e17 192 if (!file_opened) {
tnhnrl 36:966a86937e17 193 mbedLogger().openFile(); //open MBED file once
tnhnrl 36:966a86937e17 194 file_opened = true; //stops it from continuing to open it
tnhnrl 37:357e98a929cc 195
tnhnrl 36:966a86937e17 196 pc().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
tnhnrl 36:966a86937e17 197 }
tnhnrl 36:966a86937e17 198
tnhnrl 36:966a86937e17 199 //record to OpenLog hardware
tnhnrl 36:966a86937e17 200 OpenLog().recordData(current_state); //start recording
tnhnrl 36:966a86937e17 201
tnhnrl 36:966a86937e17 202 //record to Mbed
tnhnrl 36:966a86937e17 203 mbedLogger().saveDataToFile(current_state,stateMachine().dataArray());
tnhnrl 36:966a86937e17 204 }
tnhnrl 36:966a86937e17 205
tnhnrl 36:966a86937e17 206 //when the current state is zero, reset the file
tnhnrl 36:966a86937e17 207 else {
tnhnrl 36:966a86937e17 208 //this can only happen once
tnhnrl 36:966a86937e17 209 if (file_opened) {
tnhnrl 36:966a86937e17 210 mbedLogger().closeFile();
tnhnrl 36:966a86937e17 211 file_opened = false;
tnhnrl 37:357e98a929cc 212
tnhnrl 37:357e98a929cc 213 pc().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 36:966a86937e17 214 }
tnhnrl 34:9b66c5188051 215 }
tnhnrl 34:9b66c5188051 216
tnhnrl 34:9b66c5188051 217 log_loop = false; // wait until the loop rate timer fires again
tnhnrl 34:9b66c5188051 218 }
danstrider 10:085ab7328054 219 }
danstrider 10:085ab7328054 220 }