rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions
efi_interpolation.h File Reference

Detailed Description

See also libfirmware interpolation.h

Date
Oct 17, 2013
Author
Andrey Belomutskiy, (c) 2012-2020

Definition in file efi_interpolation.h.

Functions

float interpolateClampedWithValidation (float x1, float y1, float x2, float y2, float x)
 
float interpolateClamped (float x1, float y1, float x2, float y2, float x)
 
float interpolateMsg (const char *msg, float x1, float y1, float x2, float y2, float x)
 Linear interpolation by two points.
 
template<typename TValue , int TSize>
void ensureArrayIsAscending (const char *msg, const TValue(&values)[TSize])
 
template<typename TValue , int TSize>
void ensureArrayIsAscendingOrDefault (const char *msg, const TValue(&values)[TSize])
 
template<typename kType >
int findIndexMsg (const char *msg, const kType array[], int size, kType value)
 Binary search.
 
template<typename VType , typename kType >
void setCurveValue (const kType bins[], VType values[], int size, float key, float value)
 

Function Documentation

◆ ensureArrayIsAscending()

template<typename TValue , int TSize>
void ensureArrayIsAscending ( const char msg,
const TValue(&)  values[TSize] 
)

Definition at line 32 of file efi_interpolation.h.

32 {
33 for (size_t i = 0; i < TSize - 1; i++) {
34 float cur = values[i];
35 float next = values[i + 1];
36 if (next <= cur) {
37 firmwareError(ObdCode::CUSTOM_ERR_AXIS_ORDER, "Invalid table axis (must be ascending!): %s %f should be below %f at %d", msg, cur, next, i);
38 }
39 }
40}
void firmwareError(ObdCode code, const char *fmt,...)
@ CUSTOM_ERR_AXIS_ORDER

Referenced by ensureArrayIsAscendingOrDefault(), and validateConfigOnStartUpOrBurn().

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

◆ ensureArrayIsAscendingOrDefault()

template<typename TValue , int TSize>
void ensureArrayIsAscendingOrDefault ( const char msg,
const TValue(&)  values[TSize] 
)

Definition at line 43 of file efi_interpolation.h.

43 {
44 if (values[1] == 0) {
45 return; // looks like default empty array, do not check
46 }
47 ensureArrayIsAscending(msg, values);
48}
void ensureArrayIsAscending(const char *msg, const TValue(&values)[TSize])

Referenced by validateConfigOnStartUpOrBurn().

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

◆ findIndexMsg()

template<typename kType >
int findIndexMsg ( const char msg,
const kType  array[],
int  size,
kType  value 
)

Binary search.

Returns
the highest index within sorted array such that array[i] is greater than or equal to the parameter
Note
If the parameter is smaller than the first element of the array, -1 is returned.

See also ensureArrayIsAscending

Definition at line 57 of file efi_interpolation.h.

57 {
58 float fvalue = (float)value;
59 if (std::isnan(fvalue)) {
60 firmwareError(ObdCode::ERROR_NAN_FIND_INDEX, "NaN in findIndex%s", msg);
61 return 0;
62 }
63
64 if (value < array[0])
65 return -1;
66 int middle;
67
68 int left = 0;
69 int right = size;
70
71 // todo: extract binary search as template method?
72 while (true) {
73#if 0
74 // that's an assertion to make sure we do not loop here
75 size--;
76 efiAssert(ObdCode::CUSTOM_ERR_ASSERT, size > 0, "Unexpected state in binary search", 0);
77#endif
78
79 // todo: compare current implementation with
80 // http://eigenjoy.com/2011/01/21/worlds-fastest-binary-search/
81 // ?
82 middle = (left + right) / 2;
83
84// print("left=%d middle=%d right=%d: %.2f\r\n", left, middle, right, array[middle]);
85
86 if (middle == left)
87 break;
88
89 if (middle != 0 && array[middle - 1] > array[middle]) {
90#if EFI_UNIT_TEST
91 firmwareError(ObdCode::CUSTOM_ERR_6610, "%s: out of order %.2f %.2f", msg, array[middle - 1], array[middle]);
92#else
93 warning(ObdCode::CUSTOM_ERR_OUT_OF_ORDER, "%s: out of order %.2f %.2f", msg, array[middle - 1], array[middle]);
94
95#endif /* EFI_UNIT_TEST */
96 }
97
98 if (value < array[middle]) {
99 right = middle;
100 } else if (value > array[middle]) {
101 left = middle;
102 } else {
103 break;
104 }
105 }
106
107 return middle;
108}
bool warning(ObdCode code, const char *fmt,...)
@ ERROR_NAN_FIND_INDEX
@ CUSTOM_ERR_6610
@ CUSTOM_ERR_ASSERT
@ CUSTOM_ERR_OUT_OF_ORDER
composite packet size

Referenced by setCurveValue().

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

◆ interpolateClamped()

float interpolateClamped ( float  x1,
float  y1,
float  x2,
float  y2,
float  x 
)

todo: use 'interpolateClampedWithValidation' wider?

See also
interpolateMsg

Definition at line 69 of file interpolation.cpp.

69 {
70 // note how we assume order of x1 and x2 here! see also interpolateClampedWithValidation
71 if (x <= x1)
72 return y1;
73 if (x >= x2)
74 return y2;
75
76 // todo: do we care with code duplication with interpolateMsg above?
77 float a = INTERPOLATION_A(x1, y1, x2, y2);
78 float b = y1 - a * x1;
79 return a * x + b;
80}

Referenced by LaunchControlBase::calculateSparkSkipRatio(), ThrottleModelBase::estimateThrottleFlow(), flexCallback(), IdleController::getClosedLoop(), getCrankingAdvance(), getCrankingFuel3(), IdleController::getIdleTimingAdjustment(), IdleController::getOpenLoop(), getRunningAdvance(), IdleController::getRunningOpenLoop(), EtbController::getSetpointEtb(), FuelComputer::getStoichiometricRatio(), IdleController::getTargetRpm(), IFuelComputer::getTCharge(), IFuelComputer::getTChargeCoefficient(), DfcoController::getTimingRetard(), AirmassVeModelBase::getVe(), interpolateClampedWithValidation(), HellaOilLevelSensor::onEdge(), setHysteresis(), LimpManager::updateRevLimit(), and updateVrThresholdPwm().

Here is the caller graph for this function:

◆ interpolateClampedWithValidation()

float interpolateClampedWithValidation ( float  x1,
float  y1,
float  x2,
float  y2,
float  x 
)

Definition at line 58 of file interpolation.cpp.

58 {
59 if (x1 >= x2) {
60 criticalError("interpolateClamped %f has to be smaller than %f", x1, x2);
61 }
62 return interpolateClamped(x1, y1, x2, y2, x);
63}
float interpolateClamped(float x1, float y1, float x2, float y2, float x)
Here is the call graph for this function:

◆ interpolateMsg()

float interpolateMsg ( const char msg,
float  x1,
float  y1,
float  x2,
float  y2,
float  x 
)

Linear interpolation by two points.

Parameters
x1key of the first point
y1value of the first point
x2key of the second point
y2value of the second point
Xkey to be interpolated
Note
For example, "interpolateMsg("", engineConfiguration.tpsMin, 0, engineConfiguration.tpsMax, 100, adc);"
See also
interpolateClamped

we could end up here for example while resetting bins while changing engine type

Definition at line 28 of file interpolation.cpp.

28 {
29 if (std::isnan(x1) || std::isnan(x2) || std::isnan(y1) || std::isnan(y2)) {
30 warning(ObdCode::CUSTOM_ERR_INTERPOLATE_1, "interpolate%s: why param", msg);
31 return NAN;
32 }
33 if (std::isnan(x)) {
34 warning(ObdCode::CUSTOM_ERR_INTERPOLATE_2, "interpolate%s: why X", msg);
35 return NAN;
36 }
37 // todo: double comparison using EPS
38 if (x1 == x2) {
39 /**
40 * we could end up here for example while resetting bins while changing engine type
41 */
42 warning(ObdCode::CUSTOM_ERR_INTERPOLATE_3, "interpolate%s: Same x1 and x2 in interpolate: %.2f/%.2f", msg, x1, x2);
43 return NAN;
44 }
45
46 // a*x1 + b = y1
47 // a*x2 + b = y2
48// efiAssertVoid(ObdCode::CUSTOM_ERR_ASSERT_VOID, x1 != x2, "no way we can interpolate");
49 float a = INTERPOLATION_A(x1, y1, x2, y2);
50 if (std::isnan(a)) {
51 warning(ObdCode::CUSTOM_ERR_INTERPOLATE_4, "interpolate%s: why a", msg);
52 return NAN;
53 }
54 float b = y1 - a * x1;
55 return a * x + b;
56}
@ CUSTOM_ERR_INTERPOLATE_3
@ CUSTOM_ERR_INTERPOLATE_4
@ CUSTOM_ERR_INTERPOLATE_2
@ CUSTOM_ERR_INTERPOLATE_1

Referenced by decodeTpsSentValue(), getAdvanceForRpm(), getAfr(), IFuelComputer::getTChargeCoefficient(), lua_interpolate(), and setLinearCurve().

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

◆ setCurveValue()

template<typename VType , typename kType >
void setCurveValue ( const kType  bins[],
VType  values[],
int  size,
float  key,
float  value 
)

Sets specified value for specified key in a correction curve see also setLinearCurve()

Definition at line 115 of file efi_interpolation.h.

115 {
116 int index = findIndexMsg("tbVl", bins, size, key);
117 if (index == -1)
118 index = 0;
119 values[index] = value;
120}
int findIndexMsg(const char *msg, const kType array[], int size, kType value)
Binary search.
Here is the call graph for this function:

Go to the source code of this file.