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:
Thu May 02 20:34:16 2019 +0000
Revision:
86:ba3a118b0080
Parent:
85:dd8176285b6e
Child:
87:6d95f853dab3
works and gives led signals of life

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 86:ba3a118b0080 26
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 86:ba3a118b0080 150 configFileIO().load_StartTime();
joel_ssc 86:ba3a118b0080 151 // mbedLogger().setLogTime(); replaced by call in configfileIO
joel_ssc 82:0981b9ada820 152 int print_diag = 0; // do not print to diag file before it is named
joel_ssc 82:0981b9ada820 153 configFileIO().load_LogVers_config(print_diag); // version numbers of the log and diag files from "logvers.txt"
joel_ssc 82:0981b9ada820 154 mbedLogger().initializeDiagFile(print_diag);
joel_ssc 82:0981b9ada820 155 print_diag=1;
joel_ssc 82:0981b9ada820 156 configFileIO().load_LogVers_config(print_diag); // Cnow print info to diag file
joel_ssc 82:0981b9ada820 157 mbedLogger().initializeDiagFile(print_diag);
joel_ssc 82:0981b9ada820 158
tnhnrl 65:2ac186553959 159 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 65:2ac186553959 160 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 65:2ac186553959 161
tnhnrl 65:2ac186553959 162 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 65:2ac186553959 163 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 65:2ac186553959 164
tnhnrl 65:2ac186553959 165 configFileIO().load_RUDDER_config(); // load the rudder servo inner loop parameters from the file "SERVO.txt"
tnhnrl 65:2ac186553959 166 configFileIO().load_HEADING_config(); // load the rudder servo outer loop HEADING control parameters from the file "HEADING.txt" (contains neutral position)
tnhnrl 43:891baf306e0a 167
danstrider 10:085ab7328054 168 // set up the linear actuators. adc has to be running first.
tnhnrl 65:2ac186553959 169 bce().setPIDHighLimit(bce().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 170 bce().init();
tnhnrl 65:2ac186553959 171 //bce().start(); //removed start, it's handled by the interrupt
tnhnrl 67:c86a4b464682 172 bce().runLinearActuator();
mkelly10 12:a0519d11d2b6 173 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 174
tnhnrl 65:2ac186553959 175 batt().setPIDHighLimit(batt().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 176 batt().init();
tnhnrl 65:2ac186553959 177 batt().runLinearActuator(); // _init = true;
tnhnrl 65:2ac186553959 178 //batt().start();//removed start, it's handled by the interrupt
mkelly10 12:a0519d11d2b6 179 batt().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 180
tnhnrl 65:2ac186553959 181 // set up the depth, pitch, and rudder outer loop controllers
danstrider 10:085ab7328054 182 depthLoop().init();
tnhnrl 65:2ac186553959 183 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 184 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 185
danstrider 10:085ab7328054 186 pitchLoop().init();
tnhnrl 65:2ac186553959 187 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 188 pitchLoop().setCommand(stateMachine().getPitchCommand());
tnhnrl 55:f4ec445c42fe 189
tnhnrl 55:f4ec445c42fe 190 headingLoop().init();
tnhnrl 65:2ac186553959 191 //removed start, it's handled by the interrupt
tnhnrl 65:2ac186553959 192 //headingLoop().setCommand(stateMachine().getHeadingCommand()); // FIX LATER
tnhnrl 65:2ac186553959 193 //heading flag that adjust the PID error is set in the constructor
tnhnrl 65:2ac186553959 194
tnhnrl 65:2ac186553959 195 //systemTicker.attach_us(&system_timer, 10000); // Interrupt timer running at 0.01 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 196
tnhnrl 65:2ac186553959 197
tnhnrl 20:8987a9ae2bc7 198
tnhnrl 20:8987a9ae2bc7 199 // show that the PID gains are loading from the file
joel_ssc 82:0981b9ada820 200 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 201 bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
joel_ssc 82:0981b9ada820 202 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 203 batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
joel_ssc 82:0981b9ada820 204 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 205 rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
joel_ssc 82:0981b9ada820 206 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 207 depthLoop().getControllerD(), depthLoop().getOutputOffset());
joel_ssc 82:0981b9ada820 208 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 209 pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
joel_ssc 82:0981b9ada820 210 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 211 headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(), headingLoop().getDeadband());
tnhnrl 65:2ac186553959 212
tnhnrl 74:d281aaef9766 213 xbee().printf("\n\r");
tnhnrl 21:38c8544db6f4 214
tnhnrl 17:7c16b5671d0e 215 //load sequence from file
tnhnrl 17:7c16b5671d0e 216 sequenceController().loadSequence();
joel_ssc 82:0981b9ada820 217 char buf[256];
joel_ssc 82:0981b9ada820 218 //xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
joel_ssc 82:0981b9ada820 219 sprintf(buf, "\n\n\r in setup(): starting legController().loadleg() \n\n\r");
joel_ssc 86:ba3a118b0080 220 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 221 have_legfile = legController().loadLeg(); // this should be 1 if the legfile reader has found 1 or more legs
joel_ssc 86:ba3a118b0080 222 sprintf(buf, "Time is amystery, here is a message before the basic call\n");
joel_ssc 86:ba3a118b0080 223 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 224 int jj;
joel_ssc 86:ba3a118b0080 225 long int kk;
joel_ssc 86:ba3a118b0080 226 time_t secval;
joel_ssc 86:ba3a118b0080 227
joel_ssc 86:ba3a118b0080 228 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 229 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 230
joel_ssc 86:ba3a118b0080 231 secval = mbedLogger().getSystemTime();
joel_ssc 86:ba3a118b0080 232 sprintf(buf, "Time as a basic string = %s\n", ctime(&secval));
joel_ssc 86:ba3a118b0080 233 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 234
joel_ssc 86:ba3a118b0080 235 sprintf(buf, "Time is still amystery, here is a message after the basic call\n");
joel_ssc 82:0981b9ada820 236 mbedLogger().appendDiagFile(buf,0);
tnhnrl 32:f2f8ae34aadc 237
joel_ssc 82:0981b9ada820 238 //set time of logger (to current or close-to-current time) now set earlier at line 149
joel_ssc 82:0981b9ada820 239 // mbedLogger().setLogTime();
tnhnrl 65:2ac186553959 240 //sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 241
tnhnrl 47:fb3c7929d3f3 242 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 243 mbedLogger().initializeLogFile();
joel_ssc 82:0981b9ada820 244
joel_ssc 82:0981b9ada820 245 mbedLogger().appendLogFile(current_state, 1); //write the idle state, then close
joel_ssc 82:0981b9ada820 246 //sdLogger().appendLogFile(current_state, 1); //write the idle state, then close
joel_ssc 82:0981b9ada820 247
joel_ssc 82:0981b9ada820 248 mbedLogger().appendLogFile(current_state, 0); //close log file added jcw nov 9 2018 for test
joel_ssc 82:0981b9ada820 249 //sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 80:4e5d306d695b 250 //sdLogger().initializeLogFile(); // works 10/23/18
tnhnrl 65:2ac186553959 251
tnhnrl 68:8f549749b8ce 252 setup_complete = true;
tnhnrl 65:2ac186553959 253 }
tnhnrl 58:94b7fd55185e 254
joel_ssc 84:eccd8e837134 255 void cycle_logfiles(int logversion, int diagversion) {
joel_ssc 84:eccd8e837134 256 //int logversion;
joel_ssc 84:eccd8e837134 257 //int diagversion;
joel_ssc 84:eccd8e837134 258 char bufx[256];
joel_ssc 84:eccd8e837134 259 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 260 mbedLogger().appendDiagFile(bufx,0);
joel_ssc 84:eccd8e837134 261 mbedLogger().appendLogFile(current_state, 0); //both files are now closed
joel_ssc 84:eccd8e837134 262 //use the present values and increment
joel_ssc 84:eccd8e837134 263 //logversion = configFileIO().logFilesStruct.logversion + 1;
joel_ssc 84:eccd8e837134 264 //diagversion = configFileIO().logFilesStruct.diagversion + 1;
joel_ssc 84:eccd8e837134 265 configFileIO().saveLogVersData(logversion+1, diagversion+1); // updates the file logvers.txt
joel_ssc 84:eccd8e837134 266 configFileIO().load_LogVers_config(0); // now read them back into the structure
joel_ssc 84:eccd8e837134 267 mbedLogger().initializeDiagFile(0); //don't print before initializing
joel_ssc 84:eccd8e837134 268
joel_ssc 84:eccd8e837134 269
joel_ssc 84:eccd8e837134 270 mbedLogger().initializeLogFile();
joel_ssc 84:eccd8e837134 271 mbedLogger().initializeDiagFile(1);
joel_ssc 84:eccd8e837134 272
joel_ssc 84:eccd8e837134 273 }
joel_ssc 84:eccd8e837134 274
tnhnrl 68:8f549749b8ce 275 /*************************** v1.8 **************************/
tnhnrl 68:8f549749b8ce 276
tnhnrl 73:f6f378311c8d 277 int main() {
tnhnrl 73:f6f378311c8d 278 setup(); //setup electronics/hardware
joel_ssc 82:0981b9ada820 279 // on landing, check orientation, if upside down, fix that first
tnhnrl 72:250b2665755c 280 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
joel_ssc 86:ba3a118b0080 281 led2()=1; led4()=0;
tnhnrl 65:2ac186553959 282 unsigned int tNow = 0;
joel_ssc 82:0981b9ada820 283 int vernum=0;
joel_ssc 82:0981b9ada820 284 int diagnum=0;
joel_ssc 82:0981b9ada820 285 char buf[256];
tnhnrl 75:92e79d23d29a 286 xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
joel_ssc 86:ba3a118b0080 287 sprintf(buf, "\n\n\r 2019-feb-25 FSG PCB XBee line271main (setup complete) \n\n\r");
joel_ssc 86:ba3a118b0080 288 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 289 sprintf(buf, "finished setting up and will exit\n\n\r");
joel_ssc 82:0981b9ada820 290 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 291 //tNow=5; sprintf(buf, "log file config values logfile= %s diag file= %s\n", configFileIO().logFilesStruct.logFileName,
joel_ssc 84:eccd8e837134 292 // configFileIO().logFilesStruct.diagFileName); tNow=0;
joel_ssc 82:0981b9ada820 293 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 294 vernum = configFileIO().logFilesStruct.logversion;
joel_ssc 82:0981b9ada820 295 diagnum = configFileIO().logFilesStruct.diagversion;
joel_ssc 82:0981b9ada820 296 sprintf(buf, "translated values LOG FILE VERSION number (vernum)=%d diag file version number(diagnum) = %d\n", vernum, diagnum);
joel_ssc 82:0981b9ada820 297 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 298 sprintf(buf, "logfiles_struct values - direct LOG FILE VERSION ().logversion=%d diag file version().diagversion = %d\n",
joel_ssc 82:0981b9ada820 299 configFileIO().logFilesStruct.logversion, configFileIO().logFilesStruct.diagversion);
joel_ssc 82:0981b9ada820 300 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 301 //sprintf(buf, "try another message after closing file up and then will exit\n\n\r");
joel_ssc 86:ba3a118b0080 302 //mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 303
joel_ssc 84:eccd8e837134 304
joel_ssc 84:eccd8e837134 305 // increment the log file names once
joel_ssc 84:eccd8e837134 306 //cycle_logfiles(vernum,diagnum);
joel_ssc 84:eccd8e837134 307 //sprintf(buf, "This message should be in a new diag file\n\n\r");
joel_ssc 84:eccd8e837134 308 //mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 309 // mbedLogger().appendLogFile(current_state, 1);
joel_ssc 82:0981b9ada820 310 //wait(5);
joel_ssc 82:0981b9ada820 311 //exit(0);
tnhnrl 56:48a8a5a65b82 312 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
joel_ssc 82:0981b9ada820 313 int keeprunning = 1;
tnhnrl 65:2ac186553959 314
joel_ssc 82:0981b9ada820 315 if(have_legfile) {
joel_ssc 82:0981b9ada820 316 //install the leg variables in a structure, and set the state there.
joel_ssc 82:0981b9ada820 317 stateMachine().getLegParams(); //should set up everything with proper LEG_POSITION_DIVE state
joel_ssc 86:ba3a118b0080 318 sprintf(buf, "have_legfile succeeded \n\r");
joel_ssc 86:ba3a118b0080 319 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 320 }
joel_ssc 86:ba3a118b0080 321 if(!have_legfile) {
joel_ssc 86:ba3a118b0080 322 sprintf(buf, "have_legfile failed! .... will exit\n\n\r");
joel_ssc 86:ba3a118b0080 323 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 324 keeprunning = 0;
joel_ssc 85:dd8176285b6e 325 }
joel_ssc 86:ba3a118b0080 326
joel_ssc 84:eccd8e837134 327 while (keeprunning) {
joel_ssc 84:eccd8e837134 328 if( read_ticker() ) { // read_ticker runs at the speed of 10 kHz (adc timing)
tnhnrl 65:2ac186553959 329 ++tNow;
mkelly10 51:c5c40272ecc3 330
tnhnrl 81:7ff2c6467892 331 //Note to self: Retest data transmission code.
tnhnrl 81:7ff2c6467892 332 //This is currently running at 0.1 second intervals (10 hz) and was working well for data transmission
tnhnrl 74:d281aaef9766 333 if (current_state == TX_MBED_LOG or current_state == RX_SEQUENCE) {
tnhnrl 73:f6f378311c8d 334 if ( (tNow % 100) == 0 ) { // 0.01 second intervals (100 Hz)
tnhnrl 66:0f20870117b7 335 fsm_loop = true;
tnhnrl 66:0f20870117b7 336 FSM();
tnhnrl 66:0f20870117b7 337 }
joel_ssc 82:0981b9ada820 338 } // end if(currentstate..)
joel_ssc 84:eccd8e837134 339
tnhnrl 67:c86a4b464682 340 //NOT TRANSMITTING DATA, NORMAL OPERATIONS
joel_ssc 84:eccd8e837134 341 else { // **88**
joel_ssc 84:eccd8e837134 342 //FSM
tnhnrl 68:8f549749b8ce 343 if ( (tNow % 100) == 0 ) { // 0.1 second intervals
tnhnrl 66:0f20870117b7 344 fsm_loop = true;
tnhnrl 66:0f20870117b7 345 FSM();
joel_ssc 84:eccd8e837134 346
tnhnrl 73:f6f378311c8d 347 //get commands and update GUI
tnhnrl 73:f6f378311c8d 348 gui().getCommandFSM();
joel_ssc 84:eccd8e837134 349 }
joel_ssc 84:eccd8e837134 350 //LOGGING
joel_ssc 84:eccd8e837134 351 if ( (tNow % 1000) == 0 ) { // 1.0 second intervals
joel_ssc 84:eccd8e837134 352 log_loop = true;
joel_ssc 84:eccd8e837134 353 log_function();
joel_ssc 85:dd8176285b6e 354 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 86:ba3a118b0080 355 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 356 led3()=0; led2()=0;
joel_ssc 86:ba3a118b0080 357 }
joel_ssc 86:ba3a118b0080 358 if ( (tNow % 5000) == 0 ) { // 5 second intervals
joel_ssc 86:ba3a118b0080 359
joel_ssc 86:ba3a118b0080 360 led3()=1; led2()=1;
joel_ssc 84:eccd8e837134 361 }
joel_ssc 84:eccd8e837134 362 //update the log and diagnostics files
joel_ssc 85:dd8176285b6e 363 if ( (tNow % 31000) == 0 ) { // 1.0 hour intervals= 3600*1000 check for testing via 4 second intervals
joel_ssc 85:dd8176285b6e 364 sprintf(buf, "hit cycle seconds replace_logfiles interval in main loop tNow =%d \n\n\r", tNow);
joel_ssc 85:dd8176285b6e 365 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 366 vernum = configFileIO().logFilesStruct.logversion;
joel_ssc 84:eccd8e837134 367 diagnum = configFileIO().logFilesStruct.diagversion;
joel_ssc 85:dd8176285b6e 368 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 369 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 370 cycle_logfiles(vernum,diagnum);
joel_ssc 85:dd8176285b6e 371 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 372 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 373 // close the log and diagnostics files
joel_ssc 84:eccd8e837134 374 // increment the version numbers for each
joel_ssc 84:eccd8e837134 375 // save the new version numbers
joel_ssc 84:eccd8e837134 376 // initialize new log and diagnostics files
tnhnrl 66:0f20870117b7 377 log_loop = true;
tnhnrl 66:0f20870117b7 378 log_function();
joel_ssc 86:ba3a118b0080 379 led3()=1;
tnhnrl 66:0f20870117b7 380 }
joel_ssc 84:eccd8e837134 381 } // end else { at **88**
joel_ssc 84:eccd8e837134 382 if(current_state == FB_EXIT) {
joel_ssc 84:eccd8e837134 383 log_loop=true;
joel_ssc 86:ba3a118b0080 384 log_function(); led2()=1;
joel_ssc 84:eccd8e837134 385 keeprunning=0; // and this will force exit to the main while() loop.
joel_ssc 85:dd8176285b6e 386 // 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 387 sprintf(buf, "INSIDE main loop: BUT now called to exit out of it via FB_EXIT\n\n\r");
joel_ssc 86:ba3a118b0080 388 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 389 }
joel_ssc 82:0981b9ada820 390 } // end if(read_ticker) {
joel_ssc 82:0981b9ada820 391 } // end while(keeprunning)
joel_ssc 82:0981b9ada820 392 mbedLogger().appendLogFile(current_state, 1);
joel_ssc 84:eccd8e837134 393 sprintf(buf, "outside of main loop: exiting out of the leg loop via FB_EXIT\n\n\r");
joel_ssc 86:ba3a118b0080 394 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 395 led1()=1; led2() =1; led3()=1 ;led4() = 1;
joel_ssc 86:ba3a118b0080 396 wait(5);
joel_ssc 86:ba3a118b0080 397
joel_ssc 82:0981b9ada820 398 mbedLogger().appendLogFile(current_state, 1);
joel_ssc 82:0981b9ada820 399 mbedLogger().appendLogFile(current_state, 0);
joel_ssc 86:ba3a118b0080 400 sprintf(buf, "wait 5 more seconds? out of the leg loop via FB_EXIT\n\n\r");
joel_ssc 82:0981b9ada820 401 mbedLogger().appendDiagFile(buf,0);
joel_ssc 86:ba3a118b0080 402 led1()=0; led2()=0; led3()= 0 ;led4()=0;
joel_ssc 82:0981b9ada820 403 exit(0);
joel_ssc 82:0981b9ada820 404 } // end main()