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

Files at this revision

API Documentation at this revision

Comitter:
TickTock
Date:
Wed Nov 20 13:13:54 2013 +0000
Parent:
159:577e9d015693
Child:
161:71ac85d11f03
Commit message:
// * Tweaked heater monitor to only issue once per power on/off; // * Added filtering to timeSync routing (some times set time to bogus value); // * Removed debug wait commands from config save and firmware update; // * Added 3-tone sound messages

Changed in this revision

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/main.cpp	Fri Nov 08 16:07:53 2013 +0000
+++ b/main.cpp	Wed Nov 20 13:13:54 2013 +0000
@@ -8,8 +8,11 @@
 // * Change pack volt color when CVLI fails
 // * Add tire pressure cal (40psi for me = FR 38, RR 38.2, FL 37.8, RL 38 - maybe 2psi error on my tire gauge?)
 
-// rev159
-// * Added heater activation warning message
+// rev160
+// * Tweaked heater monitor to only issue once per power on/off
+// * Added filtering to timeSync routing (some times set time to bogus value)
+// * Removed debug wait commands from config save and firmware update
+// * Added 3-tone sound messages
 
 #include "mbed.h"
 #include "CAN.h"
@@ -19,7 +22,7 @@
 #include "utility.h"
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
-char revStr[7] = "159"; // gg - revision string, max 6 characters
+char revStr[7] = "160"; // gg - revision string, max 6 characters
 
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -120,7 +123,7 @@
 bool step = false;
 char header[5];
 char data[8];
-signed long motorRPM;
+signed long motorRPM_x2;
 unsigned char skin = ttSkin ;
 unsigned char dtePeriod = 14; //ten minute averaging interval
 float kWh_trip[3]={0};
@@ -615,7 +618,7 @@
                                 } else if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = configScreen ; // GoTo Config Screen
                                 } else if ((dMode[whichTouched]==dateScreen)&&accOn){
-                                    syncDateTime();
+                                    syncDone=false; // initiate clock sync
                                     lastDMode[whichTouched]=99;
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
@@ -744,10 +747,13 @@
                 curEff = 0;
             }
             headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false;  // headlight/turn signal indicator
-            lHeaterOn = heaterOn;
+            if(heaterOn){
+                lHeaterOn=true; // Only indicate heater once per power cycle
+            }
             heaterOn =((lastMsg[indexLastMsg[0x54f]].data[5]&0x3f)>0)?true:false;
             if(heaterMon && heaterOn && !lHeaterOn){ //Heat on alarm
-                beep(1600,0.5);
+                beep3(800,0.25,1200,0.25,1600,0.25);
+                //beep(1600,0.5);
             }
             if(accOn&&indexLastMsg[0x355]>0){
                 miles_kmbar = (lastMsg[indexLastMsg[0x355]].data[4]&0x20)?true:false;  // indicates selected distance units
@@ -758,6 +764,7 @@
             moving=(mph[0]>0.1);
             charging=(lastMsg[indexLastMsg[0x5bf]].data[2]>0)?true:false; // FF when charging
             if (laccOn&&!accOn){ // Car turned off
+                lHeaterOn=false;
                 if (showHealth){
                     if (saveDmode==99){
                         saveDmode=dMode[0];
@@ -791,6 +798,7 @@
                 }
             }
             if (!laccOn&&accOn){ // Car turned on
+                lHeaterOn=false;
                 miles_trip[0]=0;
                 kWh_trip[0]=0;
                 wait5secs=5;
@@ -834,7 +842,11 @@
 
             //compute historic efficiency
             if(numSsamples>0){ // Avoid div0
-                mph[0]=((float) motorRPM)/numSsamples/220; // Empirically derived with MXV4s - may change with different wheels&tires
+                // calibrated to dash mph which reads slightly fast.
+                // 227 would give more accurate mph for MXV4s@40psi - 11.75" distance from center of tire to pavement
+                // but then efficiency estimation would not track miles driven as read from the odometer so
+                // making CANary have the same error as the Leaf instrumentation
+                mph[0]=((float) motorRPM_x2)/numSsamples/220; 
             } else {
                 mph[0]=0;
             }
@@ -868,7 +880,7 @@
                 kWh_trip[1]=0;
             }
             
-            motorRPM=0;
+            motorRPM_x2=0;
             mWs_x4=0;
             
             // Compute ESR
--- a/utility.cpp	Fri Nov 08 16:07:53 2013 +0000
+++ b/utility.cpp	Wed Nov 20 13:13:54 2013 +0000
@@ -3,6 +3,9 @@
 unsigned long brkMonRate = 400000;
 unsigned short brkMonThr = 4000;
 Timeout beepOff;
+unsigned char bCount = 2;
+float bFreq[2] = { 1000, 1000 };
+float bTime[2] = { 0.1, 0.1 };
 
 void mbed_reset();
 
@@ -48,7 +51,7 @@
     static unsigned char ii = 0;
     static unsigned char lasti = 0; // indexindex
     static unsigned char bdi=0;
-    static signed short imotorRPM = 0;
+    static signed short imotorRPM_x2 = 0;
     static unsigned short nLost = 0; // gg - overrun
  
     char sTemp[40];    
@@ -349,20 +352,20 @@
             }
             numWsamples++;
         }else if((mType==1)&&(canRXmsg.id==0x1da)){ //Motor Speed
-            imotorRPM=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
-            if(imotorRPM<0){ // take absolute value
-                imotorRPM=-imotorRPM;
+            imotorRPM_x2=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
+            if(imotorRPM_x2<0){ // take absolute value
+                imotorRPM_x2=-imotorRPM_x2;
             }
-            motorRPM+=imotorRPM;
+            motorRPM_x2+=imotorRPM_x2;
             numSsamples++;
 //        }else if((mType==2)&&(canRXmsg.id==0x1ca)){ //Brake Pressure
         }else if((mType==2)&&(canRXmsg.id==0x292)){ //Brake Pressure
             if(brakeMon){
                 if(canRXmsg.data[0]<0xff){
-                    if((canRXmsg.data[6]*imotorRPM)<brkMonThr){
+                    if((canRXmsg.data[6]*imotorRPM_x2)<brkMonThr){
                         chirpInt=0;
                     }else{
-                        chirpInt=brkMonRate/imotorRPM;
+                        chirpInt=brkMonRate/imotorRPM_x2;
                         chirpInt/=canRXmsg.data[6];
                     }
                 }
@@ -517,7 +520,7 @@
     char buffer[bufSize];
     char sTemp[16];
     printMsg("Saving local config file.\n");
-    wait(2);
+    //wait(2);
     cfile = fopen("/local/config.txt", "w");    
     fprintf(cfile,"format 9\r\n");
     fprintf(cfile,"x0_off %d\r\n",tt.x0_off);
@@ -560,7 +563,7 @@
     
     // Make copy of CONFIG.TXT
     printMsg("Saving CONFIG.BAK.\n");
-    wait(2);
+    //wait(2);
     cfile = fopen("/local/CONFIG.TXT", "r");
     sfr = f_open(&efile,"CONFIG.BAK",FA_WRITE|FA_CREATE_NEW);    
     if((cfile != NULL)&&(sfr == FR_OK)){
@@ -577,7 +580,7 @@
 
     // Make copy of ehist.cny
     printMsg("Saving ehist.bak.\n");
-    wait(2);
+    //wait(2);
     cfile = fopen("/local/ehist.cny", "r");
     sfr = f_open(&efile,"ehist.bak",FA_WRITE|FA_CREATE_NEW);    
     if((cfile != NULL)&&(sfr == FR_OK)){
@@ -591,7 +594,7 @@
         fclose(cfile);
         f_close(&efile);
     }
-    wait(2);
+    //wait(2);
 }
 
 void readConfig(){
@@ -777,6 +780,7 @@
     struct tm t; // pointer to a static tm structure
     time_t seconds ;
     CANMessage msg;
+    static unsigned char lastHour, numMatched;
     seconds = time(NULL);
     t = *localtime(&seconds);
     if(modelYear<2013){ //MY2011,2012
@@ -796,8 +800,15 @@
         msg = lastMsg[indexLastMsg[0x509]];
         t.tm_sec = msg.data[2]>>2;
     }
-    if((t.tm_mon>=0)&&(t.tm_mon<12)&&(t.tm_mday>0)&&(t.tm_mday<32)&&(t.tm_hour>=0)&&(t.tm_hour<24)&&(t.tm_min>=0)&&(t.tm_min<60)&&(t.tm_sec>=0)&&(t.tm_sec<60)){ // sanity check result before using
+    if(t.tm_hour==lastHour){ //filter
+        numMatched++;
+    }else{
+        numMatched=0;
+    }
+    lastHour=t.tm_hour;
+    if((numMatched>5)&&(t.tm_mon>=0)&&(t.tm_mon<12)&&(t.tm_mday>0)&&(t.tm_mday<32)&&(t.tm_hour>=0)&&(t.tm_hour<24)&&(t.tm_min>=0)&&(t.tm_min<60)&&(t.tm_sec>=0)&&(t.tm_sec<60)){ // sanity check result before using
         set_time(mktime(&t));
+        numMatched=0;
         return(true);
     }else{
         return(false);
@@ -971,7 +982,7 @@
         int fwc_tmp = fwCount;
         readConfig();
         fwCount = fwc_tmp; // Do no overwrite fwcount when loading new config
-        wait(2);
+        //wait(2);
     }
 
     // Check for history file on USB drive
@@ -990,7 +1001,7 @@
             fclose(lfile);
         }
         f_close(&efile);
-        wait(2);
+        //wait(2);
     }
 
     sfr = f_open(&efile,"firmware.bin",FA_READ|FA_OPEN_EXISTING);    
@@ -1029,7 +1040,7 @@
     } else {
       //could not open directory
         printf("Couldn't open folder.\n");
-        wait(2);
+        wait(3);
         return;
     }        
     printf("Copying new firmware.\n");
@@ -1037,12 +1048,12 @@
     //The newest bin file is the one that is used by the mbed  
     sprintf(sTemp,"/local/fw%d.bin",fwCount);
     printf("Writing %s.\n",sTemp);
-    wait(2);
+    //wait(2);
     lfile = fopen(sTemp, "wb");
     if(lfile == NULL)
     {
         printf("Couldn't open destination.\n");
-        wait(2);
+        wait(3);
         return;
     }
 
@@ -1073,8 +1084,13 @@
 }
 
 void spkrOff(void){
-    spkr=0;
-    dled.period(.001);
+    if(bCount<2){
+        spkr.period(1.0/bFreq[bCount]);
+        beepOff.attach(&spkrOff, bTime[bCount++]);   
+    }else{
+        spkr=0;
+        dled.period(.001);
+    }
 }
 
 void beep(float freq, float time){
@@ -1090,6 +1106,15 @@
     }
 }
 
+void beep3(float freq1, float time1, float freq2, float time2, float freq3, float time3){
+    bFreq[0]=freq2;
+    bTime[0]=time2;
+    bFreq[1]=freq3;
+    bTime[1]=time3;
+    bCount=0;
+    beep(freq1, time1);
+}
+
 void chirp(void){
     static unsigned short counter=0;
     
--- a/utility.h	Fri Nov 08 16:07:53 2013 +0000
+++ b/utility.h	Wed Nov 20 13:13:54 2013 +0000
@@ -47,7 +47,7 @@
 extern float playbackInt;
 extern signed long mWs_x4;
 extern unsigned short numWsamples;
-extern signed long motorRPM;
+extern signed long motorRPM_x2;
 extern unsigned short numSsamples;
 extern unsigned char skin;
 extern unsigned char dtePeriod;
@@ -111,6 +111,7 @@
     bool detectUSB();
     void chirp();
     void beep(float freq, float time);
+    void beep3(float freq1, float time1, float freq2, float time2, float freq3, float time3);
 }
 
 //LEAF OBD