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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "StaticDefs.hpp"
00003 
00004 ////////////////////////////////////////////////////////////////// NEW TICKER
00005 Ticker systemTicker;
00006 bool setup_complete = false;
00007 volatile unsigned int bTick = 0;
00008 volatile unsigned int timer_counter = 0;
00009 
00010 static unsigned int read_ticker(void) {      //Basically this makes sure you're reading the data at one instance (not while it's changing)
00011     unsigned int val = bTick;
00012     if( val )
00013         bTick = 0;
00014     return( val );
00015 }
00016 ////////////////////////////////////////////////////////////////// NEW TICKER
00017 
00018 volatile bool fsm_loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)
00019 volatile bool log_loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)
00020 
00021 void loop_trigger() { fsm_loop = true;} // loop trigger (used in while loop)
00022 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
00023 
00024 static int current_state = 0; 
00025 static int have_legfile = 0; 
00026 
00027 static int save_state = 0;   // state to hold while find_neutral runs ( if necessary)
00028        int begin_state = 0;
00029 static int neutral_via_leg = 0;  // flag for entry of find_neutral via leg_pos and setval==0
00030 static int setval = -1;    // has neutral been set, found in neutral.txt   
00031 
00032 static bool file_opened = false;
00033 
00034 void FSM() {                    // FSM loop runs at 10 hz
00035     if(fsm_loop) {
00036         // led one removed
00037         fsm_loop = false;       // wait until the loop rate timer fires again
00038         current_state = stateMachine().runStateMachine();       //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
00039     }
00040 }
00041 
00042 void log_function() {    
00043     // log loop runs at 1 hz
00044     if (log_loop) {  //start logloop8
00045         //when the state machine is not in SIT_IDLE state (or a random keyboard press)
00046         
00047         if((current_state != SIT_IDLE)   &&  (current_state != FLYING_IDLE)) {  //first if  - not in sit_idle/keyboard state
00048             if (!file_opened) {                                 //if the log file is not open, open it
00049                 mbedLogger().appendLogFile(current_state, 1);   //open MBED file once
00050                 //sdLogger().appendLogFile(current_state, 0);     //open SD file once
00051                            
00052                 file_opened = true;                             //stops it from continuing to open it
00053 
00054                 xbee().printf(">>>>>>>> Recording. Log file re opened. <<<<<<<<\n\r");
00055             }
00056             else {
00057             
00058                   //just record  to the  Mbed file system   
00059             
00060                    mbedLogger().appendLogFile(current_state, 1);    //writing data
00061                    //sdLogger().appendLogFile(current_state, 1);    //writing data
00062                  }
00063         }  //end first if
00064         
00065         //when the current FSM state is zero (SIT_IDLE), close the file
00066         else {  //start first else  for current_state ==0  sit idle/keyboard
00067             //this can only happen once
00068             if (file_opened) {
00069                 //WRITE ONCE
00070                 mbedLogger().appendLogFile(current_state, 1);   //write the idle state, then close
00071                 //sdLogger().appendLogFile(current_state, 1);     //write the idle state, then close
00072                 
00073                 mbedLogger().appendLogFile(current_state, 0);    //close log file
00074                 //sdLogger().appendLogFile(current_state, 0);    //close log file
00075                 
00076                 file_opened = false;
00077                 
00078                 xbee().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
00079             }
00080         }  //end first else
00081     }   //END OF LOG LOOP8
00082     
00083     log_loop = false;   // wait until the loop rate timer fires again
00084 }
00085 
00086 //single system timer to run hardware/electronics timing
00087 static void system_timer(void) {
00088     bTick = 1;
00089     
00090     timer_counter++;
00091     
00092     //only start these updates when everything is properly setup (through setup function)
00093     if (setup_complete) {
00094         if ( (timer_counter % 5) == 0) {    //this runs at 0.005 second intervals (200 Hz)
00095             adc().update();  //every iteration of this the A/D converter runs   //now this runs at 0.01 second intervals 03/12/2018
00096         }
00097         
00098         if ( (timer_counter % 10) == 0) {
00099             bce().update();      //update() inside LinearActuator class (running at 0.01 second intervals)
00100             batt().update();
00101         }
00102         
00103         if ( (timer_counter % 20) == 0 ) {    // 0.02 second intervals
00104             rudder().runServo();
00105         }
00106         
00107         if ( (timer_counter % 50) == 0 ) {    // 0.05 second intervals 
00108             imu().runIMU();
00109         }
00110                 
00111         if ( (timer_counter % 100) == 0) {     // 100,000 microseconds = 0.1 second intervals
00112             depthLoop().runOuterLoop();
00113             pitchLoop().runOuterLoop();
00114             headingLoop().runOuterLoop();
00115             altimLoop().runOuterLoop();
00116         }
00117         
00118         if ( (timer_counter % 1000) == 0) {        // update at 1.0 second intervals
00119             //gui().updateGUI();
00120         }
00121         
00122         if ( (timer_counter % 30000) == 0) {        // update at 30.0 second intervals
00123             //pc().printf("XB!\n");
00124         }
00125     }
00126 }
00127 
00128 void setup() {
00129     //  xbee().baud(115200);  // comment out so default is 9600 for USB communications
00130     xbee().printf("\n\n\r 2018-11-08 FSG PCB XBee\n\n\r");
00131     
00132     //pc().baud(57600);
00133  
00134     // start up the system timer
00135     //systemTimer().start();
00136  
00137     // set up and start the adc. This runs on a fixed interval and is interrupt driven
00138     adc().initialize();
00139     //one central interrupt is updating the ADC (not using the start function)
00140     
00141     // setup and run the rudder(servo) pwm signal (start the ticker)
00142     //rudder().init();
00143     xbee().printf("Rudder servo initialized!\n\r");
00144     
00145     // set up and start the imu. This polls in the background
00146     imu().initialize();
00147     //imu().start();
00148     
00149     // construct the MBED local file system
00150     local();
00151     
00152     // construct the SD card file system TEST 10/23/18
00153     //sd_card();
00154                    //  mbedLogger().initializeDiagFile();
00155     // load config data from files
00156     configFileIO().load_StartTime();
00157     //     mbedLogger().setLogTime();  replaced by call in configfileIO
00158     int print_diag = 0;    // do not print to diag file before it is named
00159     configFileIO().load_LogVers_config(print_diag);    // version numbers of the log and diag files from "logvers.txt"
00160     mbedLogger().initializeDiagFile(print_diag);
00161     print_diag=1;
00162     configFileIO().load_LogVers_config(print_diag);    // Cnow print info to diag file
00163     mbedLogger().initializeDiagFile(print_diag);
00164         
00165     configFileIO().load_BCE_config();       // load the buoyancy engine parameters from the file "bce.txt"
00166     configFileIO().load_BATT_config();      // load the battery mass mover parameters from the file "batt.txt"
00167     
00168     configFileIO().load_DEPTH_config();     // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
00169     configFileIO().load_PITCH_config();     // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
00170     
00171     configFileIO().load_RUDDER_config();    // load the rudder servo inner loop parameters from the file "SERVO.txt"
00172     configFileIO().load_HEADING_config();   // load the rudder servo outer loop HEADING control parameters from the file "HEADING.txt" (contains neutral position)
00173  
00174     // set up the linear actuators.  adc has to be running first.
00175     bce().setPIDHighLimit(bce().getTravelLimit());     //travel limit of this linear actuator
00176     bce().init();
00177     //bce().start();  //removed start, it's handled by the interrupt
00178     bce().runLinearActuator();
00179     bce().pause(); // start by not moving
00180  
00181     batt().setPIDHighLimit(batt().getTravelLimit());    //travel limit of this linear actuator
00182     batt().init();
00183     batt().runLinearActuator(); // _init = true;
00184     //batt().start();//removed start, it's handled by the interrupt
00185     batt().pause(); // start by not moving
00186  
00187     // set up the depth, pitch, and rudder outer loop controllers
00188     depthLoop().init();
00189     //removed start, it's handled by the interrupt
00190     depthLoop().setCommand(stateMachine().getDepthCommand());
00191  
00192     pitchLoop().init();
00193     //removed start, it's handled by the interrupt
00194     pitchLoop().setCommand(stateMachine().getPitchCommand());
00195     char buf[256];
00196     sprintf(buf, "INIT  of Loop operators   headingLoop().init() is coming\n");
00197     mbedLogger().appendDiagFile(buf,3);
00198     headingLoop().init();  
00199     altimLoop().init();   
00200     sprintf(buf, "AltimLOOP.init() succeeded!  even with just default values - no starting file\n");
00201     mbedLogger().appendDiagFile(buf,3);  
00202     
00203     sprintf(buf, "in setup():  before load_setneutral_status setval = %d \n\r", setval);
00204     mbedLogger().appendDiagFile(buf,3);
00205             
00206     configFileIO().load_setneutral_status();   // "neutral.txt" has flag for whether neutral has been set, and neutral values
00207     sprintf(buf, "\n in setup():  load_setneutral_status succeeded:    setval = %d \n\r", configFileIO().neutralStruct.setval);
00208     mbedLogger().appendDiagFile(buf,3);
00209     setval = configFileIO().neutralStruct.setval;
00210     sprintf(buf, "\n in setup():  after load_setneutral_status setval = %d \n\r", setval);
00211     mbedLogger().appendDiagFile(buf,3);
00212     
00213     //removed start, it's handled by the interrupt
00214     //headingLoop().setCommand(stateMachine().getHeadingCommand());         // FIX LATER
00215     //heading flag that adjust the PID error is set in the constructor
00216     
00217     //systemTicker.attach_us(&system_timer, 10000);         // Interrupt timer running at 0.01 seconds       (slower than original ADC time interval)
00218     
00219     
00220  
00221     // show that the PID gains are loading from the file
00222     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(), 
00223              bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
00224     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(),
00225              batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
00226     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(), 
00227             rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());    
00228     xbee().printf("depth   P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), 
00229          depthLoop().getControllerD(), depthLoop().getOutputOffset());
00230     xbee().printf("pitch   P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(),
00231      pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
00232     xbee().printf("heading P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f deg (deadband: %0.1f)\r\n", headingLoop().getControllerP(),
00233      headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(), headingLoop().getDeadband());
00234     
00235     xbee().printf("\n\r");
00236          
00237     //load sequence from file
00238     sequenceController().loadSequence();
00239     
00240     //xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
00241     sprintf(buf, " in setup():  starting legController().loadleg() \n\n\r");
00242     mbedLogger().appendDiagFile(buf,3);
00243     have_legfile = legController().loadLeg();  // this should be 1 if the legfile reader has found 1 or more legs
00244     
00245     current_state = legController().legStructLoaded[0].state;
00246     begin_state = current_state;
00247     sprintf(buf, "in setup(): have_legfile = %d    current_state = %d (sit_idle= 0 or 1)\n", have_legfile, current_state);
00248     mbedLogger().appendDiagFile(buf,3);
00249     sprintf(buf, "in setup(): LEG_POSITION_DIVE = %d   START_SWIM = %d \n", LEG_POSITION_DIVE, START_SWIM);
00250     mbedLogger().appendDiagFile(buf,3);
00251     
00252     sprintf(buf, "Time is a mystery, here is a message before the basic call\n");
00253     mbedLogger().appendDiagFile(buf,3);
00254     int jj;
00255     long int kk;
00256     time_t secval;
00257     
00258     sprintf(buf, "sizeof int=%d    size of long int = %d  size of time_t = %d\n", (sizeof jj), (sizeof kk), (sizeof secval));
00259     mbedLogger().appendDiagFile(buf,3);
00260     
00261     secval = mbedLogger().getSystemTime();
00262     sprintf(buf, "Time as a basic string = %s\n", ctime(&secval));
00263     mbedLogger().appendDiagFile(buf,3);
00264     
00265     sprintf(buf, "Time is still amystery, here is a message after the basic call\n");
00266     mbedLogger().appendDiagFile(buf,0);
00267     
00268     //set time of logger (to current or close-to-current time)   now set earlier  at line 149
00269     // mbedLogger().setLogTime();
00270     //sdLogger().setLogTime();
00271     
00272     //create log files if not present on file system
00273     mbedLogger().initializeLogFile();
00274     
00275     mbedLogger().appendLogFile(current_state, 1);   //write the idle state, then close
00276                 //sdLogger().appendLogFile(current_state, 1);     //write the idle state, then close
00277                 
00278                 mbedLogger().appendLogFile(current_state, 0);    //close log file  added jcw nov 9 2018 for test
00279                 //sdLogger().appendLogFile(current_state, 0);    //close log file
00280     //sdLogger().initializeLogFile(); // works 10/23/18
00281     
00282     setup_complete = true;    
00283 }
00284 
00285 void cycle_logfiles(int logversion, int diagversion) {
00286    //int logversion;
00287    //int diagversion;
00288    char bufx[256];
00289    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);
00290   mbedLogger().appendDiagFile(bufx,0);
00291  mbedLogger().appendLogFile(current_state, 0);  //both files are now closed
00292        //use the present values and increment
00293   //logversion =  configFileIO().logFilesStruct.logversion  + 1;  
00294   //diagversion = configFileIO().logFilesStruct.diagversion  + 1;
00295   configFileIO().saveLogVersData(logversion+1, diagversion+1);   // updates the file logvers.txt
00296   configFileIO().load_LogVers_config(0);    // now read them back into the structure
00297   mbedLogger().initializeDiagFile(0);      //don't print before initializing
00298   
00299   
00300   mbedLogger().initializeLogFile();
00301   mbedLogger().initializeDiagFile(1);
00302    
00303 }  
00304 
00305 /*************************** v1.8 **************************/
00306 
00307 int main() {   
00308     setup();    //setup electronics/hardware
00309     // on landing, check orientation, if upside down, fix that first
00310     // systemTicker.attach_us(&system_timer, 1000);         // Interrupt timer running at 0.001 seconds       (slower than original ADC time interval)
00311     led2()=1; led4()=0;
00312     unsigned int tNow = 0;
00313     int vernum=0;
00314     int diagnum=0;
00315      char buf[256];
00316     xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
00317     sprintf(buf, "\n\n\r 2019-may-07 FSG PCB XBee line315  in main (setup complete) \n\n\r");
00318     mbedLogger().appendDiagFile(buf,3);
00319     int fn_timeout = 240;
00320     int save_timeout = 300;
00321     
00322     
00323     //tNow=5; sprintf(buf, "log file config values logfile= %s     diag file= %s\n", configFileIO().logFilesStruct.logFileName,
00324      //        configFileIO().logFilesStruct.diagFileName); tNow=0;
00325     //         mbedLogger().appendDiagFile(buf,3);
00326     vernum = configFileIO().logFilesStruct.logversion;
00327     diagnum = configFileIO().logFilesStruct.diagversion;
00328     sprintf(buf, "translated values LOG FILE VERSION number (vernum)=%d           diag file version number(diagnum) = %d\n", vernum, diagnum);
00329     mbedLogger().appendDiagFile(buf,3);
00330     sprintf(buf, "logfiles_struct values - direct LOG FILE VERSION ().logversion=%d           diag file version().diagversion = %d\n", 
00331     configFileIO().logFilesStruct.logversion,   configFileIO().logFilesStruct.diagversion);
00332     mbedLogger().appendDiagFile(buf,3);
00333      //sprintf(buf, "try another message after closing file up and then will exit\n\n\r");
00334     //mbedLogger().appendDiagFile(buf,0);
00335     
00336     
00337     // increment the log file names once
00338     //cycle_logfiles(vernum,diagnum);
00339      //sprintf(buf, "This message should be in a new diag file\n\n\r");
00340     //mbedLogger().appendDiagFile(buf,0);
00341     // mbedLogger().appendLogFile(current_state, 1);
00342     //wait(5);
00343     //exit(0);
00344     systemTicker.attach_us(&system_timer, 1000);         // Interrupt timer running at 0.001 seconds       (slower than original ADC time interval)
00345     int keeprunning = 1;
00346         
00347         if(have_legfile) {
00348             //install the leg variables in a structure, and set the state there.
00349             stateMachine().getLegParams(); //should set up everything with proper LEG_POSITION_DIVE state
00350             sprintf(buf, "have_legfile succeeded  main: line 318 \n\r");
00351              mbedLogger().appendDiagFile(buf,3);
00352             }
00353         
00354         if(!have_legfile) {
00355             sprintf(buf, "have_legfile failed!   .... will exit\n\n\r");
00356             mbedLogger().appendDiagFile(buf,3);
00357             keeprunning = 0;
00358             }
00359         if(motorDisconnect().read() == 1) {
00360             sprintf(buf, "motorDisconnect.read() = 1  surprising!\n\n\r");
00361             mbedLogger().appendDiagFile(buf,3); led1()=1; led4()=1;
00362             }
00363         if(motorDisconnect().read() == 0) {
00364             sprintf(buf, "motorDisconnect.read() = 0  I expected that\n\n\r");
00365             mbedLogger().appendDiagFile(buf,3); led1()=1; led3()=1;
00366             }
00367   
00368     while (keeprunning) {
00369         if( read_ticker() ) {                       // read_ticker runs at the speed of 10 kHz (adc timing)
00370             ++tNow;
00371 
00372             //Note to self: Retest data transmission code.
00373             //This is currently running at 0.1 second intervals (10 hz) and was working well for data transmission
00374             if (current_state == TX_MBED_LOG or current_state == RX_SEQUENCE) {
00375                 if ( (tNow % 100) == 0 ) {   // 0.01 second intervals  (100 Hz)
00376                     fsm_loop = true;
00377                     FSM();
00378                 }
00379             } // end if(currentstate..)
00380 
00381             //NOT TRANSMITTING DATA, NORMAL OPERATIONS
00382             else {  // **88**
00383                 //FSM
00384                        // preamble to running the FSM
00385                 if (setval == 0 && (begin_state != FLYING_IDLE) && (current_state == START_SWIM  || current_state == LEG_POSITION_DIVE) && (fabs(imu().getRoll()) < 30)) {
00386                     // These tests mean: - second start,plus already upright
00387                     // from setup()  current_state = legController().legStructLoaded[0].state; either start_swim or leg_pos_dive
00388                     save_state = current_state;
00389                     neutral_via_leg =1;
00390                     save_timeout = legController().legStructLoaded[0].timeout;
00391                     fn_timeout = 240;
00392                     current_state = FIND_NEUTRAL;  // does runStatemachine know about this value?
00393                     sprintf(buf, "in main(): setval==0, but upright, so pre-empt start_swim or leg_pos_dive\n  set state directly  to find_neutral\n");
00394                     mbedLogger().appendDiagFile(buf,3);
00395                     stateMachine().setstate_frommain(FIND_NEUTRAL, fn_timeout);  // this will change state inside runstatemachine, change state val inthe right structure, first
00396                 }
00397                 if ( (neutral_via_leg == 1) && (setval == 1) && current_state == RISE) {  // usual endstate of FIND_NEUTRAL new state for after successful find neutral
00398                     current_state = save_state;   //setval reset at line 366  when find_neutral succeeds
00399                     stateMachine().setstate_frommain(current_state, save_timeout);   //back to what you were doing
00400                     sprintf(buf, "in main(): presumably after find_neutral exits to RISE, but successful in setting setval\n");
00401                     mbedLogger().appendDiagFile(buf,3);
00402                     neutral_via_leg = 0;
00403                 }
00404                 if ( (neutral_via_leg == 1) && (setval == 0) && current_state == RISE) {  // failed endstate of FIND_NEUTRAL new state
00405                     
00406                     sprintf(buf, "in main(): presumably after find_neutral exits to RISE, UNSUCCESSFULLY\n");
00407                     mbedLogger().appendDiagFile(buf,3);
00408                     neutral_via_leg = 0;
00409                 }
00410                       // end preamble
00411                 if ( (tNow % 100) == 0 ) {   // 0.1 second intervals
00412                     fsm_loop = true;
00413                     FSM();
00414 
00415                     //get commands and update GUI
00416                     gui().getCommandFSM();
00417                 }
00418                 //LOGGING
00419                 if ( (tNow % 1000) == 0 ) {   // 1.0 second intervals
00420                     log_loop = true;
00421                     log_function();
00422                     //sprintf(buf, "hit 1 second  log interval in main loop tNow =%d  imu.roll = %f not-unsampled \n\n\r", tNow, imu().getRoll());  
00423                     //mbedLogger().appendDiagFile(buf,3);
00424                     led3()=0; led2()=0; led1()=0; led4()=0;
00425                 }
00426                 if ( (tNow % 2000) == 0 ) {   // 2 second intervals
00427                     
00428                     led3()=1; 
00429                     sprintf(buf, "main loop:  current_state=%d tNow =%d imu.heading=%f  headingLoop.heading= %f  imu.roll = %f  \n\n\r", 
00430                                 current_state, tNow,imu().getHeading(), headingLoop().getPosition(), imu().getRoll());  
00431                     mbedLogger().appendDiagFile(buf,3);
00432                 }
00433                 if ((tNow % 3000) == 0) {
00434                     led2()=1;
00435                     }
00436                 //update the log and diagnostics files
00437                 if ( (tNow % 31000) == 0 ) {   // 1.0 hour intervals= 3600*1000  check for testing via 31 second intervals
00438                 sprintf(buf, "hit cycle seconds  replace_logfiles interval in main loop tNow =%d  \n\n\r", tNow);
00439                     mbedLogger().appendDiagFile(buf,3);
00440                     vernum = configFileIO().logFilesStruct.logversion;
00441                     diagnum = configFileIO().logFilesStruct.diagversion;
00442                     sprintf(buf, "cycle log file names at tnow=%d at  seconds. This message should be in old diag file\n\n\r", tNow);
00443                     mbedLogger().appendDiagFile(buf,3);
00444                   cycle_logfiles(vernum,diagnum);
00445                     sprintf(buf, "cycled log files at tNow = %d at seconds, This message should be in a NEW diag file\n\n\r", tNow);
00446                     mbedLogger().appendDiagFile(buf,3);
00447                     //  close the log and diagnostics files
00448                     // increment the version numbers for each
00449                     //  save the new version numbers
00450                     // initialize new log and diagnostics files
00451                     log_loop = true;
00452                     log_function();
00453                     led3()=1; led1()=1;
00454                 }
00455             } // end else { at  **88**
00456             if(current_state == FB_EXIT) {
00457                 log_loop=true;
00458                 log_function();
00459                 led2()=1;
00460                 led4()=1;
00461                 keeprunning=0;  // and this will force exit to the main while() loop.
00462                 //  switch state to EOL_WAIT, spit out xbee messages,  wait 60 seconds or so. if keyboard comes back, do not exit
00463                 sprintf(buf, "INSIDE  main loop: BUT now called to exit out of it via FB_EXIT\n\n\r");
00464                 mbedLogger().appendDiagFile(buf,3);
00465             }
00466             if(tNow ==  90000) {  // don't wait forever  -remove this for real operations!!
00467                 keeprunning=0;
00468                 sprintf(buf, "INSIDE  main loop: Triggered FINAL timeout at 90 seconds\n\n\r");
00469                 mbedLogger().appendDiagFile(buf,3);
00470             }
00471         }  // end if(read_ticker) {
00472     }  // end while(keeprunning)
00473     
00474     mbedLogger().appendLogFile(current_state, 1);
00475     sprintf(buf, "outside of main loop:  exiting out of the leg loop via FB_EXIT  tNow= %d \n\n\r", tNow);
00476     mbedLogger().appendDiagFile(buf,3);
00477     led1()=1; led2() =1; led3()=1 ;led4() = 1;
00478     wait(5);
00479     
00480     mbedLogger().appendLogFile(current_state, 1);
00481     mbedLogger().appendLogFile(current_state, 0);
00482     sprintf(buf, "wait 5 more seconds? out of the leg loop via FB_EXIT\n\n\r");
00483     mbedLogger().appendDiagFile(buf,0);
00484     led1()=0; led2()=0; led3()= 0 ;led4()=0;
00485     configFileIO().save_FinalTime();   // saves last time before closing shop
00486     exit(0);
00487 }  // end main()