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:
mkelly10
Date:
Mon Jun 04 15:20:21 2018 +0000
Revision:
51:c5c40272ecc3
Parent:
50:1d59ea7c7a1c
Child:
52:f207567d3ea4
FSG_PCB_V1

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