megan gimple / Mbed 2 deprecated OCE360_Final_Project

Dependencies:   mbed MMA8452Q MS5837 SDFileSystem SCI_SENSOR

Revision:
34:dd00bd18ead9
Parent:
33:b9e349e91d29
Child:
35:414778105560
diff -r b9e349e91d29 -r dd00bd18ead9 main.cpp
--- a/main.cpp	Wed Dec 08 21:23:08 2021 +0000
+++ b/main.cpp	Wed Dec 08 21:53:14 2021 +0000
@@ -54,7 +54,7 @@
 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 tim = 1;           //define thruster on time
 float percent = 1.5;    //user defined percent of thrust power
 
 //Control related functions
@@ -96,21 +96,20 @@
     //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);
+    //accel_ticker.attach(&accel_update,0.5);  //10Hz
+    //log_ticker.attach(&log_data,0.5);
     led2=0;
     led1=0;
-    //thrust_on(1.2,2);  //Runs the thrusters at 20% for 2 seconds, just to test them.
 
-while(1){
+
     //Part One
     int count=0;
     while(count<3) {
         led3=1;
-        float pw= percent;
-        float on_time =tim;
+        count=count+1;
+        float pw=percent;
+        float on_time=tim;
         thrust_on(pw, on_time); //turn thruster on for 5 seconds
-        count=count+1;
         wait(1);
     }
 
@@ -120,16 +119,16 @@
         led3=0;
         float current_depth=p_sensor.depth();
         float pw=percent;
-        float on_time=3;
+        float on_time=5;
 
         if (current_depth-vessel_length<2) {
             thrust_on(pw,on_time);
         }
-        if (timer2.read()>10){ //60 because counting in seconds?
+        if (timer2.read()>60) { //60 because counting in seconds?
             return 0;
         }
     }
-}
+
 
     /*Part Three
     while(1) {
@@ -145,107 +144,111 @@
                     thrust_on(pw,on_time); //run thruster at very low percentage to maintain depth
         }
     */
-    }
+}
 
 //-------------Customized functions---------------------------------------------//----------------------------------------
 ///-----------Welcome menu---------------------///
-    void welcome() {
-        char buffer[100]= {0};
-        int flag=1;
-        //Flush the port
-        while(BLE.readable()) {
-            BLE.getc();
-        }
-        led1=1;
-        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
+void welcome()
+{
+    char buffer[100]= {0};
+    int flag=1;
+    //Flush the port
+    while(BLE.readable()) {
+        BLE.getc();
+    }
+    led1=1;
+    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);
+            flag = 0; //set the flag to 0 to break the while
         }
-        led2=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 5 seconds\r\n");
-        wait(5);
+        wait(1);
+    }
+    led2=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 5 seconds\r\n");
+    wait(5);
+}
+
 ///-----------log functions---------------------///
-    void log_data() {
-        
-        p_sensor.Barometer_MS5837();
-        
-        //Code from Licht
-        //Sample code shows how to read temperature and light data:
-        BLE.printf("Temperature: %f   Light: %f\r\n",temp.temp(), light.light());
-    
-        //Sample code shows how to read pressure sensor
-        BLE.printf("Depth: %f Pressure: %f Temperature: %f\r\n", p_sensor.depth(), p_sensor.MS5837_Pressure(), p_sensor.MS5837_Temperature());
-        p_sensor.Barometer_MS5837(); //NOTE: this function prompts the pressure sensor to collect the next data point, so it must be included!
+void log_data()
+{
+
+    p_sensor.Barometer_MS5837();
+
+    //Code from Licht
+    //Sample code shows how to read temperature and light data:
+    BLE.printf("Temperature: %f   Light: %f\r\n",temp.temp(), light.light());
 
-        //Sample code indicates how to read accelerometer data:    
-        BLE.printf("Acceleration: %f %f %f\r\n",accelData[0],accelData[1],accelData[2]);   
-        //end of code from Licht
-        
-        fp = fopen(fname, "a");
-        fprintf(fp, "$DATA \r\n");
-        fprintf(fp, "Time:%f \r\n", t.read());
-        fprintf(fp, "Pressure:%f \r\n", p_sensor.MS5837_Pressure());
-        // Time:%f, Pressure:%f, Depth:%f, Acceleration:%f, %f, %f, Temp:%f, Light:%f, Percent:%f\r\n", t.read(),p_sensor.MS5837_Pressure(),p_sensor.depth(),accelData[0],accelData[1],accelData[2],temp.temp(),light.light(),percent);
-        fclose(fp);
-        
-    }
+    //Sample code shows how to read pressure sensor
+    BLE.printf("Depth: %f Pressure: %f Temperature: %f\r\n", p_sensor.depth(), p_sensor.MS5837_Pressure(), p_sensor.MS5837_Temperature());
+    p_sensor.Barometer_MS5837(); //NOTE: this function prompts the pressure sensor to collect the next data point, so it must be included!
+
+    //Sample code indicates how to read accelerometer data:
+    BLE.printf("Acceleration: %f %f %f\r\n",accelData[0],accelData[1],accelData[2]);
+    //end of code from Licht
+
+    fp = fopen(fname, "a");
+    fprintf(fp, "$DATA \r\n");
+    //fprintf(fp, "Time:%f \r\n", t.read());
+    //fprintf(fp, "Pressure:%f \r\n", p_sensor.MS5837_Pressure());
+    // Time:%f, Pressure:%f, Depth:%f, Acceleration:%f, %f, %f, Temp:%f, Light:%f, Percent:%f\r\n", t.read(),p_sensor.MS5837_Pressure(),p_sensor.depth(),accelData[0],accelData[1],accelData[2],temp.temp(),light.light(),percent);
+    fclose(fp);
+
+}
 
 ///-----------acceleromter related functions---------------------///
-    void accel_update() {
-        accelData[0] = accel.readX();
-        accelData[1] = accel.readY();
-        accelData[2] = accel.readZ();
-    }
+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
+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) {
+    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) {
 
-            BLE.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);
+        BLE.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);
 
-    }
+}