update with altimeter, swimfile.txt endleg.txt, etc see changes_13sep.txt also reset_PI()

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
joel_ssc
Date:
Fri Sep 13 19:15:40 2019 +0000
Revision:
104:426224a55f5f
Parent:
102:0f430de62447
slight change  includes reset_PI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tzyoung 0:ea293bbf9717 1 #include "mbed.h"
tzyoung 0:ea293bbf9717 2 #include "StaticDefs.hpp"
joel_ssc 104:426224a55f5f 3 //Raspberry Pi reset pin set high at beginning of code
joel_ssc 104:426224a55f5f 4 DigitalOut reset_pin(p11,1);
tnhnrl 65:2ac186553959 5
tnhnrl 65:2ac186553959 6 ////////////////////////////////////////////////////////////////// NEW TICKER
tnhnrl 65:2ac186553959 7 Ticker systemTicker;
tnhnrl 65:2ac186553959 8 bool setup_complete = false;
tnhnrl 65:2ac186553959 9 volatile unsigned int bTick = 0;
tnhnrl 65:2ac186553959 10 volatile unsigned int timer_counter = 0;
tnhnrl 65:2ac186553959 11
tnhnrl 65:2ac186553959 12 static unsigned int read_ticker(void) { //Basically this makes sure you're reading the data at one instance (not while it's changing)
tnhnrl 65:2ac186553959 13 unsigned int val = bTick;
tnhnrl 65:2ac186553959 14 if( val )
tnhnrl 65:2ac186553959 15 bTick = 0;
tnhnrl 65:2ac186553959 16 return( val );
tnhnrl 65:2ac186553959 17 }
tnhnrl 65:2ac186553959 18 ////////////////////////////////////////////////////////////////// NEW TICKER
tnhnrl 32:f2f8ae34aadc 19
tnhnrl 65:2ac186553959 20 volatile bool fsm_loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)
tnhnrl 34:9b66c5188051 21 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 22
tnhnrl 65:2ac186553959 23 void loop_trigger() { fsm_loop = true;} // loop trigger (used in while loop)
tnhnrl 34:9b66c5188051 24 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
tnhnrl 32:f2f8ae34aadc 25
joel_ssc 82:0981b9ada820 26 static int current_state = 0;
joel_ssc 87:6d95f853dab3 27 static int have_legfile = 0;
joel_ssc 87:6d95f853dab3 28
joel_ssc 87:6d95f853dab3 29 static int save_state = 0; // state to hold while find_neutral runs ( if necessary)
joel_ssc 87:6d95f853dab3 30 int begin_state = 0;
joel_ssc 87:6d95f853dab3 31 static int neutral_via_leg = 0; // flag for entry of find_neutral via leg_pos and setval==0
joel_ssc 87:6d95f853dab3 32 static int setval = -1; // has neutral been set, found in neutral.txt
joel_ssc 86:ba3a118b0080 33
tnhnrl 65:2ac186553959 34 static bool file_opened = false;
tnhnrl 65:2ac186553959 35
tnhnrl 73:f6f378311c8d 36 void FSM() { // FSM loop runs at 10 hz
tnhnrl 65:2ac186553959 37 if(fsm_loop) {
tnhnrl 73:f6f378311c8d 38 // led one removed
tnhnrl 65:2ac186553959 39 fsm_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 40 current_state = stateMachine().runStateMachine(); //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
tnhnrl 65:2ac186553959 41 }
tnhnrl 65:2ac186553959 42 }
tnhnrl 65:2ac186553959 43
joel_ssc 102:0f430de62447 44 void log_function(int option) {
tnhnrl 65:2ac186553959 45 // log loop runs at 1 hz
joel_ssc 102:0f430de62447 46 // option =1 normal short output
joel_ssc 102:0f430de62447 47 // option = 2 longer output, state name and actual time value, etc
joel_ssc 82:0981b9ada820 48 if (log_loop) { //start logloop8
tnhnrl 65:2ac186553959 49 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
joel_ssc 88:1813f583cee9 50 if((current_state != SIT_IDLE) && (current_state != FLYING_IDLE)) { //first if - not in sit_idle/keyboard state
tnhnrl 65:2ac186553959 51 if (!file_opened) { //if the log file is not open, open it
joel_ssc 102:0f430de62447 52 mbedLogger().appendLogFile(current_state, option); //open MBED file once
tnhnrl 80:4e5d306d695b 53 //sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 65:2ac186553959 54
tnhnrl 65:2ac186553959 55 file_opened = true; //stops it from continuing to open it
tnhnrl 56:48a8a5a65b82 56
joel_ssc 82:0981b9ada820 57 xbee().printf(">>>>>>>> Recording. Log file re opened. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 58 }
joel_ssc 82:0981b9ada820 59 else {
tnhnrl 65:2ac186553959 60
joel_ssc 82:0981b9ada820 61 //just record to the Mbed file system
tnhnrl 65:2ac186553959 62
joel_ssc 102:0f430de62447 63 mbedLogger().appendLogFile(current_state, option); //writing data
joel_ssc 82:0981b9ada820 64 //sdLogger().appendLogFile(current_state, 1); //writing data
joel_ssc 82:0981b9ada820 65 }
joel_ssc 82:0981b9ada820 66 } //end first if
tnhnrl 65:2ac186553959 67
tnhnrl 65:2ac186553959 68 //when the current FSM state is zero (SIT_IDLE), close the file
joel_ssc 82:0981b9ada820 69 else { //start first else for current_state ==0 sit idle/keyboard
tnhnrl 65:2ac186553959 70 //this can only happen once
tnhnrl 65:2ac186553959 71 if (file_opened) {
tnhnrl 67:c86a4b464682 72 //WRITE ONCE
joel_ssc 102:0f430de62447 73 mbedLogger().appendLogFile(current_state, option); //write the idle state, then close
tnhnrl 80:4e5d306d695b 74 //sdLogger().appendLogFile(current_state, 1); //write the idle state, then close
tnhnrl 67:c86a4b464682 75
tnhnrl 65:2ac186553959 76 mbedLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 80:4e5d306d695b 77 //sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 65:2ac186553959 78
tnhnrl 65:2ac186553959 79 file_opened = false;
tnhnrl 65:2ac186553959 80
tnhnrl 74:d281aaef9766 81 xbee().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 82 }
joel_ssc 82:0981b9ada820 83 } //end first else
tnhnrl 74:d281aaef9766 84 } //END OF LOG LOOP8
tnhnrl 65:2ac186553959 85
tnhnrl 65:2ac186553959 86 log_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 87 }
tnhnrl 56:48a8a5a65b82 88
tnhnrl 72:250b2665755c 89 //single system timer to run hardware/electronics timing
tnhnrl 65:2ac186553959 90 static void system_timer(void) {
tnhnrl 65:2ac186553959 91 bTick = 1;
tnhnrl 65:2ac186553959 92
tnhnrl 56:48a8a5a65b82 93 timer_counter++;
tnhnrl 56:48a8a5a65b82 94
tnhnrl 56:48a8a5a65b82 95 //only start these updates when everything is properly setup (through setup function)
tnhnrl 56:48a8a5a65b82 96 if (setup_complete) {
tnhnrl 74:d281aaef9766 97 if ( (timer_counter % 5) == 0) { //this runs at 0.005 second intervals (200 Hz)
tnhnrl 65:2ac186553959 98 adc().update(); //every iteration of this the A/D converter runs //now this runs at 0.01 second intervals 03/12/2018
tnhnrl 65:2ac186553959 99 }
tnhnrl 65:2ac186553959 100
tnhnrl 65:2ac186553959 101 if ( (timer_counter % 10) == 0) {
tnhnrl 65:2ac186553959 102 bce().update(); //update() inside LinearActuator class (running at 0.01 second intervals)
tnhnrl 65:2ac186553959 103 batt().update();
tnhnrl 65:2ac186553959 104 }
tnhnrl 65:2ac186553959 105
tnhnrl 65:2ac186553959 106 if ( (timer_counter % 20) == 0 ) { // 0.02 second intervals
tnhnrl 74:d281aaef9766 107 rudder().runServo();
tnhnrl 65:2ac186553959 108 }
tnhnrl 65:2ac186553959 109
tnhnrl 65:2ac186553959 110 if ( (timer_counter % 50) == 0 ) { // 0.05 second intervals
tnhnrl 67:c86a4b464682 111 imu().runIMU();
tnhnrl 65:2ac186553959 112 }
tnhnrl 73:f6f378311c8d 113
tnhnrl 65:2ac186553959 114 if ( (timer_counter % 100) == 0) { // 100,000 microseconds = 0.1 second intervals
tnhnrl 65:2ac186553959 115 depthLoop().runOuterLoop();
tnhnrl 65:2ac186553959 116 pitchLoop().runOuterLoop();
tnhnrl 65:2ac186553959 117 headingLoop().runOuterLoop();
joel_ssc 87:6d95f853dab3 118 altimLoop().runOuterLoop();
tnhnrl 65:2ac186553959 119 }
tnhnrl 73:f6f378311c8d 120
tnhnrl 73:f6f378311c8d 121 if ( (timer_counter % 1000) == 0) { // update at 1.0 second intervals
tnhnrl 73:f6f378311c8d 122 //gui().updateGUI();
tnhnrl 73:f6f378311c8d 123 }
tnhnrl 74:d281aaef9766 124
tnhnrl 74:d281aaef9766 125 if ( (timer_counter % 30000) == 0) { // update at 30.0 second intervals
tnhnrl 74:d281aaef9766 126 //pc().printf("XB!\n");
tnhnrl 74:d281aaef9766 127 }
tnhnrl 56:48a8a5a65b82 128 }
tnhnrl 56:48a8a5a65b82 129 }
joel_ssc 104:426224a55f5f 130 void reset_Pi(){
joel_ssc 104:426224a55f5f 131 reset_pin = 0;
joel_ssc 104:426224a55f5f 132 wait_ms(20);
joel_ssc 104:426224a55f5f 133 reset_pin = 1;
joel_ssc 104:426224a55f5f 134 xbee().printf("\n\rWaking up Raspberry PI\n\r");
joel_ssc 104:426224a55f5f 135 }
danstrider 10:085ab7328054 136 void setup() {
joel_ssc 82:0981b9ada820 137 // xbee().baud(115200); // comment out so default is 9600 for USB communications
joel_ssc 102:0f430de62447 138 xbee().printf("\n\n\r 2019-13-09 FSG PCB XBee\n\n\r");
tnhnrl 74:d281aaef9766 139
tnhnrl 74:d281aaef9766 140 //pc().baud(57600);
tnhnrl 65:2ac186553959 141
danstrider 10:085ab7328054 142 // start up the system timer
tnhnrl 65:2ac186553959 143 //systemTimer().start();
tnhnrl 20:8987a9ae2bc7 144
danstrider 10:085ab7328054 145 // set up and start the adc. This runs on a fixed interval and is interrupt driven
tnhnrl 65:2ac186553959 146 adc().initialize();
tnhnrl 67:c86a4b464682 147 //one central interrupt is updating the ADC (not using the start function)
tnhnrl 65:2ac186553959 148
tnhnrl 65:2ac186553959 149 // setup and run the rudder(servo) pwm signal (start the ticker)
tnhnrl 65:2ac186553959 150 //rudder().init();
tnhnrl 74:d281aaef9766 151 xbee().printf("Rudder servo initialized!\n\r");
danstrider 10:085ab7328054 152
danstrider 10:085ab7328054 153 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 154 imu().initialize();
tnhnrl 65:2ac186553959 155 //imu().start();
danstrider 10:085ab7328054 156
tnhnrl 48:20e681885161 157 // construct the MBED local file system
danstrider 10:085ab7328054 158 local();
tnhnrl 48:20e681885161 159
tnhnrl 79:3688c3a0d7f4 160 // construct the SD card file system TEST 10/23/18
joel_ssc 97:2b4f78a54227 161 // need an if test that the sd fileststem setup in staticdefs.cpp succeeded, THEN use the sd filesystem
tnhnrl 80:4e5d306d695b 162 //sd_card();
joel_ssc 82:0981b9ada820 163 // mbedLogger().initializeDiagFile();
danstrider 10:085ab7328054 164 // load config data from files
joel_ssc 86:ba3a118b0080 165 configFileIO().load_StartTime();
joel_ssc 86:ba3a118b0080 166 // mbedLogger().setLogTime(); replaced by call in configfileIO
joel_ssc 82:0981b9ada820 167 int print_diag = 0; // do not print to diag file before it is named
joel_ssc 82:0981b9ada820 168 configFileIO().load_LogVers_config(print_diag); // version numbers of the log and diag files from "logvers.txt"
joel_ssc 82:0981b9ada820 169 mbedLogger().initializeDiagFile(print_diag);
joel_ssc 82:0981b9ada820 170 print_diag=1;
joel_ssc 82:0981b9ada820 171 configFileIO().load_LogVers_config(print_diag); // Cnow print info to diag file
joel_ssc 82:0981b9ada820 172 mbedLogger().initializeDiagFile(print_diag);
joel_ssc 87:6d95f853dab3 173
tnhnrl 65:2ac186553959 174 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 65:2ac186553959 175 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 65:2ac186553959 176
tnhnrl 65:2ac186553959 177 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 65:2ac186553959 178 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 65:2ac186553959 179
tnhnrl 65:2ac186553959 180 configFileIO().load_RUDDER_config(); // load the rudder servo inner loop parameters from the file "SERVO.txt"
tnhnrl 65:2ac186553959 181 configFileIO().load_HEADING_config(); // load the rudder servo outer loop HEADING control parameters from the file "HEADING.txt" (contains neutral position)
joel_ssc 102:0f430de62447 182
joel_ssc 102:0f430de62447 183 char buf[256];
joel_ssc 102:0f430de62447 184 sprintf(buf, "INIT of Loop operators line 174 main.cpp headingLoop().init() is coming\n");
joel_ssc 102:0f430de62447 185 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 186
joel_ssc 102:0f430de62447 187
joel_ssc 102:0f430de62447 188
joel_ssc 102:0f430de62447 189
danstrider 10:085ab7328054 190 // set up the linear actuators. adc has to be running first.
tnhnrl 65:2ac186553959 191 bce().setPIDHighLimit(bce().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 192 bce().init();
tnhnrl 65:2ac186553959 193 //bce().start(); //removed start, it's handled by the interrupt
tnhnrl 67:c86a4b464682 194 bce().runLinearActuator();
mkelly10 12:a0519d11d2b6 195 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 196
tnhnrl 65:2ac186553959 197 batt().setPIDHighLimit(batt().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 198 batt().init();
tnhnrl 65:2ac186553959 199 batt().runLinearActuator(); // _init = true;
tnhnrl 65:2ac186553959 200 //batt().start();//removed start, it's handled by the interrupt
mkelly10 12:a0519d11d2b6 201 batt().pause(); // start by not moving
joel_ssc 102:0f430de62447 202 //char buf[256];
joel_ssc 102:0f430de62447 203 sprintf(buf, "INIT of Loop operators line 196 main.cpp headingLoop().init() is coming\n");
joel_ssc 102:0f430de62447 204 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 205
joel_ssc 102:0f430de62447 206
tnhnrl 65:2ac186553959 207 // set up the depth, pitch, and rudder outer loop controllers
danstrider 10:085ab7328054 208 depthLoop().init();
tnhnrl 65:2ac186553959 209 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 210 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 211
danstrider 10:085ab7328054 212 pitchLoop().init();
tnhnrl 65:2ac186553959 213 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 214 pitchLoop().setCommand(stateMachine().getPitchCommand());
joel_ssc 102:0f430de62447 215 sprintf(buf, "INIT of Loop operators line 208 main.cpp headingLoop().init() is coming\n");
joel_ssc 87:6d95f853dab3 216 mbedLogger().appendDiagFile(buf,3);
tnhnrl 55:f4ec445c42fe 217
joel_ssc 102:0f430de62447 218
joel_ssc 87:6d95f853dab3 219 configFileIO().load_setneutral_status(); // "neutral.txt" has flag for whether neutral has been set, and neutral values
joel_ssc 87:6d95f853dab3 220 sprintf(buf, "\n in setup(): load_setneutral_status succeeded: setval = %d \n\r", configFileIO().neutralStruct.setval);
joel_ssc 87:6d95f853dab3 221 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 222 setval = configFileIO().neutralStruct.setval;
joel_ssc 87:6d95f853dab3 223 sprintf(buf, "\n in setup(): after load_setneutral_status setval = %d \n\r", setval);
joel_ssc 87:6d95f853dab3 224 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 225 configFileIO().load_swim_config(); // load in more parameters bmm_dive_mm, bce, fn_timeout, max_mbed-runtime_hr, altim_blank_m, altim_abort_m
joel_ssc 102:0f430de62447 226 sprintf(buf, "\n in setup(): after load_swim_config: fn_timeout = %d\n\r", configFileIO().swimConstants.fn_timeout);
joel_ssc 102:0f430de62447 227 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 228 sprintf(buf, "\n in setup(): after load_swim_config: altim_blank_m = %.1f\n\r", configFileIO().swimConstants.altim_blank_m);
joel_ssc 102:0f430de62447 229 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 230 sprintf(buf, "\n in setup(): after load_swim_config: bce_dive_mm = %.1f\n\r", configFileIO().swimConstants.bce_dive_mm);
joel_ssc 102:0f430de62447 231 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 232 sprintf(buf, "depth loop: P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(),
joel_ssc 102:0f430de62447 233 depthLoop().getControllerD(), depthLoop().getOutputOffset());
joel_ssc 102:0f430de62447 234 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 235
joel_ssc 102:0f430de62447 236 headingLoop().init();
joel_ssc 102:0f430de62447 237 xbee().printf("heading Loop initialized! line 230\n\r");
joel_ssc 102:0f430de62447 238
joel_ssc 102:0f430de62447 239 sprintf(buf, "INIT of Loop operators line 230 headingLoop().init() is finished\n");
joel_ssc 102:0f430de62447 240 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 241 // mbedLogger().appendDiagFile(buf,0);
joel_ssc 102:0f430de62447 242
joel_ssc 102:0f430de62447 243
joel_ssc 102:0f430de62447 244
joel_ssc 102:0f430de62447 245 configFileIO().load_Altimeter_config(); // load the altimeter filter parameters from the file "altim.txt" // edit by CAM
joel_ssc 102:0f430de62447 246 sprintf(buf, "loaded altimeter configuration line 237 \n");
joel_ssc 102:0f430de62447 247 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 248 sprintf(buf,"line 239: altimeter reading (meters) = %f\n", sensors().getAltimeterReading_m());
joel_ssc 102:0f430de62447 249 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 250 mbedLogger().appendDiagFile(buf,0);
joel_ssc 102:0f430de62447 251
joel_ssc 102:0f430de62447 252 xbee().printf("altimeter config loaded line 245\n\r");
joel_ssc 102:0f430de62447 253
joel_ssc 102:0f430de62447 254
joel_ssc 102:0f430de62447 255 altimLoop().init();
joel_ssc 102:0f430de62447 256 xbee().printf("altimLoop().init() run line 247\n\r");
joel_ssc 102:0f430de62447 257 sprintf(buf, "ran altimLoop().init() after reading config line 244 \n");
joel_ssc 102:0f430de62447 258 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 259 mbedLogger().appendDiagFile(buf,0);
joel_ssc 102:0f430de62447 260
joel_ssc 102:0f430de62447 261
joel_ssc 102:0f430de62447 262 sprintf(buf, "AltimLoop.init() succeeded! after loading altim.txt values and init() line 250\n");
joel_ssc 102:0f430de62447 263 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 264
joel_ssc 102:0f430de62447 265 sprintf(buf, "in setup(): before load_setneutral_status setval = %d line 253\n\r", setval);
joel_ssc 102:0f430de62447 266 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 267 mbedLogger().appendDiagFile(buf,0);
joel_ssc 102:0f430de62447 268
joel_ssc 102:0f430de62447 269
joel_ssc 102:0f430de62447 270
joel_ssc 102:0f430de62447 271 //
joel_ssc 102:0f430de62447 272 // configFileIO().load_setneutral_status(); // "neutral.txt" has flag for whether neutral has been set, and neutral values
joel_ssc 102:0f430de62447 273 // sprintf(buf, "\n in setup(): load_setneutral_status succeeded: setval = %d \n\r", configFileIO().neutralStruct.setval);
joel_ssc 102:0f430de62447 274 // mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 275 // setval = configFileIO().neutralStruct.setval;
joel_ssc 102:0f430de62447 276 // sprintf(buf, "\n in setup(): after load_setneutral_status setval = %d \n\r", setval);
joel_ssc 102:0f430de62447 277 // mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 278 // configFileIO().load_swim_config(); // load in more parameters bmm_dive_mm, bce, fn_timeout, max_mbed-runtime_hr, altim_blank_m, altim_abort_m
joel_ssc 102:0f430de62447 279 // sprintf(buf, "\n in setup(): after load_swim_config: fn_timeout = %d\n\r", configFileIO().swimConstants.fn_timeout);
joel_ssc 102:0f430de62447 280 // mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 281 // sprintf(buf, "\n in setup(): after load_swim_config: altim_blank_m = %.1f\n\r", configFileIO().swimConstants.altim_blank_m);
joel_ssc 102:0f430de62447 282 // mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 283 // sprintf(buf, "\n in setup(): after load_swim_config: bce_dive_mm = %.1f\n\r", configFileIO().swimConstants.bce_dive_mm);
joel_ssc 102:0f430de62447 284 // mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 285
tnhnrl 65:2ac186553959 286 //removed start, it's handled by the interrupt
tnhnrl 65:2ac186553959 287 //headingLoop().setCommand(stateMachine().getHeadingCommand()); // FIX LATER
tnhnrl 65:2ac186553959 288 //heading flag that adjust the PID error is set in the constructor
tnhnrl 65:2ac186553959 289
tnhnrl 65:2ac186553959 290 //systemTicker.attach_us(&system_timer, 10000); // Interrupt timer running at 0.01 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 291
tnhnrl 65:2ac186553959 292
tnhnrl 20:8987a9ae2bc7 293
tnhnrl 20:8987a9ae2bc7 294 // show that the PID gains are loading from the file
joel_ssc 82:0981b9ada820 295 xbee().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(),
joel_ssc 82:0981b9ada820 296 bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
joel_ssc 82:0981b9ada820 297 xbee().printf("batt P:%6.2f, I:%6.2f, D:%6.2f, zero %3i, limit %6.1f mm, slope %0.5f \r\n", batt().getControllerP(),
joel_ssc 82:0981b9ada820 298 batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
joel_ssc 82:0981b9ada820 299 xbee().printf("rudder min pwm: %6.2f, max pwm: %6.2f, center pwm: %6.2f, min deg: %6.2f, max deg: %6.2f\r\n", rudder().getMinPWM(), rudder().getMaxPWM(),
joel_ssc 82:0981b9ada820 300 rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
joel_ssc 82:0981b9ada820 301 xbee().printf("depth P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(),
joel_ssc 82:0981b9ada820 302 depthLoop().getControllerD(), depthLoop().getOutputOffset());
joel_ssc 82:0981b9ada820 303 xbee().printf("pitch P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(),
joel_ssc 82:0981b9ada820 304 pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
joel_ssc 82:0981b9ada820 305 xbee().printf("heading P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f deg (deadband: %0.1f)\r\n", headingLoop().getControllerP(),
joel_ssc 82:0981b9ada820 306 headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(), headingLoop().getDeadband());
tnhnrl 65:2ac186553959 307
tnhnrl 74:d281aaef9766 308 xbee().printf("\n\r");
tnhnrl 21:38c8544db6f4 309
tnhnrl 17:7c16b5671d0e 310 //load sequence from file
tnhnrl 17:7c16b5671d0e 311 sequenceController().loadSequence();
joel_ssc 87:6d95f853dab3 312
joel_ssc 82:0981b9ada820 313 //xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
joel_ssc 87:6d95f853dab3 314 sprintf(buf, " in setup(): starting legController().loadleg() \n\n\r");
joel_ssc 86:ba3a118b0080 315 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 316 have_legfile = legController().loadLeg(); // this should be 1 if the legfile reader has found 1 or more legs
joel_ssc 87:6d95f853dab3 317
joel_ssc 87:6d95f853dab3 318 current_state = legController().legStructLoaded[0].state;
joel_ssc 88:1813f583cee9 319 begin_state = current_state;
joel_ssc 87:6d95f853dab3 320 sprintf(buf, "in setup(): have_legfile = %d current_state = %d (sit_idle= 0 or 1)\n", have_legfile, current_state);
joel_ssc 87:6d95f853dab3 321 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 322 sprintf(buf, "in setup(): LEG_POSITION_DIVE = %d START_SWIM = %d \n", LEG_POSITION_DIVE, START_SWIM);
joel_ssc 87:6d95f853dab3 323 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 324
joel_ssc 87:6d95f853dab3 325 sprintf(buf, "Time is a mystery, here is a message before the basic call\n");
joel_ssc 86:ba3a118b0080 326 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 327 int jj;
joel_ssc 86:ba3a118b0080 328 long int kk;
joel_ssc 86:ba3a118b0080 329 time_t secval;
joel_ssc 102:0f430de62447 330 xbee().printf("legcontroller read and Time line 323\n\r");
joel_ssc 86:ba3a118b0080 331 sprintf(buf, "sizeof int=%d size of long int = %d size of time_t = %d\n", (sizeof jj), (sizeof kk), (sizeof secval));
joel_ssc 86:ba3a118b0080 332 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 333
joel_ssc 86:ba3a118b0080 334 secval = mbedLogger().getSystemTime();
joel_ssc 86:ba3a118b0080 335 sprintf(buf, "Time as a basic string = %s\n", ctime(&secval));
joel_ssc 86:ba3a118b0080 336 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 337
joel_ssc 86:ba3a118b0080 338 sprintf(buf, "Time is still amystery, here is a message after the basic call\n");
joel_ssc 82:0981b9ada820 339 mbedLogger().appendDiagFile(buf,0);
tnhnrl 32:f2f8ae34aadc 340
joel_ssc 82:0981b9ada820 341 //set time of logger (to current or close-to-current time) now set earlier at line 149
joel_ssc 82:0981b9ada820 342 // mbedLogger().setLogTime();
tnhnrl 65:2ac186553959 343 //sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 344
tnhnrl 47:fb3c7929d3f3 345 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 346 mbedLogger().initializeLogFile();
joel_ssc 82:0981b9ada820 347
joel_ssc 102:0f430de62447 348 mbedLogger().appendLogFile(current_state, 2); //write the idle state, then close
joel_ssc 82:0981b9ada820 349 //sdLogger().appendLogFile(current_state, 1); //write the idle state, then close
joel_ssc 82:0981b9ada820 350
joel_ssc 82:0981b9ada820 351 mbedLogger().appendLogFile(current_state, 0); //close log file added jcw nov 9 2018 for test
joel_ssc 82:0981b9ada820 352 //sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 80:4e5d306d695b 353 //sdLogger().initializeLogFile(); // works 10/23/18
tnhnrl 65:2ac186553959 354
tnhnrl 68:8f549749b8ce 355 setup_complete = true;
tnhnrl 65:2ac186553959 356 }
tnhnrl 58:94b7fd55185e 357
joel_ssc 84:eccd8e837134 358 void cycle_logfiles(int logversion, int diagversion) {
joel_ssc 84:eccd8e837134 359 //int logversion;
joel_ssc 84:eccd8e837134 360 //int diagversion;
joel_ssc 84:eccd8e837134 361 char bufx[256];
joel_ssc 84:eccd8e837134 362 sprintf(bufx, "\n\n\r in cycle_logfiles(%d, %d): starting new diag file. Will add 1 to these values \n\n\r", logversion, diagversion);
joel_ssc 84:eccd8e837134 363 mbedLogger().appendDiagFile(bufx,0);
joel_ssc 84:eccd8e837134 364 mbedLogger().appendLogFile(current_state, 0); //both files are now closed
joel_ssc 84:eccd8e837134 365 //use the present values and increment
joel_ssc 84:eccd8e837134 366 //logversion = configFileIO().logFilesStruct.logversion + 1;
joel_ssc 84:eccd8e837134 367 //diagversion = configFileIO().logFilesStruct.diagversion + 1;
joel_ssc 84:eccd8e837134 368 configFileIO().saveLogVersData(logversion+1, diagversion+1); // updates the file logvers.txt
joel_ssc 84:eccd8e837134 369 configFileIO().load_LogVers_config(0); // now read them back into the structure
joel_ssc 84:eccd8e837134 370 mbedLogger().initializeDiagFile(0); //don't print before initializing
joel_ssc 84:eccd8e837134 371
joel_ssc 84:eccd8e837134 372
joel_ssc 84:eccd8e837134 373 mbedLogger().initializeLogFile();
joel_ssc 84:eccd8e837134 374 mbedLogger().initializeDiagFile(1);
joel_ssc 84:eccd8e837134 375
joel_ssc 84:eccd8e837134 376 }
joel_ssc 84:eccd8e837134 377
tnhnrl 68:8f549749b8ce 378 /*************************** v1.8 **************************/
tnhnrl 68:8f549749b8ce 379
tnhnrl 73:f6f378311c8d 380 int main() {
tnhnrl 73:f6f378311c8d 381 setup(); //setup electronics/hardware
joel_ssc 82:0981b9ada820 382 // on landing, check orientation, if upside down, fix that first
joel_ssc 88:1813f583cee9 383 // systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
joel_ssc 86:ba3a118b0080 384 led2()=1; led4()=0;
tnhnrl 65:2ac186553959 385 unsigned int tNow = 0;
joel_ssc 82:0981b9ada820 386 int vernum=0;
joel_ssc 82:0981b9ada820 387 int diagnum=0;
joel_ssc 82:0981b9ada820 388 char buf[256];
tnhnrl 75:92e79d23d29a 389 xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
joel_ssc 87:6d95f853dab3 390 sprintf(buf, "\n\n\r 2019-may-07 FSG PCB XBee line315 in main (setup complete) \n\n\r");
joel_ssc 86:ba3a118b0080 391 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 392 int fn_timeout = 240; // need to get this into config file
joel_ssc 88:1813f583cee9 393 int save_timeout = 300;
joel_ssc 87:6d95f853dab3 394
joel_ssc 87:6d95f853dab3 395
joel_ssc 84:eccd8e837134 396 //tNow=5; sprintf(buf, "log file config values logfile= %s diag file= %s\n", configFileIO().logFilesStruct.logFileName,
joel_ssc 84:eccd8e837134 397 // configFileIO().logFilesStruct.diagFileName); tNow=0;
joel_ssc 87:6d95f853dab3 398 // mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 399 vernum = configFileIO().logFilesStruct.logversion;
joel_ssc 82:0981b9ada820 400 diagnum = configFileIO().logFilesStruct.diagversion;
joel_ssc 82:0981b9ada820 401 sprintf(buf, "translated values LOG FILE VERSION number (vernum)=%d diag file version number(diagnum) = %d\n", vernum, diagnum);
joel_ssc 82:0981b9ada820 402 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 403 sprintf(buf, "logfiles_struct values - direct LOG FILE VERSION ().logversion=%d diag file version().diagversion = %d\n",
joel_ssc 82:0981b9ada820 404 configFileIO().logFilesStruct.logversion, configFileIO().logFilesStruct.diagversion);
joel_ssc 82:0981b9ada820 405 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 406 //sprintf(buf, "try another message after closing file up and then will exit\n\n\r");
joel_ssc 86:ba3a118b0080 407 //mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 408
joel_ssc 84:eccd8e837134 409
joel_ssc 84:eccd8e837134 410 // increment the log file names once
joel_ssc 84:eccd8e837134 411 //cycle_logfiles(vernum,diagnum);
joel_ssc 84:eccd8e837134 412 //sprintf(buf, "This message should be in a new diag file\n\n\r");
joel_ssc 84:eccd8e837134 413 //mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 414 // mbedLogger().appendLogFile(current_state, 1);
joel_ssc 82:0981b9ada820 415 //wait(5);
joel_ssc 82:0981b9ada820 416 //exit(0);
tnhnrl 56:48a8a5a65b82 417 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
joel_ssc 82:0981b9ada820 418 int keeprunning = 1;
tnhnrl 65:2ac186553959 419
joel_ssc 82:0981b9ada820 420 if(have_legfile) {
joel_ssc 82:0981b9ada820 421 //install the leg variables in a structure, and set the state there.
joel_ssc 82:0981b9ada820 422 stateMachine().getLegParams(); //should set up everything with proper LEG_POSITION_DIVE state
joel_ssc 87:6d95f853dab3 423 sprintf(buf, "have_legfile succeeded main: line 318 \n\r");
joel_ssc 86:ba3a118b0080 424 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 425 }
joel_ssc 87:6d95f853dab3 426
joel_ssc 86:ba3a118b0080 427 if(!have_legfile) {
joel_ssc 86:ba3a118b0080 428 sprintf(buf, "have_legfile failed! .... will exit\n\n\r");
joel_ssc 86:ba3a118b0080 429 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 430 keeprunning = 0;
joel_ssc 85:dd8176285b6e 431 }
joel_ssc 98:81db9332212d 432 // another digitalIn option is is_connected() how can one get both 0 and 1 here?
joel_ssc 87:6d95f853dab3 433 if(motorDisconnect().read() == 1) {
joel_ssc 87:6d95f853dab3 434 sprintf(buf, "motorDisconnect.read() = 1 surprising!\n\n\r");
joel_ssc 87:6d95f853dab3 435 mbedLogger().appendDiagFile(buf,3); led1()=1; led4()=1;
joel_ssc 87:6d95f853dab3 436 }
joel_ssc 87:6d95f853dab3 437 if(motorDisconnect().read() == 0) {
joel_ssc 87:6d95f853dab3 438 sprintf(buf, "motorDisconnect.read() = 0 I expected that\n\n\r");
joel_ssc 87:6d95f853dab3 439 mbedLogger().appendDiagFile(buf,3); led1()=1; led3()=1;
joel_ssc 87:6d95f853dab3 440 }
joel_ssc 86:ba3a118b0080 441
joel_ssc 84:eccd8e837134 442 while (keeprunning) {
joel_ssc 84:eccd8e837134 443 if( read_ticker() ) { // read_ticker runs at the speed of 10 kHz (adc timing)
tnhnrl 65:2ac186553959 444 ++tNow;
mkelly10 51:c5c40272ecc3 445
tnhnrl 81:7ff2c6467892 446 //Note to self: Retest data transmission code.
tnhnrl 81:7ff2c6467892 447 //This is currently running at 0.1 second intervals (10 hz) and was working well for data transmission
tnhnrl 74:d281aaef9766 448 if (current_state == TX_MBED_LOG or current_state == RX_SEQUENCE) {
tnhnrl 73:f6f378311c8d 449 if ( (tNow % 100) == 0 ) { // 0.01 second intervals (100 Hz)
tnhnrl 66:0f20870117b7 450 fsm_loop = true;
tnhnrl 66:0f20870117b7 451 FSM();
tnhnrl 66:0f20870117b7 452 }
joel_ssc 82:0981b9ada820 453 } // end if(currentstate..)
joel_ssc 84:eccd8e837134 454
tnhnrl 67:c86a4b464682 455 //NOT TRANSMITTING DATA, NORMAL OPERATIONS
joel_ssc 84:eccd8e837134 456 else { // **88**
joel_ssc 84:eccd8e837134 457 //FSM
joel_ssc 97:2b4f78a54227 458 setval = configFileIO().neutralStruct.setval;
joel_ssc 88:1813f583cee9 459 // preamble to running the FSM
joel_ssc 97:2b4f78a54227 460 if (setval == 0 && (begin_state != FLYING_IDLE) && ( current_state == LEG_POSITION_DIVE) && (fabs(imu().getRoll()) < 30)) {
joel_ssc 95:1aac4086928a 461 // begin_state is first state found in setup()
joel_ssc 95:1aac4086928a 462 //These tests mean: - second start,plus already upright
joel_ssc 88:1813f583cee9 463 // from setup() current_state = legController().legStructLoaded[0].state; either start_swim or leg_pos_dive
joel_ssc 88:1813f583cee9 464 save_state = current_state;
joel_ssc 88:1813f583cee9 465 neutral_via_leg =1;
joel_ssc 88:1813f583cee9 466 save_timeout = legController().legStructLoaded[0].timeout;
joel_ssc 102:0f430de62447 467 fn_timeout = configFileIO().swimConstants.fn_timeout;
joel_ssc 97:2b4f78a54227 468 current_state = START_SWIM; // does runStatemachine know about this value?
joel_ssc 97:2b4f78a54227 469 sprintf(buf, "in main(): setval==0, but upright, so pre-empt leg_pos_dive to start_swim\n set state directly start_swim\n");
joel_ssc 88:1813f583cee9 470 mbedLogger().appendDiagFile(buf,3);
joel_ssc 97:2b4f78a54227 471 stateMachine().setstate_frommain(START_SWIM, fn_timeout); // this will change state inside runstatemachine, change state val inthe right structure, first
joel_ssc 88:1813f583cee9 472 }
joel_ssc 95:1aac4086928a 473 //setval is now updated in log_data() above, line 45 or so
joel_ssc 88:1813f583cee9 474 if ( (neutral_via_leg == 1) && (setval == 1) && current_state == RISE) { // usual endstate of FIND_NEUTRAL new state for after successful find neutral
joel_ssc 88:1813f583cee9 475 current_state = save_state; //setval reset at line 366 when find_neutral succeeds
joel_ssc 88:1813f583cee9 476 stateMachine().setstate_frommain(current_state, save_timeout); //back to what you were doing
joel_ssc 88:1813f583cee9 477 sprintf(buf, "in main(): presumably after find_neutral exits to RISE, but successful in setting setval\n");
joel_ssc 88:1813f583cee9 478 mbedLogger().appendDiagFile(buf,3);
joel_ssc 88:1813f583cee9 479 neutral_via_leg = 0;
joel_ssc 88:1813f583cee9 480 }
joel_ssc 95:1aac4086928a 481 if ( (neutral_via_leg == 1) && (setval != 1) && current_state == RISE) { // failed endstate of FIND_NEUTRAL new state
joel_ssc 88:1813f583cee9 482
joel_ssc 95:1aac4086928a 483 sprintf(buf, "in main(): line 409 after find_neutral fail and exits to RISE, UNSUCCESSFULLY\n");
joel_ssc 88:1813f583cee9 484 mbedLogger().appendDiagFile(buf,3);
joel_ssc 88:1813f583cee9 485 neutral_via_leg = 0;
joel_ssc 88:1813f583cee9 486 }
joel_ssc 88:1813f583cee9 487 // end preamble
tnhnrl 68:8f549749b8ce 488 if ( (tNow % 100) == 0 ) { // 0.1 second intervals
tnhnrl 66:0f20870117b7 489 fsm_loop = true;
tnhnrl 66:0f20870117b7 490 FSM();
joel_ssc 84:eccd8e837134 491
tnhnrl 73:f6f378311c8d 492 //get commands and update GUI
tnhnrl 73:f6f378311c8d 493 gui().getCommandFSM();
joel_ssc 84:eccd8e837134 494 }
joel_ssc 84:eccd8e837134 495 //LOGGING
joel_ssc 84:eccd8e837134 496 if ( (tNow % 1000) == 0 ) { // 1.0 second intervals
joel_ssc 84:eccd8e837134 497 log_loop = true;
joel_ssc 102:0f430de62447 498 if((tNow % 20000) ==0) {log_function(2);} // long data string every 20 seconds
joel_ssc 102:0f430de62447 499 else {log_function(1); } //otherwise short
joel_ssc 102:0f430de62447 500 // sprintf(buf, "hit 1 second log interval in main loop tNow =%d imu.roll = %f not-unsampled \n\n\r", tNow, imu().getRoll());
joel_ssc 102:0f430de62447 501 // mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 502 led3()=0; led2()=0; led1()=0; led4()=0;
joel_ssc 86:ba3a118b0080 503 }
joel_ssc 87:6d95f853dab3 504 if ( (tNow % 2000) == 0 ) { // 2 second intervals
joel_ssc 86:ba3a118b0080 505
joel_ssc 87:6d95f853dab3 506 led3()=1;
joel_ssc 92:52a91656458a 507 sprintf(buf, "main loop: current_state=%d tNow =%d imu.heading=%f headingLoop.heading= %f imu.roll = %f \n\n\r",
joel_ssc 92:52a91656458a 508 current_state, tNow,imu().getHeading(), headingLoop().getPosition(), imu().getRoll());
joel_ssc 87:6d95f853dab3 509 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 510 }
joel_ssc 87:6d95f853dab3 511 if ((tNow % 3000) == 0) {
joel_ssc 87:6d95f853dab3 512 led2()=1;
joel_ssc 87:6d95f853dab3 513 }
joel_ssc 84:eccd8e837134 514 //update the log and diagnostics files
joel_ssc 87:6d95f853dab3 515 if ( (tNow % 31000) == 0 ) { // 1.0 hour intervals= 3600*1000 check for testing via 31 second intervals
joel_ssc 85:dd8176285b6e 516 sprintf(buf, "hit cycle seconds replace_logfiles interval in main loop tNow =%d \n\n\r", tNow);
joel_ssc 85:dd8176285b6e 517 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 518 vernum = configFileIO().logFilesStruct.logversion;
joel_ssc 84:eccd8e837134 519 diagnum = configFileIO().logFilesStruct.diagversion;
joel_ssc 85:dd8176285b6e 520 sprintf(buf, "cycle log file names at tnow=%d at seconds. This message should be in old diag file\n\n\r", tNow);
joel_ssc 86:ba3a118b0080 521 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 522 cycle_logfiles(vernum,diagnum);
joel_ssc 85:dd8176285b6e 523 sprintf(buf, "cycled log files at tNow = %d at seconds, This message should be in a NEW diag file\n\n\r", tNow);
joel_ssc 85:dd8176285b6e 524 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 525 // close the log and diagnostics files
joel_ssc 84:eccd8e837134 526 // increment the version numbers for each
joel_ssc 84:eccd8e837134 527 // save the new version numbers
joel_ssc 84:eccd8e837134 528 // initialize new log and diagnostics files
tnhnrl 66:0f20870117b7 529 log_loop = true;
joel_ssc 102:0f430de62447 530 log_function(2);
joel_ssc 87:6d95f853dab3 531 led3()=1; led1()=1;
tnhnrl 66:0f20870117b7 532 }
joel_ssc 84:eccd8e837134 533 } // end else { at **88**
joel_ssc 84:eccd8e837134 534 if(current_state == FB_EXIT) {
joel_ssc 84:eccd8e837134 535 log_loop=true;
joel_ssc 102:0f430de62447 536 log_function(2);
joel_ssc 92:52a91656458a 537 led2()=1;
joel_ssc 92:52a91656458a 538 led4()=1;
joel_ssc 84:eccd8e837134 539 keeprunning=0; // and this will force exit to the main while() loop.
joel_ssc 85:dd8176285b6e 540 // switch state to EOL_WAIT, spit out xbee messages, wait 60 seconds or so. if keyboard comes back, do not exit
joel_ssc 84:eccd8e837134 541 sprintf(buf, "INSIDE main loop: BUT now called to exit out of it via FB_EXIT\n\n\r");
joel_ssc 86:ba3a118b0080 542 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 543 // and put out the end_leg message file
joel_ssc 102:0f430de62447 544 configFileIO().writeEndleg_reason();
joel_ssc 84:eccd8e837134 545 }
joel_ssc 92:52a91656458a 546 if(tNow == 90000) { // don't wait forever -remove this for real operations!!
joel_ssc 102:0f430de62447 547 // if(tNow == 1000 * configFileIO().swimConstants.max_mbed_runtime_hr * 3600) {
joel_ssc 92:52a91656458a 548 keeprunning=0;
joel_ssc 102:0f430de62447 549
joel_ssc 92:52a91656458a 550 sprintf(buf, "INSIDE main loop: Triggered FINAL timeout at 90 seconds\n\n\r");
joel_ssc 92:52a91656458a 551 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 552 sprintf(buf, "AT FINAL TIMEOUT: before changing endleg-reason to 8: endleg_reason= %d\n\n\r", configFileIO().swimConstants.endleg_reason);
joel_ssc 102:0f430de62447 553 mbedLogger().appendDiagFile(buf,3);
joel_ssc 102:0f430de62447 554 configFileIO().swimConstants.endleg_reason = 8; // timed out on mbed_max_runtime_hr
joel_ssc 102:0f430de62447 555 configFileIO().writeEndleg_reason();
joel_ssc 92:52a91656458a 556 }
joel_ssc 82:0981b9ada820 557 } // end if(read_ticker) {
joel_ssc 82:0981b9ada820 558 } // end while(keeprunning)
joel_ssc 87:6d95f853dab3 559
joel_ssc 102:0f430de62447 560 mbedLogger().appendLogFile(current_state, 2);
joel_ssc 87:6d95f853dab3 561 sprintf(buf, "outside of main loop: exiting out of the leg loop via FB_EXIT tNow= %d \n\n\r", tNow);
joel_ssc 86:ba3a118b0080 562 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 563 led1()=1; led2() =1; led3()=1 ;led4() = 1;
joel_ssc 86:ba3a118b0080 564 wait(5);
joel_ssc 86:ba3a118b0080 565
joel_ssc 102:0f430de62447 566 mbedLogger().appendLogFile(current_state, 2);
joel_ssc 82:0981b9ada820 567 mbedLogger().appendLogFile(current_state, 0);
joel_ssc 86:ba3a118b0080 568 sprintf(buf, "wait 5 more seconds? out of the leg loop via FB_EXIT\n\n\r");
joel_ssc 82:0981b9ada820 569 mbedLogger().appendDiagFile(buf,0);
joel_ssc 86:ba3a118b0080 570 led1()=0; led2()=0; led3()= 0 ;led4()=0;
joel_ssc 88:1813f583cee9 571 configFileIO().save_FinalTime(); // saves last time before closing shop
joel_ssc 104:426224a55f5f 572
joel_ssc 104:426224a55f5f 573 reset_Pi();
joel_ssc 104:426224a55f5f 574
joel_ssc 82:0981b9ada820 575 exit(0);
joel_ssc 82:0981b9ada820 576 } // end main()