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


16 months ago

File content as of revision 54:d4990fb68404:

    Modified FSG PCB V1_5
        - Freezes when doing a dive or any timed sequence (commented out SD card references)
        - commented out sdLogger().appendLogFile(current_state, 0);     //open SD file once
        - commented out sdLogger().appendLogFile(current_state, 1);    //writing data
        - commented out sdLogger().appendLogFile(current_state, 0);    //close log file
        - reduced timer to 20 seconds for bench testing
        - modified ConfigFileIO for rudder()
        - added in getFloatUserInput function from newer code
        - changed LinearActuator & batt() in StaticDefs to match new pinouts from Bob/Matt/Troy fixes
        - slowed down battery motor because it's silly fast at 30 volts (bench test)
            * BCE gain is proportional 0.1 now 0.01
        - BATT was moving in the wrong direction, gain was P: 0.10, I: 0.0, D: 0.0
            * change gain to P: -0.10 (gain was flipped, I think the old circuit board had the voltages flipped??)
        - StateMachine changes for testing
            * added keyboard_menu_STREAM_STATUS();
            * added keyboard_menu_RUDDER_SERVO_settings();
        - modified the zero on the battery position from 610 to 836
        - BMM (batt) slope may be incorrect, seems low, currently 0.12176
        - modified the zero on BCE from 253 to 460
        - Pressure readings are wrong
            * added readADCCounts() to omegaPX209 class to see channel readings
            * modified omegaPX209 class to use filtered ADC readings from SpiADC.readCh4()
        - fixed rudderLoop to headingLoop from newer code
    NOTE: Flipped motor controller output on connector side with battery mass mover (BMM)
        - Motor direction was opposite the BCE motor (because of gearing) 
        - BMM P gain is now positive 0.02 (from -0.10)
#include "mbed.h"
#include "StaticDefs.hpp"
// loop rate used to determine how fast events trigger in the while loop
Ticker loop_rate_ticker;
Ticker log_loop_rate_ticker;

volatile bool loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)
volatile bool log_loop = false; //used so the compiler does not optimize this variable (load from memory, do not assume state of variable)

void loop_trigger() { loop = true;} // loop trigger (used in while loop)
void log_loop_trigger() { log_loop = true;} // log loop trigger (used in while loop)

void setup() {
    pc().printf("\n\n\rFSG PCB V1.5 2018-06-06 \n\n\r");

/*    //setup data logger baud rate and write the start of the program (every time you reset)
    datalogger().printf("SYSTEM, RESET\n");
    // start up the system timer
    // set up and start the adc. This runs on a fixed interval and is interrupt driven
    // set up and start the imu. This polls in the background
    // set up the depth sensor. This is an internal ADC read, but eventually will be on the ltc1298
    // construct the MBED local file system
    // construct the SD card file system
//    sd_card();
    // load config data from files
    configFileIO().load_BCE_config();      // load the buoyancy engine parameters from the file "bce.txt"
    configFileIO().load_BATT_config();     // load the battery mass mover parameters from the file "batt.txt"
    configFileIO().load_DEPTH_config();    // load the depth control loop parameters from the file "depth.txt" (contains neutral position)
    configFileIO().load_PITCH_config();    // load the depth control loop parameters from the file "pitch.txt" (contains neutral position)
    // set up the linear actuators.  adc has to be running first.
    bce().pause(); // start by not moving
    batt().pause(); // start by not moving
    // set up the depth and pitch outer loop controllers
    // show that the PID gains are loading from the file
    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());
    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());
    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());
    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());
    //load sequence from file
    // establish the main loop rate
    loop_rate_ticker.attach(&loop_trigger, 0.1); // fires the ticker at 10 Hz rate
    // setup the data logger rate
    log_loop_rate_ticker.attach(&log_loop_trigger, 1.0); // fires the ticker at 1 Hz rate (every second)
    //set time of logger (to current or close-to-current time)
//    sdLogger().setLogTime();
    //create log files if not present on file system
//    sdLogger().initializeLogFile();

int main() {

    while(1) {
        static int current_state = 0;
        static bool file_opened = false;
        // FSM loop runs at 10 hz
        if(loop) {
            led1() = !led1(); // blink led 1
            current_state = stateMachine().runStateMachine();       //running State Machine. Returns 0 if sitting idle or keyboard press (SIT_IDLE state).
            loop = false; // wait until the loop rate timer fires again
        // log loop runs at 1 hz
        if (log_loop) {
            //when the state machine is not in SIT_IDLE state (or a random keyboard press)
            if (current_state == TRANSMIT_LOG or current_state == RECEIVE_SEQUENCE) {
                ;   //pass
            else if(current_state != 0) {
                if (!file_opened) {                                 //if the log file is not open, open it
                    mbedLogger().appendLogFile(current_state, 0);   //open MBED file once
//                    sdLogger().appendLogFile(current_state, 0);     //open SD file once
                    file_opened = true;                             //stops it from continuing to open it

                    pc().printf(">>>>>>>> Recording. Log file opened. <<<<<<<<\n\r");
                //record to Mbed file system     
                mbedLogger().appendLogFile(current_state, 1);    //writing data
//                sdLogger().appendLogFile(current_state, 1);    //writing data
            //when the current FSM state is zero, reset the file
            else {
                //this can only happen once
                if (file_opened) {
                    mbedLogger().appendLogFile(current_state, 0);    //close log file
//                    sdLogger().appendLogFile(current_state, 0);    //close log file
                    file_opened = false;
                    pc().printf(">>>>>>>> Stopped recording. Log file closed. <<<<<<<<\n\r");
            log_loop = false;   // wait until the loop rate timer fires again
        }   //END OF LOG LOOP