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:
Mon Jul 30 16:48:48 2018 +0000
Revision:
73:f6f378311c8d
Parent:
72:250b2665755c
Child:
74:d281aaef9766
work in progress 7/30 12:48 pm

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 #ifndef lround
tnhnrl 65:2ac186553959 5 #define lround(var) (long)(var+0.5f)
tnhnrl 65:2ac186553959 6 #endif
tnhnrl 65:2ac186553959 7
tnhnrl 65:2ac186553959 8 ////////////////////////////////////////////////////////////////// NEW TICKER
tnhnrl 65:2ac186553959 9 Ticker systemTicker;
tnhnrl 65:2ac186553959 10 bool setup_complete = false;
tnhnrl 65:2ac186553959 11 volatile unsigned int bTick = 0;
tnhnrl 65:2ac186553959 12 volatile unsigned int timer_counter = 0;
tnhnrl 65:2ac186553959 13
tnhnrl 65:2ac186553959 14 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 15 unsigned int val = bTick;
tnhnrl 65:2ac186553959 16 if( val )
tnhnrl 65:2ac186553959 17 bTick = 0;
tnhnrl 65:2ac186553959 18 return( val );
tnhnrl 65:2ac186553959 19 }
tnhnrl 65:2ac186553959 20 ////////////////////////////////////////////////////////////////// NEW TICKER
tnhnrl 20:8987a9ae2bc7 21
tnhnrl 32:f2f8ae34aadc 22 // loop rate used to determine how fast events trigger in the while loop
tnhnrl 65:2ac186553959 23 //Ticker main_loop_rate_ticker;
tnhnrl 65:2ac186553959 24 //Ticker log_loop_rate_ticker;
tnhnrl 32:f2f8ae34aadc 25
tnhnrl 65:2ac186553959 26 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 27 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 28
tnhnrl 65:2ac186553959 29 void loop_trigger() { fsm_loop = true;} // loop trigger (used in while loop)
tnhnrl 34:9b66c5188051 30 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
tnhnrl 32:f2f8ae34aadc 31
tnhnrl 65:2ac186553959 32 static int current_state = 0;
tnhnrl 65:2ac186553959 33 static bool file_opened = false;
tnhnrl 65:2ac186553959 34
tnhnrl 73:f6f378311c8d 35 void FSM() { // FSM loop runs at 10 hz
tnhnrl 65:2ac186553959 36 if(fsm_loop) {
tnhnrl 73:f6f378311c8d 37 // led one removed
tnhnrl 65:2ac186553959 38 fsm_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 39 current_state = stateMachine().runStateMachine(); //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
tnhnrl 65:2ac186553959 40 }
tnhnrl 65:2ac186553959 41 }
tnhnrl 65:2ac186553959 42
tnhnrl 65:2ac186553959 43 void log_function() {
tnhnrl 65:2ac186553959 44 // log loop runs at 1 hz
tnhnrl 65:2ac186553959 45 if (log_loop) {
tnhnrl 65:2ac186553959 46 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
tnhnrl 65:2ac186553959 47
tnhnrl 68:8f549749b8ce 48 //if (current_state == TRANSMIT_MBED_LOG or current_state == RECEIVE_SEQUENCE) {
tnhnrl 68:8f549749b8ce 49 // ; //pass
tnhnrl 68:8f549749b8ce 50 // }
tnhnrl 65:2ac186553959 51
tnhnrl 68:8f549749b8ce 52 if(current_state != 0) {
tnhnrl 65:2ac186553959 53 if (!file_opened) { //if the log file is not open, open it
tnhnrl 65:2ac186553959 54 mbedLogger().appendLogFile(current_state, 0); //open MBED file once
tnhnrl 65:2ac186553959 55 //sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 65:2ac186553959 56
tnhnrl 65:2ac186553959 57 file_opened = true; //stops it from continuing to open it
tnhnrl 56:48a8a5a65b82 58
tnhnrl 65:2ac186553959 59 pc().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 60 }
tnhnrl 65:2ac186553959 61
tnhnrl 65:2ac186553959 62 //record to Mbed file system
tnhnrl 65:2ac186553959 63
tnhnrl 65:2ac186553959 64 mbedLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 65:2ac186553959 65 //sdLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 65:2ac186553959 66 }
tnhnrl 65:2ac186553959 67
tnhnrl 65:2ac186553959 68 //when the current FSM state is zero (SIT_IDLE), close the file
tnhnrl 65:2ac186553959 69 else {
tnhnrl 65:2ac186553959 70 //this can only happen once
tnhnrl 65:2ac186553959 71 if (file_opened) {
tnhnrl 67:c86a4b464682 72 //WRITE ONCE
tnhnrl 67:c86a4b464682 73 mbedLogger().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 65:2ac186553959 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 65:2ac186553959 80 pc().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 81 }
tnhnrl 65:2ac186553959 82 }
tnhnrl 65:2ac186553959 83 } //END OF LOG LOOP
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 65:2ac186553959 96 if ( (timer_counter % 1) == 0) { //this runs at 0.001 second intervals (1000 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 73:f6f378311c8d 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();
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 56:48a8a5a65b82 122 }
tnhnrl 56:48a8a5a65b82 123 }
tnhnrl 56:48a8a5a65b82 124
danstrider 10:085ab7328054 125 void setup() {
danstrider 11:3b241ecb75ed 126 pc().baud(57600);
tnhnrl 72:250b2665755c 127 pc().printf("\n\n\r FSG PCB Bench Test version 1.7C)\n\n\r");
tnhnrl 65:2ac186553959 128
danstrider 10:085ab7328054 129 // start up the system timer
tnhnrl 65:2ac186553959 130 //systemTimer().start();
tnhnrl 20:8987a9ae2bc7 131
danstrider 10:085ab7328054 132 // set up and start the adc. This runs on a fixed interval and is interrupt driven
tnhnrl 65:2ac186553959 133 adc().initialize();
tnhnrl 67:c86a4b464682 134 //one central interrupt is updating the ADC (not using the start function)
tnhnrl 65:2ac186553959 135
tnhnrl 65:2ac186553959 136 // setup and run the rudder(servo) pwm signal (start the ticker)
tnhnrl 65:2ac186553959 137 //rudder().init();
tnhnrl 65:2ac186553959 138 pc().printf("Rudder servo initialized!\n\r");
danstrider 10:085ab7328054 139
danstrider 10:085ab7328054 140 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 141 imu().initialize();
tnhnrl 65:2ac186553959 142 //imu().start();
danstrider 10:085ab7328054 143
tnhnrl 48:20e681885161 144 // construct the MBED local file system
danstrider 10:085ab7328054 145 local();
tnhnrl 48:20e681885161 146
tnhnrl 48:20e681885161 147 // construct the SD card file system
tnhnrl 65:2ac186553959 148 //sd_card();
tnhnrl 20:8987a9ae2bc7 149
danstrider 10:085ab7328054 150 // load config data from files
tnhnrl 65:2ac186553959 151 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 65:2ac186553959 152 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 65:2ac186553959 153
tnhnrl 65:2ac186553959 154 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 65:2ac186553959 155 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 65:2ac186553959 156
tnhnrl 65:2ac186553959 157 configFileIO().load_RUDDER_config(); // load the rudder servo inner loop parameters from the file "SERVO.txt"
tnhnrl 65:2ac186553959 158 configFileIO().load_HEADING_config(); // load the rudder servo outer loop HEADING control parameters from the file "HEADING.txt" (contains neutral position)
tnhnrl 43:891baf306e0a 159
danstrider 10:085ab7328054 160 // set up the linear actuators. adc has to be running first.
tnhnrl 65:2ac186553959 161 bce().setPIDHighLimit(bce().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 162 bce().init();
tnhnrl 65:2ac186553959 163 //bce().start(); //removed start, it's handled by the interrupt
tnhnrl 67:c86a4b464682 164 bce().runLinearActuator();
mkelly10 12:a0519d11d2b6 165 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 166
tnhnrl 65:2ac186553959 167 batt().setPIDHighLimit(batt().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 168 batt().init();
tnhnrl 65:2ac186553959 169 batt().runLinearActuator(); // _init = true;
tnhnrl 65:2ac186553959 170 //batt().start();//removed start, it's handled by the interrupt
mkelly10 12:a0519d11d2b6 171 batt().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 172
tnhnrl 65:2ac186553959 173 // set up the depth, pitch, and rudder outer loop controllers
danstrider 10:085ab7328054 174 depthLoop().init();
tnhnrl 65:2ac186553959 175 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 176 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 177
danstrider 10:085ab7328054 178 pitchLoop().init();
tnhnrl 65:2ac186553959 179 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 180 pitchLoop().setCommand(stateMachine().getPitchCommand());
tnhnrl 55:f4ec445c42fe 181
tnhnrl 55:f4ec445c42fe 182 headingLoop().init();
tnhnrl 65:2ac186553959 183 //removed start, it's handled by the interrupt
tnhnrl 65:2ac186553959 184 //headingLoop().setCommand(stateMachine().getHeadingCommand()); // FIX LATER
tnhnrl 65:2ac186553959 185 //heading flag that adjust the PID error is set in the constructor
tnhnrl 65:2ac186553959 186
tnhnrl 65:2ac186553959 187 //systemTicker.attach_us(&system_timer, 10000); // Interrupt timer running at 0.01 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 188
tnhnrl 65:2ac186553959 189
tnhnrl 20:8987a9ae2bc7 190
tnhnrl 20:8987a9ae2bc7 191 // show that the PID gains are loading from the file
tnhnrl 38:83d06c294807 192 pc().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 38:83d06c294807 193 pc().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 65:2ac186553959 194 pc().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 195
tnhnrl 65:2ac186553959 196 pc().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 65:2ac186553959 197 pc().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 65:2ac186553959 198 pc().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 199
danstrider 10:085ab7328054 200 pc().printf("\n\r");
tnhnrl 21:38c8544db6f4 201
tnhnrl 17:7c16b5671d0e 202 //load sequence from file
tnhnrl 17:7c16b5671d0e 203 sequenceController().loadSequence();
tnhnrl 32:f2f8ae34aadc 204
tnhnrl 47:fb3c7929d3f3 205 //set time of logger (to current or close-to-current time)
tnhnrl 65:2ac186553959 206 mbedLogger().setLogTime();
tnhnrl 65:2ac186553959 207 //sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 208
tnhnrl 47:fb3c7929d3f3 209 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 210 mbedLogger().initializeLogFile();
tnhnrl 65:2ac186553959 211 //sdLogger().initializeLogFile();
tnhnrl 65:2ac186553959 212
tnhnrl 68:8f549749b8ce 213 setup_complete = true;
tnhnrl 65:2ac186553959 214 }
tnhnrl 58:94b7fd55185e 215
tnhnrl 68:8f549749b8ce 216 /*************************** v1.8 **************************/
tnhnrl 68:8f549749b8ce 217
tnhnrl 73:f6f378311c8d 218 int main() {
tnhnrl 73:f6f378311c8d 219 setup(); //setup electronics/hardware
tnhnrl 56:48a8a5a65b82 220
tnhnrl 72:250b2665755c 221 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
tnhnrl 68:8f549749b8ce 222
tnhnrl 65:2ac186553959 223 unsigned int tNow = 0;
tnhnrl 65:2ac186553959 224
tnhnrl 72:250b2665755c 225 //pc().baud(57600);
tnhnrl 73:f6f378311c8d 226 pc().printf("\n\n\r FSG PCB v1.8 (XBee) 07/12/2018 \n\n\r");
tnhnrl 56:48a8a5a65b82 227
tnhnrl 56:48a8a5a65b82 228 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 229
tnhnrl 65:2ac186553959 230 while (1) {
tnhnrl 66:0f20870117b7 231 if( read_ticker() ) // read_ticker runs at the speed of 10 kHz (adc timing)
tnhnrl 65:2ac186553959 232 {
tnhnrl 65:2ac186553959 233 ++tNow;
mkelly10 51:c5c40272ecc3 234
tnhnrl 66:0f20870117b7 235 //run finite state machine fast when transmitting data
tnhnrl 71:939d179478c4 236 if (current_state == TX_MBED_LOG or current_state == RECEIVE_SEQUENCE) {
tnhnrl 73:f6f378311c8d 237 if ( (tNow % 100) == 0 ) { // 0.01 second intervals (100 Hz)
tnhnrl 66:0f20870117b7 238 fsm_loop = true;
tnhnrl 66:0f20870117b7 239 FSM();
tnhnrl 66:0f20870117b7 240 }
tnhnrl 45:16b8162188ca 241 }
tnhnrl 34:9b66c5188051 242
tnhnrl 67:c86a4b464682 243 //NOT TRANSMITTING DATA, NORMAL OPERATIONS
tnhnrl 68:8f549749b8ce 244 else {
tnhnrl 68:8f549749b8ce 245 //FSM
tnhnrl 68:8f549749b8ce 246 if ( (tNow % 100) == 0 ) { // 0.1 second intervals
tnhnrl 66:0f20870117b7 247 fsm_loop = true;
tnhnrl 66:0f20870117b7 248 FSM();
tnhnrl 73:f6f378311c8d 249
tnhnrl 73:f6f378311c8d 250 //get commands and update GUI
tnhnrl 73:f6f378311c8d 251 gui().getCommandFSM();
tnhnrl 68:8f549749b8ce 252 }
tnhnrl 68:8f549749b8ce 253 //LOGGING
tnhnrl 68:8f549749b8ce 254 if ( (tNow % 1000) == 0 ) { // 1.0 second intervals
tnhnrl 66:0f20870117b7 255 log_loop = true;
tnhnrl 66:0f20870117b7 256 log_function();
tnhnrl 66:0f20870117b7 257 }
tnhnrl 66:0f20870117b7 258 }
tnhnrl 65:2ac186553959 259 }
danstrider 10:085ab7328054 260 }
tnhnrl 72:250b2665755c 261 }
tnhnrl 72:250b2665755c 262
tnhnrl 72:250b2665755c 263
tnhnrl 72:250b2665755c 264
tnhnrl 72:250b2665755c 265
tnhnrl 72:250b2665755c 266
tnhnrl 72:250b2665755c 267