2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file table_helper.h
|
|
|
|
*
|
|
|
|
* @date Jul 6, 2014
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
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
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
// popular left edge of CLT-based correction curves
|
2019-05-25 10:36:29 -07:00
|
|
|
#define CLT_CURVE_RANGE_FROM -40
|
|
|
|
|
2019-07-12 05:31:38 -07:00
|
|
|
class ValueProvider3D {
|
|
|
|
public:
|
|
|
|
virtual float getValue(float xRpm, float y) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-18 09:03:32 -08:00
|
|
|
/**
|
|
|
|
* this helper class brings together 3D table with two 2D axis curves
|
|
|
|
*/
|
2019-06-10 09:38:32 -07:00
|
|
|
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE, typename vType, typename kType>
|
2019-07-12 05:31:38 -07:00
|
|
|
class Map3D : public ValueProvider3D {
|
2015-07-10 06:01:56 -07:00
|
|
|
public:
|
2019-06-08 06:51:36 -07:00
|
|
|
explicit Map3D(const char*name);
|
2016-06-30 19:02:49 -07:00
|
|
|
Map3D(const char*name, float multiplier);
|
2019-06-10 09:38:32 -07:00
|
|
|
void init(vType table[RPM_BIN_SIZE][LOAD_BIN_SIZE], const kType loadBins[LOAD_BIN_SIZE], const kType rpmBins[RPM_BIN_SIZE]);
|
2016-03-11 12:01:58 -08:00
|
|
|
float getValue(float xRpm, float y);
|
2016-06-29 19:03:26 -07:00
|
|
|
void setAll(vType value);
|
|
|
|
vType *pointers[LOAD_BIN_SIZE];
|
2016-08-26 16:02:56 -07:00
|
|
|
private:
|
2019-05-27 08:43:34 -07:00
|
|
|
void create(const char*name, float multiplier);
|
2019-06-10 09:38:32 -07:00
|
|
|
const kType *loadBins = NULL;
|
|
|
|
const kType *rpmBins = NULL;
|
2019-05-27 08:43:34 -07:00
|
|
|
bool initialized = false;
|
2015-12-27 09:01:53 -08:00
|
|
|
const char *name;
|
2016-06-30 19:02:49 -07:00
|
|
|
float multiplier;
|
2015-07-10 06:01:56 -07:00
|
|
|
};
|
|
|
|
|
2019-06-10 08:17:36 -07:00
|
|
|
/*
|
2019-06-10 09:38:32 -07:00
|
|
|
* this dead code is a questionable performance optimization idea: instead of division every time
|
|
|
|
* we want interpolation for a curve we can pre-calculate A and B and save the division at the cost of more RAM usage
|
|
|
|
* Realistically we probably value RAM over CPU at this time and the costs are not justified.
|
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) {
|
2018-01-23 09:05:14 -08:00
|
|
|
warning(CUSTOM_INTEPOLATE_ERROR_4, "preCalc: Same x1 and x2 in interpolate: %.2f/%.2f", x1, x2);
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float y1 = values[i];
|
|
|
|
float y2 = values[i + 1];
|
|
|
|
|
|
|
|
aTable[i] = INTERPOLATION_A(x1, y1, x2, y2);
|
|
|
|
bTable[i] = y1 - aTable[i] * x1;
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 08:17:36 -07:00
|
|
|
*/
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE, typename vType, typename kType>
|
|
|
|
void Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE, vType, kType>::init(vType table[RPM_BIN_SIZE][LOAD_BIN_SIZE],
|
|
|
|
const kType loadBins[LOAD_BIN_SIZE],
|
|
|
|
const kType 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;
|
|
|
|
}
|
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE, typename vType, typename kType>
|
|
|
|
float Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE, vType, kType>::getValue(float xRpm, float y) {
|
2018-07-25 20:30:00 -07:00
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, initialized, "map not initialized", NAN);
|
2016-03-11 12:01:58 -08:00
|
|
|
if (cisnan(y)) {
|
2017-04-19 19:03:14 -07:00
|
|
|
warning(CUSTOM_PARAM_RANGE, "%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
|
2019-06-10 09:38:32 -07:00
|
|
|
return multiplier * interpolate3d<vType, kType>(y, loadBins, LOAD_BIN_SIZE, xRpm, rpmBins, RPM_BIN_SIZE, pointers);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE, typename vType, typename kType>
|
|
|
|
Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE, vType, kType>::Map3D(const char *name) {
|
2016-06-30 19:02:49 -07:00
|
|
|
create(name, 1);
|
|
|
|
}
|
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE, typename vType, typename kType>
|
|
|
|
Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE, vType, kType>::Map3D(const char *name, float multiplier) {
|
2016-06-30 19:02:49 -07:00
|
|
|
create(name, multiplier);
|
|
|
|
}
|
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE, typename vType, typename kType>
|
|
|
|
void Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE, vType, kType>::create(const char *name, float multiplier) {
|
2015-12-27 09:01:53 -08:00
|
|
|
this->name = name;
|
2016-06-30 19:02:49 -07:00
|
|
|
this->multiplier = multiplier;
|
2015-07-10 06:01:56 -07:00
|
|
|
memset(&pointers, 0, sizeof(pointers));
|
|
|
|
}
|
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE, typename vType, typename kType>
|
|
|
|
void Map3D<RPM_BIN_SIZE, LOAD_BIN_SIZE, vType, kType>::setAll(vType value) {
|
2018-07-25 20:03:04 -07:00
|
|
|
efiAssertVoid(CUSTOM_ERR_6573, 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++) {
|
2016-06-30 19:02:49 -07:00
|
|
|
pointers[l][r] = value / multiplier;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
template<int RPM_BIN_SIZE, int LOAD_BIN_SIZE, typename vType, typename kType>
|
2016-07-02 15:02:12 -07:00
|
|
|
void copy2DTable(const vType source[LOAD_BIN_SIZE][RPM_BIN_SIZE], vType destination[LOAD_BIN_SIZE][RPM_BIN_SIZE]) {
|
|
|
|
for (int k = 0; k < LOAD_BIN_SIZE; k++) {
|
|
|
|
for (int rpmIndex = 0; rpmIndex < RPM_BIN_SIZE; rpmIndex++) {
|
|
|
|
destination[k][rpmIndex] = source[k][rpmIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 20:01:22 -07:00
|
|
|
/**
|
|
|
|
* AFR value is packed into uint8_t with a multiplier of 10
|
|
|
|
*/
|
|
|
|
#define AFR_STORAGE_MULT 10
|
2018-09-26 01:13:16 -07:00
|
|
|
/**
|
|
|
|
* TPS-based Advance value is packed into int16_t with a multiplier of 100
|
|
|
|
*/
|
|
|
|
#define ADVANCE_TPS_STORAGE_MULT 100
|
2016-07-01 20:01:22 -07:00
|
|
|
|
2019-06-10 09:38:32 -07:00
|
|
|
typedef Map3D<FUEL_RPM_COUNT, FUEL_LOAD_COUNT, uint8_t, float> afr_Map3D_t;
|
|
|
|
typedef Map3D<IGN_RPM_COUNT, IGN_LOAD_COUNT, float, float> ign_Map3D_t;
|
|
|
|
typedef Map3D<IGN_RPM_COUNT, IGN_TPS_COUNT, int16_t, float> ign_tps_Map3D_t;
|
|
|
|
typedef Map3D<FUEL_RPM_COUNT, FUEL_LOAD_COUNT, float, float> fuel_Map3D_t;
|
|
|
|
typedef Map3D<BARO_CORR_SIZE, BARO_CORR_SIZE, float, float> baroCorr_Map3D_t;
|
2019-06-15 11:11:38 -07:00
|
|
|
typedef Map3D<PEDAL_TO_TPS_SIZE, PEDAL_TO_TPS_SIZE, uint8_t, uint8_t> pedal2tps_t;
|
2019-12-03 21:37:32 -08:00
|
|
|
typedef Map3D<IAC_PID_MULT_SIZE, IAC_PID_MULT_SIZE, uint8_t, uint8_t> iacPidMultiplier_t;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-01-21 18:03:08 -08:00
|
|
|
void setRpmBin(float array[], int size, float idleRpm, float topRpm);
|
|
|
|
|
2019-06-10 20:27:54 -07:00
|
|
|
/**
|
2019-11-22 20:27:24 -08:00
|
|
|
* @param precision for example '0.1' for one digit fractional part. Default to 0.01, two digits.
|
2019-06-10 20:27:54 -07:00
|
|
|
*/
|
2019-11-22 20:27:24 -08:00
|
|
|
template<typename TValue, int TSize>
|
|
|
|
void setLinearCurve(TValue (&array)[TSize], float from, float to, float precision = 0.01f) {
|
|
|
|
for (int i = 0; i < TSize; i++) {
|
|
|
|
float value = interpolateMsg("setLinearCurve", 0, from, TSize - 1, to, i);
|
|
|
|
|
2019-06-10 20:27:54 -07:00
|
|
|
/**
|
|
|
|
* rounded values look nicer, also we want to avoid precision mismatch with Tuner Studio
|
|
|
|
*/
|
|
|
|
array[i] = efiRound(value, precision);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 20:27:24 -08:00
|
|
|
template<typename TValue, int TSize>
|
|
|
|
void setArrayValues(TValue (&array)[TSize], TValue value) {
|
|
|
|
for (int i = 0; i < TSize; i++) {
|
|
|
|
array[i] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void setRpmTableBin(float array[], int size);
|
|
|
|
|
|
|
|
#endif /* TABLE_HELPER_H_ */
|