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 15:41:51 2018 +0000
Revision:
55:f4ec445c42fe
Parent:
54:d4990fb68404
Child:
56:48a8a5a65b82
Working v2

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
danstrider 10:085ab7328054 33 */
tnhnrl 20:8987a9ae2bc7 34
tzyoung 0:ea293bbf9717 35 #include "mbed.h"
tzyoung 0:ea293bbf9717 36 #include "StaticDefs.hpp"
tnhnrl 20:8987a9ae2bc7 37
tnhnrl 32:f2f8ae34aadc 38 // loop rate used to determine how fast events trigger in the while loop
tnhnrl 20:8987a9ae2bc7 39 Ticker loop_rate_ticker;
tnhnrl 34:9b66c5188051 40 Ticker log_loop_rate_ticker;
tnhnrl 32:f2f8ae34aadc 41
tnhnrl 32:f2f8ae34aadc 42 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 43 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 44
tnhnrl 32:f2f8ae34aadc 45 void loop_trigger() { loop = true;} // loop trigger (used in while loop)
tnhnrl 34:9b66c5188051 46 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
tnhnrl 32:f2f8ae34aadc 47
danstrider 10:085ab7328054 48 void setup() {
danstrider 11:3b241ecb75ed 49 pc().baud(57600);
tnhnrl 52:f207567d3ea4 50 pc().printf("\n\n\rFSG PCB V1.5 2018-06-06 \n\n\r");
mkelly10 51:c5c40272ecc3 51
mkelly10 51:c5c40272ecc3 52 /* //setup data logger baud rate and write the start of the program (every time you reset)
tnhnrl 32:f2f8ae34aadc 53 datalogger().baud(57600);
tnhnrl 39:58375ca6b6ff 54 datalogger().printf("SYSTEM, RESET\n");
mkelly10 51:c5c40272ecc3 55 */
mkelly10 51:c5c40272ecc3 56
danstrider 10:085ab7328054 57 // start up the system timer
tzyoung 0:ea293bbf9717 58 systemTime().start();
tnhnrl 20:8987a9ae2bc7 59
danstrider 10:085ab7328054 60 // set up and start the adc. This runs on a fixed interval and is interrupt driven
mkelly10 51:c5c40272ecc3 61 adc().initialize();
tzyoung 0:ea293bbf9717 62 adc().start();
danstrider 10:085ab7328054 63
danstrider 10:085ab7328054 64 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 65 imu().initialize();
danstrider 10:085ab7328054 66 imu().start();
danstrider 10:085ab7328054 67
danstrider 10:085ab7328054 68 // set up the depth sensor. This is an internal ADC read, but eventually will be on the ltc1298
danstrider 14:85b64a4d08e8 69 depth().init();
danstrider 14:85b64a4d08e8 70 depth().tare();
danstrider 10:085ab7328054 71
tnhnrl 48:20e681885161 72 // construct the MBED local file system
danstrider 10:085ab7328054 73 local();
tnhnrl 48:20e681885161 74
tnhnrl 48:20e681885161 75 // construct the SD card file system
mkelly10 51:c5c40272ecc3 76 // sd_card();
tnhnrl 20:8987a9ae2bc7 77
danstrider 10:085ab7328054 78 // load config data from files
tnhnrl 21:38c8544db6f4 79 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 21:38c8544db6f4 80 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 21:38c8544db6f4 81 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 21:38c8544db6f4 82 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 20:8987a9ae2bc7 83
tnhnrl 43:891baf306e0a 84
danstrider 10:085ab7328054 85 // set up the linear actuators. adc has to be running first.
danstrider 10:085ab7328054 86 bce().init();
danstrider 10:085ab7328054 87 bce().start();
mkelly10 12:a0519d11d2b6 88 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 89
danstrider 10:085ab7328054 90 batt().init();
tzyoung 2:892b58e56712 91 batt().start();
mkelly10 12:a0519d11d2b6 92 batt().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 93
danstrider 10:085ab7328054 94 // set up the depth and pitch outer loop controllers
danstrider 10:085ab7328054 95 depthLoop().init();
tnhnrl 13:84fcbe1dcd62 96 depthLoop().start();
tnhnrl 16:3363b9f14913 97 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 98
danstrider 10:085ab7328054 99 pitchLoop().init();
tnhnrl 13:84fcbe1dcd62 100 pitchLoop().start();
tnhnrl 16:3363b9f14913 101 pitchLoop().setCommand(stateMachine().getPitchCommand());
tnhnrl 55:f4ec445c42fe 102
tnhnrl 55:f4ec445c42fe 103 //new 6/8/2018 for testing
tnhnrl 55:f4ec445c42fe 104 headingLoop().init();
tnhnrl 55:f4ec445c42fe 105 headingLoop().setCommand(0.0); //setting heading to 0 for testing
tnhnrl 55:f4ec445c42fe 106 //new 6/8/2018 for testing
tnhnrl 20:8987a9ae2bc7 107
tnhnrl 20:8987a9ae2bc7 108 // show that the PID gains are loading from the file
tnhnrl 38:83d06c294807 109 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 110 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 111 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 112 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 113 pc().printf("\n\r");
tnhnrl 21:38c8544db6f4 114
tnhnrl 17:7c16b5671d0e 115 //load sequence from file
tnhnrl 17:7c16b5671d0e 116 sequenceController().loadSequence();
tnhnrl 20:8987a9ae2bc7 117
tnhnrl 20:8987a9ae2bc7 118 // establish the main loop rate
tnhnrl 20:8987a9ae2bc7 119 loop_rate_ticker.attach(&loop_trigger, 0.1); // fires the ticker at 10 Hz rate
tnhnrl 32:f2f8ae34aadc 120
tnhnrl 32:f2f8ae34aadc 121 // setup the data logger rate
tnhnrl 34:9b66c5188051 122 log_loop_rate_ticker.attach(&log_loop_trigger, 1.0); // fires the ticker at 1 Hz rate (every second)
mkelly10 51:c5c40272ecc3 123
tnhnrl 47:fb3c7929d3f3 124 //set time of logger (to current or close-to-current time)
mkelly10 51:c5c40272ecc3 125 mbedLogger().setLogTime();
mkelly10 51:c5c40272ecc3 126 // sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 127
tnhnrl 47:fb3c7929d3f3 128 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 129 mbedLogger().initializeLogFile();
mkelly10 51:c5c40272ecc3 130 // sdLogger().initializeLogFile();
mkelly10 51:c5c40272ecc3 131
tnhnrl 21:38c8544db6f4 132 }
tnhnrl 20:8987a9ae2bc7 133
danstrider 10:085ab7328054 134 int main() {
danstrider 10:085ab7328054 135 setup();
mkelly10 51:c5c40272ecc3 136
danstrider 10:085ab7328054 137 while(1) {
tnhnrl 34:9b66c5188051 138 static int current_state = 0;
tnhnrl 36:966a86937e17 139
tnhnrl 36:966a86937e17 140 static bool file_opened = false;
tnhnrl 36:966a86937e17 141
tnhnrl 39:58375ca6b6ff 142 // FSM loop runs at 10 hz
tnhnrl 20:8987a9ae2bc7 143 if(loop) {
tnhnrl 32:f2f8ae34aadc 144 led1() = !led1(); // blink led 1
tnhnrl 45:16b8162188ca 145 current_state = stateMachine().runStateMachine(); //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
tnhnrl 20:8987a9ae2bc7 146 loop = false; // wait until the loop rate timer fires again
tnhnrl 17:7c16b5671d0e 147 }
tnhnrl 32:f2f8ae34aadc 148
tnhnrl 39:58375ca6b6ff 149 // log loop runs at 1 hz
tnhnrl 34:9b66c5188051 150 if (log_loop) {
tnhnrl 45:16b8162188ca 151 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
tnhnrl 45:16b8162188ca 152 if (current_state == TRANSMIT_LOG or current_state == RECEIVE_SEQUENCE) {
tnhnrl 45:16b8162188ca 153 //main_loop_rate_ticker.detach();
tnhnrl 45:16b8162188ca 154 //log_loop_rate_ticker.detach();
tnhnrl 45:16b8162188ca 155
tnhnrl 45:16b8162188ca 156 ; //pass
tnhnrl 45:16b8162188ca 157 }
tnhnrl 34:9b66c5188051 158
tnhnrl 45:16b8162188ca 159 else if(current_state != 0) {
tnhnrl 45:16b8162188ca 160 if (!file_opened) { //if the log file is not open, open it
tnhnrl 45:16b8162188ca 161 mbedLogger().appendLogFile(current_state, 0); //open MBED file once
tnhnrl 52:f207567d3ea4 162 // sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 45:16b8162188ca 163
tnhnrl 45:16b8162188ca 164 file_opened = true; //stops it from continuing to open it
tnhnrl 37:357e98a929cc 165
tnhnrl 36:966a86937e17 166 pc().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
tnhnrl 36:966a86937e17 167 }
tnhnrl 36:966a86937e17 168
tnhnrl 45:16b8162188ca 169 //record to Mbed file system
tnhnrl 45:16b8162188ca 170 mbedLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 52:f207567d3ea4 171 // sdLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 36:966a86937e17 172 }
tnhnrl 36:966a86937e17 173
tnhnrl 39:58375ca6b6ff 174 //when the current FSM state is zero, reset the file
tnhnrl 36:966a86937e17 175 else {
tnhnrl 36:966a86937e17 176 //this can only happen once
tnhnrl 36:966a86937e17 177 if (file_opened) {
tnhnrl 45:16b8162188ca 178 mbedLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 52:f207567d3ea4 179 // sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 45:16b8162188ca 180
tnhnrl 36:966a86937e17 181 file_opened = false;
tnhnrl 37:357e98a929cc 182
tnhnrl 37:357e98a929cc 183 pc().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 36:966a86937e17 184 }
tnhnrl 34:9b66c5188051 185 }
tnhnrl 34:9b66c5188051 186
tnhnrl 34:9b66c5188051 187 log_loop = false; // wait until the loop rate timer fires again
tnhnrl 45:16b8162188ca 188 } //END OF LOG LOOP
danstrider 10:085ab7328054 189 }
mkelly10 51:c5c40272ecc3 190
danstrider 10:085ab7328054 191 }