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@62:edc9632bcc43, 2015-07-08 (annotated)
- Committer:
- joshuasmth04
- Date:
- Wed Jul 08 13:56:40 2015 +0000
- Revision:
- 62:edc9632bcc43
- Parent:
- 60:5913d77c8a4a
- Child:
- 63:66796aef8d68
Converted control loop to callbacks, should prevent the i2c communication from being interrupted, and broken. Also added comments about how to disable and enable interrupts, also to prevent communications from being interrupted.
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 | 57:0b554f7aa9a3 | 12 | #include "UPAS_Serial.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 | 57:0b554f7aa9a3 | 32 | UPAS_Serial Menu; |
caseyquinn | 57:0b554f7aa9a3 | 33 | |
caseyquinn | 57:0b554f7aa9a3 | 34 | Timeout stop; //This is the stop call back object |
joshuasmth04 | 62:edc9632bcc43 | 35 | Timeout logg; //This is the logging call back object |
caseyquinn | 0:14d46ef4b6cb | 36 | |
lionberg | 52:80480b2fafba | 37 | //UPAS0012 CALIBRATION TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx' |
lionberg | 52:80480b2fafba | 38 | //mass flow sensor output signal (x) vs. mass flow (y) |
lionberg | 60:5913d77c8a4a | 39 | //y = -0.6154x4 + 3.7873x3 - 7.2564x2 + 7.0202x - 1.9413 |
caseyquinn | 55:f24d70f519cd | 40 | float MF4 = -0.6154; |
caseyquinn | 55:f24d70f519cd | 41 | float MF3 = 3.7873; |
caseyquinn | 55:f24d70f519cd | 42 | float MF2 = -7.2564; |
caseyquinn | 55:f24d70f519cd | 43 | float MF1 = 7.0202; |
caseyquinn | 55:f24d70f519cd | 44 | float MF0 = -1.9413; |
lionberg | 52:80480b2fafba | 45 | //Mass flow sensor polynomial deviation limits |
lionberg | 60:5913d77c8a4a | 46 | float omronVMin = 0.425; //V |
lionberg | 60:5913d77c8a4a | 47 | float omronVMax = 2.005; //V |
lionberg | 60:5913d77c8a4a | 48 | float omronMFMin = 0.002; //g/L |
lionberg | 60:5913d77c8a4a | 49 | float omronMFMax = 3.544; //g/L |
lionberg | 52:80480b2fafba | 50 | //DIGITAL POTENTIOSTAT dig-pot vs m_dot POLYNOMIAL TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx' |
lionberg | 60:5913d77c8a4a | 51 | //y = 5.3839x4 - 51.627x3 + 191.09x2 - 345.9x + 277.63 |
caseyquinn | 55:f24d70f519cd | 52 | float DP4 = 5.3839; |
caseyquinn | 55:f24d70f519cd | 53 | float DP3 = -51.627; |
caseyquinn | 55:f24d70f519cd | 54 | float DP2 = 191.09; |
caseyquinn | 55:f24d70f519cd | 55 | float DP1 = -345.9; |
caseyquinn | 55:f24d70f519cd | 56 | float DP0 = 277.63; |
lionberg | 52:80480b2fafba | 57 | |
caseyquinn | 20:ad9883973d86 | 58 | float press; |
caseyquinn | 20:ad9883973d86 | 59 | float temp; |
caseyquinn | 20:ad9883973d86 | 60 | float rh; |
caseyquinn | 4:69bd7e8a994c | 61 | |
caseyquinn | 20:ad9883973d86 | 62 | int uv; |
caseyquinn | 20:ad9883973d86 | 63 | int vis; |
caseyquinn | 20:ad9883973d86 | 64 | int ir; |
caseyquinn | 8:204c21adf693 | 65 | |
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 | 10:f9cb61b29340 | 70 | float mag_x; |
caseyquinn | 10:f9cb61b29340 | 71 | float mag_y; |
caseyquinn | 10:f9cb61b29340 | 72 | float mag_z; |
caseyquinn | 10:f9cb61b29340 | 73 | |
caseyquinn | 0:14d46ef4b6cb | 74 | int vInReading; |
caseyquinn | 0:14d46ef4b6cb | 75 | int vBlowerReading; |
caseyquinn | 0:14d46ef4b6cb | 76 | int omronDiff; |
lionberg | 52:80480b2fafba | 77 | float omronVolt; //V |
caseyquinn | 20:ad9883973d86 | 78 | int omronReading; |
lionberg | 52:80480b2fafba | 79 | float atmoRho; //g/L |
caseyquinn | 49:19e828650618 | 80 | |
caseyquinn | 20:ad9883973d86 | 81 | float massflow; //g/min |
caseyquinn | 20:ad9883973d86 | 82 | float volflow; //L/min |
joshuasmth04 | 62:edc9632bcc43 | 83 | float volflowSet = 2.0; //L/min |
caseyquinn | 57:0b554f7aa9a3 | 84 | int logInerval = 10; |
caseyquinn | 57:0b554f7aa9a3 | 85 | double secondsD = 0; |
joshuasmth04 | 41:1fb3e0ac6f87 | 86 | float massflowSet; |
caseyquinn | 42:fc2f2b9f07ae | 87 | float deltaVflow = 0.0; |
caseyquinn | 42:fc2f2b9f07ae | 88 | float deltaMflow = 0.0; |
caseyquinn | 47:3146e8c949a9 | 89 | float gainFlow; |
lionberg | 52:80480b2fafba | 90 | float sampledVol; //L, total sampled volume |
caseyquinn | 20:ad9883973d86 | 91 | |
caseyquinn | 55:f24d70f519cd | 92 | int digital_pot_setpoint; //min = 0x7F, max = 0x00 |
caseyquinn | 55:f24d70f519cd | 93 | int digital_pot_set; |
caseyquinn | 55:f24d70f519cd | 94 | int digital_pot_change; |
caseyquinn | 55:f24d70f519cd | 95 | int digitalpotMax = 127; |
caseyquinn | 55:f24d70f519cd | 96 | int digitalpotMin = 2; |
caseyquinn | 55:f24d70f519cd | 97 | |
joshuasmth04 | 59:a9b21b3d9afc | 98 | int dutyUp; |
joshuasmth04 | 59:a9b21b3d9afc | 99 | int dutyDown; |
joshuasmth04 | 59:a9b21b3d9afc | 100 | |
caseyquinn | 57:0b554f7aa9a3 | 101 | 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 | 102 | |
caseyquinn | 57:0b554f7aa9a3 | 103 | char device_name[] = "---------------"; |
caseyquinn | 57:0b554f7aa9a3 | 104 | char filename[] = "/sd/UPAS0012LOG000000000000---------------.txt"; |
caseyquinn | 7:a24d7156bc02 | 105 | 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 | 106 | |
caseyquinn | 7:a24d7156bc02 | 107 | |
joshuasmth04 | 62:edc9632bcc43 | 108 | void check_stop() // this checks if it's time to stop and shutdown |
caseyquinn | 57:0b554f7aa9a3 | 109 | { |
joshuasmth04 | 62:edc9632bcc43 | 110 | RTC.get_time(); //debug |
joshuasmth04 | 62:edc9632bcc43 | 111 | pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debig |
joshuasmth04 | 59:a9b21b3d9afc | 112 | if(RTC.compare(Menu.f_sec, Menu.f_min, Menu.f_hour, Menu.f_day, Menu.f_month, Menu.f_year)) { |
caseyquinn | 57:0b554f7aa9a3 | 113 | pbKill = 0; // this is were we shut everything down |
caseyquinn | 57:0b554f7aa9a3 | 114 | } |
caseyquinn | 57:0b554f7aa9a3 | 115 | stop.detach(); |
caseyquinn | 57:0b554f7aa9a3 | 116 | stop.attach(&check_stop, 5); // checks stop time every 5 seconds |
joshuasmth04 | 62:edc9632bcc43 | 117 | } |
caseyquinn | 58:7239c2ab2b65 | 118 | |
joshuasmth04 | 41:1fb3e0ac6f87 | 119 | |
caseyquinn | 57:0b554f7aa9a3 | 120 | void log_data() |
joshuasmth04 | 59:a9b21b3d9afc | 121 | { |
joshuasmth04 | 59:a9b21b3d9afc | 122 | RTC.get_time(); |
joshuasmth04 | 59:a9b21b3d9afc | 123 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 124 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 125 | |
joshuasmth04 | 59:a9b21b3d9afc | 126 | if(omronVolt<=omronVMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 127 | massflow = omronMFMin; |
joshuasmth04 | 59:a9b21b3d9afc | 128 | } else if(omronVolt>=omronVMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 129 | massflow = omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 130 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 131 | massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 132 | } |
joshuasmth04 | 59:a9b21b3d9afc | 133 | |
joshuasmth04 | 59:a9b21b3d9afc | 134 | 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 | 135 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 136 | sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow); |
joshuasmth04 | 59:a9b21b3d9afc | 137 | deltaVflow = volflow-volflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 138 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 139 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 140 | |
joshuasmth04 | 59:a9b21b3d9afc | 141 | if(abs(deltaMflow)>.025) { |
joshuasmth04 | 59:a9b21b3d9afc | 142 | digital_pot_change = (int)(gainFlow*deltaMflow); |
joshuasmth04 | 59:a9b21b3d9afc | 143 | |
joshuasmth04 | 59:a9b21b3d9afc | 144 | |
joshuasmth04 | 59:a9b21b3d9afc | 145 | if(abs(digital_pot_change)>=50) { |
joshuasmth04 | 59:a9b21b3d9afc | 146 | digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 147 | RGB_LED.set_led(1,0,0); |
joshuasmth04 | 41:1fb3e0ac6f87 | 148 | |
joshuasmth04 | 59:a9b21b3d9afc | 149 | } else if(digital_pot_change+digital_pot_set>=digitalpotMax&abs(digital_pot_change)<50) { |
joshuasmth04 | 59:a9b21b3d9afc | 150 | digital_pot_set = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 151 | RGB_LED.set_led(1,0,0); |
joshuasmth04 | 59:a9b21b3d9afc | 152 | } else if(digital_pot_change+digital_pot_set<=digitalpotMin&abs(digital_pot_change)<50) { |
joshuasmth04 | 59:a9b21b3d9afc | 153 | digital_pot_set = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 154 | RGB_LED.set_led(1,0,0); |
joshuasmth04 | 59:a9b21b3d9afc | 155 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 156 | digital_pot_set = (digital_pot_set+ digital_pot_change); |
joshuasmth04 | 59:a9b21b3d9afc | 157 | RGB_LED.set_led(1,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 158 | } |
joshuasmth04 | 59:a9b21b3d9afc | 159 | |
joshuasmth04 | 59:a9b21b3d9afc | 160 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 161 | |
joshuasmth04 | 59:a9b21b3d9afc | 162 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 163 | RGB_LED.set_led(0,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 164 | } |
joshuasmth04 | 59:a9b21b3d9afc | 165 | |
joshuasmth04 | 59:a9b21b3d9afc | 166 | movementsensor.getACCEL(); |
joshuasmth04 | 59:a9b21b3d9afc | 167 | movementsensor.getCOMPASS(); |
joshuasmth04 | 59:a9b21b3d9afc | 168 | accel_x = movementsensor.AccelData.x; |
joshuasmth04 | 59:a9b21b3d9afc | 169 | accel_y = movementsensor.AccelData.y; |
joshuasmth04 | 59:a9b21b3d9afc | 170 | accel_z = movementsensor.AccelData.z; |
joshuasmth04 | 59:a9b21b3d9afc | 171 | accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0; |
joshuasmth04 | 59:a9b21b3d9afc | 172 | mag_x = movementsensor.MagData.x; |
joshuasmth04 | 59:a9b21b3d9afc | 173 | mag_y = movementsensor.MagData.y; |
joshuasmth04 | 59:a9b21b3d9afc | 174 | mag_z = movementsensor.MagData.z; |
joshuasmth04 | 59:a9b21b3d9afc | 175 | |
joshuasmth04 | 59:a9b21b3d9afc | 176 | vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 |
joshuasmth04 | 59:a9b21b3d9afc | 177 | vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 |
joshuasmth04 | 59:a9b21b3d9afc | 178 | omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 |
joshuasmth04 | 59:a9b21b3d9afc | 179 | press = bmesensor.getPressure(); |
joshuasmth04 | 59:a9b21b3d9afc | 180 | temp = bmesensor.getTemperature()-5.0; |
joshuasmth04 | 59:a9b21b3d9afc | 181 | rh = bmesensor.getHumidity(); |
joshuasmth04 | 59:a9b21b3d9afc | 182 | uv = lightsensor.getUV(); |
joshuasmth04 | 59:a9b21b3d9afc | 183 | vis = lightsensor.getVIS(); |
joshuasmth04 | 59:a9b21b3d9afc | 184 | ir = lightsensor.getIR(); |
joshuasmth04 | 59:a9b21b3d9afc | 185 | |
joshuasmth04 | 59:a9b21b3d9afc | 186 | //Mount the filesystem |
joshuasmth04 | 59:a9b21b3d9afc | 187 | //sd.mount(); |
joshuasmth04 | 59:a9b21b3d9afc | 188 | FILE *fp = fopen(filename, "a"); |
joshuasmth04 | 59:a9b21b3d9afc | 189 | 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\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); |
joshuasmth04 | 59:a9b21b3d9afc | 190 | fclose(fp); |
joshuasmth04 | 59:a9b21b3d9afc | 191 | //Unmount the filesystem |
joshuasmth04 | 59:a9b21b3d9afc | 192 | //sd.unmount(); |
joshuasmth04 | 59:a9b21b3d9afc | 193 | |
joshuasmth04 | 59:a9b21b3d9afc | 194 | wait(1); |
joshuasmth04 | 62:edc9632bcc43 | 195 | logg.detach(); |
joshuasmth04 | 62:edc9632bcc43 | 196 | logg.attach(&log_data, 10); |
joshuasmth04 | 59:a9b21b3d9afc | 197 | } |
caseyquinn | 57:0b554f7aa9a3 | 198 | |
caseyquinn | 57:0b554f7aa9a3 | 199 | int main() |
caseyquinn | 57:0b554f7aa9a3 | 200 | { |
caseyquinn | 57:0b554f7aa9a3 | 201 | |
caseyquinn | 57:0b554f7aa9a3 | 202 | // Setup and Initialization |
caseyquinn | 57:0b554f7aa9a3 | 203 | //---------------------------------------------------------------------------------------------// |
caseyquinn | 57:0b554f7aa9a3 | 204 | |
caseyquinn | 57:0b554f7aa9a3 | 205 | RGB_LED.set_led(0,1,1); |
caseyquinn | 57:0b554f7aa9a3 | 206 | |
joshuasmth04 | 59:a9b21b3d9afc | 207 | Menu.read_menu(E2PROM, refresh_Time, volflowSet, device_name, dutyUp, dutyDown); //Read all data from the EEPROM here |
caseyquinn | 57:0b554f7aa9a3 | 208 | fix_error: |
joshuasmth04 | 59:a9b21b3d9afc | 209 | Menu.Start(pc, E2PROM, RTC, refresh_Time, volflowSet, device_name, dutyUp, dutyDown); //Forces you to open the menu |
joshuasmth04 | 59:a9b21b3d9afc | 210 | Menu.save_menu(E2PROM, refresh_Time, volflowSet, device_name, dutyUp, dutyDown); //Save all data to the EEPROM |
joshuasmth04 | 59:a9b21b3d9afc | 211 | |
caseyquinn | 57:0b554f7aa9a3 | 212 | //Test for errors |
joshuasmth04 | 59:a9b21b3d9afc | 213 | if(RTC.OSF()) { //Don't proceed if the RTC needs reset |
caseyquinn | 57:0b554f7aa9a3 | 214 | RGB_LED.set_led(1,0,0); // error code/color |
lionberg | 60:5913d77c8a4a | 215 | pc.printf("DATE/TIMESTAMP NEEDS TO BE RECALIBRATED!!\r\n"); |
caseyquinn | 57:0b554f7aa9a3 | 216 | goto fix_error; |
caseyquinn | 57:0b554f7aa9a3 | 217 | } |
caseyquinn | 58:7239c2ab2b65 | 218 | RTC.get_time(); |
joshuasmth04 | 59:a9b21b3d9afc | 219 | if(RTC.compare(Menu.f_sec, Menu.f_min, Menu.f_hour, Menu.f_day, Menu.f_month, Menu.f_year)) { //Don't proceed if it's already time to stop |
caseyquinn | 57:0b554f7aa9a3 | 220 | RGB_LED.set_led(1,0,0); // error code/color |
lionberg | 60:5913d77c8a4a | 221 | pc.printf("Please fix the START or STOP times before exiting!\r\n"); |
caseyquinn | 57:0b554f7aa9a3 | 222 | goto fix_error; |
caseyquinn | 57:0b554f7aa9a3 | 223 | } |
joshuasmth04 | 59:a9b21b3d9afc | 224 | pc.printf("You're done, you can now disconect the USB cable."); |
joshuasmth04 | 59:a9b21b3d9afc | 225 | |
caseyquinn | 57:0b554f7aa9a3 | 226 | RGB_LED.set_led(1,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 227 | while(!RTC.compare(Menu.s_sec, Menu.s_min, Menu.s_hour, Menu.s_day, Menu.s_month, Menu.s_year)) { // this while waits for the start time by looping until the start time |
caseyquinn | 57:0b554f7aa9a3 | 228 | // serial print a count down?? |
caseyquinn | 57:0b554f7aa9a3 | 229 | } |
joshuasmth04 | 62:edc9632bcc43 | 230 | stop.attach(&check_stop, 60); // check if we should shut down every 5 seconds, starting 60s after the start. |
caseyquinn | 57:0b554f7aa9a3 | 231 | |
caseyquinn | 57:0b554f7aa9a3 | 232 | |
joshuasmth04 | 59:a9b21b3d9afc | 233 | if(volflowSet==1.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 234 | gainFlow = 100; |
joshuasmth04 | 59:a9b21b3d9afc | 235 | } else if(volflowSet==2.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 236 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 237 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 238 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 239 | } |
joshuasmth04 | 59:a9b21b3d9afc | 240 | |
caseyquinn | 57:0b554f7aa9a3 | 241 | RGB_LED.set_led(1,0,0); |
caseyquinn | 57:0b554f7aa9a3 | 242 | press = bmesensor.getPressure(); |
caseyquinn | 57:0b554f7aa9a3 | 243 | temp = bmesensor.getTemperature(); |
caseyquinn | 57:0b554f7aa9a3 | 244 | rh = bmesensor.getHumidity(); |
caseyquinn | 57:0b554f7aa9a3 | 245 | |
caseyquinn | 57:0b554f7aa9a3 | 246 | 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 | 247 | massflowSet = volflowSet*atmoRho; |
caseyquinn | 57:0b554f7aa9a3 | 248 | //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
caseyquinn | 57:0b554f7aa9a3 | 249 | 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 | 250 | |
joshuasmth04 | 59:a9b21b3d9afc | 251 | if(digital_pot_setpoint>=digitalpotMax) { |
caseyquinn | 57:0b554f7aa9a3 | 252 | digital_pot_setpoint = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 253 | } else if(digital_pot_setpoint<=digitalpotMin) { |
caseyquinn | 57:0b554f7aa9a3 | 254 | digital_pot_setpoint = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 255 | } |
joshuasmth04 | 59:a9b21b3d9afc | 256 | |
caseyquinn | 57:0b554f7aa9a3 | 257 | DigPot.writeRegister(digital_pot_setpoint); |
caseyquinn | 57:0b554f7aa9a3 | 258 | wait(1); |
caseyquinn | 57:0b554f7aa9a3 | 259 | blower = 1; |
caseyquinn | 57:0b554f7aa9a3 | 260 | |
caseyquinn | 57:0b554f7aa9a3 | 261 | RTC.get_time(); |
lionberg | 60:5913d77c8a4a | 262 | 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 | 263 | FILE *fp = fopen(filename, "w"); |
caseyquinn | 57:0b554f7aa9a3 | 264 | fclose(fp); |
joshuasmth04 | 59:a9b21b3d9afc | 265 | //pc.printf("%d\r\n",digital_pot_setpoint); |
joshuasmth04 | 59:a9b21b3d9afc | 266 | |
joshuasmth04 | 59:a9b21b3d9afc | 267 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 268 | //Following lines are needed to enter into the initiallization flow control loop |
joshuasmth04 | 59:a9b21b3d9afc | 269 | |
caseyquinn | 57:0b554f7aa9a3 | 270 | wait(10); |
joshuasmth04 | 59:a9b21b3d9afc | 271 | |
caseyquinn | 57:0b554f7aa9a3 | 272 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
caseyquinn | 57:0b554f7aa9a3 | 273 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 274 | if(omronVolt<=omronVMin) { |
caseyquinn | 57:0b554f7aa9a3 | 275 | massflow = omronMFMin; |
joshuasmth04 | 59:a9b21b3d9afc | 276 | } else if(omronVolt>=omronVMax) { |
caseyquinn | 57:0b554f7aa9a3 | 277 | massflow = omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 278 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 279 | massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 280 | } |
joshuasmth04 | 59:a9b21b3d9afc | 281 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 282 | digital_pot_set = digital_pot_setpoint; |
joshuasmth04 | 59:a9b21b3d9afc | 283 | wait(5); |
joshuasmth04 | 59:a9b21b3d9afc | 284 | |
joshuasmth04 | 59:a9b21b3d9afc | 285 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 286 | //Sets the flow withen +-1.5% of the desired flow rate based on mass flow |
joshuasmth04 | 59:a9b21b3d9afc | 287 | |
joshuasmth04 | 59:a9b21b3d9afc | 288 | while(abs(deltaMflow)>.015) { |
joshuasmth04 | 59:a9b21b3d9afc | 289 | |
joshuasmth04 | 59:a9b21b3d9afc | 290 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 291 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 292 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 59:a9b21b3d9afc | 293 | if(omronVolt<=omronVMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 294 | massflow = omronMFMin; |
joshuasmth04 | 59:a9b21b3d9afc | 295 | } else if(omronVolt>=omronVMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 296 | massflow = omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 297 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 298 | massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; |
joshuasmth04 | 59:a9b21b3d9afc | 299 | } |
caseyquinn | 57:0b554f7aa9a3 | 300 | |
joshuasmth04 | 59:a9b21b3d9afc | 301 | 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 | 302 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 303 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 304 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 62:edc9632bcc43 | 305 | pc.printf("%f,%f,%f,%f,%d,%u,%x\r\n",volflow,massflow,massflowSet,deltaMflow,digital_pot_set,digital_pot_set,digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 306 | digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 307 | if(digital_pot_set>=digitalpotMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 308 | digital_pot_set = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 309 | } else if(digital_pot_set<=digitalpotMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 310 | digital_pot_set = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 311 | } |
joshuasmth04 | 59:a9b21b3d9afc | 312 | |
joshuasmth04 | 59:a9b21b3d9afc | 313 | wait(2); |
joshuasmth04 | 59:a9b21b3d9afc | 314 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 315 | wait(1); |
joshuasmth04 | 59:a9b21b3d9afc | 316 | |
joshuasmth04 | 59:a9b21b3d9afc | 317 | |
joshuasmth04 | 59:a9b21b3d9afc | 318 | } |
joshuasmth04 | 59:a9b21b3d9afc | 319 | |
joshuasmth04 | 59:a9b21b3d9afc | 320 | sampledVol = 0.0; |
joshuasmth04 | 59:a9b21b3d9afc | 321 | RGB_LED.set_led(0,1,0); |
joshuasmth04 | 59:a9b21b3d9afc | 322 | |
joshuasmth04 | 59:a9b21b3d9afc | 323 | //** end of initalization **// |
joshuasmth04 | 59:a9b21b3d9afc | 324 | |
joshuasmth04 | 59:a9b21b3d9afc | 325 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 326 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 327 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 328 | // Main Control Loop |
joshuasmth04 | 59:a9b21b3d9afc | 329 | |
joshuasmth04 | 62:edc9632bcc43 | 330 | logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c |
joshuasmth04 | 59:a9b21b3d9afc | 331 | while(1) { |
joshuasmth04 | 62:edc9632bcc43 | 332 | //__disable_irq(); // Disable Interrupts |
joshuasmth04 | 62:edc9632bcc43 | 333 | //RTC.get_time(); |
joshuasmth04 | 62:edc9632bcc43 | 334 | //__enable_irq(); // Enable Interrupts |
joshuasmth04 | 62:edc9632bcc43 | 335 | //secondsD = (double)RTC.seconds; |
joshuasmth04 | 62:edc9632bcc43 | 336 | //if(fmod(secondsD,logInerval)==0) { |
joshuasmth04 | 62:edc9632bcc43 | 337 | //log_data(); |
caseyquinn | 34:481403146690 | 338 | } |
caseyquinn | 0:14d46ef4b6cb | 339 | } |
joshuasmth04 | 59:a9b21b3d9afc | 340 | |
caseyquinn | 0:14d46ef4b6cb | 341 | } |
caseyquinn | 0:14d46ef4b6cb | 342 | |
caseyquinn | 0:14d46ef4b6cb | 343 |