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@114:84e759e07948, 2016-02-18 (annotated)
- Committer:
- caseyquinn
- Date:
- Thu Feb 18 01:02:00 2016 +0000
- Revision:
- 114:84e759e07948
- Parent:
- 113:846f67efaa80
- Child:
- 115:fb25810c1fab
updated mbed library to rev 96. still having issues with callbacks and also i2c crashing...
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 | 114:84e759e07948 | 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 | #define SERIAL_BAUD_RATE 9600 |
joshuasmth04 | 41:1fb3e0ac6f87 | 17 | #define SCL 20 |
caseyquinn | 7:a24d7156bc02 | 18 | #define SDA 22 |
caseyquinn | 81:480f0310ef9a | 19 | |
caseyquinn | 0:14d46ef4b6cb | 20 | |
joshuasmth04 | 71:78edbceff4fc | 21 | |
jelord | 94:c57720890e76 | 22 | |
joshuasmth04 | 71:78edbceff4fc | 23 | |
jelord | 97:c596f8ed4562 | 24 | uint8_t startAndEndTime[12] = {0,}; |
jelord | 97:c596f8ed4562 | 25 | uint8_t logIntervalReadOut[1] = {0,}; |
jelord | 94:c57720890e76 | 26 | static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID |
joshuasmth04 | 71:78edbceff4fc | 27 | BLEDevice ble; |
jelord | 94:c57720890e76 | 28 | UPAS_Service *upasServicePtr; |
jelord | 94:c57720890e76 | 29 | //BLE_Menu bleMenu(ble, txPayload, rxPayload, uart_base_uuid_rev, uart_base_uuid, uartService); |
joshuasmth04 | 71:78edbceff4fc | 30 | |
joshuasmth04 | 72:0b36575ab00d | 31 | I2C i2c(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 32 | Adafruit_ADS1115 ads(&i2c); |
joshuasmth04 | 72:0b36575ab00d | 33 | MCP40D17 DigPot(&i2c); |
joshuasmth04 | 72:0b36575ab00d | 34 | BME280 bmesensor(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 35 | STC3100 gasG(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 36 | Serial pc(USBTX, USBRX); |
joshuasmth04 | 72:0b36575ab00d | 37 | DigitalOut blower(p29, 0); |
joshuasmth04 | 72:0b36575ab00d | 38 | DigitalOut pbKill(p18, 1); |
caseyquinn | 114:84e759e07948 | 39 | //LSM303 movementsensor(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 40 | SI1145 lightsensor(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 41 | NCP5623BMUTBG RGB_LED(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 42 | CronoDot RTC(p22, p20); |
joshuasmth04 | 72:0b36575ab00d | 43 | EEPROM E2PROM(p22, p20); |
joshuasmth04 | 77:24fbeb2bfe05 | 44 | DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications |
joshuasmth04 | 77:24fbeb2bfe05 | 45 | Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option |
caseyquinn | 57:0b554f7aa9a3 | 46 | |
caseyquinn | 114:84e759e07948 | 47 | //Timeout stop; //This is the stop call back object |
caseyquinn | 114:84e759e07948 | 48 | //Timeout logg; //This is the logging call back object |
caseyquinn | 111:5f2ba5f1a468 | 49 | |
caseyquinn | 110:67980ebdebf6 | 50 | |
caseyquinn | 67:300418575137 | 51 | |
joshuasmth04 | 77:24fbeb2bfe05 | 52 | uint16_t serial_num = 1; // Default serial/calibration number |
joshuasmth04 | 77:24fbeb2bfe05 | 53 | int RunReady =0; |
caseyquinn | 70:81f04e69e08a | 54 | |
caseyquinn | 70:81f04e69e08a | 55 | |
caseyquinn | 20:ad9883973d86 | 56 | float press; |
caseyquinn | 20:ad9883973d86 | 57 | float temp; |
caseyquinn | 20:ad9883973d86 | 58 | float rh; |
caseyquinn | 4:69bd7e8a994c | 59 | |
caseyquinn | 20:ad9883973d86 | 60 | int uv; |
caseyquinn | 20:ad9883973d86 | 61 | int vis; |
caseyquinn | 20:ad9883973d86 | 62 | int ir; |
caseyquinn | 8:204c21adf693 | 63 | |
caseyquinn | 114:84e759e07948 | 64 | /* |
caseyquinn | 67:300418575137 | 65 | float compass; |
caseyquinn | 10:f9cb61b29340 | 66 | float accel_x; |
caseyquinn | 10:f9cb61b29340 | 67 | float accel_y; |
caseyquinn | 10:f9cb61b29340 | 68 | float accel_z; |
lionberg | 52:80480b2fafba | 69 | float accel_comp; |
caseyquinn | 105:f3be9e0314f7 | 70 | float angle_x; |
caseyquinn | 105:f3be9e0314f7 | 71 | float angle_y; |
caseyquinn | 105:f3be9e0314f7 | 72 | float angle_z; |
caseyquinn | 10:f9cb61b29340 | 73 | float mag_x; |
caseyquinn | 10:f9cb61b29340 | 74 | float mag_y; |
caseyquinn | 10:f9cb61b29340 | 75 | float mag_z; |
caseyquinn | 114:84e759e07948 | 76 | */ |
caseyquinn | 10:f9cb61b29340 | 77 | |
caseyquinn | 0:14d46ef4b6cb | 78 | int vInReading; |
caseyquinn | 0:14d46ef4b6cb | 79 | int vBlowerReading; |
caseyquinn | 0:14d46ef4b6cb | 80 | int omronDiff; |
lionberg | 52:80480b2fafba | 81 | float omronVolt; //V |
caseyquinn | 20:ad9883973d86 | 82 | int omronReading; |
lionberg | 52:80480b2fafba | 83 | float atmoRho; //g/L |
caseyquinn | 49:19e828650618 | 84 | |
caseyquinn | 20:ad9883973d86 | 85 | float massflow; //g/min |
caseyquinn | 20:ad9883973d86 | 86 | float volflow; //L/min |
caseyquinn | 67:300418575137 | 87 | float volflowSet = 1.0; //L/min |
caseyquinn | 81:480f0310ef9a | 88 | int logInerval = 10; //seconds |
caseyquinn | 57:0b554f7aa9a3 | 89 | double secondsD = 0; |
caseyquinn | 105:f3be9e0314f7 | 90 | double lastsecondD = 0; |
joshuasmth04 | 41:1fb3e0ac6f87 | 91 | float massflowSet; |
caseyquinn | 42:fc2f2b9f07ae | 92 | float deltaVflow = 0.0; |
caseyquinn | 42:fc2f2b9f07ae | 93 | float deltaMflow = 0.0; |
caseyquinn | 47:3146e8c949a9 | 94 | float gainFlow; |
lionberg | 52:80480b2fafba | 95 | float sampledVol; //L, total sampled volume |
caseyquinn | 20:ad9883973d86 | 96 | |
caseyquinn | 55:f24d70f519cd | 97 | int digital_pot_setpoint; //min = 0x7F, max = 0x00 |
caseyquinn | 55:f24d70f519cd | 98 | int digital_pot_set; |
caseyquinn | 55:f24d70f519cd | 99 | int digital_pot_change; |
caseyquinn | 55:f24d70f519cd | 100 | int digitalpotMax = 127; |
caseyquinn | 55:f24d70f519cd | 101 | int digitalpotMin = 2; |
caseyquinn | 55:f24d70f519cd | 102 | |
joshuasmth04 | 59:a9b21b3d9afc | 103 | int dutyUp; |
joshuasmth04 | 59:a9b21b3d9afc | 104 | int dutyDown; |
joshuasmth04 | 59:a9b21b3d9afc | 105 | |
joshuasmth04 | 72:0b36575ab00d | 106 | // variables are only place holders for the US_Menu // |
joshuasmth04 | 72:0b36575ab00d | 107 | int refreshtime; |
joshuasmth04 | 72:0b36575ab00d | 108 | float home_lat, home_lon, work_lat, work_lon; |
joshuasmth04 | 77:24fbeb2bfe05 | 109 | //*************************************************// |
joshuasmth04 | 72:0b36575ab00d | 110 | |
caseyquinn | 67:300418575137 | 111 | //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 | 112 | |
caseyquinn | 81:480f0310ef9a | 113 | char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt"; |
caseyquinn | 7:a24d7156bc02 | 114 | 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 | 115 | |
caseyquinn | 7:a24d7156bc02 | 116 | |
joshuasmth04 | 71:78edbceff4fc | 117 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
joshuasmth04 | 71:78edbceff4fc | 118 | { |
jelord | 94:c57720890e76 | 119 | ble.startAdvertising(); |
joshuasmth04 | 71:78edbceff4fc | 120 | } |
joshuasmth04 | 71:78edbceff4fc | 121 | |
joshuasmth04 | 71:78edbceff4fc | 122 | void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) // called any time the phone sends a message |
joshuasmth04 | 71:78edbceff4fc | 123 | { |
jelord | 94:c57720890e76 | 124 | uint8_t *writeData = const_cast<uint8_t*>(Handler->data); |
jelord | 94:c57720890e76 | 125 | // check to see what characteristic was written, by handle |
jelord | 94:c57720890e76 | 126 | if(Handler->charHandle == upasServicePtr->rtcCharacteristic.getValueAttribute().getHandle()) { |
jelord | 94:c57720890e76 | 127 | |
jelord | 94:c57720890e76 | 128 | |
jelord | 94:c57720890e76 | 129 | |
jelord | 94:c57720890e76 | 130 | E2PROM.write(0x00015, writeData+6, 12); |
jelord | 94:c57720890e76 | 131 | RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]); |
jelord | 94:c57720890e76 | 132 | |
jelord | 94:c57720890e76 | 133 | }else if(Handler->charHandle == upasServicePtr->sampleTimeCharacteristic.getValueAttribute().getHandle()){ |
jelord | 94:c57720890e76 | 134 | |
jelord | 94:c57720890e76 | 135 | E2PROM.write(0x00015, writeData, 12); |
jelord | 94:c57720890e76 | 136 | |
jelord | 94:c57720890e76 | 137 | }else if(Handler->charHandle == upasServicePtr->subjectLabelCharacteristic.getValueAttribute().getHandle()){ |
jelord | 94:c57720890e76 | 138 | E2PROM.write(0x00001,writeData,15); |
jelord | 94:c57720890e76 | 139 | |
jelord | 94:c57720890e76 | 140 | }else if(Handler->charHandle == upasServicePtr->runReadyCharacteristic.getValueAttribute().getHandle()){ |
jelord | 100:da71436aa52a | 141 | |
jelord | 94:c57720890e76 | 142 | uint8_t runData = writeData[0]; |
jelord | 100:da71436aa52a | 143 | E2PROM.write(0x00033,writeData,1); |
jelord | 94:c57720890e76 | 144 | |
jelord | 100:da71436aa52a | 145 | if(runData == 0x0A){ |
jelord | 100:da71436aa52a | 146 | RunReady = 10; |
jelord | 100:da71436aa52a | 147 | }else{ |
jelord | 100:da71436aa52a | 148 | RunReady = 12; |
jelord | 100:da71436aa52a | 149 | } |
jelord | 94:c57720890e76 | 150 | |
jelord | 96:03106adb45c9 | 151 | }else if(Handler->charHandle == upasServicePtr->logIntevalCharacteristic.getValueAttribute().getHandle()){ |
jelord | 94:c57720890e76 | 152 | /* Trigger demo mode*/ |
jelord | 96:03106adb45c9 | 153 | //RGB_LED.set_led(3,1,0); |
jelord | 96:03106adb45c9 | 154 | E2PROM.write(0x00014,writeData,1); |
jelord | 96:03106adb45c9 | 155 | |
jelord | 96:03106adb45c9 | 156 | }else if(Handler->charHandle == upasServicePtr->flowRateCharacteristic.getValueAttribute().getHandle()){ |
jelord | 96:03106adb45c9 | 157 | //RGB_LED.set_led(3,1,0); |
jelord | 98:d70ff5a0b2e6 | 158 | E2PROM.write(0x00010,writeData,4); |
jelord | 98:d70ff5a0b2e6 | 159 | |
jelord | 98:d70ff5a0b2e6 | 160 | //float volFlowTester = 0.0; |
jelord | 98:d70ff5a0b2e6 | 161 | //uint8_t flowRateTestBytes[4] = {0,}; |
jelord | 98:d70ff5a0b2e6 | 162 | //E2PROM.read(0x00010,flowRateTestBytes,4); |
jelord | 98:d70ff5a0b2e6 | 163 | //E2PROM.byteToFloat(flowRateTestBytes, &volFlowTester); |
jelord | 98:d70ff5a0b2e6 | 164 | |
jelord | 104:c57913399e79 | 165 | }else if(Handler->charHandle == upasServicePtr->serialNumCharacteristic.getValueAttribute().getHandle()){ |
jelord | 104:c57913399e79 | 166 | E2PROM.write(0x00034,writeData,2); |
jelord | 94:c57720890e76 | 167 | } |
jelord | 104:c57913399e79 | 168 | |
joshuasmth04 | 71:78edbceff4fc | 169 | } |
joshuasmth04 | 71:78edbceff4fc | 170 | |
caseyquinn | 113:846f67efaa80 | 171 | /* |
joshuasmth04 | 62:edc9632bcc43 | 172 | void check_stop() // this checks if it's time to stop and shutdown |
caseyquinn | 57:0b554f7aa9a3 | 173 | { |
jelord | 94:c57720890e76 | 174 | |
jelord | 94:c57720890e76 | 175 | if(RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) { |
caseyquinn | 57:0b554f7aa9a3 | 176 | pbKill = 0; // this is were we shut everything down |
joshuasmth04 | 92:bb36c4bedb8e | 177 | pc.printf("If you're reading this something has gone very wrong."); |
caseyquinn | 57:0b554f7aa9a3 | 178 | } |
caseyquinn | 57:0b554f7aa9a3 | 179 | stop.detach(); |
joshuasmth04 | 77:24fbeb2bfe05 | 180 | stop.attach(&check_stop, 9); |
jelord | 97:c596f8ed4562 | 181 | |
joshuasmth04 | 62:edc9632bcc43 | 182 | } |
caseyquinn | 58:7239c2ab2b65 | 183 | |
caseyquinn | 114:84e759e07948 | 184 | //int r =0; |
caseyquinn | 114:84e759e07948 | 185 | //int g =1; |
caseyquinn | 114:84e759e07948 | 186 | //int b=1; |
caseyquinn | 57:0b554f7aa9a3 | 187 | void log_data() |
joshuasmth04 | 59:a9b21b3d9afc | 188 | { |
caseyquinn | 114:84e759e07948 | 189 | logg.detach(); |
caseyquinn | 114:84e759e07948 | 190 | logg.attach(&log_data, logInerval); // reading and logging data must take significintly less than 0.5s. This can be increased. |
caseyquinn | 114:84e759e07948 | 191 | // if(r==0) r=1; |
caseyquinn | 114:84e759e07948 | 192 | //else r=0; |
caseyquinn | 114:84e759e07948 | 193 | //RGB_LED.set_led(r,g,b); |
caseyquinn | 114:84e759e07948 | 194 | //wait(0.5); |
caseyquinn | 105:f3be9e0314f7 | 195 | |
joshuasmth04 | 59:a9b21b3d9afc | 196 | RTC.get_time(); |
caseyquinn | 105:f3be9e0314f7 | 197 | |
joshuasmth04 | 59:a9b21b3d9afc | 198 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 199 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 200 | |
joshuasmth04 | 77:24fbeb2bfe05 | 201 | if(omronVolt<=calibrations.omronVMin) { |
joshuasmth04 | 77:24fbeb2bfe05 | 202 | massflow = calibrations.omronMFMin; |
joshuasmth04 | 77:24fbeb2bfe05 | 203 | } else if(omronVolt>=calibrations.omronVMax) { |
joshuasmth04 | 77:24fbeb2bfe05 | 204 | massflow = calibrations.omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 205 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 206 | 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 | 207 | } |
joshuasmth04 | 59:a9b21b3d9afc | 208 | |
joshuasmth04 | 59:a9b21b3d9afc | 209 | 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 | 210 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 211 | sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow); |
joshuasmth04 | 59:a9b21b3d9afc | 212 | deltaVflow = volflow-volflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 213 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 214 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 215 | if(abs(deltaMflow)>.025) { |
joshuasmth04 | 59:a9b21b3d9afc | 216 | digital_pot_change = (int)(gainFlow*deltaMflow); |
joshuasmth04 | 59:a9b21b3d9afc | 217 | |
joshuasmth04 | 59:a9b21b3d9afc | 218 | |
joshuasmth04 | 59:a9b21b3d9afc | 219 | if(abs(digital_pot_change)>=50) { |
joshuasmth04 | 59:a9b21b3d9afc | 220 | digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 221 | RGB_LED.set_led(1,0,0); |
caseyquinn | 105:f3be9e0314f7 | 222 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 223 | digital_pot_set = (digital_pot_set+ digital_pot_change); |
joshuasmth04 | 59:a9b21b3d9afc | 224 | RGB_LED.set_led(1,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 225 | } |
caseyquinn | 105:f3be9e0314f7 | 226 | |
caseyquinn | 105:f3be9e0314f7 | 227 | if(digital_pot_set>=digitalpotMax) { |
caseyquinn | 105:f3be9e0314f7 | 228 | digital_pot_set = digitalpotMax; |
caseyquinn | 105:f3be9e0314f7 | 229 | RGB_LED.set_led(1,0,0); |
caseyquinn | 105:f3be9e0314f7 | 230 | } else if(digital_pot_set<=digitalpotMin) { |
caseyquinn | 105:f3be9e0314f7 | 231 | digital_pot_set = digitalpotMin; |
caseyquinn | 105:f3be9e0314f7 | 232 | RGB_LED.set_led(1,0,0); |
caseyquinn | 105:f3be9e0314f7 | 233 | } |
caseyquinn | 105:f3be9e0314f7 | 234 | |
joshuasmth04 | 59:a9b21b3d9afc | 235 | |
joshuasmth04 | 59:a9b21b3d9afc | 236 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 237 | |
joshuasmth04 | 59:a9b21b3d9afc | 238 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 239 | RGB_LED.set_led(0,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 240 | } |
caseyquinn | 106:3911b3c7a24e | 241 | |
joshuasmth04 | 59:a9b21b3d9afc | 242 | movementsensor.getACCEL(); |
joshuasmth04 | 59:a9b21b3d9afc | 243 | movementsensor.getCOMPASS(); |
caseyquinn | 67:300418575137 | 244 | compass = movementsensor.getCOMPASS_HEADING(); |
joshuasmth04 | 59:a9b21b3d9afc | 245 | accel_x = movementsensor.AccelData.x; |
joshuasmth04 | 59:a9b21b3d9afc | 246 | accel_y = movementsensor.AccelData.y; |
joshuasmth04 | 59:a9b21b3d9afc | 247 | accel_z = movementsensor.AccelData.z; |
joshuasmth04 | 59:a9b21b3d9afc | 248 | accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0; |
joshuasmth04 | 59:a9b21b3d9afc | 249 | mag_x = movementsensor.MagData.x; |
joshuasmth04 | 59:a9b21b3d9afc | 250 | mag_y = movementsensor.MagData.y; |
joshuasmth04 | 59:a9b21b3d9afc | 251 | mag_z = movementsensor.MagData.z; |
caseyquinn | 106:3911b3c7a24e | 252 | |
joshuasmth04 | 59:a9b21b3d9afc | 253 | vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 |
joshuasmth04 | 59:a9b21b3d9afc | 254 | vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 |
joshuasmth04 | 59:a9b21b3d9afc | 255 | omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 |
joshuasmth04 | 59:a9b21b3d9afc | 256 | press = bmesensor.getPressure(); |
joshuasmth04 | 59:a9b21b3d9afc | 257 | temp = bmesensor.getTemperature()-5.0; |
joshuasmth04 | 59:a9b21b3d9afc | 258 | rh = bmesensor.getHumidity(); |
joshuasmth04 | 59:a9b21b3d9afc | 259 | uv = lightsensor.getUV(); |
joshuasmth04 | 59:a9b21b3d9afc | 260 | vis = lightsensor.getVIS(); |
joshuasmth04 | 59:a9b21b3d9afc | 261 | ir = lightsensor.getIR(); |
caseyquinn | 106:3911b3c7a24e | 262 | |
caseyquinn | 108:2cf8d5159fc6 | 263 | |
caseyquinn | 108:2cf8d5159fc6 | 264 | FILE *fp = fopen(filename, "a"); |
caseyquinn | 108:2cf8d5159fc6 | 265 | fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds); |
caseyquinn | 108:2cf8d5159fc6 | 266 | fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho); |
caseyquinn | 108:2cf8d5159fc6 | 267 | 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 | 108:2cf8d5159fc6 | 268 | fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass); |
caseyquinn | 108:2cf8d5159fc6 | 269 | fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps()); |
caseyquinn | 108:2cf8d5159fc6 | 270 | fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow); |
caseyquinn | 108:2cf8d5159fc6 | 271 | fclose(fp); |
caseyquinn | 108:2cf8d5159fc6 | 272 | |
joshuasmth04 | 59:a9b21b3d9afc | 273 | } |
caseyquinn | 113:846f67efaa80 | 274 | */ |
caseyquinn | 113:846f67efaa80 | 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 | 104:c57913399e79 | 302 | |
jelord | 104:c57913399e79 | 303 | uint8_t sampleTimePassValues[12] = {0x00,0x00,0x0A,0x01,0x01,0x10,0x00,0x00,0x0A,0x01,0x01,0x10}; //Start 1/1/16 12:00:00 End 1/1/16 12:00:00 |
jelord | 104:c57913399e79 | 304 | uint8_t subjectLabelOriginal[8] = {0x52,0x45,0x53,0x45,0x54,0x5F,0x5F,0x5f}; // RESET___ |
jelord | 104:c57913399e79 | 305 | uint8_t dataLogOriginal[1] = {0x0A,}; //10 seconds |
jelord | 104:c57913399e79 | 306 | uint8_t flowRateOriginal[4] = {0x00,0x00,0x80,0x3F}; //1.0 LPM |
jelord | 104:c57913399e79 | 307 | |
jelord | 104:c57913399e79 | 308 | uint8_t serialNumberAndType[6] = {0x50,0x53}; |
jelord | 104:c57913399e79 | 309 | |
jelord | 104:c57913399e79 | 310 | //Code/conditional set up to prevent bad values in the EEPROM. Does not set serial number currently |
jelord | 104:c57913399e79 | 311 | uint8_t eepromFlag[1] = {0,}; |
jelord | 104:c57913399e79 | 312 | E2PROM.read(0x00071,eepromFlag,1); |
jelord | 109:33d7fc6c1b9b | 313 | if(eepromFlag[0]!= 0x0B){ |
jelord | 104:c57913399e79 | 314 | E2PROM.write(0x00015, sampleTimePassValues, 12); |
jelord | 104:c57913399e79 | 315 | E2PROM.write(0x00001, subjectLabelOriginal,8); |
jelord | 104:c57913399e79 | 316 | E2PROM.write(0x00014,dataLogOriginal,1); |
jelord | 104:c57913399e79 | 317 | E2PROM.write(0x00010,flowRateOriginal,4); |
jelord | 104:c57913399e79 | 318 | |
jelord | 109:33d7fc6c1b9b | 319 | eepromFlag[0] = 0x0B; |
jelord | 104:c57913399e79 | 320 | E2PROM.write(0x00071,eepromFlag,1); |
jelord | 104:c57913399e79 | 321 | } |
jelord | 104:c57913399e79 | 322 | |
jelord | 104:c57913399e79 | 323 | |
jelord | 104:c57913399e79 | 324 | //Retrieve current eeprom values |
jelord | 94:c57720890e76 | 325 | E2PROM.read(0x00015, sampleTimePassValues, 12); |
jelord | 99:229435dd4cfb | 326 | E2PROM.read(0x00001, subjectLabelOriginal,8); |
jelord | 96:03106adb45c9 | 327 | E2PROM.read(0x00014,dataLogOriginal,1); |
jelord | 96:03106adb45c9 | 328 | E2PROM.read(0x00010,flowRateOriginal,4); |
jelord | 104:c57913399e79 | 329 | |
jelord | 104:c57913399e79 | 330 | |
jelord | 104:c57913399e79 | 331 | //Following 12 lines of code parses the serial number from a byte id to a readable set of characters for the app user to see |
jelord | 104:c57913399e79 | 332 | E2PROM.read(0x00034,serialNumberAndType+2,2); |
jelord | 104:c57913399e79 | 333 | int tempSerialNum = serialNumberAndType[2]+serialNumberAndType[3]; |
jelord | 104:c57913399e79 | 334 | int serialNumDigits[4]; |
jelord | 104:c57913399e79 | 335 | serialNumDigits[0] = tempSerialNum / 1000 % 10; |
jelord | 104:c57913399e79 | 336 | serialNumDigits[1] = tempSerialNum / 100 % 10; |
jelord | 104:c57913399e79 | 337 | serialNumDigits[2] = tempSerialNum / 10 % 10; |
jelord | 104:c57913399e79 | 338 | serialNumDigits[3] = tempSerialNum % 10; |
jelord | 104:c57913399e79 | 339 | |
jelord | 104:c57913399e79 | 340 | serialNumberAndType[2] = serialNumDigits[0]+48; |
jelord | 104:c57913399e79 | 341 | serialNumberAndType[3] = serialNumDigits[1]+48; |
jelord | 104:c57913399e79 | 342 | serialNumberAndType[4] = serialNumDigits[2]+48; |
jelord | 104:c57913399e79 | 343 | serialNumberAndType[5] = serialNumDigits[3]+48; |
jelord | 104:c57913399e79 | 344 | |
jelord | 104:c57913399e79 | 345 | |
jelord | 102:d25eab697fe0 | 346 | //E2PROM.read(0x00033,presetRunModeCheck,1); //commented out mem issue |
joshuasmth04 | 77:24fbeb2bfe05 | 347 | ble.init(); |
jelord | 94:c57720890e76 | 348 | ble.onDisconnection(disconnectionCallback); |
jelord | 94:c57720890e76 | 349 | ble.onDataWritten(WrittenHandler); //add writeCharCallback (custom function) to whenever data is being written to device |
jelord | 102:d25eab697fe0 | 350 | UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal,dataLogOriginal,flowRateOriginal); //Create a GattService that is defined in UPAS_Service.h |
jelord | 94:c57720890e76 | 351 | upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service |
jelord | 94:c57720890e76 | 352 | |
jelord | 94:c57720890e76 | 353 | /* setup advertising |
jelord | 94:c57720890e76 | 354 | Following lines do the follow: |
jelord | 94:c57720890e76 | 355 | 1:Declare the device as Bluetooth Smart(Low-Energy) |
jelord | 100:da71436aa52a | 356 | 2.Advertise the UPAS service that will send and receive the 113+ bits of settable values in the UPAS EEPROM |
jelord | 94:c57720890e76 | 357 | 3.Advertise the name that will be associated with the UPAS |
jelord | 94:c57720890e76 | 358 | 4.Allow the UPAS to advertise unrestricted (this might change) */ |
jelord | 94:c57720890e76 | 359 | |
jelord | 94:c57720890e76 | 360 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
jelord | 94:c57720890e76 | 361 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
jelord | 104:c57913399e79 | 362 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)serialNumberAndType, sizeof(serialNumberAndType)); |
joshuasmth04 | 71:78edbceff4fc | 363 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
jelord | 94:c57720890e76 | 364 | ble.setAdvertisingInterval(160); /* 160ms. */ |
joshuasmth04 | 77:24fbeb2bfe05 | 365 | ble.startAdvertising(); |
jelord | 94:c57720890e76 | 366 | |
joshuasmth04 | 71:78edbceff4fc | 367 | //**************//BLE initialization//**************// |
jelord | 96:03106adb45c9 | 368 | |
jelord | 96:03106adb45c9 | 369 | //Logic Loop waiting for responses from the BLE iPhone App. |
jelord | 96:03106adb45c9 | 370 | //Will not break loop without response from the app |
jelord | 102:d25eab697fe0 | 371 | |
jelord | 102:d25eab697fe0 | 372 | //outer loop commented out to prevent mem issues |
jelord | 102:d25eab697fe0 | 373 | //if(presetRunModeCheck[0] != 0x0C){ |
caseyquinn | 106:3911b3c7a24e | 374 | |
jelord | 100:da71436aa52a | 375 | while (1) { |
jelord | 100:da71436aa52a | 376 | ble.waitForEvent(); |
jelord | 100:da71436aa52a | 377 | |
jelord | 100:da71436aa52a | 378 | if(RunReady==10){ //Check to see if app is done with configurations |
jelord | 100:da71436aa52a | 379 | ble.stopAdvertising(); |
jelord | 100:da71436aa52a | 380 | break; |
jelord | 100:da71436aa52a | 381 | } |
jelord | 98:d70ff5a0b2e6 | 382 | |
jelord | 100:da71436aa52a | 383 | if(RunReady==12){ //If 24 hour mode has been set, then shut down the UPAS for automatic start later. |
jelord | 100:da71436aa52a | 384 | pbKill = 0; |
jelord | 100:da71436aa52a | 385 | } |
jelord | 100:da71436aa52a | 386 | } |
jelord | 100:da71436aa52a | 387 | |
jelord | 94:c57720890e76 | 388 | E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM |
caseyquinn | 113:846f67efaa80 | 389 | RGB_LED.set_led(1,0,1); |
jelord | 94:c57720890e76 | 390 | 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 | 391 | wait(0.5); |
jelord | 98:d70ff5a0b2e6 | 392 | |
jelord | 94:c57720890e76 | 393 | RTC.get_time(); |
jelord | 94:c57720890e76 | 394 | |
jelord | 100:da71436aa52a | 395 | } |
caseyquinn | 106:3911b3c7a24e | 396 | |
caseyquinn | 106:3911b3c7a24e | 397 | |
jelord | 98:d70ff5a0b2e6 | 398 | RGB_LED.set_led(0,1,0); |
jelord | 102:d25eab697fe0 | 399 | |
jelord | 102:d25eab697fe0 | 400 | //Get the proper serial number |
jelord | 102:d25eab697fe0 | 401 | E2PROM.read(0x00034, flowRateOriginal,2); |
jelord | 102:d25eab697fe0 | 402 | serial_num = ((uint16_t)flowRateOriginal[1] << 8) | flowRateOriginal[0]; |
caseyquinn | 107:5e3364e42214 | 403 | //---------------------------------------------- |
jelord | 109:33d7fc6c1b9b | 404 | calibrations.initialize(serial_num); |
jelord | 109:33d7fc6c1b9b | 405 | pc.printf("%f\n",calibrations.MF4); |
caseyquinn | 107:5e3364e42214 | 406 | //---------------------------------------------- |
caseyquinn | 106:3911b3c7a24e | 407 | wait(1); |
jelord | 94:c57720890e76 | 408 | blower=1; |
jelord | 97:c596f8ed4562 | 409 | E2PROM.read(0x00014,logIntervalReadOut,1); |
jelord | 97:c596f8ed4562 | 410 | logInerval = logIntervalReadOut[0]; |
jelord | 100:da71436aa52a | 411 | |
joshuasmth04 | 89:047e8558fd09 | 412 | pc.printf("You're done, you can now disconect the USB cable.\r\n"); |
caseyquinn | 84:85d11d422da3 | 413 | RunReady = 0; |
joshuasmth04 | 85:a95dd5f03818 | 414 | |
jelord | 94:c57720890e76 | 415 | |
caseyquinn | 114:84e759e07948 | 416 | //stop.attach(&check_stop, 30); // check if we should shut down every 30 seconds, starting 60s after the start. |
joshuasmth04 | 85:a95dd5f03818 | 417 | |
jelord | 102:d25eab697fe0 | 418 | //Use the flow rate value stored in eeprom |
jelord | 102:d25eab697fe0 | 419 | E2PROM.read(0x00010,flowRateOriginal,4); |
jelord | 102:d25eab697fe0 | 420 | E2PROM.byteToFloat(flowRateOriginal, &volflowSet); |
jelord | 98:d70ff5a0b2e6 | 421 | |
caseyquinn | 81:480f0310ef9a | 422 | if(volflowSet<=1.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 423 | gainFlow = 100; |
caseyquinn | 81:480f0310ef9a | 424 | } else if(volflowSet>=2.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 425 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 426 | } else { |
caseyquinn | 88:9142f21a4715 | 427 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 428 | } |
joshuasmth04 | 59:a9b21b3d9afc | 429 | |
caseyquinn | 57:0b554f7aa9a3 | 430 | RGB_LED.set_led(1,0,0); |
caseyquinn | 57:0b554f7aa9a3 | 431 | press = bmesensor.getPressure(); |
caseyquinn | 57:0b554f7aa9a3 | 432 | temp = bmesensor.getTemperature(); |
caseyquinn | 57:0b554f7aa9a3 | 433 | rh = bmesensor.getHumidity(); |
caseyquinn | 57:0b554f7aa9a3 | 434 | |
caseyquinn | 57:0b554f7aa9a3 | 435 | 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 | 436 | massflowSet = volflowSet*atmoRho; |
caseyquinn | 57:0b554f7aa9a3 | 437 | //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 85:a95dd5f03818 | 438 | |
joshuasmth04 | 85:a95dd5f03818 | 439 | |
joshuasmth04 | 77:24fbeb2bfe05 | 440 | 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 | 441 | |
joshuasmth04 | 59:a9b21b3d9afc | 442 | if(digital_pot_setpoint>=digitalpotMax) { |
caseyquinn | 57:0b554f7aa9a3 | 443 | digital_pot_setpoint = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 444 | } else if(digital_pot_setpoint<=digitalpotMin) { |
caseyquinn | 57:0b554f7aa9a3 | 445 | digital_pot_setpoint = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 446 | } |
joshuasmth04 | 59:a9b21b3d9afc | 447 | |
caseyquinn | 57:0b554f7aa9a3 | 448 | DigPot.writeRegister(digital_pot_setpoint); |
caseyquinn | 57:0b554f7aa9a3 | 449 | wait(1); |
caseyquinn | 57:0b554f7aa9a3 | 450 | blower = 1; |
jelord | 102:d25eab697fe0 | 451 | |
jelord | 97:c596f8ed4562 | 452 | |
jelord | 102:d25eab697fe0 | 453 | E2PROM.read(0x00001, subjectLabelOriginal,8); |
jelord | 102:d25eab697fe0 | 454 | //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 | 455 | 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 | 456 | FILE *fp = fopen(filename, "w"); |
caseyquinn | 57:0b554f7aa9a3 | 457 | fclose(fp); |
joshuasmth04 | 59:a9b21b3d9afc | 458 | |
joshuasmth04 | 59:a9b21b3d9afc | 459 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 460 | //Following lines are needed to enter into the initiallization flow control loop |
joshuasmth04 | 59:a9b21b3d9afc | 461 | |
caseyquinn | 57:0b554f7aa9a3 | 462 | wait(10); |
joshuasmth04 | 59:a9b21b3d9afc | 463 | |
caseyquinn | 57:0b554f7aa9a3 | 464 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
caseyquinn | 57:0b554f7aa9a3 | 465 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 77:24fbeb2bfe05 | 466 | if(omronVolt<=calibrations.omronVMin) { |
joshuasmth04 | 77:24fbeb2bfe05 | 467 | massflow = calibrations.omronMFMin; |
joshuasmth04 | 77:24fbeb2bfe05 | 468 | } else if(omronVolt>=calibrations.omronVMax) { |
joshuasmth04 | 77:24fbeb2bfe05 | 469 | massflow = calibrations.omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 470 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 471 | 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 | 472 | } |
joshuasmth04 | 59:a9b21b3d9afc | 473 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 474 | digital_pot_set = digital_pot_setpoint; |
joshuasmth04 | 59:a9b21b3d9afc | 475 | wait(5); |
joshuasmth04 | 59:a9b21b3d9afc | 476 | |
joshuasmth04 | 59:a9b21b3d9afc | 477 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 478 | //Sets the flow withen +-1.5% of the desired flow rate based on mass flow |
joshuasmth04 | 59:a9b21b3d9afc | 479 | |
joshuasmth04 | 59:a9b21b3d9afc | 480 | while(abs(deltaMflow)>.015) { |
joshuasmth04 | 77:24fbeb2bfe05 | 481 | |
joshuasmth04 | 59:a9b21b3d9afc | 482 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 483 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 484 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 77:24fbeb2bfe05 | 485 | if(omronVolt<=calibrations.omronVMin) { |
joshuasmth04 | 77:24fbeb2bfe05 | 486 | massflow = calibrations.omronMFMin; |
joshuasmth04 | 77:24fbeb2bfe05 | 487 | } else if(omronVolt>=calibrations.omronVMax) { |
joshuasmth04 | 77:24fbeb2bfe05 | 488 | massflow = calibrations.omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 489 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 490 | 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 | 491 | } |
caseyquinn | 57:0b554f7aa9a3 | 492 | |
joshuasmth04 | 59:a9b21b3d9afc | 493 | 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 | 494 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 495 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 496 | deltaMflow = massflow-massflowSet; |
jelord | 94:c57720890e76 | 497 | |
joshuasmth04 | 59:a9b21b3d9afc | 498 | digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 499 | if(digital_pot_set>=digitalpotMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 500 | digital_pot_set = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 501 | } else if(digital_pot_set<=digitalpotMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 502 | digital_pot_set = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 503 | } |
joshuasmth04 | 59:a9b21b3d9afc | 504 | |
joshuasmth04 | 59:a9b21b3d9afc | 505 | wait(2); |
joshuasmth04 | 59:a9b21b3d9afc | 506 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 507 | wait(1); |
joshuasmth04 | 59:a9b21b3d9afc | 508 | |
joshuasmth04 | 59:a9b21b3d9afc | 509 | |
joshuasmth04 | 59:a9b21b3d9afc | 510 | } |
joshuasmth04 | 59:a9b21b3d9afc | 511 | |
joshuasmth04 | 59:a9b21b3d9afc | 512 | sampledVol = 0.0; |
joshuasmth04 | 59:a9b21b3d9afc | 513 | RGB_LED.set_led(0,1,0); |
caseyquinn | 114:84e759e07948 | 514 | |
caseyquinn | 114:84e759e07948 | 515 | // logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c |
joshuasmth04 | 77:24fbeb2bfe05 | 516 | |
caseyquinn | 67:300418575137 | 517 | |
jelord | 112:fd14d51e3493 | 518 | while(1){ |
jelord | 112:fd14d51e3493 | 519 | if(RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) |
jelord | 112:fd14d51e3493 | 520 | pbKill = 0; // this is were we shut everything down |
jelord | 112:fd14d51e3493 | 521 | |
jelord | 112:fd14d51e3493 | 522 | wait(logInerval - 0.2); |
jelord | 112:fd14d51e3493 | 523 | // if(r==0) r=1; |
jelord | 112:fd14d51e3493 | 524 | // else r=0; |
jelord | 112:fd14d51e3493 | 525 | // RGB_LED.set_led(r,g,b); |
jelord | 112:fd14d51e3493 | 526 | // wait(0.5); |
jelord | 112:fd14d51e3493 | 527 | |
jelord | 112:fd14d51e3493 | 528 | RTC.get_time(); |
jelord | 112:fd14d51e3493 | 529 | |
jelord | 112:fd14d51e3493 | 530 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
jelord | 112:fd14d51e3493 | 531 | omronVolt = (omronReading*4.096)/(32768*2); |
jelord | 112:fd14d51e3493 | 532 | |
jelord | 112:fd14d51e3493 | 533 | if(omronVolt<=calibrations.omronVMin) { |
jelord | 112:fd14d51e3493 | 534 | massflow = calibrations.omronMFMin; |
jelord | 112:fd14d51e3493 | 535 | } else if(omronVolt>=calibrations.omronVMax) { |
jelord | 112:fd14d51e3493 | 536 | massflow = calibrations.omronMFMax; |
jelord | 112:fd14d51e3493 | 537 | } else { |
jelord | 112:fd14d51e3493 | 538 | massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0; |
jelord | 112:fd14d51e3493 | 539 | } |
jelord | 112:fd14d51e3493 | 540 | |
jelord | 112:fd14d51e3493 | 541 | 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 | 112:fd14d51e3493 | 542 | volflow = massflow/atmoRho; |
jelord | 112:fd14d51e3493 | 543 | sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow); |
jelord | 112:fd14d51e3493 | 544 | deltaVflow = volflow-volflowSet; |
jelord | 112:fd14d51e3493 | 545 | massflowSet = volflowSet*atmoRho; |
jelord | 112:fd14d51e3493 | 546 | deltaMflow = massflow-massflowSet; |
jelord | 112:fd14d51e3493 | 547 | if(abs(deltaMflow)>.025) { |
jelord | 112:fd14d51e3493 | 548 | digital_pot_change = (int)(gainFlow*deltaMflow); |
jelord | 112:fd14d51e3493 | 549 | |
jelord | 112:fd14d51e3493 | 550 | |
jelord | 112:fd14d51e3493 | 551 | if(abs(digital_pot_change)>=50) { |
jelord | 112:fd14d51e3493 | 552 | digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow))); |
jelord | 112:fd14d51e3493 | 553 | RGB_LED.set_led(1,0,0); |
jelord | 112:fd14d51e3493 | 554 | } else { |
jelord | 112:fd14d51e3493 | 555 | digital_pot_set = (digital_pot_set+ digital_pot_change); |
jelord | 112:fd14d51e3493 | 556 | RGB_LED.set_led(1,1,0); |
jelord | 112:fd14d51e3493 | 557 | } |
jelord | 112:fd14d51e3493 | 558 | |
jelord | 112:fd14d51e3493 | 559 | if(digital_pot_set>=digitalpotMax) { |
jelord | 112:fd14d51e3493 | 560 | digital_pot_set = digitalpotMax; |
jelord | 112:fd14d51e3493 | 561 | RGB_LED.set_led(1,0,0); |
jelord | 112:fd14d51e3493 | 562 | } else if(digital_pot_set<=digitalpotMin) { |
jelord | 112:fd14d51e3493 | 563 | digital_pot_set = digitalpotMin; |
jelord | 112:fd14d51e3493 | 564 | RGB_LED.set_led(1,0,0); |
jelord | 112:fd14d51e3493 | 565 | } |
jelord | 112:fd14d51e3493 | 566 | |
jelord | 112:fd14d51e3493 | 567 | |
jelord | 112:fd14d51e3493 | 568 | DigPot.writeRegister(digital_pot_set); |
jelord | 112:fd14d51e3493 | 569 | |
jelord | 112:fd14d51e3493 | 570 | } else { |
jelord | 112:fd14d51e3493 | 571 | RGB_LED.set_led(0,1,0); |
jelord | 112:fd14d51e3493 | 572 | } |
jelord | 112:fd14d51e3493 | 573 | |
caseyquinn | 114:84e759e07948 | 574 | /* |
jelord | 112:fd14d51e3493 | 575 | movementsensor.getACCEL(); |
jelord | 112:fd14d51e3493 | 576 | movementsensor.getCOMPASS(); |
jelord | 112:fd14d51e3493 | 577 | compass = movementsensor.getCOMPASS_HEADING(); |
jelord | 112:fd14d51e3493 | 578 | accel_x = movementsensor.AccelData.x; |
jelord | 112:fd14d51e3493 | 579 | accel_y = movementsensor.AccelData.y; |
jelord | 112:fd14d51e3493 | 580 | accel_z = movementsensor.AccelData.z; |
jelord | 112:fd14d51e3493 | 581 | accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0; |
jelord | 112:fd14d51e3493 | 582 | mag_x = movementsensor.MagData.x; |
jelord | 112:fd14d51e3493 | 583 | mag_y = movementsensor.MagData.y; |
jelord | 112:fd14d51e3493 | 584 | mag_z = movementsensor.MagData.z; |
caseyquinn | 114:84e759e07948 | 585 | */ |
jelord | 112:fd14d51e3493 | 586 | vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 |
jelord | 112:fd14d51e3493 | 587 | vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 |
jelord | 112:fd14d51e3493 | 588 | omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 |
jelord | 112:fd14d51e3493 | 589 | press = bmesensor.getPressure(); |
jelord | 112:fd14d51e3493 | 590 | temp = bmesensor.getTemperature()-5.0; |
jelord | 112:fd14d51e3493 | 591 | rh = bmesensor.getHumidity(); |
jelord | 112:fd14d51e3493 | 592 | uv = lightsensor.getUV(); |
jelord | 112:fd14d51e3493 | 593 | vis = lightsensor.getVIS(); |
jelord | 112:fd14d51e3493 | 594 | ir = lightsensor.getIR(); |
jelord | 112:fd14d51e3493 | 595 | |
jelord | 112:fd14d51e3493 | 596 | |
jelord | 112:fd14d51e3493 | 597 | FILE *fp = fopen(filename, "a"); |
jelord | 112:fd14d51e3493 | 598 | fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds); |
jelord | 112:fd14d51e3493 | 599 | fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho); |
caseyquinn | 114:84e759e07948 | 600 | fprintf(fp, "%1.3f,%5.1f,", volflow, sampledVol); |
caseyquinn | 114:84e759e07948 | 601 | // 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 | 114:84e759e07948 | 602 | // fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass); |
jelord | 112:fd14d51e3493 | 603 | fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps()); |
jelord | 112:fd14d51e3493 | 604 | fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow); |
jelord | 112:fd14d51e3493 | 605 | fclose(fp); |
caseyquinn | 114:84e759e07948 | 606 | //free(fp); |
jelord | 112:fd14d51e3493 | 607 | |
jelord | 112:fd14d51e3493 | 608 | } |
caseyquinn | 111:5f2ba5f1a468 | 609 | |
caseyquinn | 110:67980ebdebf6 | 610 | |
joshuasmth04 | 59:a9b21b3d9afc | 611 | |
caseyquinn | 0:14d46ef4b6cb | 612 | } |
caseyquinn | 0:14d46ef4b6cb | 613 |