Code supports writing to the SD card as well as working with the Volckens group smartphone apps for the mbed HRM1017
Dependencies: ADS1115 BLE_API BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed nRF51822
Fork of UPAS_BLE_and_USB by
main.cpp@103:a2d7d946d69c, 2016-01-20 (annotated)
- Committer:
- caseyquinn
- Date:
- Wed Jan 20 21:05:07 2016 +0000
- Revision:
- 103:a2d7d946d69c
- Parent:
- 102:d25eab697fe0
Updated the datalog function to match the code in UPAS_USB_Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
caseyquinn | 0:14d46ef4b6cb | 1 | #include "mbed.h" |
caseyquinn | 0:14d46ef4b6cb | 2 | #include "SDFileSystem.h" |
caseyquinn | 0:14d46ef4b6cb | 3 | #include "Adafruit_ADS1015.h" |
caseyquinn | 1:37babeb68ab9 | 4 | #include "MCP40D17.h" |
caseyquinn | 7:a24d7156bc02 | 5 | #include "STC3100.h" |
caseyquinn | 10:f9cb61b29340 | 6 | #include "LSM303.h" |
caseyquinn | 12:8c00a7f5d483 | 7 | #include "BME280.h" |
caseyquinn | 14:ad550174db8b | 8 | #include "SI1145.h" |
joshuasmth04 | 40:ef7e1dcb3780 | 9 | #include "NCP5623BMUTBG.h" |
joshuasmth04 | 41:1fb3e0ac6f87 | 10 | #include "CronoDot.h" |
caseyquinn | 56:c4d6bdd7c3fb | 11 | #include "EEPROM.h" |
joshuasmth04 | 77:24fbeb2bfe05 | 12 | #include "BLEDevice.h" |
joshuasmth04 | 77:24fbeb2bfe05 | 13 | #include "Calibration.h" |
jelord | 94:c57720890e76 | 14 | #include "UPAS_Service.h" |
joshuasmth04 | 71:78edbceff4fc | 15 | |
caseyquinn | 1:37babeb68ab9 | 16 | |
caseyquinn | 1:37babeb68ab9 | 17 | #define SERIAL_BAUD_RATE 9600 |
joshuasmth04 | 41:1fb3e0ac6f87 | 18 | #define SCL 20 |
caseyquinn | 7:a24d7156bc02 | 19 | #define SDA 22 |
caseyquinn | 81:480f0310ef9a | 20 | |
caseyquinn | 0:14d46ef4b6cb | 21 | |
joshuasmth04 | 71:78edbceff4fc | 22 | |
jelord | 94:c57720890e76 | 23 | |
joshuasmth04 | 71:78edbceff4fc | 24 | |
jelord | 97:c596f8ed4562 | 25 | uint8_t startAndEndTime[12] = {0,}; |
jelord | 97:c596f8ed4562 | 26 | uint8_t logIntervalReadOut[1] = {0,}; |
jelord | 94:c57720890e76 | 27 | static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID |
joshuasmth04 | 71:78edbceff4fc | 28 | BLEDevice ble; |
jelord | 94:c57720890e76 | 29 | UPAS_Service *upasServicePtr; |
jelord | 94:c57720890e76 | 30 | //BLE_Menu bleMenu(ble, txPayload, rxPayload, uart_base_uuid_rev, uart_base_uuid, uartService); |
joshuasmth04 | 71:78edbceff4fc | 31 | |
joshuasmth04 | 72:0b36575ab00d | 32 | I2C i2c(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 33 | Adafruit_ADS1115 ads(&i2c); |
joshuasmth04 | 72:0b36575ab00d | 34 | MCP40D17 DigPot(&i2c); |
joshuasmth04 | 72:0b36575ab00d | 35 | BME280 bmesensor(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 36 | STC3100 gasG(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 37 | Serial pc(USBTX, USBRX); |
joshuasmth04 | 72:0b36575ab00d | 38 | DigitalOut blower(p29, 0); |
joshuasmth04 | 72:0b36575ab00d | 39 | DigitalOut pbKill(p18, 1); |
joshuasmth04 | 72:0b36575ab00d | 40 | LSM303 movementsensor(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 41 | SI1145 lightsensor(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 42 | NCP5623BMUTBG RGB_LED(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 43 | CronoDot RTC(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 44 | EEPROM E2PROM(p22, p20); |
joshuasmth04 | 77:24fbeb2bfe05 | 45 | DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications |
joshuasmth04 | 77:24fbeb2bfe05 | 46 | Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option |
caseyquinn | 57:0b554f7aa9a3 | 47 | |
caseyquinn | 57:0b554f7aa9a3 | 48 | Timeout stop; //This is the stop call back object |
joshuasmth04 | 77:24fbeb2bfe05 | 49 | Timeout logg; //This is the logging call back object |
caseyquinn | 67:300418575137 | 50 | |
joshuasmth04 | 77:24fbeb2bfe05 | 51 | uint16_t serial_num = 1; // Default serial/calibration number |
joshuasmth04 | 77:24fbeb2bfe05 | 52 | int RunReady =0; |
caseyquinn | 70:81f04e69e08a | 53 | |
caseyquinn | 70:81f04e69e08a | 54 | |
caseyquinn | 20:ad9883973d86 | 55 | float press; |
caseyquinn | 20:ad9883973d86 | 56 | float temp; |
caseyquinn | 20:ad9883973d86 | 57 | float rh; |
caseyquinn | 4:69bd7e8a994c | 58 | |
caseyquinn | 20:ad9883973d86 | 59 | int uv; |
caseyquinn | 20:ad9883973d86 | 60 | int vis; |
caseyquinn | 20:ad9883973d86 | 61 | int ir; |
caseyquinn | 8:204c21adf693 | 62 | |
caseyquinn | 67:300418575137 | 63 | float compass; |
caseyquinn | 10:f9cb61b29340 | 64 | float accel_x; |
caseyquinn | 10:f9cb61b29340 | 65 | float accel_y; |
caseyquinn | 10:f9cb61b29340 | 66 | float accel_z; |
lionberg | 52:80480b2fafba | 67 | float accel_comp; |
caseyquinn | 103:a2d7d946d69c | 68 | float angle_x; |
caseyquinn | 103:a2d7d946d69c | 69 | float angle_y; |
caseyquinn | 103:a2d7d946d69c | 70 | float angle_z; |
caseyquinn | 10:f9cb61b29340 | 71 | float mag_x; |
caseyquinn | 10:f9cb61b29340 | 72 | float mag_y; |
caseyquinn | 10:f9cb61b29340 | 73 | float mag_z; |
caseyquinn | 10:f9cb61b29340 | 74 | |
caseyquinn | 0:14d46ef4b6cb | 75 | int vInReading; |
caseyquinn | 0:14d46ef4b6cb | 76 | int vBlowerReading; |
caseyquinn | 0:14d46ef4b6cb | 77 | int omronDiff; |
lionberg | 52:80480b2fafba | 78 | float omronVolt; //V |
caseyquinn | 20:ad9883973d86 | 79 | int omronReading; |
lionberg | 52:80480b2fafba | 80 | float atmoRho; //g/L |
caseyquinn | 49:19e828650618 | 81 | |
caseyquinn | 20:ad9883973d86 | 82 | float massflow; //g/min |
caseyquinn | 20:ad9883973d86 | 83 | float volflow; //L/min |
caseyquinn | 67:300418575137 | 84 | float volflowSet = 1.0; //L/min |
caseyquinn | 81:480f0310ef9a | 85 | int logInerval = 10; //seconds |
caseyquinn | 57:0b554f7aa9a3 | 86 | double secondsD = 0; |
caseyquinn | 103:a2d7d946d69c | 87 | double lastsecondD = 0; |
joshuasmth04 | 41:1fb3e0ac6f87 | 88 | float massflowSet; |
caseyquinn | 42:fc2f2b9f07ae | 89 | float deltaVflow = 0.0; |
caseyquinn | 42:fc2f2b9f07ae | 90 | float deltaMflow = 0.0; |
caseyquinn | 47:3146e8c949a9 | 91 | float gainFlow; |
lionberg | 52:80480b2fafba | 92 | float sampledVol; //L, total sampled volume |
caseyquinn | 20:ad9883973d86 | 93 | |
caseyquinn | 55:f24d70f519cd | 94 | int digital_pot_setpoint; //min = 0x7F, max = 0x00 |
caseyquinn | 55:f24d70f519cd | 95 | int digital_pot_set; |
caseyquinn | 55:f24d70f519cd | 96 | int digital_pot_change; |
caseyquinn | 55:f24d70f519cd | 97 | int digitalpotMax = 127; |
caseyquinn | 55:f24d70f519cd | 98 | int digitalpotMin = 2; |
caseyquinn | 55:f24d70f519cd | 99 | |
joshuasmth04 | 59:a9b21b3d9afc | 100 | int dutyUp; |
joshuasmth04 | 59:a9b21b3d9afc | 101 | int dutyDown; |
joshuasmth04 | 59:a9b21b3d9afc | 102 | |
joshuasmth04 | 72:0b36575ab00d | 103 | // variables are only place holders for the US_Menu // |
joshuasmth04 | 72:0b36575ab00d | 104 | int refreshtime; |
joshuasmth04 | 72:0b36575ab00d | 105 | float home_lat, home_lon, work_lat, work_lon; |
joshuasmth04 | 77:24fbeb2bfe05 | 106 | //*************************************************// |
joshuasmth04 | 72:0b36575ab00d | 107 | |
caseyquinn | 67:300418575137 | 108 | //int refresh_Time = 10; // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s) |
caseyquinn | 42:fc2f2b9f07ae | 109 | |
caseyquinn | 81:480f0310ef9a | 110 | char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt"; |
caseyquinn | 7:a24d7156bc02 | 111 | SDFileSystem sd(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, SPIS_PSELSS, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (p12, p13, p15, p14) |
caseyquinn | 7:a24d7156bc02 | 112 | |
caseyquinn | 7:a24d7156bc02 | 113 | |
joshuasmth04 | 71:78edbceff4fc | 114 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
joshuasmth04 | 71:78edbceff4fc | 115 | { |
jelord | 94:c57720890e76 | 116 | ble.startAdvertising(); |
joshuasmth04 | 71:78edbceff4fc | 117 | } |
joshuasmth04 | 71:78edbceff4fc | 118 | |
joshuasmth04 | 71:78edbceff4fc | 119 | void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) // called any time the phone sends a message |
joshuasmth04 | 71:78edbceff4fc | 120 | { |
jelord | 94:c57720890e76 | 121 | uint8_t *writeData = const_cast<uint8_t*>(Handler->data); |
jelord | 94:c57720890e76 | 122 | // check to see what characteristic was written, by handle |
jelord | 94:c57720890e76 | 123 | if(Handler->charHandle == upasServicePtr->rtcCharacteristic.getValueAttribute().getHandle()) { |
jelord | 94:c57720890e76 | 124 | |
jelord | 94:c57720890e76 | 125 | |
jelord | 94:c57720890e76 | 126 | |
jelord | 94:c57720890e76 | 127 | E2PROM.write(0x00015, writeData+6, 12); |
jelord | 94:c57720890e76 | 128 | RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]); |
jelord | 94:c57720890e76 | 129 | |
jelord | 94:c57720890e76 | 130 | }else if(Handler->charHandle == upasServicePtr->sampleTimeCharacteristic.getValueAttribute().getHandle()){ |
jelord | 94:c57720890e76 | 131 | |
jelord | 94:c57720890e76 | 132 | E2PROM.write(0x00015, writeData, 12); |
jelord | 94:c57720890e76 | 133 | |
jelord | 94:c57720890e76 | 134 | }else if(Handler->charHandle == upasServicePtr->subjectLabelCharacteristic.getValueAttribute().getHandle()){ |
jelord | 94:c57720890e76 | 135 | E2PROM.write(0x00001,writeData,15); |
jelord | 94:c57720890e76 | 136 | |
jelord | 94:c57720890e76 | 137 | }else if(Handler->charHandle == upasServicePtr->runReadyCharacteristic.getValueAttribute().getHandle()){ |
jelord | 100:da71436aa52a | 138 | |
jelord | 94:c57720890e76 | 139 | uint8_t runData = writeData[0]; |
jelord | 100:da71436aa52a | 140 | E2PROM.write(0x00033,writeData,1); |
jelord | 94:c57720890e76 | 141 | |
jelord | 100:da71436aa52a | 142 | if(runData == 0x0A){ |
jelord | 100:da71436aa52a | 143 | RunReady = 10; |
jelord | 100:da71436aa52a | 144 | }else{ |
jelord | 100:da71436aa52a | 145 | RunReady = 12; |
jelord | 100:da71436aa52a | 146 | } |
jelord | 94:c57720890e76 | 147 | |
jelord | 96:03106adb45c9 | 148 | }else if(Handler->charHandle == upasServicePtr->logIntevalCharacteristic.getValueAttribute().getHandle()){ |
jelord | 94:c57720890e76 | 149 | /* Trigger demo mode*/ |
jelord | 96:03106adb45c9 | 150 | //RGB_LED.set_led(3,1,0); |
jelord | 96:03106adb45c9 | 151 | E2PROM.write(0x00014,writeData,1); |
jelord | 96:03106adb45c9 | 152 | |
jelord | 96:03106adb45c9 | 153 | }else if(Handler->charHandle == upasServicePtr->flowRateCharacteristic.getValueAttribute().getHandle()){ |
jelord | 96:03106adb45c9 | 154 | //RGB_LED.set_led(3,1,0); |
jelord | 98:d70ff5a0b2e6 | 155 | E2PROM.write(0x00010,writeData,4); |
jelord | 98:d70ff5a0b2e6 | 156 | |
jelord | 98:d70ff5a0b2e6 | 157 | //float volFlowTester = 0.0; |
jelord | 98:d70ff5a0b2e6 | 158 | //uint8_t flowRateTestBytes[4] = {0,}; |
jelord | 98:d70ff5a0b2e6 | 159 | //E2PROM.read(0x00010,flowRateTestBytes,4); |
jelord | 98:d70ff5a0b2e6 | 160 | //E2PROM.byteToFloat(flowRateTestBytes, &volFlowTester); |
jelord | 98:d70ff5a0b2e6 | 161 | |
jelord | 94:c57720890e76 | 162 | } |
joshuasmth04 | 71:78edbceff4fc | 163 | } |
joshuasmth04 | 71:78edbceff4fc | 164 | |
joshuasmth04 | 62:edc9632bcc43 | 165 | void check_stop() // this checks if it's time to stop and shutdown |
caseyquinn | 57:0b554f7aa9a3 | 166 | { |
jelord | 94:c57720890e76 | 167 | |
jelord | 94:c57720890e76 | 168 | if(RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) { |
caseyquinn | 57:0b554f7aa9a3 | 169 | pbKill = 0; // this is were we shut everything down |
caseyquinn | 103:a2d7d946d69c | 170 | //pc.printf("If you're reading this something has gone very wrong."); |
caseyquinn | 57:0b554f7aa9a3 | 171 | } |
caseyquinn | 57:0b554f7aa9a3 | 172 | stop.detach(); |
joshuasmth04 | 77:24fbeb2bfe05 | 173 | stop.attach(&check_stop, 9); |
jelord | 97:c596f8ed4562 | 174 | |
joshuasmth04 | 62:edc9632bcc43 | 175 | } |
caseyquinn | 58:7239c2ab2b65 | 176 | |
joshuasmth04 | 41:1fb3e0ac6f87 | 177 | |
caseyquinn | 57:0b554f7aa9a3 | 178 | void log_data() |
joshuasmth04 | 59:a9b21b3d9afc | 179 | { |
caseyquinn | 70:81f04e69e08a | 180 | logg.detach(); |
jelord | 94:c57720890e76 | 181 | logg.attach(&log_data, logInerval); // reading and logging data must take significintly less than 0.5s. This can be increased. |
caseyquinn | 103:a2d7d946d69c | 182 | |
joshuasmth04 | 59:a9b21b3d9afc | 183 | RTC.get_time(); |
caseyquinn | 103:a2d7d946d69c | 184 | |
caseyquinn | 103:a2d7d946d69c | 185 | //*****************************************// |
caseyquinn | 103:a2d7d946d69c | 186 | //RTC.get_time(); //debug |
caseyquinn | 103:a2d7d946d69c | 187 | //pc.printf("%02d:%02d:%02d on %d/%d/%d before fmod \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debug |
caseyquinn | 103:a2d7d946d69c | 188 | //*****************************************// |
caseyquinn | 103:a2d7d946d69c | 189 | secondsD = RTC.seconds; |
caseyquinn | 103:a2d7d946d69c | 190 | |
caseyquinn | 103:a2d7d946d69c | 191 | while(fmod(secondsD,logInerval)!=0 || floor(secondsD)==floor(lastsecondD)) { |
caseyquinn | 103:a2d7d946d69c | 192 | //pc.printf("%f, %f\r\n", floor(secondsD), floor(lastsecondD)); |
caseyquinn | 103:a2d7d946d69c | 193 | RTC.get_time(); |
caseyquinn | 103:a2d7d946d69c | 194 | secondsD = RTC.seconds; |
caseyquinn | 103:a2d7d946d69c | 195 | wait_ms(100); |
caseyquinn | 103:a2d7d946d69c | 196 | } |
caseyquinn | 103:a2d7d946d69c | 197 | lastsecondD = secondsD; |
caseyquinn | 103:a2d7d946d69c | 198 | //*****************************************// |
caseyquinn | 103:a2d7d946d69c | 199 | //RTC.get_time(); //debug |
caseyquinn | 103:a2d7d946d69c | 200 | //pc.printf("%02d:%02d:%02d on %d/%d/%d after fmod \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debug |
caseyquinn | 103:a2d7d946d69c | 201 | //*****************************************// |
joshuasmth04 | 59:a9b21b3d9afc | 202 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 203 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 204 | |
joshuasmth04 | 77:24fbeb2bfe05 | 205 | if(omronVolt<=calibrations.omronVMin) { |
joshuasmth04 | 77:24fbeb2bfe05 | 206 | massflow = calibrations.omronMFMin; |
joshuasmth04 | 77:24fbeb2bfe05 | 207 | } else if(omronVolt>=calibrations.omronVMax) { |
joshuasmth04 | 77:24fbeb2bfe05 | 208 | massflow = calibrations.omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 209 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 210 | massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 211 | } |
joshuasmth04 | 59:a9b21b3d9afc | 212 | |
joshuasmth04 | 59:a9b21b3d9afc | 213 | 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)); |
joshuasmth04 | 59:a9b21b3d9afc | 214 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 215 | sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow); |
joshuasmth04 | 59:a9b21b3d9afc | 216 | deltaVflow = volflow-volflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 217 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 218 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 219 | if(abs(deltaMflow)>.025) { |
joshuasmth04 | 59:a9b21b3d9afc | 220 | digital_pot_change = (int)(gainFlow*deltaMflow); |
joshuasmth04 | 59:a9b21b3d9afc | 221 | |
joshuasmth04 | 59:a9b21b3d9afc | 222 | |
joshuasmth04 | 59:a9b21b3d9afc | 223 | if(abs(digital_pot_change)>=50) { |
joshuasmth04 | 59:a9b21b3d9afc | 224 | digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 225 | RGB_LED.set_led(1,0,0); |
caseyquinn | 103:a2d7d946d69c | 226 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 227 | digital_pot_set = (digital_pot_set+ digital_pot_change); |
joshuasmth04 | 59:a9b21b3d9afc | 228 | RGB_LED.set_led(1,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 229 | } |
caseyquinn | 103:a2d7d946d69c | 230 | |
caseyquinn | 103:a2d7d946d69c | 231 | if(digital_pot_set>=digitalpotMax) { |
caseyquinn | 103:a2d7d946d69c | 232 | digital_pot_set = digitalpotMax; |
caseyquinn | 103:a2d7d946d69c | 233 | RGB_LED.set_led(1,0,0); |
caseyquinn | 103:a2d7d946d69c | 234 | } else if(digital_pot_set<=digitalpotMin) { |
caseyquinn | 103:a2d7d946d69c | 235 | digital_pot_set = digitalpotMin; |
caseyquinn | 103:a2d7d946d69c | 236 | RGB_LED.set_led(1,0,0); |
caseyquinn | 103:a2d7d946d69c | 237 | } |
caseyquinn | 103:a2d7d946d69c | 238 | |
joshuasmth04 | 59:a9b21b3d9afc | 239 | |
joshuasmth04 | 59:a9b21b3d9afc | 240 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 241 | |
joshuasmth04 | 59:a9b21b3d9afc | 242 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 243 | RGB_LED.set_led(0,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 244 | } |
joshuasmth04 | 59:a9b21b3d9afc | 245 | movementsensor.getACCEL(); |
joshuasmth04 | 59:a9b21b3d9afc | 246 | movementsensor.getCOMPASS(); |
caseyquinn | 67:300418575137 | 247 | compass = movementsensor.getCOMPASS_HEADING(); |
joshuasmth04 | 59:a9b21b3d9afc | 248 | accel_x = movementsensor.AccelData.x; |
joshuasmth04 | 59:a9b21b3d9afc | 249 | accel_y = movementsensor.AccelData.y; |
joshuasmth04 | 59:a9b21b3d9afc | 250 | accel_z = movementsensor.AccelData.z; |
joshuasmth04 | 59:a9b21b3d9afc | 251 | accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0; |
joshuasmth04 | 59:a9b21b3d9afc | 252 | mag_x = movementsensor.MagData.x; |
joshuasmth04 | 59:a9b21b3d9afc | 253 | mag_y = movementsensor.MagData.y; |
joshuasmth04 | 59:a9b21b3d9afc | 254 | mag_z = movementsensor.MagData.z; |
joshuasmth04 | 59:a9b21b3d9afc | 255 | vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 |
joshuasmth04 | 59:a9b21b3d9afc | 256 | vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 |
joshuasmth04 | 59:a9b21b3d9afc | 257 | omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 |
joshuasmth04 | 59:a9b21b3d9afc | 258 | press = bmesensor.getPressure(); |
joshuasmth04 | 59:a9b21b3d9afc | 259 | temp = bmesensor.getTemperature()-5.0; |
joshuasmth04 | 59:a9b21b3d9afc | 260 | rh = bmesensor.getHumidity(); |
joshuasmth04 | 59:a9b21b3d9afc | 261 | uv = lightsensor.getUV(); |
joshuasmth04 | 59:a9b21b3d9afc | 262 | vis = lightsensor.getVIS(); |
joshuasmth04 | 59:a9b21b3d9afc | 263 | ir = lightsensor.getIR(); |
caseyquinn | 103:a2d7d946d69c | 264 | |
joshuasmth04 | 59:a9b21b3d9afc | 265 | FILE *fp = fopen(filename, "a"); |
caseyquinn | 103:a2d7d946d69c | 266 | fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds); |
caseyquinn | 103:a2d7d946d69c | 267 | fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho); |
caseyquinn | 103:a2d7d946d69c | 268 | 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 | 103:a2d7d946d69c | 269 | fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass); |
caseyquinn | 103:a2d7d946d69c | 270 | fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps()); |
caseyquinn | 103:a2d7d946d69c | 271 | fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow); |
joshuasmth04 | 59:a9b21b3d9afc | 272 | fclose(fp); |
caseyquinn | 103:a2d7d946d69c | 273 | |
caseyquinn | 93:b53a9a7cb8f1 | 274 | |
joshuasmth04 | 59:a9b21b3d9afc | 275 | } |
jelord | 96:03106adb45c9 | 276 | /*EEPROM ADDRESSING: |
jelord | 96:03106adb45c9 | 277 | 0:Status bit-Unused |
jelord | 96:03106adb45c9 | 278 | 1-15:Device Name |
jelord | 96:03106adb45c9 | 279 | 16-19:Flow Rate |
jelord | 96:03106adb45c9 | 280 | 20: Data Log Interval |
jelord | 96:03106adb45c9 | 281 | 21-26: Start Time: ssmmHHddMMyy |
jelord | 96:03106adb45c9 | 282 | 27-32: Stop Time: ssmmHHddMMyy |
jelord | 96:03106adb45c9 | 283 | 33: Duty Up |
jelord | 96:03106adb45c9 | 284 | 34: Duty Down |
jelord | 96:03106adb45c9 | 285 | 35-38: Home Latitude |
jelord | 96:03106adb45c9 | 286 | 39-42: Home Longitude |
jelord | 96:03106adb45c9 | 287 | 43-46: Work Latitude |
jelord | 96:03106adb45c9 | 288 | 47-50: Work Longitude |
jelord | 96:03106adb45c9 | 289 | 51: Runready: Currently useless, should be 0 |
jelord | 96:03106adb45c9 | 290 | 52-53: Device Calibration |
jelord | 96:03106adb45c9 | 291 | 54: Consider RunReady |
jelord | 96:03106adb45c9 | 292 | 55-56: Menu Options |
jelord | 96:03106adb45c9 | 293 | 57+ Nothing*/ |
caseyquinn | 57:0b554f7aa9a3 | 294 | int main() |
caseyquinn | 57:0b554f7aa9a3 | 295 | { |
joshuasmth04 | 91:f838d9a5b596 | 296 | RGB_LED.set_led(1,1,1); |
caseyquinn | 57:0b554f7aa9a3 | 297 | // Setup and Initialization |
caseyquinn | 57:0b554f7aa9a3 | 298 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 71:78edbceff4fc | 299 | //**************//BLE initialization//**************// |
jelord | 94:c57720890e76 | 300 | RTC.get_time(); |
jelord | 94:c57720890e76 | 301 | uint8_t rtcPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year}; |
jelord | 94:c57720890e76 | 302 | uint8_t sampleTimePassValues[12] = {0,}; |
jelord | 99:229435dd4cfb | 303 | uint8_t subjectLabelOriginal[8] = {0,}; |
jelord | 96:03106adb45c9 | 304 | uint8_t dataLogOriginal[1] = {0,}; |
jelord | 96:03106adb45c9 | 305 | uint8_t flowRateOriginal[4] = {0,}; |
jelord | 102:d25eab697fe0 | 306 | //uint8_t presetRunModeCheck[1] = {0,}; Commented and currently unused to prevent mem issues |
jelord | 94:c57720890e76 | 307 | E2PROM.read(0x00015, sampleTimePassValues, 12); |
jelord | 99:229435dd4cfb | 308 | E2PROM.read(0x00001, subjectLabelOriginal,8); |
jelord | 96:03106adb45c9 | 309 | E2PROM.read(0x00014,dataLogOriginal,1); |
jelord | 96:03106adb45c9 | 310 | E2PROM.read(0x00010,flowRateOriginal,4); |
jelord | 102:d25eab697fe0 | 311 | //E2PROM.read(0x00033,presetRunModeCheck,1); //commented out mem issue |
joshuasmth04 | 77:24fbeb2bfe05 | 312 | ble.init(); |
jelord | 94:c57720890e76 | 313 | ble.onDisconnection(disconnectionCallback); |
jelord | 94:c57720890e76 | 314 | ble.onDataWritten(WrittenHandler); //add writeCharCallback (custom function) to whenever data is being written to device |
jelord | 102:d25eab697fe0 | 315 | UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal,dataLogOriginal,flowRateOriginal); //Create a GattService that is defined in UPAS_Service.h |
jelord | 94:c57720890e76 | 316 | upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service |
jelord | 94:c57720890e76 | 317 | |
jelord | 94:c57720890e76 | 318 | /* setup advertising |
jelord | 94:c57720890e76 | 319 | Following lines do the follow: |
jelord | 94:c57720890e76 | 320 | 1:Declare the device as Bluetooth Smart(Low-Energy) |
jelord | 100:da71436aa52a | 321 | 2.Advertise the UPAS service that will send and receive the 113+ bits of settable values in the UPAS EEPROM |
jelord | 94:c57720890e76 | 322 | 3.Advertise the name that will be associated with the UPAS |
jelord | 94:c57720890e76 | 323 | 4.Allow the UPAS to advertise unrestricted (this might change) */ |
jelord | 94:c57720890e76 | 324 | |
jelord | 94:c57720890e76 | 325 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
jelord | 94:c57720890e76 | 326 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
jelord | 99:229435dd4cfb | 327 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)subjectLabelOriginal, sizeof(subjectLabelOriginal)); |
joshuasmth04 | 71:78edbceff4fc | 328 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
jelord | 94:c57720890e76 | 329 | ble.setAdvertisingInterval(160); /* 160ms. */ |
joshuasmth04 | 77:24fbeb2bfe05 | 330 | ble.startAdvertising(); |
jelord | 94:c57720890e76 | 331 | |
joshuasmth04 | 71:78edbceff4fc | 332 | //**************//BLE initialization//**************// |
jelord | 96:03106adb45c9 | 333 | |
jelord | 96:03106adb45c9 | 334 | //Logic Loop waiting for responses from the BLE iPhone App. |
jelord | 96:03106adb45c9 | 335 | //Will not break loop without response from the app |
jelord | 102:d25eab697fe0 | 336 | |
jelord | 102:d25eab697fe0 | 337 | //outer loop commented out to prevent mem issues |
jelord | 102:d25eab697fe0 | 338 | //if(presetRunModeCheck[0] != 0x0C){ |
jelord | 100:da71436aa52a | 339 | while (1) { |
jelord | 100:da71436aa52a | 340 | ble.waitForEvent(); |
jelord | 100:da71436aa52a | 341 | |
jelord | 100:da71436aa52a | 342 | if(RunReady==10){ //Check to see if app is done with configurations |
jelord | 100:da71436aa52a | 343 | ble.stopAdvertising(); |
jelord | 100:da71436aa52a | 344 | break; |
jelord | 100:da71436aa52a | 345 | } |
jelord | 98:d70ff5a0b2e6 | 346 | |
jelord | 100:da71436aa52a | 347 | if(RunReady==12){ //If 24 hour mode has been set, then shut down the UPAS for automatic start later. |
jelord | 100:da71436aa52a | 348 | pbKill = 0; |
jelord | 100:da71436aa52a | 349 | } |
jelord | 100:da71436aa52a | 350 | } |
jelord | 102:d25eab697fe0 | 351 | //}else{ |
jelord | 102:d25eab697fe0 | 352 | // RunReady = 12; |
jelord | 102:d25eab697fe0 | 353 | // } |
jelord | 98:d70ff5a0b2e6 | 354 | |
jelord | 102:d25eab697fe0 | 355 | //If 24 hour mode was set by app, make sure to clear it for next app cycle...commented out for now |
jelord | 102:d25eab697fe0 | 356 | // presetRunModeCheck[0] = 0x01; |
jelord | 102:d25eab697fe0 | 357 | // E2PROM.write(0x00033,presetRunModeCheck,1); |
jelord | 100:da71436aa52a | 358 | |
jelord | 94:c57720890e76 | 359 | E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM |
jelord | 94:c57720890e76 | 360 | while(!RTC.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 | 94:c57720890e76 | 361 | wait(0.5); |
jelord | 98:d70ff5a0b2e6 | 362 | |
jelord | 94:c57720890e76 | 363 | RTC.get_time(); |
jelord | 94:c57720890e76 | 364 | |
jelord | 100:da71436aa52a | 365 | } |
jelord | 102:d25eab697fe0 | 366 | //Commented out to prevent mem issues |
jelord | 100:da71436aa52a | 367 | //MARK: editting 24 hour mode code here |
jelord | 102:d25eab697fe0 | 368 | //if(RunReady == 12) { |
jelord | 102:d25eab697fe0 | 369 | // RTC.get_time(); |
jelord | 102:d25eab697fe0 | 370 | // startAndEndTime[6] = RTC.seconds; |
jelord | 102:d25eab697fe0 | 371 | // startAndEndTime[7] = RTC.minutes; |
jelord | 102:d25eab697fe0 | 372 | // startAndEndTime[8] = RTC.hour; |
jelord | 102:d25eab697fe0 | 373 | // if(RTC.month == 1 | RTC.month == 3 | RTC.month == 5 | RTC.month == 7 | RTC.month == 8 | RTC.month == 10) { |
jelord | 102:d25eab697fe0 | 374 | // if(RTC.date == 31) { |
jelord | 102:d25eab697fe0 | 375 | // startAndEndTime[9] = 1; |
jelord | 102:d25eab697fe0 | 376 | // startAndEndTime[10] = RTC.month +1; |
jelord | 102:d25eab697fe0 | 377 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 378 | // } else { |
jelord | 102:d25eab697fe0 | 379 | // startAndEndTime[9] = RTC.date+1; |
jelord | 102:d25eab697fe0 | 380 | // startAndEndTime[10] = RTC.month; |
jelord | 102:d25eab697fe0 | 381 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 382 | // } |
jelord | 102:d25eab697fe0 | 383 | // } else if(RTC.month == 4 | RTC.month == 6 | RTC.month == 9 | RTC.month == 11) { |
jelord | 102:d25eab697fe0 | 384 | // if(RTC.date == 30) { |
jelord | 102:d25eab697fe0 | 385 | // startAndEndTime[9] = 1; |
jelord | 102:d25eab697fe0 | 386 | // startAndEndTime[10] = RTC.month +1; |
jelord | 102:d25eab697fe0 | 387 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 388 | // } else { |
jelord | 102:d25eab697fe0 | 389 | // startAndEndTime[9] = RTC.date+1; |
jelord | 102:d25eab697fe0 | 390 | // startAndEndTime[10] = RTC.month; |
jelord | 102:d25eab697fe0 | 391 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 392 | // } |
jelord | 102:d25eab697fe0 | 393 | // } else if(RTC.month == 2) { |
jelord | 102:d25eab697fe0 | 394 | // if(RTC.year == 16 | RTC.year == 20 | RTC.year == 24| RTC.year == 28) { |
jelord | 102:d25eab697fe0 | 395 | // if(RTC.date == 29) { |
jelord | 102:d25eab697fe0 | 396 | // startAndEndTime[9] = 1; |
jelord | 102:d25eab697fe0 | 397 | // startAndEndTime[10] = RTC.month +1; |
jelord | 102:d25eab697fe0 | 398 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 399 | // } else { |
jelord | 102:d25eab697fe0 | 400 | // startAndEndTime[9] = RTC.date+1; |
jelord | 102:d25eab697fe0 | 401 | // startAndEndTime[10] = RTC.month; |
jelord | 102:d25eab697fe0 | 402 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 403 | // } |
jelord | 102:d25eab697fe0 | 404 | // } else { |
jelord | 102:d25eab697fe0 | 405 | // if(RTC.date == 28) { |
jelord | 102:d25eab697fe0 | 406 | // startAndEndTime[9] = 1; |
jelord | 102:d25eab697fe0 | 407 | // startAndEndTime[10] = RTC.month +1; |
jelord | 102:d25eab697fe0 | 408 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 409 | // } else { |
jelord | 102:d25eab697fe0 | 410 | // startAndEndTime[9] = RTC.date+1; |
jelord | 102:d25eab697fe0 | 411 | // startAndEndTime[10] = RTC.month; |
jelord | 102:d25eab697fe0 | 412 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 413 | // } |
jelord | 102:d25eab697fe0 | 414 | // } |
jelord | 102:d25eab697fe0 | 415 | // } else if(RTC.month == 12) { |
jelord | 102:d25eab697fe0 | 416 | // if(RTC.date == 31) { |
jelord | 102:d25eab697fe0 | 417 | // startAndEndTime[9] = 1; |
jelord | 102:d25eab697fe0 | 418 | // startAndEndTime[10] = 1; |
jelord | 102:d25eab697fe0 | 419 | // startAndEndTime[11] = RTC.year+1; |
jelord | 102:d25eab697fe0 | 420 | // } else { |
jelord | 102:d25eab697fe0 | 421 | // startAndEndTime[9] = RTC.date+1; |
jelord | 102:d25eab697fe0 | 422 | // startAndEndTime[10] = RTC.month; |
jelord | 102:d25eab697fe0 | 423 | // startAndEndTime[11] = RTC.year; |
jelord | 102:d25eab697fe0 | 424 | // } |
jelord | 102:d25eab697fe0 | 425 | // } |
jelord | 102:d25eab697fe0 | 426 | // } |
jelord | 100:da71436aa52a | 427 | //MARK: end of 24 hour mode code |
jelord | 100:da71436aa52a | 428 | |
jelord | 98:d70ff5a0b2e6 | 429 | RGB_LED.set_led(0,1,0); |
jelord | 102:d25eab697fe0 | 430 | |
jelord | 102:d25eab697fe0 | 431 | //Get the proper serial number |
jelord | 102:d25eab697fe0 | 432 | E2PROM.read(0x00034, flowRateOriginal,2); |
jelord | 102:d25eab697fe0 | 433 | serial_num = ((uint16_t)flowRateOriginal[1] << 8) | flowRateOriginal[0]; |
jelord | 94:c57720890e76 | 434 | calibrations.initialize(serial_num); |
jelord | 94:c57720890e76 | 435 | blower=1; |
jelord | 97:c596f8ed4562 | 436 | E2PROM.read(0x00014,logIntervalReadOut,1); |
jelord | 97:c596f8ed4562 | 437 | logInerval = logIntervalReadOut[0]; |
jelord | 100:da71436aa52a | 438 | |
caseyquinn | 103:a2d7d946d69c | 439 | //pc.printf("You're done, you can now disconect the USB cable.\r\n"); |
caseyquinn | 84:85d11d422da3 | 440 | RunReady = 0; |
joshuasmth04 | 85:a95dd5f03818 | 441 | |
jelord | 94:c57720890e76 | 442 | |
jelord | 96:03106adb45c9 | 443 | stop.attach(&check_stop, 30); // check if we should shut down every 9 seconds, starting 60s after the start. |
joshuasmth04 | 85:a95dd5f03818 | 444 | |
jelord | 102:d25eab697fe0 | 445 | //Use the flow rate value stored in eeprom |
jelord | 102:d25eab697fe0 | 446 | E2PROM.read(0x00010,flowRateOriginal,4); |
jelord | 102:d25eab697fe0 | 447 | E2PROM.byteToFloat(flowRateOriginal, &volflowSet); |
jelord | 98:d70ff5a0b2e6 | 448 | |
caseyquinn | 81:480f0310ef9a | 449 | if(volflowSet<=1.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 450 | gainFlow = 100; |
caseyquinn | 81:480f0310ef9a | 451 | } else if(volflowSet>=2.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 452 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 453 | } else { |
caseyquinn | 88:9142f21a4715 | 454 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 455 | } |
joshuasmth04 | 59:a9b21b3d9afc | 456 | |
caseyquinn | 57:0b554f7aa9a3 | 457 | RGB_LED.set_led(1,0,0); |
caseyquinn | 57:0b554f7aa9a3 | 458 | press = bmesensor.getPressure(); |
caseyquinn | 57:0b554f7aa9a3 | 459 | temp = bmesensor.getTemperature(); |
caseyquinn | 57:0b554f7aa9a3 | 460 | rh = bmesensor.getHumidity(); |
caseyquinn | 57:0b554f7aa9a3 | 461 | |
caseyquinn | 57:0b554f7aa9a3 | 462 | 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)); |
caseyquinn | 57:0b554f7aa9a3 | 463 | massflowSet = volflowSet*atmoRho; |
caseyquinn | 57:0b554f7aa9a3 | 464 | //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 85:a95dd5f03818 | 465 | |
joshuasmth04 | 85:a95dd5f03818 | 466 | |
joshuasmth04 | 77:24fbeb2bfe05 | 467 | digital_pot_setpoint = (int)floor(calibrations.DP4*pow(massflowSet,4)+calibrations.DP3*pow(massflowSet,3)+calibrations.DP2*pow(massflowSet,2)+calibrations.DP1*massflowSet+calibrations.DP0); //min = 0x7F, max = 0x00 |
joshuasmth04 | 85:a95dd5f03818 | 468 | |
joshuasmth04 | 59:a9b21b3d9afc | 469 | if(digital_pot_setpoint>=digitalpotMax) { |
caseyquinn | 57:0b554f7aa9a3 | 470 | digital_pot_setpoint = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 471 | } else if(digital_pot_setpoint<=digitalpotMin) { |
caseyquinn | 57:0b554f7aa9a3 | 472 | digital_pot_setpoint = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 473 | } |
joshuasmth04 | 59:a9b21b3d9afc | 474 | |
caseyquinn | 57:0b554f7aa9a3 | 475 | DigPot.writeRegister(digital_pot_setpoint); |
caseyquinn | 57:0b554f7aa9a3 | 476 | wait(1); |
caseyquinn | 57:0b554f7aa9a3 | 477 | blower = 1; |
jelord | 102:d25eab697fe0 | 478 | |
jelord | 97:c596f8ed4562 | 479 | |
jelord | 102:d25eab697fe0 | 480 | E2PROM.read(0x00001, subjectLabelOriginal,8); |
jelord | 102:d25eab697fe0 | 481 | //sprintf(filename, "/sd/%c%c%c%c%c%c%c%cLOG_%02d-%02d-%02d_%02d=%02d=%02d.txt",subjectLabelOriginal[0],subjectLabelOriginal[1],subjectLabelOriginal[2],subjectLabelOriginal[3],subjectLabelOriginal[4],subjectLabelOriginal[5],subjectLabelOriginal[6],subjectLabelOriginal[7],RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds); |
jelord | 102:d25eab697fe0 | 482 | sprintf(filename, "/sd/UPAS%04dLOG_%02d-%02d-%02d_%02d=%02d=%02d_%c%c%c%c%c%c%c%c.txt",serial_num,RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,subjectLabelOriginal[0],subjectLabelOriginal[1],subjectLabelOriginal[2],subjectLabelOriginal[3],subjectLabelOriginal[4],subjectLabelOriginal[5],subjectLabelOriginal[6],subjectLabelOriginal[7]); |
caseyquinn | 57:0b554f7aa9a3 | 483 | FILE *fp = fopen(filename, "w"); |
caseyquinn | 57:0b554f7aa9a3 | 484 | fclose(fp); |
joshuasmth04 | 59:a9b21b3d9afc | 485 | |
joshuasmth04 | 59:a9b21b3d9afc | 486 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 487 | //Following lines are needed to enter into the initiallization flow control loop |
joshuasmth04 | 59:a9b21b3d9afc | 488 | |
caseyquinn | 57:0b554f7aa9a3 | 489 | wait(10); |
joshuasmth04 | 59:a9b21b3d9afc | 490 | |
caseyquinn | 57:0b554f7aa9a3 | 491 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
caseyquinn | 57:0b554f7aa9a3 | 492 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 77:24fbeb2bfe05 | 493 | if(omronVolt<=calibrations.omronVMin) { |
joshuasmth04 | 77:24fbeb2bfe05 | 494 | massflow = calibrations.omronMFMin; |
joshuasmth04 | 77:24fbeb2bfe05 | 495 | } else if(omronVolt>=calibrations.omronVMax) { |
joshuasmth04 | 77:24fbeb2bfe05 | 496 | massflow = calibrations.omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 497 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 498 | massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 499 | } |
joshuasmth04 | 59:a9b21b3d9afc | 500 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 501 | digital_pot_set = digital_pot_setpoint; |
joshuasmth04 | 59:a9b21b3d9afc | 502 | wait(5); |
joshuasmth04 | 59:a9b21b3d9afc | 503 | |
joshuasmth04 | 59:a9b21b3d9afc | 504 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 505 | //Sets the flow withen +-1.5% of the desired flow rate based on mass flow |
joshuasmth04 | 59:a9b21b3d9afc | 506 | |
joshuasmth04 | 59:a9b21b3d9afc | 507 | while(abs(deltaMflow)>.015) { |
joshuasmth04 | 77:24fbeb2bfe05 | 508 | |
joshuasmth04 | 59:a9b21b3d9afc | 509 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 510 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 511 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 77:24fbeb2bfe05 | 512 | if(omronVolt<=calibrations.omronVMin) { |
joshuasmth04 | 77:24fbeb2bfe05 | 513 | massflow = calibrations.omronMFMin; |
joshuasmth04 | 77:24fbeb2bfe05 | 514 | } else if(omronVolt>=calibrations.omronVMax) { |
joshuasmth04 | 77:24fbeb2bfe05 | 515 | massflow = calibrations.omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 516 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 517 | massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 518 | } |
caseyquinn | 57:0b554f7aa9a3 | 519 | |
joshuasmth04 | 59:a9b21b3d9afc | 520 | 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)); |
joshuasmth04 | 59:a9b21b3d9afc | 521 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 522 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 523 | deltaMflow = massflow-massflowSet; |
jelord | 94:c57720890e76 | 524 | |
joshuasmth04 | 59:a9b21b3d9afc | 525 | digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 526 | if(digital_pot_set>=digitalpotMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 527 | digital_pot_set = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 528 | } else if(digital_pot_set<=digitalpotMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 529 | digital_pot_set = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 530 | } |
joshuasmth04 | 59:a9b21b3d9afc | 531 | |
joshuasmth04 | 59:a9b21b3d9afc | 532 | wait(2); |
joshuasmth04 | 59:a9b21b3d9afc | 533 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 534 | wait(1); |
joshuasmth04 | 59:a9b21b3d9afc | 535 | |
joshuasmth04 | 59:a9b21b3d9afc | 536 | |
joshuasmth04 | 59:a9b21b3d9afc | 537 | } |
joshuasmth04 | 59:a9b21b3d9afc | 538 | |
joshuasmth04 | 59:a9b21b3d9afc | 539 | sampledVol = 0.0; |
joshuasmth04 | 59:a9b21b3d9afc | 540 | RGB_LED.set_led(0,1,0); |
joshuasmth04 | 77:24fbeb2bfe05 | 541 | |
caseyquinn | 67:300418575137 | 542 | |
joshuasmth04 | 59:a9b21b3d9afc | 543 | |
joshuasmth04 | 59:a9b21b3d9afc | 544 | //** end of initalization **// |
joshuasmth04 | 59:a9b21b3d9afc | 545 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 546 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 547 | // Main Control Loop |
joshuasmth04 | 59:a9b21b3d9afc | 548 | |
caseyquinn | 93:b53a9a7cb8f1 | 549 | |
joshuasmth04 | 62:edc9632bcc43 | 550 | logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c |
joshuasmth04 | 59:a9b21b3d9afc | 551 | |
caseyquinn | 0:14d46ef4b6cb | 552 | } |
caseyquinn | 0:14d46ef4b6cb | 553 |