test code megasquirt and digole lcd

Dependencies:   FatFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Code Designed to display run-time variables from a Megasquirt Fuel Injection controller.
00002 //This is the base code to load variables and print to DigoleSerialDisp and write them to a file.  File format is .FRD for import into EFI Analytics tuner Studio
00003 
00004 #include "mbed.h"
00005 //#include "TextDigoleSerialDisp.h"
00006 #include "SDFileSystem.h"
00007 #include "SerialBuffered.h"
00008 //#include "LIS302.h"
00009 
00010 
00011 //SD Card pin assignment
00012 SDFileSystem sd(p11, p12, p13, p14, "sd");//DI, D0, SCK, CS
00013 
00014 AnalogIn SDenable(p20);//define SD card switch
00015 DigitalOut myled1(LED1);//define LED
00016 Serial loggerSerial(USBTX, USBRX);
00017 Timer t;
00018 
00019 LocalFileSystem local("local");
00020 
00021 
00022 
00023 //Serial usb(USBTX, USBRX); //define USB connection pins
00024 SerialBuffered megasquirt(120, p28, p27);  // that 128 is the size of the read buffer it will make for you
00025 
00026 Serial DigoleSerialDisp(p9, p10);//UART DigoleSerialDisp(PinName TX, PinName RX);
00027 
00028 DigitalOut usb_activity(LED1); //define USB activity
00029 DigitalOut megasquirt_activity(LED2); //define Megasquirt activity
00030 unsigned char      tmp_data[16];
00031 unsigned char       buf[120];
00032 int tmp;
00033 float Time;
00034 long float SecL; //get secoonds
00035 long float        PW;//get pulseWidth1
00036 long float        PW2; //get pulseWidth2
00037 int        RPM; //get Rpm
00038 float DutyCycle; //Injector Duty Cycle
00039 float DutyCycle2; //Injector Bank2 Duty  Cycle
00040 long float        SparkAdv; //get advance
00041 float        squirt; //get squirt
00042 float        Engine; //get squirt
00043 float       afrtgt1; //get AFR target - Table 1
00044 float        afrtgt2; //get AFR target - Table 2
00045 float        wbo2_en1; //get WideBand Valid1
00046 float        wbo2_en2; //get WideBand Valid2
00047 long float       barometer; //get Barometer
00048 long float        MAP; //get manifold absolute pressure MAP
00049 long float        MAT; //get manifold absolute temperature (MAT)
00050 long float      CLT; //get coolant temperature (CLT)
00051 long float        TP; //get Throttle Position Sensor (TPS)
00052 long float        vBatt; //get BAttery Voltage
00053 long float        AFR; //get Realtime AFR for VE1
00054 long float        AFR2; //get Realtime AFR for VE2
00055 long float        knock; //get Knock Threshold Value
00056 long float       Gego; //get egoCorrrection1 amount %
00057 long float        Gego2; //get egoCorrrection2 amount %
00058 long float       Gair; //get air correction (G_air)
00059 long float        Gwarmup; //get Warmup Enrichment
00060 long float        TPSacc; //get accel enrichment (ms)
00061 long float        TPScut; //get TPS based fuel cut %
00062 long float       Gbaro; //get baroCorrection %
00063 long float       Gammae; //get gammaEnrich %
00064 long float        veCurr1; //get Current VE value Table 1
00065 long float        veCurr2; //get Current VE value Table 2
00066 long float        IAC; //get IAC Step %
00067 long float        ColdAdv; //get Cold Ignition Advance
00068 long float       tpsDOT;    //get Rate of Change TPS
00069 long float        mapDOT;  //get Rate of Change MAP
00070 long float        Dwell; //get Ignition Dwell
00071 long float        maf; //get MAF - Mass Air Flow
00072 long float       fuelload; //get MAP/TPS Blend %
00073 long float        Ethanol; //get fuel load percent alchohol
00074 char                portstatus; //get Spare Port Status
00075 char        knockRetard; //get Knock timing retard (deg)
00076 long float        EAEFuelCorr; //get EAE Fuel correction
00077 long float        egoV; //get egoV
00078 long float        egoV2; //get egoV2
00079 char        status1; //get Status1
00080 char        status2; //get Status2
00081 char        status3; //get Status3
00082 char        status4; //get Status4
00083 long float        looptime; //get looptime
00084 char        status5; //get Status5
00085 long float        tpsADC; //get tpsADC
00086 long float        fuelload2; //get fuelload2
00087 long float        ignload; //get ignload
00088 long float        ignload2; //get ignload2
00089 char        syncstatus; //get Sync-Status (0 - sync-error, 1 - syncstatus)
00090 float        deltaT; //get deltaT
00091 long float        wallfuel; //get wallfuel
00092 
00093 int runtime;
00094 
00095 
00096 unsigned char data_out;
00097 int n;
00098 int row;
00099 int column;
00100 int i;
00101 char MSII_288Header[] = {0x46,0x52,0x44,0x00,0x00,0x00,0x00,0x01,0x4b,0x61,0xf1,0x63,0x4d,0x53,0x49,0x49,0x20,0x52,0x65,0x76,0x20,0x32,0x2e,0x38,0x38,0x30,0x30,0x30,0x20,0x20,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x51,0x00,0x70};//file header
00102 char MSIIExtraHeader[] = {0x46,0x52,0x44,0x00,0x00,0x00,0x00,0x01,0x4b,0x6a,0xf3,0xa8,0x4d,0x53,0x32,0x45,0x78,0x74,0x72,0x61,0x20,0x52,0x65,0x6c,0x20,0x32,0x2e,0x31,0x2e,0x31,0x62,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x51,0x00,0x91};
00103 char parse_header[] = {0x01,0x00};//data header
00104 
00105 
00106 /*DigoleSerialDisp 2x20 parallel code pin assignment*/
00107 //TextDigoleSerialDisp DigoleSerialDisp(p21, p22, p23, p24, p25, p26, p30, 20, 2); // (rs, rw, e, d0, d1, d2, d3, n_column, n_rows)
00108 //define file write
00109 //AnalogIn file_writeenable(p20);//define SD card switch
00110 
00111 
00112 //enter main loop
00113 int main() {
00114 
00115 
00116   DigoleSerialDisp.baud(9600);//set Digole Serial Baud Rate
00117 //    unsigned char tmp;
00118 //    unsigned char data_in;
00119 
00120 /////Startup Screen////////////
00121 DigoleSerialDisp.printf("CL\r");//clear screen
00122 //DigoleSerialDisp.init_Display();
00123 //DigitalOut myled(LED1);
00124 DigoleSerialDisp.printf("  DashView V2.1  ");
00125 wait(3);
00126 DigoleSerialDisp.printf("CL\r");//clear screen
00127 DigoleSerialDisp.printf("SD3\r\n");//set rotation 0=0deg,1=90deg,2=180deg,3=270deg
00128 DigoleSerialDisp.printf("TRT");//offset Line 1 in housing
00129 DigoleSerialDisp.printf("SF3\rTT Have a great drive\r\n");
00130 DigoleSerialDisp.printf("TRT");//next line
00131 
00132 ////////////End Startup Screen//////////
00133     megasquirt.baud(115200); //define MEgasquirt serial speed
00134     loggerSerial.baud(115200); //define USB connection speed
00135 /*
00136 //file header setup
00137     FILE *fp = fopen("/sd/datalog.csv", "a");
00138     //print header for MS.csv file
00139     fprintf(fp, "MSII Rev 2.88000, \nTime,SecL,RPM,MAP,TP,vBatt,AFR,MAT,CLT,Engine,Gego,Gair,Gwarm,Gbaro,Gammae,TPSacc,PW,DutyCycle,PW2,DutyCycle2,SparkAdv,ColdAdv,Dwell,tpsDOT,mapDOT,IAC,deltaT,Ethanol%,\n");
00140 
00141     fclose(fp);
00142 */
00143     t.start();
00144 
00145     while (1) {
00146 
00147 //        char c = loggerSerial.getc();
00148         //temporary call function using keyboard - in future will trigger automatically.
00149 //        if ((c == 'a'))
00150 //       {
00151         //DigoleSerialDisp.printf(" "); //print space for data output format
00152         //poll serial device for data stream (a,0,6) to (request, return, confirm)
00153         megasquirt.putc(97);//send 97 for run parameters, 83 for board revision, 81 code for revision number
00154         wait(0.005);
00155         megasquirt.putc(0);
00156         wait(0.005);
00157         megasquirt.putc(6);
00158         int actuallyRead = megasquirt.readBytes( buf, 112); // where 112 is the number of bytes you are expecting
00159 
00160 
00161 //decodeMS();
00162 
00163         Time = t.read();
00164 
00165 //void decodeMS() {
00166 
00167 //get seconds
00168         tmp = buf[0];
00169         tmp = tmp << 8;
00170         SecL = tmp | buf[1];
00171         //MSseconds = MSseconds/256;
00172 //get pulseWidth1
00173         tmp = buf[2];
00174         tmp = tmp << 8;
00175         tmp = tmp | buf[3];
00176         PW = tmp*0.000666;
00177 
00178 
00179 //get pulseWidth2
00180         tmp = buf[4];
00181         tmp = tmp << 8;
00182         tmp = tmp | buf[5];
00183         PW2 = tmp*0.000666;
00184 //get Rpm
00185         tmp = buf[6];
00186         tmp = tmp << 8;
00187         RPM = tmp | buf[7];
00188 
00189 //Calculate DutyCycle
00190         DutyCycle = (PW * (RPM / 60)) / 10;
00191 
00192 //Calculate DutyCycle2
00193         DutyCycle2 = (PW2 * (RPM / 60)) / 10;
00194 
00195 
00196 //get advance
00197         tmp = buf[8];
00198         tmp = tmp << 8;
00199         tmp = tmp | buf[9];
00200         SparkAdv = tmp/10;
00201 //get squirt
00202         squirt = buf[10];
00203 //get engine
00204         Engine = buf[11];
00205 //get AFR target - Table 1
00206         afrtgt1 = buf[12]*10;
00207 
00208 //get AFR target - Table 2
00209         afrtgt2 = buf[13]*10;
00210 //get WideBand Valid1
00211         wbo2_en1 = buf[14];
00212 //get WideBand Valid2
00213         wbo2_en2 = buf[15];
00214 //get Barometer
00215         tmp = buf[16];
00216         tmp = tmp << 8;
00217         tmp = tmp | buf[17];
00218         barometer = tmp/10;
00219 
00220 //get manifold absolute pressure MAP
00221         tmp = buf[18];
00222         tmp = tmp << 8;
00223         MAP = tmp | buf[19];
00224         MAP = MAP/10;
00225 //get manifold absolute temperature (MAT)
00226         tmp = buf[20];
00227         tmp = tmp << 8;
00228         MAT = tmp | buf[21];
00229        MAT = MAT/10; ///for Farenheit
00230         MAT = 0.555*(MAT - 32); //for Celcius
00231 //get coolant temperature (CLT)
00232         tmp = buf[22];
00233         tmp = tmp << 8;
00234         CLT = tmp | buf[23];
00235         CLT = CLT/10;
00236         CLT = 0.555*(CLT - 32); //for Celcius
00237 //get Throttle Position Sensor (TPS)
00238         tmp = buf[24];
00239         tmp = tmp << 8;
00240         TP = tmp | buf[25];
00241         TP = TP / 10;
00242 //get BAttery Voltage
00243         tmp = buf[26];
00244         tmp = tmp << 8;
00245         vBatt = tmp | buf[27];
00246         vBatt = vBatt /10,
00247 //get Realtime AFR for VE1
00248                 tmp = buf[28];
00249         tmp = tmp  << 8;
00250         AFR = tmp | buf[29];
00251         AFR = AFR /10;
00252 //get Realtime AFR for VE2
00253         tmp = buf[30];
00254         tmp  = tmp << 8;
00255         AFR2 = tmp  | buf[31];
00256         AFR2 = AFR2 / 10;
00257 
00258 
00259 //get Knock Threshold Value
00260         tmp = buf[32];
00261         tmp = tmp << 8;
00262         knock = tmp | buf[33];
00263         knock = knock /10;
00264 //get egoCorrrection1 amount %
00265         tmp = buf[34];
00266         tmp  = tmp  << 8;
00267         Gego = tmp | buf[35];
00268 //get egoCorrrection2 amount %
00269         tmp = buf[36];
00270         tmp = tmp << 8;
00271         Gego2 = tmp | buf[37];
00272 //get air correction (G_air)
00273         tmp = buf[38];
00274         tmp  = tmp << 8;
00275         Gair = tmp | buf[39];
00276 //get Warmup Enrichment
00277         tmp = buf[40];
00278         tmp = tmp << 8;
00279         Gwarmup = tmp | buf[41];
00280 
00281 //get accel enrichment (ms)
00282         tmp = buf[42];
00283         tmp = tmp << 8;
00284         TPSacc = tmp | buf[43];
00285         TPSacc = TPSacc/10;
00286 //get TPS based fuel cut %
00287         tmp = buf[44];
00288         tmp = tmp << 8;
00289         TPScut = tmp | buf[45];
00290 //get baroCorrection %
00291         tmp = buf[46];
00292         tmp = tmp << 8;
00293         Gbaro = tmp | buf[47];
00294 //get gammaEnrich %
00295         tmp = buf[48];
00296         tmp = tmp << 8;
00297         Gammae = tmp | buf[49];
00298 
00299 //get Current VE value Table 1
00300         tmp = buf[50];
00301         tmp  = tmp << 8;
00302         veCurr1 = tmp | buf[51];
00303         veCurr1 = veCurr1/10;
00304 //get Current VE value Table 2
00305         tmp = buf[52];
00306         tmp  = tmp  << 8;
00307         veCurr2 = tmp  | buf[53];
00308         veCurr2 = veCurr2/10;
00309 //get IAC Step %
00310         tmp = buf[54];
00311         tmp = tmp << 8;
00312         IAC = tmp | buf[55];
00313 //get Cold Ignition Advance
00314         tmp = buf[56];
00315         tmp = tmp << 8;
00316         ColdAdv = tmp | buf[57];
00317         ColdAdv = ColdAdv/10;
00318 //get Rate of Change TPS
00319         tmp = buf[58];
00320         tmp = tmp << 8;
00321         tpsDOT = tmp | buf[59];
00322         tpsDOT = tpsDOT/10;
00323 //get Rate of Change MAP
00324         tmp = buf[60];
00325         tmp = tmp << 8;
00326         mapDOT = tmp | buf[67];
00327 //get Ignition Dwell
00328         tmp = buf[62];
00329         tmp = tmp  << 8;
00330         Dwell = tmp | buf[63];
00331         Dwell = Dwell*0.6666;
00332 //get MAF - Mass Air Flow
00333         tmp = buf[64];
00334         tmp = tmp << 8;
00335         maf = tmp | buf[65];
00336 //get MAP/TPS Blend %
00337         tmp = buf[66];
00338         tmp = tmp << 8;
00339         fuelload = tmp | buf[67];
00340         fuelload = fuelload/10;
00341 //get fuel load percent alchohol
00342         tmp = buf[68];
00343         tmp = tmp  << 8;
00344         Ethanol = tmp  | buf[69];
00345 
00346 //get Spare Port Status
00347         portstatus = buf[70];
00348 //get Knock timing retard (deg)
00349         knockRetard = buf[71];
00350         knockRetard = knockRetard/10;
00351 //get EAE Fuel correction
00352         tmp = buf[72];
00353         tmp = tmp << 8;
00354         EAEFuelCorr  = tmp  | buf[73];
00355 
00356 //get egoV
00357         tmp = buf[74];
00358         tmp = tmp  << 8;
00359         egoV = tmp  | buf[75];
00360         egoV = egoV/100;
00361 //get egoV2
00362         tmp = buf[76];
00363         tmp = tmp << 8;
00364         egoV2 = tmp | buf[77];
00365         egoV2 = egoV2/100;
00366 //get Status1
00367         status1 = buf[78];
00368 //get Status2
00369         status2 = buf[79];
00370 //get Status3
00371         status3 = buf[80];
00372 //get Status4
00373         status4 = buf[81];
00374 //get looptime
00375         tmp = buf[82];
00376         tmp = tmp << 8;
00377         looptime = tmp | buf[83];
00378         looptime = looptime*0.6667;
00379 //get Status5
00380         status5 = buf[84];
00381 //get tpsADC
00382         tmp = buf[85];
00383         tmp = tmp << 8;
00384         tpsADC = tmp | buf[86];
00385 //get fuelload2
00386         tmp  = buf[87];
00387         tmp  = tmp  << 8;
00388         fuelload2 = tmp | buf[88];
00389 //get ignload
00390         tmp = buf[89];
00391         tmp = tmp << 8;
00392         ignload = tmp | buf[90];
00393         ignload = ignload/100;
00394 //get ignload2
00395         tmp = buf[91];
00396         tmp = tmp << 8;
00397         ignload2 = tmp  | buf[92];
00398         ignload2 = ignload2/100;
00399 //get Sync-Status (0 - sync-error, 1 - syncstatus)
00400         syncstatus = buf[104];
00401 
00402 //get deltaT
00403         tmp = buf[106];
00404         tmp = tmp  << 8;
00405         deltaT = tmp | buf[107];
00406 //get wallfuel
00407         tmp = buf[110];
00408         tmp = tmp << 8;
00409         wallfuel = tmp  | buf[111];
00410 
00411 ///Display Parameters to DigoleSerialDisp///////////  
00412 DigoleSerialDisp.printf("SF3\r\n");
00413 DigoleSerialDisp.printf("SD3\r\n");//set rotation 0=0deg,1=90deg,2=180deg,3=270deg
00414 DigoleSerialDisp.printf("TRT");//offset Line 1 in housing
00415 
00416         //DigoleSerialDisp.locate(0,1);
00417         DigoleSerialDisp.printf("TT Volt=%4.1f \r\nTRT TT AFR =%4.1f\r\n TRT TT MAT= %3.0f C\r\nTRT TT CLT= %3.0f C\r\nTRT",vBatt,AFR,MAT,CLT);
00418         DigoleSerialDisp.printf("TRT");//next line
00419 
00420         //DigoleSerialDisp.locate(0,0);
00421         //DigoleSerialDisp.printf("TT AFR =%4.1f\r\n", AFR);
00422         //DigoleSerialDisp.printf("TRT");//next line
00423 
00424 
00425 
00426 //get manifold absolute temperature (MAT)
00427  /*
00428         //DigoleSerialDisp.locate(10,1);
00429         DigoleSerialDisp.printf("TT MAT= %3.0f C\r\n", MAT);
00430         DigoleSerialDisp.printf("TRT");//next line
00431         
00432 //get Clt
00433        // CLT = 0.5555*(CLT - 32); //for Celcius  
00434         //DigoleSerialDisp.locate(10,0);
00435         DigoleSerialDisp.printf("TT CLT= %3.0f C\r\n", CLT);
00436         DigoleSerialDisp.printf("TRT");//next line
00437  */       
00438         DigoleSerialDisp.printf("CL\r");//clear screen
00439 
00440 /*
00441 ////////////////////////write to SD Card////////////////////////////////
00442         if (SDenable > 0.5) //switch enable
00443         {
00444             myled1 = 1;
00445             FILE *fp = fopen("/sd/datalog.csv", "a");
00446             fprintf(fp, "%8.3f, %4f, %i, %4.1f, %3f, %4.1f, %4.1f, %5.1f, %5.1f, %f, %f, %f, %f, %f, %f, %f, %f, %4.1f, %f, %4.1f, %f, %f, %f, %f, %f, %f, %f, %i, \n", Time, SecL, RPM, MAP, TP, vBatt, AFR, MAT, CLT, Engine, Gego, Gair, Gwarmup, Gbaro, Gammae, TPSacc, PW, DutyCycle,  PW2, DutyCycle2, SparkAdv, ColdAdv, Dwell, tpsDOT, mapDOT, IAC, deltaT, Ethanol);
00447             fclose(fp);
00448         } else {
00449             myled1 = 0;
00450         }
00451 */
00452     }
00453 }
00454 
00455 //exit main loop