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:
Wed May 08 13:24:04 2019 +0000
Revision:
87:6d95f853dab3
Parent:
86:ba3a118b0080
Child:
88:1813f583cee9
caught up all but main.cpp to 18apr version

Who changed what in which revision?

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