2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file engine_math.cpp
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @date Jul 13, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "engine_math.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "interpolation.h"
|
|
|
|
#include "allsensors.h"
|
2020-04-04 05:41:09 -07:00
|
|
|
#include "sensor.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "event_registry.h"
|
2019-03-29 06:11:13 -07:00
|
|
|
#include "efi_gpio.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "fuel_math.h"
|
2016-01-08 12:01:38 -08:00
|
|
|
#include "advance_map.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-03-29 16:06:03 -07:00
|
|
|
EXTERN_ENGINE;
|
2019-08-24 22:15:18 -07:00
|
|
|
#if EFI_UNIT_TEST
|
|
|
|
extern bool verboseMode;
|
|
|
|
#endif /* EFI_UNIT_TEST */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-08-07 21:19:09 -07:00
|
|
|
floatms_t getEngineCycleDuration(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
return getCrankshaftRevolutionTimeMs(rpm) * (engine->getOperationMode(PASS_ENGINE_PARAMETER_SIGNATURE) == TWO_STROKE ? 1 : 2);
|
2017-03-08 21:51:27 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2016-01-08 12:01:38 -08:00
|
|
|
* @return number of milliseconds in one crank shaft revolution
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2015-09-07 06:03:24 -07:00
|
|
|
floatms_t getCrankshaftRevolutionTimeMs(int rpm) {
|
2016-08-30 19:02:21 -07:00
|
|
|
if (rpm == 0) {
|
|
|
|
return NAN;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
return 360 * getOneDegreeTimeMs(rpm);
|
|
|
|
}
|
|
|
|
|
2020-07-05 13:25:19 -07:00
|
|
|
float getFuelingLoad(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
return ENGINE(engineState.fuelingLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
float getIgnitionLoad(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
return ENGINE(engineState.ignitionLoad);
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @brief Returns engine load according to selected engine_load_mode
|
|
|
|
*
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
float getEngineLoadT(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2018-07-25 20:30:00 -07:00
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, engine!=NULL, "engine 2NULL", NAN);
|
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, engineConfiguration!=NULL, "engineConfiguration 2NULL", NAN);
|
2016-08-28 13:02:34 -07:00
|
|
|
switch (engineConfiguration->fuelAlgorithm) {
|
2015-07-10 06:01:56 -07:00
|
|
|
case LM_SPEED_DENSITY:
|
2021-01-02 16:13:10 -08:00
|
|
|
return Sensor::get(SensorType::Map).value_or(0);
|
2020-11-27 12:44:31 -08:00
|
|
|
case LM_ALPHA_N:
|
2020-04-04 05:41:09 -07:00
|
|
|
return Sensor::get(SensorType::Tps1).value_or(0);
|
2020-05-23 07:46:28 -07:00
|
|
|
case LM_REAL_MAF:
|
2017-05-15 20:28:49 -07:00
|
|
|
return getRealMaf(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2015-07-10 06:01:56 -07:00
|
|
|
default:
|
2020-05-23 07:46:28 -07:00
|
|
|
firmwareError(CUSTOM_UNKNOWN_ALGORITHM, "Unexpected engine load parameter: %d", engineConfiguration->fuelAlgorithm);
|
2020-07-23 02:46:45 -07:00
|
|
|
return 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 20:17:44 -08:00
|
|
|
/**
|
|
|
|
* see also setConstantDwell
|
|
|
|
*/
|
|
|
|
void setSingleCoilDwell(DECLARE_CONFIG_PARAMETER_SIGNATURE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
for (int i = 0; i < DWELL_CURVE_SIZE; i++) {
|
2017-02-18 12:01:47 -08:00
|
|
|
engineConfiguration->sparkDwellRpmBins[i] = i + 1;
|
|
|
|
engineConfiguration->sparkDwellValues[i] = 4;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-02-18 12:01:47 -08:00
|
|
|
engineConfiguration->sparkDwellRpmBins[5] = 10;
|
|
|
|
engineConfiguration->sparkDwellValues[5] = 4;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-18 12:01:47 -08:00
|
|
|
engineConfiguration->sparkDwellRpmBins[6] = 4500;
|
|
|
|
engineConfiguration->sparkDwellValues[6] = 4;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-18 12:01:47 -08:00
|
|
|
engineConfiguration->sparkDwellRpmBins[7] = 12500;
|
|
|
|
engineConfiguration->sparkDwellValues[7] = 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-07-06 08:19:02 -07:00
|
|
|
static floatms_t getCrankingSparkDwell(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2016-11-11 16:02:29 -08:00
|
|
|
if (engineConfiguration->useConstantDwellDuringCranking) {
|
|
|
|
return engineConfiguration->ignitionDwellForCrankingMs;
|
|
|
|
} else {
|
|
|
|
// technically this could be implemented via interpolate2d
|
|
|
|
float angle = engineConfiguration->crankingChargeAngle;
|
2020-09-03 16:29:15 -07:00
|
|
|
return getOneDegreeTimeMs(GET_RPM()) * angle;
|
2016-11-11 16:02:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2017-04-18 05:16:53 -07:00
|
|
|
* @return Spark dwell time, in milliseconds. 0 if tables are not ready.
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
floatms_t getSparkDwell(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
|
2016-11-30 12:02:43 -08:00
|
|
|
float dwellMs;
|
2020-09-05 15:49:42 -07:00
|
|
|
if (ENGINE(rpmCalculator).isCranking()) {
|
2017-07-06 08:19:02 -07:00
|
|
|
dwellMs = getCrankingSparkDwell(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-11-30 12:02:43 -08:00
|
|
|
} else {
|
2018-07-25 20:30:00 -07:00
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(rpm), "invalid rpm", NAN);
|
2016-11-30 12:02:43 -08:00
|
|
|
|
2021-02-16 06:32:16 -08:00
|
|
|
dwellMs = interpolate2d(rpm, engineConfiguration->sparkDwellRpmBins, engineConfiguration->sparkDwellValues);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-04-18 05:16:53 -07:00
|
|
|
if (cisnan(dwellMs) || dwellMs <= 0) {
|
|
|
|
// this could happen during engine configuration reset
|
2018-01-23 09:05:14 -08:00
|
|
|
warning(CUSTOM_ERR_DWELL_DURATION, "invalid dwell: %.2f at rpm=%d", dwellMs, rpm);
|
2017-04-18 05:16:53 -07:00
|
|
|
return 0;
|
2016-11-30 12:02:43 -08:00
|
|
|
}
|
|
|
|
return dwellMs;
|
2019-01-31 14:55:23 -08:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-05 05:06:18 -08:00
|
|
|
static const int order_1_2[] = {1, 2};
|
2017-02-07 22:03:36 -08:00
|
|
|
|
2019-01-05 05:06:18 -08:00
|
|
|
static const int order_1_2_3[] = {1, 2, 3};
|
2020-06-26 06:07:43 -07:00
|
|
|
static const int order_1_3_2[] = {1, 3, 2};
|
2017-02-07 22:03:36 -08:00
|
|
|
// 4 cylinder
|
|
|
|
|
2019-01-05 05:06:18 -08:00
|
|
|
static const int order_1_THEN_3_THEN_4_THEN2[] = { 1, 3, 4, 2 };
|
|
|
|
static const int order_1_THEN_2_THEN_4_THEN3[] = { 1, 2, 4, 3 };
|
|
|
|
static const int order_1_THEN_3_THEN_2_THEN4[] = { 1, 3, 2, 4 };
|
2019-08-15 16:36:16 -07:00
|
|
|
static const int order_1_THEN_4_THEN_3_THEN2[] = { 1, 4, 3, 2 };
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-07 22:03:36 -08:00
|
|
|
// 5 cylinder
|
2019-01-05 05:06:18 -08:00
|
|
|
static const int order_1_2_4_5_3[] = {1, 2, 4, 5, 3};
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-07 22:03:36 -08:00
|
|
|
// 6 cylinder
|
2019-01-05 05:06:18 -08:00
|
|
|
static const int order_1_THEN_5_THEN_3_THEN_6_THEN_2_THEN_4[] = { 1, 5, 3, 6, 2, 4 };
|
|
|
|
static const int order_1_THEN_4_THEN_2_THEN_5_THEN_3_THEN_6[] = { 1, 4, 2, 5, 3, 6 };
|
|
|
|
static const int order_1_THEN_2_THEN_3_THEN_4_THEN_5_THEN_6[] = { 1, 2, 3, 4, 5, 6 };
|
|
|
|
static const int order_1_6_3_2_5_4[] = {1, 6, 3, 2, 5, 4};
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-07 22:03:36 -08:00
|
|
|
// 8 cylinder
|
2019-01-05 05:06:18 -08:00
|
|
|
static const int order_1_8_4_3_6_5_7_2[] = { 1, 8, 4, 3, 6, 5, 7, 2 };
|
|
|
|
static const int order_1_8_7_2_6_5_4_3[] = { 1, 8, 7, 2, 6, 5, 4, 3 };
|
|
|
|
static const int order_1_5_4_2_6_3_7_8[] = { 1, 5, 4, 2, 6, 3, 7, 8 };
|
2019-09-28 07:25:57 -07:00
|
|
|
static const int order_1_2_7_8_4_5_6_3[] = { 1, 2, 7, 8, 4, 5, 6, 3 };
|
2020-04-29 13:46:59 -07:00
|
|
|
static const int order_1_3_7_2_6_5_4_8[] = { 1, 3, 7, 2, 6, 5, 4, 8 };
|
2021-03-05 16:28:17 -08:00
|
|
|
static const int order_1_2_3_4_5_6_7_8[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
|
|
|
|
static const int order_1_5_4_8_6_3_7_2[] = { 1, 5, 4, 8, 6, 3, 7, 2 };
|
2020-04-29 13:46:59 -07:00
|
|
|
|
|
|
|
// 9 cylinder
|
|
|
|
static const int order_1_2_3_4_5_6_7_8_9[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
2016-05-22 10:07:12 -07:00
|
|
|
|
2017-02-07 22:03:36 -08:00
|
|
|
// 10 cylinder
|
2019-01-05 05:06:18 -08:00
|
|
|
static const int order_1_10_9_4_3_6_5_8_7_2[] = {1, 10, 9, 4, 3, 6, 5, 8, 7, 2};
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-07 22:03:36 -08:00
|
|
|
// 12 cyliner
|
2019-01-05 05:06:18 -08:00
|
|
|
static const int order_1_7_5_11_3_9_6_12_2_8_4_10[] = {1, 7, 5, 11, 3, 9, 6, 12, 2, 8, 4, 10};
|
|
|
|
static const int order_1_7_4_10_2_8_6_12_3_9_5_11[] = {1, 7, 4, 10, 2, 8, 6, 12, 3, 9, 5, 11};
|
2019-09-14 11:47:22 -07:00
|
|
|
static const int order_1_12_5_8_3_10_6_7_2_11_4_9[] = {1, 12, 5, 8, 3, 10, 6, 7, 2, 11, 4, 9};
|
2020-05-21 17:44:14 -07:00
|
|
|
static const int order_1_2_3_4_5_6_7_8_9_10_11_12[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
|
2015-09-10 16:01:32 -07:00
|
|
|
|
2020-05-21 15:28:35 -07:00
|
|
|
// no comments
|
|
|
|
static const int order_1_14_9_4_7_12_15_6_13_8_3_16_11_2_5_10[] = {1, 14, 9, 4, 7, 12, 15, 6, 13, 8, 3, 16, 11, 2, 5, 10};
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
static int getFiringOrderLength(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-03-23 19:01:10 -07:00
|
|
|
|
|
|
|
switch (CONFIG(specs.firingOrder)) {
|
|
|
|
case FO_1:
|
|
|
|
return 1;
|
|
|
|
// 2 cylinder
|
|
|
|
case FO_1_2:
|
|
|
|
return 2;
|
|
|
|
// 3 cylinder
|
|
|
|
case FO_1_2_3:
|
2020-06-26 06:07:43 -07:00
|
|
|
case FO_1_3_2:
|
2017-03-23 19:01:10 -07:00
|
|
|
return 3;
|
|
|
|
// 4 cylinder
|
|
|
|
case FO_1_3_4_2:
|
|
|
|
case FO_1_2_4_3:
|
|
|
|
case FO_1_3_2_4:
|
2019-08-15 16:36:16 -07:00
|
|
|
case FO_1_4_3_2:
|
2017-03-23 19:01:10 -07:00
|
|
|
return 4;
|
|
|
|
// 5 cylinder
|
|
|
|
case FO_1_2_4_5_3:
|
|
|
|
return 5;
|
|
|
|
|
|
|
|
// 6 cylinder
|
|
|
|
case FO_1_5_3_6_2_4:
|
|
|
|
case FO_1_4_2_5_3_6:
|
|
|
|
case FO_1_2_3_4_5_6:
|
|
|
|
case FO_1_6_3_2_5_4:
|
|
|
|
return 6;
|
|
|
|
|
|
|
|
// 8 cylinder
|
|
|
|
case FO_1_8_4_3_6_5_7_2:
|
|
|
|
case FO_1_8_7_2_6_5_4_3:
|
|
|
|
case FO_1_5_4_2_6_3_7_8:
|
2019-09-28 07:25:57 -07:00
|
|
|
case FO_1_2_7_8_4_5_6_3:
|
2020-04-29 13:46:59 -07:00
|
|
|
case FO_1_3_7_2_6_5_4_8:
|
2021-03-05 16:28:17 -08:00
|
|
|
case FO_1_2_3_4_5_6_7_8:
|
|
|
|
case FO_1_5_4_8_6_3_7_2:
|
2017-03-23 19:01:10 -07:00
|
|
|
return 8;
|
|
|
|
|
2020-04-29 13:46:59 -07:00
|
|
|
// 9 cylinder radial
|
|
|
|
case FO_1_2_3_4_5_6_7_8_9:
|
|
|
|
return 9;
|
|
|
|
|
2017-03-23 19:01:10 -07:00
|
|
|
// 10 cylinder
|
|
|
|
case FO_1_10_9_4_3_6_5_8_7_2:
|
|
|
|
return 10;
|
|
|
|
|
|
|
|
// 12 cylinder
|
|
|
|
case FO_1_7_5_11_3_9_6_12_2_8_4_10:
|
2017-06-08 17:35:42 -07:00
|
|
|
case FO_1_7_4_10_2_8_6_12_3_9_5_11:
|
2019-09-14 11:47:22 -07:00
|
|
|
case FO_1_12_5_8_3_10_6_7_2_11_4_9:
|
2020-05-21 17:44:14 -07:00
|
|
|
case FO_1_2_3_4_5_6_7_8_9_10_11_12:
|
2017-03-23 19:01:10 -07:00
|
|
|
return 12;
|
|
|
|
|
2020-05-21 15:28:35 -07:00
|
|
|
case FO_1_14_9_4_7_12_15_6_13_8_3_16_11_2_5_10:
|
|
|
|
return 16;
|
|
|
|
|
2017-03-23 19:01:10 -07:00
|
|
|
default:
|
2020-05-23 07:46:28 -07:00
|
|
|
firmwareError(CUSTOM_OBD_UNKNOWN_FIRING_ORDER, "Invalid firing order: %d", CONFIG(specs.firingOrder));
|
2017-03-23 19:01:10 -07:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @param index from zero to cylindersCount - 1
|
|
|
|
* @return cylinderId from one to cylindersCount
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
int getCylinderId(int index DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2017-03-23 19:01:10 -07:00
|
|
|
|
2018-07-28 10:58:19 -07:00
|
|
|
const int firingOrderLength = getFiringOrderLength(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
|
2018-07-28 11:01:28 -07:00
|
|
|
if (firingOrderLength < 1 || firingOrderLength > INJECTION_PIN_COUNT) {
|
2018-07-28 10:58:19 -07:00
|
|
|
firmwareError(CUSTOM_ERR_6687, "fol %d", firingOrderLength);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (engineConfiguration->specs.cylindersCount != firingOrderLength) {
|
2020-05-23 15:11:04 -07:00
|
|
|
// May 2020 this somehow still happens with functional tests, maybe race condition?
|
|
|
|
warning(CUSTOM_OBD_WRONG_FIRING_ORDER, "Wrong cyl count for firing order, expected %d cylinders", firingOrderLength);
|
2018-07-28 10:58:19 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index < 0 || index >= firingOrderLength) {
|
2020-05-23 15:01:40 -07:00
|
|
|
// May 2020 this somehow still happens with functional tests, maybe race condition?
|
|
|
|
warning(CUSTOM_ERR_6686, "firing order index %d", index);
|
2017-03-23 19:01:10 -07:00
|
|
|
return 1;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-03-23 19:01:10 -07:00
|
|
|
switch (CONFIG(specs.firingOrder)) {
|
2016-07-20 16:04:27 -07:00
|
|
|
case FO_1:
|
2015-07-10 06:01:56 -07:00
|
|
|
return 1;
|
2015-09-10 16:01:32 -07:00
|
|
|
// 2 cylinder
|
2016-07-20 16:04:27 -07:00
|
|
|
case FO_1_2:
|
2015-07-10 06:01:56 -07:00
|
|
|
return order_1_2[index];
|
2015-09-10 16:01:32 -07:00
|
|
|
// 3 cylinder
|
|
|
|
case FO_1_2_3:
|
|
|
|
return order_1_2_3[index];
|
2020-06-26 06:07:43 -07:00
|
|
|
case FO_1_3_2:
|
|
|
|
return order_1_3_2[index];
|
2015-07-10 06:01:56 -07:00
|
|
|
// 4 cylinder
|
2016-07-20 16:04:27 -07:00
|
|
|
case FO_1_3_4_2:
|
2015-07-10 06:01:56 -07:00
|
|
|
return order_1_THEN_3_THEN_4_THEN2[index];
|
2016-07-20 16:04:27 -07:00
|
|
|
case FO_1_2_4_3:
|
2015-07-10 06:01:56 -07:00
|
|
|
return order_1_THEN_2_THEN_4_THEN3[index];
|
2016-07-20 16:04:27 -07:00
|
|
|
case FO_1_3_2_4:
|
2015-07-10 06:01:56 -07:00
|
|
|
return order_1_THEN_3_THEN_2_THEN4[index];
|
2019-08-15 16:36:16 -07:00
|
|
|
case FO_1_4_3_2:
|
|
|
|
return order_1_THEN_4_THEN_3_THEN2[index];
|
2015-07-10 06:01:56 -07:00
|
|
|
// 5 cylinder
|
|
|
|
case FO_1_2_4_5_3:
|
|
|
|
return order_1_2_4_5_3[index];
|
|
|
|
|
|
|
|
// 6 cylinder
|
2016-07-20 16:04:27 -07:00
|
|
|
case FO_1_5_3_6_2_4:
|
2015-07-10 06:01:56 -07:00
|
|
|
return order_1_THEN_5_THEN_3_THEN_6_THEN_2_THEN_4[index];
|
2016-07-20 16:04:27 -07:00
|
|
|
case FO_1_4_2_5_3_6:
|
2015-07-10 06:01:56 -07:00
|
|
|
return order_1_THEN_4_THEN_2_THEN_5_THEN_3_THEN_6[index];
|
2016-07-20 16:04:27 -07:00
|
|
|
case FO_1_2_3_4_5_6:
|
2015-09-05 20:02:46 -07:00
|
|
|
return order_1_THEN_2_THEN_3_THEN_4_THEN_5_THEN_6[index];
|
2017-01-12 21:03:30 -08:00
|
|
|
case FO_1_6_3_2_5_4:
|
|
|
|
return order_1_6_3_2_5_4[index];
|
2015-09-05 20:02:46 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
// 8 cylinder
|
|
|
|
case FO_1_8_4_3_6_5_7_2:
|
|
|
|
return order_1_8_4_3_6_5_7_2[index];
|
2016-05-22 10:07:12 -07:00
|
|
|
case FO_1_8_7_2_6_5_4_3:
|
|
|
|
return order_1_8_7_2_6_5_4_3[index];
|
2016-06-05 17:03:16 -07:00
|
|
|
case FO_1_5_4_2_6_3_7_8:
|
|
|
|
return order_1_5_4_2_6_3_7_8[index];
|
2019-09-28 07:25:57 -07:00
|
|
|
case FO_1_2_7_8_4_5_6_3:
|
|
|
|
return order_1_2_7_8_4_5_6_3[index];
|
2020-04-29 13:46:59 -07:00
|
|
|
case FO_1_3_7_2_6_5_4_8:
|
|
|
|
return order_1_3_7_2_6_5_4_8[index];
|
2021-03-05 16:28:17 -08:00
|
|
|
case FO_1_2_3_4_5_6_7_8:
|
|
|
|
return order_1_2_3_4_5_6_7_8[index];
|
|
|
|
case FO_1_5_4_8_6_3_7_2:
|
|
|
|
return order_1_5_4_8_6_3_7_2[index];
|
2020-04-29 13:46:59 -07:00
|
|
|
|
2021-03-05 16:28:17 -08:00
|
|
|
// 9 cylinder
|
2020-04-29 13:46:59 -07:00
|
|
|
case FO_1_2_3_4_5_6_7_8_9:
|
|
|
|
return order_1_2_3_4_5_6_7_8_9[index];
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-07 22:03:36 -08:00
|
|
|
// 10 cylinder
|
|
|
|
case FO_1_10_9_4_3_6_5_8_7_2:
|
|
|
|
return order_1_10_9_4_3_6_5_8_7_2[index];
|
|
|
|
|
|
|
|
// 12 cylinder
|
|
|
|
case FO_1_7_5_11_3_9_6_12_2_8_4_10:
|
|
|
|
return order_1_7_5_11_3_9_6_12_2_8_4_10[index];
|
2017-06-08 17:35:42 -07:00
|
|
|
case FO_1_7_4_10_2_8_6_12_3_9_5_11:
|
|
|
|
return order_1_7_4_10_2_8_6_12_3_9_5_11[index];
|
2019-09-14 11:47:22 -07:00
|
|
|
case FO_1_12_5_8_3_10_6_7_2_11_4_9:
|
|
|
|
return order_1_12_5_8_3_10_6_7_2_11_4_9[index];
|
2020-05-21 17:44:14 -07:00
|
|
|
case FO_1_2_3_4_5_6_7_8_9_10_11_12:
|
|
|
|
return order_1_2_3_4_5_6_7_8_9_10_11_12[index];
|
2017-02-07 22:03:36 -08:00
|
|
|
|
2020-05-21 15:28:35 -07:00
|
|
|
// do not ask
|
|
|
|
case FO_1_14_9_4_7_12_15_6_13_8_3_16_11_2_5_10:
|
|
|
|
return order_1_14_9_4_7_12_15_6_13_8_3_16_11_2_5_10[index];
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
default:
|
2020-05-23 07:46:28 -07:00
|
|
|
firmwareError(CUSTOM_OBD_UNKNOWN_FIRING_ORDER, "Invalid firing order: %d", CONFIG(specs.firingOrder));
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-26 18:53:22 -07:00
|
|
|
/**
|
2019-11-17 06:18:43 -08:00
|
|
|
* @param cylinderIndex from 0 to cylinderCount, not cylinder number
|
2019-10-26 18:53:22 -07:00
|
|
|
*/
|
|
|
|
static int getIgnitionPinForIndex(int cylinderIndex DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2019-01-20 08:33:40 -08:00
|
|
|
switch (getCurrentIgnitionMode(PASS_ENGINE_PARAMETER_SIGNATURE)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
case IM_ONE_COIL:
|
2016-01-16 14:02:38 -08:00
|
|
|
return 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
case IM_WASTED_SPARK: {
|
2019-10-10 04:07:59 -07:00
|
|
|
if (CONFIG(specs.cylindersCount) == 1) {
|
|
|
|
// we do not want to divide by zero
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-26 18:53:22 -07:00
|
|
|
return cylinderIndex % (CONFIG(specs.cylindersCount) / 2);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
case IM_INDIVIDUAL_COILS:
|
2019-10-26 18:53:22 -07:00
|
|
|
return cylinderIndex;
|
2019-10-27 13:19:59 -07:00
|
|
|
case IM_TWO_COILS:
|
|
|
|
return cylinderIndex % 2;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
default:
|
2020-05-23 07:46:28 -07:00
|
|
|
firmwareError(CUSTOM_OBD_IGNITION_MODE, "Invalid ignition mode getIgnitionPinForIndex(): %d", engineConfiguration->ignitionMode);
|
2016-01-16 14:02:38 -08:00
|
|
|
return 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 17:58:51 -08:00
|
|
|
void prepareIgnitionPinIndices(ignition_mode_e ignitionMode DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2021-01-11 05:49:20 -08:00
|
|
|
(void)ignitionMode;
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_ENGINE_CONTROL
|
2020-12-20 16:31:14 -08:00
|
|
|
for (int cylinderIndex = 0; cylinderIndex < CONFIG(specs.cylindersCount); cylinderIndex++) {
|
|
|
|
ENGINE(ignitionPin[cylinderIndex]) = getIgnitionPinForIndex(cylinderIndex PASS_ENGINE_PARAMETER_SUFFIX);
|
2018-03-10 17:58:51 -08:00
|
|
|
}
|
2020-12-20 16:31:14 -08:00
|
|
|
#endif /* EFI_ENGINE_CONTROL */
|
2018-03-10 17:58:51 -08:00
|
|
|
}
|
|
|
|
|
2019-01-20 08:33:40 -08:00
|
|
|
/**
|
|
|
|
* @return IM_WASTED_SPARK if in SPINNING mode and IM_INDIVIDUAL_COILS setting
|
|
|
|
* @return CONFIG(ignitionMode) otherwise
|
|
|
|
*/
|
|
|
|
ignition_mode_e getCurrentIgnitionMode(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2018-03-10 17:58:51 -08:00
|
|
|
ignition_mode_e ignitionMode = CONFIG(ignitionMode);
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_SHAFT_POSITION_INPUT
|
2018-03-10 17:58:51 -08:00
|
|
|
// In spin-up cranking mode we don't have full phase sync. info yet, so wasted spark mode is better
|
2020-09-05 15:49:42 -07:00
|
|
|
if (ignitionMode == IM_INDIVIDUAL_COILS && ENGINE(rpmCalculator.isSpinningUp()))
|
2018-03-10 17:58:51 -08:00
|
|
|
ignitionMode = IM_WASTED_SPARK;
|
2019-01-31 14:55:23 -08:00
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
2018-03-10 17:58:51 -08:00
|
|
|
return ignitionMode;
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_ENGINE_CONTROL
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-01-14 21:01:42 -08:00
|
|
|
/**
|
|
|
|
* This heavy method is only invoked in case of a configuration change or initialization.
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
void prepareOutputSignals(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2019-08-07 21:19:09 -07:00
|
|
|
ENGINE(engineCycle) = getEngineCycle(engine->getOperationMode(PASS_ENGINE_PARAMETER_SIGNATURE));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-11-11 16:02:29 -08:00
|
|
|
angle_t maxTimingCorrMap = -720.0f;
|
|
|
|
angle_t maxTimingMap = -720.0f;
|
|
|
|
for (int rpmIndex = 0;rpmIndex<IGN_RPM_COUNT;rpmIndex++) {
|
|
|
|
for (int l = 0;l<IGN_LOAD_COUNT;l++) {
|
|
|
|
maxTimingCorrMap = maxF(maxTimingCorrMap, config->ignitionIatCorrTable[l][rpmIndex]);
|
|
|
|
maxTimingMap = maxF(maxTimingMap, config->ignitionTable[l][rpmIndex]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 19:02:12 -07:00
|
|
|
#if EFI_UNIT_TEST
|
2019-08-24 22:15:18 -07:00
|
|
|
if (verboseMode) {
|
|
|
|
printf("prepareOutputSignals %d onlyEdge=%s %s\r\n", engineConfiguration->trigger.type, boolToString(engineConfiguration->useOnlyRisingEdgeForTrigger),
|
|
|
|
getIgnition_mode_e(engineConfiguration->ignitionMode));
|
|
|
|
}
|
2018-03-03 06:11:49 -08:00
|
|
|
#endif /* EFI_UNIT_TEST */
|
2016-10-31 19:02:12 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
for (int i = 0; i < CONFIG(specs.cylindersCount); i++) {
|
2019-10-26 18:53:22 -07:00
|
|
|
ENGINE(ignitionPositionWithinEngineCycle[i]) = ENGINE(engineCycle) * i / CONFIG(specs.cylindersCount);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-03-10 17:58:51 -08:00
|
|
|
prepareIgnitionPinIndices(CONFIG(ignitionMode) PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
|
2020-08-25 09:45:18 -07:00
|
|
|
TRIGGER_WAVEFORM(prepareShape(&ENGINE(triggerCentral.triggerFormDetails) PASS_ENGINE_PARAMETER_SUFFIX));
|
2021-01-11 05:01:54 -08:00
|
|
|
|
|
|
|
// Fuel schedule may now be completely wrong, force a reset
|
|
|
|
ENGINE(injectionEvents).invalidate();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 16:25:07 -08:00
|
|
|
void setTimingRpmBin(float from, float to DECLARE_CONFIG_PARAMETER_SUFFIX) {
|
2016-01-21 19:01:31 -08:00
|
|
|
setRpmBin(config->ignitionRpmBins, IGN_RPM_COUNT, from, to);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 16:25:07 -08:00
|
|
|
void setTimingLoadBin(float from, float to DECLARE_CONFIG_PARAMETER_SUFFIX) {
|
2019-11-22 20:27:24 -08:00
|
|
|
setLinearCurve(config->ignitionLoadBins, from, to);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this method sets algorithm and ignition table scale
|
|
|
|
*/
|
2019-01-10 16:25:07 -08:00
|
|
|
void setAlgorithm(engine_load_mode_e algo DECLARE_CONFIG_PARAMETER_SUFFIX) {
|
2016-08-28 13:02:34 -07:00
|
|
|
engineConfiguration->fuelAlgorithm = algo;
|
2020-07-29 01:35:17 -07:00
|
|
|
if (algo == LM_SPEED_DENSITY) {
|
2019-11-22 20:27:24 -08:00
|
|
|
setLinearCurve(config->ignitionLoadBins, 20, 120, 3);
|
2019-01-10 16:25:07 -08:00
|
|
|
buildTimingMap(35 PASS_CONFIG_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
2016-07-24 20:02:52 -07:00
|
|
|
|
2019-01-10 16:25:07 -08:00
|
|
|
void setFlatInjectorLag(float value DECLARE_CONFIG_PARAMETER_SUFFIX) {
|
2019-11-22 20:27:24 -08:00
|
|
|
setArrayValues(engineConfiguration->injector.battLagCorr, value);
|
2016-07-24 20:02:52 -07:00
|
|
|
}
|
2020-02-26 15:16:35 -08:00
|
|
|
|
|
|
|
#endif /* EFI_ENGINE_CONTROL */
|