attempt to fix posible power issues with the sharp

Dependencies:   ADS1115 BME280 CronoDot SDFileSystem mbed

Fork of Outdoor_UPAS_v1_2_Tboard by scott kelleher

Revision:
28:42932d3b105d
Parent:
27:922f53fa649c
Child:
29:fd74725294d5
--- a/main.cpp	Thu Apr 21 18:22:47 2016 +0000
+++ b/main.cpp	Sat Apr 23 00:10:57 2016 +0000
@@ -50,8 +50,8 @@
 //Battery Monitoring
 /////////////////////////////////////////////
 STC3100             gasG(PB_9, PB_8);//(D14, D15);  // http://www.st.com/web/en/resource/technical/document/datasheet/CD00219947.pdf
-DigitalIn           bcs1(PC_9); //Charge complete if High. Connected but currently unused. (MCP73871) http://www.mouser.com/ds/2/268/22090a-52174.pdf
-DigitalIn           bcs2(PA_8); //Batt charging if High. Connected but currently unused. (MCP73871) http://www.mouser.com/ds/2/268/22090a-52174.pdf
+InterruptIn           bcs1(PC_9); //Charge complete if High. Connected but currently unused. (MCP73871) http://www.mouser.com/ds/2/268/22090a-52174.pdf
+InterruptIn           bcs2(PA_8); //Batt charging if High. Connected but currently unused. (MCP73871) http://www.mouser.com/ds/2/268/22090a-52174.pdf
 
 /////////////////////////////////////////////
 //Accelerometer and Magnometer
@@ -77,23 +77,23 @@
 //Hbridge Valve Control
 /////////////////////////////////////////////
 
-/*
-DRV8830    motor1(PB_9, PB_8, 0xC4); //Not working due to trace issue on tboard
-DRV8830    motor2(PB_9, PB_8, 0xC8); //Works with 0xC8, not 0xCA since R8 isn't on the Tboards.
-DRV8830    motor3(PB_9, PB_8, 0xCC); //Not working due to trace issue on tboard
-DRV8830    motor4(PB_9, PB_8, 0xCE); //Works!
-DigitalIn  hb_fault1(PA_6);
-DigitalIn  hb_fault2(PA_7);
-DigitalIn  hb_fault3(PA_5);
-DigitalIn  hb_fault4(PA_4);
-*/
+
+DRV8830    schoolF1(PB_9, PB_8, 0xC4); //Work/School Filter
+DRV8830    homeF2(PB_9, PB_8, 0xCA); //Home Filter
+DRV8830    transitF3(PB_9, PB_8, 0xCC); //Transit Filter
+DRV8830    blankF4(PB_9, PB_8, 0xCE); //Blank Filter
+DigitalIn  hb1_school(PA_6);
+DigitalIn  hb2_home(PA_7);
+DigitalIn  hb3_transit(PA_5);
+DigitalIn  hb4_blank(PA_4);
+
 
 /////////////////////////////////////////////
 //SD Card
 /////////////////////////////////////////////
 char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
 SDFileSystem sd(PB_5, PB_4, PB_3, PB_6, "sd");//(D4, D5, D3, D10, "sd"); // (MOSI, MISO, SCK, SEL)
-DigitalIn    sdCD(PA_11, PullUp);
+DigitalIn     sdCD(PA_11, PullUp);
 
 /////////////////////////////////////////////
 //Callbacks
@@ -157,7 +157,7 @@
 float gainFlow;
 float sampledVol; //L, total sampled volume
 
-int digital_pot_setpoint = 30; //min = 0x7F, max = 0x00
+int digital_pot_setpoint = 100; //min = 0x7F, max = 0x00
 int digital_pot_set;
 int digital_pot_change;
 int digitalpotMax = 127;
@@ -169,6 +169,7 @@
 bool    gpsFix;
 uint8_t gpssatellites = 0;
 double  gpsspeed = 0.0;
+double  gpscourse = 0.0;
 double  gpslatitude = 0.0;
 double  gpslongitude = 0.0;
 float   gpsaltitude = 0.0;
@@ -424,14 +425,14 @@
 //////////////////////////////////////////////////////////////
 void log_data()
 {
-
-
-           
+    //Get time and set RTC 
+    ///////////////////////////
     time_t seconds = time(NULL);
     strftime(timestr, 32, "%y%m%d%H%M%S", localtime(&seconds));
-
     RTC_UPAS.get_time(); 
     
+    //Get Sensor Data except GPS
+    ////////////////////////////
     press = bmesensor.getPressure();
     temp = bmesensor.getTemperature()-5.0;
     rh = bmesensor.getHumidity();
@@ -448,18 +449,96 @@
     mag_z = movementsensor.MagData.z;
     vInReadingLast = vInReading;
     vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
-    
-    if(vInReading > 5950 && amps > 8191) {
-        //pumps = 0;
-        wait(1);
-    } else if(pumps == 0 && amps < 8191) {
-        //pumps = 1;
-    }
-          
     amps = gasG.getAmps();
     bVolt = gasG.getVolts(); 
     bFuel = gasG.getCharge();
     
+    //Check for fully charged battery
+    if(bVolt > 1750 && amps > 8191) {
+               RGB_LED.set_led(0,1,0);
+    //Check for battery with ~2 hours left of runtime at 2lpm or if after 8pm to remind user to plug in sampler            
+    }else if(amps > 8191 && (RTC_UPAS.hour >=20 || bVolt < 1500)) {
+        if(ledOn) {
+            RGB_LED.set_led(0,0,0);
+            ledOn = 0;
+        }else {
+            RGB_LED.set_led(1,0,0);
+            ledOn = 1;
+        }
+    //No LED on when not low battery and not fully charged    
+    }else{
+        RGB_LED.set_led(0,0,0);
+    }
+    
+    
+    // Get GPS Data
+    //////////////////////////////
+    //if(gpsEN ==1){  
+    
+        gpsFix = gps.read(1);
+        gpsspeed = gps.speed;
+        //gpscourse = gps.course;
+        gpssatellites =  gps.satellites;
+        gpslatitude = gps.lat;
+        gpslongitude = gps.lon;
+        gpsaltitude = gps.altitude;
+      
+        if(gpsFix){
+            workDistance = GPSdistanceCalc (work_lat, work_lon);
+            homeDistance = GPSdistanceCalc (home_lat, home_lon);
+            
+            if(workDistance < 50) {
+                    location = 1;
+                    //RGB_LED.set_led(0,1,1);
+            }else if(homeDistance < 50) { // 25 or 30 m instead?
+                    location = 2;
+                    //RGB_LED.set_led(1,0,1);
+            }else{
+                    location = 3;
+                    //RGB_LED.set_led(1,1,1);
+                  }
+        
+        }else if(homeDistance == 99999 && workDistance == 99999){
+                location = 0;
+                RGB_LED.set_led(1,1,0);
+        }
+   
+//}
+    
+    //Check for 3.3V rail cut out and turn off pumps in this event
+    if(vInReading > 5950 && amps > 8191) {
+        //pumps = 0;
+        wait(1);
+    //Turn pumps back on once the sampler is plugged in and charging after pumps shutoff and 3.3V rail drops out
+    } else if(pumps == 0 && amps < 8191) {
+        
+        // If pumps are plugged in the next morning assume the sampler is at school if after 8 if no GPSfix is available
+        if(gpsFix == 0 && RTC_UPAS.hour>8){
+            gpslatitude =   work_lat; // specific to Rivendell Study. In case student forgets to plug in and teachers catch this. Assume if plugged in after 8 and no GPS signal will change to proper valve. Hopefully...
+            gpslongitude =  work_lon; // specific to Rivendell Study. In case student forgets to plug in and teachers catch this. Assume if plugged in after 8 and no GPS signal will change to proper valve. Hopefully...
+        }
+            //Adjust valves to ensure everything is in proper place before pumps restart
+            /*
+            schoolF1.getFault();
+            homeF2.getFault();
+            transitF3.getFault();
+            blankF4.getFault();
+
+            schoolF1.drive(254); //closed = 253, open = 254
+            homeF2.drive(253); //closed = 253, open = 254
+            transitF3.drive(253); //closed = 253, open = 254
+            blankF4.drive(253); //closed = 253, open = 254
+            wait(1);
+            schoolF1.stop();
+            homeF2.stop();
+            transitF3.stop();
+            blankF4.stop();
+            */
+            //pumps = 1;
+            
+    }
+          
+   
     
     if(pumps == 1){     
         omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
@@ -484,36 +563,6 @@
         omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
     }
     
-    //if(gpsEN ==1){  
-    
-        gpsFix = gps.read(1);
-        gpsspeed = gps.speed;
-        gpssatellites =  gps.satellites;
-        gpslatitude = gps.lat;
-        gpslongitude = gps.lon;
-        gpsaltitude = gps.altitude;
-      
-        if(gpsFix){
-            workDistance = GPSdistanceCalc (work_lat, work_lon);
-            homeDistance = GPSdistanceCalc (home_lat, home_lon);
-            
-            if(workDistance < 50) {
-                    location = 1;
-                    RGB_LED.set_led(0,1,1);
-            }else if(homeDistance < 20) { // 25 or 30 m instead?
-                    location = 2;
-                    RGB_LED.set_led(1,0,1);
-            }else{
-                    location = 3;
-                    RGB_LED.set_led(1,1,1);
-                  }
-        
-        }else if(homeDistance == 99999 && workDistance == 99999){
-                location = 0;
-                RGB_LED.set_led(1,1,0);
-        }
-   
-//}
 
    
     FILE *fp = fopen(filename, "a");
@@ -525,29 +574,16 @@
     fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,amps);
     fprintf(fp, "%d,%d,%d,%1.3f,%1.3f,", bVolt, bFuel,digital_pot_set, deltaMflow, deltaVflow);
     fprintf(fp, "%f,%f,%06d,%06d,", gpslatitude, gpslongitude, (long)gps.date, (long)gps.utc); 
-    fprintf(fp, "%f,%d,%f,%d,", gpsspeed, gpssatellites,  gpsaltitude, gpsFix);
+    //fprintf(fp, "%f,%d,%f,%f,%d,", gpsspeed, gpssatellites, gpscourse, gpsaltitude, gpsFix);
+    fprintf(fp, "%f,%d,%f,%d,", gpsspeed, gpssatellites, gpsaltitude, gpsFix);
     fprintf(fp, "%f,%f,%d,%d\r\n", homeDistance, workDistance, location, pumps == 1); // test and add in speed, etc that Josh added in to match the adafruit GPS
     fclose(fp);
     free(fp);
     
 
-    if(bVolt > 1750 && amps > 8191) {
-               RGB_LED.set_led(0,1,0); 
-    } else if(amps > 8191 && (RTC_UPAS.hour >=20 || bVolt < 1500)) {
-        if(ledOn) {
-            RGB_LED.set_led(0,0,0);
-            ledOn = 0;
-        } else {
-            RGB_LED.set_led(1,0,0);
-            ledOn = 1;
-        }
-    } else {
-        RGB_LED.set_led(0,0,0);
-    }
 
 
 
-//pc.printf("%s,", timestr);
 
 
 }
@@ -611,8 +647,8 @@
 int main(){
     
     RGB_LED.set_led(0,0,1);
-    
-    /*
+  
+ /*   
     while(1){
     if(sdCD == 0){
        RGB_LED.set_led(0,1,0);
@@ -623,28 +659,23 @@
     wait(10); 
     }
 */
-/*
-motor1.getFault();
-    wait(1);
-motor2.getFault();
-    wait(1);
-motor3.getFault();
-    wait(1);
-motor4.getFault();
-    wait(1);
 
-    motor1.drive(254); //closed = 253, open = 254
-    motor2.drive(253); //closed = 253, open = 254
-    motor3.drive(253); //closed = 253, open = 254
-    motor4.drive(253); //closed = 253, open = 254
+    schoolF1.getFault();
+    homeF2.getFault();
+    transitF3.getFault();
+    blankF4.getFault();
     
+    schoolF1.drive(253); //closed = 253, open = 254
+    homeF2.drive(253); //closed = 253, open = 254
+    transitF3.drive(254); //closed = 253, open = 254
+    blankF4.drive(253); //closed = 253, open = 254
+    
+    wait(1);
+    schoolF1.stop();
+    homeF2.stop();
+    transitF3.stop();
+    blankF4.stop();
 
-    wait(1);
-    motor1.stop();
-    motor2.stop();
-    motor3.stop();
-    motor4.stop();
-*/
 
 
     gpsEN = 1;
@@ -779,7 +810,7 @@
 
     DigPot.writeRegister(digital_pot_setpoint);
     wait(1);
-    //pumps = 1;
+    pumps = 1;
     //pumpOn = 1;
 
     uint8_t subjectLabelOriginal[8] = {0,};