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