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

Revision:
82:0981b9ada820
Parent:
81:7ff2c6467892
Child:
84:eccd8e837134
--- a/main.cpp	Thu Nov 08 22:30:32 2018 +0000
+++ b/main.cpp	Fri Feb 15 16:00:17 2019 +0000
@@ -21,7 +21,8 @@
 void loop_trigger() { fsm_loop = true;} // loop trigger (used in while loop)
 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
 
-static int current_state = 0;     
+static int current_state = 0; 
+static int have_legfile = 0;    
 static bool file_opened = false;
 
 void FSM() {                    // FSM loop runs at 10 hz
@@ -34,27 +35,29 @@
 
 void log_function() {    
     // log loop runs at 1 hz
-    if (log_loop) {
+    if (log_loop) {  //start logloop8
         //when the state machine is not in SIT_IDLE state (or a random keyboard press)
         
-        if(current_state != 0) {
+        if(current_state != 0) {  //first if  - not in sit_idle/keyboard state
             if (!file_opened) {                                 //if the log file is not open, open it
-                mbedLogger().appendLogFile(current_state, 0);   //open MBED file once
+                mbedLogger().appendLogFile(current_state, 1);   //open MBED file once
                 //sdLogger().appendLogFile(current_state, 0);     //open SD file once
                            
                 file_opened = true;                             //stops it from continuing to open it
 
-                xbee().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
+                xbee().printf(">>>>>>>> Recording. Log file re opened. <<<<<<<<\n\r");
             }
+            else {
             
-            //record to Mbed file system   
+                  //just record  to the  Mbed file system   
             
-            mbedLogger().appendLogFile(current_state, 1);    //writing data
-            //sdLogger().appendLogFile(current_state, 1);    //writing data
-        }
+                   mbedLogger().appendLogFile(current_state, 1);    //writing data
+                   //sdLogger().appendLogFile(current_state, 1);    //writing data
+                 }
+        }  //end first if
         
         //when the current FSM state is zero (SIT_IDLE), close the file
-        else {
+        else {  //start first else  for current_state ==0  sit idle/keyboard
             //this can only happen once
             if (file_opened) {
                 //WRITE ONCE
@@ -68,7 +71,7 @@
                 
                 xbee().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
             }
-        }
+        }  //end first else
     }   //END OF LOG LOOP8
     
     log_loop = false;   // wait until the loop rate timer fires again
@@ -116,7 +119,7 @@
 }
 
 void setup() {
-    xbee().baud(115200);
+    //  xbee().baud(115200);  // comment out so default is 9600 for USB communications
     xbee().printf("\n\n\r 2018-11-08 FSG PCB XBee\n\n\r");
     
     //pc().baud(57600);
@@ -141,8 +144,16 @@
     
     // construct the SD card file system TEST 10/23/18
     //sd_card();
- 
+                   //  mbedLogger().initializeDiagFile();
     // load config data from files
+    mbedLogger().setLogTime();
+    int print_diag = 0;    // do not print to diag file before it is named
+    configFileIO().load_LogVers_config(print_diag);    // version numbers of the log and diag files from "logvers.txt"
+    mbedLogger().initializeDiagFile(print_diag);
+    print_diag=1;
+    configFileIO().load_LogVers_config(print_diag);    // Cnow print info to diag file
+    mbedLogger().initializeDiagFile(print_diag);
+    
     configFileIO().load_BCE_config();       // load the buoyancy engine parameters from the file "bce.txt"
     configFileIO().load_BATT_config();      // load the battery mass mover parameters from the file "batt.txt"
     
@@ -184,25 +195,41 @@
     
  
     // show that the PID gains are loading from the file
-    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(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
-    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(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
-    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(), rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
-    
-    xbee().printf("depth   P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset());
-    xbee().printf("pitch   P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
-    xbee().printf("heading P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f deg (deadband: %0.1f)\r\n", headingLoop().getControllerP(), headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(), headingLoop().getDeadband());
+    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(), 
+             bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
+    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(),
+             batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
+    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(), 
+            rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());    
+    xbee().printf("depth   P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), 
+         depthLoop().getControllerD(), depthLoop().getOutputOffset());
+    xbee().printf("pitch   P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(),
+     pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
+    xbee().printf("heading P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f deg (deadband: %0.1f)\r\n", headingLoop().getControllerP(),
+     headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(), headingLoop().getDeadband());
     
     xbee().printf("\n\r");
          
     //load sequence from file
     sequenceController().loadSequence();
+    char buf[256];
+    //xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
+    sprintf(buf, "\n\n\r in setup():  starting legController().loadleg() \n\n\r");
+    mbedLogger().appendDiagFile(buf,0);
+    have_legfile = legController().loadLeg();
     
-    //set time of logger (to current or close-to-current time)
-    mbedLogger().setLogTime();
+    //set time of logger (to current or close-to-current time)   now set earlier  at line 149
+    // mbedLogger().setLogTime();
     //sdLogger().setLogTime();
     
     //create log files if not present on file system
     mbedLogger().initializeLogFile();
+    
+    mbedLogger().appendLogFile(current_state, 1);   //write the idle state, then close
+                //sdLogger().appendLogFile(current_state, 1);     //write the idle state, then close
+                
+                mbedLogger().appendLogFile(current_state, 0);    //close log file  added jcw nov 9 2018 for test
+                //sdLogger().appendLogFile(current_state, 0);    //close log file
     //sdLogger().initializeLogFile(); // works 10/23/18
     
     setup_complete = true;    
@@ -212,16 +239,40 @@
 
 int main() {   
     setup();    //setup electronics/hardware
-    
+    // on landing, check orientation, if upside down, fix that first
     systemTicker.attach_us(&system_timer, 1000);         // Interrupt timer running at 0.001 seconds       (slower than original ADC time interval)
     
     unsigned int tNow = 0;
-
+    int vernum=0;
+    int diagnum=0;
+     char buf[256];
     xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
-    
+    sprintf(buf, "\n\n\r 2018-08-14 FSG PCB XBee line234main (setup complete) \n\n\r");
+    mbedLogger().appendDiagFile(buf,0);
+    sprintf(buf, "finished setting up and will exit\n\n\r");
+    mbedLogger().appendDiagFile(buf,3);
+    tNow=5; sprintf(buf, "log file config values logfile= %s     diag file= %s\n", configFileIO().logFilesStruct.logFileName,
+             configFileIO().logFilesStruct.diagFileName); tNow=0;
+    mbedLogger().appendDiagFile(buf,3);
+    vernum = configFileIO().logFilesStruct.logversion;
+    diagnum = configFileIO().logFilesStruct.diagversion;
+    sprintf(buf, "translated values LOG FILE VERSION number (vernum)=%d           diag file version number(diagnum) = %d\n", vernum, diagnum);
+    mbedLogger().appendDiagFile(buf,3);
+    sprintf(buf, "logfiles_struct values - direct LOG FILE VERSION ().logversion=%d           diag file version().diagversion = %d\n", 
+    configFileIO().logFilesStruct.logversion,   configFileIO().logFilesStruct.diagversion);
+    mbedLogger().appendDiagFile(buf,3);
+     sprintf(buf, "try another messgae after closing file up and then will exit\n\n\r");
+    mbedLogger().appendDiagFile(buf,0);
+    //wait(5);
+    //exit(0);
     systemTicker.attach_us(&system_timer, 1000);         // Interrupt timer running at 0.001 seconds       (slower than original ADC time interval)
+    int keeprunning = 1;
         
-    while (1) {        
+        if(have_legfile) {
+            //install the leg variables in a structure, and set the state there.
+            stateMachine().getLegParams(); //should set up everything with proper LEG_POSITION_DIVE state
+            }
+    while (keeprunning) {        
         if( read_ticker() )                         // read_ticker runs at the speed of 10 kHz (adc timing)
         {
             ++tNow;
@@ -233,7 +284,7 @@
                     fsm_loop = true;
                     FSM();
                 }
-            }
+            } // end if(currentstate..)
             
             //NOT TRANSMITTING DATA, NORMAL OPERATIONS
             else {  
@@ -250,7 +301,22 @@
                     log_loop = true;
                     log_function();
                 }
-            }
-        }
-    }
-}
\ No newline at end of file
+            } // end else {
+             if(current_state == FB_EXIT) {
+                    log_loop=true;
+                    log_function();
+                    keeprunning=0;  // and this will force exit to the main while() loop.
+                }   
+        }  // end if(read_ticker) {
+    }  // end while(keeprunning)
+    mbedLogger().appendLogFile(current_state, 1);
+     sprintf(buf, "exiting out of the leg loop via FB_EXIT\n\n\r");
+    mbedLogger().appendDiagFile(buf,0);
+    wait(25);  
+    
+    mbedLogger().appendLogFile(current_state, 1);
+    mbedLogger().appendLogFile(current_state, 0);
+    sprintf(buf, "wait 25 more seconds? out of the leg loop via FB_EXIT\n\n\r");
+    mbedLogger().appendDiagFile(buf,0);
+    exit(0);
+}  // end main()
\ No newline at end of file