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