Program to control UPAS with MicroChip BLE chip + iPhone App

Dependencies:   ADS1115 BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 MicroBLE NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed

Committer:
jelord
Date:
Mon Jan 18 22:09:51 2016 +0000
Revision:
0:2cb2b2ea316f
Child:
1:9fbb5b665068
Code can currently send bytes of info off the EEPROM and receive bytes over BLE.  Currently does not do anything with sent data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jelord 0:2cb2b2ea316f 1 #include "mbed.h"
jelord 0:2cb2b2ea316f 2 #include "SDFileSystem.h"
jelord 0:2cb2b2ea316f 3 #include "Adafruit_ADS1015.h"
jelord 0:2cb2b2ea316f 4 #include "MCP40D17.h"
jelord 0:2cb2b2ea316f 5 #include "STC3100.h"
jelord 0:2cb2b2ea316f 6 #include "LSM303.h"
jelord 0:2cb2b2ea316f 7 #include "BME280.h"
jelord 0:2cb2b2ea316f 8 #include "SI1145.h"
jelord 0:2cb2b2ea316f 9 #include "NCP5623BMUTBG.h"
jelord 0:2cb2b2ea316f 10 #include "CronoDot.h"
jelord 0:2cb2b2ea316f 11 #include "EEPROM.h"
jelord 0:2cb2b2ea316f 12 #include "Calibration.h"
jelord 0:2cb2b2ea316f 13
jelord 0:2cb2b2ea316f 14 #define SCL 20
jelord 0:2cb2b2ea316f 15 #define SDA 22
jelord 0:2cb2b2ea316f 16
jelord 0:2cb2b2ea316f 17
jelord 0:2cb2b2ea316f 18 uint8_t startAndEndTime[12] = {0,};
jelord 0:2cb2b2ea316f 19 uint8_t logIntervalReadOut[1] = {0,};
jelord 0:2cb2b2ea316f 20
jelord 0:2cb2b2ea316f 21 I2C i2c(p22, p20);
jelord 0:2cb2b2ea316f 22 Adafruit_ADS1115 ads(&i2c);
jelord 0:2cb2b2ea316f 23 MCP40D17 DigPot(&i2c);
jelord 0:2cb2b2ea316f 24 BME280 bmesensor(p22, p20);
jelord 0:2cb2b2ea316f 25 STC3100 gasG(p22, p20);
jelord 0:2cb2b2ea316f 26
jelord 0:2cb2b2ea316f 27 Serial microChannel(P0_9,P0_11);
jelord 0:2cb2b2ea316f 28
jelord 0:2cb2b2ea316f 29 DigitalOut blower(p29, 0);
jelord 0:2cb2b2ea316f 30 DigitalOut pbKill(p18, 1);
jelord 0:2cb2b2ea316f 31 LSM303 movementsensor(p22, p20);
jelord 0:2cb2b2ea316f 32 SI1145 lightsensor(p22, p20);
jelord 0:2cb2b2ea316f 33 NCP5623BMUTBG RGB_LED(p22, p20);
jelord 0:2cb2b2ea316f 34 CronoDot RTC(p22, p20);
jelord 0:2cb2b2ea316f 35 EEPROM E2PROM(p22, p20);
jelord 0:2cb2b2ea316f 36 DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications
jelord 0:2cb2b2ea316f 37 Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option
jelord 0:2cb2b2ea316f 38
jelord 0:2cb2b2ea316f 39 Timeout stop; //This is the stop call back object
jelord 0:2cb2b2ea316f 40 Timeout logg; //This is the logging call back object
jelord 0:2cb2b2ea316f 41
jelord 0:2cb2b2ea316f 42 uint16_t serial_num = 1; // Default serial/calibration number
jelord 0:2cb2b2ea316f 43 int RunReady =0;
jelord 0:2cb2b2ea316f 44
jelord 0:2cb2b2ea316f 45
jelord 0:2cb2b2ea316f 46 float press;
jelord 0:2cb2b2ea316f 47 float temp;
jelord 0:2cb2b2ea316f 48 float rh;
jelord 0:2cb2b2ea316f 49
jelord 0:2cb2b2ea316f 50 int uv;
jelord 0:2cb2b2ea316f 51 int vis;
jelord 0:2cb2b2ea316f 52 int ir;
jelord 0:2cb2b2ea316f 53
jelord 0:2cb2b2ea316f 54 float compass;
jelord 0:2cb2b2ea316f 55 float accel_x;
jelord 0:2cb2b2ea316f 56 float accel_y;
jelord 0:2cb2b2ea316f 57 float accel_z;
jelord 0:2cb2b2ea316f 58 float accel_comp;
jelord 0:2cb2b2ea316f 59 float mag_x;
jelord 0:2cb2b2ea316f 60 float mag_y;
jelord 0:2cb2b2ea316f 61 float mag_z;
jelord 0:2cb2b2ea316f 62
jelord 0:2cb2b2ea316f 63 int vInReading;
jelord 0:2cb2b2ea316f 64 int vBlowerReading;
jelord 0:2cb2b2ea316f 65 int omronDiff;
jelord 0:2cb2b2ea316f 66 float omronVolt; //V
jelord 0:2cb2b2ea316f 67 int omronReading;
jelord 0:2cb2b2ea316f 68 float atmoRho; //g/L
jelord 0:2cb2b2ea316f 69
jelord 0:2cb2b2ea316f 70 float massflow; //g/min
jelord 0:2cb2b2ea316f 71 float volflow; //L/min
jelord 0:2cb2b2ea316f 72 float volflowSet = 1.0; //L/min
jelord 0:2cb2b2ea316f 73 int logInerval = 10; //seconds
jelord 0:2cb2b2ea316f 74 double secondsD = 0;
jelord 0:2cb2b2ea316f 75 float massflowSet;
jelord 0:2cb2b2ea316f 76 float deltaVflow = 0.0;
jelord 0:2cb2b2ea316f 77 float deltaMflow = 0.0;
jelord 0:2cb2b2ea316f 78 float gainFlow;
jelord 0:2cb2b2ea316f 79 float sampledVol; //L, total sampled volume
jelord 0:2cb2b2ea316f 80
jelord 0:2cb2b2ea316f 81 int digital_pot_setpoint; //min = 0x7F, max = 0x00
jelord 0:2cb2b2ea316f 82 int digital_pot_set;
jelord 0:2cb2b2ea316f 83 int digital_pot_change;
jelord 0:2cb2b2ea316f 84 int digitalpotMax = 127;
jelord 0:2cb2b2ea316f 85 int digitalpotMin = 2;
jelord 0:2cb2b2ea316f 86
jelord 0:2cb2b2ea316f 87 int dutyUp;
jelord 0:2cb2b2ea316f 88 int dutyDown;
jelord 0:2cb2b2ea316f 89
jelord 0:2cb2b2ea316f 90 // variables are only place holders for the US_Menu //
jelord 0:2cb2b2ea316f 91 int refreshtime;
jelord 0:2cb2b2ea316f 92 float home_lat, home_lon, work_lat, work_lon;
jelord 0:2cb2b2ea316f 93 //*************************************************//
jelord 0:2cb2b2ea316f 94
jelord 0:2cb2b2ea316f 95 //int refresh_Time = 10; // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
jelord 0:2cb2b2ea316f 96
jelord 0:2cb2b2ea316f 97 char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
jelord 0:2cb2b2ea316f 98 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)
jelord 0:2cb2b2ea316f 99
jelord 0:2cb2b2ea316f 100
jelord 0:2cb2b2ea316f 101
jelord 0:2cb2b2ea316f 102 void check_stop() // this checks if it's time to stop and shutdown
jelord 0:2cb2b2ea316f 103 {
jelord 0:2cb2b2ea316f 104
jelord 0:2cb2b2ea316f 105 if(RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) {
jelord 0:2cb2b2ea316f 106 pbKill = 0; // this is were we shut everything down
jelord 0:2cb2b2ea316f 107 }
jelord 0:2cb2b2ea316f 108 stop.detach();
jelord 0:2cb2b2ea316f 109 stop.attach(&check_stop, 9);
jelord 0:2cb2b2ea316f 110
jelord 0:2cb2b2ea316f 111 }
jelord 0:2cb2b2ea316f 112
jelord 0:2cb2b2ea316f 113
jelord 0:2cb2b2ea316f 114 void log_data()
jelord 0:2cb2b2ea316f 115 {
jelord 0:2cb2b2ea316f 116 logg.detach();
jelord 0:2cb2b2ea316f 117
jelord 0:2cb2b2ea316f 118 logg.attach(&log_data, logInerval); // reading and logging data must take significintly less than 0.5s. This can be increased.
jelord 0:2cb2b2ea316f 119
jelord 0:2cb2b2ea316f 120 RTC.get_time();
jelord 0:2cb2b2ea316f 121
jelord 0:2cb2b2ea316f 122 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
jelord 0:2cb2b2ea316f 123 omronVolt = (omronReading*4.096)/(32768*2);
jelord 0:2cb2b2ea316f 124
jelord 0:2cb2b2ea316f 125 if(omronVolt<=calibrations.omronVMin) {
jelord 0:2cb2b2ea316f 126 massflow = calibrations.omronMFMin;
jelord 0:2cb2b2ea316f 127 } else if(omronVolt>=calibrations.omronVMax) {
jelord 0:2cb2b2ea316f 128 massflow = calibrations.omronMFMax;
jelord 0:2cb2b2ea316f 129 } else {
jelord 0:2cb2b2ea316f 130 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
jelord 0:2cb2b2ea316f 131 }
jelord 0:2cb2b2ea316f 132
jelord 0:2cb2b2ea316f 133 atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
jelord 0:2cb2b2ea316f 134 volflow = massflow/atmoRho;
jelord 0:2cb2b2ea316f 135 sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow);
jelord 0:2cb2b2ea316f 136 deltaVflow = volflow-volflowSet;
jelord 0:2cb2b2ea316f 137 massflowSet = volflowSet*atmoRho;
jelord 0:2cb2b2ea316f 138 deltaMflow = massflow-massflowSet;
jelord 0:2cb2b2ea316f 139 if(abs(deltaMflow)>.025) {
jelord 0:2cb2b2ea316f 140 digital_pot_change = (int)(gainFlow*deltaMflow);
jelord 0:2cb2b2ea316f 141
jelord 0:2cb2b2ea316f 142
jelord 0:2cb2b2ea316f 143 if(abs(digital_pot_change)>=50) {
jelord 0:2cb2b2ea316f 144 digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow)));
jelord 0:2cb2b2ea316f 145 RGB_LED.set_led(1,0,0);
jelord 0:2cb2b2ea316f 146
jelord 0:2cb2b2ea316f 147 } else if(digital_pot_change+digital_pot_set>=digitalpotMax&abs(digital_pot_change)<50) {
jelord 0:2cb2b2ea316f 148 digital_pot_set = digitalpotMax;
jelord 0:2cb2b2ea316f 149 RGB_LED.set_led(1,0,0);
jelord 0:2cb2b2ea316f 150 } else if(digital_pot_change+digital_pot_set<=digitalpotMin&abs(digital_pot_change)<50) {
jelord 0:2cb2b2ea316f 151 digital_pot_set = digitalpotMin;
jelord 0:2cb2b2ea316f 152 RGB_LED.set_led(1,0,0);
jelord 0:2cb2b2ea316f 153 } else {
jelord 0:2cb2b2ea316f 154 digital_pot_set = (digital_pot_set+ digital_pot_change);
jelord 0:2cb2b2ea316f 155 RGB_LED.set_led(1,1,0);
jelord 0:2cb2b2ea316f 156 }
jelord 0:2cb2b2ea316f 157
jelord 0:2cb2b2ea316f 158 DigPot.writeRegister(digital_pot_set);
jelord 0:2cb2b2ea316f 159
jelord 0:2cb2b2ea316f 160 } else {
jelord 0:2cb2b2ea316f 161 RGB_LED.set_led(0,1,0);
jelord 0:2cb2b2ea316f 162 }
jelord 0:2cb2b2ea316f 163 movementsensor.getACCEL();
jelord 0:2cb2b2ea316f 164 movementsensor.getCOMPASS();
jelord 0:2cb2b2ea316f 165 compass = movementsensor.getCOMPASS_HEADING();
jelord 0:2cb2b2ea316f 166 accel_x = movementsensor.AccelData.x;
jelord 0:2cb2b2ea316f 167 accel_y = movementsensor.AccelData.y;
jelord 0:2cb2b2ea316f 168 accel_z = movementsensor.AccelData.z;
jelord 0:2cb2b2ea316f 169 accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0;
jelord 0:2cb2b2ea316f 170 mag_x = movementsensor.MagData.x;
jelord 0:2cb2b2ea316f 171 mag_y = movementsensor.MagData.y;
jelord 0:2cb2b2ea316f 172 mag_z = movementsensor.MagData.z;
jelord 0:2cb2b2ea316f 173 vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
jelord 0:2cb2b2ea316f 174 vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
jelord 0:2cb2b2ea316f 175 omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
jelord 0:2cb2b2ea316f 176 press = bmesensor.getPressure();
jelord 0:2cb2b2ea316f 177 temp = bmesensor.getTemperature()-5.0;
jelord 0:2cb2b2ea316f 178 rh = bmesensor.getHumidity();
jelord 0:2cb2b2ea316f 179 uv = lightsensor.getUV();
jelord 0:2cb2b2ea316f 180 vis = lightsensor.getVIS();
jelord 0:2cb2b2ea316f 181 ir = lightsensor.getIR();
jelord 0:2cb2b2ea316f 182 FILE *fp = fopen(filename, "a");
jelord 0:2cb2b2ea316f 183 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);
jelord 0:2cb2b2ea316f 184 fclose(fp);
jelord 0:2cb2b2ea316f 185
jelord 0:2cb2b2ea316f 186 }
jelord 0:2cb2b2ea316f 187
jelord 0:2cb2b2ea316f 188 static uint8_t rx_buf[20];
jelord 0:2cb2b2ea316f 189 static uint8_t rx_len=0;
jelord 0:2cb2b2ea316f 190
jelord 0:2cb2b2ea316f 191 void uartMicro(void){
jelord 0:2cb2b2ea316f 192 int j = 0;
jelord 0:2cb2b2ea316f 193 while(microChannel.readable()){
jelord 0:2cb2b2ea316f 194 rx_buf[rx_len++] = microChannel.getc();
jelord 0:2cb2b2ea316f 195 if(rx_len>=20 ||rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')break;
jelord 0:2cb2b2ea316f 196 j++;
jelord 0:2cb2b2ea316f 197 }
jelord 0:2cb2b2ea316f 198 for(int i=0; i<rx_len; i++){
jelord 0:2cb2b2ea316f 199 microChannel.putc(rx_buf[i]);
jelord 0:2cb2b2ea316f 200 }
jelord 0:2cb2b2ea316f 201 rx_len = 0;
jelord 0:2cb2b2ea316f 202
jelord 0:2cb2b2ea316f 203 }
jelord 0:2cb2b2ea316f 204 /*EEPROM ADDRESSING:
jelord 0:2cb2b2ea316f 205 0:Status bit-Unused
jelord 0:2cb2b2ea316f 206 1-15:Device Name
jelord 0:2cb2b2ea316f 207 16-19:Flow Rate
jelord 0:2cb2b2ea316f 208 20: Data Log Interval
jelord 0:2cb2b2ea316f 209 21-26: Start Time: ssmmHHddMMyy
jelord 0:2cb2b2ea316f 210 27-32: Stop Time: ssmmHHddMMyy
jelord 0:2cb2b2ea316f 211 33: Duty Up
jelord 0:2cb2b2ea316f 212 34: Duty Down
jelord 0:2cb2b2ea316f 213 35-38: Home Latitude
jelord 0:2cb2b2ea316f 214 39-42: Home Longitude
jelord 0:2cb2b2ea316f 215 43-46: Work Latitude
jelord 0:2cb2b2ea316f 216 47-50: Work Longitude
jelord 0:2cb2b2ea316f 217 51: Runready: Currently useless, should be 0
jelord 0:2cb2b2ea316f 218 52-53: Device Calibration
jelord 0:2cb2b2ea316f 219 54: Consider RunReady
jelord 0:2cb2b2ea316f 220 55-56: Menu Options
jelord 0:2cb2b2ea316f 221 57+ Nothing*/
jelord 0:2cb2b2ea316f 222 int main()
jelord 0:2cb2b2ea316f 223 {
jelord 0:2cb2b2ea316f 224 RGB_LED.set_led(1,1,1);
jelord 0:2cb2b2ea316f 225 microChannel.baud(115200);
jelord 0:2cb2b2ea316f 226 microChannel.attach(uartMicro,microChannel.RxIrq);
jelord 0:2cb2b2ea316f 227 // Setup and Initialization
jelord 0:2cb2b2ea316f 228 //---------------------------------------------------------------------------------------------//
jelord 0:2cb2b2ea316f 229 RTC.get_time();
jelord 0:2cb2b2ea316f 230 uint8_t rtcPassValues[7] = {0x00,RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
jelord 0:2cb2b2ea316f 231 uint8_t sampleTimePassValues[13] = {0x01,};
jelord 0:2cb2b2ea316f 232 uint8_t subjectLabelOriginal[9] = {0x02,};
jelord 0:2cb2b2ea316f 233 uint8_t dataLogOriginal[2] = {0x03,};
jelord 0:2cb2b2ea316f 234 uint8_t flowRateOriginal[5] = {0x04,};
jelord 0:2cb2b2ea316f 235 //uint8_t presetRunModeCheck[1] = {0,}; Commented and currently unused to prevent mem issues
jelord 0:2cb2b2ea316f 236 E2PROM.read(0x00015, sampleTimePassValues+1, 12);
jelord 0:2cb2b2ea316f 237 E2PROM.read(0x00001, subjectLabelOriginal+1,8);
jelord 0:2cb2b2ea316f 238 E2PROM.read(0x00014,dataLogOriginal+1,1);
jelord 0:2cb2b2ea316f 239 E2PROM.read(0x00010,flowRateOriginal+1,4);
jelord 0:2cb2b2ea316f 240 //E2PROM.read(0x00033,presetRunModeCheck,1); //commented out mem issue
jelord 0:2cb2b2ea316f 241
jelord 0:2cb2b2ea316f 242 while (1) {
jelord 0:2cb2b2ea316f 243
jelord 0:2cb2b2ea316f 244 for(int i=0; i<7; i++){
jelord 0:2cb2b2ea316f 245 microChannel.putc(rtcPassValues[i]);
jelord 0:2cb2b2ea316f 246 }
jelord 0:2cb2b2ea316f 247 wait(2);
jelord 0:2cb2b2ea316f 248
jelord 0:2cb2b2ea316f 249 for(int i=0; i<13; i++){
jelord 0:2cb2b2ea316f 250 microChannel.putc(sampleTimePassValues[i]);
jelord 0:2cb2b2ea316f 251 }
jelord 0:2cb2b2ea316f 252 wait(2);
jelord 0:2cb2b2ea316f 253
jelord 0:2cb2b2ea316f 254 for(int i=0; i<9; i++){
jelord 0:2cb2b2ea316f 255 microChannel.putc(subjectLabelOriginal[i]);
jelord 0:2cb2b2ea316f 256 }
jelord 0:2cb2b2ea316f 257 wait(2);
jelord 0:2cb2b2ea316f 258
jelord 0:2cb2b2ea316f 259 for(int i=0; i<2; i++){
jelord 0:2cb2b2ea316f 260 microChannel.putc(dataLogOriginal[i]);
jelord 0:2cb2b2ea316f 261 }
jelord 0:2cb2b2ea316f 262 wait(2);
jelord 0:2cb2b2ea316f 263
jelord 0:2cb2b2ea316f 264 for(int i=0; i<5; i++){
jelord 0:2cb2b2ea316f 265 microChannel.putc(flowRateOriginal[i]);
jelord 0:2cb2b2ea316f 266 }
jelord 0:2cb2b2ea316f 267 wait(2);
jelord 0:2cb2b2ea316f 268
jelord 0:2cb2b2ea316f 269 if(RunReady==10){ //Check to see if app is done with configurations
jelord 0:2cb2b2ea316f 270 break;
jelord 0:2cb2b2ea316f 271 }
jelord 0:2cb2b2ea316f 272
jelord 0:2cb2b2ea316f 273 if(RunReady==12){ //If 24 hour mode has been set, then shut down the UPAS for automatic start later.
jelord 0:2cb2b2ea316f 274 pbKill = 0;
jelord 0:2cb2b2ea316f 275 }
jelord 0:2cb2b2ea316f 276 }
jelord 0:2cb2b2ea316f 277
jelord 0:2cb2b2ea316f 278 E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM
jelord 0:2cb2b2ea316f 279 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 0:2cb2b2ea316f 280 wait(0.5);
jelord 0:2cb2b2ea316f 281
jelord 0:2cb2b2ea316f 282 RTC.get_time();
jelord 0:2cb2b2ea316f 283
jelord 0:2cb2b2ea316f 284 }
jelord 0:2cb2b2ea316f 285
jelord 0:2cb2b2ea316f 286
jelord 0:2cb2b2ea316f 287 RGB_LED.set_led(0,1,0);
jelord 0:2cb2b2ea316f 288
jelord 0:2cb2b2ea316f 289 //Get the proper serial number
jelord 0:2cb2b2ea316f 290 E2PROM.read(0x00034, flowRateOriginal,2);
jelord 0:2cb2b2ea316f 291 serial_num = ((uint16_t)flowRateOriginal[1] << 8) | flowRateOriginal[0];
jelord 0:2cb2b2ea316f 292 calibrations.initialize(serial_num);
jelord 0:2cb2b2ea316f 293 blower=1;
jelord 0:2cb2b2ea316f 294 E2PROM.read(0x00014,logIntervalReadOut,1);
jelord 0:2cb2b2ea316f 295 logInerval = logIntervalReadOut[0];
jelord 0:2cb2b2ea316f 296
jelord 0:2cb2b2ea316f 297 RunReady = 0;
jelord 0:2cb2b2ea316f 298
jelord 0:2cb2b2ea316f 299
jelord 0:2cb2b2ea316f 300 stop.attach(&check_stop, 30); // check if we should shut down every 9 seconds, starting 60s after the start.
jelord 0:2cb2b2ea316f 301
jelord 0:2cb2b2ea316f 302 //Use the flow rate value stored in eeprom
jelord 0:2cb2b2ea316f 303 E2PROM.read(0x00010,flowRateOriginal,4);
jelord 0:2cb2b2ea316f 304 E2PROM.byteToFloat(flowRateOriginal, &volflowSet);
jelord 0:2cb2b2ea316f 305
jelord 0:2cb2b2ea316f 306 if(volflowSet<=1.0) {
jelord 0:2cb2b2ea316f 307 gainFlow = 100;
jelord 0:2cb2b2ea316f 308 } else if(volflowSet>=2.0) {
jelord 0:2cb2b2ea316f 309 gainFlow = 25;
jelord 0:2cb2b2ea316f 310 } else {
jelord 0:2cb2b2ea316f 311 gainFlow = 25;
jelord 0:2cb2b2ea316f 312 }
jelord 0:2cb2b2ea316f 313
jelord 0:2cb2b2ea316f 314 RGB_LED.set_led(1,0,0);
jelord 0:2cb2b2ea316f 315 press = bmesensor.getPressure();
jelord 0:2cb2b2ea316f 316 temp = bmesensor.getTemperature();
jelord 0:2cb2b2ea316f 317 rh = bmesensor.getHumidity();
jelord 0:2cb2b2ea316f 318
jelord 0:2cb2b2ea316f 319 atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
jelord 0:2cb2b2ea316f 320 massflowSet = volflowSet*atmoRho;
jelord 0:2cb2b2ea316f 321 //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
jelord 0:2cb2b2ea316f 322
jelord 0:2cb2b2ea316f 323
jelord 0:2cb2b2ea316f 324 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
jelord 0:2cb2b2ea316f 325
jelord 0:2cb2b2ea316f 326 if(digital_pot_setpoint>=digitalpotMax) {
jelord 0:2cb2b2ea316f 327 digital_pot_setpoint = digitalpotMax;
jelord 0:2cb2b2ea316f 328 } else if(digital_pot_setpoint<=digitalpotMin) {
jelord 0:2cb2b2ea316f 329 digital_pot_setpoint = digitalpotMin;
jelord 0:2cb2b2ea316f 330 }
jelord 0:2cb2b2ea316f 331
jelord 0:2cb2b2ea316f 332 DigPot.writeRegister(digital_pot_setpoint);
jelord 0:2cb2b2ea316f 333 wait(1);
jelord 0:2cb2b2ea316f 334 blower = 1;
jelord 0:2cb2b2ea316f 335
jelord 0:2cb2b2ea316f 336
jelord 0:2cb2b2ea316f 337 E2PROM.read(0x00001, subjectLabelOriginal,8);
jelord 0:2cb2b2ea316f 338 //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 0:2cb2b2ea316f 339 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]);
jelord 0:2cb2b2ea316f 340 FILE *fp = fopen(filename, "w");
jelord 0:2cb2b2ea316f 341 fclose(fp);
jelord 0:2cb2b2ea316f 342
jelord 0:2cb2b2ea316f 343 //---------------------------------------------------------------------------------------------//
jelord 0:2cb2b2ea316f 344 //Following lines are needed to enter into the initiallization flow control loop
jelord 0:2cb2b2ea316f 345
jelord 0:2cb2b2ea316f 346 wait(10);
jelord 0:2cb2b2ea316f 347
jelord 0:2cb2b2ea316f 348 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
jelord 0:2cb2b2ea316f 349 omronVolt = (omronReading*4.096)/(32768*2);
jelord 0:2cb2b2ea316f 350 if(omronVolt<=calibrations.omronVMin) {
jelord 0:2cb2b2ea316f 351 massflow = calibrations.omronMFMin;
jelord 0:2cb2b2ea316f 352 } else if(omronVolt>=calibrations.omronVMax) {
jelord 0:2cb2b2ea316f 353 massflow = calibrations.omronMFMax;
jelord 0:2cb2b2ea316f 354 } else {
jelord 0:2cb2b2ea316f 355 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
jelord 0:2cb2b2ea316f 356 }
jelord 0:2cb2b2ea316f 357 deltaMflow = massflow-massflowSet;
jelord 0:2cb2b2ea316f 358 digital_pot_set = digital_pot_setpoint;
jelord 0:2cb2b2ea316f 359 wait(5);
jelord 0:2cb2b2ea316f 360
jelord 0:2cb2b2ea316f 361 //---------------------------------------------------------------------------------------------//
jelord 0:2cb2b2ea316f 362 //Sets the flow withen +-1.5% of the desired flow rate based on mass flow
jelord 0:2cb2b2ea316f 363
jelord 0:2cb2b2ea316f 364 while(abs(deltaMflow)>.015) {
jelord 0:2cb2b2ea316f 365
jelord 0:2cb2b2ea316f 366 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
jelord 0:2cb2b2ea316f 367 omronVolt = (omronReading*4.096)/(32768*2);
jelord 0:2cb2b2ea316f 368 //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
jelord 0:2cb2b2ea316f 369 if(omronVolt<=calibrations.omronVMin) {
jelord 0:2cb2b2ea316f 370 massflow = calibrations.omronMFMin;
jelord 0:2cb2b2ea316f 371 } else if(omronVolt>=calibrations.omronVMax) {
jelord 0:2cb2b2ea316f 372 massflow = calibrations.omronMFMax;
jelord 0:2cb2b2ea316f 373 } else {
jelord 0:2cb2b2ea316f 374 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
jelord 0:2cb2b2ea316f 375 }
jelord 0:2cb2b2ea316f 376
jelord 0:2cb2b2ea316f 377 atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
jelord 0:2cb2b2ea316f 378 volflow = massflow/atmoRho;
jelord 0:2cb2b2ea316f 379 massflowSet = volflowSet*atmoRho;
jelord 0:2cb2b2ea316f 380 deltaMflow = massflow-massflowSet;
jelord 0:2cb2b2ea316f 381
jelord 0:2cb2b2ea316f 382 digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
jelord 0:2cb2b2ea316f 383 if(digital_pot_set>=digitalpotMax) {
jelord 0:2cb2b2ea316f 384 digital_pot_set = digitalpotMax;
jelord 0:2cb2b2ea316f 385 } else if(digital_pot_set<=digitalpotMin) {
jelord 0:2cb2b2ea316f 386 digital_pot_set = digitalpotMin;
jelord 0:2cb2b2ea316f 387 }
jelord 0:2cb2b2ea316f 388
jelord 0:2cb2b2ea316f 389 wait(2);
jelord 0:2cb2b2ea316f 390 DigPot.writeRegister(digital_pot_set);
jelord 0:2cb2b2ea316f 391 wait(1);
jelord 0:2cb2b2ea316f 392
jelord 0:2cb2b2ea316f 393
jelord 0:2cb2b2ea316f 394 }
jelord 0:2cb2b2ea316f 395
jelord 0:2cb2b2ea316f 396 sampledVol = 0.0;
jelord 0:2cb2b2ea316f 397 RGB_LED.set_led(0,1,0);
jelord 0:2cb2b2ea316f 398
jelord 0:2cb2b2ea316f 399
jelord 0:2cb2b2ea316f 400
jelord 0:2cb2b2ea316f 401 //** end of initalization **//
jelord 0:2cb2b2ea316f 402 //---------------------------------------------------------------------------------------------//
jelord 0:2cb2b2ea316f 403 //---------------------------------------------------------------------------------------------//
jelord 0:2cb2b2ea316f 404 // Main Control Loop
jelord 0:2cb2b2ea316f 405
jelord 0:2cb2b2ea316f 406
jelord 0:2cb2b2ea316f 407 logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c
jelord 0:2cb2b2ea316f 408
jelord 0:2cb2b2ea316f 409 }
jelord 0:2cb2b2ea316f 410