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

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
CodyMarquardt
Date:
Fri Jun 28 13:59:11 2019 +0000
Revision:
99:9d0849f5fcd7
Parent:
98:81db9332212d
Program has bugs. Committing in order to access in MBED studio to debug

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