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 Aug 14 21:06:48 2018 +0000
Revision:
74:d281aaef9766
Parent:
73:f6f378311c8d
Child:
75:92e79d23d29a
1) Fixed deadband on float broadcast ; 2) Fixed find neutral commands

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tzyoung 0:ea293bbf9717 1 #include "mbed.h"
tzyoung 0:ea293bbf9717 2 #include "StaticDefs.hpp"
tnhnrl 65:2ac186553959 3
tnhnrl 65:2ac186553959 4 ////////////////////////////////////////////////////////////////// NEW TICKER
tnhnrl 65:2ac186553959 5 Ticker systemTicker;
tnhnrl 65:2ac186553959 6 bool setup_complete = false;
tnhnrl 65:2ac186553959 7 volatile unsigned int bTick = 0;
tnhnrl 65:2ac186553959 8 volatile unsigned int timer_counter = 0;
tnhnrl 65:2ac186553959 9
tnhnrl 65:2ac186553959 10 static unsigned int read_ticker(void) { //Basically this makes sure you're reading the data at one instance (not while it's changing)
tnhnrl 65:2ac186553959 11 unsigned int val = bTick;
tnhnrl 65:2ac186553959 12 if( val )
tnhnrl 65:2ac186553959 13 bTick = 0;
tnhnrl 65:2ac186553959 14 return( val );
tnhnrl 65:2ac186553959 15 }
tnhnrl 65:2ac186553959 16 ////////////////////////////////////////////////////////////////// NEW TICKER
tnhnrl 20:8987a9ae2bc7 17
tnhnrl 32:f2f8ae34aadc 18 // loop rate used to determine how fast events trigger in the while loop
tnhnrl 65:2ac186553959 19 //Ticker main_loop_rate_ticker;
tnhnrl 65:2ac186553959 20 //Ticker log_loop_rate_ticker;
tnhnrl 32:f2f8ae34aadc 21
tnhnrl 65:2ac186553959 22 volatile bool fsm_loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)
tnhnrl 34:9b66c5188051 23 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 24
tnhnrl 65:2ac186553959 25 void loop_trigger() { fsm_loop = true;} // loop trigger (used in while loop)
tnhnrl 34:9b66c5188051 26 void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)
tnhnrl 32:f2f8ae34aadc 27
tnhnrl 65:2ac186553959 28 static int current_state = 0;
tnhnrl 65:2ac186553959 29 static bool file_opened = false;
tnhnrl 65:2ac186553959 30
tnhnrl 73:f6f378311c8d 31 void FSM() { // FSM loop runs at 10 hz
tnhnrl 65:2ac186553959 32 if(fsm_loop) {
tnhnrl 73:f6f378311c8d 33 // led one removed
tnhnrl 65:2ac186553959 34 fsm_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 35 current_state = stateMachine().runStateMachine(); //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
tnhnrl 65:2ac186553959 36 }
tnhnrl 65:2ac186553959 37 }
tnhnrl 65:2ac186553959 38
tnhnrl 65:2ac186553959 39 void log_function() {
tnhnrl 65:2ac186553959 40 // log loop runs at 1 hz
tnhnrl 65:2ac186553959 41 if (log_loop) {
tnhnrl 65:2ac186553959 42 //when the state machine is not in SIT_IDLE state (or a random keyboard press)
tnhnrl 65:2ac186553959 43
tnhnrl 68:8f549749b8ce 44 //if (current_state == TRANSMIT_MBED_LOG or current_state == RECEIVE_SEQUENCE) {
tnhnrl 68:8f549749b8ce 45 // ; //pass
tnhnrl 68:8f549749b8ce 46 // }
tnhnrl 65:2ac186553959 47
tnhnrl 68:8f549749b8ce 48 if(current_state != 0) {
tnhnrl 65:2ac186553959 49 if (!file_opened) { //if the log file is not open, open it
tnhnrl 65:2ac186553959 50 mbedLogger().appendLogFile(current_state, 0); //open MBED file once
tnhnrl 65:2ac186553959 51 //sdLogger().appendLogFile(current_state, 0); //open SD file once
tnhnrl 65:2ac186553959 52
tnhnrl 65:2ac186553959 53 file_opened = true; //stops it from continuing to open it
tnhnrl 56:48a8a5a65b82 54
tnhnrl 74:d281aaef9766 55 xbee().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 56 }
tnhnrl 65:2ac186553959 57
tnhnrl 65:2ac186553959 58 //record to Mbed file system
tnhnrl 65:2ac186553959 59
tnhnrl 65:2ac186553959 60 mbedLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 65:2ac186553959 61 //sdLogger().appendLogFile(current_state, 1); //writing data
tnhnrl 65:2ac186553959 62 }
tnhnrl 65:2ac186553959 63
tnhnrl 65:2ac186553959 64 //when the current FSM state is zero (SIT_IDLE), close the file
tnhnrl 65:2ac186553959 65 else {
tnhnrl 65:2ac186553959 66 //this can only happen once
tnhnrl 65:2ac186553959 67 if (file_opened) {
tnhnrl 67:c86a4b464682 68 //WRITE ONCE
tnhnrl 67:c86a4b464682 69 mbedLogger().appendLogFile(current_state, 1); //write the idle state, then close
tnhnrl 67:c86a4b464682 70
tnhnrl 65:2ac186553959 71 mbedLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 65:2ac186553959 72 //sdLogger().appendLogFile(current_state, 0); //close log file
tnhnrl 65:2ac186553959 73
tnhnrl 65:2ac186553959 74 file_opened = false;
tnhnrl 65:2ac186553959 75
tnhnrl 74:d281aaef9766 76 xbee().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
tnhnrl 65:2ac186553959 77 }
tnhnrl 65:2ac186553959 78 }
tnhnrl 74:d281aaef9766 79 } //END OF LOG LOOP8
tnhnrl 65:2ac186553959 80
tnhnrl 65:2ac186553959 81 log_loop = false; // wait until the loop rate timer fires again
tnhnrl 65:2ac186553959 82 }
tnhnrl 56:48a8a5a65b82 83
tnhnrl 72:250b2665755c 84 //single system timer to run hardware/electronics timing
tnhnrl 65:2ac186553959 85 static void system_timer(void) {
tnhnrl 65:2ac186553959 86 bTick = 1;
tnhnrl 65:2ac186553959 87
tnhnrl 56:48a8a5a65b82 88 timer_counter++;
tnhnrl 56:48a8a5a65b82 89
tnhnrl 56:48a8a5a65b82 90 //only start these updates when everything is properly setup (through setup function)
tnhnrl 56:48a8a5a65b82 91 if (setup_complete) {
tnhnrl 74:d281aaef9766 92 if ( (timer_counter % 5) == 0) { //this runs at 0.005 second intervals (200 Hz)
tnhnrl 65:2ac186553959 93 adc().update(); //every iteration of this the A/D converter runs //now this runs at 0.01 second intervals 03/12/2018
tnhnrl 65:2ac186553959 94 }
tnhnrl 65:2ac186553959 95
tnhnrl 65:2ac186553959 96 if ( (timer_counter % 10) == 0) {
tnhnrl 65:2ac186553959 97 bce().update(); //update() inside LinearActuator class (running at 0.01 second intervals)
tnhnrl 65:2ac186553959 98 batt().update();
tnhnrl 65:2ac186553959 99 }
tnhnrl 65:2ac186553959 100
tnhnrl 65:2ac186553959 101 if ( (timer_counter % 20) == 0 ) { // 0.02 second intervals
tnhnrl 74:d281aaef9766 102 rudder().runServo();
tnhnrl 65:2ac186553959 103 }
tnhnrl 65:2ac186553959 104
tnhnrl 65:2ac186553959 105 if ( (timer_counter % 50) == 0 ) { // 0.05 second intervals
tnhnrl 67:c86a4b464682 106 imu().runIMU();
tnhnrl 65:2ac186553959 107 }
tnhnrl 73:f6f378311c8d 108
tnhnrl 65:2ac186553959 109 if ( (timer_counter % 100) == 0) { // 100,000 microseconds = 0.1 second intervals
tnhnrl 65:2ac186553959 110 depthLoop().runOuterLoop();
tnhnrl 65:2ac186553959 111 pitchLoop().runOuterLoop();
tnhnrl 65:2ac186553959 112 headingLoop().runOuterLoop();
tnhnrl 65:2ac186553959 113 }
tnhnrl 73:f6f378311c8d 114
tnhnrl 73:f6f378311c8d 115 if ( (timer_counter % 1000) == 0) { // update at 1.0 second intervals
tnhnrl 73:f6f378311c8d 116 //gui().updateGUI();
tnhnrl 73:f6f378311c8d 117 }
tnhnrl 74:d281aaef9766 118
tnhnrl 74:d281aaef9766 119 if ( (timer_counter % 30000) == 0) { // update at 30.0 second intervals
tnhnrl 74:d281aaef9766 120 //pc().printf("XB!\n");
tnhnrl 74:d281aaef9766 121 }
tnhnrl 56:48a8a5a65b82 122 }
tnhnrl 56:48a8a5a65b82 123 }
tnhnrl 56:48a8a5a65b82 124
danstrider 10:085ab7328054 125 void setup() {
tnhnrl 74:d281aaef9766 126 xbee().baud(115200);
tnhnrl 74:d281aaef9766 127 xbee().printf("\n\n\r FSG PCB Bench Test version 1.8L 115200 BAUD)\n\n\r");
tnhnrl 74:d281aaef9766 128
tnhnrl 74:d281aaef9766 129 //pc().baud(57600);
tnhnrl 65:2ac186553959 130
danstrider 10:085ab7328054 131 // start up the system timer
tnhnrl 65:2ac186553959 132 //systemTimer().start();
tnhnrl 20:8987a9ae2bc7 133
danstrider 10:085ab7328054 134 // set up and start the adc. This runs on a fixed interval and is interrupt driven
tnhnrl 65:2ac186553959 135 adc().initialize();
tnhnrl 67:c86a4b464682 136 //one central interrupt is updating the ADC (not using the start function)
tnhnrl 65:2ac186553959 137
tnhnrl 65:2ac186553959 138 // setup and run the rudder(servo) pwm signal (start the ticker)
tnhnrl 65:2ac186553959 139 //rudder().init();
tnhnrl 74:d281aaef9766 140 xbee().printf("Rudder servo initialized!\n\r");
danstrider 10:085ab7328054 141
danstrider 10:085ab7328054 142 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 143 imu().initialize();
tnhnrl 65:2ac186553959 144 //imu().start();
danstrider 10:085ab7328054 145
tnhnrl 48:20e681885161 146 // construct the MBED local file system
danstrider 10:085ab7328054 147 local();
tnhnrl 48:20e681885161 148
tnhnrl 48:20e681885161 149 // construct the SD card file system
tnhnrl 65:2ac186553959 150 //sd_card();
tnhnrl 20:8987a9ae2bc7 151
danstrider 10:085ab7328054 152 // load config data from files
tnhnrl 65:2ac186553959 153 configFileIO().load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
tnhnrl 65:2ac186553959 154 configFileIO().load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
tnhnrl 65:2ac186553959 155
tnhnrl 65:2ac186553959 156 configFileIO().load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
tnhnrl 65:2ac186553959 157 configFileIO().load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
tnhnrl 65:2ac186553959 158
tnhnrl 65:2ac186553959 159 configFileIO().load_RUDDER_config(); // load the rudder servo inner loop parameters from the file "SERVO.txt"
tnhnrl 65:2ac186553959 160 configFileIO().load_HEADING_config(); // load the rudder servo outer loop HEADING control parameters from the file "HEADING.txt" (contains neutral position)
tnhnrl 43:891baf306e0a 161
danstrider 10:085ab7328054 162 // set up the linear actuators. adc has to be running first.
tnhnrl 65:2ac186553959 163 bce().setPIDHighLimit(bce().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 164 bce().init();
tnhnrl 65:2ac186553959 165 //bce().start(); //removed start, it's handled by the interrupt
tnhnrl 67:c86a4b464682 166 bce().runLinearActuator();
mkelly10 12:a0519d11d2b6 167 bce().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 168
tnhnrl 65:2ac186553959 169 batt().setPIDHighLimit(batt().getTravelLimit()); //travel limit of this linear actuator
danstrider 10:085ab7328054 170 batt().init();
tnhnrl 65:2ac186553959 171 batt().runLinearActuator(); // _init = true;
tnhnrl 65:2ac186553959 172 //batt().start();//removed start, it's handled by the interrupt
mkelly10 12:a0519d11d2b6 173 batt().pause(); // start by not moving
tnhnrl 20:8987a9ae2bc7 174
tnhnrl 65:2ac186553959 175 // set up the depth, pitch, and rudder outer loop controllers
danstrider 10:085ab7328054 176 depthLoop().init();
tnhnrl 65:2ac186553959 177 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 178 depthLoop().setCommand(stateMachine().getDepthCommand());
tnhnrl 20:8987a9ae2bc7 179
danstrider 10:085ab7328054 180 pitchLoop().init();
tnhnrl 65:2ac186553959 181 //removed start, it's handled by the interrupt
tnhnrl 16:3363b9f14913 182 pitchLoop().setCommand(stateMachine().getPitchCommand());
tnhnrl 55:f4ec445c42fe 183
tnhnrl 55:f4ec445c42fe 184 headingLoop().init();
tnhnrl 65:2ac186553959 185 //removed start, it's handled by the interrupt
tnhnrl 65:2ac186553959 186 //headingLoop().setCommand(stateMachine().getHeadingCommand()); // FIX LATER
tnhnrl 65:2ac186553959 187 //heading flag that adjust the PID error is set in the constructor
tnhnrl 65:2ac186553959 188
tnhnrl 65:2ac186553959 189 //systemTicker.attach_us(&system_timer, 10000); // Interrupt timer running at 0.01 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 190
tnhnrl 65:2ac186553959 191
tnhnrl 20:8987a9ae2bc7 192
tnhnrl 20:8987a9ae2bc7 193 // show that the PID gains are loading from the file
tnhnrl 74:d281aaef9766 194 xbee().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 74:d281aaef9766 195 xbee().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 74:d281aaef9766 196 xbee().printf("rudder min pwm: %6.2f, max pwm: %6.2f, center pwm: %6.2f, min deg: %6.2f, max deg: %6.2f\r\n", rudder().getMinPWM(), rudder().getMaxPWM(), rudder().getCenterPWM(), rudder().getMinDeg(), rudder().getMaxDeg());
tnhnrl 65:2ac186553959 197
tnhnrl 74:d281aaef9766 198 xbee().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 74:d281aaef9766 199 xbee().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());
tnhnrl 74:d281aaef9766 200 xbee().printf("heading P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f deg (deadband: %0.1f)\r\n", headingLoop().getControllerP(), headingLoop().getControllerI(), headingLoop().getControllerD(), headingLoop().getOutputOffset(), headingLoop().getDeadband());
tnhnrl 65:2ac186553959 201
tnhnrl 74:d281aaef9766 202 xbee().printf("\n\r");
tnhnrl 21:38c8544db6f4 203
tnhnrl 17:7c16b5671d0e 204 //load sequence from file
tnhnrl 17:7c16b5671d0e 205 sequenceController().loadSequence();
tnhnrl 32:f2f8ae34aadc 206
tnhnrl 47:fb3c7929d3f3 207 //set time of logger (to current or close-to-current time)
tnhnrl 65:2ac186553959 208 mbedLogger().setLogTime();
tnhnrl 65:2ac186553959 209 //sdLogger().setLogTime();
tnhnrl 47:fb3c7929d3f3 210
tnhnrl 47:fb3c7929d3f3 211 //create log files if not present on file system
tnhnrl 47:fb3c7929d3f3 212 mbedLogger().initializeLogFile();
tnhnrl 65:2ac186553959 213 //sdLogger().initializeLogFile();
tnhnrl 65:2ac186553959 214
tnhnrl 68:8f549749b8ce 215 setup_complete = true;
tnhnrl 65:2ac186553959 216 }
tnhnrl 58:94b7fd55185e 217
tnhnrl 68:8f549749b8ce 218 /*************************** v1.8 **************************/
tnhnrl 68:8f549749b8ce 219
tnhnrl 73:f6f378311c8d 220 int main() {
tnhnrl 73:f6f378311c8d 221 setup(); //setup electronics/hardware
tnhnrl 56:48a8a5a65b82 222
tnhnrl 72:250b2665755c 223 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
tnhnrl 68:8f549749b8ce 224
tnhnrl 65:2ac186553959 225 unsigned int tNow = 0;
tnhnrl 74:d281aaef9766 226
tnhnrl 74:d281aaef9766 227 xbee().printf("\n\n\r FSG PCB v1.8D (XBee) 08/07/2018 \n\n\r");
tnhnrl 56:48a8a5a65b82 228
tnhnrl 56:48a8a5a65b82 229 systemTicker.attach_us(&system_timer, 1000); // Interrupt timer running at 0.001 seconds (slower than original ADC time interval)
tnhnrl 65:2ac186553959 230
tnhnrl 65:2ac186553959 231 while (1) {
tnhnrl 66:0f20870117b7 232 if( read_ticker() ) // read_ticker runs at the speed of 10 kHz (adc timing)
tnhnrl 65:2ac186553959 233 {
tnhnrl 65:2ac186553959 234 ++tNow;
mkelly10 51:c5c40272ecc3 235
tnhnrl 66:0f20870117b7 236 //run finite state machine fast when transmitting data
tnhnrl 74:d281aaef9766 237 if (current_state == TX_MBED_LOG or current_state == RX_SEQUENCE) {
tnhnrl 73:f6f378311c8d 238 if ( (tNow % 100) == 0 ) { // 0.01 second intervals (100 Hz)
tnhnrl 66:0f20870117b7 239 fsm_loop = true;
tnhnrl 66:0f20870117b7 240 FSM();
tnhnrl 66:0f20870117b7 241 }
tnhnrl 45:16b8162188ca 242 }
tnhnrl 34:9b66c5188051 243
tnhnrl 67:c86a4b464682 244 //NOT TRANSMITTING DATA, NORMAL OPERATIONS
tnhnrl 68:8f549749b8ce 245 else {
tnhnrl 68:8f549749b8ce 246 //FSM
tnhnrl 68:8f549749b8ce 247 if ( (tNow % 100) == 0 ) { // 0.1 second intervals
tnhnrl 66:0f20870117b7 248 fsm_loop = true;
tnhnrl 66:0f20870117b7 249 FSM();
tnhnrl 73:f6f378311c8d 250
tnhnrl 73:f6f378311c8d 251 //get commands and update GUI
tnhnrl 73:f6f378311c8d 252 gui().getCommandFSM();
tnhnrl 68:8f549749b8ce 253 }
tnhnrl 68:8f549749b8ce 254 //LOGGING
tnhnrl 68:8f549749b8ce 255 if ( (tNow % 1000) == 0 ) { // 1.0 second intervals
tnhnrl 66:0f20870117b7 256 log_loop = true;
tnhnrl 66:0f20870117b7 257 log_function();
tnhnrl 66:0f20870117b7 258 }
tnhnrl 66:0f20870117b7 259 }
tnhnrl 65:2ac186553959 260 }
danstrider 10:085ab7328054 261 }
tnhnrl 74:d281aaef9766 262 }