fome-fw/firmware/controllers/core/table_helper.h

126 lines
3.3 KiB
C
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file table_helper.h
*
* @date Jul 6, 2014
2015-12-31 13:02:30 -08:00
* @author Andrey Belomutskiy, (c) 2012-2016
2015-07-10 06:01:56 -07:00
*/
#ifndef TABLE_HELPER_H_
#define TABLE_HELPER_H_
#include <math.h>
#include "error_handling.h"
#include "interpolation.h"
2015-12-27 09:01:53 -08:00
#include "efilib.h"
2015-07-10 06:01:56 -07:00
2016-01-18 09:03:32 -08:00
/**
* this helper class brings together 3D table with two 2D axis curves
*/
2015-07-10 06:01:56 -07:00
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE>
class Map3D {
public:
2015-12-27 09:01:53 -08:00
Map3D(const char*name);
2015-07-10 06:01:56 -07:00
void init(float table[RPM_BIN_SIZE][LOAD_BIN_SIZE], float loadBins[LOAD_BIN_SIZE], float rpmBins[RPM_BIN_SIZE]);
2016-03-11 12:01:58 -08:00
float getValue(float xRpm, float y);
2015-07-10 06:01:56 -07:00
void setAll(float value);
private:
float *pointers[LOAD_BIN_SIZE];
float *loadBins;
float *rpmBins;
2016-01-18 09:03:32 -08:00
bool initialized;
2015-12-27 09:01:53 -08:00
const char *name;
2015-07-10 06:01:56 -07:00
};
template<int SIZE>
class Table2D {
public:
Table2D();
void preCalc(float *bin, float *values);
float aTable[SIZE];
float bTable[SIZE];
float *bin;
};
template<int SIZE>
Table2D<SIZE>::Table2D() {
bin = NULL;
}
template<int SIZE>
void Table2D<SIZE>::preCalc(float *bin, float *values) {
this->bin = bin;
for (int i = 0; i < SIZE - 1; i++) {
float x1 = bin[i];
float x2 = bin[i + 1];
if (x1 == x2) {
warning(OBD_PCM_Processor_Fault, "preCalc: Same x1 and x2 in interpolate: %f/%f", x1, x2);
return;
}
float y1 = values[i];
float y2 = values[i + 1];
aTable[i] = INTERPOLATION_A(x1, y1, x2, y2);
bTable[i] = y1 - aTable[i] * x1;
}
}
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE>
void Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE>::init(float table[RPM_BIN_SIZE][LOAD_BIN_SIZE],
float loadBins[LOAD_BIN_SIZE],
float rpmBins[RPM_BIN_SIZE]) {
2016-04-15 20:01:40 -07:00
// this method cannot use logger because it's invoked before everything
// that's because this method needs to be invoked before initial configuration processing
// and initial configuration load is done prior to logging initialization
2015-07-10 06:01:56 -07:00
for (int k = 0; k < LOAD_BIN_SIZE; k++) {
pointers[k] = table[k];
}
2016-01-18 09:03:32 -08:00
initialized = true;
2015-07-10 06:01:56 -07:00
this->loadBins = loadBins;
this->rpmBins = rpmBins;
}
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE>
2016-03-11 12:01:58 -08:00
float Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE>::getValue(float xRpm, float y) {
2016-01-18 09:03:32 -08:00
efiAssert(initialized, "map not initialized", NAN);
2016-03-11 12:01:58 -08:00
if (cisnan(y)) {
warning(OBD_PCM_Processor_Fault, "%s: y is NaN", name);
2015-12-27 09:01:53 -08:00
return NAN;
}
2016-03-11 12:01:58 -08:00
// todo: we have a bit of a mess: in TunerStudio, RPM is X-axis
return interpolate3d(y, loadBins, LOAD_BIN_SIZE, xRpm, rpmBins, RPM_BIN_SIZE, pointers);
2015-07-10 06:01:56 -07:00
}
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE>
2015-12-27 09:01:53 -08:00
Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE>::Map3D(const char *name) {
this->name = name;
2016-04-15 20:01:40 -07:00
initialized = false;
2015-07-10 06:01:56 -07:00
memset(&pointers, 0, sizeof(pointers));
loadBins = NULL;
rpmBins = NULL;
}
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE>
void Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE>::setAll(float value) {
2016-01-18 09:03:32 -08:00
efiAssertVoid(initialized, "map not initialized");
2015-07-10 06:01:56 -07:00
for (int l = 0; l < LOAD_BIN_SIZE; l++) {
for (int r = 0; r < RPM_BIN_SIZE; r++) {
pointers[l][r] = value;
}
}
}
typedef Map3D<IGN_RPM_COUNT, IGN_LOAD_COUNT> ign_Map3D_t;
typedef Map3D<FUEL_RPM_COUNT, FUEL_LOAD_COUNT> fuel_Map3D_t;
typedef Map3D<BARO_CORR_SIZE, BARO_CORR_SIZE> baroCorr_Map3D_t;
2016-01-21 18:03:08 -08:00
void setRpmBin(float array[], int size, float idleRpm, float topRpm);
2015-07-10 06:01:56 -07:00
void setTableBin(float array[], int size, float l, float r);
void setTableBin2(float array[], int size, float l, float r, float precision);
void setRpmTableBin(float array[], int size);
#endif /* TABLE_HELPER_H_ */