megan gimple / Mbed 2 deprecated OCE360_Final_Project

Dependencies:   mbed MMA8452Q MS5837 SDFileSystem SCI_SENSOR

Committer:
mgimple
Date:
Wed Dec 08 20:40:25 2021 +0000
Revision:
30:c54217fc9314
Parent:
29:48534309e09d
Child:
31:f62d09120c6a
Thursters work... Bluetooth sending to phone

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elizabethtaylor 11:fc58334fd36b 1 //OCE 360 Final Project
elizabethtaylor 11:fc58334fd36b 2 //Annaliese Nardi, Eliza Taylor, Mackenzie Fraser, Megan Gimple
elizabethtaylor 11:fc58334fd36b 3 //Updated Code by Stephen Licht
elizabethtaylor 25:69027120c44f 4 //Description: This code controls the movement of a UAV. Collects pressure, temperaure, light, and acceleration data and writes that to a file on an onboard SD card.
elizabethtaylor 11:fc58334fd36b 5
slicht_instructor 0:6e97f9a6aca0 6 #include "mbed.h"
slicht_instructor 0:6e97f9a6aca0 7 #include "MMA8452Q.h" //accelerometer library
slicht_instructor 0:6e97f9a6aca0 8 #include "MS5837.h" //pressure sensor library***
mgimple 1:e1a2b47c3098 9 #include "SCI_SENSOR.h" //science sensor library, photocell, temperature cell
slicht_instructor 0:6e97f9a6aca0 10 #include "SDFileSystem.h" // SD card library
slicht_instructor 0:6e97f9a6aca0 11
slicht_instructor 0:6e97f9a6aca0 12 DigitalOut led1(LED1);
slicht_instructor 0:6e97f9a6aca0 13 DigitalOut led2(LED2);
mgimple 28:55b04fb61809 14 DigitalOut led3(LED3);
slicht_instructor 0:6e97f9a6aca0 15
mgimple 1:e1a2b47c3098 16 Serial pc(USBTX, USBRX); //initial serial
mgimple 1:e1a2b47c3098 17 Serial BLE(p13,p14); //Bluetooth
mgimple 1:e1a2b47c3098 18 MMA8452Q accel(p28,p27,0x1D); //initial accelerometer
mgimple 1:e1a2b47c3098 19 LM19 temp(p19); //temperature sensor
mgimple 1:e1a2b47c3098 20 PhotoCell light(p20); //photocell
slicht_instructor 0:6e97f9a6aca0 21 MS5837 p_sensor(p9, p10, ms5837_addr_no_CS); //pressure sensor
slicht_instructor 0:6e97f9a6aca0 22 PwmOut thruster(p21); //set PWM pin //max 1.3ms min 1.1ms
slicht_instructor 0:6e97f9a6aca0 23 PwmOut thruster2(p22); //set PWM pin
slicht_instructor 0:6e97f9a6aca0 24 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
slicht_instructor 0:6e97f9a6aca0 25
slicht_instructor 0:6e97f9a6aca0 26 //global ticker
slicht_instructor 0:6e97f9a6aca0 27 Ticker log_ticker;
slicht_instructor 0:6e97f9a6aca0 28 Ticker accel_ticker;
mgimple 1:e1a2b47c3098 29
slicht_instructor 0:6e97f9a6aca0 30 // global timer
slicht_instructor 0:6e97f9a6aca0 31 Timer t;
mgimple 20:6fb55c833f9f 32 Timer timer2; //part two timer
slicht_instructor 0:6e97f9a6aca0 33
slicht_instructor 0:6e97f9a6aca0 34 ///File
slicht_instructor 0:6e97f9a6aca0 35 FILE *fp;
slicht_instructor 0:6e97f9a6aca0 36 char fname[100];
slicht_instructor 0:6e97f9a6aca0 37 float PI = 3.14159265358979323846f;
slicht_instructor 0:6e97f9a6aca0 38
slicht_instructor 0:6e97f9a6aca0 39 //float operation parameters
mgimple 1:e1a2b47c3098 40 float target_depth=0; //global target depth default 0
mgimple 1:e1a2b47c3098 41 int yo_num=0; //global yo_num default 0
mgimple 1:e1a2b47c3098 42 float thrust_on_time=0; //global thrust_on time default 0
mgimple 1:e1a2b47c3098 43 float accelData[3]; //global accel data
mgimple 20:6fb55c833f9f 44 float vessel_length=1;
slicht_instructor 0:6e97f9a6aca0 45
slicht_instructor 0:6e97f9a6aca0 46 //functions
slicht_instructor 0:6e97f9a6aca0 47 void welcome();
slicht_instructor 0:6e97f9a6aca0 48 void log_data();
mgimple 4:d87b1e40d419 49
slicht_instructor 0:6e97f9a6aca0 50 //IMU related
slicht_instructor 0:6e97f9a6aca0 51 void accel_update(); //update accelerometer related variables. we use imu_ticker to call this function
mgimple 5:17f2b3e61112 52
mgimple 9:e4e1d5db0c04 53 //Control Parameters
mgimple 9:e4e1d5db0c04 54 float tim =5; //define thruster on time
mgimple 14:ed172dec4022 55 float percent = 1.5; //user defined percent of thrust power
mgimple 9:e4e1d5db0c04 56
slicht_instructor 0:6e97f9a6aca0 57 //Control related functions
slicht_instructor 0:6e97f9a6aca0 58 void thrust_on(float pw, float on_time); //input is pulse width
mgimple 10:b2fe6d1dee81 59 //void thrust_off(float pw, float on_time); // turn off pulse width
mgimple 9:e4e1d5db0c04 60
mgimple 5:17f2b3e61112 61
elizabethtaylor 11:fc58334fd36b 62 //global variables
elizabethtaylor 11:fc58334fd36b 63
elizabethtaylor 11:fc58334fd36b 64
slicht_instructor 0:6e97f9a6aca0 65 //-------------Main functions-----------------------------------------------------------------------------------------
slicht_instructor 0:6e97f9a6aca0 66 int main()
slicht_instructor 0:6e97f9a6aca0 67 {
slicht_instructor 0:6e97f9a6aca0 68 //-----Initialization realted code-------//
mgimple 18:b5f58a274570 69 //inital set the thruster esc to 1ms duty cycle
slicht_instructor 0:6e97f9a6aca0 70 thruster.period(0.002); // 2 ms period
mgimple 18:b5f58a274570 71 thruster.pulsewidth(1.0/1000.000);
mgimple 29:48534309e09d 72
mgimple 29:48534309e09d 73 thruster2.period(0.002); // 2 ms period
mgimple 29:48534309e09d 74 thruster2.pulsewidth(1.0/1000.000);
mgimple 18:b5f58a274570 75
slicht_instructor 0:6e97f9a6aca0 76 //Initialize accelerometer:
slicht_instructor 0:6e97f9a6aca0 77 accel.init();
slicht_instructor 0:6e97f9a6aca0 78 led1=1;
mgimple 18:b5f58a274570 79
slicht_instructor 0:6e97f9a6aca0 80 //initialize pressure sensor
slicht_instructor 0:6e97f9a6aca0 81 pc.printf("setting the pressure sensor\r\n");
slicht_instructor 0:6e97f9a6aca0 82 p_sensor.MS5837Reset();
slicht_instructor 0:6e97f9a6aca0 83 p_sensor.MS5837Init();
mgimple 6:5f55105701ac 84 pc.printf("setting the tickers\r\n");
slicht_instructor 0:6e97f9a6aca0 85 t.start();
slicht_instructor 0:6e97f9a6aca0 86 led2=1;
slicht_instructor 0:6e97f9a6aca0 87 welcome();
mgimple 18:b5f58a274570 88
slicht_instructor 0:6e97f9a6aca0 89 //-----setup ticker-------//
slicht_instructor 0:6e97f9a6aca0 90 //setup ticker to separate log and IMU data update.
slicht_instructor 0:6e97f9a6aca0 91 //so we could have all our control code in the while loop
slicht_instructor 0:6e97f9a6aca0 92 // //log at 2 Hz
mgimple 30:c54217fc9314 93 //accel_ticker.attach(&accel_update,0.1); //10Hz
mgimple 29:48534309e09d 94 //log_ticker.attach(&log_data,0.5);
mgimple 28:55b04fb61809 95 led1=0;
mgimple 28:55b04fb61809 96 led2=0;
mgimple 18:b5f58a274570 97
mgimple 17:e72c4ac9a39e 98 //Part One
elizabethtaylor 25:69027120c44f 99 int count=0;
elizabethtaylor 25:69027120c44f 100 while(count<3) {
mgimple 27:e0943cb83251 101 led3=1;
mgimple 20:6fb55c833f9f 102 float pw= percent;
mgimple 20:6fb55c833f9f 103 float on_time =tim;
mgimple 20:6fb55c833f9f 104 thrust_on(pw, on_time); //turn thruster on for 5 seconds
mgimple 29:48534309e09d 105 count=count+1;
mgimple 21:1a9fe2de6e4f 106 wait(1);
mgimple 19:c78d3e8353c4 107 }
mgimple 18:b5f58a274570 108
mgimple 17:e72c4ac9a39e 109 //Part Two
mgimple 21:1a9fe2de6e4f 110 timer2.start();
mgimple 18:b5f58a274570 111 while(1) {
mgimple 27:e0943cb83251 112 led3=0;
mgimple 17:e72c4ac9a39e 113 float current_depth=p_sensor.depth();
mgimple 17:e72c4ac9a39e 114 float pw=percent;
mgimple 17:e72c4ac9a39e 115 float on_time=3;
mgimple 18:b5f58a274570 116
mgimple 18:b5f58a274570 117 if (current_depth-vessel_length<2) {
mgimple 17:e72c4ac9a39e 118 thrust_on(pw,on_time);
mgimple 18:b5f58a274570 119 }
mgimple 21:1a9fe2de6e4f 120 if (timer2.read()>10){ //60 because counting in seconds?
mgimple 17:e72c4ac9a39e 121 return 0;
mgimple 17:e72c4ac9a39e 122 }
mgimple 18:b5f58a274570 123 }
mgimple 21:1a9fe2de6e4f 124
mgimple 18:b5f58a274570 125
mgimple 19:c78d3e8353c4 126 /*Part Three
mgimple 18:b5f58a274570 127 while(1) {
mgimple 18:b5f58a274570 128 error_deep = 2.5; //deep end of error band 2.5m ?
mgimple 18:b5f58a274570 129 error_shallow = 1.5; //shallow end of error band 1.5m?
slicht_instructor 0:6e97f9a6aca0 130
mgimple 18:b5f58a274570 131 if (current_depth <= error_shallow) { //if float is less than 1.5meters deep
mgimple 18:b5f58a274570 132 thrust_on(pw,on_time); // turn on thruster at set pw and ontime
mgimple 18:b5f58a274570 133 else if (current_depth >= error_deep) //if float is more than 2.5 meters deep
mgimple 18:b5f58a274570 134 wait(1); // do nothing for 1 sec.. float will go up for 1 sec
mgimple 20:6fb55c833f9f 135 else
mgimple 18:b5f58a274570 136 pw = ; //very low thruster percentage
mgimple 20:6fb55c833f9f 137 thrust_on(pw,on_time); //run thruster at very low percentage to maintain depth
mgimple 18:b5f58a274570 138 }
mgimple 19:c78d3e8353c4 139 */
mgimple 20:6fb55c833f9f 140 }
slicht_instructor 0:6e97f9a6aca0 141
slicht_instructor 0:6e97f9a6aca0 142 //-------------Customized functions---------------------------------------------//----------------------------------------
slicht_instructor 0:6e97f9a6aca0 143 ///-----------Welcome menu---------------------///
mgimple 20:6fb55c833f9f 144 void welcome() {
mgimple 20:6fb55c833f9f 145 char buffer[100]= {0};
mgimple 20:6fb55c833f9f 146 int flag=1;
mgimple 20:6fb55c833f9f 147 //Flush the port
mgimple 20:6fb55c833f9f 148 while(BLE.readable()) {
mgimple 20:6fb55c833f9f 149 BLE.getc();
mgimple 20:6fb55c833f9f 150 }
mgimple 20:6fb55c833f9f 151 while(flag) {
mgimple 20:6fb55c833f9f 152 BLE.printf("### I am alive\r\n");
mgimple 20:6fb55c833f9f 153 BLE.printf("### Please enter the log file name you want\r\n");
mgimple 20:6fb55c833f9f 154 if(BLE.readable()) {
mgimple 20:6fb55c833f9f 155 BLE.scanf("%s",buffer);
mgimple 20:6fb55c833f9f 156 sprintf(fname,"/sd/mydir/%s.txt",buffer); //make file name
slicht_instructor 0:6e97f9a6aca0 157
mgimple 20:6fb55c833f9f 158 flag = 0; //set the flag to 0 to break the while
mgimple 18:b5f58a274570 159 }
mgimple 20:6fb55c833f9f 160 wait(1);
mgimple 20:6fb55c833f9f 161 }
mgimple 20:6fb55c833f9f 162 //print name
mgimple 20:6fb55c833f9f 163 BLE.printf("### name received\r\n");
mgimple 20:6fb55c833f9f 164 BLE.printf("### file name and directory is: \r\n %s\r\n",fname); //file name and location
mgimple 20:6fb55c833f9f 165 //open file test
mgimple 20:6fb55c833f9f 166 mkdir("/sd/mydir",0777); //keep 0777, this is magic #
mgimple 20:6fb55c833f9f 167 fp = fopen(fname, "a");
mgimple 20:6fb55c833f9f 168 if(fp == NULL) {
mgimple 20:6fb55c833f9f 169 BLE.printf("Could not open file for write\n");
mgimple 20:6fb55c833f9f 170 } else {
mgimple 20:6fb55c833f9f 171 BLE.printf("##file open good \n"); //open file and tell if open
mgimple 20:6fb55c833f9f 172 fprintf(fp, "Hello\r\n");
elizabethtaylor 25:69027120c44f 173 fclose(fp);
slicht_instructor 0:6e97f9a6aca0 174 }
slicht_instructor 0:6e97f9a6aca0 175
mgimple 20:6fb55c833f9f 176 BLE.printf("### The main program will start in 5 seconds\r\n");
mgimple 20:6fb55c833f9f 177 wait(5);
mgimple 20:6fb55c833f9f 178 }
mgimple 20:6fb55c833f9f 179
slicht_instructor 0:6e97f9a6aca0 180 ///-----------log functions---------------------///
mgimple 20:6fb55c833f9f 181 void log_data() {
elizabethtaylor 22:2a49efff8839 182
mgimple 20:6fb55c833f9f 183 p_sensor.Barometer_MS5837();
elizabethtaylor 22:2a49efff8839 184
elizabethtaylor 22:2a49efff8839 185 //Code from Licht
elizabethtaylor 22:2a49efff8839 186 //Sample code shows how to read temperature and light data:
elizabethtaylor 22:2a49efff8839 187 BLE.printf("Temperature: %f Light: %f\r\n",temp.temp(), light.light());
elizabethtaylor 22:2a49efff8839 188
elizabethtaylor 22:2a49efff8839 189 //Sample code shows how to read pressure sensor
elizabethtaylor 22:2a49efff8839 190 BLE.printf("Depth: %f Pressure: %f Temperature: %f\r\n", p_sensor.depth(), p_sensor.MS5837_Pressure(), p_sensor.MS5837_Temperature());
elizabethtaylor 22:2a49efff8839 191 p_sensor.Barometer_MS5837(); //NOTE: this function prompts the pressure sensor to collect the next data point, so it must be included!
slicht_instructor 0:6e97f9a6aca0 192
elizabethtaylor 22:2a49efff8839 193 //Sample code indicates how to read accelerometer data:
elizabethtaylor 22:2a49efff8839 194 BLE.printf("Acceleration: %f %f %f\r\n",accelData[0],accelData[1],accelData[2]);
elizabethtaylor 22:2a49efff8839 195 //end of code from Licht
elizabethtaylor 25:69027120c44f 196
mgimple 26:379bc50f5f23 197 fp = fopen(fname, "a");
mgimple 29:48534309e09d 198 fprintf(fp, "$DATA, 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);
elizabethtaylor 25:69027120c44f 199 fclose(fp);
mgimple 20:6fb55c833f9f 200 }
slicht_instructor 0:6e97f9a6aca0 201
slicht_instructor 0:6e97f9a6aca0 202 ///-----------acceleromter related functions---------------------///
mgimple 20:6fb55c833f9f 203 void accel_update() {
mgimple 20:6fb55c833f9f 204 accelData[0] = accel.readX();
mgimple 20:6fb55c833f9f 205 accelData[1] = accel.readY();
mgimple 20:6fb55c833f9f 206 accelData[2] = accel.readZ();
mgimple 20:6fb55c833f9f 207 }
slicht_instructor 0:6e97f9a6aca0 208
slicht_instructor 0:6e97f9a6aca0 209 ///-----------Control related functions---------------------///
slicht_instructor 0:6e97f9a6aca0 210 ////Thruster on control, pw->pulse width in milli-second//
slicht_instructor 0:6e97f9a6aca0 211 //// pw range between 1 to 1.5//
slicht_instructor 0:6e97f9a6aca0 212 //// on_time-> thruster on time.
mgimple 20:6fb55c833f9f 213 void thrust_on(float pw, float on_time) { //input is pulse width
slicht_instructor 0:6e97f9a6aca0 214
mgimple 20:6fb55c833f9f 215 float pw_max=2.0;
mgimple 20:6fb55c833f9f 216 if(pw>pw_max) {
mgimple 20:6fb55c833f9f 217 pw=pw_max; //hard limitation
mgimple 20:6fb55c833f9f 218 }
mgimple 20:6fb55c833f9f 219 Timer tt;
mgimple 20:6fb55c833f9f 220 tt.reset();
mgimple 20:6fb55c833f9f 221 tt.start();
mgimple 20:6fb55c833f9f 222 // lets set the pulse width
mgimple 20:6fb55c833f9f 223 //thruster.period(20.0/1000.00); // 20 ms period
mgimple 20:6fb55c833f9f 224 thruster.pulsewidth(pw/1000.00);
mgimple 20:6fb55c833f9f 225 thruster2.pulsewidth(pw/1000.00);
mgimple 20:6fb55c833f9f 226 //PWM will be kept until time out
mgimple 20:6fb55c833f9f 227 while(tt.read()<=on_time) {
mgimple 18:b5f58a274570 228
mgimple 29:48534309e09d 229 BLE.printf("thruster on?...mack is cool\r\n"); //print check
mgimple 20:6fb55c833f9f 230 }
mgimple 20:6fb55c833f9f 231 //stop the timer
mgimple 20:6fb55c833f9f 232 tt.stop();
mgimple 20:6fb55c833f9f 233 //turn off the thruster
mgimple 20:6fb55c833f9f 234 thruster.pulsewidth(1.0/1000.00);
mgimple 20:6fb55c833f9f 235 thruster2.pulsewidth(1.0/1000.00);
mgimple 18:b5f58a274570 236
mgimple 20:6fb55c833f9f 237 }