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@10:b2fe6d1dee81, 2021-12-02 (annotated)
- Committer:
- mgimple
- Date:
- Thu Dec 02 14:29:55 2021 +0000
- Revision:
- 10:b2fe6d1dee81
- Parent:
- 9:e4e1d5db0c04
- Child:
- 12:54b498af39df
- Child:
- 14:ed172dec4022
Thruster on :complies :)
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 | |
mgimple | 9:e4e1d5db0c04 | 51 | //Control Parameters |
mgimple | 9:e4e1d5db0c04 | 52 | float tim =5; //define thruster on time |
mgimple | 9:e4e1d5db0c04 | 53 | float percent = 1.2; //user defined percent of thrust power |
mgimple | 9:e4e1d5db0c04 | 54 | |
slicht_instructor | 0:6e97f9a6aca0 | 55 | //Control related functions |
slicht_instructor | 0:6e97f9a6aca0 | 56 | void thrust_on(float pw, float on_time); //input is pulse width |
mgimple | 10:b2fe6d1dee81 | 57 | //void thrust_off(float pw, float on_time); // turn off pulse width |
mgimple | 9:e4e1d5db0c04 | 58 | |
mgimple | 5:17f2b3e61112 | 59 | |
slicht_instructor | 0:6e97f9a6aca0 | 60 | //-------------Main functions----------------------------------------------------------------------------------------- |
slicht_instructor | 0:6e97f9a6aca0 | 61 | int main() |
slicht_instructor | 0:6e97f9a6aca0 | 62 | { |
slicht_instructor | 0:6e97f9a6aca0 | 63 | //-----Initialization realted code-------// |
slicht_instructor | 0:6e97f9a6aca0 | 64 | //inital set the thruster esc to 1ms duty cycle |
slicht_instructor | 0:6e97f9a6aca0 | 65 | thruster.period(0.002); // 2 ms period |
slicht_instructor | 0:6e97f9a6aca0 | 66 | thruster.pulsewidth(1.0/1000.000); |
mgimple | 4:d87b1e40d419 | 67 | |
slicht_instructor | 0:6e97f9a6aca0 | 68 | //Initialize accelerometer: |
slicht_instructor | 0:6e97f9a6aca0 | 69 | accel.init(); |
slicht_instructor | 0:6e97f9a6aca0 | 70 | led1=1; |
mgimple | 4:d87b1e40d419 | 71 | |
slicht_instructor | 0:6e97f9a6aca0 | 72 | //initialize pressure sensor |
slicht_instructor | 0:6e97f9a6aca0 | 73 | pc.printf("setting the pressure sensor\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 74 | p_sensor.MS5837Reset(); |
slicht_instructor | 0:6e97f9a6aca0 | 75 | p_sensor.MS5837Init(); |
mgimple | 6:5f55105701ac | 76 | pc.printf("setting the tickers\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 77 | t.start(); |
slicht_instructor | 0:6e97f9a6aca0 | 78 | led2=1; |
slicht_instructor | 0:6e97f9a6aca0 | 79 | welcome(); |
mgimple | 4:d87b1e40d419 | 80 | |
slicht_instructor | 0:6e97f9a6aca0 | 81 | //-----setup ticker-------// |
slicht_instructor | 0:6e97f9a6aca0 | 82 | //setup ticker to separate log and IMU data update. |
slicht_instructor | 0:6e97f9a6aca0 | 83 | //so we could have all our control code in the while loop |
slicht_instructor | 0:6e97f9a6aca0 | 84 | // //log at 2 Hz |
slicht_instructor | 0:6e97f9a6aca0 | 85 | accel_ticker.attach(&accel_update,0.1); //10Hz |
slicht_instructor | 0:6e97f9a6aca0 | 86 | log_ticker.attach(&log_data,0.5); |
slicht_instructor | 0:6e97f9a6aca0 | 87 | wait(1); |
mgimple | 9:e4e1d5db0c04 | 88 | |
slicht_instructor | 0:6e97f9a6aca0 | 89 | while(1) |
slicht_instructor | 0:6e97f9a6aca0 | 90 | { |
mgimple | 10:b2fe6d1dee81 | 91 | float pw= percent; |
mgimple | 10:b2fe6d1dee81 | 92 | float on_time =tim; |
mgimple | 10:b2fe6d1dee81 | 93 | thrust_on(pw, on_time); //turn thruster on for 5 seconds |
mgimple | 10:b2fe6d1dee81 | 94 | return 0; |
slicht_instructor | 0:6e97f9a6aca0 | 95 | } |
slicht_instructor | 0:6e97f9a6aca0 | 96 | |
slicht_instructor | 0:6e97f9a6aca0 | 97 | } |
slicht_instructor | 0:6e97f9a6aca0 | 98 | |
slicht_instructor | 0:6e97f9a6aca0 | 99 | //-------------Customized functions---------------------------------------------//---------------------------------------- |
slicht_instructor | 0:6e97f9a6aca0 | 100 | ///-----------Welcome menu---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 101 | void welcome() |
slicht_instructor | 0:6e97f9a6aca0 | 102 | { |
slicht_instructor | 0:6e97f9a6aca0 | 103 | char buffer[100]={0}; |
slicht_instructor | 0:6e97f9a6aca0 | 104 | int flag=1; |
slicht_instructor | 0:6e97f9a6aca0 | 105 | //Flush the port |
slicht_instructor | 0:6e97f9a6aca0 | 106 | while(BLE.readable()) |
slicht_instructor | 0:6e97f9a6aca0 | 107 | { |
slicht_instructor | 0:6e97f9a6aca0 | 108 | BLE.getc(); |
slicht_instructor | 0:6e97f9a6aca0 | 109 | } |
slicht_instructor | 0:6e97f9a6aca0 | 110 | while(flag) |
slicht_instructor | 0:6e97f9a6aca0 | 111 | { |
slicht_instructor | 0:6e97f9a6aca0 | 112 | BLE.printf("### I am alive\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 113 | BLE.printf("### Please enter the log file name you want\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 114 | if(BLE.readable()) |
slicht_instructor | 0:6e97f9a6aca0 | 115 | { |
slicht_instructor | 0:6e97f9a6aca0 | 116 | BLE.scanf("%s",buffer); |
slicht_instructor | 0:6e97f9a6aca0 | 117 | sprintf(fname,"/sd/mydir/%s.txt",buffer); //make file name |
slicht_instructor | 0:6e97f9a6aca0 | 118 | |
slicht_instructor | 0:6e97f9a6aca0 | 119 | flag = 0; //set the flag to 0 to break the while |
slicht_instructor | 0:6e97f9a6aca0 | 120 | } |
slicht_instructor | 0:6e97f9a6aca0 | 121 | wait(1); |
slicht_instructor | 0:6e97f9a6aca0 | 122 | } |
slicht_instructor | 0:6e97f9a6aca0 | 123 | //print name |
slicht_instructor | 0:6e97f9a6aca0 | 124 | BLE.printf("### name received\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 125 | BLE.printf("### file name and directory is: \r\n %s\r\n",fname); //file name and location |
slicht_instructor | 0:6e97f9a6aca0 | 126 | //open file test |
slicht_instructor | 0:6e97f9a6aca0 | 127 | mkdir("/sd/mydir",0777); //keep 0777, this is magic # |
slicht_instructor | 0:6e97f9a6aca0 | 128 | fp = fopen(fname, "a"); |
slicht_instructor | 0:6e97f9a6aca0 | 129 | if(fp == NULL){ |
slicht_instructor | 0:6e97f9a6aca0 | 130 | BLE.printf("Could not open file for write\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 131 | } |
slicht_instructor | 0:6e97f9a6aca0 | 132 | else |
slicht_instructor | 0:6e97f9a6aca0 | 133 | { |
slicht_instructor | 0:6e97f9a6aca0 | 134 | BLE.printf("##file open good \n"); //open file and tell if open |
slicht_instructor | 0:6e97f9a6aca0 | 135 | fprintf(fp, "Hello\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 136 | fclose(fp); |
slicht_instructor | 0:6e97f9a6aca0 | 137 | } |
slicht_instructor | 0:6e97f9a6aca0 | 138 | |
slicht_instructor | 0:6e97f9a6aca0 | 139 | BLE.printf("### The main program will start in 10 seconds\r\n"); |
slicht_instructor | 0:6e97f9a6aca0 | 140 | wait(5); |
slicht_instructor | 0:6e97f9a6aca0 | 141 | } |
slicht_instructor | 0:6e97f9a6aca0 | 142 | |
slicht_instructor | 0:6e97f9a6aca0 | 143 | ///-----------log functions---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 144 | void log_data() |
slicht_instructor | 0:6e97f9a6aca0 | 145 | { |
slicht_instructor | 0:6e97f9a6aca0 | 146 | //log system time t.read() |
slicht_instructor | 0:6e97f9a6aca0 | 147 | // log imu data, log science data |
slicht_instructor | 0:6e97f9a6aca0 | 148 | // log pulse width |
slicht_instructor | 0:6e97f9a6aca0 | 149 | // log pressure sensor data. |
slicht_instructor | 0:6e97f9a6aca0 | 150 | //science sensor: temp.temp(), light.light() |
slicht_instructor | 0:6e97f9a6aca0 | 151 | //IMU sensor |
slicht_instructor | 0:6e97f9a6aca0 | 152 | |
slicht_instructor | 0:6e97f9a6aca0 | 153 | } |
slicht_instructor | 0:6e97f9a6aca0 | 154 | |
slicht_instructor | 0:6e97f9a6aca0 | 155 | ///-----------acceleromter related functions---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 156 | void accel_update() |
slicht_instructor | 0:6e97f9a6aca0 | 157 | { |
slicht_instructor | 0:6e97f9a6aca0 | 158 | accelData[0] = accel.readX(); |
slicht_instructor | 0:6e97f9a6aca0 | 159 | accelData[1] = accel.readY(); |
slicht_instructor | 0:6e97f9a6aca0 | 160 | accelData[2] = accel.readZ(); |
slicht_instructor | 0:6e97f9a6aca0 | 161 | } |
slicht_instructor | 0:6e97f9a6aca0 | 162 | |
slicht_instructor | 0:6e97f9a6aca0 | 163 | ///-----------Control related functions---------------------/// |
slicht_instructor | 0:6e97f9a6aca0 | 164 | ////Thruster on control, pw->pulse width in milli-second// |
slicht_instructor | 0:6e97f9a6aca0 | 165 | //// pw range between 1 to 1.5// |
slicht_instructor | 0:6e97f9a6aca0 | 166 | //// on_time-> thruster on time. |
slicht_instructor | 0:6e97f9a6aca0 | 167 | void thrust_on(float pw, float on_time) //input is pulse width |
slicht_instructor | 0:6e97f9a6aca0 | 168 | { |
mgimple | 10:b2fe6d1dee81 | 169 | |
slicht_instructor | 0:6e97f9a6aca0 | 170 | float pw_max=2.0; |
slicht_instructor | 0:6e97f9a6aca0 | 171 | if(pw>pw_max) |
slicht_instructor | 0:6e97f9a6aca0 | 172 | { |
slicht_instructor | 0:6e97f9a6aca0 | 173 | pw=pw_max; //hard limitation |
slicht_instructor | 0:6e97f9a6aca0 | 174 | } |
slicht_instructor | 0:6e97f9a6aca0 | 175 | Timer tt; |
slicht_instructor | 0:6e97f9a6aca0 | 176 | tt.reset(); |
slicht_instructor | 0:6e97f9a6aca0 | 177 | tt.start(); |
slicht_instructor | 0:6e97f9a6aca0 | 178 | // lets set the pulse width |
slicht_instructor | 0:6e97f9a6aca0 | 179 | //thruster.period(20.0/1000.00); // 20 ms period |
slicht_instructor | 0:6e97f9a6aca0 | 180 | thruster.pulsewidth(pw/1000.00); |
slicht_instructor | 0:6e97f9a6aca0 | 181 | thruster2.pulsewidth(pw/1000.00); |
slicht_instructor | 0:6e97f9a6aca0 | 182 | //PWM will be kept until time out |
slicht_instructor | 0:6e97f9a6aca0 | 183 | while(tt.read()<=on_time) |
slicht_instructor | 0:6e97f9a6aca0 | 184 | { |
mgimple | 10:b2fe6d1dee81 | 185 | |
mgimple | 6:5f55105701ac | 186 | pc.printf("thruster on?...mack is cool\r\n"); //print check |
slicht_instructor | 0:6e97f9a6aca0 | 187 | } |
slicht_instructor | 0:6e97f9a6aca0 | 188 | //stop the timer |
slicht_instructor | 0:6e97f9a6aca0 | 189 | tt.stop(); |
slicht_instructor | 0:6e97f9a6aca0 | 190 | //turn off the thruster |
slicht_instructor | 0:6e97f9a6aca0 | 191 | thruster.pulsewidth(1.0/1000.00); |
slicht_instructor | 0:6e97f9a6aca0 | 192 | thruster2.pulsewidth(1.0/1000.00); |
slicht_instructor | 0:6e97f9a6aca0 | 193 | |
slicht_instructor | 0:6e97f9a6aca0 | 194 | } |
slicht_instructor | 0:6e97f9a6aca0 | 195 | |
mgimple | 10:b2fe6d1dee81 | 196 | ///void thrust_off(float pw, float on_time) //input is pulse width |
mgimple | 10:b2fe6d1dee81 | 197 | // |