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 16:51:48 2019 +0000
Revision:
102:0f430de62447
Parent:
98:81db9332212d
Child:
104:426224a55f5f
changes of the 2019_15may code  that worked,  altimeter code,; swimfile code,  altimeter logic

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