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:
lionberg
Date:
2015-06-18
Revision:
52:80480b2fafba
Parent:
51:91cfb90e901c
Child:
54:8ee11c9ecd8a

File content as of revision 52:80480b2fafba:

#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);

//UPAS0012 CALIBRATION TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx'
//mass flow sensor output signal (x) vs. mass flow (y)
//y = -0.9198x4 + 4.995x3 - 9.0171x2 + 8.1039x - 2.1758
float MF4 = -0.9198;
float MF3 = 4.995;
float MF2 = -9.0171;
float MF1 = 8.1039;
float MF0 = -2.1758;
//Mass flow sensor polynomial deviation limits
float omronVMin = 0.500; //V
float omronVMax = 2.437; //V
float omronMFMin = 0.189; //g/L
float omronMFMax = 3.873; //g/L
//DIGITAL POTENTIOSTAT dig-pot vs m_dot POLYNOMIAL TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx'
//y = 6.2912x4 - 56.643x3 + 195.7x2 - 329.36x + 245.2
float DP4 = 6.2912;
float DP3 = -56.643;
float DP2 = 195.7;
float DP1 = -329.36;
float DP0 = 245.2;

float press;
float temp;
float rh;

int uv;
int vis;
int ir;

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

int vInReading;
int vBlowerReading;
int omronDiff;
float omronVolt; //V
int omronReading;
float atmoRho; //g/L

float massflow; //g/min
float volflow; //L/min
float volflowSet = 1
; //L/min
int   logInerval = 10;
float massflowSet;
float deltaVflow = 0.0;
float deltaMflow = 0.0;
float gainFlow;
float sampledVol; //L, total sampled volume

uint8_t digital_pot_setpoint; //min = 0x7F, max = 0x00
uint8_t digital_pot_set;

char filename[] = "/sd/UPAS0012LOG000000000000.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()
{

// Setup and Initialization
//---------------------------------------------------------------------------------------------//
   
    if(volflowSet==1.0){
        gainFlow = 100;}
    else if(volflowSet==2.0){
        gainFlow = 25;}
    else{
        gainFlow = 25;}
        
    RGB_LED.set_led(1,0,0);
    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
    
    if(digital_pot_setpoint>=127){
        digital_pot_setpoint = 127;
        }
    else if(digital_pot_setpoint<=1){
        digital_pot_setpoint = 1;
        }
        
    DigPot.writeRegister(digital_pot_setpoint);
    wait(1);
    blower = 1;

    RTCtime = RTC.get_time();
    sprintf(filename, "/sd/UPAS0012LOG_%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);
    //pc.printf("%d\r\n",digital_pot_setpoint); 
   
   //---------------------------------------------------------------------------------------------//             
   //Following lines are needed to enter into the initiallization flow control loop
               
    wait(10);
 
    omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
    omronVolt = (omronReading*4.096)/(32768*2);
    if(omronVolt<=omronVMin){
        massflow = omronMFMin;
    }else if(omronVolt>=omronVMax){
        massflow = omronMFMax;
    }else{
        massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0;}
        deltaMflow = massflow-massflowSet;    
        digital_pot_set = digital_pot_setpoint;             
        wait(5);
    
     //---------------------------------------------------------------------------------------------//    
     //Sets the flow withen +-1.5% of the desired flow rate based on mass flow
    
    while(abs(deltaMflow)>.015){
      
            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
            if(omronVolt<=omronVMin){
                massflow = omronMFMin;
            }else if(omronVolt>=omronVMax){
                massflow = omronMFMax;
            }else{
            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;
            massflowSet = volflowSet*atmoRho; 
            deltaMflow = massflow-massflowSet;
            //pc.printf("%f,%f,%f,%f,%d,%u,%x\r\n",omronVolt,massflow,massflowSet,deltaMflow,digital_pot_set,digital_pot_set,digital_pot_set);
            digital_pot_set = (uint8_t)(digital_pot_set+(int8_t)((gainFlow*deltaMflow)));
            if(digital_pot_set>=127){
                digital_pot_set = 127;
            }else if(digital_pot_set<=1){
                digital_pot_set = 1;
            }
                
            wait(2); 
            DigPot.writeRegister(digital_pot_set);
            wait(1);
            
            
            }
    
    sampledVol = 0.0;
    RGB_LED.set_led(0,1,0);
     
      //---------------------------------------------------------------------------------------------// 
      //---------------------------------------------------------------------------------------------//    
      //---------------------------------------------------------------------------------------------//       
      // Main Control Loop
    
    while(1) {

       
        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,logInerval)==0) {
            
            omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
            omronVolt = (omronReading*4.096)/(32768*2);

            if(omronVolt<=omronVMin){
                massflow = omronMFMin;
            }else if(omronVolt>=omronVMax){
                massflow = omronMFMax;
            }else{
            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;
            sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow);
            deltaVflow = volflow-volflowSet;
            massflowSet = volflowSet*atmoRho; 
            deltaMflow = massflow-massflowSet;
            
            if(abs(deltaMflow)>.025){
                digital_pot_set = (uint8_t)(digital_pot_set+(int8_t)(gainFlow*deltaMflow));
                        
                if(digital_pot_set>=127){
                    digital_pot_set = 127;
                    RGB_LED.set_led(1,0,0);
                }else if(digital_pot_set<=1){
                    digital_pot_set = 1;
                    RGB_LED.set_led(1,0,0);
                }else{
                    RGB_LED.set_led(1,1,0);}
                    
                DigPot.writeRegister(digital_pot_set);
                
             }else{
                 RGB_LED.set_led(0,1,0);}
    
            movementsensor.getACCEL();
            movementsensor.getCOMPASS();
            accel_x = movementsensor.AccelData.x;
            accel_y = movementsensor.AccelData.y;
            accel_z = movementsensor.AccelData.z;
            accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0;
            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.0;
            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,%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",Year,Month,Date,Hour,Minutes,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);
            fclose(fp);
            //Unmount the filesystem
            //sd.unmount();
            
            wait(1);

        }
    }
}