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

main.cpp

Committer:
caseyquinn
Date:
2015-06-11
Revision:
42:fc2f2b9f07ae
Parent:
41:1fb3e0ac6f87
Child:
43:da0708632a21
Child:
50:dddf01867e85

File content as of revision 42:fc2f2b9f07ae:

#include "mbed.h"
#include "SDFileSystem.h"
#include "Adafruit_ADS1015.h"
#include "MCP40D17.h"
#include "STC3100.h"
#include "LSM303.h"
#include "BME280.h"
#include "SI1145.h"
#include "NCP5623BMUTBG.h"
#include "CronoDot.h"

#define SERIAL_BAUD_RATE    9600
#define SCL         20
#define SDA         22
#define Crono       0xD0       //D0 for the chronoDot

I2C i2c(p22, p20);
Adafruit_ADS1115 ads(&i2c);
MCP40D17 DigPot(&i2c);
BME280 bmesensor(p22, p20);
STC3100 gasG(p22, p20);
Serial  pc(USBTX, USBRX);
DigitalOut blower(p29, 0);
DigitalOut pbKill(p18, 1);
LSM303 movementsensor(p22, p20);
SI1145 lightsensor(p22, p20);
NCP5623BMUTBG RGB_LED(p22, p20);
CronoDot RTC(p22, p20);

float press;
float temp;
float rh;

int uv;
int vis;
int ir;

float accel_x;
float accel_y;
float accel_z;
float mag_x;
float mag_y;
float mag_z;


int vInReading;
int vBlowerReading;
int omronDiff;

int omronReading;
float omronVolt; //V
float atmoRho; //g/L
float MF0 = 7.8618;
float MF1 = -33.538;
float MF2 = 53.545;
float MF3 = -34.801;
float MF4 = 8.3314;
float massflow; //g/min
float volflow; //L/min
float volflowSet = 1.0; //L/min
float massflowSet;
float deltaVflow = 0.0;
float deltaMflow = 0.0;
float gainFlow = 0.5;

int digital_pot_setpoint; //min = 0x7F, max = 0x00
int digital_pot_setpointCK;
int digital_pot_delta;
float DP0 = 285.74;
float DP1 = -461.55;
float DP2 = 352.54;
float DP3 = -133.93;
float DP4 = 19.476;

char filename[] = "/sd/UPAS0010LOG000000000000.txt";
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)

char Seconds = 0; //Seconds
char Minutes = 0; //Minutes
char Hour    = 0; //Hour
char Date    = 0; //Date
char Month   = 0; //Month
char Year    = 0; //Year

double secondsD = 0;
char * RTCtime;


int main()
{
    RGB_LED.set_led(1,1,1);
    press = bmesensor.getPressure();
    temp = bmesensor.getTemperature();
    rh = bmesensor.getHumidity();

    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));
    massflowSet = volflowSet*atmoRho;
    //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
    digital_pot_setpoint = (int)floor(DP4*pow(massflowSet,4)+DP3*pow(massflowSet,3)+DP2*pow(massflowSet,2)+DP1*massflowSet+DP0); //min = 0x7F, max = 0x00

    DigPot.writeRegister(digital_pot_setpoint);
    wait(1);
    blower = 1;

    RTCtime = RTC.get_time();
    sprintf(filename, "/sd/UPAS0010LOG_%02d-%02d-%02d_%02d-%02d-%02d.txt",RTCtime[5],RTCtime[4],RTCtime[3],RTCtime[2],RTCtime[1],RTCtime[0]);
    FILE *fp = fopen(filename, "w");
    fclose(fp);

    wait(5);
    
    while(1) {

        RGB_LED.set_led(0,1,0);
        RTCtime = RTC.get_time();   // the way the variable RTCtime works you must save the variables in normal chars or weird things happen
        Seconds = RTCtime[0];//Seconds 
        Minutes = RTCtime[1];//Minutes
        Hour    = RTCtime[2];//Hour
        Date    = RTCtime[3];//Date
        Month   = RTCtime[4];//Month
        Year    = RTCtime[5];//Year
        
        secondsD = (double)RTCtime[0];
        
        if(fmod(secondsD,10)==0) {
            
            omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
            omronVolt = (omronReading*4.096)/(32768*2);

            //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet
            //UPAS0001=-2.8112*L14^4+15.636*L14^3-30.192*L14^2+25.945*L14-7.6599
            //massflow = -2.8112*pow(omronVolt,(float)4)+15.636*pow(omronVolt,(float)3)-30.192*pow(omronVolt,(float)2)+25.945*omronVolt-7.6599;
            //UPAS0002=-1.4365*L14^4+8.4231*L14^3-16.754*L14^2+15.071*L14-4.5382
            //massflow = -1.4365*pow(omronVolt,(float)4)+8.4231*pow(omronVolt,(float)3)-16.754*pow(omronVolt,(float)2)+15.071*omronVolt-4.5382;
            //UPAS0003=-2.7391*L14^4+15.215*L14^3-29.522*L14^2+25.424*L14-7.5516
            //massflow = -2.7391*pow(omronVolt,(float)4)+15.215*pow(omronVolt,(float)3)-29.522*pow(omronVolt,(float)2)+25.424*omronVolt-7.5516;
            //UPAS0004 =-1.5374*L14^4+9.2898*L14^3-18.894*L14^2+17.112*L14-5.1938
            //massflow = -1.5374*pow(omronVolt,(float)4)+9.2898*pow(omronVolt,(float)3)-18.894*pow(omronVolt,(float)2)+17.112*omronVolt-5.1938;
            //UPAS0005 =-0.4654*L14^4+3.507*L14^3-7.7254*L14^2+7.8831*L14-2.4317
            //massflow = -0.4654*pow(omronVolt,(float)4)+3.507*pow(omronVolt,(float)3)-7.7254*pow(omronVolt,(float)2)+7.8831*omronVolt-2.4317;
            //UPAS0006=-0.0507*L14^4+1.2322*L14^3-2.9659*L14^2+3.9118*L14-1.0765
            //massflow = -0.0507*pow(omronVolt,(float)4)+1.2322*pow(omronVolt,(float)3)-2.9659*pow(omronVolt,(float)2)+3.9118*omronVolt-1.0765;
            //UPAS0007=-1.5303*L14^4+10.411*L14^3-24.002*L14^2+24.321*L14-8.4713
            //massflow = -1.5303*pow(omronVolt,(float)4)+10.411*pow(omronVolt,(float)3)-24.002*pow(omronVolt,(float)2)+24.321*omronVolt-8.4713;
            //UPAS0008=-1.1291*L14^4+7.701*L14^3-17.501*L14^2+17.754*L14-6.073
            //massflow = -1.1291*pow(omronVolt,(float)4)+7.701*pow(omronVolt,(float)3)-17.501*pow(omronVolt,(float)2)+17.754*omronVolt-6.073;
            //UPAS0009=-2.662*L14^4+16.421*L14^3-35.797*L14^2+34.579*L14-11.77
            //massflow = -2.662*pow(omronVolt,(float)4)+16.421*pow(omronVolt,(float)3)-35.797*pow(omronVolt,(float)2)+34.579*omronVolt-11.77;
            //UPAS0010=-3.6933*L14^4+21.633*L14^3-44.694*L14^2+40.387*L14-12.82
            massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0;

            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));
            volflow = massflow/atmoRho;
            deltaVflow = volflow-volflowSet;

            /*if(abs(deltaflow)>.05*volflowSet){
                digital_pot_setpoint = f(Vsetpoint);
                DigPot.writeRegister(digital_pot_setpoint);
                }
            */
            movementsensor.getACCEL();
            movementsensor.getCOMPASS();
            accel_x = movementsensor.AccelData.x;
            accel_y = movementsensor.AccelData.y;
            accel_z = movementsensor.AccelData.z;
            mag_x = movementsensor.MagData.x;
            mag_y = movementsensor.MagData.y;
            mag_z = movementsensor.MagData.z;
            
            vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
            vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
            omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
            press = bmesensor.getPressure();
            temp = bmesensor.getTemperature()-5;
            rh = bmesensor.getHumidity();
            uv =  lightsensor.getUV();
            vis = lightsensor.getVIS();
            ir = lightsensor.getIR();


            //Mount the filesystem
            sd.mount();
            FILE *fp = fopen(filename, "a");
            fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,%f,%f,%f,%f,%2.2f,%04.2f,%2.2f,%1.4f,%1.4f,%1.4f,%.0f,%.0f,%.0f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%d\r\n", Year,Month,Date,Hour,Minutes,Seconds,omronVolt,atmoRho,volflow,massflow,temp,press,rh,accel_x, accel_y, accel_z, mag_x, mag_y, mag_z, uv, vis, ir, omronReading,vInReading, vBlowerReading, omronDiff, gasG.getAmps(), gasG.getVolts(), gasG.getCharge(), digital_pot_setpoint,deltaMflow,deltaVflow,digital_pot_delta);
            fclose(fp);
            //Unmount the filesystem
            sd.unmount();
            
            omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
            omronVolt = (omronReading*4.096)/(32768*2);
            //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
            massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0;
            digital_pot_setpointCK = (int)floor(DP4*pow(massflow,4)+DP3*pow(massflow,3)+DP2*pow(massflow,2)+DP1*massflow+DP0); //min = 0x7F, max = 0x00
            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));
            massflowSet = volflowSet*atmoRho; 
            deltaMflow = massflow-massflowSet;
                    
                if(abs(deltaMflow)>.05){
                    digital_pot_delta = (int)floor(gainFlow*(digital_pot_setpointCK-digital_pot_setpoint));
                    digital_pot_setpoint = digital_pot_setpoint+digital_pot_delta;
                    DigPot.writeRegister(digital_pot_setpoint);
                    
                
                    }

            wait(1);

        }
    }
}