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 Volckens Group Sensors

Committer:
caseyquinn
Date:
Fri Jan 22 02:19:28 2016 +0000
Revision:
105:f3be9e0314f7
Parent:
104:c57913399e79
Child:
106:3911b3c7a24e
Updated the log function to the latest control loop added onto Jake's latest version of the app code

Who changed what in which revision?

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