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:
Fri Jun 08 13:56:30 2018 +0000
Revision:
54:d4990fb68404
Parent:
53:c0586fe62b01
Child:
55:f4ec445c42fe
heading not working

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