Dual CANbus monitor and instrumentation cluster supporting ILI9341 display controller
Dependencies: SPI_TFTx2_ILI9341 TOUCH_TFTx2_ILI9341 TFT_fonts mbed
Fork of CANary by
Diff: main.cpp
- Revision:
- 184:5ca7b78297fe
- Parent:
- 183:a1fba6f76e69
- Child:
- 186:d1f9b57fdd27
--- a/main.cpp Sun Mar 30 03:31:37 2014 +0000 +++ b/main.cpp Tue May 06 14:43:58 2014 +0000 @@ -10,8 +10,8 @@ // * Add on screen messages for heater on, etc, and use refresh feature above to clear in x seconds // * Be more efficient with write buffer (use msgLen instead of always storing 8 bytes) -// rev183 -// * Fix wrong whpg bug when charging away from home (now update and clear on each charge) +// rev184 +// Fixed wh adjustment algorithm #include "mbed.h" #include "CAN.h" @@ -22,7 +22,7 @@ #include "displayModes.h" #include "TOUCH_TFTx2.h" -char revStr[7] = "183"; +char revStr[7] = "184"; unsigned long maxTarget = 1000; FATFS USBdrive; LocalFileSystem local("local"); @@ -515,6 +515,9 @@ maxTripEff = 0; minTripEff = 5; beep(2000,0.25); + for(i=0;i<300;i++){ // initialize wh lookup + wh[i]=i*kWperGid*1000; + } } else if (dMode[whichTouched]==playbackScreen) { // pause/unpause playbackEn=!playbackEn; if(playbackEn){ @@ -536,8 +539,6 @@ sMode=0; } else if (dMode[whichTouched]==configScreen) { dMode[whichTouched]=mainScreen; - saveConfig(); - beep(2000,0.25); //write efficiency history data hfile = fopen("/local/ehist.cny", "w"); if (hfile!=NULL){ // found a efficiency history file @@ -555,6 +556,8 @@ fclose(hfile); } beep(2000,0.25); + saveConfig(); + beep(2000,0.25); } else if (dMode[whichTouched]==config2Screen) { showHealth = !showHealth; } else if (dMode[whichTouched]==playbackScreen) { // faster @@ -830,7 +833,7 @@ if(heaterOn){ lHeaterOn=true; // Only indicate heater once per power cycle } - heaterOn =((lastMsg[indexLastMsg[0x54f]].data[5]&0x3f)>0)?true:false; + heaterOn =((lastMsg[indexLastMsg[0x54f]].data[5]&0x3f)>2)?true:false; if(heaterMon && heaterOn && !lHeaterOn){ //Heat on alarm beep3(800,0.25,1200,0.25,1600,0.25); //beep(1600,0.5); @@ -886,44 +889,36 @@ seconds = time(NULL); t = *localtime(&seconds); if(miles_trip[1]<1){ // charged since last trip + + // Adjust wh lookup with whpg data maxWhpg=0; minWh=0; whOff=0; - // Adjust wh lookup with whpg data weighted 20% for(i=1;i<300;i++){ - if(whpg[i]>maxWhpg){ - maxWhpg=whpg[i]; - minWh=wh[i]; + if(whpg[i]>maxWhpg){ //Find maxWhpg and associated Wh + maxWhpg = (float) whpg[i]; + minWh = wh[i]; if(debugMode){ - sprintf(sTemp,"maxWhpg=%3.1f;minWh=%3.1f\n", maxWhpg,minWh); - printMsg(sTemp); // revision + sprintf(sTemp,"maxWhpg=%3.1f; minWh=%3.1f\n", maxWhpg, minWh); + printMsg(sTemp); } } - if(minWh>0){ - if((whpg[i]==0)&&(whpg[i-1]>0)){ //Remember the offset at the end of the measured range - whOff = minWh+maxWhpg-wh[i]; - if(debugMode){ - sprintf(sTemp,"whOff=%3.1f\n", whOff); - printMsg(sTemp); // revision - } - } - wh[i] *= 4; - if(whpg[i]==0){ // Apply offset to all levels outside measured range - wh[i] += wh[i]; - wh[i] += whOff; - }else{ // Apply adjustment to measured range - wh[i] += minWh; - wh[i] += maxWhpg; - wh[i] -= (float) whpg[i]; + if(whpg[i]>0){ // Compute adjustment to measured range + whOff = (maxWhpg-(float)whpg[i])-(wh[i]-minWh); + }else if(whpg[i-1]>0){ // Compute final offset for rest of range + whOff = maxWhpg-(wh[i]-minWh); + if(debugMode){ + sprintf(sTemp,"whOff=%3.1f\n", whOff); + printMsg(sTemp); } - wh[i] /= 5; } + wh[i] += 0.1*whOff; // Add offset; use last known good offset when no data + whpg[i-1]=0; } - for(i=0;i<300;i++){ // clear array - whpg[i]=0; - } + whpg[299]=0; + + // Check and reset daily efficiency if charged since last trip and at least 24 hours has past if((t.tm_yday>lt.tm_yday)&&(t.tm_hour>effCheckTime)){ - // Check and reset daily efficiency if charged since last trip and at least 24 hours has past if (!ignoreDayData&&(miles_trip[3]>15)){ // Ignore low mileage data curEff = miles_trip[3]/kWh_trip[3]; // Get current daily efficiency if (maxTripEff<curEff) { @@ -985,7 +980,11 @@ } if((cgids>0)&&(cgids<300)){ if(cgids!=lgids){ - whpg[cgids] = (unsigned short) (1000*(kWh_trip[1]+CCkWh_trip[1])); // Save kWh for each gid since last charge + if((kWh_trip[1]+CCkWh_trip[1])>0){ + whpg[cgids] = (unsigned short) (1000*(kWh_trip[1]+CCkWh_trip[1])); // Save kWh for each gid since last charge + }else{ + whpg[cgids] = 0; + } lgids=cgids; } }