update with altimeter, swimfile.txt endleg.txt, etc see changes_13sep.txt also reset_PI()

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
joel_ssc
Date:
Fri May 17 14:25:03 2019 +0000
Revision:
95:1aac4086928a
Parent:
92:52a91656458a
Child:
97:2b4f78a54227
chnage logic for find_neutral in main.cpp

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