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@67:300418575137, 2015-08-15 (annotated)
- Committer:
- caseyquinn
- Date:
- Sat Aug 15 02:10:49 2015 +0000
- Revision:
- 67:300418575137
- Parent:
- 64:2906b0c48403
- Child:
- 68:ca444d0760a0
Upon first powerup requires the menu to be brought up and time/date and sample name to be updated, once exit menu, powers down. Second power starts initialization and then runs for 24 hours or until powered down.
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 | 67:300418575137 | 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 | |
lionberg | 52:80480b2fafba | 40 | //UPAS0012 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) |
lionberg | 60:5913d77c8a4a | 42 | //y = -0.6154x4 + 3.7873x3 - 7.2564x2 + 7.0202x - 1.9413 |
caseyquinn | 55:f24d70f519cd | 43 | float MF4 = -0.6154; |
caseyquinn | 55:f24d70f519cd | 44 | float MF3 = 3.7873; |
caseyquinn | 55:f24d70f519cd | 45 | float MF2 = -7.2564; |
caseyquinn | 55:f24d70f519cd | 46 | float MF1 = 7.0202; |
caseyquinn | 55:f24d70f519cd | 47 | float MF0 = -1.9413; |
lionberg | 52:80480b2fafba | 48 | //Mass flow sensor polynomial deviation limits |
lionberg | 60:5913d77c8a4a | 49 | float omronVMin = 0.425; //V |
lionberg | 60:5913d77c8a4a | 50 | float omronVMax = 2.005; //V |
lionberg | 60:5913d77c8a4a | 51 | float omronMFMin = 0.002; //g/L |
lionberg | 60:5913d77c8a4a | 52 | float omronMFMax = 3.544; //g/L |
lionberg | 52:80480b2fafba | 53 | //DIGITAL POTENTIOSTAT dig-pot vs m_dot POLYNOMIAL TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx' |
lionberg | 60:5913d77c8a4a | 54 | //y = 5.3839x4 - 51.627x3 + 191.09x2 - 345.9x + 277.63 |
caseyquinn | 55:f24d70f519cd | 55 | float DP4 = 5.3839; |
caseyquinn | 55:f24d70f519cd | 56 | float DP3 = -51.627; |
caseyquinn | 55:f24d70f519cd | 57 | float DP2 = 191.09; |
caseyquinn | 55:f24d70f519cd | 58 | float DP1 = -345.9; |
caseyquinn | 55:f24d70f519cd | 59 | float DP0 = 277.63; |
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 | 57:0b554f7aa9a3 | 109 | char filename[] = "/sd/UPAS0012LOG000000000000---------------.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 | 67:300418575137 | 317 | } |
caseyquinn | 67:300418575137 | 318 | } |
caseyquinn | 67:300418575137 | 319 | |
caseyquinn | 67:300418575137 | 320 | |
lionberg | 60:5913d77c8a4a | 321 | sprintf(filename, "/sd/UPAS0012LOG %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 | 322 | FILE *fp = fopen(filename, "w"); |
caseyquinn | 57:0b554f7aa9a3 | 323 | fclose(fp); |
joshuasmth04 | 59:a9b21b3d9afc | 324 | //pc.printf("%d\r\n",digital_pot_setpoint); |
joshuasmth04 | 59:a9b21b3d9afc | 325 | |
joshuasmth04 | 59:a9b21b3d9afc | 326 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 327 | //Following lines are needed to enter into the initiallization flow control loop |
joshuasmth04 | 59:a9b21b3d9afc | 328 | |
caseyquinn | 57:0b554f7aa9a3 | 329 | wait(10); |
joshuasmth04 | 59:a9b21b3d9afc | 330 | |
caseyquinn | 57:0b554f7aa9a3 | 331 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
caseyquinn | 57:0b554f7aa9a3 | 332 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 333 | if(omronVolt<=omronVMin) { |
caseyquinn | 57:0b554f7aa9a3 | 334 | massflow = omronMFMin; |
joshuasmth04 | 59:a9b21b3d9afc | 335 | } else if(omronVolt>=omronVMax) { |
caseyquinn | 57:0b554f7aa9a3 | 336 | massflow = omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 337 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 338 | massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 339 | } |
joshuasmth04 | 59:a9b21b3d9afc | 340 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 341 | digital_pot_set = digital_pot_setpoint; |
joshuasmth04 | 59:a9b21b3d9afc | 342 | wait(5); |
joshuasmth04 | 59:a9b21b3d9afc | 343 | |
joshuasmth04 | 59:a9b21b3d9afc | 344 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 345 | //Sets the flow withen +-1.5% of the desired flow rate based on mass flow |
joshuasmth04 | 59:a9b21b3d9afc | 346 | |
joshuasmth04 | 59:a9b21b3d9afc | 347 | while(abs(deltaMflow)>.015) { |
caseyquinn | 67:300418575137 | 348 | |
joshuasmth04 | 59:a9b21b3d9afc | 349 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 350 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 351 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 59:a9b21b3d9afc | 352 | if(omronVolt<=omronVMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 353 | massflow = omronMFMin; |
joshuasmth04 | 59:a9b21b3d9afc | 354 | } else if(omronVolt>=omronVMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 355 | massflow = omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 356 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 357 | massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 358 | } |
caseyquinn | 57:0b554f7aa9a3 | 359 | |
joshuasmth04 | 59:a9b21b3d9afc | 360 | 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 | 361 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 362 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 363 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 63:66796aef8d68 | 364 | //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 | 365 | 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 | 366 | digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 367 | if(digital_pot_set>=digitalpotMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 368 | digital_pot_set = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 369 | } else if(digital_pot_set<=digitalpotMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 370 | digital_pot_set = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 371 | } |
joshuasmth04 | 59:a9b21b3d9afc | 372 | |
joshuasmth04 | 59:a9b21b3d9afc | 373 | wait(2); |
joshuasmth04 | 59:a9b21b3d9afc | 374 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 375 | wait(1); |
joshuasmth04 | 59:a9b21b3d9afc | 376 | |
joshuasmth04 | 59:a9b21b3d9afc | 377 | |
joshuasmth04 | 59:a9b21b3d9afc | 378 | } |
joshuasmth04 | 59:a9b21b3d9afc | 379 | |
joshuasmth04 | 59:a9b21b3d9afc | 380 | sampledVol = 0.0; |
joshuasmth04 | 59:a9b21b3d9afc | 381 | RGB_LED.set_led(0,1,0); |
caseyquinn | 67:300418575137 | 382 | |
caseyquinn | 67:300418575137 | 383 | |
joshuasmth04 | 59:a9b21b3d9afc | 384 | |
joshuasmth04 | 59:a9b21b3d9afc | 385 | //** end of initalization **// |
joshuasmth04 | 59:a9b21b3d9afc | 386 | |
joshuasmth04 | 59:a9b21b3d9afc | 387 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 388 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 389 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 390 | // Main Control Loop |
joshuasmth04 | 59:a9b21b3d9afc | 391 | |
joshuasmth04 | 62:edc9632bcc43 | 392 | logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c |
joshuasmth04 | 59:a9b21b3d9afc | 393 | while(1) { |
joshuasmth04 | 62:edc9632bcc43 | 394 | //__disable_irq(); // Disable Interrupts |
joshuasmth04 | 62:edc9632bcc43 | 395 | //RTC.get_time(); |
joshuasmth04 | 62:edc9632bcc43 | 396 | //__enable_irq(); // Enable Interrupts |
joshuasmth04 | 62:edc9632bcc43 | 397 | //secondsD = (double)RTC.seconds; |
joshuasmth04 | 62:edc9632bcc43 | 398 | //if(fmod(secondsD,logInerval)==0) { |
joshuasmth04 | 62:edc9632bcc43 | 399 | //log_data(); |
joshuasmth04 | 64:2906b0c48403 | 400 | //} |
caseyquinn | 0:14d46ef4b6cb | 401 | } |
joshuasmth04 | 59:a9b21b3d9afc | 402 | |
caseyquinn | 0:14d46ef4b6cb | 403 | } |
caseyquinn | 0:14d46ef4b6cb | 404 | |
caseyquinn | 0:14d46ef4b6cb | 405 |