all working version 2.0
Dependencies: ADS1115 BME280 CronoDot SDFileSystem mbed
Fork of Outdoor_UPAS_v1_2_powerfunction by
main.cpp@4:5d004fd997d5, 2016-02-26 (annotated)
- Committer:
- caseyquinn
- Date:
- Fri Feb 26 22:40:31 2016 +0000
- Revision:
- 4:5d004fd997d5
- Parent:
- 3:122bfc998c4c
- Child:
- 5:c3252e5d45ca
Initial UPAS Tboard code. Currently has i2c, STRTC, and SDcard all working. Still need to figure out/add in RN4677, Ublox GPS, and hbridges
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jelord | 0:2cb2b2ea316f | 1 | #include "mbed.h" |
jelord | 2:88fcbfadec6a | 2 | #include "SDFileSystem.h" |
jelord | 2:88fcbfadec6a | 3 | #include "Adafruit_ADS1015.h" |
jelord | 2:88fcbfadec6a | 4 | #include "MCP40D17.h" |
jelord | 2:88fcbfadec6a | 5 | #include "STC3100.h" |
jelord | 2:88fcbfadec6a | 6 | #include "LSM303.h" |
jelord | 2:88fcbfadec6a | 7 | #include "BME280.h" |
jelord | 2:88fcbfadec6a | 8 | #include "SI1145.h" |
jelord | 2:88fcbfadec6a | 9 | #include "NCP5623BMUTBG.h" |
jelord | 2:88fcbfadec6a | 10 | #include "CronoDot.h" |
jelord | 2:88fcbfadec6a | 11 | #include "EEPROM.h" |
jelord | 2:88fcbfadec6a | 12 | #include "Calibration.h" |
caseyquinn | 4:5d004fd997d5 | 13 | //Serial pc(USBTX, USBRX); |
caseyquinn | 4:5d004fd997d5 | 14 | //Serial microChannel(D0, D1); // tx, rx |
jelord | 1:9fbb5b665068 | 15 | Timer t; |
jelord | 1:9fbb5b665068 | 16 | struct tm tt; |
jelord | 0:2cb2b2ea316f | 17 | |
jelord | 2:88fcbfadec6a | 18 | I2C i2c(D14, D15); |
jelord | 2:88fcbfadec6a | 19 | Adafruit_ADS1115 ads(&i2c); |
jelord | 2:88fcbfadec6a | 20 | MCP40D17 DigPot(&i2c); |
jelord | 2:88fcbfadec6a | 21 | BME280 bmesensor(D14, D15); |
jelord | 2:88fcbfadec6a | 22 | STC3100 gasG(D14, D15); |
caseyquinn | 4:5d004fd997d5 | 23 | DigitalOut blower(D8, 0); |
caseyquinn | 4:5d004fd997d5 | 24 | DigitalOut pbKill(PC_12, 1); |
caseyquinn | 4:5d004fd997d5 | 25 | DigitalOut bleRTS(PB_14, 0); |
caseyquinn | 4:5d004fd997d5 | 26 | DigitalOut bleCTS(PB_13, 0); |
jelord | 2:88fcbfadec6a | 27 | LSM303 movementsensor(D14, D15); |
jelord | 2:88fcbfadec6a | 28 | SI1145 lightsensor(D14, D15); |
jelord | 2:88fcbfadec6a | 29 | NCP5623BMUTBG RGB_LED(D14, D15); |
jelord | 3:122bfc998c4c | 30 | CronoDot RTC_UPAS(D14, D15); |
jelord | 2:88fcbfadec6a | 31 | EEPROM E2PROM(D14, D15); |
jelord | 2:88fcbfadec6a | 32 | //DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications |
jelord | 2:88fcbfadec6a | 33 | Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option |
jelord | 0:2cb2b2ea316f | 34 | |
jelord | 3:122bfc998c4c | 35 | Timeout stop; //This is the stop call back object |
jelord | 3:122bfc998c4c | 36 | Timeout logg; //This is the logging call back object |
jelord | 3:122bfc998c4c | 37 | |
jelord | 3:122bfc998c4c | 38 | uint16_t serial_num = 1; // Default serial/calibration number |
jelord | 3:122bfc998c4c | 39 | int RunReady =0; |
jelord | 3:122bfc998c4c | 40 | |
caseyquinn | 4:5d004fd997d5 | 41 | struct tm STtime; |
caseyquinn | 4:5d004fd997d5 | 42 | |
caseyquinn | 4:5d004fd997d5 | 43 | |
jelord | 3:122bfc998c4c | 44 | |
jelord | 3:122bfc998c4c | 45 | float press = 1.1; |
jelord | 3:122bfc998c4c | 46 | float temp = 75.5; |
jelord | 3:122bfc998c4c | 47 | float rh = 12.1; |
jelord | 3:122bfc998c4c | 48 | |
jelord | 3:122bfc998c4c | 49 | int uv = 8; |
jelord | 3:122bfc998c4c | 50 | int vis = 7; |
jelord | 3:122bfc998c4c | 51 | int ir = 6; |
jelord | 3:122bfc998c4c | 52 | |
jelord | 3:122bfc998c4c | 53 | float compass = 0.1; |
jelord | 3:122bfc998c4c | 54 | float accel_x = 0.2; |
jelord | 3:122bfc998c4c | 55 | float accel_y = 0.3; |
jelord | 3:122bfc998c4c | 56 | float accel_z = 0.4; |
jelord | 3:122bfc998c4c | 57 | float accel_comp = 0.5; |
jelord | 3:122bfc998c4c | 58 | float angle_x = 0.6; |
jelord | 3:122bfc998c4c | 59 | float angle_y = 0.7; |
jelord | 3:122bfc998c4c | 60 | float angle_z = 0.8; |
jelord | 3:122bfc998c4c | 61 | float mag_x = 0.9; |
jelord | 3:122bfc998c4c | 62 | float mag_y = 1.0; |
jelord | 3:122bfc998c4c | 63 | float mag_z = 1.2; |
jelord | 3:122bfc998c4c | 64 | |
jelord | 3:122bfc998c4c | 65 | int vInReading = 18; |
jelord | 3:122bfc998c4c | 66 | int vBlowerReading = 19; |
jelord | 3:122bfc998c4c | 67 | int omronDiff = 20; |
jelord | 3:122bfc998c4c | 68 | float omronVolt = 1.2; //V |
jelord | 3:122bfc998c4c | 69 | int omronReading = 100; |
jelord | 3:122bfc998c4c | 70 | float atmoRho = 1.5; //g/L |
jelord | 3:122bfc998c4c | 71 | |
jelord | 3:122bfc998c4c | 72 | float massflow = 87.6; //g/min |
jelord | 3:122bfc998c4c | 73 | float volflow = 88.8; //L/min |
jelord | 3:122bfc998c4c | 74 | float volflowSet = 1.0; //L/min |
jelord | 3:122bfc998c4c | 75 | int logInerval = 10; //seconds |
jelord | 3:122bfc998c4c | 76 | double secondsD = 0; |
jelord | 3:122bfc998c4c | 77 | double lastsecondD = 0; |
jelord | 3:122bfc998c4c | 78 | float massflowSet = 19.2; |
jelord | 3:122bfc998c4c | 79 | float deltaVflow = 0.0; |
jelord | 3:122bfc998c4c | 80 | float deltaMflow = 0.0; |
jelord | 3:122bfc998c4c | 81 | float gainFlow = 98.1; |
jelord | 3:122bfc998c4c | 82 | float sampledVol = 2.0; //L, total sampled volume |
jelord | 3:122bfc998c4c | 83 | |
jelord | 3:122bfc998c4c | 84 | int digital_pot_setpoint = 5 ; //min = 0x7F, max = 0x00 |
jelord | 3:122bfc998c4c | 85 | int digital_pot_set = 6; |
jelord | 3:122bfc998c4c | 86 | int digital_pot_change = 7; |
jelord | 3:122bfc998c4c | 87 | int digitalpotMax = 127; |
jelord | 3:122bfc998c4c | 88 | int digitalpotMin = 2; |
jelord | 3:122bfc998c4c | 89 | |
jelord | 3:122bfc998c4c | 90 | int dutyUp = 4; |
jelord | 3:122bfc998c4c | 91 | int dutyDown = 3; |
jelord | 3:122bfc998c4c | 92 | |
jelord | 3:122bfc998c4c | 93 | // variables are only place holders for the US_Menu // |
jelord | 3:122bfc998c4c | 94 | int refreshtime = 6; |
jelord | 3:122bfc998c4c | 95 | float home_lat, home_lon, work_lat, work_lon; |
jelord | 3:122bfc998c4c | 96 | //*************************************************// |
jelord | 3:122bfc998c4c | 97 | |
jelord | 3:122bfc998c4c | 98 | //int refresh_Time = 10; // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s) |
jelord | 3:122bfc998c4c | 99 | |
jelord | 3:122bfc998c4c | 100 | char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt"; |
jelord | 3:122bfc998c4c | 101 | |
caseyquinn | 4:5d004fd997d5 | 102 | SDFileSystem sd(D4, D5, D3, D10, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (MOSI, MISO, SCK, SEL) |
jelord | 3:122bfc998c4c | 103 | |
jelord | 2:88fcbfadec6a | 104 | void sendData(); |
jelord | 2:88fcbfadec6a | 105 | |
jelord | 3:122bfc998c4c | 106 | //int timeout = 2; |
jelord | 2:88fcbfadec6a | 107 | |
jelord | 3:122bfc998c4c | 108 | //void pc_recv(void){ |
jelord | 3:122bfc998c4c | 109 | // while(pc.readable()){ |
jelord | 3:122bfc998c4c | 110 | // pc.getc(); |
jelord | 3:122bfc998c4c | 111 | // } |
jelord | 3:122bfc998c4c | 112 | //} |
jelord | 0:2cb2b2ea316f | 113 | |
jelord | 0:2cb2b2ea316f | 114 | static uint8_t rx_buf[20]; |
jelord | 0:2cb2b2ea316f | 115 | static uint8_t rx_len=0; |
jelord | 1:9fbb5b665068 | 116 | static int haltBLE = 1; |
jelord | 1:9fbb5b665068 | 117 | static int transmissionValue = 0; |
jelord | 2:88fcbfadec6a | 118 | uint8_t writeData[20] = {0,}; |
jelord | 2:88fcbfadec6a | 119 | static uint8_t dataLength = 0; |
jelord | 3:122bfc998c4c | 120 | static int runReady = 0; |
jelord | 3:122bfc998c4c | 121 | static uint8_t startAndEndTime[12] = {0,}; |
caseyquinn | 4:5d004fd997d5 | 122 | /* |
jelord | 0:2cb2b2ea316f | 123 | void uartMicro(void){ |
jelord | 3:122bfc998c4c | 124 | if(runReady!=1){ |
jelord | 3:122bfc998c4c | 125 | haltBLE = 2; |
jelord | 3:122bfc998c4c | 126 | while(microChannel.readable()){ |
jelord | 3:122bfc998c4c | 127 | rx_buf[rx_len++] = microChannel.getc(); |
jelord | 2:88fcbfadec6a | 128 | |
jelord | 3:122bfc998c4c | 129 | //Code block to verify what is being transmitted. To function correctly, all data must terminate with \0 or \n |
jelord | 3:122bfc998c4c | 130 | if(transmissionValue==0){ |
jelord | 3:122bfc998c4c | 131 | |
caseyquinn | 4:5d004fd997d5 | 132 | if (rx_buf[0] == 0x01)transmissionValue = 1; //rtc |
jelord | 3:122bfc998c4c | 133 | else if(rx_buf[0] == 0x02)transmissionValue = 2; //sample start and end times |
jelord | 3:122bfc998c4c | 134 | else if(rx_buf[0] == 0x03)transmissionValue = 3; //sample name |
jelord | 3:122bfc998c4c | 135 | else if(rx_buf[0] == 0x04)transmissionValue = 4; //Send Data Check |
jelord | 3:122bfc998c4c | 136 | |
jelord | 3:122bfc998c4c | 137 | else if(rx_buf[0] == 0x05)transmissionValue = 5; //log interval |
jelord | 3:122bfc998c4c | 138 | else if(rx_buf[0] == 0x06)transmissionValue = 6; //Flow Rate |
jelord | 3:122bfc998c4c | 139 | else if(rx_buf[0] == 0x07)transmissionValue = 7; //Serial Number |
jelord | 3:122bfc998c4c | 140 | else if(rx_buf[0] == 0x08)transmissionValue = 8; //Run Enable |
jelord | 3:122bfc998c4c | 141 | else transmissionValue = 100; //Not useful data |
jelord | 3:122bfc998c4c | 142 | } |
jelord | 3:122bfc998c4c | 143 | |
jelord | 3:122bfc998c4c | 144 | if(rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n' || rx_buf[rx_len-1] == 0xff){ |
jelord | 3:122bfc998c4c | 145 | if((transmissionValue == 1 || transmissionValue == 2 || transmissionValue == 3 || transmissionValue == 4 || transmissionValue == 5 || |
jelord | 3:122bfc998c4c | 146 | transmissionValue == 6 || transmissionValue == 7) && rx_buf[rx_len-1] != 0xff) |
jelord | 3:122bfc998c4c | 147 | {}else{ |
jelord | 3:122bfc998c4c | 148 | if(transmissionValue == 4 ) sendData(); |
jelord | 3:122bfc998c4c | 149 | if(transmissionValue == 8){ |
jelord | 3:122bfc998c4c | 150 | runReady = 1; |
jelord | 3:122bfc998c4c | 151 | microChannel.attach(NULL,microChannel.RxIrq); |
jelord | 3:122bfc998c4c | 152 | } |
jelord | 3:122bfc998c4c | 153 | haltBLE = 1; |
jelord | 3:122bfc998c4c | 154 | transmissionValue = 0; |
jelord | 3:122bfc998c4c | 155 | dataLength = 0; |
jelord | 2:88fcbfadec6a | 156 | |
jelord | 3:122bfc998c4c | 157 | } |
jelord | 2:88fcbfadec6a | 158 | } |
jelord | 1:9fbb5b665068 | 159 | } |
jelord | 3:122bfc998c4c | 160 | if(haltBLE!=1){ |
jelord | 2:88fcbfadec6a | 161 | |
jelord | 3:122bfc998c4c | 162 | if((transmissionValue!=100) && (dataLength!= 0)) writeData[dataLength-1] = rx_buf[0]; |
jelord | 3:122bfc998c4c | 163 | |
jelord | 3:122bfc998c4c | 164 | if(transmissionValue ==100){ |
jelord | 3:122bfc998c4c | 165 | //pc.putc(rx_buf[0]); |
jelord | 2:88fcbfadec6a | 166 | |
jelord | 3:122bfc998c4c | 167 | }else if(transmissionValue ==1){ //process and store RTC values |
jelord | 3:122bfc998c4c | 168 | |
jelord | 3:122bfc998c4c | 169 | if(dataLength==6)RTC_UPAS.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]); |
jelord | 3:122bfc998c4c | 170 | |
jelord | 3:122bfc998c4c | 171 | }else if(transmissionValue ==2){ //process and store sample start/end |
jelord | 3:122bfc998c4c | 172 | if(dataLength ==12)E2PROM.write(0x00015, writeData, 12); |
jelord | 3:122bfc998c4c | 173 | |
jelord | 3:122bfc998c4c | 174 | }else if(transmissionValue ==3){ //process and store sample name |
jelord | 3:122bfc998c4c | 175 | if(dataLength ==8)E2PROM.write(0x00001,writeData,8); |
jelord | 3:122bfc998c4c | 176 | |
jelord | 3:122bfc998c4c | 177 | }else if(transmissionValue ==5){ //process and store Log Interval |
jelord | 3:122bfc998c4c | 178 | if(dataLength ==1)E2PROM.write(0x00014,writeData,1); |
jelord | 2:88fcbfadec6a | 179 | |
jelord | 3:122bfc998c4c | 180 | }else if(transmissionValue ==6){ //process and store Flow Rate |
jelord | 3:122bfc998c4c | 181 | if(dataLength ==4)E2PROM.write(0x00010,writeData,4); |
jelord | 3:122bfc998c4c | 182 | |
jelord | 3:122bfc998c4c | 183 | }else if(transmissionValue ==7){ //process and store Serial Number |
jelord | 3:122bfc998c4c | 184 | if(dataLength ==2)E2PROM.write(0x00034,writeData,2); |
jelord | 3:122bfc998c4c | 185 | } |
jelord | 3:122bfc998c4c | 186 | dataLength++; |
jelord | 1:9fbb5b665068 | 187 | } |
jelord | 3:122bfc998c4c | 188 | |
jelord | 3:122bfc998c4c | 189 | rx_len = 0; |
jelord | 3:122bfc998c4c | 190 | }else{ |
jelord | 3:122bfc998c4c | 191 | while(microChannel.readable()) |
jelord | 3:122bfc998c4c | 192 | uint8_t extract = microChannel.getc(); |
jelord | 3:122bfc998c4c | 193 | } |
jelord | 2:88fcbfadec6a | 194 | |
jelord | 2:88fcbfadec6a | 195 | } |
jelord | 2:88fcbfadec6a | 196 | void sendData(){ |
jelord | 2:88fcbfadec6a | 197 | |
jelord | 2:88fcbfadec6a | 198 | uint8_t sampleTimePassValues[13] = {0x01,}; |
jelord | 2:88fcbfadec6a | 199 | uint8_t subjectLabelOriginal[9] = {0x02,}; |
jelord | 2:88fcbfadec6a | 200 | uint8_t dataLogOriginal[2] = {0x03,}; |
jelord | 2:88fcbfadec6a | 201 | uint8_t flowRateOriginal[5] = {0x04,}; |
jelord | 2:88fcbfadec6a | 202 | //uint8_t presetRunModeCheck[1] = {0,}; Commented and currently unused to prevent mem issues |
jelord | 2:88fcbfadec6a | 203 | E2PROM.read(0x00015, sampleTimePassValues+1, 12); |
jelord | 2:88fcbfadec6a | 204 | E2PROM.read(0x00001, subjectLabelOriginal+1,8); |
jelord | 2:88fcbfadec6a | 205 | E2PROM.read(0x00014,dataLogOriginal+1,1); |
jelord | 2:88fcbfadec6a | 206 | E2PROM.read(0x00010,flowRateOriginal+1,4); |
jelord | 2:88fcbfadec6a | 207 | |
jelord | 2:88fcbfadec6a | 208 | for(int i=0; i<13; i++){ |
jelord | 2:88fcbfadec6a | 209 | microChannel.putc(sampleTimePassValues[i]); |
jelord | 2:88fcbfadec6a | 210 | } |
jelord | 2:88fcbfadec6a | 211 | wait(.25); |
jelord | 2:88fcbfadec6a | 212 | |
jelord | 2:88fcbfadec6a | 213 | for(int i=0; i<9; i++){ |
jelord | 2:88fcbfadec6a | 214 | microChannel.putc(subjectLabelOriginal[i]); |
jelord | 2:88fcbfadec6a | 215 | } |
jelord | 2:88fcbfadec6a | 216 | wait(.25); |
jelord | 2:88fcbfadec6a | 217 | |
jelord | 2:88fcbfadec6a | 218 | for(int i=0; i<2; i++){ |
jelord | 2:88fcbfadec6a | 219 | microChannel.putc(dataLogOriginal[i]); |
jelord | 2:88fcbfadec6a | 220 | } |
jelord | 2:88fcbfadec6a | 221 | wait(.25); |
jelord | 2:88fcbfadec6a | 222 | |
jelord | 2:88fcbfadec6a | 223 | for(int i=0; i<5; i++){ |
jelord | 2:88fcbfadec6a | 224 | microChannel.putc(flowRateOriginal[i]); |
jelord | 2:88fcbfadec6a | 225 | } |
jelord | 0:2cb2b2ea316f | 226 | |
jelord | 2:88fcbfadec6a | 227 | |
jelord | 2:88fcbfadec6a | 228 | } |
caseyquinn | 4:5d004fd997d5 | 229 | */ |
jelord | 3:122bfc998c4c | 230 | void check_stop() // this checks if it's time to stop and shutdown |
jelord | 3:122bfc998c4c | 231 | { |
jelord | 3:122bfc998c4c | 232 | |
jelord | 3:122bfc998c4c | 233 | if(RTC_UPAS.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) { |
jelord | 3:122bfc998c4c | 234 | pbKill = 0; // this is were we shut everything down |
caseyquinn | 4:5d004fd997d5 | 235 | //pc.printf("If you're reading this something has gone very wrong."); |
jelord | 3:122bfc998c4c | 236 | } |
jelord | 3:122bfc998c4c | 237 | stop.detach(); |
jelord | 3:122bfc998c4c | 238 | stop.attach(&check_stop, 9); |
jelord | 3:122bfc998c4c | 239 | |
jelord | 3:122bfc998c4c | 240 | } |
jelord | 3:122bfc998c4c | 241 | int r = 1; |
jelord | 3:122bfc998c4c | 242 | int g = 0; |
jelord | 3:122bfc998c4c | 243 | int b = 1; |
jelord | 3:122bfc998c4c | 244 | |
jelord | 3:122bfc998c4c | 245 | void log_data() |
jelord | 3:122bfc998c4c | 246 | { |
jelord | 3:122bfc998c4c | 247 | |
jelord | 3:122bfc998c4c | 248 | |
jelord | 3:122bfc998c4c | 249 | RGB_LED.set_led(r,g,b); |
jelord | 3:122bfc998c4c | 250 | logg.detach(); |
jelord | 3:122bfc998c4c | 251 | logg.attach(&log_data, logInerval); // reading and logging data must take significintly less than 0.5s. This can be increased. |
jelord | 3:122bfc998c4c | 252 | |
jelord | 3:122bfc998c4c | 253 | RTC_UPAS.get_time(); |
jelord | 3:122bfc998c4c | 254 | |
jelord | 3:122bfc998c4c | 255 | secondsD = RTC_UPAS.seconds; |
jelord | 3:122bfc998c4c | 256 | lastsecondD = secondsD; |
jelord | 3:122bfc998c4c | 257 | |
jelord | 3:122bfc998c4c | 258 | omronVolt = (omronReading*4.096)/(32768*2); |
caseyquinn | 4:5d004fd997d5 | 259 | /* |
jelord | 3:122bfc998c4c | 260 | FILE *fp = fopen(filename, "a"); |
jelord | 3:122bfc998c4c | 261 | fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC_UPAS.year, RTC_UPAS.month,RTC_UPAS.date,RTC_UPAS.hour,RTC_UPAS.minutes,RTC_UPAS.seconds); |
jelord | 3:122bfc998c4c | 262 | fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho); |
jelord | 3:122bfc998c4c | 263 | fprintf(fp, "%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,", volflow, sampledVol, accel_x, accel_y, accel_z, accel_comp); |
jelord | 3:122bfc998c4c | 264 | fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass); |
jelord | 3:122bfc998c4c | 265 | fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps()); |
jelord | 3:122bfc998c4c | 266 | fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow); |
jelord | 3:122bfc998c4c | 267 | fclose(fp); |
jelord | 3:122bfc998c4c | 268 | //wait_ms(5); |
caseyquinn | 4:5d004fd997d5 | 269 | */ |
jelord | 3:122bfc998c4c | 270 | } |
jelord | 1:9fbb5b665068 | 271 | |
jelord | 1:9fbb5b665068 | 272 | int main(){ |
caseyquinn | 4:5d004fd997d5 | 273 | RGB_LED.set_led(1,1,1); |
caseyquinn | 4:5d004fd997d5 | 274 | STtime.tm_sec = 10; // 0-59 |
caseyquinn | 4:5d004fd997d5 | 275 | STtime.tm_min = 38; // 0-59 |
caseyquinn | 4:5d004fd997d5 | 276 | STtime.tm_hour = 15; // 0-23 |
caseyquinn | 4:5d004fd997d5 | 277 | STtime.tm_mday = 26; // 1-31 |
caseyquinn | 4:5d004fd997d5 | 278 | STtime.tm_mon = 1; // 0-11 |
caseyquinn | 4:5d004fd997d5 | 279 | STtime.tm_year = 116; // year since 1900 |
caseyquinn | 4:5d004fd997d5 | 280 | time_t STseconds = mktime(&STtime); |
caseyquinn | 4:5d004fd997d5 | 281 | set_time(STseconds); // Set RTC time to 16 December 2013 10:05:23 UTC |
caseyquinn | 4:5d004fd997d5 | 282 | char timestr[32]; |
caseyquinn | 4:5d004fd997d5 | 283 | //wait(10); |
caseyquinn | 4:5d004fd997d5 | 284 | RGB_LED.set_led(0,0,1); |
caseyquinn | 4:5d004fd997d5 | 285 | //pc.baud(115200); // set what you want here depending on your terminal program speed |
caseyquinn | 4:5d004fd997d5 | 286 | //pc.printf("\f\n\r-------------Startup-------------\n\r"); |
jelord | 1:9fbb5b665068 | 287 | wait(0.5); |
jelord | 3:122bfc998c4c | 288 | //timeout=2; |
caseyquinn | 4:5d004fd997d5 | 289 | /* |
jelord | 3:122bfc998c4c | 290 | uint8_t serialNumberAndType[6] = {0x50,0x53}; |
jelord | 3:122bfc998c4c | 291 | E2PROM.read(0x00034,serialNumberAndType+2,2); |
caseyquinn | 4:5d004fd997d5 | 292 | |
jelord | 3:122bfc998c4c | 293 | int tempSerialNum = serialNumberAndType[2]+serialNumberAndType[3]; |
jelord | 3:122bfc998c4c | 294 | int serialNumDigits[4]; |
jelord | 3:122bfc998c4c | 295 | serialNumDigits[0] = tempSerialNum / 1000 % 10; |
jelord | 3:122bfc998c4c | 296 | serialNumDigits[1] = tempSerialNum / 100 % 10; |
jelord | 3:122bfc998c4c | 297 | serialNumDigits[2] = tempSerialNum / 10 % 10; |
jelord | 3:122bfc998c4c | 298 | serialNumDigits[3] = tempSerialNum % 10; |
jelord | 3:122bfc998c4c | 299 | |
jelord | 3:122bfc998c4c | 300 | serialNumberAndType[2] = serialNumDigits[0]+48; |
jelord | 3:122bfc998c4c | 301 | serialNumberAndType[3] = serialNumDigits[1]+48; |
jelord | 3:122bfc998c4c | 302 | serialNumberAndType[4] = serialNumDigits[2]+48; |
jelord | 3:122bfc998c4c | 303 | serialNumberAndType[5] = serialNumDigits[3]+48; |
jelord | 2:88fcbfadec6a | 304 | |
jelord | 3:122bfc998c4c | 305 | //pc.attach(pc_recv); |
jelord | 1:9fbb5b665068 | 306 | microChannel.attach(uartMicro,microChannel.RxIrq); |
jelord | 3:122bfc998c4c | 307 | microChannel.baud(115200); |
jelord | 0:2cb2b2ea316f | 308 | |
jelord | 1:9fbb5b665068 | 309 | microChannel.printf("$$$"); |
jelord | 3:122bfc998c4c | 310 | wait(0.5); |
jelord | 3:122bfc998c4c | 311 | microChannel.printf("SN,"); |
jelord | 3:122bfc998c4c | 312 | for(int i=0;i<6;i++)microChannel.putc(serialNumberAndType[i]); |
jelord | 3:122bfc998c4c | 313 | microChannel.printf("\r"); |
jelord | 3:122bfc998c4c | 314 | wait(0.5); |
jelord | 1:9fbb5b665068 | 315 | microChannel.printf("A\r"); |
jelord | 3:122bfc998c4c | 316 | wait(0.5); |
jelord | 2:88fcbfadec6a | 317 | microChannel.printf("---\r"); |
jelord | 3:122bfc998c4c | 318 | wait(0.5); |
jelord | 2:88fcbfadec6a | 319 | |
jelord | 2:88fcbfadec6a | 320 | |
caseyquinn | 4:5d004fd997d5 | 321 | |
jelord | 2:88fcbfadec6a | 322 | |
jelord | 2:88fcbfadec6a | 323 | RGB_LED.set_led(1,1,1); |
jelord | 3:122bfc998c4c | 324 | while(runReady!=1) { |
jelord | 3:122bfc998c4c | 325 | wait(1); |
jelord | 3:122bfc998c4c | 326 | //pc.printf("Waiting for BLE instruction"); |
jelord | 3:122bfc998c4c | 327 | |
jelord | 3:122bfc998c4c | 328 | } |
jelord | 3:122bfc998c4c | 329 | |
jelord | 3:122bfc998c4c | 330 | |
jelord | 3:122bfc998c4c | 331 | //wait(1); |
jelord | 3:122bfc998c4c | 332 | |
jelord | 3:122bfc998c4c | 333 | E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM |
jelord | 3:122bfc998c4c | 334 | RGB_LED.set_led(0,1,0); |
jelord | 3:122bfc998c4c | 335 | while(!RTC_UPAS.compare(startAndEndTime[0], startAndEndTime[1], startAndEndTime[2], startAndEndTime[3], startAndEndTime[4], startAndEndTime[5])) { // this while waits for the start time by looping until the start time |
jelord | 3:122bfc998c4c | 336 | wait(0.5); |
jelord | 3:122bfc998c4c | 337 | |
jelord | 3:122bfc998c4c | 338 | RTC_UPAS.get_time(); |
jelord | 3:122bfc998c4c | 339 | |
jelord | 3:122bfc998c4c | 340 | } |
jelord | 3:122bfc998c4c | 341 | |
jelord | 3:122bfc998c4c | 342 | |
jelord | 3:122bfc998c4c | 343 | //Get the proper serial number |
jelord | 3:122bfc998c4c | 344 | uint8_t serialBytes[2] = {0,}; |
jelord | 3:122bfc998c4c | 345 | E2PROM.read(0x00034, serialBytes,2); |
jelord | 3:122bfc998c4c | 346 | serial_num = ((uint16_t)serialBytes[1] << 8) | serialBytes[0]; |
jelord | 3:122bfc998c4c | 347 | calibrations.initialize(serial_num); |
jelord | 3:122bfc998c4c | 348 | |
jelord | 3:122bfc998c4c | 349 | uint8_t logByte[1] = {0,}; |
jelord | 3:122bfc998c4c | 350 | E2PROM.read(0x00014,logByte,1); |
jelord | 3:122bfc998c4c | 351 | logInerval = logByte[0]; |
jelord | 3:122bfc998c4c | 352 | |
jelord | 3:122bfc998c4c | 353 | stop.attach(&check_stop, 30); // check if we should shut down every 9 seconds, starting 60s after the start. |
caseyquinn | 4:5d004fd997d5 | 354 | |
jelord | 3:122bfc998c4c | 355 | //Use the flow rate value stored in eeprom |
jelord | 3:122bfc998c4c | 356 | uint8_t flowRateBytes[4] = {0,}; |
jelord | 3:122bfc998c4c | 357 | E2PROM.read(0x00010,flowRateBytes,4); |
jelord | 3:122bfc998c4c | 358 | E2PROM.byteToFloat(flowRateBytes, &volflowSet); |
jelord | 3:122bfc998c4c | 359 | |
jelord | 3:122bfc998c4c | 360 | if(volflowSet<=1.0) { |
jelord | 3:122bfc998c4c | 361 | gainFlow = 100; |
jelord | 3:122bfc998c4c | 362 | } else if(volflowSet>=2.0) { |
jelord | 3:122bfc998c4c | 363 | gainFlow = 25; |
jelord | 3:122bfc998c4c | 364 | } else { |
jelord | 3:122bfc998c4c | 365 | gainFlow = 25; |
jelord | 0:2cb2b2ea316f | 366 | } |
jelord | 3:122bfc998c4c | 367 | |
jelord | 3:122bfc998c4c | 368 | RGB_LED.set_led(1,0,0); |
jelord | 3:122bfc998c4c | 369 | press = bmesensor.getPressure(); |
jelord | 3:122bfc998c4c | 370 | temp = bmesensor.getTemperature(); |
jelord | 3:122bfc998c4c | 371 | rh = bmesensor.getHumidity(); |
jelord | 3:122bfc998c4c | 372 | |
jelord | 3:122bfc998c4c | 373 | atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15)); |
jelord | 3:122bfc998c4c | 374 | massflowSet = volflowSet*atmoRho; |
jelord | 3:122bfc998c4c | 375 | //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
jelord | 3:122bfc998c4c | 376 | |
jelord | 3:122bfc998c4c | 377 | |
jelord | 3:122bfc998c4c | 378 | |
jelord | 3:122bfc998c4c | 379 | |
jelord | 3:122bfc998c4c | 380 | DigPot.writeRegister(digital_pot_setpoint); |
jelord | 3:122bfc998c4c | 381 | wait(1); |
jelord | 3:122bfc998c4c | 382 | blower = 1; |
jelord | 3:122bfc998c4c | 383 | |
jelord | 3:122bfc998c4c | 384 | uint8_t subjectLabelOriginal[8] = {0,}; |
caseyquinn | 4:5d004fd997d5 | 385 | E2PROM.read(0x00001, subjectLabelOriginal,8); |
jelord | 3:122bfc998c4c | 386 | sprintf(filename, "/sd/UPAS%04dLOG_%02d-%02d-%02d_%02d=%02d=%02d_%c%c%c%c%c%c%c%c.txt",serial_num,RTC_UPAS.year,RTC_UPAS.month,RTC_UPAS.date,RTC_UPAS.hour,RTC_UPAS.minutes,RTC_UPAS.seconds,subjectLabelOriginal[0],subjectLabelOriginal[1],subjectLabelOriginal[2],subjectLabelOriginal[3],subjectLabelOriginal[4],subjectLabelOriginal[5],subjectLabelOriginal[6],subjectLabelOriginal[7]); |
caseyquinn | 4:5d004fd997d5 | 387 | */ |
caseyquinn | 4:5d004fd997d5 | 388 | time_t seconds = time(NULL); |
caseyquinn | 4:5d004fd997d5 | 389 | strftime(timestr, 32, "%y-%m-%d-%H=%M=%S", localtime(&seconds)); |
caseyquinn | 4:5d004fd997d5 | 390 | sprintf(filename, "/sd/UPASboardtest_%s.txt", timestr); |
jelord | 3:122bfc998c4c | 391 | FILE *fp = fopen(filename, "w"); |
jelord | 3:122bfc998c4c | 392 | fclose(fp); |
jelord | 3:122bfc998c4c | 393 | |
jelord | 3:122bfc998c4c | 394 | //---------------------------------------------------------------------------------------------// |
jelord | 3:122bfc998c4c | 395 | //Following lines are needed to enter into the initiallization flow control loop |
jelord | 3:122bfc998c4c | 396 | |
jelord | 3:122bfc998c4c | 397 | wait(5); |
jelord | 3:122bfc998c4c | 398 | |
jelord | 3:122bfc998c4c | 399 | |
jelord | 3:122bfc998c4c | 400 | sampledVol = 0.0; |
jelord | 3:122bfc998c4c | 401 | RGB_LED.set_led(0,1,0); |
jelord | 3:122bfc998c4c | 402 | |
jelord | 3:122bfc998c4c | 403 | |
jelord | 3:122bfc998c4c | 404 | |
jelord | 3:122bfc998c4c | 405 | //** end of initalization **// |
jelord | 3:122bfc998c4c | 406 | //---------------------------------------------------------------------------------------------// |
jelord | 3:122bfc998c4c | 407 | //---------------------------------------------------------------------------------------------// |
jelord | 3:122bfc998c4c | 408 | // Main Control Loop |
jelord | 3:122bfc998c4c | 409 | |
jelord | 3:122bfc998c4c | 410 | |
jelord | 3:122bfc998c4c | 411 | //logg.attach(&log_data, 30); // uses callbacks or block Interrupts for anything that uses i2c |
caseyquinn | 4:5d004fd997d5 | 412 | // while(!RTC_UPAS.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])){ |
caseyquinn | 4:5d004fd997d5 | 413 | while(1){ |
jelord | 3:122bfc998c4c | 414 | wait(logInerval); |
jelord | 3:122bfc998c4c | 415 | RGB_LED.set_led(1,1,0); |
caseyquinn | 4:5d004fd997d5 | 416 | |
caseyquinn | 4:5d004fd997d5 | 417 | |
caseyquinn | 4:5d004fd997d5 | 418 | time_t seconds = time(NULL); |
caseyquinn | 4:5d004fd997d5 | 419 | strftime(timestr, 32, "%y%m%d%H%M%S", localtime(&seconds)); |
caseyquinn | 4:5d004fd997d5 | 420 | press = bmesensor.getPressure(); |
caseyquinn | 4:5d004fd997d5 | 421 | temp = bmesensor.getTemperature()-5.0; |
caseyquinn | 4:5d004fd997d5 | 422 | rh = bmesensor.getHumidity(); |
caseyquinn | 4:5d004fd997d5 | 423 | uv = lightsensor.getUV(); |
caseyquinn | 4:5d004fd997d5 | 424 | |
caseyquinn | 4:5d004fd997d5 | 425 | FILE *fp = fopen(filename, "a"); |
caseyquinn | 4:5d004fd997d5 | 426 | //fprintf(fp, "Time as a basic string = %s\r\n", ctime(&seconds)); |
caseyquinn | 4:5d004fd997d5 | 427 | //fprintf(fp, "Time as a basic string = %s\r\n", timestr); |
caseyquinn | 4:5d004fd997d5 | 428 | //fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC_UPAS.year, RTC_UPAS.month,RTC_UPAS.date,RTC_UPAS.hour,RTC_UPAS.minutes,RTC_UPAS.seconds); |
caseyquinn | 4:5d004fd997d5 | 429 | fprintf(fp, "%s,", timestr); |
caseyquinn | 4:5d004fd997d5 | 430 | fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho); |
caseyquinn | 4:5d004fd997d5 | 431 | fprintf(fp, "%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,", volflow, sampledVol, accel_x, accel_y, accel_z, accel_comp); |
caseyquinn | 4:5d004fd997d5 | 432 | fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass); |
caseyquinn | 4:5d004fd997d5 | 433 | fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps()); |
caseyquinn | 4:5d004fd997d5 | 434 | fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow); |
caseyquinn | 4:5d004fd997d5 | 435 | fclose(fp); |
caseyquinn | 4:5d004fd997d5 | 436 | free(fp); |
caseyquinn | 4:5d004fd997d5 | 437 | RGB_LED.set_led(1,0,0); |
jelord | 3:122bfc998c4c | 438 | } |
caseyquinn | 4:5d004fd997d5 | 439 | |
jelord | 3:122bfc998c4c | 440 | |
jelord | 0:2cb2b2ea316f | 441 | } |