FreeEMS  0.2.0-SNAPSHOT-285-g028e24c
coreVarsGenerator.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 and average the core variables.
32  *
33  * This file contains the function that transfers the raw ADC values to actual
34  * physical measurements and averages them.
35  */
36 
37 
38 #define COREVARSGENERATOR_C
39 #include "inc/freeEMS.h"
40 #include "inc/commsCore.h"
41 #include "inc/coreVarsGenerator.h"
42 #include "inc/decoderInterface.h"
43 
44 
45 /**
46  * Calculate and obtain the core variables. Each raw ADC value is converted to a
47  * usable measurement via a variety of methods. They are then stored in a struct
48  * and used as input to the next phase.
49  */
51  // Battery Reference Voltage
52  unsigned short localBRV;
58  localBRV = (ADCBuffers->BRV * 14) + VOLTS(7.2); // 0 ADC = 7.2V, 1023 ADC = 21.522C
59  }else{ // Default to normal alternator charging voltage 14.4V
60  localBRV = VOLTS(14.4);
61  }
62 
63  // Coolant/Head Temperature
64  unsigned short localCHT;
66  localCHT = CHTTransferTable[ADCBuffers->CHT];
70  localCHT = (ADCBuffers->CHT * 10) + DEGREES_C(0); // 0 ADC = 0C, 1023 ADC = 102.3C
71  }else{ // Default to slightly cold and therefore rich: 65C
72  localCHT = DEGREES_C(65);
73  }
74 
75  // Inlet Air Temperature
76  unsigned short localIAT;
78  localIAT = IATTransferTable[ADCBuffers->IAT];
82  localIAT = (ADCBuffers->IAT * 10) + DEGREES_C(0); // 0 ADC = 0C, 1023 ADC = 102.3C
83  }else{ // Default to room temperature
84  localIAT = DEGREES_C(20);
85  }
86 
87  // Throttle Position Sensor
88  /* Bound the TPS ADC reading and shift it to start at zero */
89  unsigned short unboundedTPSADC = ADCBuffers->TPS;
90  unsigned short boundedTPSADC;
92  if(unboundedTPSADC > fixedConfigs2.sensorRanges.TPSMaximumADC){
93  boundedTPSADC = TPSADCRange;
94  }else if(unboundedTPSADC > fixedConfigs2.sensorRanges.TPSMinimumADC){
95  boundedTPSADC = unboundedTPSADC - fixedConfigs2.sensorRanges.TPSMinimumADC;
96  } else{
97  boundedTPSADC = 0;
98  }
99  }else{ // Reverse slope!
100  if(unboundedTPSADC > fixedConfigs2.sensorRanges.TPSMinimumADC){
101  boundedTPSADC = 0;
102  }else if(unboundedTPSADC > fixedConfigs2.sensorRanges.TPSMaximumADC){
103  boundedTPSADC = fixedConfigs2.sensorRanges.TPSMinimumADC - unboundedTPSADC;
104  }else{
105  boundedTPSADC = TPSADCRange;
106  }
107  }
108 
109  /* Get TPS from ADC no need to add TPS min as we know it is zero by definition */
110  unsigned short localTPS = ((unsigned long)boundedTPSADC * PERCENT(100)) / TPSADCRange;
111  // TODO fail safe mode, only if on the ADC rails AND configured to do so
112  // Default to a low value that will get you home if you are in Alpha-N mode
113 
114  /* Get RPM by locking out ISRs for a second and grabbing the Tooth logging data */
115  //atomic start
116  // copy rpm data
117  //atomic end
118 
119  // Calculate RPM and delta RPM and delta delta RPM from data recorded
120  if(*ticksPerDegree != 0){
121  CoreVars->RPM = (unsigned short)(degreeTicksPerMinute / *ticksPerDegree);
122  }else{
123  CoreVars->RPM = 0;
124  }
125 
127 // unsigned short localDRPM = 0;
128 // unsigned short localDDRPM = 0;
129 
130 
131  // TODO This might get done somewhere else, separation of concerns, etc
132  /*&&&&&&&&&&&&&&&&&&&&&&&&&&&& Average the variables as per the configuration &&&&&&&&&&&&&&&&&&&&&&&&&&*/
133  /* Strictly speaking only the primary variables need to be averaged. After that, the derived ones are */
134  /* already averaged in a way. However, there may be some advantage to some short term averaging on the */
135  /* derived ones also, so it is something to look into later. */
136 
137  /// @todo TODO average the generated values here
138 
139 // newVal var word ' the value from the ADC
140 // smoothed var word ' a nicely smoothed result
141 //
142 // if newval > smoothed then
143 // smoothed = smoothed + (newval - smoothed)/alpha
144 // else
145 // smoothed = smoothed - (smoothed - newval)/alpha
146 // endif
147 
148  // from : http://www.tigoe.net/pcomp/code/category/code/arduinowiring/41
149 
150  // for now just copy them in.
151  CoreVars->BRV = localBRV;
152  CoreVars->CHT = localCHT;
153  CoreVars->IAT = localIAT;
154  CoreVars->TPS = localTPS;
159 
160 
161  // Not actually used, feed raw values for now TODO migrate these to a SpecialVars struct or similar not included in default datalog
165 
166 // CoreVars->DRPM = localDRPM;
167 // CoreVars->DDRPM = localDDRPM;
168 // CoreVars->DTPS = localDTPS;
169 }