Dual CANbus monitor and instrumentation cluster
Dependencies: SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed
Fork of CANary by
Revision 49:a3d2c5bb3cfa, committed 2013-04-07
- Comitter:
- TickTock
- Date:
- Sun Apr 07 08:29:04 2013 +0000
- Parent:
- 48:d1ce92104a1f
- Child:
- 50:83d5864c64a0
- Commit message:
- Added graphical DTE - flickers...
Changed in this revision
--- a/SPI_TFTx2.lib Sat Apr 06 04:18:49 2013 +0000 +++ b/SPI_TFTx2.lib Sun Apr 07 08:29:04 2013 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/TickTock/code/SPI_TFTx2/#c39d5b3745af +http://mbed.org/users/TickTock/code/SPI_TFTx2/#ff509eb02e37
--- a/displayModes.cpp Sat Apr 06 04:18:49 2013 +0000 +++ b/displayModes.cpp Sun Apr 07 08:29:04 2013 +0000 @@ -79,7 +79,7 @@ void mainDisplay (bool force, bool showButtons){ unsigned short gids, SOC, packV; static unsigned short lgids=0, lSOC=0, lpackV=0; - static float lkW=0, laccV=0;; + static float lkW=0, laccV=0, lmpkWh=0; CANMessage msg; msg = lastMsg[indexLastMsg[0x5bc]]; //Get gids @@ -94,16 +94,17 @@ tt.set_font((unsigned char*) Arial28x28); if(force) tt.cls(); if(skin==ttSkin){ - if(force||gids!=lgids){ + if(force||gids!=lgids||mpkWh[9]!=lmpkWh){ tt.locate(10,10); printf("%4d gids \n",gids); - tt.locate(20,40); - printf("%4.1f kWh \n",(float)gids*0.08); + tt.locate(10,40); + printf("%4.1f kWh \n",(float)(gids-5)*0.075); tt.set_font((unsigned char*) SCProSB31x55); tt.foreground(Green); tt.locate(60,96); - printf("%4.1f mi \n",(float)(gids-5)*0.31); // Approx for now + printf("%4.1f mi \n",mpkWh[9]*((float)(gids-5)*.075)); // Use 1 minute average lgids=gids; + lmpkWh-mpkWh[9]; tt.foreground(Yellow); tt.set_font((unsigned char*) Arial28x28); } @@ -122,10 +123,11 @@ printf("%3.1fV \n",accV); laccV=accV; } - if(force||kW!=lkW){ + if(force||kW[0]!=lkW){ tt.locate(180,40); - printf("%3.2fkW \n",kW); - lkW=kW; + printf("%3.2fkW \n",kW[0]); + //printf("%3.1f mpkWh \n",mpkWh[0]); + lkW=kW[0]; } }else {//if(skin==ggSkin){ if(force||gids!=lgids){ @@ -163,10 +165,10 @@ printf("%3.1fV \n",accV); laccV=accV; } - if(force||kW!=lkW){ + if(force||kW[0]!=lkW){ tt.locate(160,40); // gg - move left to keep from wrap - printf("%3.2fkw \n",kW); // use small w to save space - lkW=kW; + printf("%3.2fkw \n",kW[0]); // use small w to save space + lkW=kW[0]; } } } @@ -726,19 +728,67 @@ } void dteDisplay(bool force, bool showButtons){ - static float lmpkWh=0; - static float lMPH=0; + unsigned short i,x,y,lx,ly; tt.background(Navy); - if(force) tt.cls(); - if(force||mpkWh!=lmpkWh||MPH!=lMPH){ - tt.foreground(Yellow); - tt.set_font((unsigned char*) Arial28x28); - tt.locate(10,200); - printf("%3.1f \n",MPH); - tt.locate(200,200); - printf("%3.1f \n",mpkWh); - lmpkWh=mpkWh; - lMPH=MPH; + tt.foreground(Yellow); + if(force||updateDTE){ + tt.cls(); + tt.set_font((unsigned char*) Arial12x12); + for(i=0;i<10;i++){ + y=200-i*20; + tt.locate(10,y-8); + printf("%d.0\n",i); + tt.line(40,y,280,y,LightGrey); + } + + x=50+0*6; + tt.locate(x-8,226); + printf("sec\n"); + tt.line(x,10,x,220,LightGrey); + x=50+9*6; + tt.locate(x-8,226); + printf("min\n"); + tt.line(x,10,x,220,LightGrey); + x=50+18*6; + tt.locate(x-8,226); + printf("hour\n"); + tt.line(x,10,x,220,LightGrey); + x=50+25*6; + tt.locate(x-8,226); + printf("day\n"); + tt.line(x,10,x,220,LightGrey); + x=50+32*6; + tt.locate(x-8,226); + printf("mon\n"); + tt.line(x,10,x,220,LightGrey); + x=50+38*6; + tt.locate(x-8,226); + printf("year\n"); + tt.line(x,10,x,220,LightGrey); + + lx=50; + ly=mpkWh[0]*20; + if(ly<200) { + ly=200-ly; + }else{ + ly=0; + } + tt.fillcircle(lx,ly,4,Green); + + for(i=1;i<39;i++){ + x=50+i*6; + y=mpkWh[i]*20; + if(y<200) { + y=200-y; + }else{ + y=0; + } + tt.circle(x,y,2,Green); + tt.line(lx,ly,x,y,White); + lx=x; + ly=y; + } + updateDTE=false; } }
--- a/displayModes.h Sat Apr 06 04:18:49 2013 +0000 +++ b/displayModes.h Sun Apr 07 08:29:04 2013 +0000 @@ -29,11 +29,11 @@ extern bool playbackEn; extern bool playbackOpen; extern float playbackInt; -extern float kW; -extern float mpkWh; -extern float MPH; +extern float kW[39]; +extern float mpkWh[39]; extern unsigned char whichTouched; extern unsigned char skin; +extern bool updateDTE; extern "C" { void printLast (bool force, bool showButtons);
--- 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;