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:
joshuasmth04
Date:
2015-06-02
Revision:
35:bd978e6ccd82
Parent:
34:481403146690
Child:
37:838824df3f5f

File content as of revision 35:bd978e6ccd82:

#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

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 RGBLED(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 massflow; //g/min
float volflow; //L/min
float volflowSet = 1.0; //L/min
float massflowSet;
float deltaflow;
float gainFlow = 1.0;

int digital_pot_setpoint; //min = 0x7F, max = 0x00
int digital_pot_setpointCK;
char filename[] = "/sd/UPAS0005LOG000000000000.txt";

//TwoWire Wire = TwoWire(NRF_TWI0);
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 * RTCtime;

double secondsD = 0;


int main()
{

    RGBLED.set_led(31, 0, 0); // (R, G, B) max of 31 (five bits)
    
    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(9.2456*pow(massflowSet,4)-89.538*pow(massflowSet,3)+329.03*pow(massflowSet,2)-566.12*massflowSet+412.72); //min = 0x7F, max = 0x00

    DigPot.writeRegister(digital_pot_setpoint);
    //Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);//
    wait(1);
    blower = 1;

    RTCtime = RTC.get_time();
    sprintf(filename, "/sd/UPAS0005LOG_%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);


    while(1) {
                
        RTCtime = RTC.get_time();
        secondsD = RTCtime[0];

        if(fmod(secondsD,2)==0) {
            omronVolt = (omronReading*4.096)/(32768*2);
            //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
            massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794;
            digital_pot_setpointCK = (int)floor(9.2456*pow(massflow,4)-89.538*pow(massflow,3)+329.03*pow(massflow,2)-566.12*massflow+412.72); //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;
            digital_pot_setpoint = (int)floor(9.2456*pow(massflowSet,4)-89.538*pow(massflowSet,3)+329.03*pow(massflowSet,2)-566.12*massflowSet+412.72); //min = 0x7F, max = 0x00
            deltaflow = massflow-massflowSet;

            if(abs(deltaflow)>.025*massflowSet) {
                digital_pot_setpoint = digital_pot_setpoint+gainFlow*(digital_pot_setpointCK-digital_pot_setpoint);
                DigPot.writeRegister(digital_pot_setpoint);
            }


        }

        if(fmod(secondsD,10)==0) {

            omronVolt = (omronReading*4.096)/(32768*2);
            //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
            massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794;
            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;
            deltaflow = volflow-volflowSet;


            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;
            omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
            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()-3;
            rh = bmesensor.getHumidity();
            uv =  lightsensor.getUV();
            vis = lightsensor.getVIS();
            ir = lightsensor.getIR();


            //Mount the filesystem
            sd.mount();
            FILE *fp = fopen(filename, "a");
            fprintf(fp, "%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%2.2f,%04.2f,%2.2f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", RTCtime[5],RTCtime[4],RTCtime[3],RTCtime[2],RTCtime[1],RTCtime[0],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);
            fclose(fp);
            //Unmount the filesystem
            sd.unmount();

            wait(1);

        }

    }
}