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@5:17f2b3e61112, 2021-11-30 (annotated)
- Committer:
- mgimple
- Date:
- Tue Nov 30 15:08:01 2021 +0000
- Revision:
- 5:17f2b3e61112
- Parent:
- 4:d87b1e40d419
- Child:
- 6:5f55105701ac
defining global variable "on_time"; pull/push
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elizabethtaylor | 2:a3b55216ffbd | 1 | //Eliza |
mgimple | 3:22a86bc49f44 | 2 | //Megan |
slicht_instructor | 0:6e97f9a6aca0 | 3 | /* |
slicht_instructor | 0:6e97f9a6aca0 | 4 | Author Mingxi Zhou |
slicht_instructor | 0:6e97f9a6aca0 | 5 | OCE360 underwater float template program |
slicht_instructor | 0:6e97f9a6aca0 | 6 | */ |
slicht_instructor | 0:6e97f9a6aca0 | 7 | #include "mbed.h" |
slicht_instructor | 0:6e97f9a6aca0 | 8 | #include "MMA8452Q.h" //accelerometer library |
slicht_instructor | 0:6e97f9a6aca0 | 9 | #include "MS5837.h" //pressure sensor library*** |
mgimple | 1:e1a2b47c3098 | 10 | #include "SCI_SENSOR.h" //science sensor library, photocell, temperature cell |
slicht_instructor | 0:6e97f9a6aca0 | 11 | #include "SDFileSystem.h" // SD card library |
slicht_instructor | 0:6e97f9a6aca0 | 12 | |
slicht_instructor | 0:6e97f9a6aca0 | 13 | DigitalOut led1(LED1); |
slicht_instructor | 0:6e97f9a6aca0 | 14 | DigitalOut led2(LED2); |
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; |
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 |
slicht_instructor | 0:6e97f9a6aca0 | 43 | |
slicht_instructor | 0:6e97f9a6aca0 | 44 | //functions |
slicht_instructor | 0:6e97f9a6aca0 | 45 | void welcome(); |
slicht_instructor | 0:6e97f9a6aca0 | 46 | void log_data(); |
mgimple | 4:d87b1e40d419 | 47 | |
slicht_instructor | 0:6e97f9a6aca0 | 48 | //IMU related |
slicht_instructor | 0:6e97f9a6aca0 | 49 | void accel_update(); //update accelerometer related variables. we use imu_ticker to call this function |
mgimple | 5:17f2b3e61112 | 50 | |
slicht_instructor | 0:6e97f9a6aca0 | 51 | //Control related functions |
slicht_instructor | 0:6e97f9a6aca0 | 52 | void thrust_on(float pw, float on_time); //input is pulse width |
slicht_instructor | 0:6e97f9a6aca0 | 53 | |
mgimple | 5:17f2b3e61112 | 54 | //Control Parameters |
mgimple | 5:17f2b3e61112 | 55 | float on_time = 5; //on for 5 seconds part one |
mgimple | 5:17f2b3e61112 | 56 | |
slicht_instructor | 0:6e97f9a6aca0 | 57 | //-------------Main functions----------------------------------------------------------------------------------------- |
slicht_instructor | 0:6e97f9a6aca0 | 58 | int main() |
slicht_instructor | 0:6e97f9a6aca0 | 59 | { |
slicht_instructor | 0:6e97f9a6aca0 | 60 | //-----Initialization realted code-------// |
slicht_instructor | 0:6e97f9a6aca0 | 61 | //inital set the thruster esc to 1ms duty cycle |
slicht_instructor | 0:6e97f9a6aca0 | 62 | thruster.period(0.002); // 2 ms period |
slicht_instructor | 0:6e97f9a6aca0 | 63 | thruster.pulsewidth(1.0/1000.000); |
mgimple | 4:d87b1e40d419 | 64 | |
slicht_instructor | 0:6e97f9a6aca0 | 65 | //Initialize accelerometer: |
slicht_instructor | 0:6e97f9a6aca0 | 66 | accel.init(); |
slicht_instructor | 0:6e97f9a6aca0 | 67 | led1=1; |
mgimple | 4:d87b1e40d419 | 68 | |
slicht_instructor | 0:6e97f9a6aca0 | 69 | //initialize pressure sensor |
slicht_instructor | 0:6e97f9a6aca0 | 70 | pc.printf("setting the pressure sensor\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 71 | p_sensor.MS5837Reset(); |
slicht_instructor | 0:6e97f9a6aca0 | 72 | p_sensor.MS5837Init(); |
slicht_instructor | 0:6e97f9a6aca0 | 73 | pc.printf("settting the tickers\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 74 | t.start(); |
slicht_instructor | 0:6e97f9a6aca0 | 75 | led2=1; |
slicht_instructor | 0:6e97f9a6aca0 | 76 | welcome(); |
mgimple | 4:d87b1e40d419 | 77 | |
slicht_instructor | 0:6e97f9a6aca0 | 78 | //-----setup ticker-------// |
slicht_instructor | 0:6e97f9a6aca0 | 79 | //setup ticker to separate log and IMU data update. |
slicht_instructor | 0:6e97f9a6aca0 | 80 | //so we could have all our control code in the while loop |
slicht_instructor | 0:6e97f9a6aca0 | 81 | // //log at 2 Hz |
slicht_instructor | 0:6e97f9a6aca0 | 82 | accel_ticker.attach(&accel_update,0.1); //10Hz |
slicht_instructor | 0:6e97f9a6aca0 | 83 | log_ticker.attach(&log_data,0.5); |
slicht_instructor | 0:6e97f9a6aca0 | 84 | wait(1); |
slicht_instructor | 0:6e97f9a6aca0 | 85 | while(1) |
slicht_instructor | 0:6e97f9a6aca0 | 86 | { |
mgimple | 5:17f2b3e61112 | 87 | |
slicht_instructor | 0:6e97f9a6aca0 | 88 | // put your main control code here |
slicht_instructor | 0:6e97f9a6aca0 | 89 | } |
slicht_instructor | 0:6e97f9a6aca0 | 90 | |
slicht_instructor | 0:6e97f9a6aca0 | 91 | } |
slicht_instructor | 0:6e97f9a6aca0 | 92 | |
slicht_instructor | 0:6e97f9a6aca0 | 93 | //-------------Customized functions---------------------------------------------//---------------------------------------- |
slicht_instructor | 0:6e97f9a6aca0 | 94 | ///-----------Welcome menu---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 95 | void welcome() |
slicht_instructor | 0:6e97f9a6aca0 | 96 | { |
slicht_instructor | 0:6e97f9a6aca0 | 97 | char buffer[100]={0}; |
slicht_instructor | 0:6e97f9a6aca0 | 98 | int flag=1; |
slicht_instructor | 0:6e97f9a6aca0 | 99 | //Flush the port |
slicht_instructor | 0:6e97f9a6aca0 | 100 | while(BLE.readable()) |
slicht_instructor | 0:6e97f9a6aca0 | 101 | { |
slicht_instructor | 0:6e97f9a6aca0 | 102 | BLE.getc(); |
slicht_instructor | 0:6e97f9a6aca0 | 103 | } |
slicht_instructor | 0:6e97f9a6aca0 | 104 | while(flag) |
slicht_instructor | 0:6e97f9a6aca0 | 105 | { |
slicht_instructor | 0:6e97f9a6aca0 | 106 | BLE.printf("### I am alive\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 107 | BLE.printf("### Please enter the log file name you want\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 108 | if(BLE.readable()) |
slicht_instructor | 0:6e97f9a6aca0 | 109 | { |
slicht_instructor | 0:6e97f9a6aca0 | 110 | BLE.scanf("%s",buffer); |
slicht_instructor | 0:6e97f9a6aca0 | 111 | sprintf(fname,"/sd/mydir/%s.txt",buffer); //make file name |
slicht_instructor | 0:6e97f9a6aca0 | 112 | |
slicht_instructor | 0:6e97f9a6aca0 | 113 | flag = 0; //set the flag to 0 to break the while |
slicht_instructor | 0:6e97f9a6aca0 | 114 | } |
slicht_instructor | 0:6e97f9a6aca0 | 115 | wait(1); |
slicht_instructor | 0:6e97f9a6aca0 | 116 | } |
slicht_instructor | 0:6e97f9a6aca0 | 117 | //print name |
slicht_instructor | 0:6e97f9a6aca0 | 118 | BLE.printf("### name received\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 119 | BLE.printf("### file name and directory is: \r\n %s\r\n",fname); //file name and location |
slicht_instructor | 0:6e97f9a6aca0 | 120 | //open file test |
slicht_instructor | 0:6e97f9a6aca0 | 121 | mkdir("/sd/mydir",0777); //keep 0777, this is magic # |
slicht_instructor | 0:6e97f9a6aca0 | 122 | fp = fopen(fname, "a"); |
slicht_instructor | 0:6e97f9a6aca0 | 123 | if(fp == NULL){ |
slicht_instructor | 0:6e97f9a6aca0 | 124 | BLE.printf("Could not open file for write\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 125 | } |
slicht_instructor | 0:6e97f9a6aca0 | 126 | else |
slicht_instructor | 0:6e97f9a6aca0 | 127 | { |
slicht_instructor | 0:6e97f9a6aca0 | 128 | BLE.printf("##file open good \n"); //open file and tell if open |
slicht_instructor | 0:6e97f9a6aca0 | 129 | fprintf(fp, "Hello\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 130 | fclose(fp); |
slicht_instructor | 0:6e97f9a6aca0 | 131 | } |
slicht_instructor | 0:6e97f9a6aca0 | 132 | |
slicht_instructor | 0:6e97f9a6aca0 | 133 | BLE.printf("### The main program will start in 10 seconds\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 134 | wait(5); |
slicht_instructor | 0:6e97f9a6aca0 | 135 | } |
slicht_instructor | 0:6e97f9a6aca0 | 136 | |
slicht_instructor | 0:6e97f9a6aca0 | 137 | ///-----------log functions---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 138 | void log_data() |
slicht_instructor | 0:6e97f9a6aca0 | 139 | { |
slicht_instructor | 0:6e97f9a6aca0 | 140 | //log system time t.read() |
slicht_instructor | 0:6e97f9a6aca0 | 141 | // log imu data, log science data |
slicht_instructor | 0:6e97f9a6aca0 | 142 | // log pulse width |
slicht_instructor | 0:6e97f9a6aca0 | 143 | // log pressure sensor data. |
slicht_instructor | 0:6e97f9a6aca0 | 144 | //science sensor: temp.temp(), light.light() |
slicht_instructor | 0:6e97f9a6aca0 | 145 | //IMU sensor |
slicht_instructor | 0:6e97f9a6aca0 | 146 | |
slicht_instructor | 0:6e97f9a6aca0 | 147 | } |
slicht_instructor | 0:6e97f9a6aca0 | 148 | |
slicht_instructor | 0:6e97f9a6aca0 | 149 | ///-----------acceleromter related functions---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 150 | void accel_update() |
slicht_instructor | 0:6e97f9a6aca0 | 151 | { |
slicht_instructor | 0:6e97f9a6aca0 | 152 | accelData[0] = accel.readX(); |
slicht_instructor | 0:6e97f9a6aca0 | 153 | accelData[1] = accel.readY(); |
slicht_instructor | 0:6e97f9a6aca0 | 154 | accelData[2] = accel.readZ(); |
slicht_instructor | 0:6e97f9a6aca0 | 155 | } |
slicht_instructor | 0:6e97f9a6aca0 | 156 | |
slicht_instructor | 0:6e97f9a6aca0 | 157 | ///-----------Control related functions---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 158 | ////Thruster on control, pw->pulse width in milli-second// |
slicht_instructor | 0:6e97f9a6aca0 | 159 | //// pw range between 1 to 1.5// |
slicht_instructor | 0:6e97f9a6aca0 | 160 | //// on_time-> thruster on time. |
slicht_instructor | 0:6e97f9a6aca0 | 161 | void thrust_on(float pw, float on_time) //input is pulse width |
slicht_instructor | 0:6e97f9a6aca0 | 162 | { |
slicht_instructor | 0:6e97f9a6aca0 | 163 | float pw_max=2.0; |
slicht_instructor | 0:6e97f9a6aca0 | 164 | if(pw>pw_max) |
slicht_instructor | 0:6e97f9a6aca0 | 165 | { |
slicht_instructor | 0:6e97f9a6aca0 | 166 | pw=pw_max; //hard limitation |
slicht_instructor | 0:6e97f9a6aca0 | 167 | } |
slicht_instructor | 0:6e97f9a6aca0 | 168 | Timer tt; |
slicht_instructor | 0:6e97f9a6aca0 | 169 | tt.reset(); |
slicht_instructor | 0:6e97f9a6aca0 | 170 | tt.start(); |
slicht_instructor | 0:6e97f9a6aca0 | 171 | // lets set the pulse width |
slicht_instructor | 0:6e97f9a6aca0 | 172 | //thruster.period(20.0/1000.00); // 20 ms period |
slicht_instructor | 0:6e97f9a6aca0 | 173 | thruster.pulsewidth(pw/1000.00); |
slicht_instructor | 0:6e97f9a6aca0 | 174 | thruster2.pulsewidth(pw/1000.00); |
slicht_instructor | 0:6e97f9a6aca0 | 175 | //PWM will be kept until time out |
slicht_instructor | 0:6e97f9a6aca0 | 176 | while(tt.read()<=on_time) |
slicht_instructor | 0:6e97f9a6aca0 | 177 | { |
slicht_instructor | 0:6e97f9a6aca0 | 178 | } |
slicht_instructor | 0:6e97f9a6aca0 | 179 | //stop the timer |
slicht_instructor | 0:6e97f9a6aca0 | 180 | tt.stop(); |
slicht_instructor | 0:6e97f9a6aca0 | 181 | //turn off the thruster |
slicht_instructor | 0:6e97f9a6aca0 | 182 | thruster.pulsewidth(1.0/1000.00); |
slicht_instructor | 0:6e97f9a6aca0 | 183 | thruster2.pulsewidth(1.0/1000.00); |
slicht_instructor | 0:6e97f9a6aca0 | 184 | |
slicht_instructor | 0:6e97f9a6aca0 | 185 | } |
slicht_instructor | 0:6e97f9a6aca0 | 186 |