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 09 14:26:40 2019 +0000
Revision:
88:1813f583cee9
Parent:
87:6d95f853dab3
Child:
92:52a91656458a
all functionality to splashdown, find neutral, and start mission.; short timeouts for testing still in code.

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 88:1813f583cee9 47 if((current_state != SIT_IDLE) && (current_state != FLYING_IDLE)) { //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 88:1813f583cee9 246 begin_state = current_state;
joel_ssc 87:6d95f853dab3 247 sprintf(buf, "in setup(): have_legfile = %d current_state = %d (sit_idle= 0 or 1)\n", have_legfile, current_state);
joel_ssc 87:6d95f853dab3 248 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 249 sprintf(buf, "in setup(): LEG_POSITION_DIVE = %d START_SWIM = %d \n", LEG_POSITION_DIVE, START_SWIM);
joel_ssc 87:6d95f853dab3 250 mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 251
joel_ssc 87:6d95f853dab3 252 sprintf(buf, "Time is a mystery, here is a message before the basic call\n");
joel_ssc 86:ba3a118b0080 253 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 254 int jj;
joel_ssc 86:ba3a118b0080 255 long int kk;
joel_ssc 86:ba3a118b0080 256 time_t secval;
joel_ssc 86:ba3a118b0080 257
joel_ssc 86:ba3a118b0080 258 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 259 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 260
joel_ssc 86:ba3a118b0080 261 secval = mbedLogger().getSystemTime();
joel_ssc 86:ba3a118b0080 262 sprintf(buf, "Time as a basic string = %s\n", ctime(&secval));
joel_ssc 86:ba3a118b0080 263 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 264
joel_ssc 86:ba3a118b0080 265 sprintf(buf, "Time is still amystery, here is a message after the basic call\n");
joel_ssc 82:0981b9ada820 266 mbedLogger().appendDiagFile(buf,0);
tnhnrl 32:f2f8ae34aadc 267
joel_ssc 82:0981b9ada820 268 //set time of logger (to current or close-to-current time) now set earlier at line 149
joel_ssc 82:0981b9ada820 269 // mbedLogger().setLogTime();
tnhnrl 65:2ac186553959 270 //sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 271
tnhnrl 47:fb3c7929d3f3 272 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 273 mbedLogger().initializeLogFile();
joel_ssc 82:0981b9ada820 274
joel_ssc 82:0981b9ada820 275 mbedLogger().appendLogFile(current_state, 1); //write the idle state, then close
joel_ssc 82:0981b9ada820 276 //sdLogger().appendLogFile(current_state, 1); //write the idle state, then close
joel_ssc 82:0981b9ada820 277
joel_ssc 82:0981b9ada820 278 mbedLogger().appendLogFile(current_state, 0); //close log file added jcw nov 9 2018 for test
joel_ssc 82:0981b9ada820 279 //sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 80:4e5d306d695b 280 //sdLogger().initializeLogFile(); // works 10/23/18
tnhnrl 65:2ac186553959 281
tnhnrl 68:8f549749b8ce 282 setup_complete = true;
tnhnrl 65:2ac186553959 283 }
tnhnrl 58:94b7fd55185e 284
joel_ssc 84:eccd8e837134 285 void cycle_logfiles(int logversion, int diagversion) {
joel_ssc 84:eccd8e837134 286 //int logversion;
joel_ssc 84:eccd8e837134 287 //int diagversion;
joel_ssc 84:eccd8e837134 288 char bufx[256];
joel_ssc 84:eccd8e837134 289 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 290 mbedLogger().appendDiagFile(bufx,0);
joel_ssc 84:eccd8e837134 291 mbedLogger().appendLogFile(current_state, 0); //both files are now closed
joel_ssc 84:eccd8e837134 292 //use the present values and increment
joel_ssc 84:eccd8e837134 293 //logversion = configFileIO().logFilesStruct.logversion + 1;
joel_ssc 84:eccd8e837134 294 //diagversion = configFileIO().logFilesStruct.diagversion + 1;
joel_ssc 84:eccd8e837134 295 configFileIO().saveLogVersData(logversion+1, diagversion+1); // updates the file logvers.txt
joel_ssc 84:eccd8e837134 296 configFileIO().load_LogVers_config(0); // now read them back into the structure
joel_ssc 84:eccd8e837134 297 mbedLogger().initializeDiagFile(0); //don't print before initializing
joel_ssc 84:eccd8e837134 298
joel_ssc 84:eccd8e837134 299
joel_ssc 84:eccd8e837134 300 mbedLogger().initializeLogFile();
joel_ssc 84:eccd8e837134 301 mbedLogger().initializeDiagFile(1);
joel_ssc 84:eccd8e837134 302
joel_ssc 84:eccd8e837134 303 }
joel_ssc 84:eccd8e837134 304
tnhnrl 68:8f549749b8ce 305 /*************************** v1.8 **************************/
tnhnrl 68:8f549749b8ce 306
tnhnrl 73:f6f378311c8d 307 int main() {
tnhnrl 73:f6f378311c8d 308 setup(); //setup electronics/hardware
joel_ssc 82:0981b9ada820 309 // on landing, check orientation, if upside down, fix that first
joel_ssc 88:1813f583cee9 310 // systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
joel_ssc 86:ba3a118b0080 311 led2()=1; led4()=0;
tnhnrl 65:2ac186553959 312 unsigned int tNow = 0;
joel_ssc 82:0981b9ada820 313 int vernum=0;
joel_ssc 82:0981b9ada820 314 int diagnum=0;
joel_ssc 82:0981b9ada820 315 char buf[256];
tnhnrl 75:92e79d23d29a 316 xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
joel_ssc 87:6d95f853dab3 317 sprintf(buf, "\n\n\r 2019-may-07 FSG PCB XBee line315 in main (setup complete) \n\n\r");
joel_ssc 86:ba3a118b0080 318 mbedLogger().appendDiagFile(buf,3);
joel_ssc 88:1813f583cee9 319 int fn_timeout = 240;
joel_ssc 88:1813f583cee9 320 int save_timeout = 300;
joel_ssc 87:6d95f853dab3 321
joel_ssc 87:6d95f853dab3 322
joel_ssc 84:eccd8e837134 323 //tNow=5; sprintf(buf, "log file config values logfile= %s diag file= %s\n", configFileIO().logFilesStruct.logFileName,
joel_ssc 84:eccd8e837134 324 // configFileIO().logFilesStruct.diagFileName); tNow=0;
joel_ssc 87:6d95f853dab3 325 // mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 326 vernum = configFileIO().logFilesStruct.logversion;
joel_ssc 82:0981b9ada820 327 diagnum = configFileIO().logFilesStruct.diagversion;
joel_ssc 82:0981b9ada820 328 sprintf(buf, "translated values LOG FILE VERSION number (vernum)=%d diag file version number(diagnum) = %d\n", vernum, diagnum);
joel_ssc 82:0981b9ada820 329 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 330 sprintf(buf, "logfiles_struct values - direct LOG FILE VERSION ().logversion=%d diag file version().diagversion = %d\n",
joel_ssc 82:0981b9ada820 331 configFileIO().logFilesStruct.logversion, configFileIO().logFilesStruct.diagversion);
joel_ssc 82:0981b9ada820 332 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 333 //sprintf(buf, "try another message after closing file up and then will exit\n\n\r");
joel_ssc 86:ba3a118b0080 334 //mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 335
joel_ssc 84:eccd8e837134 336
joel_ssc 84:eccd8e837134 337 // increment the log file names once
joel_ssc 84:eccd8e837134 338 //cycle_logfiles(vernum,diagnum);
joel_ssc 84:eccd8e837134 339 //sprintf(buf, "This message should be in a new diag file\n\n\r");
joel_ssc 84:eccd8e837134 340 //mbedLogger().appendDiagFile(buf,0);
joel_ssc 84:eccd8e837134 341 // mbedLogger().appendLogFile(current_state, 1);
joel_ssc 82:0981b9ada820 342 //wait(5);
joel_ssc 82:0981b9ada820 343 //exit(0);
tnhnrl 56:48a8a5a65b82 344 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
joel_ssc 82:0981b9ada820 345 int keeprunning = 1;
tnhnrl 65:2ac186553959 346
joel_ssc 82:0981b9ada820 347 if(have_legfile) {
joel_ssc 82:0981b9ada820 348 //install the leg variables in a structure, and set the state there.
joel_ssc 82:0981b9ada820 349 stateMachine().getLegParams(); //should set up everything with proper LEG_POSITION_DIVE state
joel_ssc 87:6d95f853dab3 350 sprintf(buf, "have_legfile succeeded main: line 318 \n\r");
joel_ssc 86:ba3a118b0080 351 mbedLogger().appendDiagFile(buf,3);
joel_ssc 82:0981b9ada820 352 }
joel_ssc 87:6d95f853dab3 353
joel_ssc 86:ba3a118b0080 354 if(!have_legfile) {
joel_ssc 86:ba3a118b0080 355 sprintf(buf, "have_legfile failed! .... will exit\n\n\r");
joel_ssc 86:ba3a118b0080 356 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 357 keeprunning = 0;
joel_ssc 85:dd8176285b6e 358 }
joel_ssc 87:6d95f853dab3 359 if(motorDisconnect().read() == 1) {
joel_ssc 87:6d95f853dab3 360 sprintf(buf, "motorDisconnect.read() = 1 surprising!\n\n\r");
joel_ssc 87:6d95f853dab3 361 mbedLogger().appendDiagFile(buf,3); led1()=1; led4()=1;
joel_ssc 87:6d95f853dab3 362 }
joel_ssc 87:6d95f853dab3 363 if(motorDisconnect().read() == 0) {
joel_ssc 87:6d95f853dab3 364 sprintf(buf, "motorDisconnect.read() = 0 I expected that\n\n\r");
joel_ssc 87:6d95f853dab3 365 mbedLogger().appendDiagFile(buf,3); led1()=1; led3()=1;
joel_ssc 87:6d95f853dab3 366 }
joel_ssc 86:ba3a118b0080 367
joel_ssc 84:eccd8e837134 368 while (keeprunning) {
joel_ssc 84:eccd8e837134 369 if( read_ticker() ) { // read_ticker runs at the speed of 10 kHz (adc timing)
tnhnrl 65:2ac186553959 370 ++tNow;
mkelly10 51:c5c40272ecc3 371
tnhnrl 81:7ff2c6467892 372 //Note to self: Retest data transmission code.
tnhnrl 81:7ff2c6467892 373 //This is currently running at 0.1 second intervals (10 hz) and was working well for data transmission
tnhnrl 74:d281aaef9766 374 if (current_state == TX_MBED_LOG or current_state == RX_SEQUENCE) {
tnhnrl 73:f6f378311c8d 375 if ( (tNow % 100) == 0 ) { // 0.01 second intervals (100 Hz)
tnhnrl 66:0f20870117b7 376 fsm_loop = true;
tnhnrl 66:0f20870117b7 377 FSM();
tnhnrl 66:0f20870117b7 378 }
joel_ssc 82:0981b9ada820 379 } // end if(currentstate..)
joel_ssc 84:eccd8e837134 380
tnhnrl 67:c86a4b464682 381 //NOT TRANSMITTING DATA, NORMAL OPERATIONS
joel_ssc 84:eccd8e837134 382 else { // **88**
joel_ssc 84:eccd8e837134 383 //FSM
joel_ssc 88:1813f583cee9 384 // preamble to running the FSM
joel_ssc 88:1813f583cee9 385 if (setval == 0 && (begin_state != FLYING_IDLE) && (current_state == START_SWIM || current_state == LEG_POSITION_DIVE) && (fabs(imu().getRoll()) < 30)) {
joel_ssc 88:1813f583cee9 386 // These tests mean: - second start,plus already upright
joel_ssc 88:1813f583cee9 387 // from setup() current_state = legController().legStructLoaded[0].state; either start_swim or leg_pos_dive
joel_ssc 88:1813f583cee9 388 save_state = current_state;
joel_ssc 88:1813f583cee9 389 neutral_via_leg =1;
joel_ssc 88:1813f583cee9 390 save_timeout = legController().legStructLoaded[0].timeout;
joel_ssc 88:1813f583cee9 391 fn_timeout = 240;
joel_ssc 88:1813f583cee9 392 current_state = FIND_NEUTRAL; // does runStatemachine know about this value?
joel_ssc 88:1813f583cee9 393 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 394 mbedLogger().appendDiagFile(buf,3);
joel_ssc 88:1813f583cee9 395 stateMachine().setstate_frommain(FIND_NEUTRAL, fn_timeout); // this will change state inside runstatemachine, change state val inthe right structure, first
joel_ssc 88:1813f583cee9 396 }
joel_ssc 88:1813f583cee9 397 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 398 current_state = save_state; //setval reset at line 366 when find_neutral succeeds
joel_ssc 88:1813f583cee9 399 stateMachine().setstate_frommain(current_state, save_timeout); //back to what you were doing
joel_ssc 88:1813f583cee9 400 sprintf(buf, "in main(): presumably after find_neutral exits to RISE, but successful in setting setval\n");
joel_ssc 88:1813f583cee9 401 mbedLogger().appendDiagFile(buf,3);
joel_ssc 88:1813f583cee9 402 neutral_via_leg = 0;
joel_ssc 88:1813f583cee9 403 }
joel_ssc 88:1813f583cee9 404 if ( (neutral_via_leg == 1) && (setval == 0) && current_state == RISE) { // failed endstate of FIND_NEUTRAL new state
joel_ssc 88:1813f583cee9 405
joel_ssc 88:1813f583cee9 406 sprintf(buf, "in main(): presumably after find_neutral exits to RISE, UNSUCCESSFULLY\n");
joel_ssc 88:1813f583cee9 407 mbedLogger().appendDiagFile(buf,3);
joel_ssc 88:1813f583cee9 408 neutral_via_leg = 0;
joel_ssc 88:1813f583cee9 409 }
joel_ssc 88:1813f583cee9 410 // end preamble
tnhnrl 68:8f549749b8ce 411 if ( (tNow % 100) == 0 ) { // 0.1 second intervals
tnhnrl 66:0f20870117b7 412 fsm_loop = true;
tnhnrl 66:0f20870117b7 413 FSM();
joel_ssc 84:eccd8e837134 414
tnhnrl 73:f6f378311c8d 415 //get commands and update GUI
tnhnrl 73:f6f378311c8d 416 gui().getCommandFSM();
joel_ssc 84:eccd8e837134 417 }
joel_ssc 84:eccd8e837134 418 //LOGGING
joel_ssc 84:eccd8e837134 419 if ( (tNow % 1000) == 0 ) { // 1.0 second intervals
joel_ssc 84:eccd8e837134 420 log_loop = true;
joel_ssc 84:eccd8e837134 421 log_function();
joel_ssc 87:6d95f853dab3 422 //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 423 //mbedLogger().appendDiagFile(buf,3);
joel_ssc 87:6d95f853dab3 424 led3()=0; led2()=0; led1()=0; led4()=0;
joel_ssc 86:ba3a118b0080 425 }
joel_ssc 87:6d95f853dab3 426 if ( (tNow % 2000) == 0 ) { // 2 second intervals
joel_ssc 86:ba3a118b0080 427
joel_ssc 87:6d95f853dab3 428 led3()=1;
joel_ssc 87:6d95f853dab3 429 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 430 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 431 }
joel_ssc 87:6d95f853dab3 432 if ((tNow % 3000) == 0) {
joel_ssc 87:6d95f853dab3 433 led2()=1;
joel_ssc 87:6d95f853dab3 434 }
joel_ssc 84:eccd8e837134 435 //update the log and diagnostics files
joel_ssc 87:6d95f853dab3 436 if ( (tNow % 31000) == 0 ) { // 1.0 hour intervals= 3600*1000 check for testing via 31 second intervals
joel_ssc 85:dd8176285b6e 437 sprintf(buf, "hit cycle seconds replace_logfiles interval in main loop tNow =%d \n\n\r", tNow);
joel_ssc 85:dd8176285b6e 438 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 439 vernum = configFileIO().logFilesStruct.logversion;
joel_ssc 84:eccd8e837134 440 diagnum = configFileIO().logFilesStruct.diagversion;
joel_ssc 85:dd8176285b6e 441 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 442 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 443 cycle_logfiles(vernum,diagnum);
joel_ssc 85:dd8176285b6e 444 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 445 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 446 // close the log and diagnostics files
joel_ssc 84:eccd8e837134 447 // increment the version numbers for each
joel_ssc 84:eccd8e837134 448 // save the new version numbers
joel_ssc 84:eccd8e837134 449 // initialize new log and diagnostics files
tnhnrl 66:0f20870117b7 450 log_loop = true;
tnhnrl 66:0f20870117b7 451 log_function();
joel_ssc 87:6d95f853dab3 452 led3()=1; led1()=1;
tnhnrl 66:0f20870117b7 453 }
joel_ssc 84:eccd8e837134 454 } // end else { at **88**
joel_ssc 84:eccd8e837134 455 if(current_state == FB_EXIT) {
joel_ssc 84:eccd8e837134 456 log_loop=true;
joel_ssc 87:6d95f853dab3 457 log_function(); led2()=1; led4()=1;
joel_ssc 84:eccd8e837134 458 keeprunning=0; // and this will force exit to the main while() loop.
joel_ssc 85:dd8176285b6e 459 // 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 460 sprintf(buf, "INSIDE main loop: BUT now called to exit out of it via FB_EXIT\n\n\r");
joel_ssc 86:ba3a118b0080 461 mbedLogger().appendDiagFile(buf,3);
joel_ssc 84:eccd8e837134 462 }
joel_ssc 87:6d95f853dab3 463 if(tNow == 90000) { // don't wait forever -remove this for real operations!!
joel_ssc 87:6d95f853dab3 464 keeprunning=0;
joel_ssc 87:6d95f853dab3 465 }
joel_ssc 82:0981b9ada820 466 } // end if(read_ticker) {
joel_ssc 82:0981b9ada820 467 } // end while(keeprunning)
joel_ssc 87:6d95f853dab3 468
joel_ssc 82:0981b9ada820 469 mbedLogger().appendLogFile(current_state, 1);
joel_ssc 87:6d95f853dab3 470 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 471 mbedLogger().appendDiagFile(buf,3);
joel_ssc 86:ba3a118b0080 472 led1()=1; led2() =1; led3()=1 ;led4() = 1;
joel_ssc 86:ba3a118b0080 473 wait(5);
joel_ssc 86:ba3a118b0080 474
joel_ssc 82:0981b9ada820 475 mbedLogger().appendLogFile(current_state, 1);
joel_ssc 82:0981b9ada820 476 mbedLogger().appendLogFile(current_state, 0);
joel_ssc 86:ba3a118b0080 477 sprintf(buf, "wait 5 more seconds? out of the leg loop via FB_EXIT\n\n\r");
joel_ssc 82:0981b9ada820 478 mbedLogger().appendDiagFile(buf,0);
joel_ssc 86:ba3a118b0080 479 led1()=0; led2()=0; led3()= 0 ;led4()=0;
joel_ssc 88:1813f583cee9 480 configFileIO().save_FinalTime(); // saves last time before closing shop
joel_ssc 82:0981b9ada820 481 exit(0);
joel_ssc 82:0981b9ada820 482 } // end main()