megan gimple / Mbed 2 deprecated OCE360_Final_Project

Dependencies:   mbed MMA8452Q MS5837 SDFileSystem SCI_SENSOR

main.cpp

Committer:
mgimple
Date:
2021-12-02
Revision:
10:b2fe6d1dee81
Parent:
9:e4e1d5db0c04
Child:
12:54b498af39df
Child:
14:ed172dec4022

File content as of revision 10:b2fe6d1dee81:

//Eliza
//Megan
/*
Author Mingxi Zhou
OCE360 underwater float template program
*/
#include "mbed.h"
#include "MMA8452Q.h"  //accelerometer library
#include "MS5837.h"     //pressure sensor library***
#include "SCI_SENSOR.h"     //science sensor library, photocell, temperature cell
#include "SDFileSystem.h"   // SD card library

DigitalOut led1(LED1);
DigitalOut led2(LED2);

Serial pc(USBTX, USBRX);        //initial serial
Serial BLE(p13,p14);            //Bluetooth
MMA8452Q accel(p28,p27,0x1D);   //initial accelerometer
LM19 temp(p19);                 //temperature sensor
PhotoCell light(p20);           //photocell
MS5837 p_sensor(p9, p10, ms5837_addr_no_CS);  //pressure sensor
PwmOut thruster(p21);  //set PWM pin    //max 1.3ms min 1.1ms
PwmOut thruster2(p22); //set PWM pin
SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board

//global ticker
Ticker log_ticker;
Ticker accel_ticker;

// global timer
Timer t;

///File
FILE *fp;
char fname[100];
float PI = 3.14159265358979323846f;

//float operation parameters
float target_depth=0;       //global target depth default 0
int yo_num=0;               //global yo_num default 0
float thrust_on_time=0;     //global thrust_on time default 0
float accelData[3];         //global accel data

//functions
void welcome();
void log_data();

//IMU related
void accel_update(); //update accelerometer related variables. we use imu_ticker to call this function

//Control Parameters
float tim =5;           //define thruster on time
float percent = 1.2;    //user defined percent of thrust power

//Control related functions
void thrust_on(float pw, float on_time);  //input is pulse width
//void thrust_off(float pw, float on_time); // turn off pulse width


//-------------Main functions-----------------------------------------------------------------------------------------
int main()
{
//-----Initialization realted code-------//
   //inital set the thruster esc to 1ms duty cycle
    thruster.period(0.002);      // 2 ms period
    thruster.pulsewidth(1.0/1000.000);   
    
    //Initialize accelerometer:
    accel.init();
    led1=1;
    
    //initialize pressure sensor
    pc.printf("setting the pressure sensor\r\n");
    p_sensor.MS5837Reset();
    p_sensor.MS5837Init();
    pc.printf("setting the tickers\r\n");
    t.start();
    led2=1;
    welcome();
    
    //-----setup ticker-------//
    //setup ticker to separate log and IMU data update.
    //so we could have all our control code in the while loop
    //   //log at 2 Hz
    accel_ticker.attach(&accel_update,0.1);  //10Hz
    log_ticker.attach(&log_data,0.5);
    wait(1);
    
    while(1)
    {
      float pw= percent;
      float on_time =tim;
      thrust_on(pw, on_time); //turn thruster on for 5 seconds
      return 0;
    }

}

//-------------Customized functions---------------------------------------------//----------------------------------------
///-----------Welcome menu---------------------///
void welcome()
{
    char buffer[100]={0};
    int flag=1;
    //Flush the port
    while(BLE.readable())
    {
    BLE.getc();
    }
    while(flag)
    {
        BLE.printf("### I am alive\r\n");
        BLE.printf("### Please enter the log file name you want\r\n");
        if(BLE.readable())
        {
            BLE.scanf("%s",buffer);
            sprintf(fname,"/sd/mydir/%s.txt",buffer); //make file name

            flag = 0; //set the flag to 0 to break the while
        }
        wait(1);
    }
    //print name
    BLE.printf("### name received\r\n");
    BLE.printf("### file name and directory is: \r\n %s\r\n",fname); //file name and location
    //open file test
    mkdir("/sd/mydir",0777); //keep 0777, this is magic #
    fp = fopen(fname, "a");
    if(fp == NULL){
        BLE.printf("Could not open file for write\n");
    }
    else
    {
        BLE.printf("##file open good \n"); //open file and tell if open
        fprintf(fp, "Hello\r\n");
        fclose(fp);
    }

    BLE.printf("### The main program will start in 10 seconds\r\n");
    wait(5);
}

///-----------log functions---------------------///
void log_data()
{
    //log system time t.read()
    // log imu data, log science data
    // log pulse width
    // log pressure sensor data.
    //science sensor: temp.temp(), light.light()
    //IMU sensor

}

///-----------acceleromter related functions---------------------///
void accel_update()
{
    accelData[0] = accel.readX();
    accelData[1] = accel.readY();
    accelData[2] = accel.readZ(); 
}

///-----------Control related functions---------------------///
////Thruster on control, pw->pulse width in milli-second//
////                        pw range between 1 to 1.5//
////                       on_time-> thruster on time.
void thrust_on(float pw, float on_time)  //input is pulse width
{
    
    float pw_max=2.0;
    if(pw>pw_max)
    {
        pw=pw_max; //hard limitation
    }
    Timer tt;
    tt.reset();
    tt.start();
    // lets set the pulse width
    //thruster.period(20.0/1000.00);      // 20 ms period
    thruster.pulsewidth(pw/1000.00);
    thruster2.pulsewidth(pw/1000.00);
    //PWM will be kept until time out
    while(tt.read()<=on_time)
    {
        
        pc.printf("thruster on?...mack is cool\r\n"); //print check
    }
    //stop the timer
    tt.stop();
    //turn off the thruster
    thruster.pulsewidth(1.0/1000.00);
    thruster2.pulsewidth(1.0/1000.00);

}

///void thrust_off(float pw, float on_time)  //input is pulse width
//