test fork
Dependencies: SPI_TFTx2 SPI_TFTx2_ILI9341 TFT_fonts TOUCH_TFTx2 mbed
Fork of CANary_9341 by
Diff: main.cpp
- Revision:
- 49:a3d2c5bb3cfa
- Parent:
- 48:d1ce92104a1f
- Child:
- 50:83d5864c64a0
diff -r d1ce92104a1f -r a3d2c5bb3cfa main.cpp --- a/main.cpp Sat Apr 06 04:18:49 2013 +0000 +++ b/main.cpp Sun Apr 07 08:29:04 2013 +0000 @@ -81,29 +81,41 @@ float ledLo = 0.1; // Dim LED value (until config file read) unsigned short pollInt = 300; // polling interval=5 minutes (until config file read) bool accOn = false; // Accessories on +bool laccOn = false; float scale12V = 16.2; // R1:R2 ratio signed long mWs_x4 = 0; unsigned short numWsamples = 0; unsigned short numSsamples = 0; -float kW = 0; -float mpkWh = 0; float accV = 0; bool playbackEn = false; bool playbackOpen = false; -float playbackInt = 0.05; //read messages every 50 ms +//float playbackInt = 0.05; //read messages every 50 ms +float playbackInt = 0.005; //read messages every 50 ms bool step = false; char header[5]; char data[8]; signed long motorRPM; -float MPH = 0; unsigned char skin = 0; +float mph[39]={0}; +float kW[39]={0}; +float mpkWh[39]={0}; +// Logarithmic division scale (roughly - snapped to common units of time) +// First 10 are updated each second; the rest are updated each minute +float timeConstant[39] = {1, 1.58, 2.51, 3.98, 6.31, 10, 15.8, 25.1, 39.8, 60, // 1 minute + 1.58, 2.51, 3.98, 6.31, 10, 15.8, 25.1, 39.8, 60, // 1 hour + 60*1.58, 60*2.51, 60*3.98, 60*6.31, 60*10, 60*15.8, 60*24, // 1 day + 60*24*1.58, 60*24*2.51, 60*24*3.98, 60*24*6.31, 60*24*10, 60*24*15.8, 60*24*30, // 1 month + 60*24*39.8, 60*24*63.1, 60*24*100, 60*24*158, 60*24*251, 60*24*365}; // 1 year +bool updateDTE = false; int main() { int readPointer=0; char sTemp[40]; unsigned long secs; - unsigned char i,j,display=0,lwt=0;; + unsigned char i,j,display=0,lwt=0; + unsigned char sixtySecCount=0; point lastTouch; + float average,mph9,kW9; can1.monitor(true); // set to snoop mode can2.monitor(true); // set to snoop mode @@ -129,7 +141,6 @@ NVIC_SetPriority(TIMER3_IRQn, 1); //set ticker priority NVIC_SetPriority(CAN_IRQn, 2); //higher than can (so RTC sync works) - seconds = time(NULL); t = *localtime(&seconds) ; // is it a date before 2012 ? @@ -143,10 +154,7 @@ t = *localtime(&seconds) ; strftime(sTemp, 32, "%a %m/%d/%Y %X\n", &t); logMsg(sTemp); - - //---- revisions ---- - sprintf(sTemp,"CANary firmware rev48\n"); - + sprintf(sTemp,"CANary firmware rev49\n"); logMsg(sTemp); // Look for new binary on thumbdrive @@ -185,6 +193,29 @@ secsNoMsg = 0; + //read efficiency history data + cfile = fopen("/local/ehist.cny", "r"); + if (cfile!=NULL){ // found a efficiency history file + for(i=0;i<39;i++){ + if(!feof(cfile)){ + fscanf(cfile,"%f %f\r\n",&mph[i],&kW[i]); + mpkWh[i]=mph[i]/kW[i]; + } + } + fclose(cfile); + sprintf(sTemp,"History Loaded.\n"); + logMsg(sTemp); + } else { // create initial file + sprintf(sTemp,"History not found. Created.\n"); + logMsg(sTemp); + for(i=0;i<39;i++){ + // Pre-load with 4 mpkWh @ 40 mph + mph[i]=40*timeConstant[i]; + kW[i]=10*timeConstant[i]; + mpkWh[i]=4; + } + } + // Read config file readConfig(); @@ -477,6 +508,17 @@ if(tick){ // Executes once a second accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th accOn=(accV>5)?true:false; + if(laccOn&&!accOn){ // Car turned off + //write efficiency history data + cfile = fopen("/local/ehist.cny", "w"); + if (cfile!=NULL){ // found a efficiency history file + for(i=0;i<39;i++){ + fprintf(cfile,"%f %f\r\n",mph[i],kW[i]); + } + fclose(cfile); + } + } + laccOn=accOn; if(!accOn&&!logEn&&userIdle&&!playbackEn){ //sprintf(sTemp,"Display Off %4.2f\n",accV); //logMsg(sTemp); @@ -486,20 +528,66 @@ }else{ dled = ledLo; } + + //compute historic efficiency if(numSsamples>0){ // Avoid div0 - MPH=((float) motorRPM)/numSsamples/215; // Empirically derived - may change car to car + mph[0]=((float) motorRPM)/numSsamples/215; // Empirically derived - may change car to car }else{ - MPH=0; + mph[0]=0; } + if(mph[0]>99){ + mph[0]=0; + } + mpkWh[0]=mph[0]; if(numWsamples>0){ // Avoid div0 - kW=((float) mWs_x4)/numWsamples/4e3; - mpkWh=floor(MPH*10/kW+0.5)/10; // Round to nearest 10th + kW[0]=((float) mWs_x4)/numWsamples/4e3; + mpkWh[0]/=kW[0]; + if (mpkWh[0]<0) { + mpkWh[0]=99;// negative means inf. + } }else{ - kW=0; - } motorRPM=0; + kW[0]=0; + mpkWh[0]=0; + } + //mpkWh[0]=floor(mpkWh[0]*10+0.5)/10; // Round to nearest 10th + motorRPM=0; numSsamples=0; mWs_x4=0; numWsamples=0; + // First ten are updated each second + for(i=1;i<10;i++){ + mph9=mph[i]/timeConstant[i]; + mph[i]-=mph9; + mph[i]+=mph[0]; + mpkWh[i]=mph9; + kW9=kW[i]/timeConstant[i]; + kW[i]-=kW9; + kW[i]+=kW[0]; + mpkWh[i]/=kW9; + if (mpkWh[i]<0) { + mpkWh[i]=99;// negative means inf. + } + //mpkWh[i]=floor(mpkWh[i]*10+0.5)/10; // Round to nearest 10th + } + // The rest are updated each minute + if(++sixtySecCount>59){ // + sixtySecCount=0; + for(i=10;i<39;i++){ + average=mph[i]/timeConstant[i]; + mph[i]-=average; + mph[i]+=mph9; + mpkWh[i]=average; + average=kW[i]/timeConstant[i]; + kW[i]-=average; + kW[i]+=kW9; + mpkWh[i]/=average; + if (mpkWh[i]<0) { + mpkWh[i]=99;// negative means inf. + } + //mpkWh[i]=floor(mpkWh[i]*10+0.5)/10; // Round to nearest 10th + } + } + updateDTE=true; if(logCP) logPackVoltages(); // Turbo3 tick=false;