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

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
joel_ssc
Date:
Mon Feb 25 21:26:34 2019 +0000
Revision:
85:dd8176285b6e
Parent:
84:eccd8e837134
Child:
86:ba3a118b0080
tests imu.roll

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 85:dd8176285b6e 25 static int have_legfile = 0;
joel_ssc 85:dd8176285b6e 26 static int upside_down = 0;
tnhnrl 65:2ac186553959 27 static bool file_opened = false;
tnhnrl 65:2ac186553959 28
tnhnrl 73:f6f378311c8d 29 void FSM() { // FSM loop runs at 10 hz
tnhnrl 65:2ac186553959 30 if(fsm_loop) {
tnhnrl 73:f6f378311c8d 31 // led one removed
tnhnrl 65:2ac186553959 32 fsm_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 33 current_state = stateMachine().runStateMachine(); //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
tnhnrl 65:2ac186553959 34 }
tnhnrl 65:2ac186553959 35 }
tnhnrl 65:2ac186553959 36
tnhnrl 65:2ac186553959 37 void log_function() {
tnhnrl 65:2ac186553959 38 // log loop runs at 1 hz
joel_ssc 82:0981b9ada820 39 if (log_loop) { //start logloop8
tnhnrl 65:2ac186553959 40 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
tnhnrl 65:2ac186553959 41
joel_ssc 82:0981b9ada820 42 if(current_state != 0) { //first if - not in sit_idle/keyboard state
tnhnrl 65:2ac186553959 43 if (!file_opened) { //if the log file is not open, open it
joel_ssc 82:0981b9ada820 44 mbedLogger().appendLogFile(current_state, 1); //open MBED file once
tnhnrl 80:4e5d306d695b 45 //sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 65:2ac186553959 46
tnhnrl 65:2ac186553959 47 file_opened = true; //stops it from continuing to open it
tnhnrl 56:48a8a5a65b82 48
joel_ssc 82:0981b9ada820 49 xbee().printf(">>>>>>>> Recording. Log file re opened. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 50 }
joel_ssc 82:0981b9ada820 51 else {
tnhnrl 65:2ac186553959 52
joel_ssc 82:0981b9ada820 53 //just record to the Mbed file system
tnhnrl 65:2ac186553959 54
joel_ssc 82:0981b9ada820 55 mbedLogger().appendLogFile(current_state, 1); //writing data
joel_ssc 82:0981b9ada820 56 //sdLogger().appendLogFile(current_state, 1); //writing data
joel_ssc 82:0981b9ada820 57 }
joel_ssc 82:0981b9ada820 58 } //end first if
tnhnrl 65:2ac186553959 59
tnhnrl 65:2ac186553959 60 //when the current FSM state is zero (SIT_IDLE), close the file
joel_ssc 82:0981b9ada820 61 else { //start first else for current_state ==0 sit idle/keyboard
tnhnrl 65:2ac186553959 62 //this can only happen once
tnhnrl 65:2ac186553959 63 if (file_opened) {
tnhnrl 67:c86a4b464682 64 //WRITE ONCE
tnhnrl 67:c86a4b464682 65 mbedLogger().appendLogFile(current_state, 1); //write the idle state, then close
tnhnrl 80:4e5d306d695b 66 //sdLogger().appendLogFile(current_state, 1); //write the idle state, then close
tnhnrl 67:c86a4b464682 67
tnhnrl 65:2ac186553959 68 mbedLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 80:4e5d306d695b 69 //sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 65:2ac186553959 70
tnhnrl 65:2ac186553959 71 file_opened = false;
tnhnrl 65:2ac186553959 72
tnhnrl 74:d281aaef9766 73 xbee().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 74 }
joel_ssc 82:0981b9ada820 75 } //end first else
tnhnrl 74:d281aaef9766 76 } //END OF LOG LOOP8
tnhnrl 65:2ac186553959 77
tnhnrl 65:2ac186553959 78 log_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 79 }
tnhnrl 56:48a8a5a65b82 80
tnhnrl 72:250b2665755c 81 //single system timer to run hardware/electronics timing
tnhnrl 65:2ac186553959 82 static void system_timer(void) {
tnhnrl 65:2ac186553959 83 bTick = 1;
tnhnrl 65:2ac186553959 84
tnhnrl 56:48a8a5a65b82 85 timer_counter++;
tnhnrl 56:48a8a5a65b82 86
tnhnrl 56:48a8a5a65b82 87 //only start these updates when everything is properly setup (through setup function)
tnhnrl 56:48a8a5a65b82 88 if (setup_complete) {
tnhnrl 74:d281aaef9766 89 if ( (timer_counter % 5) == 0) { //this runs at 0.005 second intervals (200 Hz)
tnhnrl 65:2ac186553959 90 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 91 }
tnhnrl 65:2ac186553959 92
tnhnrl 65:2ac186553959 93 if ( (timer_counter % 10) == 0) {
tnhnrl 65:2ac186553959 94 bce().update(); //update() inside LinearActuator class (running at 0.01 second intervals)
tnhnrl 65:2ac186553959 95 batt().update();
tnhnrl 65:2ac186553959 96 }
tnhnrl 65:2ac186553959 97
tnhnrl 65:2ac186553959 98 if ( (timer_counter % 20) == 0 ) { // 0.02 second intervals
tnhnrl 74:d281aaef9766 99 rudder().runServo();
tnhnrl 65:2ac186553959 100 }
tnhnrl 65:2ac186553959 101
tnhnrl 65:2ac186553959 102 if ( (timer_counter % 50) == 0 ) { // 0.05 second intervals
tnhnrl 67:c86a4b464682 103 imu().runIMU();
tnhnrl 65:2ac186553959 104 }
tnhnrl 73:f6f378311c8d 105
tnhnrl 65:2ac186553959 106 if ( (timer_counter % 100) == 0) { // 100,000 microseconds = 0.1 second intervals
tnhnrl 65:2ac186553959 107 depthLoop().runOuterLoop();
tnhnrl 65:2ac186553959 108 pitchLoop().runOuterLoop();
tnhnrl 65:2ac186553959 109 headingLoop().runOuterLoop();
tnhnrl 65:2ac186553959 110 }
tnhnrl 73:f6f378311c8d 111
tnhnrl 73:f6f378311c8d 112 if ( (timer_counter % 1000) == 0) { // update at 1.0 second intervals
tnhnrl 73:f6f378311c8d 113 //gui().updateGUI();
tnhnrl 73:f6f378311c8d 114 }
tnhnrl 74:d281aaef9766 115
tnhnrl 74:d281aaef9766 116 if ( (timer_counter % 30000) == 0) { // update at 30.0 second intervals
tnhnrl 74:d281aaef9766 117 //pc().printf("XB!\n");
tnhnrl 74:d281aaef9766 118 }
tnhnrl 56:48a8a5a65b82 119 }
tnhnrl 56:48a8a5a65b82 120 }
tnhnrl 56:48a8a5a65b82 121
danstrider 10:085ab7328054 122 void setup() {
joel_ssc 82:0981b9ada820 123 // xbee().baud(115200); // comment out so default is 9600 for USB communications
tnhnrl 81:7ff2c6467892 124 xbee().printf("\n\n\r 2018-11-08 FSG PCB XBee\n\n\r");
tnhnrl 74:d281aaef9766 125
tnhnrl 74:d281aaef9766 126 //pc().baud(57600);
tnhnrl 65:2ac186553959 127
danstrider 10:085ab7328054 128 // start up the system timer
tnhnrl 65:2ac186553959 129 //systemTimer().start();
tnhnrl 20:8987a9ae2bc7 130
danstrider 10:085ab7328054 131 // set up and start the adc. This runs on a fixed interval and is interrupt driven
tnhnrl 65:2ac186553959 132 adc().initialize();
tnhnrl 67:c86a4b464682 133 //one central interrupt is updating the ADC (not using the start function)
tnhnrl 65:2ac186553959 134
tnhnrl 65:2ac186553959 135 // setup and run the rudder(servo) pwm signal (start the ticker)
tnhnrl 65:2ac186553959 136 //rudder().init();
tnhnrl 74:d281aaef9766 137 xbee().printf("Rudder servo initialized!\n\r");
danstrider 10:085ab7328054 138
danstrider 10:085ab7328054 139 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 140 imu().initialize();
tnhnrl 65:2ac186553959 141 //imu().start();
danstrider 10:085ab7328054 142
tnhnrl 48:20e681885161 143 // construct the MBED local file system
danstrider 10:085ab7328054 144 local();
tnhnrl 48:20e681885161 145
tnhnrl 79:3688c3a0d7f4 146 // construct the SD card file system TEST 10/23/18
tnhnrl 80:4e5d306d695b 147 //sd_card();
joel_ssc 82:0981b9ada820 148 // mbedLogger().initializeDiagFile();
danstrider 10:085ab7328054 149 // load config data from files
joel_ssc 82:0981b9ada820 150 mbedLogger().setLogTime();
joel_ssc 82:0981b9ada820 151 int print_diag = 0; // do not print to diag file before it is named
joel_ssc 82:0981b9ada820 152 configFileIO().load_LogVers_config(print_diag); // version numbers of the log and diag files from "logvers.txt"
joel_ssc 82:0981b9ada820 153 mbedLogger().initializeDiagFile(print_diag);
joel_ssc 82:0981b9ada820 154 print_diag=1;
joel_ssc 82:0981b9ada820 155 configFileIO().load_LogVers_config(print_diag); // Cnow print info to diag file
joel_ssc 82:0981b9ada820 156 mbedLogger().initializeDiagFile(print_diag);
joel_ssc 82:0981b9ada820 157
tnhnrl 65:2ac186553959 158 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 65:2ac186553959 159 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 65:2ac186553959 160
tnhnrl 65:2ac186553959 161 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 65:2ac186553959 162 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 65:2ac186553959 163
tnhnrl 65:2ac186553959 164 configFileIO().load_RUDDER_config(); // load the rudder servo inner loop parameters from the file "SERVO.txt"
tnhnrl 65:2ac186553959 165 configFileIO().load_HEADING_config(); // load the rudder servo outer loop HEADING control parameters from the file "HEADING.txt" (contains neutral position)
tnhnrl 43:891baf306e0a 166
danstrider 10:085ab7328054 167 // set up the linear actuators. adc has to be running first.
tnhnrl 65:2ac186553959 168 bce().setPIDHighLimit(bce().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 169 bce().init();
tnhnrl 65:2ac186553959 170 //bce().start(); //removed start, it's handled by the interrupt
tnhnrl 67:c86a4b464682 171 bce().runLinearActuator();
mkelly10 12:a0519d11d2b6 172 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 173
tnhnrl 65:2ac186553959 174 batt().setPIDHighLimit(batt().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 175 batt().init();
tnhnrl 65:2ac186553959 176 batt().runLinearActuator(); // _init = true;
tnhnrl 65:2ac186553959 177 //batt().start();//removed start, it's handled by the interrupt
mkelly10 12:a0519d11d2b6 178 batt().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 179
tnhnrl 65:2ac186553959 180 // set up the depth, pitch, and rudder outer loop controllers
danstrider 10:085ab7328054 181 depthLoop().init();
tnhnrl 65:2ac186553959 182 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 183 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 184
danstrider 10:085ab7328054 185 pitchLoop().init();
tnhnrl 65:2ac186553959 186 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 187 pitchLoop().setCommand(stateMachine().getPitchCommand());
tnhnrl 55:f4ec445c42fe 188
tnhnrl 55:f4ec445c42fe 189 headingLoop().init();
tnhnrl 65:2ac186553959 190 //removed start, it's handled by the interrupt
tnhnrl 65:2ac186553959 191 //headingLoop().setCommand(stateMachine().getHeadingCommand()); // FIX LATER
tnhnrl 65:2ac186553959 192 //heading flag that adjust the PID error is set in the constructor
tnhnrl 65:2ac186553959 193
tnhnrl 65:2ac186553959 194 //systemTicker.attach_us(&system_timer, 10000); // Interrupt timer running at 0.01 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 195
tnhnrl 65:2ac186553959 196
tnhnrl 20:8987a9ae2bc7 197
tnhnrl 20:8987a9ae2bc7 198 // show that the PID gains are loading from the file
joel_ssc 82:0981b9ada820 199 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 200 bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
joel_ssc 82:0981b9ada820 201 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 202 batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
joel_ssc 82:0981b9ada820 203 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 204 rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
joel_ssc 82:0981b9ada820 205 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 206 depthLoop().getControllerD(), depthLoop().getOutputOffset());
joel_ssc 82:0981b9ada820 207 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 208 pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
joel_ssc 82:0981b9ada820 209 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 210 headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(), headingLoop().getDeadband());
tnhnrl 65:2ac186553959 211
tnhnrl 74:d281aaef9766 212 xbee().printf("\n\r");
tnhnrl 21:38c8544db6f4 213
tnhnrl 17:7c16b5671d0e 214 //load sequence from file
tnhnrl 17:7c16b5671d0e 215 sequenceController().loadSequence();
joel_ssc 82:0981b9ada820 216 char buf[256];
joel_ssc 82:0981b9ada820 217 //xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
joel_ssc 82:0981b9ada820 218 sprintf(buf, "\n\n\r in setup(): starting legController().loadleg() \n\n\r");
joel_ssc 82:0981b9ada820 219 mbedLogger().appendDiagFile(buf,0);
joel_ssc 85:dd8176285b6e 220 have_legfile = legController().loadLeg(); // this should be 1 if the legfile reader has found 1 or more legs
tnhnrl 32:f2f8ae34aadc 221
joel_ssc 82:0981b9ada820 222 //set time of logger (to current or close-to-current time) now set earlier at line 149
joel_ssc 82:0981b9ada820 223 // mbedLogger().setLogTime();
tnhnrl 65:2ac186553959 224 //sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 225
tnhnrl 47:fb3c7929d3f3 226 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 227 mbedLogger().initializeLogFile();
joel_ssc 82:0981b9ada820 228
joel_ssc 82:0981b9ada820 229 mbedLogger().appendLogFile(current_state, 1); //write the idle state, then close
joel_ssc 82:0981b9ada820 230 //sdLogger().appendLogFile(current_state, 1); //write the idle state, then close
joel_ssc 82:0981b9ada820 231
joel_ssc 82:0981b9ada820 232 mbedLogger().appendLogFile(current_state, 0); //close log file added jcw nov 9 2018 for test
joel_ssc 82:0981b9ada820 233 //sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 80:4e5d306d695b 234 //sdLogger().initializeLogFile(); // works 10/23/18
tnhnrl 65:2ac186553959 235
tnhnrl 68:8f549749b8ce 236 setup_complete = true;
tnhnrl 65:2ac186553959 237 }
tnhnrl 58:94b7fd55185e 238
joel_ssc 84:eccd8e837134 239 void cycle_logfiles(int logversion, int diagversion) {
joel_ssc 84:eccd8e837134 240 //int logversion;
joel_ssc 84:eccd8e837134 241 //int diagversion;
joel_ssc 84:eccd8e837134 242 char bufx[256];
joel_ssc 84:eccd8e837134 243 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 244 mbedLogger().appendDiagFile(bufx,0);
joel_ssc 84:eccd8e837134 245 mbedLogger().appendLogFile(current_state, 0); //both files are now closed
joel_ssc 84:eccd8e837134 246 //use the present values and increment
joel_ssc 84:eccd8e837134 247 //logversion = configFileIO().logFilesStruct.logversion + 1;
joel_ssc 84:eccd8e837134 248 //diagversion = configFileIO().logFilesStruct.diagversion + 1;
joel_ssc 84:eccd8e837134 249 configFileIO().saveLogVersData(logversion+1, diagversion+1); // updates the file logvers.txt
joel_ssc 84:eccd8e837134 250 configFileIO().load_LogVers_config(0); // now read them back into the structure
joel_ssc 84:eccd8e837134 251 mbedLogger().initializeDiagFile(0); //don't print before initializing
joel_ssc 84:eccd8e837134 252
joel_ssc 84:eccd8e837134 253
joel_ssc 84:eccd8e837134 254 mbedLogger().initializeLogFile();
joel_ssc 84:eccd8e837134 255 mbedLogger().initializeDiagFile(1);
joel_ssc 84:eccd8e837134 256
joel_ssc 84:eccd8e837134 257 }
joel_ssc 84:eccd8e837134 258
tnhnrl 68:8f549749b8ce 259 /*************************** v1.8 **************************/
tnhnrl 68:8f549749b8ce 260
tnhnrl 73:f6f378311c8d 261 int main() {
tnhnrl 73:f6f378311c8d 262 setup(); //setup electronics/hardware
joel_ssc 82:0981b9ada820 263 // on landing, check orientation, if upside down, fix that first
tnhnrl 72:250b2665755c 264 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
tnhnrl 68:8f549749b8ce 265
tnhnrl 65:2ac186553959 266 unsigned int tNow = 0;
joel_ssc 82:0981b9ada820 267 int vernum=0;
joel_ssc 82:0981b9ada820 268 int diagnum=0;
joel_ssc 82:0981b9ada820 269 char buf[256];
tnhnrl 75:92e79d23d29a 270 xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
joel_ssc 82:0981b9ada820 271 sprintf(buf, "\n\n\r 2018-08-14 FSG PCB XBee line234main (setup complete) \n\n\r");
joel_ssc 82:0981b9ada820 272 mbedLogger().appendDiagFile(buf,0);
joel_ssc 82:0981b9ada820 273 sprintf(buf, "finished setting up and will exit\n\n\r");
joel_ssc 82:0981b9ada820 274 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 275 //tNow=5; sprintf(buf, "log file config values logfile= %s diag file= %s\n", configFileIO().logFilesStruct.logFileName,
joel_ssc 84:eccd8e837134 276 // configFileIO().logFilesStruct.diagFileName); tNow=0;
joel_ssc 82:0981b9ada820 277 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 278 vernum = configFileIO().logFilesStruct.logversion;
joel_ssc 82:0981b9ada820 279 diagnum = configFileIO().logFilesStruct.diagversion;
joel_ssc 82:0981b9ada820 280 sprintf(buf, "translated values LOG FILE VERSION number (vernum)=%d diag file version number(diagnum) = %d\n", vernum, diagnum);
joel_ssc 82:0981b9ada820 281 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 282 sprintf(buf, "logfiles_struct values - direct LOG FILE VERSION ().logversion=%d diag file version().diagversion = %d\n",
joel_ssc 82:0981b9ada820 283 configFileIO().logFilesStruct.logversion, configFileIO().logFilesStruct.diagversion);
joel_ssc 82:0981b9ada820 284 mbedLogger().appendDiagFile(buf,3);
joel_ssc 85:dd8176285b6e 285 sprintf(buf, "try another message after closing file up and then will exit\n\n\r");
joel_ssc 82:0981b9ada820 286 mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 287
joel_ssc 84:eccd8e837134 288
joel_ssc 84:eccd8e837134 289 // increment the log file names once
joel_ssc 84:eccd8e837134 290 //cycle_logfiles(vernum,diagnum);
joel_ssc 84:eccd8e837134 291 //sprintf(buf, "This message should be in a new diag file\n\n\r");
joel_ssc 84:eccd8e837134 292 //mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 293 // mbedLogger().appendLogFile(current_state, 1);
joel_ssc 82:0981b9ada820 294 //wait(5);
joel_ssc 82:0981b9ada820 295 //exit(0);
tnhnrl 56:48a8a5a65b82 296 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
joel_ssc 82:0981b9ada820 297 int keeprunning = 1;
tnhnrl 65:2ac186553959 298
joel_ssc 82:0981b9ada820 299 if(have_legfile) {
joel_ssc 82:0981b9ada820 300 //install the leg variables in a structure, and set the state there.
joel_ssc 82:0981b9ada820 301 stateMachine().getLegParams(); //should set up everything with proper LEG_POSITION_DIVE state
joel_ssc 82:0981b9ada820 302 }
joel_ssc 85:dd8176285b6e 303 if(upside_down ) { // should this be done inside the statemachine?
joel_ssc 85:dd8176285b6e 304 //turn right side up ;
joel_ssc 85:dd8176285b6e 305 upside_down =0;
joel_ssc 85:dd8176285b6e 306 }
joel_ssc 84:eccd8e837134 307 while (keeprunning) {
joel_ssc 84:eccd8e837134 308 if( read_ticker() ) { // read_ticker runs at the speed of 10 kHz (adc timing)
tnhnrl 65:2ac186553959 309 ++tNow;
mkelly10 51:c5c40272ecc3 310
tnhnrl 81:7ff2c6467892 311 //Note to self: Retest data transmission code.
tnhnrl 81:7ff2c6467892 312 //This is currently running at 0.1 second intervals (10 hz) and was working well for data transmission
tnhnrl 74:d281aaef9766 313 if (current_state == TX_MBED_LOG or current_state == RX_SEQUENCE) {
tnhnrl 73:f6f378311c8d 314 if ( (tNow % 100) == 0 ) { // 0.01 second intervals (100 Hz)
tnhnrl 66:0f20870117b7 315 fsm_loop = true;
tnhnrl 66:0f20870117b7 316 FSM();
tnhnrl 66:0f20870117b7 317 }
joel_ssc 82:0981b9ada820 318 } // end if(currentstate..)
joel_ssc 84:eccd8e837134 319
tnhnrl 67:c86a4b464682 320 //NOT TRANSMITTING DATA, NORMAL OPERATIONS
joel_ssc 84:eccd8e837134 321 else { // **88**
joel_ssc 84:eccd8e837134 322 //FSM
tnhnrl 68:8f549749b8ce 323 if ( (tNow % 100) == 0 ) { // 0.1 second intervals
tnhnrl 66:0f20870117b7 324 fsm_loop = true;
tnhnrl 66:0f20870117b7 325 FSM();
joel_ssc 84:eccd8e837134 326
tnhnrl 73:f6f378311c8d 327 //get commands and update GUI
tnhnrl 73:f6f378311c8d 328 gui().getCommandFSM();
joel_ssc 84:eccd8e837134 329 }
joel_ssc 84:eccd8e837134 330 //LOGGING
joel_ssc 84:eccd8e837134 331 if ( (tNow % 1000) == 0 ) { // 1.0 second intervals
joel_ssc 84:eccd8e837134 332 log_loop = true;
joel_ssc 84:eccd8e837134 333 log_function();
joel_ssc 85:dd8176285b6e 334 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 84:eccd8e837134 335 mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 336 }
joel_ssc 84:eccd8e837134 337 //update the log and diagnostics files
joel_ssc 85:dd8176285b6e 338 if ( (tNow % 31000) == 0 ) { // 1.0 hour intervals= 3600*1000 check for testing via 4 second intervals
joel_ssc 85:dd8176285b6e 339 sprintf(buf, "hit cycle seconds replace_logfiles interval in main loop tNow =%d \n\n\r", tNow);
joel_ssc 85:dd8176285b6e 340 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 341 vernum = configFileIO().logFilesStruct.logversion;
joel_ssc 84:eccd8e837134 342 diagnum = configFileIO().logFilesStruct.diagversion;
joel_ssc 85:dd8176285b6e 343 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 84:eccd8e837134 344 mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 345 cycle_logfiles(vernum,diagnum);
joel_ssc 85:dd8176285b6e 346 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 347 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 348 // close the log and diagnostics files
joel_ssc 84:eccd8e837134 349 // increment the version numbers for each
joel_ssc 84:eccd8e837134 350 // save the new version numbers
joel_ssc 84:eccd8e837134 351 // initialize new log and diagnostics files
tnhnrl 66:0f20870117b7 352 log_loop = true;
tnhnrl 66:0f20870117b7 353 log_function();
tnhnrl 66:0f20870117b7 354 }
joel_ssc 84:eccd8e837134 355 } // end else { at **88**
joel_ssc 84:eccd8e837134 356 if(current_state == FB_EXIT) {
joel_ssc 84:eccd8e837134 357 log_loop=true;
joel_ssc 84:eccd8e837134 358 log_function();
joel_ssc 84:eccd8e837134 359 keeprunning=0; // and this will force exit to the main while() loop.
joel_ssc 85:dd8176285b6e 360 // 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 361 sprintf(buf, "INSIDE main loop: BUT now called to exit out of it via FB_EXIT\n\n\r");
joel_ssc 84:eccd8e837134 362 mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 363 }
joel_ssc 82:0981b9ada820 364 } // end if(read_ticker) {
joel_ssc 82:0981b9ada820 365 } // end while(keeprunning)
joel_ssc 82:0981b9ada820 366 mbedLogger().appendLogFile(current_state, 1);
joel_ssc 84:eccd8e837134 367 sprintf(buf, "outside of main loop: exiting out of the leg loop via FB_EXIT\n\n\r");
joel_ssc 82:0981b9ada820 368 mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 369 wait(25);
joel_ssc 84:eccd8e837134 370
joel_ssc 82:0981b9ada820 371 mbedLogger().appendLogFile(current_state, 1);
joel_ssc 82:0981b9ada820 372 mbedLogger().appendLogFile(current_state, 0);
joel_ssc 82:0981b9ada820 373 sprintf(buf, "wait 25 more seconds? out of the leg loop via FB_EXIT\n\n\r");
joel_ssc 82:0981b9ada820 374 mbedLogger().appendDiagFile(buf,0);
joel_ssc 82:0981b9ada820 375 exit(0);
joel_ssc 82:0981b9ada820 376 } // end main()