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.
main.cpp@0:feaa05d35ccf, 2012-03-07 (annotated)
- Committer:
- AlexAllen
- Date:
- Wed Mar 07 20:02:35 2012 +0000
- Revision:
- 0:feaa05d35ccf
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| AlexAllen | 0:feaa05d35ccf | 1 | #include <fstream> |
| AlexAllen | 0:feaa05d35ccf | 2 | #include "mbed.h" |
| AlexAllen | 0:feaa05d35ccf | 3 | #include "SDFileSystem.h" |
| AlexAllen | 0:feaa05d35ccf | 4 | #include "GPS.h" |
| AlexAllen | 0:feaa05d35ccf | 5 | #include "BMP085.h" |
| AlexAllen | 0:feaa05d35ccf | 6 | #include "flash.h" |
| AlexAllen | 0:feaa05d35ccf | 7 | #include "HIH4030.h" |
| AlexAllen | 0:feaa05d35ccf | 8 | #include "UM12.h" |
| AlexAllen | 0:feaa05d35ccf | 9 | |
| AlexAllen | 0:feaa05d35ccf | 10 | const float expmin = 8000.0; |
| AlexAllen | 0:feaa05d35ccf | 11 | const float expmax = 11000.0; |
| AlexAllen | 0:feaa05d35ccf | 12 | |
| AlexAllen | 0:feaa05d35ccf | 13 | Serial pc(USBTX, USBRX); |
| AlexAllen | 0:feaa05d35ccf | 14 | |
| AlexAllen | 0:feaa05d35ccf | 15 | Timer tmr; |
| AlexAllen | 0:feaa05d35ccf | 16 | HIH4030 humSens(p15); |
| AlexAllen | 0:feaa05d35ccf | 17 | BMP085 presSens(p28, p27); |
| AlexAllen | 0:feaa05d35ccf | 18 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
| AlexAllen | 0:feaa05d35ccf | 19 | GPS gps(p13, p14); |
| AlexAllen | 0:feaa05d35ccf | 20 | UM12 radio(p9, p10); |
| AlexAllen | 0:feaa05d35ccf | 21 | DigitalInOut cam(p21); |
| AlexAllen | 0:feaa05d35ccf | 22 | DigitalOut jimShut(p19); |
| AlexAllen | 0:feaa05d35ccf | 23 | DigitalOut jimOpen(p20); |
| AlexAllen | 0:feaa05d35ccf | 24 | |
| AlexAllen | 0:feaa05d35ccf | 25 | int main(void) |
| AlexAllen | 0:feaa05d35ccf | 26 | { |
| AlexAllen | 0:feaa05d35ccf | 27 | bool lock=false, open = false; |
| AlexAllen | 0:feaa05d35ccf | 28 | float lat = 9001.0, lng = 9001.0, alt=-999.0, maxalt=-999.0, temp, pres, hum; |
| AlexAllen | 0:feaa05d35ccf | 29 | int start, sat=0, loop=0; |
| AlexAllen | 0:feaa05d35ccf | 30 | unsigned int time; |
| AlexAllen | 0:feaa05d35ccf | 31 | char str[] = "Loading"; |
| AlexAllen | 0:feaa05d35ccf | 32 | |
| AlexAllen | 0:feaa05d35ccf | 33 | // Set timer going |
| AlexAllen | 0:feaa05d35ccf | 34 | tmr.start(); |
| AlexAllen | 0:feaa05d35ccf | 35 | start = tmr.read_us(); |
| AlexAllen | 0:feaa05d35ccf | 36 | |
| AlexAllen | 0:feaa05d35ccf | 37 | // Ensure camera pin is set high |
| AlexAllen | 0:feaa05d35ccf | 38 | cam.mode(OpenDrain); |
| AlexAllen | 0:feaa05d35ccf | 39 | cam.output(); |
| AlexAllen | 0:feaa05d35ccf | 40 | cam=1; |
| AlexAllen | 0:feaa05d35ccf | 41 | |
| AlexAllen | 0:feaa05d35ccf | 42 | // Give obvious sign that program has started |
| AlexAllen | 0:feaa05d35ccf | 43 | lightup(); |
| AlexAllen | 0:feaa05d35ccf | 44 | flash(3, 0.25); |
| AlexAllen | 0:feaa05d35ccf | 45 | |
| AlexAllen | 0:feaa05d35ccf | 46 | // Ensure Jim's thing is closed |
| AlexAllen | 0:feaa05d35ccf | 47 | jimOpen = 0; |
| AlexAllen | 0:feaa05d35ccf | 48 | jimShut = 1; |
| AlexAllen | 0:feaa05d35ccf | 49 | wait(1.0); |
| AlexAllen | 0:feaa05d35ccf | 50 | jimShut = 0; |
| AlexAllen | 0:feaa05d35ccf | 51 | |
| AlexAllen | 0:feaa05d35ccf | 52 | // Set up text file for recording data on sd card |
| AlexAllen | 0:feaa05d35ccf | 53 | ofstream fout("/sd/data.txt"); |
| AlexAllen | 0:feaa05d35ccf | 54 | ofstream ferr("/sd/errors.txt"); |
| AlexAllen | 0:feaa05d35ccf | 55 | if(!fout) |
| AlexAllen | 0:feaa05d35ccf | 56 | { |
| AlexAllen | 0:feaa05d35ccf | 57 | errormsg1(0.25); |
| AlexAllen | 0:feaa05d35ccf | 58 | } |
| AlexAllen | 0:feaa05d35ccf | 59 | fout << "#Time Loop Lock Lat Long Altitude Pressure Temperature Humidity Sat" << endl; |
| AlexAllen | 0:feaa05d35ccf | 60 | fout.close(); |
| AlexAllen | 0:feaa05d35ccf | 61 | ferr.close(); |
| AlexAllen | 0:feaa05d35ccf | 62 | |
| AlexAllen | 0:feaa05d35ccf | 63 | // Wait for GPS lock |
| AlexAllen | 0:feaa05d35ccf | 64 | while(gps.lock!=1) |
| AlexAllen | 0:feaa05d35ccf | 65 | { |
| AlexAllen | 0:feaa05d35ccf | 66 | gps.sample(); |
| AlexAllen | 0:feaa05d35ccf | 67 | wait(0.1); |
| AlexAllen | 0:feaa05d35ccf | 68 | flash1(0.25); |
| AlexAllen | 0:feaa05d35ccf | 69 | radio.send('@'); |
| AlexAllen | 0:feaa05d35ccf | 70 | radio.send((int)strlen(str)); |
| AlexAllen | 0:feaa05d35ccf | 71 | for(int z=0; z<strlen(str); z++) radio.send(str[z]); |
| AlexAllen | 0:feaa05d35ccf | 72 | } |
| AlexAllen | 0:feaa05d35ccf | 73 | |
| AlexAllen | 0:feaa05d35ccf | 74 | // Give all clear signal |
| AlexAllen | 0:feaa05d35ccf | 75 | signal(0.25); |
| AlexAllen | 0:feaa05d35ccf | 76 | flash(5, 0.25); |
| AlexAllen | 0:feaa05d35ccf | 77 | |
| AlexAllen | 0:feaa05d35ccf | 78 | while(1) |
| AlexAllen | 0:feaa05d35ccf | 79 | { |
| AlexAllen | 0:feaa05d35ccf | 80 | // Get time |
| AlexAllen | 0:feaa05d35ccf | 81 | time = tmr.read_us() - start; |
| AlexAllen | 0:feaa05d35ccf | 82 | |
| AlexAllen | 0:feaa05d35ccf | 83 | // reopen file in append mode |
| AlexAllen | 0:feaa05d35ccf | 84 | ofstream out("/sd/data.txt", fstream::app ); |
| AlexAllen | 0:feaa05d35ccf | 85 | |
| AlexAllen | 0:feaa05d35ccf | 86 | gps.sample(); |
| AlexAllen | 0:feaa05d35ccf | 87 | |
| AlexAllen | 0:feaa05d35ccf | 88 | // Probe GPS |
| AlexAllen | 0:feaa05d35ccf | 89 | if(gps.lock==1) |
| AlexAllen | 0:feaa05d35ccf | 90 | { |
| AlexAllen | 0:feaa05d35ccf | 91 | lat = gps.latitude; |
| AlexAllen | 0:feaa05d35ccf | 92 | lng = gps.longitude; |
| AlexAllen | 0:feaa05d35ccf | 93 | alt = gps.altitude; |
| AlexAllen | 0:feaa05d35ccf | 94 | sat = gps.satillites; |
| AlexAllen | 0:feaa05d35ccf | 95 | lock = true; |
| AlexAllen | 0:feaa05d35ccf | 96 | if(alt>maxalt) maxalt = alt; |
| AlexAllen | 0:feaa05d35ccf | 97 | } |
| AlexAllen | 0:feaa05d35ccf | 98 | else |
| AlexAllen | 0:feaa05d35ccf | 99 | { |
| AlexAllen | 0:feaa05d35ccf | 100 | // Set up text for for recording errors on sd card |
| AlexAllen | 0:feaa05d35ccf | 101 | ofstream err("/sd/errors.txt", fstream::app); |
| AlexAllen | 0:feaa05d35ccf | 102 | err << "No GPS lock at time " << time << endl; |
| AlexAllen | 0:feaa05d35ccf | 103 | err.close(); |
| AlexAllen | 0:feaa05d35ccf | 104 | |
| AlexAllen | 0:feaa05d35ccf | 105 | lock = false; |
| AlexAllen | 0:feaa05d35ccf | 106 | } |
| AlexAllen | 0:feaa05d35ccf | 107 | |
| AlexAllen | 0:feaa05d35ccf | 108 | // Probe temperature/pressure sensor |
| AlexAllen | 0:feaa05d35ccf | 109 | presSens.update(); |
| AlexAllen | 0:feaa05d35ccf | 110 | pres = presSens.get_pressure(); |
| AlexAllen | 0:feaa05d35ccf | 111 | temp = presSens.get_temperature(); |
| AlexAllen | 0:feaa05d35ccf | 112 | |
| AlexAllen | 0:feaa05d35ccf | 113 | // Probe humidity sensor |
| AlexAllen | 0:feaa05d35ccf | 114 | hum = humSens.getH(temp); |
| AlexAllen | 0:feaa05d35ccf | 115 | |
| AlexAllen | 0:feaa05d35ccf | 116 | // Record data on SD card (endl to ensure flushing) |
| AlexAllen | 0:feaa05d35ccf | 117 | out << time << " " << loop << " " << lock << " " << lat << " " << lng << " "; |
| AlexAllen | 0:feaa05d35ccf | 118 | out << alt << " " << pres << " " << temp << " " << hum << " " << sat << endl; |
| AlexAllen | 0:feaa05d35ccf | 119 | out.close(); |
| AlexAllen | 0:feaa05d35ccf | 120 | |
| AlexAllen | 0:feaa05d35ccf | 121 | // Decide whether or not to take a picture |
| AlexAllen | 0:feaa05d35ccf | 122 | if(!(loop%5)) |
| AlexAllen | 0:feaa05d35ccf | 123 | { |
| AlexAllen | 0:feaa05d35ccf | 124 | cam = 0; |
| AlexAllen | 0:feaa05d35ccf | 125 | } |
| AlexAllen | 0:feaa05d35ccf | 126 | if(!((loop-2)%5)) |
| AlexAllen | 0:feaa05d35ccf | 127 | { |
| AlexAllen | 0:feaa05d35ccf | 128 | cam = 1; |
| AlexAllen | 0:feaa05d35ccf | 129 | } |
| AlexAllen | 0:feaa05d35ccf | 130 | |
| AlexAllen | 0:feaa05d35ccf | 131 | // Decide if Jim's thing should be open or closed and make it so |
| AlexAllen | 0:feaa05d35ccf | 132 | if(alt > expmin && alt < expmax && ((alt+3000)>maxalt)) |
| AlexAllen | 0:feaa05d35ccf | 133 | { |
| AlexAllen | 0:feaa05d35ccf | 134 | jimShut = 0; |
| AlexAllen | 0:feaa05d35ccf | 135 | jimOpen = 1; |
| AlexAllen | 0:feaa05d35ccf | 136 | if(open) wait(0.1); |
| AlexAllen | 0:feaa05d35ccf | 137 | else wait(1.0); |
| AlexAllen | 0:feaa05d35ccf | 138 | jimOpen = 0; |
| AlexAllen | 0:feaa05d35ccf | 139 | open = true; |
| AlexAllen | 0:feaa05d35ccf | 140 | } |
| AlexAllen | 0:feaa05d35ccf | 141 | else |
| AlexAllen | 0:feaa05d35ccf | 142 | { |
| AlexAllen | 0:feaa05d35ccf | 143 | jimOpen = 0; |
| AlexAllen | 0:feaa05d35ccf | 144 | jimShut = 1; |
| AlexAllen | 0:feaa05d35ccf | 145 | if(!open) wait(0.1); |
| AlexAllen | 0:feaa05d35ccf | 146 | else wait(1.0); |
| AlexAllen | 0:feaa05d35ccf | 147 | jimShut = 0; |
| AlexAllen | 0:feaa05d35ccf | 148 | open = false; |
| AlexAllen | 0:feaa05d35ccf | 149 | } |
| AlexAllen | 0:feaa05d35ccf | 150 | |
| AlexAllen | 0:feaa05d35ccf | 151 | // Send location over radio in the form "$<latitude>,<longitude>,<altitude>?" |
| AlexAllen | 0:feaa05d35ccf | 152 | radio.send('$'); |
| AlexAllen | 0:feaa05d35ccf | 153 | radio.send(lat); |
| AlexAllen | 0:feaa05d35ccf | 154 | radio.send(','); |
| AlexAllen | 0:feaa05d35ccf | 155 | radio.send(lng); |
| AlexAllen | 0:feaa05d35ccf | 156 | radio.send(','); |
| AlexAllen | 0:feaa05d35ccf | 157 | radio.send(alt); |
| AlexAllen | 0:feaa05d35ccf | 158 | radio.send(','); |
| AlexAllen | 0:feaa05d35ccf | 159 | radio.send(lock); |
| AlexAllen | 0:feaa05d35ccf | 160 | radio.send('?'); |
| AlexAllen | 0:feaa05d35ccf | 161 | |
| AlexAllen | 0:feaa05d35ccf | 162 | loop++; |
| AlexAllen | 0:feaa05d35ccf | 163 | } |
| AlexAllen | 0:feaa05d35ccf | 164 | |
| AlexAllen | 0:feaa05d35ccf | 165 | return 0; |
| AlexAllen | 0:feaa05d35ccf | 166 | } |