Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MMA8452Q MS5837 SDFileSystem SCI_SENSOR
main.cpp@41:9c7e35212c68, 2021-12-10 (annotated)
- Committer:
- mgimple
- Date:
- Fri Dec 10 21:58:49 2021 +0000
- Revision:
- 41:9c7e35212c68
- Parent:
- 40:8a37ea9d006d
- Child:
- 42:4cad666e57f4
wasdfgh
Who changed what in which revision?
User | Revision | Line number | New 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 | 40:8a37ea9d006d | 44 | //float vessel_length=0.75; //vessel length 0.75 meters |
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 | 40:8a37ea9d006d | 54 | float tim = 5; //define thruster on time |
mgimple | 41:9c7e35212c68 | 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 | thruster2.period(0.002); // 2 ms period |
mgimple | 29:48534309e09d | 73 | thruster2.pulsewidth(1.0/1000.000); |
mgimple | 18:b5f58a274570 | 74 | |
slicht_instructor | 0:6e97f9a6aca0 | 75 | //Initialize accelerometer: |
slicht_instructor | 0:6e97f9a6aca0 | 76 | accel.init(); |
slicht_instructor | 0:6e97f9a6aca0 | 77 | led1=1; |
mgimple | 18:b5f58a274570 | 78 | |
slicht_instructor | 0:6e97f9a6aca0 | 79 | //initialize pressure sensor |
slicht_instructor | 0:6e97f9a6aca0 | 80 | pc.printf("setting the pressure sensor\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 81 | p_sensor.MS5837Reset(); |
slicht_instructor | 0:6e97f9a6aca0 | 82 | p_sensor.MS5837Init(); |
mgimple | 6:5f55105701ac | 83 | pc.printf("setting the tickers\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 84 | t.start(); |
slicht_instructor | 0:6e97f9a6aca0 | 85 | led2=1; |
mgimple | 31:f62d09120c6a | 86 | //***************************************************** |
mgimple | 31:f62d09120c6a | 87 | //waits until it gets an answer over the Bluetooth link! |
mgimple | 31:f62d09120c6a | 88 | welcome(); //prompts user for the logfile name |
mgimple | 31:f62d09120c6a | 89 | //******************************************************** |
mgimple | 31:f62d09120c6a | 90 | |
mgimple | 18:b5f58a274570 | 91 | |
slicht_instructor | 0:6e97f9a6aca0 | 92 | //-----setup ticker-------// |
slicht_instructor | 0:6e97f9a6aca0 | 93 | //setup ticker to separate log and IMU data update. |
slicht_instructor | 0:6e97f9a6aca0 | 94 | //so we could have all our control code in the while loop |
slicht_instructor | 0:6e97f9a6aca0 | 95 | // //log at 2 Hz |
mgimple | 40:8a37ea9d006d | 96 | accel_ticker.attach(&accel_update,1); // |
mgimple | 35:414778105560 | 97 | log_ticker.attach(&log_data,0.5); |
mgimple | 31:f62d09120c6a | 98 | led2=0; |
mgimple | 28:55b04fb61809 | 99 | led1=0; |
mgimple | 18:b5f58a274570 | 100 | |
mgimple | 41:9c7e35212c68 | 101 | //Part One |
mgimple | 41:9c7e35212c68 | 102 | int count=0; |
mgimple | 41:9c7e35212c68 | 103 | while(count<3) { |
mgimple | 41:9c7e35212c68 | 104 | led1=1; |
mgimple | 41:9c7e35212c68 | 105 | count=count+1; |
mgimple | 41:9c7e35212c68 | 106 | float pw=percent; |
mgimple | 41:9c7e35212c68 | 107 | float on_time=tim; |
mgimple | 41:9c7e35212c68 | 108 | thrust_on(pw, on_time); //turn thruster on for 5 seconds |
mgimple | 41:9c7e35212c68 | 109 | wait(10); |
mgimple | 41:9c7e35212c68 | 110 | } |
mgimple | 41:9c7e35212c68 | 111 | |
mgimple | 41:9c7e35212c68 | 112 | //Part Two |
mgimple | 41:9c7e35212c68 | 113 | timer2.start(); |
mgimple | 18:b5f58a274570 | 114 | |
mgimple | 41:9c7e35212c68 | 115 | while(timer2.read()<60) { |
mgimple | 41:9c7e35212c68 | 116 | led1=0; |
mgimple | 41:9c7e35212c68 | 117 | led2=1; |
mgimple | 41:9c7e35212c68 | 118 | float current_depth=p_sensor.depth(); |
mgimple | 41:9c7e35212c68 | 119 | float pw=percent; |
mgimple | 41:9c7e35212c68 | 120 | float on_time=2; |
mgimple | 18:b5f58a274570 | 121 | |
mgimple | 41:9c7e35212c68 | 122 | if (current_depth<1.8) { |
mgimple | 41:9c7e35212c68 | 123 | thrust_on(pw,on_time); |
mgimple | 18:b5f58a274570 | 124 | } |
mgimple | 41:9c7e35212c68 | 125 | } |
mgimple | 41:9c7e35212c68 | 126 | timer2.reset(); |
mgimple | 41:9c7e35212c68 | 127 | timer2.start(); |
mgimple | 41:9c7e35212c68 | 128 | |
mgimple | 41:9c7e35212c68 | 129 | //Part Three |
mgimple | 41:9c7e35212c68 | 130 | while(timer2.read()<60) { |
mgimple | 41:9c7e35212c68 | 131 | led2=0; |
mgimple | 41:9c7e35212c68 | 132 | led3=1; |
mgimple | 41:9c7e35212c68 | 133 | float error_deep = 2.3; //deep end of error band 2.5m ? |
mgimple | 41:9c7e35212c68 | 134 | float error_shallow = 1.7; //shallow end of error band 1.5m? |
mgimple | 41:9c7e35212c68 | 135 | float target_depth=2; |
mgimple | 41:9c7e35212c68 | 136 | float current_depth = p_sensor.depth(); |
mgimple | 41:9c7e35212c68 | 137 | float on_time=2; |
mgimple | 34:dd00bd18ead9 | 138 | |
mgimple | 18:b5f58a274570 | 139 | |
mgimple | 41:9c7e35212c68 | 140 | if ((current_depth>=0)&&(current_depth<=error_shallow)) { //if float is between 0 & 1.6 meters deep |
mgimple | 41:9c7e35212c68 | 141 | float pw = 1.5; |
mgimple | 41:9c7e35212c68 | 142 | thrust_on(pw,on_time); |
mgimple | 41:9c7e35212c68 | 143 | } // turn on thruster at set pw and ontime |
mgimple | 41:9c7e35212c68 | 144 | else if ((current_depth>=error_shallow)&&(current_depth<=target_depth)) {//if float is between 1.6 & 2 meters deep |
mgimple | 41:9c7e35212c68 | 145 | float pw = 1.2; |
mgimple | 41:9c7e35212c68 | 146 | thrust_on(pw,on_time); |
mgimple | 41:9c7e35212c68 | 147 | } |
mgimple | 41:9c7e35212c68 | 148 | else if ((current_depth>=target_depth)&&(current_depth<=error_deep)) {//if float is inbetween 2 & 2.4 meters deep |
mgimple | 41:9c7e35212c68 | 149 | float pw = 1.0; |
mgimple | 41:9c7e35212c68 | 150 | thrust_on(pw,on_time); |
mgimple | 41:9c7e35212c68 | 151 | } |
mgimple | 41:9c7e35212c68 | 152 | else if (current_depth>error_deep) { |
mgimple | 41:9c7e35212c68 | 153 | wait(1); //float up 1 second |
mgimple | 41:9c7e35212c68 | 154 | } |
mgimple | 36:93a04bd48152 | 155 | } |
mgimple | 34:dd00bd18ead9 | 156 | } |
slicht_instructor | 0:6e97f9a6aca0 | 157 | |
mgimple | 41:9c7e35212c68 | 158 | |
mgimple | 41:9c7e35212c68 | 159 | |
slicht_instructor | 0:6e97f9a6aca0 | 160 | //-------------Customized functions---------------------------------------------//---------------------------------------- |
slicht_instructor | 0:6e97f9a6aca0 | 161 | ///-----------Welcome menu---------------------/// |
mgimple | 34:dd00bd18ead9 | 162 | void welcome() |
mgimple | 34:dd00bd18ead9 | 163 | { |
mgimple | 34:dd00bd18ead9 | 164 | char buffer[100]= {0}; |
mgimple | 34:dd00bd18ead9 | 165 | int flag=1; |
mgimple | 34:dd00bd18ead9 | 166 | //Flush the port |
mgimple | 34:dd00bd18ead9 | 167 | while(BLE.readable()) { |
mgimple | 34:dd00bd18ead9 | 168 | BLE.getc(); |
mgimple | 34:dd00bd18ead9 | 169 | } |
mgimple | 34:dd00bd18ead9 | 170 | led1=1; |
mgimple | 34:dd00bd18ead9 | 171 | while(flag) { |
mgimple | 34:dd00bd18ead9 | 172 | BLE.printf("### I am alive\r\n"); |
mgimple | 34:dd00bd18ead9 | 173 | BLE.printf("### Please enter the log file name you want\r\n"); |
mgimple | 34:dd00bd18ead9 | 174 | if(BLE.readable()) { |
mgimple | 34:dd00bd18ead9 | 175 | BLE.scanf("%s",buffer); |
mgimple | 34:dd00bd18ead9 | 176 | sprintf(fname,"/sd/mydir/%s.txt",buffer); //make file name |
slicht_instructor | 0:6e97f9a6aca0 | 177 | |
mgimple | 34:dd00bd18ead9 | 178 | flag = 0; //set the flag to 0 to break the while |
mgimple | 20:6fb55c833f9f | 179 | } |
mgimple | 34:dd00bd18ead9 | 180 | wait(1); |
mgimple | 34:dd00bd18ead9 | 181 | } |
mgimple | 34:dd00bd18ead9 | 182 | led2=1; |
mgimple | 34:dd00bd18ead9 | 183 | //print name |
mgimple | 34:dd00bd18ead9 | 184 | BLE.printf("### name received\r\n"); |
mgimple | 34:dd00bd18ead9 | 185 | BLE.printf("### file name and directory is: \r\n %s\r\n",fname); //file name and location |
mgimple | 34:dd00bd18ead9 | 186 | //open file test |
mgimple | 34:dd00bd18ead9 | 187 | mkdir("/sd/mydir",0777); //keep 0777, this is magic # |
mgimple | 34:dd00bd18ead9 | 188 | fp = fopen(fname, "a"); |
mgimple | 34:dd00bd18ead9 | 189 | if(fp == NULL) { |
mgimple | 34:dd00bd18ead9 | 190 | BLE.printf("Could not open file for write\n"); |
mgimple | 34:dd00bd18ead9 | 191 | } else { |
mgimple | 34:dd00bd18ead9 | 192 | BLE.printf("##file open good \n"); //open file and tell if open |
mgimple | 34:dd00bd18ead9 | 193 | fprintf(fp, "Hello\r\n"); |
mgimple | 34:dd00bd18ead9 | 194 | fclose(fp); |
mgimple | 20:6fb55c833f9f | 195 | } |
mgimple | 20:6fb55c833f9f | 196 | |
mgimple | 34:dd00bd18ead9 | 197 | BLE.printf("### The main program will start in 5 seconds\r\n"); |
mgimple | 34:dd00bd18ead9 | 198 | wait(5); |
mgimple | 34:dd00bd18ead9 | 199 | } |
mgimple | 34:dd00bd18ead9 | 200 | |
slicht_instructor | 0:6e97f9a6aca0 | 201 | ///-----------log functions---------------------/// |
mgimple | 34:dd00bd18ead9 | 202 | void log_data() |
mgimple | 34:dd00bd18ead9 | 203 | { |
mgimple | 40:8a37ea9d006d | 204 | |
mgimple | 34:dd00bd18ead9 | 205 | p_sensor.Barometer_MS5837(); |
mgimple | 34:dd00bd18ead9 | 206 | |
mgimple | 34:dd00bd18ead9 | 207 | //Code from Licht |
mgimple | 34:dd00bd18ead9 | 208 | //Sample code shows how to read temperature and light data: |
mgimple | 38:46975eaa1400 | 209 | BLE.printf("Temperature: %f Light: %f\r\n",temp.temp(), light.light()); |
slicht_instructor | 0:6e97f9a6aca0 | 210 | |
mgimple | 34:dd00bd18ead9 | 211 | //Sample code shows how to read pressure sensor |
mgimple | 39:f2f0308ebecd | 212 | BLE.printf("Depth: %f Pressure: %f \r\n", p_sensor.depth(), p_sensor.MS5837_Pressure()); //p_sensor.MS5837_Temperature()); |
mgimple | 37:7bb14ad22d78 | 213 | //p_sensor.Barometer_MS5837(); //NOTE: this function prompts the pressure sensor to collect the next data point, so it must be included! |
mgimple | 34:dd00bd18ead9 | 214 | |
mgimple | 34:dd00bd18ead9 | 215 | //Sample code indicates how to read accelerometer data: |
mgimple | 39:f2f0308ebecd | 216 | BLE.printf("Acceleration: %f %f %f\r\n",accelData[0],accelData[1],accelData[2]); |
mgimple | 34:dd00bd18ead9 | 217 | //end of code from Licht |
mgimple | 40:8a37ea9d006d | 218 | float ttime =t.read(); |
mgimple | 40:8a37ea9d006d | 219 | float depth =p_sensor.depth(); |
mgimple | 40:8a37ea9d006d | 220 | float pressure =p_sensor.MS5837_Pressure(); |
mgimple | 41:9c7e35212c68 | 221 | |
mgimple | 40:8a37ea9d006d | 222 | |
mgimple | 34:dd00bd18ead9 | 223 | fp = fopen(fname, "a"); |
mgimple | 41:9c7e35212c68 | 224 | fprintf(fp, "$DATA %f,%f,%f,%f,%f,%f,%f \r\n", ttime,depth,pressure,accelData[0],accelData[1],accelData[2],light.light()); |
mgimple | 34:dd00bd18ead9 | 225 | fclose(fp); |
mgimple | 34:dd00bd18ead9 | 226 | } |
slicht_instructor | 0:6e97f9a6aca0 | 227 | |
slicht_instructor | 0:6e97f9a6aca0 | 228 | ///-----------acceleromter related functions---------------------/// |
mgimple | 34:dd00bd18ead9 | 229 | void accel_update() |
mgimple | 34:dd00bd18ead9 | 230 | { |
mgimple | 34:dd00bd18ead9 | 231 | accelData[0] = accel.readX(); |
mgimple | 34:dd00bd18ead9 | 232 | accelData[1] = accel.readY(); |
mgimple | 34:dd00bd18ead9 | 233 | accelData[2] = accel.readZ(); |
mgimple | 34:dd00bd18ead9 | 234 | } |
slicht_instructor | 0:6e97f9a6aca0 | 235 | |
slicht_instructor | 0:6e97f9a6aca0 | 236 | ///-----------Control related functions---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 237 | ////Thruster on control, pw->pulse width in milli-second// |
slicht_instructor | 0:6e97f9a6aca0 | 238 | //// pw range between 1 to 1.5// |
slicht_instructor | 0:6e97f9a6aca0 | 239 | //// on_time-> thruster on time. |
mgimple | 34:dd00bd18ead9 | 240 | void thrust_on(float pw, float on_time) //input is pulse width |
mgimple | 34:dd00bd18ead9 | 241 | { |
slicht_instructor | 0:6e97f9a6aca0 | 242 | |
mgimple | 34:dd00bd18ead9 | 243 | float pw_max=2.0; |
mgimple | 34:dd00bd18ead9 | 244 | if(pw>pw_max) { |
mgimple | 34:dd00bd18ead9 | 245 | pw=pw_max; //hard limitation |
mgimple | 34:dd00bd18ead9 | 246 | } |
mgimple | 34:dd00bd18ead9 | 247 | Timer tt; |
mgimple | 34:dd00bd18ead9 | 248 | tt.reset(); |
mgimple | 34:dd00bd18ead9 | 249 | tt.start(); |
mgimple | 34:dd00bd18ead9 | 250 | // lets set the pulse width |
mgimple | 34:dd00bd18ead9 | 251 | //thruster.period(20.0/1000.00); // 20 ms period |
mgimple | 34:dd00bd18ead9 | 252 | thruster.pulsewidth(pw/1000.00); |
mgimple | 34:dd00bd18ead9 | 253 | thruster2.pulsewidth(pw/1000.00); |
mgimple | 34:dd00bd18ead9 | 254 | //PWM will be kept until time out |
mgimple | 34:dd00bd18ead9 | 255 | while(tt.read()<=on_time) { |
mgimple | 18:b5f58a274570 | 256 | |
mgimple | 34:dd00bd18ead9 | 257 | BLE.printf("thruster on?...mack is cool\r\n"); //print check |
mgimple | 34:dd00bd18ead9 | 258 | } |
mgimple | 34:dd00bd18ead9 | 259 | //stop the timer |
mgimple | 34:dd00bd18ead9 | 260 | tt.stop(); |
mgimple | 34:dd00bd18ead9 | 261 | //turn off the thruster |
mgimple | 34:dd00bd18ead9 | 262 | thruster.pulsewidth(1.0/1000.00); |
mgimple | 34:dd00bd18ead9 | 263 | thruster2.pulsewidth(1.0/1000.00); |
mgimple | 18:b5f58a274570 | 264 | |
mgimple | 34:dd00bd18ead9 | 265 | } |