Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Files at this revision

API Documentation at this revision

Comitter:
TickTock
Date:
Mon Jul 08 02:08:08 2013 +0000
Branch:
Metric
Parent:
119:0e2b641e98a2
Child:
121:553faf139a20
Commit message:
Changed Resr algorithm to one requiring less computation. Ignore data for small delta_V.

Changed in this revision

common.h Show annotated file Show diff for this revision Revisions of this file
displayModes.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
utility.cpp Show annotated file Show diff for this revision Revisions of this file
utility.h Show annotated file Show diff for this revision Revisions of this file
--- a/common.h	Fri Jul 05 16:55:04 2013 +0000
+++ b/common.h	Mon Jul 08 02:08:08 2013 +0000
@@ -33,3 +33,5 @@
 #define BatDataBaseG5 0x2B // 11 frames
 #define BatDataBaseG6 0x36
 #define BatDataBufMax 0x1B2 // 7 x 3E bytes
+
+#define VP230Sleep 1 // Set to 0 if using VP231 (sleep disables RX)
\ No newline at end of file
--- a/displayModes.cpp	Fri Jul 05 16:55:04 2013 +0000
+++ b/displayModes.cpp	Mon Jul 08 02:08:08 2013 +0000
@@ -65,18 +65,18 @@
             laccV=accV;
         }
         if(force||kW[0]!=lkW){
-            tt.locate(180,70);
+            tt.locate(180,40);
             printf("%3.2fkW  \n",kW[0]);
-            //printf("%3.1f mpkWh  \n",mpkWh[0]);
             lkW=kW[0];
         }
+        tt.foreground(DarkGrey);
         if(force||Ah_x10000!=lAh){
             tt.locate(10,70);
             printf("% 4.2fAh \n",(float)Ah_x10000/10000);
             lAh=Ah_x10000;
         }
         if(force||SOH_x100!=lSOH){
-            tt.locate(180,40);
+            tt.locate(180,70);
             printf(" %3.1f%s\n",(float)SOH_x100/100,"% ");
             lSOH=SOH_x100;
         }
@@ -88,7 +88,6 @@
             tt.locate(40,40); // gg - add GIDs Percent of 281
             printf("%4.1f%s \n", (float)gids*0.355872, "% ") ;
             tt.locate(20,70);
-            //printf("%4.1f kWh  \n",(float)gids*0.08); // is input, not usable
             printf("%4.1f kwh  \n",(float)gids*0.075); // gg - closer to usable
             
             tt.set_font((unsigned char*) SCProSB31x55);
@@ -253,22 +252,22 @@
     }
     if(force||Ah_x10000!=lAh){
         tt.locate(10,100);
-        printf(" %4.2fAh \n",(float)Ah_x10000/10000);
+        printf(" %4.2f Ah cap \n",(float)Ah_x10000/10000);
         lAh=Ah_x10000;
     }
     if(force||maxTemp!=lmaxTemp){
         tt.locate(10,130);
-        printf(" %4.1f%s max temp \n",convertTemperature(maxTemp),temperatureUnit());
+        printf(" %4.1f %s (max) \n",convertTemperature(maxTemp),temperatureUnit());
         lmaxTemp=maxTemp;
     }
     if(force||unloadedV_x2!=lunlV){
         tt.locate(10,160);
-        printf(" %4.1fV \n",unloadedV_x2/2);
+        printf(" %4.1f V \n",unloadedV_x2/2);
         lunlV=unloadedV_x2;
     }
     if(force||Resr!=lresr){
         tt.locate(10,190);
-        printf(" %4.1fmOhms ESR \n",Resr*1000);
+        printf(" %3.0f mOhms \n",Resr*1000);
         lresr=Resr;
     }
 }
--- a/main.cpp	Fri Jul 05 16:55:04 2013 +0000
+++ b/main.cpp	Mon Jul 08 02:08:08 2013 +0000
@@ -23,7 +23,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "119"; // gg - revision string, max 6 characters
+char revStr[7] = "120"; // gg - revision string, max 6 characters
 
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -47,8 +47,10 @@
 InterruptIn touchpad(p17);
 CAN can1(p9, p10);      // CAN1 (EV) uses pins 9 and 10 (rx, tx) and pin 8 (rs)
 DigitalOut can1SleepMode(p8);     // Use pin 8 to control the sleep mode of can2
+//DigitalInOut can1SleepMode(p8);     // Use pin 8 to control the sleep mode of can2
 CAN can2(p30, p29);     // CAN2 (CAR) uses pins 30 and 29 (rx, tx) and pin 28 (rs)
 DigitalOut can2SleepMode(p28);     // Use pin 28 to control the sleep mode of can1
+//DigitalInOut can2SleepMode(p28);     // Use pin 28 to control the sleep mode of can1
 AnalogIn mon12V(p15);
 TOUCH_TFTx2 tt(p16, p17, p19, p20, p11, p12, p13, p6, p7, p5, "TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs0, cs1, reset
 PwmOut dled(p23);
@@ -123,7 +125,7 @@
 float mph[39]={0};
 float kW[39]={0};
 float mpkWh[39]={0};
-float unloadedVavg_x2,unloadedV_x2,Resr,Rlms,redRlms,incRlms;
+float unloadedV_x2,Resr,curRmax,curRmin,redRmax,redRmin,incRmax,incRmin;
 // 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
@@ -140,6 +142,8 @@
 bool metric = false;
 
 int main() {
+    //can1SleepMode.mode(OpenDrain);
+    //can2SleepMode.mode(OpenDrain);
     char sTemp[40];
     unsigned long secs;
     unsigned char i,j,display=0,lwt=0;
@@ -153,7 +157,7 @@
     touchpad.rise(&touch_ISR);
     tt.wfi();               // enable interrupt on touch
     dled = 0.8; // turn on display LED 80%
-    Resr = 0.1; // initial guess of Resr
+    Resr = 0.075; // initial guess of Resr
     timer.start() ;
     RTC_Init(); // start the RTC Interrupts that sync the timer
     struct tm t; // pointer to a static tm structure
@@ -212,8 +216,8 @@
     can2.monitor(true); // set to snoop mode
     can1.frequency(500000);
     can2.frequency(500000);
-    can1SleepMode = 1;         // Turn on Monitor_only Mode
-    can2SleepMode = 1;         // Turn on Monitor_only Mode
+    can1SleepMode = VP230Sleep;         // Turn on Monitor_only Mode
+    can2SleepMode = VP230Sleep;         // Turn on Monitor_only Mode
     can1.attach(&recieve1);
     can2.attach(&recieve2);
 
@@ -725,15 +729,6 @@
                 if (mpkWh[0]<0) {
                     mpkWh[0]=99;// negative means inf.
                 }
-                unloadedV_x2 = unloadedVavg_x2/numWsamples;
-                //Rlms /= numWsamples; //unecessary
-                //redRlms /= numWsamples;
-                //incRlms /= numWsamples;
-                if (redRlms<Rlms) {
-                    Resr-=0.001;
-                } else if (incRlms<Rlms) {
-                    Resr+=0.001;
-                }
             } else {
                 kW[0]=0;
                 mpkWh[0]=0;
@@ -744,10 +739,23 @@
             motorRPM=0;
             numSsamples=0;
             mWs_x4=0;
-            unloadedVavg_x2=0;
-            Rlms=0;
-            redRlms=0;
-            incRlms=0;
+            if((curRmax-curRmin)<10){ // At least 5V change?
+                // do nothing - insufficient delta_V to measure resistance
+            }else if ((redRmax-redRmin)<(curRmax-curRmin)) {
+                Resr-=0.001;
+                unloadedV_x2 = (redRmax+redRmin)/2;
+            } else if ((incRmax-incRmin)<(curRmax-curRmin)) {
+                Resr+=0.001;
+                unloadedV_x2 = (incRmax+incRmin)/2;
+            } else {
+                unloadedV_x2 = (curRmax+curRmin)/2;
+            }
+            curRmin=1000;
+            curRmax=0;
+            incRmin=1000;
+            incRmax=0;
+            redRmin=1000;
+            redRmax=0;
             numWsamples=0;
             if(accOn||playbackEn){
                 for(i=1;i<39;i++){
--- a/utility.cpp	Fri Jul 05 16:55:04 2013 +0000
+++ b/utility.cpp	Mon Jul 08 02:08:08 2013 +0000
@@ -298,10 +298,24 @@
             imWs_x4 = packV_x2; // Volts*milliSeconds*2
             imWs_x4 *= -packA_x2; // milliWattseconds*4
             mWs_x4 += imWs_x4; // total mWs_x4
-            unloadedVavg_x2 += packV_x2-Resr*packA_x2;
-            Rlms    += (packV_x2-Resr*packA_x2-unloadedV_x2)*(packV_x2-Resr*packA_x2-unloadedV_x2);
-            redRlms += (packV_x2-(Resr-0.001)*packA_x2-unloadedV_x2)*(packV_x2-(Resr-0.001)*packA_x2-unloadedV_x2);
-            incRlms += (packV_x2-(Resr+0.001)*packA_x2-unloadedV_x2)*(packV_x2-(Resr+0.001)*packA_x2-unloadedV_x2);
+            float temp = packV_x2-Resr*packA_x2;
+            if(temp>curRmax){
+                curRmax=temp;
+            } else if(temp<curRmin){
+                curRmin=temp;
+            }
+            temp = packV_x2-(Resr-0.001)*packA_x2;
+            if(temp>redRmax){
+                redRmax=temp;
+            } else if(temp<redRmin){
+                redRmin=temp;
+            }
+            temp = packV_x2-(Resr+0.001)*packA_x2;
+            if(temp>incRmax){
+                incRmax=temp;
+            } else if(temp<incRmin){
+                incRmin=temp;
+            }
             numWsamples++;
         }else if((mType==1)&&(canRXmsg.id==0x1da)){ //Motor Speed
             imotorRPM=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
@@ -359,8 +373,6 @@
                 data[2]=0x01;
                 break;
             case BatDataBaseG2: // group 1 has 6 frames
-                can1.monitor(false); // set to active mode
-                can1SleepMode = 0; // enable TX
                 data[0]=0x02; //change to request group 2 (cp data)
                 data[1]=0x21;
                 data[2]=0x02;
@@ -382,10 +394,9 @@
                 break;
             case BatDataBaseG6: // group 5 has 11 frames
                 reqMsgCnt = 99;
-                can1SleepMode = 1; // disable TX
+                can1SleepMode = VP230Sleep; // disable TX
                 can1.monitor(true); // set to snoop mode
                 msgReq.detach(); // stop ticker
-                //logCP=true; //LAJ for bench test
             default:
                 data[0]=0x30; //change to request next line message
                 data[1]=0x01;
--- a/utility.h	Fri Jul 05 16:55:04 2013 +0000
+++ b/utility.h	Mon Jul 08 02:08:08 2013 +0000
@@ -35,7 +35,7 @@
 extern CAN can1,can2;
 extern unsigned int fwCount;
 extern DigitalOut can1SleepMode,can2SleepMode;
-//extern bool pollCP;
+//extern DigitalInOut can1SleepMode,can2SleepMode;
 extern bool tick;
 extern bool ZeroSecTick;
 extern bool headlights;
@@ -63,7 +63,7 @@
 extern bool metric;
 extern unsigned char lastDMode[2];
 extern unsigned char whichTouched;
-extern float unloadedVavg_x2,unloadedV_x2,Resr,Rlms,redRlms,incRlms;
+extern float unloadedV_x2,Resr,curRmax,curRmin,redRmax,redRmin,incRmax,incRmin;
 extern unsigned char tNavRow ; // gg - 4x4
 extern char revStr[7]; // gg - version
 extern bool debugMode;