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:
Tue Jun 12 16:20:03 2018 +0000
Revision:
56:48a8a5a65b82
Parent:
55:f4ec445c42fe
Child:
57:ec69651c8c21
good stuff, rudder and everything.; ; Need to fix heading next.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danstrider 10:085ab7328054 1 /*
tnhnrl 52:f207567d3ea4 2 Modified FSG PCB V1_5
tnhnrl 52:f207567d3ea4 3 - Freezes when doing a dive or any timed sequence (commented out SD card references)
tnhnrl 52:f207567d3ea4 4 - commented out sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 52:f207567d3ea4 5 - commented out sdLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 52:f207567d3ea4 6 - commented out sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 52:f207567d3ea4 7 - reduced timer to 20 seconds for bench testing
tnhnrl 52:f207567d3ea4 8 - modified ConfigFileIO for rudder()
tnhnrl 52:f207567d3ea4 9 - added in getFloatUserInput function from newer code
tnhnrl 53:c0586fe62b01 10 - changed LinearActuator & batt() in StaticDefs to match new pinouts from Bob/Matt/Troy fixes
tnhnrl 53:c0586fe62b01 11 - slowed down battery motor because it's silly fast at 30 volts (bench test)
tnhnrl 53:c0586fe62b01 12 * BCE gain is proportional 0.1 now 0.01
tnhnrl 53:c0586fe62b01 13 - BATT was moving in the wrong direction, gain was P: 0.10, I: 0.0, D: 0.0
tnhnrl 53:c0586fe62b01 14 * change gain to P: -0.10 (gain was flipped, I think the old circuit board had the voltages flipped??)
tnhnrl 53:c0586fe62b01 15 - StateMachine changes for testing
tnhnrl 53:c0586fe62b01 16 * added keyboard_menu_STREAM_STATUS();
tnhnrl 53:c0586fe62b01 17 * added keyboard_menu_RUDDER_SERVO_settings();
tnhnrl 54:d4990fb68404 18 - modified the zero on the battery position from 610 to 836
tnhnrl 54:d4990fb68404 19 - BMM (batt) slope may be incorrect, seems low, currently 0.12176
tnhnrl 54:d4990fb68404 20 - modified the zero on BCE from 253 to 460
tnhnrl 54:d4990fb68404 21 - Pressure readings are wrong
tnhnrl 54:d4990fb68404 22 * added readADCCounts() to omegaPX209 class to see channel readings
tnhnrl 54:d4990fb68404 23 * modified omegaPX209 class to use filtered ADC readings from SpiADC.readCh4()
tnhnrl 54:d4990fb68404 24 - fixed rudderLoop to headingLoop from newer code
tnhnrl 55:f4ec445c42fe 25 Modified FSG PCB V2
tnhnrl 55:f4ec445c42fe 26 - added init headingLoop to main
tnhnrl 55:f4ec445c42fe 27 - added pitch and heading outputs to STREAM_STATUS
tnhnrl 54:d4990fb68404 28
tnhnrl 54:d4990fb68404 29 NOTE: Flipped motor controller output on connector side with battery mass mover (BMM)
tnhnrl 54:d4990fb68404 30 - Motor direction was opposite the BCE motor (because of gearing)
tnhnrl 54:d4990fb68404 31 - BMM P gain is now positive 0.02 (from -0.10)
tnhnrl 54:d4990fb68404 32
tnhnrl 56:48a8a5a65b82 33 Modified FSG PCB V2.5
tnhnrl 56:48a8a5a65b82 34 - added timing code for interrupt that drives the rudder (testing with o-scope)
tnhnrl 56:48a8a5a65b82 35 -
tnhnrl 56:48a8a5a65b82 36
danstrider 10:085ab7328054 37 */
tnhnrl 20:8987a9ae2bc7 38
tzyoung 0:ea293bbf9717 39 #include "mbed.h"
tzyoung 0:ea293bbf9717 40 #include "StaticDefs.hpp"
tnhnrl 20:8987a9ae2bc7 41
tnhnrl 32:f2f8ae34aadc 42 // loop rate used to determine how fast events trigger in the while loop
tnhnrl 20:8987a9ae2bc7 43 Ticker loop_rate_ticker;
tnhnrl 34:9b66c5188051 44 Ticker log_loop_rate_ticker;
tnhnrl 32:f2f8ae34aadc 45
tnhnrl 32:f2f8ae34aadc 46 volatile bool loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)
tnhnrl 34:9b66c5188051 47 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 48
tnhnrl 32:f2f8ae34aadc 49 void loop_trigger() { loop = true;} // loop trigger (used in while loop)
tnhnrl 34:9b66c5188051 50 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
tnhnrl 32:f2f8ae34aadc 51
tnhnrl 56:48a8a5a65b82 52 /****************** for rudder servo ******************/
tnhnrl 56:48a8a5a65b82 53 Ticker systemTicker;
tnhnrl 56:48a8a5a65b82 54
tnhnrl 56:48a8a5a65b82 55 volatile unsigned int timer_counter = 0;
tnhnrl 56:48a8a5a65b82 56
tnhnrl 56:48a8a5a65b82 57 bool setup_complete = false;
tnhnrl 56:48a8a5a65b82 58
tnhnrl 56:48a8a5a65b82 59 static void system_timer(void) {
tnhnrl 56:48a8a5a65b82 60 timer_counter++;
tnhnrl 56:48a8a5a65b82 61
tnhnrl 56:48a8a5a65b82 62 //only start these updates when everything is properly setup (through setup function)
tnhnrl 56:48a8a5a65b82 63 if (setup_complete) {
tnhnrl 56:48a8a5a65b82 64 if ( (timer_counter % 20) == 0 ) { // 0.02 second intervals
tnhnrl 56:48a8a5a65b82 65 rudder().runServo();
tnhnrl 56:48a8a5a65b82 66 }
tnhnrl 56:48a8a5a65b82 67 }
tnhnrl 56:48a8a5a65b82 68 }
tnhnrl 56:48a8a5a65b82 69
tnhnrl 56:48a8a5a65b82 70 /****************** for rudder servo ******************/
tnhnrl 56:48a8a5a65b82 71
danstrider 10:085ab7328054 72 void setup() {
danstrider 11:3b241ecb75ed 73 pc().baud(57600);
tnhnrl 52:f207567d3ea4 74 pc().printf("\n\n\rFSG PCB V1.5 2018-06-06 \n\n\r");
mkelly10 51:c5c40272ecc3 75
mkelly10 51:c5c40272ecc3 76 /* //setup data logger baud rate and write the start of the program (every time you reset)
tnhnrl 32:f2f8ae34aadc 77 datalogger().baud(57600);
tnhnrl 39:58375ca6b6ff 78 datalogger().printf("SYSTEM, RESET\n");
mkelly10 51:c5c40272ecc3 79 */
mkelly10 51:c5c40272ecc3 80
danstrider 10:085ab7328054 81 // start up the system timer
tzyoung 0:ea293bbf9717 82 systemTime().start();
tnhnrl 20:8987a9ae2bc7 83
danstrider 10:085ab7328054 84 // set up and start the adc. This runs on a fixed interval and is interrupt driven
mkelly10 51:c5c40272ecc3 85 adc().initialize();
tzyoung 0:ea293bbf9717 86 adc().start();
danstrider 10:085ab7328054 87
danstrider 10:085ab7328054 88 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 89 imu().initialize();
danstrider 10:085ab7328054 90 imu().start();
danstrider 10:085ab7328054 91
danstrider 10:085ab7328054 92 // set up the depth sensor. This is an internal ADC read, but eventually will be on the ltc1298
danstrider 14:85b64a4d08e8 93 depth().init();
danstrider 14:85b64a4d08e8 94 depth().tare();
danstrider 10:085ab7328054 95
tnhnrl 48:20e681885161 96 // construct the MBED local file system
danstrider 10:085ab7328054 97 local();
tnhnrl 48:20e681885161 98
tnhnrl 48:20e681885161 99 // construct the SD card file system
mkelly10 51:c5c40272ecc3 100 // sd_card();
tnhnrl 20:8987a9ae2bc7 101
danstrider 10:085ab7328054 102 // load config data from files
tnhnrl 21:38c8544db6f4 103 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 21:38c8544db6f4 104 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 21:38c8544db6f4 105 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 21:38c8544db6f4 106 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 20:8987a9ae2bc7 107
tnhnrl 43:891baf306e0a 108
danstrider 10:085ab7328054 109 // set up the linear actuators. adc has to be running first.
danstrider 10:085ab7328054 110 bce().init();
danstrider 10:085ab7328054 111 bce().start();
mkelly10 12:a0519d11d2b6 112 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 113
danstrider 10:085ab7328054 114 batt().init();
tzyoung 2:892b58e56712 115 batt().start();
mkelly10 12:a0519d11d2b6 116 batt().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 117
danstrider 10:085ab7328054 118 // set up the depth and pitch outer loop controllers
danstrider 10:085ab7328054 119 depthLoop().init();
tnhnrl 13:84fcbe1dcd62 120 depthLoop().start();
tnhnrl 16:3363b9f14913 121 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 122
danstrider 10:085ab7328054 123 pitchLoop().init();
tnhnrl 13:84fcbe1dcd62 124 pitchLoop().start();
tnhnrl 16:3363b9f14913 125 pitchLoop().setCommand(stateMachine().getPitchCommand());
tnhnrl 55:f4ec445c42fe 126
tnhnrl 55:f4ec445c42fe 127 //new 6/8/2018 for testing
tnhnrl 55:f4ec445c42fe 128 headingLoop().init();
tnhnrl 55:f4ec445c42fe 129 headingLoop().setCommand(0.0); //setting heading to 0 for testing
tnhnrl 55:f4ec445c42fe 130 //new 6/8/2018 for testing
tnhnrl 20:8987a9ae2bc7 131
tnhnrl 20:8987a9ae2bc7 132 // show that the PID gains are loading from the file
tnhnrl 38:83d06c294807 133 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 134 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 21:38c8544db6f4 135 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 21:38c8544db6f4 136 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());
danstrider 10:085ab7328054 137 pc().printf("\n\r");
tnhnrl 21:38c8544db6f4 138
tnhnrl 17:7c16b5671d0e 139 //load sequence from file
tnhnrl 17:7c16b5671d0e 140 sequenceController().loadSequence();
tnhnrl 20:8987a9ae2bc7 141
tnhnrl 20:8987a9ae2bc7 142 // establish the main loop rate
tnhnrl 20:8987a9ae2bc7 143 loop_rate_ticker.attach(&loop_trigger, 0.1); // fires the ticker at 10 Hz rate
tnhnrl 32:f2f8ae34aadc 144
tnhnrl 32:f2f8ae34aadc 145 // setup the data logger rate
tnhnrl 34:9b66c5188051 146 log_loop_rate_ticker.attach(&log_loop_trigger, 1.0); // fires the ticker at 1 Hz rate (every second)
mkelly10 51:c5c40272ecc3 147
tnhnrl 47:fb3c7929d3f3 148 //set time of logger (to current or close-to-current time)
mkelly10 51:c5c40272ecc3 149 mbedLogger().setLogTime();
mkelly10 51:c5c40272ecc3 150 // sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 151
tnhnrl 47:fb3c7929d3f3 152 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 153 mbedLogger().initializeLogFile();
mkelly10 51:c5c40272ecc3 154 // sdLogger().initializeLogFile();
mkelly10 51:c5c40272ecc3 155
tnhnrl 56:48a8a5a65b82 156 setup_complete = true; //used for interrupt timing
tnhnrl 56:48a8a5a65b82 157
tnhnrl 21:38c8544db6f4 158 }
tnhnrl 20:8987a9ae2bc7 159
danstrider 10:085ab7328054 160 int main() {
danstrider 10:085ab7328054 161 setup();
tnhnrl 56:48a8a5a65b82 162
tnhnrl 56:48a8a5a65b82 163 //this is used to control timing on the rudder servo, it's based on the new timing model in the 5/29/18 code with one interrupt running everything...may or may not work here
tnhnrl 56:48a8a5a65b82 164
tnhnrl 56:48a8a5a65b82 165 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
mkelly10 51:c5c40272ecc3 166
danstrider 10:085ab7328054 167 while(1) {
tnhnrl 34:9b66c5188051 168 static int current_state = 0;
tnhnrl 36:966a86937e17 169
tnhnrl 36:966a86937e17 170 static bool file_opened = false;
tnhnrl 36:966a86937e17 171
tnhnrl 39:58375ca6b6ff 172 // FSM loop runs at 10 hz
tnhnrl 20:8987a9ae2bc7 173 if(loop) {
tnhnrl 32:f2f8ae34aadc 174 led1() = !led1(); // blink led 1
tnhnrl 45:16b8162188ca 175 current_state = stateMachine().runStateMachine(); //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
tnhnrl 20:8987a9ae2bc7 176 loop = false; // wait until the loop rate timer fires again
tnhnrl 17:7c16b5671d0e 177 }
tnhnrl 32:f2f8ae34aadc 178
tnhnrl 39:58375ca6b6ff 179 // log loop runs at 1 hz
tnhnrl 34:9b66c5188051 180 if (log_loop) {
tnhnrl 45:16b8162188ca 181 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
tnhnrl 45:16b8162188ca 182 if (current_state == TRANSMIT_LOG or current_state == RECEIVE_SEQUENCE) {
tnhnrl 45:16b8162188ca 183 //main_loop_rate_ticker.detach();
tnhnrl 45:16b8162188ca 184 //log_loop_rate_ticker.detach();
tnhnrl 45:16b8162188ca 185
tnhnrl 45:16b8162188ca 186 ; //pass
tnhnrl 45:16b8162188ca 187 }
tnhnrl 34:9b66c5188051 188
tnhnrl 45:16b8162188ca 189 else if(current_state != 0) {
tnhnrl 45:16b8162188ca 190 if (!file_opened) { //if the log file is not open, open it
tnhnrl 45:16b8162188ca 191 mbedLogger().appendLogFile(current_state, 0); //open MBED file once
tnhnrl 52:f207567d3ea4 192 // sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 45:16b8162188ca 193
tnhnrl 45:16b8162188ca 194 file_opened = true; //stops it from continuing to open it
tnhnrl 37:357e98a929cc 195
tnhnrl 36:966a86937e17 196 pc().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
tnhnrl 36:966a86937e17 197 }
tnhnrl 36:966a86937e17 198
tnhnrl 45:16b8162188ca 199 //record to Mbed file system
tnhnrl 45:16b8162188ca 200 mbedLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 52:f207567d3ea4 201 // sdLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 36:966a86937e17 202 }
tnhnrl 36:966a86937e17 203
tnhnrl 39:58375ca6b6ff 204 //when the current FSM state is zero, reset the file
tnhnrl 36:966a86937e17 205 else {
tnhnrl 36:966a86937e17 206 //this can only happen once
tnhnrl 36:966a86937e17 207 if (file_opened) {
tnhnrl 45:16b8162188ca 208 mbedLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 52:f207567d3ea4 209 // sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 45:16b8162188ca 210
tnhnrl 36:966a86937e17 211 file_opened = false;
tnhnrl 37:357e98a929cc 212
tnhnrl 37:357e98a929cc 213 pc().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 36:966a86937e17 214 }
tnhnrl 34:9b66c5188051 215 }
tnhnrl 34:9b66c5188051 216
tnhnrl 34:9b66c5188051 217 log_loop = false; // wait until the loop rate timer fires again
tnhnrl 45:16b8162188ca 218 } //END OF LOG LOOP
danstrider 10:085ab7328054 219 }
mkelly10 51:c5c40272ecc3 220
danstrider 10:085ab7328054 221 }