Volckens Group Sensors / Mbed 2 deprecated CEAMS_Road_Emissions

Dependencies:   ADS1115 BME280 BMP280 CAM_M8 NCP5623BMUTBG SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "NCP5623BMUTBG.h"
00003 #include "CAM_M8.h"
00004 #include "SDFileSystem.h"
00005 #include "Adafruit_ADS1015.h"
00006 #include "BME280.h"
00007 #include "BMP280.h"
00008 
00009 /////////////////////////////////////////////
00010 //Define core buses and pin states.
00011 /////////////////////////////////////////////
00012 I2C                 i2c(PB_9, PB_8);//(D14, D15); SDA,SCL
00013 Serial              pc(USBTX, USBRX);
00014 DigitalOut          pumps(PA_9, 0);//(D8, 0);
00015 DigitalOut          pbKill(PC_12, 1); // Digital input pin that conncect to the LTC2950 battery charger used to shutdown the UPAS 
00016 DigitalIn           nINT(PA_15); //Connected but currently unused is a digital ouput pin from LTC2950 battery charger. http://cds.linear.com/docs/en/datasheet/295012fd.pdf
00017 NCP5623BMUTBG       RGB_LED(PB_9, PB_8);    //(D14, D15);
00018 
00019 /////////////////////////////////////////////
00020 //RN4677 BT/BLE Module
00021 /////////////////////////////////////////////
00022 Serial              ble(PB_10, PB_11);
00023 DigitalOut          bleRTS(PB_14, 0);
00024 DigitalOut          bleCTS(PB_13, 0);
00025 DigitalOut          BT_IRST(PC_8, 0);
00026 DigitalOut          BT_SW(PA_12, 0);
00027 
00028 /////////////////////////////////////////////
00029 //Analog to Digital Converter
00030 /////////////////////////////////////////////
00031 DigitalIn           ADS_ALRT(PA_10); //Connected but currently unused. (ADS1115) http://www.ti.com/lit/ds/symlink/ads1115.pdf
00032 Adafruit_ADS1115    ads_sun(&i2c, ADS1015_ADDRESS_VDD); //Adress pin connected to 3.3V
00033 
00034 /////////////////////////////////////////////
00035 //Battery, Charger, & Supply Monitoring
00036 /////////////////////////////////////////////
00037 DigitalIn           LTCALT(PB_2); //High for normal operation. Low when a threshold is exceeded for Voltage, gas gauge, or temp
00038 DigitalIn           bcs1(PC_9); //Batt charging if High. (BQ24100)[U23]
00039 DigitalIn           bcs2(PA_8); //Charge complete if High. (BQ24100)[U23]  
00040 DigitalIn           bc_npg(PB_1); //Power to the charge controller. (BQ24100)[U23] 
00041 DigitalIn           SW3flt(PC_4); //When EN = 0 pin is HIGH, When EN = 1, LOW can be current limit, thermal limit, or UVLO.
00042 
00043 /////////////////////////////////////////////
00044 //Sensirion SDP3X(s)
00045 /////////////////////////////////////////////
00046 DigitalIn           SDP3Xflt(PC_0);
00047 DigitalIn           SDP3XAltflt(PB_7);
00048 
00049 /////////////////////////////////////////////
00050 //Accelerometer and Magnometer
00051 /////////////////////////////////////////////
00052 DigitalOut          iNemoEnable(PA_1, 0);
00053 DigitalIn           iNemoReady(PB_0);
00054 DigitalIn           iNemoInt1(PC_5);
00055 DigitalIn           iNemoInt2(PC_6);
00056 DigitalIn           iNemoInt3(PC_7);
00057 
00058 /////////////////////////////////////////////
00059 //Temperature and Pressure
00060 /////////////////////////////////////////////
00061 BMP280              bmp(PB_9, PB_8, 0xEE);
00062 BME280              bme(PB_9, PB_8, 0xEC);  //(D14, D15);
00063 
00064 /////////////////////////////////////////////
00065 //GPS
00066 /////////////////////////////////////////////
00067 DigitalOut          gpsEN(PB_15, 0);
00068 CAM_M8              gps(PB_9, PB_8,(0x84));   
00069 uint8_t gpsBTState = 1;
00070 uint8_t meState = 0;
00071 DigitalIn           gpsENFault(PB_12); //When EN = 0 pin is HIGH, When EN = 1, LOW can be current limit, thermal limit, or UVLO.
00072 
00073 /////////////////////////////////////////////
00074 //SD Card
00075 /////////////////////////////////////////////
00076 DigitalIn           sdCD(PA_11);
00077 DigitalOut          sdClk(PB_3,0 );
00078 DigitalIn           sdMISO(PB_4);
00079 DigitalOut          sdMOSI(PB_5, 0);
00080 DigitalOut          sdCS(PB_6, 1);
00081 
00082 DigitalIn           pbIso(PA_0);
00083 DigitalOut          hFault1(PA_7, 0);
00084 DigitalOut          hFault2(PA_6, 0);
00085 DigitalOut          hFault3(PA_5, 0);
00086 DigitalOut          hFault4(PA_4, 0);
00087 
00088 SDFileSystem        sd(PB_5, PB_4, PB_3, PB_6, "sd");//(D4, D5, D3, D10, "sd"); // (MOSI, MISO, SCK, SEL)
00089 char VoltFile[] = "/sd/ROAD_00.txt";
00090 
00091 /////////////////////////////////////////////
00092 //Callbacks
00093 /////////////////////////////////////////////
00094 Ticker          loggVolts;  //This is the AOD logging callback object
00095 
00096 volatile bool g_loggVolts_flag = false;
00097 
00098 static void loggVolts_ISR( void ) {
00099     g_loggVolts_flag = true;
00100 }
00101 
00102 void logVolts(void);
00103 
00104 /////////////////////////////////////////////
00105 //Globals
00106 /////////////////////////////////////////////
00107 double  gpslatitude = 0.0;
00108 double  gpslongitude = 0.0;
00109 float   gpsaltitude = 0.0;
00110 uint8_t gpsquality = 0;
00111 uint8_t gpssatellites = 0;
00112 long    gpsTime;
00113 long    gpsDate;
00114 int     gps_year;
00115 int     gps_month;
00116 int     gps_day;
00117 int     gps_hour;
00118 int     gps_minute;
00119 double  gps_second;
00120 double  v870;     //Voltage read from 870nm photodiode
00121 double  v_raw870; //Raw analog output from 870nm photodiode
00122 double  v680;     //Voltage read from 680nm photodiode
00123 double  v_raw680; //Raw analog output from 680nm photodiode
00124 double  v520;     //Voltage read from 520nm photodiode
00125 double  v_raw520; //Raw analog output from 520nm photodiode
00126 double  v440;     //Voltage read from 440nm photodiode
00127 double  v_raw440; //Raw analog output from 440nm photodiode
00128 float   bme_press;
00129 float   bme_temp;
00130 float   bmp_press;
00131 //////////////////////////////////////////////////////////////
00132 //Main Function
00133 //////////////////////////////////////////////////////////////
00134 int main()
00135 {
00136     pc.baud(115200);
00137     loggVolts.attach(&loggVolts_ISR, 1.0);
00138     RGB_LED.set_led(1, 1, 1);
00139     gpsEN = 1;
00140     
00141     FILE *fp = fopen(VoltFile, "r");//try reading the log file
00142     
00143     for(int i = 0; i < 100; i++)
00144     {
00145         VoltFile[9] = i/10 + '0';
00146         VoltFile[10] = i%10 + '0';
00147         fp = fopen(VoltFile, "r");//try reading the log file
00148         if(fp == NULL)
00149         {
00150             fp = fopen(VoltFile, "w"); // fp is local variable
00151             fclose(fp);
00152             fp = fopen(VoltFile, "a");
00153             fprintf(fp, "GPS Date,GPS Time,Latitude,Longitude,Altitude,Temperature (C),Pressure (mbar),v440,v520,v680,v870\r\n");
00154             fclose(fp);
00155             free(fp);
00156             break;
00157         }
00158     }
00159     
00160     while(1){
00161         
00162         if(g_loggVolts_flag){
00163             g_loggVolts_flag = false;
00164             logVolts();
00165         }
00166         
00167     } //end while
00168 } //end main
00169 
00170 void logVolts(void)
00171 {
00172     RGB_LED.set_led(0, 1, 1);
00173     //Get needed location data
00174     gps.read_gps();
00175     gpsquality =  gps.quality;
00176     if(gpsquality!=0)
00177     {
00178         gpslatitude = gps.lat;
00179         gpslongitude = gps.lon;
00180         gpsaltitude = gps.altitude;
00181     }
00182     gpsTime = (long)gps.utc;
00183     gpsDate = (long)gps.date;
00184                         
00185     gps_minute = (int)(floor((float)(gpsTime - (uint8_t)(floor((float)gpsTime/10000))*10000)/100));    // 0-59
00186     gps_second = (double)(floor(((float)gpsTime - 100*(floor((float)gpsTime/100)))));//0; //(uint8_t)(floor(((float)gpsTime - 100*(floor((float)gpsTime/100)))));    // 0-59
00187     gps_hour =  (int)(floor((float)gpsTime/10000));  // 0-23
00188     gps_day = (int)(floor((float)gpsDate/10000));   // 1-31
00189     gps_month = (uint8_t)(floor((float)(gpsDate - gps_day*10000)/100));     // 0-11
00190     gps_year = 1900 + (uint8_t)(100+floor(((float)gpsDate - 100*(floor((float)gpsDate/100))))); // year since 1900 (116 = 2016)//100+16
00191     
00192     RGB_LED.set_led(0, 1, 0);
00193     
00194     //Get temperature and pressure from the BME
00195     bme_temp = bme.getTemperature();
00196     bme_press = bme.getPressure(bme_temp);
00197     bmp_press = bmp.getPressure(bme_temp);
00198     
00199     // Take the average of 10 440 voltage measurements
00200     double v_440_temp;
00201     double v_440_sum = 0;
00202     for(int i = 0; i < 10; i++)
00203     {
00204         v_440_temp = (double)ads_sun.readADC_SingleEnded(A0_GAIN_TWO); //Channel A0 | 2x gain | +/-2.048V | 1 bit = 1mV | 0.0625mV
00205         v_440_sum += v_440_temp;
00206     }    
00207     v_raw440 = v_440_sum/10.0; 
00208     v440 = (v_raw440*0.0625)/(1000); //Converts to a voltage
00209     
00210     // Take the average of 10 520 voltage measurements                    
00211     double v_520_temp;
00212     double v_520_sum = 0;
00213     for(int i = 0; i < 10; i++)
00214     {
00215         v_520_temp = (double)ads_sun.readADC_SingleEnded(A1_GAIN_TWO); //Channel A0 | 2x gain | +/-2.048V | 1 bit = 1mV | 0.0625mV
00216         v_520_sum += v_520_temp;
00217     }    
00218     v_raw520 = v_520_sum/10.0; 
00219     v520 = (v_raw520*0.0625)/(1000); //Converts to a voltage
00220     
00221     // Take the average of 10 680 voltage measurements                    
00222     double v_680_temp;
00223     double v_680_sum = 0;
00224     for(int i = 0; i < 10; i++)
00225     {
00226         v_680_temp = (double)ads_sun.readADC_SingleEnded(A2_GAIN_TWO); //Channel A0 | 2x gain | +/-2.048V | 1 bit = 1mV | 0.0625mV
00227         v_680_sum += v_680_temp;
00228     }    
00229     v_raw680 = v_680_sum/10.0; 
00230     v680 = (v_raw680*0.0625)/(1000); //Converts to a voltage
00231     
00232     // Take the average of 10 870 voltage measurements                      
00233     double v_870_temp;
00234     double v_870_sum = 0;
00235     for(int i = 0; i < 10; i++)
00236     {
00237         v_870_temp = (double)ads_sun.readADC_SingleEnded(A3_GAIN_TWO); //Channel A0 | 2x gain | +/-2.048V | 1 bit = 1mV | 0.0625mV
00238         v_870_sum += v_870_temp;
00239     }    
00240     v_raw870 = v_870_sum/10.0; 
00241     v870 = (v_raw870*0.0625)/(1000); //Converts to a voltage
00242        
00243     // Write data to SD card
00244     FILE *fpAOD = fopen(VoltFile, "a");
00245     fprintf(fpAOD,"%06d,%06d,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",gpsDate,gpsTime,gpslatitude,gpslongitude,gpsaltitude,bme_temp,bme_press,v440,v520,v680,v870);
00246     fclose(fpAOD);
00247     free(fpAOD);
00248     pc.printf("%06d,%06d,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",gpsDate,gpsTime,gpslatitude,gpslongitude,gpsaltitude,bme_temp,bme_press,v440,v520,v680,v870);   
00249 }
00250 
00251