Tiny fix found while looking for mistakes that might have been causing Mike trouble...
[freeems-vanilla.git] / src / derivedVarsGenerator.c
blob3228c55490ca8bddd145afd351fb2a4b43bbaba8
1 /* FreeEMS - the open source engine management system
3 * Copyright 2008-2012 Fred Cooke
5 * This file is part of the FreeEMS project.
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.
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.
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/
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!
23 * Thank you for choosing FreeEMS to run your engine!
27 /** @file
29 * @ingroup measurementsAndCalculations
31 * @brief Generate the derived variables.
33 * Second level variables are derived from the core variables and generated here.
35 * @author Fred Cooke
39 #define DERIVEDVARSGENERATOR_C
40 #include "inc/freeEMS.h"
41 #include "inc/commsCore.h"
42 #include "inc/tableLookup.h"
43 #include "inc/derivedVarsGenerator.h"
44 #include "inc/locationIDs.h"
45 #include "inc/decoderInterface.h"
48 /** @brief Generate the derived variables.
50 * This function uses the core variables to lookup and calculate further second
51 * order variables such as load, VE, Lamdda, Transient fuel correction, engine
52 * temperature enrichment, Injector dead time, etc.
54 * @author Fred Cooke
56 void generateDerivedVars(){
57 /*&&&&&&&&&&&&&&&&&&&& Use basic variables to lookup and calculate derived variables &&&&&&&&&&&&&&&&&&&*/
60 /* Determine load based on options */
61 if(TRUE){ /* Use MAP as load */
62 DerivedVars->LoadMain = CoreVars->MAP;
63 }else if(FALSE){ /* Use TPS as load */
64 DerivedVars->LoadMain = CoreVars->TPS;
65 }else if(FALSE){ /* Use AAP corrected MAP as load */
66 DerivedVars->LoadMain = ((unsigned long)CoreVars->MAP * CoreVars->AAP) / KPA(100);
67 }else{ /* Default to MAP, but throw error */
68 DerivedVars->LoadMain = CoreVars->MAP;
69 /* If anyone is listening, let them know something is wrong */
70 sendErrorIfClear(LOAD_NOT_CONFIGURED_CODE); // or maybe queue it?
74 /* Look up VE with RPM and Load */
75 DerivedVars->VEMain = lookupMainTable(CoreVars->RPM, DerivedVars->LoadMain, VETableMainLocationID);
78 /* Look up target Lambda with RPM and Load */
79 DerivedVars->Lambda = lookupMainTable(CoreVars->RPM, DerivedVars->LoadMain, LambdaTableLocationID);
82 /* Look up injector dead time with battery voltage */
83 DerivedVars->IDT = lookupTwoDTableUS((twoDTableUS*)&TablesA.SmallTablesA.injectorDeadTimeTable, CoreVars->BRV);
85 // temp dwell and advance vars...
86 DerivedVars->Dwell = lookupTwoDTableUS((twoDTableUS*)&TablesA.SmallTablesA.dwellDesiredVersusVoltageTable, CoreVars->BRV);
87 unsigned long tempAdvance = ANGLE_FACTOR * (unsigned long)lookupMainTable(CoreVars->RPM, DerivedVars->LoadMain, IgnitionAdvanceTableMainLocationID);
88 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
89 // Move this magic number to an appropriate place and/or refactor timing calcs/values/etc
91 /// @todo TODO make generic!!!!
92 // to go generic we need:
93 // angle between ignition events (if have tpd) (or total angle and number of events)
94 // max % dwell
95 // minimum spark time
96 // a setting to choose which behaviour (don't limit/% dwell limit/min spark time/other?)
97 #ifdef HOTEL
98 /// @bug hack for hyundai! 135 = 3/4 of 180 = one cycle...
99 unsigned long threeQuartersOfAvailableTime = ((unsigned long)CoreVars->DRPM * 135 * ANGLE_FACTOR) / ticks_per_degree_multiplier;
100 if(DerivedVars->Dwell > threeQuartersOfAvailableTime){
101 DerivedVars->Dwell = threeQuartersOfAvailableTime;
103 #endif
105 /* Look up the engine temperature enrichment percentage with temperature */
106 DerivedVars->ETE = lookupTwoDTableUS((twoDTableUS*)&TablesA.SmallTablesA.engineTempEnrichmentTablePercent, CoreVars->CHT);
107 /* TODO The above needs some careful thought put into it around different loads and correction effects. */
110 /* Calculate the Transient Fuel Correction */
111 if(TRUE /*WWTFC*/){ /* Do ONLY WW correction if enabled */
112 // Do ww stuff, maybe pre done via RTC/RTI for consistent period?
113 DerivedVars->TFCTotal = 0; /* TODO replace with real code */
114 }else if(FALSE /*STDTFC*/){ /* Do any combination of standard approximate methods */
115 /* Initialse the variable as a base */
116 DerivedVars->TFCTotal = 0;
117 /* Based on the rate of change of MAP and some history/taper time */
118 if(FALSE /*MAPTFC*/){
119 // Do MAP based
120 DerivedVars->TFCTotal += 0;
123 /* Based on the rate of change of TPS and some history/taper time */
124 if(FALSE /*TPSTFC*/){
125 // Do TPS based
126 DerivedVars->TFCTotal += 0;
129 /* Based on the rate of change of RPM and some history/taper time */
130 if(FALSE /*RPMTFC*/){
131 // Do RPM based
132 DerivedVars->TFCTotal += 0;
134 }else{ /* Default to no correction */
135 DerivedVars->TFCTotal = 0;
136 /* Don't throw error as correction may not be required */
139 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/