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:
tnhnrl
Date:
Wed Oct 24 16:07:10 2018 +0000
Revision:
80:4e5d306d695b
Parent:
79:3688c3a0d7f4
Child:
81:7ff2c6467892
Tilde brings up menu to print log size, and asks whether or not you want to erase the MBED local log.; ; Tested code with SD logger, working on PCB 2.4, commented out.

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
tnhnrl 65:2ac186553959 24 static int current_state = 0;
tnhnrl 65:2ac186553959 25 static bool file_opened = false;
tnhnrl 65:2ac186553959 26
tnhnrl 73:f6f378311c8d 27 void FSM() { // FSM loop runs at 10 hz
tnhnrl 65:2ac186553959 28 if(fsm_loop) {
tnhnrl 73:f6f378311c8d 29 // led one removed
tnhnrl 65:2ac186553959 30 fsm_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 31 current_state = stateMachine().runStateMachine(); //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
tnhnrl 65:2ac186553959 32 }
tnhnrl 65:2ac186553959 33 }
tnhnrl 65:2ac186553959 34
tnhnrl 65:2ac186553959 35 void log_function() {
tnhnrl 65:2ac186553959 36 // log loop runs at 1 hz
tnhnrl 65:2ac186553959 37 if (log_loop) {
tnhnrl 65:2ac186553959 38 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
tnhnrl 65:2ac186553959 39
tnhnrl 68:8f549749b8ce 40 if(current_state != 0) {
tnhnrl 65:2ac186553959 41 if (!file_opened) { //if the log file is not open, open it
tnhnrl 65:2ac186553959 42 mbedLogger().appendLogFile(current_state, 0); //open MBED file once
tnhnrl 80:4e5d306d695b 43 //sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 65:2ac186553959 44
tnhnrl 65:2ac186553959 45 file_opened = true; //stops it from continuing to open it
tnhnrl 56:48a8a5a65b82 46
tnhnrl 74:d281aaef9766 47 xbee().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 48 }
tnhnrl 65:2ac186553959 49
tnhnrl 65:2ac186553959 50 //record to Mbed file system
tnhnrl 65:2ac186553959 51
tnhnrl 65:2ac186553959 52 mbedLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 80:4e5d306d695b 53 //sdLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 65:2ac186553959 54 }
tnhnrl 65:2ac186553959 55
tnhnrl 65:2ac186553959 56 //when the current FSM state is zero (SIT_IDLE), close the file
tnhnrl 65:2ac186553959 57 else {
tnhnrl 65:2ac186553959 58 //this can only happen once
tnhnrl 65:2ac186553959 59 if (file_opened) {
tnhnrl 67:c86a4b464682 60 //WRITE ONCE
tnhnrl 67:c86a4b464682 61 mbedLogger().appendLogFile(current_state, 1); //write the idle state, then close
tnhnrl 80:4e5d306d695b 62 //sdLogger().appendLogFile(current_state, 1); //write the idle state, then close
tnhnrl 67:c86a4b464682 63
tnhnrl 65:2ac186553959 64 mbedLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 80:4e5d306d695b 65 //sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 65:2ac186553959 66
tnhnrl 65:2ac186553959 67 file_opened = false;
tnhnrl 65:2ac186553959 68
tnhnrl 74:d281aaef9766 69 xbee().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 70 }
tnhnrl 65:2ac186553959 71 }
tnhnrl 74:d281aaef9766 72 } //END OF LOG LOOP8
tnhnrl 65:2ac186553959 73
tnhnrl 65:2ac186553959 74 log_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 75 }
tnhnrl 56:48a8a5a65b82 76
tnhnrl 72:250b2665755c 77 //single system timer to run hardware/electronics timing
tnhnrl 65:2ac186553959 78 static void system_timer(void) {
tnhnrl 65:2ac186553959 79 bTick = 1;
tnhnrl 65:2ac186553959 80
tnhnrl 56:48a8a5a65b82 81 timer_counter++;
tnhnrl 56:48a8a5a65b82 82
tnhnrl 56:48a8a5a65b82 83 //only start these updates when everything is properly setup (through setup function)
tnhnrl 56:48a8a5a65b82 84 if (setup_complete) {
tnhnrl 74:d281aaef9766 85 if ( (timer_counter % 5) == 0) { //this runs at 0.005 second intervals (200 Hz)
tnhnrl 65:2ac186553959 86 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 87 }
tnhnrl 65:2ac186553959 88
tnhnrl 65:2ac186553959 89 if ( (timer_counter % 10) == 0) {
tnhnrl 65:2ac186553959 90 bce().update(); //update() inside LinearActuator class (running at 0.01 second intervals)
tnhnrl 65:2ac186553959 91 batt().update();
tnhnrl 65:2ac186553959 92 }
tnhnrl 65:2ac186553959 93
tnhnrl 65:2ac186553959 94 if ( (timer_counter % 20) == 0 ) { // 0.02 second intervals
tnhnrl 74:d281aaef9766 95 rudder().runServo();
tnhnrl 65:2ac186553959 96 }
tnhnrl 65:2ac186553959 97
tnhnrl 65:2ac186553959 98 if ( (timer_counter % 50) == 0 ) { // 0.05 second intervals
tnhnrl 67:c86a4b464682 99 imu().runIMU();
tnhnrl 65:2ac186553959 100 }
tnhnrl 73:f6f378311c8d 101
tnhnrl 65:2ac186553959 102 if ( (timer_counter % 100) == 0) { // 100,000 microseconds = 0.1 second intervals
tnhnrl 65:2ac186553959 103 depthLoop().runOuterLoop();
tnhnrl 65:2ac186553959 104 pitchLoop().runOuterLoop();
tnhnrl 65:2ac186553959 105 headingLoop().runOuterLoop();
tnhnrl 65:2ac186553959 106 }
tnhnrl 73:f6f378311c8d 107
tnhnrl 73:f6f378311c8d 108 if ( (timer_counter % 1000) == 0) { // update at 1.0 second intervals
tnhnrl 73:f6f378311c8d 109 //gui().updateGUI();
tnhnrl 73:f6f378311c8d 110 }
tnhnrl 74:d281aaef9766 111
tnhnrl 74:d281aaef9766 112 if ( (timer_counter % 30000) == 0) { // update at 30.0 second intervals
tnhnrl 74:d281aaef9766 113 //pc().printf("XB!\n");
tnhnrl 74:d281aaef9766 114 }
tnhnrl 56:48a8a5a65b82 115 }
tnhnrl 56:48a8a5a65b82 116 }
tnhnrl 56:48a8a5a65b82 117
danstrider 10:085ab7328054 118 void setup() {
tnhnrl 74:d281aaef9766 119 xbee().baud(115200);
tnhnrl 75:92e79d23d29a 120 xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee\n\n\r");
tnhnrl 74:d281aaef9766 121
tnhnrl 74:d281aaef9766 122 //pc().baud(57600);
tnhnrl 65:2ac186553959 123
danstrider 10:085ab7328054 124 // start up the system timer
tnhnrl 65:2ac186553959 125 //systemTimer().start();
tnhnrl 20:8987a9ae2bc7 126
danstrider 10:085ab7328054 127 // set up and start the adc. This runs on a fixed interval and is interrupt driven
tnhnrl 65:2ac186553959 128 adc().initialize();
tnhnrl 67:c86a4b464682 129 //one central interrupt is updating the ADC (not using the start function)
tnhnrl 65:2ac186553959 130
tnhnrl 65:2ac186553959 131 // setup and run the rudder(servo) pwm signal (start the ticker)
tnhnrl 65:2ac186553959 132 //rudder().init();
tnhnrl 74:d281aaef9766 133 xbee().printf("Rudder servo initialized!\n\r");
danstrider 10:085ab7328054 134
danstrider 10:085ab7328054 135 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 136 imu().initialize();
tnhnrl 65:2ac186553959 137 //imu().start();
danstrider 10:085ab7328054 138
tnhnrl 48:20e681885161 139 // construct the MBED local file system
danstrider 10:085ab7328054 140 local();
tnhnrl 48:20e681885161 141
tnhnrl 79:3688c3a0d7f4 142 // construct the SD card file system TEST 10/23/18
tnhnrl 80:4e5d306d695b 143 //sd_card();
tnhnrl 20:8987a9ae2bc7 144
danstrider 10:085ab7328054 145 // load config data from files
tnhnrl 65:2ac186553959 146 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 65:2ac186553959 147 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 65:2ac186553959 148
tnhnrl 65:2ac186553959 149 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 65:2ac186553959 150 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 65:2ac186553959 151
tnhnrl 65:2ac186553959 152 configFileIO().load_RUDDER_config(); // load the rudder servo inner loop parameters from the file "SERVO.txt"
tnhnrl 65:2ac186553959 153 configFileIO().load_HEADING_config(); // load the rudder servo outer loop HEADING control parameters from the file "HEADING.txt" (contains neutral position)
tnhnrl 43:891baf306e0a 154
danstrider 10:085ab7328054 155 // set up the linear actuators. adc has to be running first.
tnhnrl 65:2ac186553959 156 bce().setPIDHighLimit(bce().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 157 bce().init();
tnhnrl 65:2ac186553959 158 //bce().start(); //removed start, it's handled by the interrupt
tnhnrl 67:c86a4b464682 159 bce().runLinearActuator();
mkelly10 12:a0519d11d2b6 160 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 161
tnhnrl 65:2ac186553959 162 batt().setPIDHighLimit(batt().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 163 batt().init();
tnhnrl 65:2ac186553959 164 batt().runLinearActuator(); // _init = true;
tnhnrl 65:2ac186553959 165 //batt().start();//removed start, it's handled by the interrupt
mkelly10 12:a0519d11d2b6 166 batt().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 167
tnhnrl 65:2ac186553959 168 // set up the depth, pitch, and rudder outer loop controllers
danstrider 10:085ab7328054 169 depthLoop().init();
tnhnrl 65:2ac186553959 170 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 171 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 172
danstrider 10:085ab7328054 173 pitchLoop().init();
tnhnrl 65:2ac186553959 174 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 175 pitchLoop().setCommand(stateMachine().getPitchCommand());
tnhnrl 55:f4ec445c42fe 176
tnhnrl 55:f4ec445c42fe 177 headingLoop().init();
tnhnrl 65:2ac186553959 178 //removed start, it's handled by the interrupt
tnhnrl 65:2ac186553959 179 //headingLoop().setCommand(stateMachine().getHeadingCommand()); // FIX LATER
tnhnrl 65:2ac186553959 180 //heading flag that adjust the PID error is set in the constructor
tnhnrl 65:2ac186553959 181
tnhnrl 65:2ac186553959 182 //systemTicker.attach_us(&system_timer, 10000); // Interrupt timer running at 0.01 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 183
tnhnrl 65:2ac186553959 184
tnhnrl 20:8987a9ae2bc7 185
tnhnrl 20:8987a9ae2bc7 186 // show that the PID gains are loading from the file
tnhnrl 74:d281aaef9766 187 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(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
tnhnrl 74:d281aaef9766 188 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(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
tnhnrl 74:d281aaef9766 189 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(), rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
tnhnrl 65:2ac186553959 190
tnhnrl 74:d281aaef9766 191 xbee().printf("depth P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset());
tnhnrl 74:d281aaef9766 192 xbee().printf("pitch P:%6.2f, I:%6.2f, D:%6.2f, offset:%6.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
tnhnrl 74:d281aaef9766 193 xbee().printf("heading P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f deg (deadband: %0.1f)\r\n", headingLoop().getControllerP(), headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(), headingLoop().getDeadband());
tnhnrl 65:2ac186553959 194
tnhnrl 74:d281aaef9766 195 xbee().printf("\n\r");
tnhnrl 21:38c8544db6f4 196
tnhnrl 17:7c16b5671d0e 197 //load sequence from file
tnhnrl 17:7c16b5671d0e 198 sequenceController().loadSequence();
tnhnrl 32:f2f8ae34aadc 199
tnhnrl 47:fb3c7929d3f3 200 //set time of logger (to current or close-to-current time)
tnhnrl 65:2ac186553959 201 mbedLogger().setLogTime();
tnhnrl 65:2ac186553959 202 //sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 203
tnhnrl 47:fb3c7929d3f3 204 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 205 mbedLogger().initializeLogFile();
tnhnrl 80:4e5d306d695b 206 //sdLogger().initializeLogFile(); // works 10/23/18
tnhnrl 65:2ac186553959 207
tnhnrl 68:8f549749b8ce 208 setup_complete = true;
tnhnrl 65:2ac186553959 209 }
tnhnrl 58:94b7fd55185e 210
tnhnrl 68:8f549749b8ce 211 /*************************** v1.8 **************************/
tnhnrl 68:8f549749b8ce 212
tnhnrl 73:f6f378311c8d 213 int main() {
tnhnrl 73:f6f378311c8d 214 setup(); //setup electronics/hardware
tnhnrl 56:48a8a5a65b82 215
tnhnrl 72:250b2665755c 216 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
tnhnrl 68:8f549749b8ce 217
tnhnrl 65:2ac186553959 218 unsigned int tNow = 0;
tnhnrl 74:d281aaef9766 219
tnhnrl 75:92e79d23d29a 220 xbee().printf("\n\n\r 2018-08-14 FSG PCB XBee (setup complete) \n\n\r");
tnhnrl 56:48a8a5a65b82 221
tnhnrl 56:48a8a5a65b82 222 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 223
tnhnrl 65:2ac186553959 224 while (1) {
tnhnrl 66:0f20870117b7 225 if( read_ticker() ) // read_ticker runs at the speed of 10 kHz (adc timing)
tnhnrl 65:2ac186553959 226 {
tnhnrl 65:2ac186553959 227 ++tNow;
mkelly10 51:c5c40272ecc3 228
tnhnrl 66:0f20870117b7 229 //run finite state machine fast when transmitting data
tnhnrl 74:d281aaef9766 230 if (current_state == TX_MBED_LOG or current_state == RX_SEQUENCE) {
tnhnrl 73:f6f378311c8d 231 if ( (tNow % 100) == 0 ) { // 0.01 second intervals (100 Hz)
tnhnrl 66:0f20870117b7 232 fsm_loop = true;
tnhnrl 66:0f20870117b7 233 FSM();
tnhnrl 66:0f20870117b7 234 }
tnhnrl 45:16b8162188ca 235 }
tnhnrl 34:9b66c5188051 236
tnhnrl 67:c86a4b464682 237 //NOT TRANSMITTING DATA, NORMAL OPERATIONS
tnhnrl 68:8f549749b8ce 238 else {
tnhnrl 68:8f549749b8ce 239 //FSM
tnhnrl 68:8f549749b8ce 240 if ( (tNow % 100) == 0 ) { // 0.1 second intervals
tnhnrl 66:0f20870117b7 241 fsm_loop = true;
tnhnrl 66:0f20870117b7 242 FSM();
tnhnrl 73:f6f378311c8d 243
tnhnrl 73:f6f378311c8d 244 //get commands and update GUI
tnhnrl 73:f6f378311c8d 245 gui().getCommandFSM();
tnhnrl 68:8f549749b8ce 246 }
tnhnrl 68:8f549749b8ce 247 //LOGGING
tnhnrl 68:8f549749b8ce 248 if ( (tNow % 1000) == 0 ) { // 1.0 second intervals
tnhnrl 66:0f20870117b7 249 log_loop = true;
tnhnrl 66:0f20870117b7 250 log_function();
tnhnrl 66:0f20870117b7 251 }
tnhnrl 66:0f20870117b7 252 }
tnhnrl 65:2ac186553959 253 }
danstrider 10:085ab7328054 254 }
tnhnrl 74:d281aaef9766 255 }