FreeEMS  0.2.0-SNAPSHOT-285-g028e24c
derivedVarsGenerator.c
Go to the documentation of this file.
1 /* FreeEMS - the open source engine management system
2  *
3  * Copyright 2008-2013 Fred Cooke
4  *
5  * This file is part of the FreeEMS project.
6  *
7  * FreeEMS software is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * FreeEMS software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with any FreeEMS software. If not, see http://www.gnu.org/licenses/
19  *
20  * We ask that if you make any changes to this file you email them upstream to
21  * us at admin(at)diyefi(dot)org or, even better, fork the code on github.com!
22  *
23  * Thank you for choosing FreeEMS to run your engine!
24  */
25 
26 
27 /** @file
28  *
29  * @ingroup measurementsAndCalculations
30  *
31  * @brief Generate the derived variables.
32  *
33  * Second level variables are derived from the core variables and generated here.
34  */
35 
36 
37 #define DERIVEDVARSGENERATOR_C
38 #include "inc/freeEMS.h"
39 #include "inc/commsCore.h"
40 #include "inc/tableLookup.h"
42 #include "inc/locationIDs.h"
43 #include "inc/decoderInterface.h"
44 
45 
46 /**
47  * Use core variables to lookup and calculate the derived variables. This
48  * function uses the core variables to lookup and calculate further second
49  * order variables such as load, VE, Lamdda, Transient fuel correction, engine
50  * temperature enrichment, Injector dead time, etc.
51  */
53  /* Determine load based on options */
54  if(!(fixedConfigs2.algorithmSettings.loadType)){ /* Use MAP as load */
56  }else if(fixedConfigs2.algorithmSettings.loadType == LOAD_TPS){ /* Use TPS as load */
58  }else if(fixedConfigs2.algorithmSettings.loadType == LOAD_AAP){ /* Use AAP corrected MAP as load */
59  DerivedVars->LoadMain = ((unsigned long)CoreVars->MAP * CoreVars->AAP) / KPA(100);
60  // TODO add maf calc load option here
61  }else{ /* Default to MAP, but throw error */
63  }
64 
65 
66  /* Look up target Lambda with RPM and Load */
68 
69 
70  /* Look up injector dead time with battery voltage */
71  DerivedVars->IDT = lookupTwoDTableUS((twoDTableUS*)&TablesA.SmallTablesA.injectorDeadTimeTable, CoreVars->BRV);
72 
74  DerivedVars->Dwell = lookupTwoDTableUS((twoDTableUS*)&TablesA.SmallTablesA.dwellDesiredVersusVoltageTable, CoreVars->BRV);
76  DerivedVars->Dwell = lookupTwoDTableUS((twoDTableUS*)&TablesA.SmallTablesA.dwellVersusRPMTable, CoreVars->RPM);
79  }else{
80  DerivedVars->Dwell = 0;
81  }
82 
83  unsigned long tempAdvance = ANGLE_FACTOR * (unsigned long)lookupMainTable(CoreVars->RPM, DerivedVars->LoadMain, IgnitionAdvanceTableMainLocationID);
84  DerivedVars->Advance = (unsigned short)(tempAdvance / 1024); // This calculation will change when the timing tables get shrunk to a more reasonable 8 bit size with appropriate scaling
85  // Move this magic number to an appropriate place and/or refactor timing calcs/values/etc
86 
87 /// @todo TODO make generic!!!!
88 // to go generic we need:
89 // angle between ignition events (if have tpd) (or total angle and number of events)
90 // max % dwell
91 // minimum spark time
92 // a setting to choose which behaviour (don't limit/% dwell limit/min spark time/other?)
93 #if CONFIG == HOTEL_ID
94  /// @bug hack for hyundai! 135 = 3/4 of 180 = one cycle...
95  unsigned long threeQuartersOfAvailableTime = ((unsigned long)CoreVars->DRPM * 135 * ANGLE_FACTOR) / ticks_per_degree_multiplier;
96  if(DerivedVars->Dwell > threeQuartersOfAvailableTime){
97  DerivedVars->Dwell = threeQuartersOfAvailableTime;
98  }
99 #endif
100 
101  /* Look up the engine temperature enrichment percentage with temperature */
102  DerivedVars->ETE = lookupTwoDTableUS((twoDTableUS*)&TablesA.SmallTablesA.engineTempEnrichmentTablePercent, CoreVars->CHT);
103  /* TODO The above needs some careful thought put into it around different loads and correction effects. */
104 
105 
106  /* Calculate the Transient Fuel Correction */
107  if(TRUE /*WWTFC*/){ /* Do ONLY WW correction if enabled */
108  // Do ww stuff, maybe pre done via RTC/RTI for consistent period?
109  DerivedVars->TFCTotal = 0; /* TODO replace with real code */
110  }else if(FALSE /*STDTFC*/){ /* Do any combination of standard approximate methods */
111  /* Initialse the variable as a base */
112  DerivedVars->TFCTotal = 0;
113  /* Based on the rate of change of MAP and some history/taper time */
114  if(FALSE /*MAPTFC*/){
115  // Do MAP based
116  DerivedVars->TFCTotal += 0;
117  }
118 
119  /* Based on the rate of change of TPS and some history/taper time */
120  if(FALSE /*TPSTFC*/){
121  // Do TPS based
122  DerivedVars->TFCTotal += 0;
123  }
124 
125  /* Based on the rate of change of RPM and some history/taper time */
126  if(FALSE /*RPMTFC*/){
127  // Do RPM based
128  DerivedVars->TFCTotal += 0;
129  }
130  }else{ /* Default to no correction */
131  DerivedVars->TFCTotal = 0;
132  /* Don't throw error as correction may not be required */
133  }
134 }