Dual CANbus monitor and instrumentation cluster supporting ILI9341 display controller

Dependencies:   SPI_TFTx2_ILI9341 TOUCH_TFTx2_ILI9341 TFT_fonts mbed

Fork of CANary by Tick Tock

Branch:
Metric
Revision:
121:553faf139a20
Parent:
120:041edeec08f5
Child:
122:138a40892a4c
--- a/main.cpp	Mon Jul 08 02:08:08 2013 +0000
+++ b/main.cpp	Thu Jul 11 05:29:18 2013 +0000
@@ -23,7 +23,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "120"; // gg - revision string, max 6 characters
+char revStr[7] = "122"; // gg - revision string, max 6 characters
 
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -126,13 +126,14 @@
 float kW[39]={0};
 float mpkWh[39]={0};
 float unloadedV_x2,Resr,curRmax,curRmin,redRmax,redRmin,incRmax,incRmin;
+signed short Imax, Imin;
 // Logarithmic division scale (roughly - snapped to common units of time)
 float timeConstant[39] = {1, 1.58, 2.51, 3.98, 6.31, 10, 15.8, 25.1, 39.8, 60, // 1 minute
                      60*1.58, 60*2.51, 60*3.98, 60*6.31, 60*10, 60*15.8, 60*25.1, 60*39.8, 60*60, // 1 hour
                      60*60*1.58, 60*60*2.51, 60*60*3.98, 60*60*6.31, 60*60*10, 60*60*15.8, 60*60*24, // 1 day
                      60*60*24*1.58, 60*60*24*2.51, 60*60*24*3.98, 60*60*24*6.31, 60*60*24*10, 60*60*24*15.8, 60*60*24*30, // 1 month
                      60*60*24*39.8, 60*60*24*63.1, 60*60*24*100, 60*60*24*158, 60*60*24*251, 60*60*24*365}; // 1 year
-bool updateDTE = false;
+bool tock = false;
 unsigned short pointerSep;
 unsigned char reqMsgCnt = 99;
 unsigned long Ah_x10000 = 0;
@@ -140,6 +141,8 @@
 unsigned short SOH_x100 = 0;
 float maxTemp = 0;
 bool metric = false;
+bool shunt[96]={0};
+bool charging=false;
 
 int main() {
     //can1SleepMode.mode(OpenDrain);
@@ -674,6 +677,7 @@
             headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false;  // headlight/turn signal indicator
             accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th
             accOn=(accV>5)?true:false;
+            charging=(mph[0]<0.1)&&(kW[0]<-1); // not moving and generating energy so much be charging
             if(laccOn&&!accOn){ // Car turned off
                 if (repeatPoll) { // Log on shutdown if autopoll enabled
                     tripLog(); // Write trip log on powerdown
@@ -718,12 +722,10 @@
             if(mph[0]>99){
                 mph[0]=0;
             }
-            miles_trip[0]+=mph[0]/3600;
-            miles_trip[1]+=mph[0]/3600;
-            miles_trip[2]+=mph[0]/3600;
+            numSsamples=0;
 
-            mpkWh[0]=mph[0];
             if(numWsamples>0){ // Avoid div0
+                mpkWh[0]=mph[0];
                 kW[0]=((float) mWs_x4)/numWsamples/4e3;     
                 mpkWh[0]/=kW[0];
                 if (mpkWh[0]<0) {
@@ -733,14 +735,23 @@
                 kW[0]=0;
                 mpkWh[0]=0;
             }
-            kWh_trip[0]+=kW[0]/3600;
-            kWh_trip[1]+=kW[0]/3600;
-            kWh_trip[2]+=kW[0]/3600;
+            numWsamples=0;
+
+            if (!charging){
+                miles_trip[0]+=mph[0]/3600;
+                miles_trip[1]+=mph[0]/3600;
+                miles_trip[2]+=mph[0]/3600;
+                kWh_trip[0]+=kW[0]/3600;
+                kWh_trip[1]+=kW[0]/3600;
+                kWh_trip[2]+=kW[0]/3600;
+            }
+            
             motorRPM=0;
-            numSsamples=0;
             mWs_x4=0;
-            if((curRmax-curRmin)<10){ // At least 5V change?
-                // do nothing - insufficient delta_V to measure resistance
+            
+            // Compute ESR
+            if((Imax-Imin)<40){ // do nothing - insufficient delta_I to measure
+                unloadedV_x2 = (curRmax+curRmin)/2;
             }else if ((redRmax-redRmin)<(curRmax-curRmin)) {
                 Resr-=0.001;
                 unloadedV_x2 = (redRmax+redRmin)/2;
@@ -756,15 +767,17 @@
             incRmax=0;
             redRmin=1000;
             redRmax=0;
-            numWsamples=0;
-            if(accOn||playbackEn){
+            Imax=-1000;
+            Imin=1000;
+
+            if((accOn||playbackEn)&&!charging){
                 for(i=1;i<39;i++){
                     average=mph[i]/timeConstant[i];
                     mph[i]-=average;
                     mph[i]+=mph[0];
                     mpkWh[i]=average;
                     average=kW[i]/timeConstant[i];
-                    if((mph[0]>0)||(kW[0]>0)){ //Not charging - so include in efficiency data
+                    if(!charging){ //Not charging - so include in efficiency data
                         kW[i]-=average;
                         kW[i]+=kW[0];
                     }
@@ -775,7 +788,6 @@
                     //mpkWh[i]=floor(mpkWh[i]*10+0.5)/10; // Round to nearest 10th
                }
             }
-            updateDTE=true;
             if(logCP&&usbEn){
                 if(logOnce){
                     tripLog();
@@ -787,7 +799,8 @@
                 usbEn=detectUSB(); // Keep looking if none found
             }
             waitasec=false; // work around to avoid hang when USB tries to init immediately
-        }
+            tock=true;
+        } // tick
 
         display=display<1?display+1:0; // toggle display
         updateDisplay(display);