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

Revision:
57:0b554f7aa9a3
Parent:
56:c4d6bdd7c3fb
Child:
58:7239c2ab2b65
--- a/main.cpp	Tue Jun 30 03:33:36 2015 +0000
+++ b/main.cpp	Wed Jul 01 22:21:02 2015 +0000
@@ -9,12 +9,12 @@
 #include "NCP5623BMUTBG.h"
 #include "CronoDot.h"
 #include "EEPROM.h"
-#include "AMAS_Serial.h"
+#include "UPAS_Serial.h"
 
 #define SERIAL_BAUD_RATE    9600
 #define SCL         20
 #define SDA         22
-#define Crono       0xD0       //D0 for the chronoDot
+//#define Crono       0xD0       //D0 for the chronoDot
 
 I2C i2c(p22, p20);
 Adafruit_ADS1115 ads(&i2c);
@@ -29,7 +29,9 @@
 NCP5623BMUTBG RGB_LED(p22, p20);
 CronoDot RTC(p22, p20);
 EEPROM          E2PROM(p22, p20);
-AMAS_Serial     Menu;
+UPAS_Serial     Menu;
+
+Timeout         stop;   //This is the stop call back object
 
 //UPAS0012 CALIBRATION TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx'
 //mass flow sensor output signal (x) vs. mass flow (y)
@@ -80,7 +82,8 @@
 float massflow; //g/min
 float volflow; //L/min
 float volflowSet = 1.0; //L/min
-int   logInerval = 5;
+int   logInerval = 10;
+double secondsD = 0;
 float massflowSet;
 float deltaVflow = 0.0;
 float deltaMflow = 0.0;
@@ -93,10 +96,15 @@
 int digitalpotMax = 127;
 int digitalpotMin = 2;
 
+uint32_t start_time = 0; // when should the test start? default is ASAP
+uint32_t stop_time = 0;  // when should the test stop? default is 24hr after start
+int refresh_Time = 10;   // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
 
-char filename[] = "/sd/UPAS0012LOG000000000000.txt";
+char device_name[] = "---------------";
+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
@@ -104,130 +112,24 @@
 char Month   = 0; //Month
 char Year    = 0; //Year
 
-double secondsD = 0;
+
 char * RTCtime;
-
-int main()
-{
-
-// Setup and Initialization
-//---------------------------------------------------------------------------------------------//
-
-/*    RGB_LED.set_led(0,0,1);
-    Timer refresh_Timer; 
-    while(refresh_Timer.read_ms() <= 8000) {    // you have 8 seconds to send any one character over the serial bus to open the menu 
-        Menu.Start(pc, E2PROM, RTC);    // loop this to read the serial buffer and open the menu
-    }
 */
-  
-    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>=digitalpotMax){
-        digital_pot_setpoint = digitalpotMax;
-        }
-    else if(digital_pot_setpoint<=digitalpotMin){
-        digital_pot_setpoint = digitalpotMin;
-        }
-        
-    DigPot.writeRegister(digital_pot_setpoint);
-    wait(1);
-    blower = 1;
-
+/*
+void check_stop()   // this checks if it's time to stop and shutdown
+{
     RTC.get_time();
-    sprintf(filename, "/sd/UPAS0012LOG_%02d-%02d-%02d_%02d-%02d-%02d.txt",RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds);
-    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 = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
-            if(digital_pot_set>=digitalpotMax){
-                digital_pot_set = digitalpotMax;
-            }else if(digital_pot_set<=digitalpotMin){
-                digital_pot_set = digitalpotMin;
-            }
-                
-            wait(2); 
-            DigPot.writeRegister(digital_pot_set);
-            wait(1);
-            
-            
-            }
-    
-    sampledVol = 0.0;
-    RGB_LED.set_led(0,1,0);
-     
-      //---------------------------------------------------------------------------------------------// 
-      //---------------------------------------------------------------------------------------------//    
-      //---------------------------------------------------------------------------------------------//       
-      // Main Control Loop
-    
-    while(1) {
+    if(RTC.our_time >= stop_time) {
+        pbKill = 0; // this is were we shut everything down
+    }
+    stop.detach();
+    stop.attach(&check_stop, 5);    // checks stop time every 5 seconds
+}
+*/
 
-       
-        RTC.get_time();   // the way the variable RTCtime works you must save the variables in normal chars or weird things happen
-        Seconds = RTC.seconds;//Seconds 
-        Minutes = RTC.minutes;//Minutes
-        Hour    = RTC.hour;//Hour
-        Date    = RTC.date;//Date
-        Month   = RTC.month;//Month
-        Year    = RTC.year;//Year
-        
-        secondsD = (double)Seconds;
-        
-        if(fmod(secondsD,logInerval)==0) {
-            
+void log_data()
+{ 
+            RTC.get_time();
             omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
             omronVolt = (omronReading*4.096)/(32768*2);
 
@@ -291,15 +193,169 @@
             //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);
+            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",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);
             fclose(fp);
             //Unmount the filesystem
             //sd.unmount();
             
             wait(1);
+            
+            }
+
+int main()
+{
+
+// Setup and Initialization
+//---------------------------------------------------------------------------------------------//
+
+    Timer refresh_Timer;        //sets up a timer for use in loop; how often do we log data?
+    refresh_Timer.start();      //starts the clock on the timer
+
+    RGB_LED.set_led(0,1,1);
+    refresh_Timer.reset(); // resets the timer to zero
+
+    
+    Menu.read_menu(E2PROM, refresh_Time, volflowSet, start_time, stop_time, device_name);       //Read all data from the EEPROM here
+    //reset defaults if the bits in byte 0 say so
+    //RTC.get_time();
+    //start_time = RTC.our_time;      // default start ASAP
+    //stop_time = start_time + 86400; // default 24hr run time
+fix_error:
+    Menu.Start(pc, E2PROM, RTC, refresh_Time, volflowSet, start_time, stop_time, device_name);  //Forces you to open the menu
+    Menu.save_menu(E2PROM, refresh_Time, volflowSet, start_time, stop_time, device_name);       //Save all data to the EEPROM
+    
+    //Test for errors
+    if(RTC.OSF()){                  //Don't proceed if the RTC needs reset 
+        RGB_LED.set_led(1,0,0); // error code/color
+        pc.printf("!Reset the time!\r\n");
+        goto fix_error;
+    }
+  /*  RTC.get_time();
+    if(RTC.our_time > stop_time){   //Don't proceed if it's already time to stop
+        RGB_LED.set_led(1,0,0); // error code/color
+        pc.printf("!Fix the stop time!\r\n");
+        goto fix_error;
+    }
+    */
+    
+    RGB_LED.set_led(1,1,0);
+    while(RTC.our_time < start_time) {  // this while waits for the start time by looping until the start time
+        RTC.get_time();
+        // serial print a count down??
+    }
+  //  stop.attach(&check_stop, 60);    // check if we should shut down every 5 seconds, starting 60s after the start.
+
+
+    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>=digitalpotMax){
+        digital_pot_setpoint = digitalpotMax;
+        }
+    else if(digital_pot_setpoint<=digitalpotMin){
+        digital_pot_setpoint = digitalpotMin;
+        }
+        
+    DigPot.writeRegister(digital_pot_setpoint);
+    wait(1);
+    blower = 1;
+
+    RTC.get_time();
+    sprintf(filename, "/sd/UPAS0012LOG_%02d-%02d-%02d_%02d-%02d-%02d%s.txt",RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,device_name);
+    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 = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
+            if(digital_pot_set>=digitalpotMax){
+                digital_pot_set = digitalpotMax;
+            }else if(digital_pot_set<=digitalpotMin){
+                digital_pot_set = digitalpotMin;
+            }
+                
+            wait(2); 
+            DigPot.writeRegister(digital_pot_set);
+            wait(1);
+            
+            
+            }
+    
+        sampledVol = 0.0;
+        RGB_LED.set_led(0,1,0);
+        refresh_Timer.reset();  //restarts the clock on the timer
+
+       //** end of initalization **//
+     
+      //---------------------------------------------------------------------------------------------// 
+      //---------------------------------------------------------------------------------------------//    
+      //---------------------------------------------------------------------------------------------//       
+      // Main Control Loop 
+                
+        while(1) {
+        
+        RTC.get_time();
+        secondsD = (double)RTC.seconds;
+              
+        if(fmod(secondsD,logInerval)==0) {
+            
+           log_data();
+           refresh_Timer.reset(); // resets the timer to zero
+          
 
         }
     }
+    
 }