rusEFI
The most advanced open source ECU
Functions | Variables
map_averaging.cpp File Reference

Detailed Description

In order to have best MAP estimate possible, we real MAP value at a relatively high frequency and average the value within a specified angle position window for each cylinder

Date
Dec 11, 2013
Author
Andrey Belomutskiy, (c) 2012-2020

This file is part of rusEfi - see http://rusefi.com

rusEfi is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file map_averaging.cpp.

Functions

 fail ("EFI_SHAFT_POSITION_INPUT required to have EFI_EMULATE_POSITION_SENSORS") static NamedOutputPin mapAveragingPin("map")
 
static void endAveraging (MapAverager *arg)
 
static void startAveraging (scheduling_s *endAveragingScheduling)
 
void mapAveragingAdcCallback (float instantVoltage)
 
static void applyMapMinBufferLength ()
 
void postMapState (TunerStudioOutputChannels *tsOutputChannels)
 
void refreshMapAveragingPreCalc ()
 
void mapAveragingTriggerCallback (uint32_t index, efitick_t edgeTimestamp)
 
void initMapAveraging ()
 

Variables

static float averagedMapRunningBuffer [MAX_MAP_BUFFER_LENGTH]
 
int mapMinBufferLength = 0
 
static int averagedMapBufIdx = 0
 
static scheduling_s startTimers [MAX_CYLINDER_COUNT][2]
 
static scheduling_s endTimers [MAX_CYLINDER_COUNT][2]
 
static size_t currentMapAverager = 0
 

Function Documentation

◆ applyMapMinBufferLength()

static void applyMapMinBufferLength ( )
static

Definition at line 160 of file map_averaging.cpp.

160  {
161  // check range
162  mapMinBufferLength = maxI(minI(engineConfiguration->mapMinBufferLength, MAX_MAP_BUFFER_LENGTH), 1);
163  // reset index
164  averagedMapBufIdx = 0;
165  // fill with maximum values
166  for (int i = 0; i < mapMinBufferLength; i++) {
167  averagedMapRunningBuffer[i] = FLT_MAX;
168  }
169 }
static float averagedMapRunningBuffer[MAX_MAP_BUFFER_LENGTH]
static int averagedMapBufIdx
int mapMinBufferLength
engine_configuration_s * engineConfiguration

Referenced by initMapAveraging(), and mapAveragingTriggerCallback().

Here is the caller graph for this function:

◆ endAveraging()

static void endAveraging ( MapAverager arg)
static

Definition at line 153 of file map_averaging.cpp.

153  {
154  arg->stop();
155 
156  mapAveragingPin.setLow();
157 }
Here is the call graph for this function:

◆ fail()

fail ( "EFI_SHAFT_POSITION_INPUT required to have EFI_EMULATE_POSITION_SENSORS"  )

this instance does not have a real physical pin - it's only used for engine sniffer

todo: we can kind of add real physical pin just for a very narrow case of troubleshooting but only if we ever need it :)

Engine idles around 20Hz and revs up to 140Hz, at 60/2 and 8 cylinders we have about 20Khz events If we can read buffer at 50Hz we want buffer to be about 400 elements.

Definition at line 34 of file trigger_emulator_algo.cpp.

41  {
42 }

Referenced by Engine::periodicSlowCallback().

Here is the caller graph for this function:

◆ initMapAveraging()

void initMapAveraging ( )

Definition at line 268 of file map_averaging.cpp.

268  {
270 }
static void applyMapMinBufferLength()

Referenced by commonInitEngineController().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mapAveragingAdcCallback()

void mapAveragingAdcCallback ( float  instantVoltage)

This method is invoked from ADC callback.

Note
This method is invoked OFTEN, this method is a potential bottleneck - the implementation should be as fast as possible

Definition at line 135 of file map_averaging.cpp.

135  {
136  efiAssertVoid(ObdCode::CUSTOM_ERR_6650, hasLotsOfRemainingStack(), "lowstck#9a");
137 
138  SensorResult mapResult = getMapAvg(currentMapAverager).submit(instantVoltage);
139 
140  if (!mapResult) {
141  // hopefully this warning is not too much CPU consumption for fast ADC callback
142  warning(ObdCode::CUSTOM_INSTANT_MAP_DECODING, "Invalid MAP at %f", instantVoltage);
143  }
144 
145  float instantMap = mapResult.value_or(0);
146 #if EFI_TUNER_STUDIO
147  engine->outputChannels.instantMAPValue = instantMap;
148 #endif // EFI_TUNER_STUDIO
149 }
TunerStudioOutputChannels outputChannels
Definition: engine.h:99
SensorResult submit(float sensorVolts)
Engine * engine
bool warning(ObdCode code, const char *fmt,...)
static size_t currentMapAverager
MapAverager & getMapAvg(size_t idx)
Definition: init_map.cpp:26
@ CUSTOM_ERR_6650
@ CUSTOM_INSTANT_MAP_DECODING
expected< float > SensorResult
Definition: sensor.h:55
scaled_channel< uint16_t, 30, 1 > instantMAPValue

Referenced by onFastAdcComplete().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mapAveragingTriggerCallback()

void mapAveragingTriggerCallback ( uint32_t  index,
efitick_t  edgeTimestamp 
)

Shaft Position callback used to schedule start and end of MAP averaging

Definition at line 211 of file map_averaging.cpp.

212  {
213 #if EFI_ENGINE_CONTROL && EFI_PROD_CODE
214  // this callback is invoked on interrupt thread
215  if (index != (uint32_t)engineConfiguration->mapAveragingSchedulingAtIndex)
216  return;
217 
219  if (!isValidRpm(rpm)) {
220  return;
221  }
222 
224 
227  }
228 
229  // todo: this could be pre-calculated
231 
232  for (int i = 0; i < samplingCount; i++) {
233  angle_t samplingStart = engine->engineState.mapAveragingStart[i];
234 
235  angle_t samplingDuration = engine->engineState.mapAveragingDuration;
236  // todo: this assertion could be moved out of trigger handler
237  assertAngleRange(samplingDuration, "samplingDuration", ObdCode::CUSTOM_ERR_6563);
238  if (samplingDuration <= 0) {
239  warning(ObdCode::CUSTOM_MAP_ANGLE_PARAM, "map sampling angle should be positive");
240  return;
241  }
242 
243  angle_t samplingEnd = samplingStart + samplingDuration;
244 
245  if (cisnan(samplingEnd)) {
246  // todo: when would this happen?
247  warning(ObdCode::CUSTOM_ERR_6549, "no map angles");
248  return;
249  }
250 
251  // todo: pre-calculate samplingEnd for each cylinder
252  wrapAngle(samplingEnd, "samplingEnd", ObdCode::CUSTOM_ERR_6563);
253  // only if value is already prepared
254  int structIndex = getRevolutionCounter() % 2;
255 
256  scheduling_s *startTimer = &startTimers[i][structIndex];
257  scheduling_s *endTimer = &endTimers[i][structIndex];
258 
259  // at the moment we schedule based on time prediction based on current RPM and angle
260  // we are loosing precision in case of changing RPM - the further away is the event the worse is precision
261  // todo: schedule this based on closest trigger event, same as ignition works
262  scheduleByAngle(startTimer, edgeTimestamp, samplingStart,
263  { startAveraging, endTimer });
264  }
265 #endif // EFI_ENGINE_CONTROL && EFI_PROD_CODE
266 }
EngineState engineState
Definition: engine.h:315
angle_t mapAveragingStart[MAX_CYLINDER_COUNT]
Definition: engine_state.h:52
angle_t mapAveragingDuration
Definition: engine_state.h:53
static float getOrZero(SensorType type)
Definition: sensor.h:92
static scheduling_s startTimers[MAX_CYLINDER_COUNT][2]
static scheduling_s endTimers[MAX_CYLINDER_COUNT][2]
static void startAveraging(scheduling_s *endAveragingScheduling)
@ CUSTOM_ERR_6549
@ CUSTOM_MAP_ANGLE_PARAM
@ CUSTOM_ERR_6563
@ MapAveragingTriggerCallback
efitick_t scheduleByAngle(scheduling_s *timer, efitick_t nowNt, angle_t angle, action_s action)
float angle_t
Definition: rusefi_types.h:58
void wrapAngle(angle_t &angle, const char *msg, ObdCode code)

Referenced by TriggerCentral::handleShaftSignal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ postMapState()

void postMapState ( TunerStudioOutputChannels tsOutputChannels)

Definition at line 172 of file map_averaging.cpp.

172  {
174 }

Referenced by updateTunerStudioState().

Here is the caller graph for this function:

◆ refreshMapAveragingPreCalc()

void refreshMapAveragingPreCalc ( )

Definition at line 177 of file map_averaging.cpp.

177  {
179  if (isValidRpm(rpm)) {
181  angle_t start = interpolate2d(rpm, c->samplingAngleBins, c->samplingAngle);
182  efiAssertVoid(ObdCode::CUSTOM_ERR_MAP_START_ASSERT, !cisnan(start), "start");
183 
185  efiAssertVoid(ObdCode::CUSTOM_ERR_MAP_AVG_OFFSET, !cisnan(offsetAngle), "offsetAngle");
186 
187  for (size_t i = 0; i < engineConfiguration->cylindersCount; i++) {
188  // todo: potential micro-optimization to reuse getEngineState()->engineCycle?
189  angle_t cylinderOffset = getEngineCycle(getEngineRotationState()->getOperationMode()) * i / engineConfiguration->cylindersCount;
190  efiAssertVoid(ObdCode::CUSTOM_ERR_MAP_CYL_OFFSET, !cisnan(cylinderOffset), "cylinderOffset");
191  // part of this formula related to specific cylinder offset is never changing - we can
192  // move the loop into start-up calculation and not have this loop as part of periodic calculation
193  // todo: change the logic as described above in order to reduce periodic CPU usage?
194  float cylinderStart = start + cylinderOffset - offsetAngle + tdcPosition();
195  wrapAngle(cylinderStart, "cylinderStart", ObdCode::CUSTOM_ERR_6562);
196  engine->engineState.mapAveragingStart[i] = cylinderStart;
197  }
199  } else {
200  for (size_t i = 0; i < engineConfiguration->cylindersCount; i++) {
202  }
204  }
205 
206 }
TriggerCentral triggerCentral
Definition: engine.h:286
TriggerFormDetails triggerFormDetails
angle_t eventAngles[2 *PWM_PHASE_MAX_COUNT]
EngineRotationState * getEngineRotationState()
Definition: engine.cpp:572
@ CUSTOM_ERR_MAP_AVG_OFFSET
@ CUSTOM_ERR_6562
@ CUSTOM_ERR_MAP_CYL_OFFSET
@ CUSTOM_ERR_MAP_START_ASSERT
angle_t getEngineCycle(operation_mode_e operationMode)

Referenced by Engine::periodicFastCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ startAveraging()

static void startAveraging ( scheduling_s endAveragingScheduling)
static

Definition at line 65 of file map_averaging.cpp.

65  {
66  efiAssertVoid(ObdCode::CUSTOM_ERR_6649, hasLotsOfRemainingStack(), "lowstck#9");
67 
68  // TODO: set currentMapAverager based on cylinder bank
69  auto& averager = getMapAvg(currentMapAverager);
70  averager.start();
71 
72  mapAveragingPin.setHigh();
73 
75  { endAveraging, &averager });
76 }
efitick_t getTimeNowNt()
Definition: efitime.cpp:19
@ CUSTOM_ERR_6649

Referenced by mapAveragingTriggerCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ averagedMapBufIdx

int averagedMapBufIdx = 0
static

Definition at line 52 of file map_averaging.cpp.

Referenced by applyMapMinBufferLength(), and MapAverager::stop().

◆ averagedMapRunningBuffer

float averagedMapRunningBuffer[MAX_MAP_BUFFER_LENGTH]
static

Definition at line 50 of file map_averaging.cpp.

Referenced by applyMapMinBufferLength(), and MapAverager::stop().

◆ currentMapAverager

size_t currentMapAverager = 0
static

Definition at line 63 of file map_averaging.cpp.

Referenced by mapAveragingAdcCallback(), and startAveraging().

◆ endTimers

scheduling_s endTimers[MAX_CYLINDER_COUNT][2]
static

Definition at line 58 of file map_averaging.cpp.

Referenced by mapAveragingTriggerCallback().

◆ mapMinBufferLength

int mapMinBufferLength = 0

◆ startTimers

scheduling_s startTimers[MAX_CYLINDER_COUNT][2]
static

here we have averaging start and averaging end points for each cylinder

Definition at line 57 of file map_averaging.cpp.

Referenced by mapAveragingTriggerCallback().

Go to the source code of this file.