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@69:7a99d97c334e, 2015-08-15 (annotated)
- Committer:
- caseyquinn
- Date:
- Sat Aug 15 17:30:20 2015 +0000
- Revision:
- 69:7a99d97c334e
- Parent:
- 68:ca444d0760a0
- Child:
- 70:81f04e69e08a
Added in the Dec to January Rollover scenario
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" |
caseyquinn | 67:300418575137 | 12 | #include "Honduras_SerialMenu.h" |
caseyquinn | 1:37babeb68ab9 | 13 | |
caseyquinn | 1:37babeb68ab9 | 14 | #define SERIAL_BAUD_RATE 9600 |
joshuasmth04 | 41:1fb3e0ac6f87 | 15 | #define SCL 20 |
caseyquinn | 7:a24d7156bc02 | 16 | #define SDA 22 |
caseyquinn | 57:0b554f7aa9a3 | 17 | //#define Crono 0xD0 //D0 for the chronoDot |
caseyquinn | 0:14d46ef4b6cb | 18 | |
caseyquinn | 0:14d46ef4b6cb | 19 | I2C i2c(p22, p20); |
caseyquinn | 0:14d46ef4b6cb | 20 | Adafruit_ADS1115 ads(&i2c); |
caseyquinn | 1:37babeb68ab9 | 21 | MCP40D17 DigPot(&i2c); |
caseyquinn | 12:8c00a7f5d483 | 22 | BME280 bmesensor(p22, p20); |
caseyquinn | 7:a24d7156bc02 | 23 | STC3100 gasG(p22, p20); |
caseyquinn | 2:e596e685eb39 | 24 | Serial pc(USBTX, USBRX); |
caseyquinn | 8:204c21adf693 | 25 | DigitalOut blower(p29, 0); |
caseyquinn | 17:1baf7cab694e | 26 | DigitalOut pbKill(p18, 1); |
caseyquinn | 10:f9cb61b29340 | 27 | LSM303 movementsensor(p22, p20); |
caseyquinn | 14:ad550174db8b | 28 | SI1145 lightsensor(p22, p20); |
joshuasmth04 | 40:ef7e1dcb3780 | 29 | NCP5623BMUTBG RGB_LED(p22, p20); |
joshuasmth04 | 41:1fb3e0ac6f87 | 30 | CronoDot RTC(p22, p20); |
caseyquinn | 56:c4d6bdd7c3fb | 31 | EEPROM E2PROM(p22, p20); |
caseyquinn | 67:300418575137 | 32 | Honduras_SerialMenu Menu; |
caseyquinn | 68:ca444d0760a0 | 33 | //DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications |
caseyquinn | 57:0b554f7aa9a3 | 34 | |
caseyquinn | 57:0b554f7aa9a3 | 35 | Timeout stop; //This is the stop call back object |
joshuasmth04 | 62:edc9632bcc43 | 36 | Timeout logg; //This is the logging call back object |
caseyquinn | 67:300418575137 | 37 | int RunReady =0; |
caseyquinn | 67:300418575137 | 38 | |
caseyquinn | 0:14d46ef4b6cb | 39 | |
caseyquinn | 68:ca444d0760a0 | 40 | //UPAS0011 CALIBRATION TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx' |
lionberg | 52:80480b2fafba | 41 | //mass flow sensor output signal (x) vs. mass flow (y) |
caseyquinn | 68:ca444d0760a0 | 42 | float MF4 = -2.5778; |
caseyquinn | 68:ca444d0760a0 | 43 | float MF3 = 15.031; |
caseyquinn | 68:ca444d0760a0 | 44 | float MF2 = -30.738; |
caseyquinn | 68:ca444d0760a0 | 45 | float MF1 = 27.914; |
caseyquinn | 68:ca444d0760a0 | 46 | float MF0 = -8.794; |
caseyquinn | 68:ca444d0760a0 | 47 | //mass flow sensor polynomial deviation limits |
caseyquinn | 68:ca444d0760a0 | 48 | float omronVMin = 0.645; //V |
caseyquinn | 68:ca444d0760a0 | 49 | float omronVMax = 2.275; //V |
caseyquinn | 68:ca444d0760a0 | 50 | float omronMFMin = 0.010; //g/L |
caseyquinn | 68:ca444d0760a0 | 51 | float omronMFMax = 3.553; //g/L |
caseyquinn | 68:ca444d0760a0 | 52 | //UPAS0011 values dig_pot=9.2763x4 - 85.942x3 + 303.77x2 - 512.82x + 376.73 |
caseyquinn | 68:ca444d0760a0 | 53 | float DP4 = 9.2763; |
caseyquinn | 68:ca444d0760a0 | 54 | float DP3 = -85.942; |
caseyquinn | 68:ca444d0760a0 | 55 | float DP2 = 303.77; |
caseyquinn | 68:ca444d0760a0 | 56 | float DP1 = -512.82; |
caseyquinn | 68:ca444d0760a0 | 57 | float DP0 = 376.73; |
caseyquinn | 68:ca444d0760a0 | 58 | |
caseyquinn | 68:ca444d0760a0 | 59 | |
lionberg | 52:80480b2fafba | 60 | |
caseyquinn | 20:ad9883973d86 | 61 | float press; |
caseyquinn | 20:ad9883973d86 | 62 | float temp; |
caseyquinn | 20:ad9883973d86 | 63 | float rh; |
caseyquinn | 4:69bd7e8a994c | 64 | |
caseyquinn | 20:ad9883973d86 | 65 | int uv; |
caseyquinn | 20:ad9883973d86 | 66 | int vis; |
caseyquinn | 20:ad9883973d86 | 67 | int ir; |
caseyquinn | 8:204c21adf693 | 68 | |
caseyquinn | 67:300418575137 | 69 | float compass; |
caseyquinn | 10:f9cb61b29340 | 70 | float accel_x; |
caseyquinn | 10:f9cb61b29340 | 71 | float accel_y; |
caseyquinn | 10:f9cb61b29340 | 72 | float accel_z; |
lionberg | 52:80480b2fafba | 73 | float accel_comp; |
caseyquinn | 10:f9cb61b29340 | 74 | float mag_x; |
caseyquinn | 10:f9cb61b29340 | 75 | float mag_y; |
caseyquinn | 10:f9cb61b29340 | 76 | float mag_z; |
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 | 57:0b554f7aa9a3 | 88 | int logInerval = 10; |
caseyquinn | 57:0b554f7aa9a3 | 89 | double secondsD = 0; |
joshuasmth04 | 41:1fb3e0ac6f87 | 90 | float massflowSet; |
caseyquinn | 42:fc2f2b9f07ae | 91 | float deltaVflow = 0.0; |
caseyquinn | 42:fc2f2b9f07ae | 92 | float deltaMflow = 0.0; |
caseyquinn | 47:3146e8c949a9 | 93 | float gainFlow; |
lionberg | 52:80480b2fafba | 94 | float sampledVol; //L, total sampled volume |
caseyquinn | 20:ad9883973d86 | 95 | |
caseyquinn | 55:f24d70f519cd | 96 | int digital_pot_setpoint; //min = 0x7F, max = 0x00 |
caseyquinn | 55:f24d70f519cd | 97 | int digital_pot_set; |
caseyquinn | 55:f24d70f519cd | 98 | int digital_pot_change; |
caseyquinn | 55:f24d70f519cd | 99 | int digitalpotMax = 127; |
caseyquinn | 55:f24d70f519cd | 100 | int digitalpotMin = 2; |
caseyquinn | 55:f24d70f519cd | 101 | |
joshuasmth04 | 59:a9b21b3d9afc | 102 | int dutyUp; |
joshuasmth04 | 59:a9b21b3d9afc | 103 | int dutyDown; |
caseyquinn | 67:300418575137 | 104 | uint8_t f_sec, f_min, f_hour, f_date, f_month, f_year; |
joshuasmth04 | 59:a9b21b3d9afc | 105 | |
caseyquinn | 67:300418575137 | 106 | //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 | 107 | |
caseyquinn | 57:0b554f7aa9a3 | 108 | char device_name[] = "---------------"; |
caseyquinn | 68:ca444d0760a0 | 109 | char filename[] = "/sd/UPAS0011LOG000000000000---------------.txt"; |
caseyquinn | 7:a24d7156bc02 | 110 | 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 | 111 | |
caseyquinn | 7:a24d7156bc02 | 112 | |
joshuasmth04 | 62:edc9632bcc43 | 113 | void check_stop() // this checks if it's time to stop and shutdown |
caseyquinn | 57:0b554f7aa9a3 | 114 | { |
joshuasmth04 | 63:66796aef8d68 | 115 | //RTC.get_time(); //debug |
joshuasmth04 | 63:66796aef8d68 | 116 | //pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debig |
caseyquinn | 67:300418575137 | 117 | if(RTC.compare(f_sec, f_min, f_hour, f_date, f_month, f_year)) { |
caseyquinn | 57:0b554f7aa9a3 | 118 | pbKill = 0; // this is were we shut everything down |
caseyquinn | 57:0b554f7aa9a3 | 119 | } |
caseyquinn | 57:0b554f7aa9a3 | 120 | stop.detach(); |
caseyquinn | 57:0b554f7aa9a3 | 121 | stop.attach(&check_stop, 5); // checks stop time every 5 seconds |
joshuasmth04 | 62:edc9632bcc43 | 122 | } |
caseyquinn | 58:7239c2ab2b65 | 123 | |
joshuasmth04 | 41:1fb3e0ac6f87 | 124 | |
caseyquinn | 57:0b554f7aa9a3 | 125 | void log_data() |
joshuasmth04 | 59:a9b21b3d9afc | 126 | { |
joshuasmth04 | 59:a9b21b3d9afc | 127 | RTC.get_time(); |
joshuasmth04 | 59:a9b21b3d9afc | 128 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 129 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 130 | |
joshuasmth04 | 59:a9b21b3d9afc | 131 | if(omronVolt<=omronVMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 132 | massflow = omronMFMin; |
joshuasmth04 | 59:a9b21b3d9afc | 133 | } else if(omronVolt>=omronVMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 134 | massflow = omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 135 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 136 | massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 137 | } |
joshuasmth04 | 59:a9b21b3d9afc | 138 | |
joshuasmth04 | 59:a9b21b3d9afc | 139 | 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 | 140 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 141 | sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow); |
joshuasmth04 | 59:a9b21b3d9afc | 142 | deltaVflow = volflow-volflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 143 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 144 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 145 | |
joshuasmth04 | 59:a9b21b3d9afc | 146 | if(abs(deltaMflow)>.025) { |
joshuasmth04 | 59:a9b21b3d9afc | 147 | digital_pot_change = (int)(gainFlow*deltaMflow); |
joshuasmth04 | 59:a9b21b3d9afc | 148 | |
joshuasmth04 | 59:a9b21b3d9afc | 149 | |
joshuasmth04 | 59:a9b21b3d9afc | 150 | if(abs(digital_pot_change)>=50) { |
joshuasmth04 | 59:a9b21b3d9afc | 151 | digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 152 | RGB_LED.set_led(1,0,0); |
joshuasmth04 | 41:1fb3e0ac6f87 | 153 | |
joshuasmth04 | 59:a9b21b3d9afc | 154 | } else if(digital_pot_change+digital_pot_set>=digitalpotMax&abs(digital_pot_change)<50) { |
joshuasmth04 | 59:a9b21b3d9afc | 155 | digital_pot_set = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 156 | RGB_LED.set_led(1,0,0); |
joshuasmth04 | 59:a9b21b3d9afc | 157 | } else if(digital_pot_change+digital_pot_set<=digitalpotMin&abs(digital_pot_change)<50) { |
joshuasmth04 | 59:a9b21b3d9afc | 158 | digital_pot_set = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 159 | RGB_LED.set_led(1,0,0); |
joshuasmth04 | 59:a9b21b3d9afc | 160 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 161 | digital_pot_set = (digital_pot_set+ digital_pot_change); |
joshuasmth04 | 59:a9b21b3d9afc | 162 | RGB_LED.set_led(1,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 163 | } |
joshuasmth04 | 59:a9b21b3d9afc | 164 | |
joshuasmth04 | 59:a9b21b3d9afc | 165 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 166 | |
joshuasmth04 | 59:a9b21b3d9afc | 167 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 168 | RGB_LED.set_led(0,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 169 | } |
joshuasmth04 | 59:a9b21b3d9afc | 170 | |
joshuasmth04 | 59:a9b21b3d9afc | 171 | movementsensor.getACCEL(); |
joshuasmth04 | 59:a9b21b3d9afc | 172 | movementsensor.getCOMPASS(); |
caseyquinn | 67:300418575137 | 173 | compass = movementsensor.getCOMPASS_HEADING(); |
joshuasmth04 | 59:a9b21b3d9afc | 174 | accel_x = movementsensor.AccelData.x; |
joshuasmth04 | 59:a9b21b3d9afc | 175 | accel_y = movementsensor.AccelData.y; |
joshuasmth04 | 59:a9b21b3d9afc | 176 | accel_z = movementsensor.AccelData.z; |
joshuasmth04 | 59:a9b21b3d9afc | 177 | accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0; |
joshuasmth04 | 59:a9b21b3d9afc | 178 | mag_x = movementsensor.MagData.x; |
joshuasmth04 | 59:a9b21b3d9afc | 179 | mag_y = movementsensor.MagData.y; |
joshuasmth04 | 59:a9b21b3d9afc | 180 | mag_z = movementsensor.MagData.z; |
joshuasmth04 | 59:a9b21b3d9afc | 181 | |
joshuasmth04 | 59:a9b21b3d9afc | 182 | vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 |
joshuasmth04 | 59:a9b21b3d9afc | 183 | vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 |
joshuasmth04 | 59:a9b21b3d9afc | 184 | omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 |
joshuasmth04 | 59:a9b21b3d9afc | 185 | press = bmesensor.getPressure(); |
joshuasmth04 | 59:a9b21b3d9afc | 186 | temp = bmesensor.getTemperature()-5.0; |
joshuasmth04 | 59:a9b21b3d9afc | 187 | rh = bmesensor.getHumidity(); |
joshuasmth04 | 59:a9b21b3d9afc | 188 | uv = lightsensor.getUV(); |
joshuasmth04 | 59:a9b21b3d9afc | 189 | vis = lightsensor.getVIS(); |
joshuasmth04 | 59:a9b21b3d9afc | 190 | ir = lightsensor.getIR(); |
joshuasmth04 | 59:a9b21b3d9afc | 191 | |
joshuasmth04 | 59:a9b21b3d9afc | 192 | //Mount the filesystem |
joshuasmth04 | 59:a9b21b3d9afc | 193 | //sd.mount(); |
joshuasmth04 | 59:a9b21b3d9afc | 194 | FILE *fp = fopen(filename, "a"); |
caseyquinn | 67:300418575137 | 195 | fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%1.3f,%1.3f,%f\r\n",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,omronVolt,massflow,temp,press,rh,atmoRho,volflow,sampledVol,accel_x,accel_y,accel_z,accel_comp,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps(), gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow, compass); |
joshuasmth04 | 59:a9b21b3d9afc | 196 | fclose(fp); |
joshuasmth04 | 59:a9b21b3d9afc | 197 | //Unmount the filesystem |
joshuasmth04 | 59:a9b21b3d9afc | 198 | //sd.unmount(); |
joshuasmth04 | 59:a9b21b3d9afc | 199 | |
joshuasmth04 | 59:a9b21b3d9afc | 200 | wait(1); |
joshuasmth04 | 62:edc9632bcc43 | 201 | logg.detach(); |
joshuasmth04 | 62:edc9632bcc43 | 202 | logg.attach(&log_data, 10); |
joshuasmth04 | 59:a9b21b3d9afc | 203 | } |
caseyquinn | 57:0b554f7aa9a3 | 204 | |
caseyquinn | 57:0b554f7aa9a3 | 205 | int main() |
caseyquinn | 57:0b554f7aa9a3 | 206 | { |
caseyquinn | 57:0b554f7aa9a3 | 207 | |
caseyquinn | 57:0b554f7aa9a3 | 208 | // Setup and Initialization |
caseyquinn | 57:0b554f7aa9a3 | 209 | //---------------------------------------------------------------------------------------------// |
caseyquinn | 57:0b554f7aa9a3 | 210 | |
caseyquinn | 67:300418575137 | 211 | |
caseyquinn | 57:0b554f7aa9a3 | 212 | |
caseyquinn | 67:300418575137 | 213 | Menu.read_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown); //Read all data from the EEPROM here |
caseyquinn | 67:300418575137 | 214 | |
caseyquinn | 57:0b554f7aa9a3 | 215 | //Test for errors |
joshuasmth04 | 59:a9b21b3d9afc | 216 | if(RTC.OSF()) { //Don't proceed if the RTC needs reset |
caseyquinn | 67:300418575137 | 217 | RGB_LED.set_led(0,1,1); // error code/color |
lionberg | 60:5913d77c8a4a | 218 | pc.printf("DATE/TIMESTAMP NEEDS TO BE RECALIBRATED!!\r\n"); |
caseyquinn | 67:300418575137 | 219 | Menu.Start(pc, E2PROM, RTC, RunReady, volflowSet, device_name, dutyUp, dutyDown); //Forces you to open the menu |
caseyquinn | 67:300418575137 | 220 | Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown); //Save all data to the EEPROM |
caseyquinn | 57:0b554f7aa9a3 | 221 | } |
caseyquinn | 67:300418575137 | 222 | |
caseyquinn | 67:300418575137 | 223 | if(RunReady == 0){ |
caseyquinn | 67:300418575137 | 224 | RGB_LED.set_led(0,1,1); // error code/color |
caseyquinn | 67:300418575137 | 225 | pc.printf("Change Name\r\n"); |
caseyquinn | 67:300418575137 | 226 | Menu.Start(pc, E2PROM, RTC, RunReady, volflowSet, device_name, dutyUp, dutyDown); //Forces you to open the menu |
caseyquinn | 67:300418575137 | 227 | RunReady = 1; |
caseyquinn | 67:300418575137 | 228 | Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown); //Save all data to the EEPROM |
caseyquinn | 67:300418575137 | 229 | pbKill = 0; |
caseyquinn | 67:300418575137 | 230 | } |
caseyquinn | 67:300418575137 | 231 | |
caseyquinn | 67:300418575137 | 232 | RunReady = 0; |
caseyquinn | 67:300418575137 | 233 | Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown); //Save all data to the EEPROM |
joshuasmth04 | 59:a9b21b3d9afc | 234 | |
joshuasmth04 | 62:edc9632bcc43 | 235 | stop.attach(&check_stop, 60); // check if we should shut down every 5 seconds, starting 60s after the start. |
caseyquinn | 57:0b554f7aa9a3 | 236 | |
joshuasmth04 | 59:a9b21b3d9afc | 237 | if(volflowSet==1.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 238 | gainFlow = 100; |
joshuasmth04 | 59:a9b21b3d9afc | 239 | } else if(volflowSet==2.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 240 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 241 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 242 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 243 | } |
joshuasmth04 | 59:a9b21b3d9afc | 244 | |
caseyquinn | 57:0b554f7aa9a3 | 245 | RGB_LED.set_led(1,0,0); |
caseyquinn | 57:0b554f7aa9a3 | 246 | press = bmesensor.getPressure(); |
caseyquinn | 57:0b554f7aa9a3 | 247 | temp = bmesensor.getTemperature(); |
caseyquinn | 57:0b554f7aa9a3 | 248 | rh = bmesensor.getHumidity(); |
caseyquinn | 57:0b554f7aa9a3 | 249 | |
caseyquinn | 57:0b554f7aa9a3 | 250 | 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 | 251 | massflowSet = volflowSet*atmoRho; |
caseyquinn | 57:0b554f7aa9a3 | 252 | //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
caseyquinn | 57:0b554f7aa9a3 | 253 | digital_pot_setpoint = (int)floor(DP4*pow(massflowSet,4)+DP3*pow(massflowSet,3)+DP2*pow(massflowSet,2)+DP1*massflowSet+DP0); //min = 0x7F, max = 0x00 |
joshuasmth04 | 59:a9b21b3d9afc | 254 | |
joshuasmth04 | 59:a9b21b3d9afc | 255 | if(digital_pot_setpoint>=digitalpotMax) { |
caseyquinn | 57:0b554f7aa9a3 | 256 | digital_pot_setpoint = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 257 | } else if(digital_pot_setpoint<=digitalpotMin) { |
caseyquinn | 57:0b554f7aa9a3 | 258 | digital_pot_setpoint = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 259 | } |
joshuasmth04 | 59:a9b21b3d9afc | 260 | |
caseyquinn | 57:0b554f7aa9a3 | 261 | DigPot.writeRegister(digital_pot_setpoint); |
caseyquinn | 57:0b554f7aa9a3 | 262 | wait(1); |
caseyquinn | 57:0b554f7aa9a3 | 263 | blower = 1; |
caseyquinn | 57:0b554f7aa9a3 | 264 | |
caseyquinn | 57:0b554f7aa9a3 | 265 | RTC.get_time(); |
caseyquinn | 67:300418575137 | 266 | f_sec = RTC.seconds; |
caseyquinn | 67:300418575137 | 267 | f_min = RTC.minutes; |
caseyquinn | 67:300418575137 | 268 | f_hour = RTC.hour; |
caseyquinn | 67:300418575137 | 269 | if(RTC.month == 1 | RTC.month == 3 | RTC.month == 5 | RTC.month == 7 | RTC.month == 8 | RTC.month == 10){ |
caseyquinn | 67:300418575137 | 270 | if(RTC.date == 31){ |
caseyquinn | 67:300418575137 | 271 | f_date = 1; |
caseyquinn | 67:300418575137 | 272 | f_month = RTC.month +1; |
caseyquinn | 67:300418575137 | 273 | f_year = RTC.year; |
caseyquinn | 67:300418575137 | 274 | } |
caseyquinn | 67:300418575137 | 275 | else{ |
caseyquinn | 67:300418575137 | 276 | f_date = RTC.date+1; |
caseyquinn | 67:300418575137 | 277 | f_month = RTC.month; |
caseyquinn | 67:300418575137 | 278 | f_year = RTC.year; |
caseyquinn | 67:300418575137 | 279 | } |
caseyquinn | 67:300418575137 | 280 | } |
caseyquinn | 67:300418575137 | 281 | else if(RTC.month == 4 | RTC.month == 6 | RTC.month == 9 | RTC.month == 11){ |
caseyquinn | 67:300418575137 | 282 | if(RTC.date == 30){ |
caseyquinn | 67:300418575137 | 283 | f_date = 1; |
caseyquinn | 67:300418575137 | 284 | f_month = RTC.month +1; |
caseyquinn | 67:300418575137 | 285 | f_year = RTC.year; |
caseyquinn | 67:300418575137 | 286 | } |
caseyquinn | 67:300418575137 | 287 | else{ |
caseyquinn | 67:300418575137 | 288 | f_date = RTC.date+1; |
caseyquinn | 67:300418575137 | 289 | f_month = RTC.month; |
caseyquinn | 67:300418575137 | 290 | f_year = RTC.year; |
caseyquinn | 67:300418575137 | 291 | } |
caseyquinn | 67:300418575137 | 292 | } |
caseyquinn | 67:300418575137 | 293 | else if(RTC.month == 2){ |
caseyquinn | 67:300418575137 | 294 | if(RTC.year == 16 | RTC.year == 20 | RTC.year == 24| RTC.year == 28){ |
caseyquinn | 67:300418575137 | 295 | if(RTC.date == 29){ |
caseyquinn | 67:300418575137 | 296 | f_date = 1; |
caseyquinn | 67:300418575137 | 297 | f_month = RTC.month +1; |
caseyquinn | 67:300418575137 | 298 | f_year = RTC.year; |
caseyquinn | 67:300418575137 | 299 | } |
caseyquinn | 67:300418575137 | 300 | else{ |
caseyquinn | 67:300418575137 | 301 | f_date = RTC.date+1; |
caseyquinn | 67:300418575137 | 302 | f_month = RTC.month; |
caseyquinn | 67:300418575137 | 303 | f_year = RTC.year; |
caseyquinn | 67:300418575137 | 304 | } |
caseyquinn | 67:300418575137 | 305 | } |
caseyquinn | 67:300418575137 | 306 | else{ |
caseyquinn | 67:300418575137 | 307 | if(RTC.date == 28){ |
caseyquinn | 67:300418575137 | 308 | f_date = 1; |
caseyquinn | 67:300418575137 | 309 | f_month = RTC.month +1; |
caseyquinn | 67:300418575137 | 310 | f_year = RTC.year; |
caseyquinn | 67:300418575137 | 311 | } |
caseyquinn | 67:300418575137 | 312 | else{ |
caseyquinn | 67:300418575137 | 313 | f_date = RTC.date+1; |
caseyquinn | 67:300418575137 | 314 | f_month = RTC.month; |
caseyquinn | 67:300418575137 | 315 | f_year = RTC.year; |
caseyquinn | 67:300418575137 | 316 | } |
caseyquinn | 69:7a99d97c334e | 317 | } |
caseyquinn | 67:300418575137 | 318 | } |
caseyquinn | 69:7a99d97c334e | 319 | else if(RTC.month == 12){ |
caseyquinn | 69:7a99d97c334e | 320 | if(RTC.date == 31){ |
caseyquinn | 69:7a99d97c334e | 321 | f_date = 1; |
caseyquinn | 69:7a99d97c334e | 322 | f_month = 1; |
caseyquinn | 69:7a99d97c334e | 323 | f_year = RTC.year+1; |
caseyquinn | 69:7a99d97c334e | 324 | } |
caseyquinn | 69:7a99d97c334e | 325 | else{ |
caseyquinn | 69:7a99d97c334e | 326 | f_date = RTC.date+1; |
caseyquinn | 69:7a99d97c334e | 327 | f_month = RTC.month; |
caseyquinn | 69:7a99d97c334e | 328 | f_year = RTC.year; |
caseyquinn | 69:7a99d97c334e | 329 | } |
caseyquinn | 69:7a99d97c334e | 330 | } |
caseyquinn | 67:300418575137 | 331 | |
caseyquinn | 67:300418575137 | 332 | |
caseyquinn | 68:ca444d0760a0 | 333 | sprintf(filename, "/sd/UPAS0011LOG %02d-%02d-%02d %02d=%02d=%02d %s.txt",RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,device_name); |
caseyquinn | 57:0b554f7aa9a3 | 334 | FILE *fp = fopen(filename, "w"); |
caseyquinn | 57:0b554f7aa9a3 | 335 | fclose(fp); |
joshuasmth04 | 59:a9b21b3d9afc | 336 | //pc.printf("%d\r\n",digital_pot_setpoint); |
joshuasmth04 | 59:a9b21b3d9afc | 337 | |
joshuasmth04 | 59:a9b21b3d9afc | 338 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 339 | //Following lines are needed to enter into the initiallization flow control loop |
joshuasmth04 | 59:a9b21b3d9afc | 340 | |
caseyquinn | 57:0b554f7aa9a3 | 341 | wait(10); |
joshuasmth04 | 59:a9b21b3d9afc | 342 | |
caseyquinn | 57:0b554f7aa9a3 | 343 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
caseyquinn | 57:0b554f7aa9a3 | 344 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 345 | if(omronVolt<=omronVMin) { |
caseyquinn | 57:0b554f7aa9a3 | 346 | massflow = omronMFMin; |
joshuasmth04 | 59:a9b21b3d9afc | 347 | } else if(omronVolt>=omronVMax) { |
caseyquinn | 57:0b554f7aa9a3 | 348 | massflow = omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 349 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 350 | massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 351 | } |
joshuasmth04 | 59:a9b21b3d9afc | 352 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 353 | digital_pot_set = digital_pot_setpoint; |
joshuasmth04 | 59:a9b21b3d9afc | 354 | wait(5); |
joshuasmth04 | 59:a9b21b3d9afc | 355 | |
joshuasmth04 | 59:a9b21b3d9afc | 356 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 357 | //Sets the flow withen +-1.5% of the desired flow rate based on mass flow |
joshuasmth04 | 59:a9b21b3d9afc | 358 | |
joshuasmth04 | 59:a9b21b3d9afc | 359 | while(abs(deltaMflow)>.015) { |
caseyquinn | 67:300418575137 | 360 | |
joshuasmth04 | 59:a9b21b3d9afc | 361 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 362 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 363 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 59:a9b21b3d9afc | 364 | if(omronVolt<=omronVMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 365 | massflow = omronMFMin; |
joshuasmth04 | 59:a9b21b3d9afc | 366 | } else if(omronVolt>=omronVMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 367 | massflow = omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 368 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 369 | massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 370 | } |
caseyquinn | 57:0b554f7aa9a3 | 371 | |
joshuasmth04 | 59:a9b21b3d9afc | 372 | 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 | 373 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 374 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 375 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 63:66796aef8d68 | 376 | //pc.printf("%f,%f,%f,%f,%d,%u,%x\r\n",volflow,massflow,massflowSet,deltaMflow,digital_pot_set,digital_pot_set,digital_pot_set); |
caseyquinn | 67:300418575137 | 377 | pc.printf("%d,%d,%d,%d,%d,%d\r\n",f_sec,f_min,f_hour,f_date,f_month,f_year,digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 378 | digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 379 | if(digital_pot_set>=digitalpotMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 380 | digital_pot_set = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 381 | } else if(digital_pot_set<=digitalpotMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 382 | digital_pot_set = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 383 | } |
joshuasmth04 | 59:a9b21b3d9afc | 384 | |
joshuasmth04 | 59:a9b21b3d9afc | 385 | wait(2); |
joshuasmth04 | 59:a9b21b3d9afc | 386 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 387 | wait(1); |
joshuasmth04 | 59:a9b21b3d9afc | 388 | |
joshuasmth04 | 59:a9b21b3d9afc | 389 | |
joshuasmth04 | 59:a9b21b3d9afc | 390 | } |
joshuasmth04 | 59:a9b21b3d9afc | 391 | |
joshuasmth04 | 59:a9b21b3d9afc | 392 | sampledVol = 0.0; |
joshuasmth04 | 59:a9b21b3d9afc | 393 | RGB_LED.set_led(0,1,0); |
caseyquinn | 67:300418575137 | 394 | |
caseyquinn | 67:300418575137 | 395 | |
joshuasmth04 | 59:a9b21b3d9afc | 396 | |
joshuasmth04 | 59:a9b21b3d9afc | 397 | //** end of initalization **// |
joshuasmth04 | 59:a9b21b3d9afc | 398 | |
joshuasmth04 | 59:a9b21b3d9afc | 399 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 400 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 401 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 402 | // Main Control Loop |
joshuasmth04 | 59:a9b21b3d9afc | 403 | |
joshuasmth04 | 62:edc9632bcc43 | 404 | logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c |
joshuasmth04 | 59:a9b21b3d9afc | 405 | while(1) { |
joshuasmth04 | 62:edc9632bcc43 | 406 | //__disable_irq(); // Disable Interrupts |
joshuasmth04 | 62:edc9632bcc43 | 407 | //RTC.get_time(); |
joshuasmth04 | 62:edc9632bcc43 | 408 | //__enable_irq(); // Enable Interrupts |
joshuasmth04 | 62:edc9632bcc43 | 409 | //secondsD = (double)RTC.seconds; |
joshuasmth04 | 62:edc9632bcc43 | 410 | //if(fmod(secondsD,logInerval)==0) { |
joshuasmth04 | 62:edc9632bcc43 | 411 | //log_data(); |
joshuasmth04 | 64:2906b0c48403 | 412 | //} |
caseyquinn | 0:14d46ef4b6cb | 413 | } |
joshuasmth04 | 59:a9b21b3d9afc | 414 | |
caseyquinn | 0:14d46ef4b6cb | 415 | } |
caseyquinn | 0:14d46ef4b6cb | 416 | |
caseyquinn | 0:14d46ef4b6cb | 417 |