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 Feb 15 23:08:38 2018 +0000
Revision:
50:1d59ea7c7a1c
Parent:
49:47ffa4feb6db
Child:
51:c5c40272ecc3
02/15/2018 LASR pool-tested code. FSM dive, check_tuning, Find_Neutral, all seemed to work successfully.  Rudder disabled for now.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danstrider 10:085ab7328054 1 /*
tnhnrl 48:20e681885161 2 Modified 2018-02-01 revA by Troy
tnhnrl 48:20e681885161 3 - Fixed bugs with file opening and closing
tnhnrl 48:20e681885161 4 - Made a directory printing function in the MbedLogger class that does not use the Directory List library (removed)
tnhnrl 48:20e681885161 5 Modified 2018-02-02 revA by Troy
tnhnrl 48:20e681885161 6 - Placed mbed file transmitter functionality into MbedLogger class
tnhnrl 48:20e681885161 7 - created a macro for printing out files to PC and serial port simultaneously (placed in main)
tnhnrl 48:20e681885161 8 Modified 2018-02-07 revA by Troy
tnhnrl 48:20e681885161 9 - SDFileSystem class added to code along with sd card file system in System "StaticDefs"
tnhnrl 48:20e681885161 10 - Had to fix bug with SDFileSystem, the imported version uses a buggy FATFileSystem class
tnhnrl 48:20e681885161 11 - Update came from https://os.mbed.com/teams/mbed-official/code/FATFileSystem/ (not the library importer tool)
tnhnrl 48:20e681885161 12 - Modified MbedLogger constructor to include string for file system, e.g. "/sd/" and "/local/"
tnhnrl 48:20e681885161 13 - Modified MbedLogger class to use local or sd card
tnhnrl 48:20e681885161 14 - Created SD card file system using reader on pins p11, p12, p13, p14 (SPI interface)
tnhnrl 48:20e681885161 15 Modified 2018-02-08 revA by Troy
tnhnrl 48:20e681885161 16 - Test code without ADC running to check behavior (timing issue)
tnhnrl 48:20e681885161 17 - Modified sequence to use sequence.txt and fixed warnings
tnhnrl 48:20e681885161 18 - Fixed CRC issue with file reception (the command that the MBED sends out)
tnhnrl 48:20e681885161 19 - Issue with IMU data on p27, need to check wiring and or MAX232
tnhnrl 48:20e681885161 20 Modified 2018-02-14 revA by Troy
tnhnrl 48:20e681885161 21 - IMU tested and working correctly
tnhnrl 48:20e681885161 22 - Data logging on SD card and MBED working simultaneously
tnhnrl 48:20e681885161 23 - State Machine is being used to transmit data over Xbee instead of a separate ticker
tnhnrl 48:20e681885161 24 - Servo driver tested separately in FSG_servo_test program (verified servo works separately)
tnhnrl 49:47ffa4feb6db 25 - Electrical issue with PWM signal interfering with motor driver signal (disabled servo for now)
tnhnrl 49:47ffa4feb6db 26 - Ability to erase MBED (only the MBED) log file through keyboard console
tnhnrl 49:47ffa4feb6db 27 Modified 2018-02-15 revA by Troy
tnhnrl 49:47ffa4feb6db 28 - Made minor changes including cleaning up some functions
tnhnrl 49:47ffa4feb6db 29 Modified 2018-02-15 revB by Troy
tnhnrl 49:47ffa4feb6db 30 - Modified StateMachine class to include manual motor-driving mode and FSM-driven battery and buoyancy engine motor inner loops (CHECK_TUNING)
tnhnrl 49:47ffa4feb6db 31 - Fixed some on-screen printouts in the StateMachine class
tnhnrl 49:47ffa4feb6db 32 - Added CHECK_TUNING state to MbedLogger printouts to MBED and SD card
tnhnrl 49:47ffa4feb6db 33 - Pool tested code worked with several dives. Neutral was BCE: 162 mm, BATT: 36 mm with CHECK_TUNING state; FIND_NEUTRAL (auto-tune) produced 161.0 and 34 mm
danstrider 10:085ab7328054 34 */
tnhnrl 20:8987a9ae2bc7 35
tzyoung 0:ea293bbf9717 36 #include "mbed.h"
tzyoung 0:ea293bbf9717 37 #include "StaticDefs.hpp"
tnhnrl 20:8987a9ae2bc7 38
tnhnrl 32:f2f8ae34aadc 39 // loop rate used to determine how fast events trigger in the while loop
tnhnrl 20:8987a9ae2bc7 40 Ticker loop_rate_ticker;
tnhnrl 34:9b66c5188051 41 Ticker log_loop_rate_ticker;
tnhnrl 32:f2f8ae34aadc 42
tnhnrl 32:f2f8ae34aadc 43 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 44 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 45
tnhnrl 32:f2f8ae34aadc 46 void loop_trigger() { loop = true;} // loop trigger (used in while loop)
tnhnrl 34:9b66c5188051 47 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
tnhnrl 32:f2f8ae34aadc 48
danstrider 10:085ab7328054 49 void setup() {
danstrider 11:3b241ecb75ed 50 pc().baud(57600);
tnhnrl 50:1d59ea7c7a1c 51 pc().printf("\n\n\rFSG POOL TEST 2017-02-15 revB\n\n\r");
tnhnrl 32:f2f8ae34aadc 52
tnhnrl 32:f2f8ae34aadc 53 //setup data logger baud rate and write the start of the program (every time you reset)
tnhnrl 32:f2f8ae34aadc 54 datalogger().baud(57600);
tnhnrl 39:58375ca6b6ff 55 datalogger().printf("SYSTEM, RESET\n");
tnhnrl 20:8987a9ae2bc7 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
tzyoung 0:ea293bbf9717 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
tnhnrl 48:20e681885161 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 20:8987a9ae2bc7 102
tnhnrl 20:8987a9ae2bc7 103 // show that the PID gains are loading from the file
tnhnrl 38:83d06c294807 104 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 105 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 106 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 107 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 108 pc().printf("\n\r");
tnhnrl 21:38c8544db6f4 109
tnhnrl 17:7c16b5671d0e 110 //load sequence from file
tnhnrl 17:7c16b5671d0e 111 sequenceController().loadSequence();
tnhnrl 20:8987a9ae2bc7 112
tnhnrl 20:8987a9ae2bc7 113 // establish the main loop rate
tnhnrl 20:8987a9ae2bc7 114 loop_rate_ticker.attach(&loop_trigger, 0.1); // fires the ticker at 10 Hz rate
tnhnrl 32:f2f8ae34aadc 115
tnhnrl 32:f2f8ae34aadc 116 // setup the data logger rate
tnhnrl 34:9b66c5188051 117 log_loop_rate_ticker.attach(&log_loop_trigger, 1.0); // fires the ticker at 1 Hz rate (every second)
tnhnrl 45:16b8162188ca 118
tnhnrl 47:fb3c7929d3f3 119 //set time of logger (to current or close-to-current time)
tnhnrl 47:fb3c7929d3f3 120 mbedLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 121 sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 122
tnhnrl 47:fb3c7929d3f3 123 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 124 mbedLogger().initializeLogFile();
tnhnrl 48:20e681885161 125 sdLogger().initializeLogFile();
tnhnrl 21:38c8544db6f4 126 }
tnhnrl 20:8987a9ae2bc7 127
danstrider 10:085ab7328054 128 int main() {
danstrider 10:085ab7328054 129 setup();
tnhnrl 16:3363b9f14913 130
danstrider 10:085ab7328054 131 while(1) {
tnhnrl 34:9b66c5188051 132 static int current_state = 0;
tnhnrl 36:966a86937e17 133
tnhnrl 36:966a86937e17 134 static bool file_opened = false;
tnhnrl 36:966a86937e17 135
tnhnrl 39:58375ca6b6ff 136 // FSM loop runs at 10 hz
tnhnrl 20:8987a9ae2bc7 137 if(loop) {
tnhnrl 32:f2f8ae34aadc 138 led1() = !led1(); // blink led 1
tnhnrl 45:16b8162188ca 139 current_state = stateMachine().runStateMachine(); //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
tnhnrl 20:8987a9ae2bc7 140 loop = false; // wait until the loop rate timer fires again
tnhnrl 17:7c16b5671d0e 141 }
tnhnrl 32:f2f8ae34aadc 142
tnhnrl 39:58375ca6b6ff 143 // log loop runs at 1 hz
tnhnrl 34:9b66c5188051 144 if (log_loop) {
tnhnrl 45:16b8162188ca 145 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
tnhnrl 45:16b8162188ca 146 if (current_state == TRANSMIT_LOG or current_state == RECEIVE_SEQUENCE) {
tnhnrl 45:16b8162188ca 147 //main_loop_rate_ticker.detach();
tnhnrl 45:16b8162188ca 148 //log_loop_rate_ticker.detach();
tnhnrl 45:16b8162188ca 149
tnhnrl 45:16b8162188ca 150 ; //pass
tnhnrl 45:16b8162188ca 151 }
tnhnrl 34:9b66c5188051 152
tnhnrl 45:16b8162188ca 153 else if(current_state != 0) {
tnhnrl 45:16b8162188ca 154 if (!file_opened) { //if the log file is not open, open it
tnhnrl 45:16b8162188ca 155 mbedLogger().appendLogFile(current_state, 0); //open MBED file once
tnhnrl 48:20e681885161 156 sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 45:16b8162188ca 157
tnhnrl 45:16b8162188ca 158 file_opened = true; //stops it from continuing to open it
tnhnrl 37:357e98a929cc 159
tnhnrl 36:966a86937e17 160 pc().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
tnhnrl 36:966a86937e17 161 }
tnhnrl 36:966a86937e17 162
tnhnrl 45:16b8162188ca 163 //record to Mbed file system
tnhnrl 45:16b8162188ca 164 mbedLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 48:20e681885161 165 sdLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 36:966a86937e17 166 }
tnhnrl 36:966a86937e17 167
tnhnrl 39:58375ca6b6ff 168 //when the current FSM state is zero, reset the file
tnhnrl 36:966a86937e17 169 else {
tnhnrl 36:966a86937e17 170 //this can only happen once
tnhnrl 36:966a86937e17 171 if (file_opened) {
tnhnrl 45:16b8162188ca 172 mbedLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 48:20e681885161 173 sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 45:16b8162188ca 174
tnhnrl 36:966a86937e17 175 file_opened = false;
tnhnrl 37:357e98a929cc 176
tnhnrl 37:357e98a929cc 177 pc().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 36:966a86937e17 178 }
tnhnrl 34:9b66c5188051 179 }
tnhnrl 34:9b66c5188051 180
tnhnrl 34:9b66c5188051 181 log_loop = false; // wait until the loop rate timer fires again
tnhnrl 45:16b8162188ca 182 } //END OF LOG LOOP
danstrider 10:085ab7328054 183 }
danstrider 10:085ab7328054 184 }