rusEFI
The most advanced open source ECU
Public Member Functions | Private Attributes
MapAverager Class Reference

#include <map_averaging.h>

Inheritance diagram for MapAverager:
Inheritance graph
[legend]
Collaboration diagram for MapAverager:
Collaboration graph
[legend]

Public Member Functions

 MapAverager (SensorType type, efidur_t timeout)
 
void start ()
 
void stop ()
 
SensorResult submit (float sensorVolts)
 
void setFunction (SensorConverter &func)
 
void showInfo (const char *sensorName) const override
 
- Public Member Functions inherited from StoredValueSensor
SensorResult get () const final override
 
 StoredValueSensor (SensorType type, efidur_t timeoutNt)
 
void invalidate ()
 
void invalidate (UnexpectedCode why)
 
void setValidValue (float value, efitick_t timestamp)
 
void showInfo (const char *sensorName) const override
 
virtual void setTimeout (int timeoutMs)
 
- Public Member Functions inherited from Sensor
bool Register ()
 
const char * getSensorName () const
 
virtual bool hasSensor () const
 
virtual float getRaw () const
 
virtual bool isRedundant () const
 
void unregister ()
 
SensorType type () const
 

Private Attributes

SensorConverterm_function = nullptr
 
bool m_isAveraging = false
 
size_t m_counter = 0
 
size_t m_lastCounter = 0
 
float m_sum = 0
 

Additional Inherited Members

- Static Public Member Functions inherited from Sensor
static void showAllSensorInfo ()
 
static void showInfo (SensorType type)
 
static void resetRegistry ()
 
static const SensorgetSensorOfType (SensorType type)
 
static SensorResult get (SensorType type)
 
static float getOrZero (SensorType type)
 
static float getRaw (SensorType type)
 
static bool isRedundant (SensorType type)
 
static bool hasSensor (SensorType type)
 
static void setMockValue (SensorType type, float value, bool mockRedundant=false)
 
static void setInvalidMockValue (SensorType type)
 
static void setMockValue (int type, float value)
 
static void resetMockValue (SensorType type)
 
static void resetAllMocks ()
 
static void inhibitTimeouts (bool inhibit)
 
static const char * getSensorName (SensorType type)
 
- Protected Member Functions inherited from Sensor
 Sensor (SensorType type)
 
- Static Protected Attributes inherited from Sensor
static bool s_inhibitSensorTimeouts = false
 

Detailed Description

Definition at line 33 of file map_averaging.h.

Constructor & Destructor Documentation

◆ MapAverager()

MapAverager::MapAverager ( SensorType  type,
efidur_t  timeout 
)
inline

Definition at line 35 of file map_averaging.h.

36  : StoredValueSensor(type, timeout)
37  {
38  }
SensorType type() const
Definition: sensor.h:176
StoredValueSensor(SensorType type, efidur_t timeoutNt)

Member Function Documentation

◆ setFunction()

void MapAverager::setFunction ( SensorConverter func)
inline

Definition at line 45 of file map_averaging.h.

45  {
46  m_function = &func;
47  }
SensorConverter * m_function
Definition: map_averaging.h:52

Referenced by initMap().

Here is the caller graph for this function:

◆ showInfo()

void MapAverager::showInfo ( const char *  sensorName) const
overridevirtual

Implements Sensor.

Definition at line 76 of file sensor_info_printing.cpp.

76  {
77  const auto value = get();
78  efiPrintf("Sensor \"%s\" is MAP averager: valid: %s value: %.2f averaged sample count: %d", sensorName, boolToString(value.Valid), value.Value, m_lastCounter);
79 }
size_t m_lastCounter
Definition: map_averaging.h:56
SensorResult get() const final override
const char * boolToString(bool value)
Definition: efilib.cpp:18
Here is the call graph for this function:

◆ start()

void MapAverager::start ( )

Definition at line 79 of file map_averaging.cpp.

79  {
80  chibios_rt::CriticalSectionLocker csl;
81 
82  m_counter = 0;
83  m_sum = 0;
84  m_isAveraging = true;
85 }
bool m_isAveraging
Definition: map_averaging.h:54
size_t m_counter
Definition: map_averaging.h:55

◆ stop()

void MapAverager::stop ( )

Definition at line 100 of file map_averaging.cpp.

100  {
101  chibios_rt::CriticalSectionLocker csl;
102 
103  m_isAveraging = false;
104 
105  if (m_counter > 0) {
106  float averageMap = m_sum / m_counter;
108 
109  // TODO: this should be per-sensor, not one for all MAP sensors
111  // increment circular running buffer index
113  // find min. value (only works for pressure values, not raw voltages!)
114  float minPressure = averagedMapRunningBuffer[0];
115  for (int i = 1; i < mapMinBufferLength; i++) {
116  if (averagedMapRunningBuffer[i] < minPressure)
117  minPressure = averagedMapRunningBuffer[i];
118  }
119 
120  setValidValue(minPressure, getTimeNowNt());
121  } else {
122 #if EFI_PROD_CODE
123  warning(ObdCode::CUSTOM_UNEXPECTED_MAP_VALUE, "No MAP values to average");
124 #endif
125  }
126 }
void setValidValue(float value, efitick_t timestamp)
efitick_t getTimeNowNt()
Definition: efitime.cpp:19
bool warning(ObdCode code, const char *fmt,...)
static float averagedMapRunningBuffer[MAX_MAP_BUFFER_LENGTH]
static int averagedMapBufIdx
int mapMinBufferLength
@ CUSTOM_UNEXPECTED_MAP_VALUE

Referenced by endAveraging().

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

◆ submit()

SensorResult MapAverager::submit ( float  sensorVolts)

Definition at line 87 of file map_averaging.cpp.

87  {
88  auto result = m_function ? m_function->convert(volts) : unexpected;
89 
90  if (m_isAveraging && result) {
91  chibios_rt::CriticalSectionLocker csl;
92 
93  m_counter++;
94  m_sum += result.Value;
95  }
96 
97  return result;
98 }
virtual SensorResult convert(float raw) const =0

Referenced by mapAveragingAdcCallback().

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

Field Documentation

◆ m_counter

size_t MapAverager::m_counter = 0
private

Definition at line 55 of file map_averaging.h.

Referenced by start(), stop(), and submit().

◆ m_function

SensorConverter* MapAverager::m_function = nullptr
private

Definition at line 52 of file map_averaging.h.

Referenced by setFunction(), and submit().

◆ m_isAveraging

bool MapAverager::m_isAveraging = false
private

Definition at line 54 of file map_averaging.h.

Referenced by start(), stop(), and submit().

◆ m_lastCounter

size_t MapAverager::m_lastCounter = 0
private

Definition at line 56 of file map_averaging.h.

Referenced by showInfo(), and stop().

◆ m_sum

float MapAverager::m_sum = 0
private

Definition at line 57 of file map_averaging.h.

Referenced by start(), stop(), and submit().


The documentation for this class was generated from the following files: