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:
Thu Jun 07 13:02:08 2018 +0000
Revision:
53:c0586fe62b01
Parent:
52:f207567d3ea4
Child:
54:d4990fb68404
motors working in correct directions

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