2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file rpm_calculator.cpp
|
|
|
|
* @brief RPM calculator
|
|
|
|
*
|
|
|
|
* Here we listen to position sensor events in order to figure our if engine is currently running or not.
|
|
|
|
* Actual getRpm() is calculated once per crankshaft revolution, based on the amount of time passed
|
|
|
|
* since the start of previous shaft revolution.
|
|
|
|
*
|
2019-10-14 03:18:08 -07:00
|
|
|
* We also have 'instant RPM' logic separate from this 'cycle RPM' logic. Open question is why do we not use
|
|
|
|
* instant RPM instead of cycle RPM more often.
|
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
* @date Jan 1, 2013
|
2020-01-07 21:02:40 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
2019-07-05 17:03:32 -07:00
|
|
|
#include "os_access.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "trigger_central.h"
|
2020-04-19 17:46:29 -07:00
|
|
|
#include "tooth_logger.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
2019-07-06 17:15:49 -07:00
|
|
|
#include "os_util.h"
|
2019-10-14 03:18:08 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_SENSOR_CHART
|
2015-09-12 16:01:20 -07:00
|
|
|
#include "sensor_chart.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
2019-03-31 14:44:34 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-07-15 18:01:45 -07:00
|
|
|
#if EFI_ENGINE_SNIFFER
|
|
|
|
#include "engine_sniffer.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
extern WaveChart waveChart;
|
2015-07-15 18:01:45 -07:00
|
|
|
#endif /* EFI_ENGINE_SNIFFER */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-03-03 05:55:19 -08:00
|
|
|
// See RpmCalculator::checkIfSpinning()
|
|
|
|
#ifndef NO_RPM_EVENTS_TIMEOUT_SECS
|
|
|
|
#define NO_RPM_EVENTS_TIMEOUT_SECS 2
|
|
|
|
#endif /* NO_RPM_EVENTS_TIMEOUT_SECS */
|
|
|
|
|
2019-06-08 06:51:36 -07:00
|
|
|
float RpmCalculator::getRpmAcceleration() const {
|
2021-01-09 17:06:19 -08:00
|
|
|
return rpmRate;
|
2019-01-31 14:55:23 -08:00
|
|
|
}
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
bool RpmCalculator::isStopped() const {
|
2019-01-31 14:55:23 -08:00
|
|
|
// Spinning-up with zero RPM means that the engine is not ready yet, and is treated as 'stopped'.
|
|
|
|
return state == STOPPED || (state == SPINNING_UP && rpmValue == 0);
|
|
|
|
}
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
bool RpmCalculator::isCranking() const {
|
2019-01-31 14:55:23 -08:00
|
|
|
// Spinning-up with non-zero RPM is suitable for all engine math, as good as cranking
|
|
|
|
return state == CRANKING || (state == SPINNING_UP && rpmValue > 0);
|
|
|
|
}
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
bool RpmCalculator::isSpinningUp() const {
|
2019-01-31 14:55:23 -08:00
|
|
|
return state == SPINNING_UP;
|
|
|
|
}
|
|
|
|
|
2019-06-08 06:51:36 -07:00
|
|
|
uint32_t RpmCalculator::getRevolutionCounterSinceStart(void) const {
|
2019-01-31 14:55:23 -08:00
|
|
|
return revolutionCounterSinceStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return -1 in case of isNoisySignal(), current RPM otherwise
|
2019-05-10 18:56:33 -07:00
|
|
|
* See NOISY_RPM
|
2019-01-31 14:55:23 -08:00
|
|
|
*/
|
|
|
|
// todo: migrate to float return result or add a float version? this would have with calculations
|
2020-09-05 15:49:42 -07:00
|
|
|
int RpmCalculator::getRpm() const {
|
2019-01-31 14:55:23 -08:00
|
|
|
#if !EFI_PROD_CODE
|
|
|
|
if (mockRpm != MOCK_UNDEFINED) {
|
|
|
|
return mockRpm;
|
|
|
|
}
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
return rpmValue;
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_SHAFT_POSITION_INPUT
|
2019-01-31 14:55:23 -08:00
|
|
|
|
2020-09-03 20:27:53 -07:00
|
|
|
RpmCalculator::RpmCalculator() :
|
2020-09-05 15:49:42 -07:00
|
|
|
StoredValueSensor(SensorType::Rpm, 0)
|
2020-09-03 20:27:53 -07:00
|
|
|
{
|
2015-07-10 06:01:56 -07:00
|
|
|
#if !EFI_PROD_CODE
|
|
|
|
mockRpm = MOCK_UNDEFINED;
|
2017-07-06 18:21:45 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2021-11-16 01:15:29 -08:00
|
|
|
// todo: reuse assignRpmValue() method which needs
|
2019-01-13 20:20:19 -08:00
|
|
|
// which we cannot provide inside this parameter-less constructor. need a solution for this minor mess
|
2021-11-04 02:46:16 -07:00
|
|
|
setValidValue(0, 0); // 0 for current time since RPM sensor never times out
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if there was a full shaft revolution within the last second
|
|
|
|
*/
|
2020-09-05 15:49:42 -07:00
|
|
|
bool RpmCalculator::isRunning() const {
|
2017-07-07 05:10:06 -07:00
|
|
|
return state == RUNNING;
|
2017-07-06 17:10:34 -07:00
|
|
|
}
|
|
|
|
|
2019-01-05 20:33:04 -08:00
|
|
|
/**
|
|
|
|
* @return true if engine is spinning (cranking or running)
|
|
|
|
*/
|
2020-09-05 15:49:42 -07:00
|
|
|
bool RpmCalculator::checkIfSpinning(efitick_t nowNt) const {
|
2021-02-28 15:56:34 -08:00
|
|
|
// Anything below 60 rpm is not running
|
|
|
|
bool noRpmEventsForTooLong = lastTdcTimer.getElapsedSeconds(nowNt) > NO_RPM_EVENTS_TIMEOUT_SECS;
|
|
|
|
|
2018-03-10 17:58:51 -08:00
|
|
|
/**
|
|
|
|
* Also check if there were no trigger events
|
|
|
|
*/
|
2021-02-28 15:56:34 -08:00
|
|
|
bool noTriggerEventsForTooLong = engine->triggerCentral.getTimeSinceTriggerEvent(nowNt) >= 1;
|
|
|
|
|
2018-03-10 17:58:51 -08:00
|
|
|
if (noRpmEventsForTooLong || noTriggerEventsForTooLong) {
|
2017-07-06 17:10:34 -07:00
|
|
|
return false;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-07-06 17:10:34 -07:00
|
|
|
return true;
|
2017-07-06 16:33:25 -07:00
|
|
|
}
|
2016-03-15 19:03:43 -07:00
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
void RpmCalculator::assignRpmValue(float floatRpmValue) {
|
2015-07-10 06:01:56 -07:00
|
|
|
previousRpmValue = rpmValue;
|
2020-12-05 21:11:57 -08:00
|
|
|
|
|
|
|
// Round to the nearest integer RPM - some other parts of the ECU expect integer, so that's what we hand out.
|
|
|
|
// TODO: RPM should eventually switch to floating point across the ECU
|
|
|
|
rpmValue = efiRound(floatRpmValue, 1);
|
2020-09-03 20:27:53 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
if (rpmValue <= 0) {
|
|
|
|
oneDegreeUs = NAN;
|
2021-11-04 02:46:16 -07:00
|
|
|
setValidValue(0, 0); // 0 for current time since RPM sensor never times out
|
2015-07-10 06:01:56 -07:00
|
|
|
} else {
|
2020-09-05 15:49:42 -07:00
|
|
|
setValidValue(floatRpmValue, 0); // 0 for current time since RPM sensor never times out
|
|
|
|
|
2019-11-05 21:31:24 -08:00
|
|
|
// here it's really important to have more precise float RPM value, see #796
|
|
|
|
oneDegreeUs = getOneDegreeTimeUs(floatRpmValue);
|
2018-03-10 17:58:51 -08:00
|
|
|
if (previousRpmValue == 0) {
|
|
|
|
/**
|
|
|
|
* this would make sure that we have good numbers for first cranking revolution
|
|
|
|
* #275 cranking could be improved
|
|
|
|
*/
|
2021-11-17 10:45:10 -08:00
|
|
|
engine->periodicFastCallback();
|
2018-03-10 17:58:51 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
void RpmCalculator::setRpmValue(float value) {
|
|
|
|
assignRpmValue(value);
|
2018-03-10 17:58:51 -08:00
|
|
|
spinning_state_e oldState = state;
|
|
|
|
// Change state
|
2017-07-07 05:10:06 -07:00
|
|
|
if (rpmValue == 0) {
|
|
|
|
state = STOPPED;
|
2021-11-17 00:54:21 -08:00
|
|
|
} else if (rpmValue >= engineConfiguration->cranking.rpm) {
|
2021-06-16 14:20:28 -07:00
|
|
|
if (state != RUNNING) {
|
|
|
|
// Store the time the engine started
|
|
|
|
engineStartTimer.reset();
|
|
|
|
}
|
|
|
|
|
2017-07-07 05:10:06 -07:00
|
|
|
state = RUNNING;
|
2018-03-10 17:58:51 -08:00
|
|
|
} else if (state == STOPPED || state == SPINNING_UP) {
|
2017-07-08 12:46:34 -07:00
|
|
|
/**
|
|
|
|
* We are here if RPM is above zero but we have not seen running RPM yet.
|
|
|
|
* This gives us cranking hysteresis - a drop of RPM during running is still running, not cranking.
|
|
|
|
*/
|
2017-07-08 10:42:14 -07:00
|
|
|
state = CRANKING;
|
2017-07-07 05:10:06 -07:00
|
|
|
}
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_ENGINE_CONTROL
|
2018-03-10 17:58:51 -08:00
|
|
|
// This presumably fixes injection mode change for cranking-to-running transition.
|
|
|
|
// 'isSimultanious' flag should be updated for events if injection modes differ for cranking and running.
|
2021-11-17 00:54:21 -08:00
|
|
|
if (state != oldState && engineConfiguration->crankingInjectionMode != engineConfiguration->injectionMode) {
|
2020-07-16 23:55:41 -07:00
|
|
|
// Reset the state of all injectors: when we change fueling modes, we could
|
|
|
|
// immediately reschedule an injection that's currently underway. That will cause
|
|
|
|
// the injector's overlappingCounter to get out of sync with reality. As the fix,
|
|
|
|
// every injector's state is forcibly reset just before we could cause that to happen.
|
2020-07-14 23:54:41 -07:00
|
|
|
engine->injectionEvents.resetOverlapping();
|
2020-07-16 23:55:41 -07:00
|
|
|
|
|
|
|
// reschedule all injection events now that we've reset them
|
2021-11-16 01:15:29 -08:00
|
|
|
engine->injectionEvents.addFuelEvents();
|
2018-03-10 17:58:51 -08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:44:29 -08:00
|
|
|
spinning_state_e RpmCalculator::getState() const {
|
2018-03-10 17:58:51 -08:00
|
|
|
return state;
|
2016-03-15 19:03:43 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void RpmCalculator::onNewEngineCycle() {
|
|
|
|
revolutionCounterSinceBoot++;
|
|
|
|
revolutionCounterSinceStart++;
|
|
|
|
}
|
|
|
|
|
2019-10-14 03:18:08 -07:00
|
|
|
uint32_t RpmCalculator::getRevolutionCounterM(void) const {
|
2015-07-10 06:01:56 -07:00
|
|
|
return revolutionCounterSinceBoot;
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:59:02 -08:00
|
|
|
void RpmCalculator::onSlowCallback() {
|
|
|
|
/**
|
|
|
|
* Update engine RPM state if needed (check timeouts).
|
|
|
|
*/
|
|
|
|
if (!checkIfSpinning(getTimeNowNt())) {
|
|
|
|
engine->rpmCalculator.setStopSpinning();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
void RpmCalculator::setStopped() {
|
2017-07-06 16:33:25 -07:00
|
|
|
revolutionCounterSinceStart = 0;
|
2021-01-09 17:06:19 -08:00
|
|
|
|
|
|
|
rpmRate = 0;
|
|
|
|
|
2017-07-06 16:33:25 -07:00
|
|
|
if (rpmValue != 0) {
|
2020-09-05 15:49:42 -07:00
|
|
|
assignRpmValue(0);
|
2020-09-07 12:36:17 -07:00
|
|
|
// needed by 'useNoiselessTriggerDecoder'
|
2020-09-07 21:15:09 -07:00
|
|
|
engine->triggerCentral.noiseFilter.resetAccumSignalData();
|
2021-04-21 09:53:13 -07:00
|
|
|
efiPrintf("engine stopped");
|
2017-07-06 16:33:25 -07:00
|
|
|
}
|
2017-07-07 04:20:04 -07:00
|
|
|
state = STOPPED;
|
2017-07-06 16:33:25 -07:00
|
|
|
}
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
void RpmCalculator::setStopSpinning() {
|
2018-03-10 17:58:51 -08:00
|
|
|
isSpinning = false;
|
2020-09-05 15:49:42 -07:00
|
|
|
setStopped();
|
2018-03-10 17:58:51 -08:00
|
|
|
}
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
void RpmCalculator::setSpinningUp(efitick_t nowNt) {
|
2021-11-17 00:54:21 -08:00
|
|
|
if (!engineConfiguration->isFasterEngineSpinUpEnabled)
|
2018-03-10 17:58:51 -08:00
|
|
|
return;
|
|
|
|
// Only a completely stopped and non-spinning engine can enter the spinning-up state.
|
2020-09-05 15:49:42 -07:00
|
|
|
if (isStopped() && !isSpinning) {
|
2018-03-10 17:58:51 -08:00
|
|
|
state = SPINNING_UP;
|
2019-01-24 18:12:55 -08:00
|
|
|
engine->triggerCentral.triggerState.spinningEventIndex = 0;
|
2018-03-10 17:58:51 -08:00
|
|
|
isSpinning = true;
|
|
|
|
}
|
|
|
|
// update variables needed by early instant RPM calc.
|
2020-09-05 15:49:42 -07:00
|
|
|
if (isSpinningUp()) {
|
2021-11-16 01:15:29 -08:00
|
|
|
engine->triggerCentral.triggerState.setLastEventTimeForInstantRpm(nowNt);
|
2018-03-10 17:58:51 -08:00
|
|
|
}
|
2019-01-20 08:33:40 -08:00
|
|
|
/**
|
|
|
|
* Update ignition pin indices if needed. Here we potentially switch to wasted spark temporarily.
|
|
|
|
*/
|
2021-11-16 01:15:29 -08:00
|
|
|
prepareIgnitionPinIndices(getCurrentIgnitionMode());
|
2018-03-10 17:58:51 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @brief Shaft position callback used by RPM calculation logic.
|
|
|
|
*
|
|
|
|
* This callback should always be the first of trigger callbacks because other callbacks depend of values
|
|
|
|
* updated here.
|
|
|
|
* This callback is invoked on interrupt thread.
|
|
|
|
*/
|
|
|
|
void rpmShaftPositionCallback(trigger_event_e ckpSignalType,
|
2021-11-16 01:15:29 -08:00
|
|
|
uint32_t index, efitick_t nowNt) {
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-03-10 17:58:51 -08:00
|
|
|
RpmCalculator *rpmState = &engine->rpmCalculator;
|
|
|
|
|
2017-05-25 19:49:40 -07:00
|
|
|
if (index == 0) {
|
2020-09-05 15:49:42 -07:00
|
|
|
bool hadRpmRecently = rpmState->checkIfSpinning(nowNt);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-02-28 15:56:34 -08:00
|
|
|
float periodSeconds = engine->rpmCalculator.lastTdcTimer.getElapsedSecondsAndReset(nowNt);
|
|
|
|
|
2017-05-25 19:49:40 -07:00
|
|
|
if (hadRpmRecently) {
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Four stroke cycle is two crankshaft revolutions
|
|
|
|
*
|
|
|
|
* We always do '* 2' because the event signal is already adjusted to 'per engine cycle'
|
|
|
|
* and each revolution of crankshaft consists of two engine cycles revolutions
|
|
|
|
*
|
|
|
|
*/
|
2021-02-28 15:56:34 -08:00
|
|
|
if (periodSeconds == 0) {
|
2020-09-05 15:49:42 -07:00
|
|
|
rpmState->setRpmValue(NOISY_RPM);
|
2021-01-09 17:06:19 -08:00
|
|
|
rpmState->rpmRate = 0;
|
2017-05-25 19:49:40 -07:00
|
|
|
} else {
|
2021-11-16 01:15:29 -08:00
|
|
|
int mult = (int)getEngineCycle(engine->getOperationMode()) / 360;
|
2021-02-28 15:56:34 -08:00
|
|
|
float rpm = 60 * mult / periodSeconds;
|
2021-01-09 17:06:19 -08:00
|
|
|
|
|
|
|
auto rpmDelta = rpm - rpmState->previousRpmValue;
|
2021-02-28 15:56:34 -08:00
|
|
|
rpmState->rpmRate = rpmDelta / (mult * periodSeconds);
|
2021-01-09 17:06:19 -08:00
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
rpmState->setRpmValue(rpm > UNREALISTIC_RPM ? NOISY_RPM : rpm);
|
2017-05-25 19:49:40 -07:00
|
|
|
}
|
2021-07-01 06:19:16 -07:00
|
|
|
} else {
|
|
|
|
// we are here only once trigger is synchronized for the first time
|
|
|
|
// while transitioning from 'spinning' to 'running'
|
2021-11-16 01:15:29 -08:00
|
|
|
engine->triggerCentral.triggerState.movePreSynchTimestamps();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2021-02-28 15:56:34 -08:00
|
|
|
|
2017-05-25 19:49:40 -07:00
|
|
|
rpmState->onNewEngineCycle();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2017-05-25 19:49:40 -07:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_SENSOR_CHART
|
2016-05-28 21:01:59 -07:00
|
|
|
// this 'index==0' case is here so that it happens after cycle callback so
|
|
|
|
// it goes into sniffer report into the first position
|
2021-11-17 00:54:21 -08:00
|
|
|
if (engine->sensorChartMode == SC_TRIGGER) {
|
2021-11-20 03:59:02 -08:00
|
|
|
angle_t crankAngle = engine->triggerCentral.getCurrentEnginePhase(nowNt).value_or(0);
|
2017-05-25 19:49:40 -07:00
|
|
|
int signal = 1000 * ckpSignalType + index;
|
2015-07-26 21:01:35 -07:00
|
|
|
scAddData(crankAngle, signal);
|
|
|
|
}
|
2019-12-04 04:33:56 -08:00
|
|
|
#endif /* EFI_SENSOR_CHART */
|
2017-05-25 19:49:40 -07:00
|
|
|
|
2021-01-14 17:45:55 -08:00
|
|
|
// Always update instant RPM even when not spinning up
|
2021-11-09 04:03:27 -08:00
|
|
|
engine->triggerCentral.triggerState.updateInstantRpm(
|
|
|
|
engine->triggerCentral.triggerShape, &engine->triggerCentral.triggerFormDetails,
|
2021-11-16 01:15:29 -08:00
|
|
|
index, nowNt);
|
2021-01-14 17:45:55 -08:00
|
|
|
|
|
|
|
if (rpmState->isSpinningUp()) {
|
|
|
|
float instantRpm = engine->triggerCentral.triggerState.getInstantRpm();
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
rpmState->assignRpmValue(instantRpm);
|
2018-03-10 17:58:51 -08:00
|
|
|
#if 0
|
2021-04-21 09:53:13 -07:00
|
|
|
efiPrintf("** RPM: idx=%d sig=%d iRPM=%d", index, ckpSignalType, instantRpm);
|
2018-03-10 17:58:51 -08:00
|
|
|
#endif
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-06-16 14:20:28 -07:00
|
|
|
float RpmCalculator::getTimeSinceEngineStart(efitick_t nowNt) const {
|
|
|
|
return engineStartTimer.getElapsedSeconds(nowNt);
|
|
|
|
}
|
|
|
|
|
2016-08-09 21:04:24 -07:00
|
|
|
static char rpmBuffer[_MAX_FILLER];
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
2019-05-02 14:52:48 -07:00
|
|
|
* This callback has nothing to do with actual engine control, it just sends a Top Dead Center mark to the rusEfi console
|
2015-07-10 06:01:56 -07:00
|
|
|
* digital sniffer.
|
|
|
|
*/
|
2021-11-16 01:15:29 -08:00
|
|
|
static void onTdcCallback(void *) {
|
2019-12-23 18:58:06 -08:00
|
|
|
#if EFI_UNIT_TEST
|
2019-12-23 18:55:08 -08:00
|
|
|
if (!engine->needTdcCallback) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-23 18:58:06 -08:00
|
|
|
#endif /* EFI_UNIT_TEST */
|
2021-11-16 01:15:29 -08:00
|
|
|
|
2019-01-21 18:48:58 -08:00
|
|
|
itoa10(rpmBuffer, GET_RPM());
|
2019-05-02 15:42:59 -07:00
|
|
|
#if EFI_ENGINE_SNIFFER
|
|
|
|
waveChart.startDataCollection();
|
|
|
|
#endif
|
2018-09-10 19:29:43 -07:00
|
|
|
addEngineSnifferEvent(TOP_DEAD_CENTER_MESSAGE, (char* ) rpmBuffer);
|
2020-04-19 17:57:01 -07:00
|
|
|
#if EFI_TOOTH_LOGGER
|
2021-11-16 01:15:29 -08:00
|
|
|
LogTriggerTopDeadCenter(getTimeNowNt());
|
2020-04-19 17:57:01 -07:00
|
|
|
#endif /* EFI_TOOTH_LOGGER */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This trigger callback schedules the actual physical TDC callback in relation to trigger synchronization point.
|
|
|
|
*/
|
2020-10-03 23:09:12 -07:00
|
|
|
void tdcMarkCallback(
|
2021-11-16 01:15:29 -08:00
|
|
|
uint32_t index0, efitick_t edgeTimestamp) {
|
2015-07-10 06:01:56 -07:00
|
|
|
bool isTriggerSynchronizationPoint = index0 == 0;
|
2021-11-17 00:54:21 -08:00
|
|
|
if (isTriggerSynchronizationPoint && engine->isEngineChartEnabled && engine->tdcMarkEnabled) {
|
2019-12-23 17:19:13 -08:00
|
|
|
// two instances of scheduling_s are needed to properly handle event overlap
|
2019-10-14 03:18:08 -07:00
|
|
|
int revIndex2 = getRevolutionCounter() % 2;
|
2019-01-21 18:48:58 -08:00
|
|
|
int rpm = GET_RPM();
|
2018-08-31 18:11:22 -07:00
|
|
|
// todo: use tooth event-based scheduling, not just time-based scheduling
|
2015-07-10 06:01:56 -07:00
|
|
|
if (isValidRpm(rpm)) {
|
2020-04-20 19:43:55 -07:00
|
|
|
angle_t tdcPosition = tdcPosition();
|
2020-04-20 20:44:59 -07:00
|
|
|
// we need a positive angle offset here
|
2020-04-20 19:43:55 -07:00
|
|
|
fixAngle(tdcPosition, "tdcPosition", CUSTOM_ERR_6553);
|
2021-11-16 01:15:29 -08:00
|
|
|
scheduleByAngle(&engine->tdcScheduler[revIndex2], edgeTimestamp, tdcPosition, onTdcCallback);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 22:42:03 -08:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void initRpmCalculator() {
|
2020-09-05 15:49:42 -07:00
|
|
|
|
2020-09-19 01:57:07 -07:00
|
|
|
#if ! HW_CHECK_MODE
|
2016-06-01 17:01:36 -07:00
|
|
|
if (hasFirmwareError()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-19 01:57:07 -07:00
|
|
|
#endif // HW_CHECK_MODE
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
// Only register if not configured to read RPM over OBD2
|
2021-11-17 00:54:21 -08:00
|
|
|
if (!engineConfiguration->consumeObdSensors) {
|
|
|
|
engine->rpmCalculator.Register();
|
2020-09-05 15:49:42 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedules a callback 'angle' degree of crankshaft from now.
|
|
|
|
* The callback would be executed once after the duration of time which
|
|
|
|
* it takes the crankshaft to rotate to the specified angle.
|
|
|
|
*/
|
2020-02-01 14:29:55 -08:00
|
|
|
efitick_t scheduleByAngle(scheduling_s *timer, efitick_t edgeTimestamp, angle_t angle,
|
2021-11-16 01:15:29 -08:00
|
|
|
action_s action) {
|
2021-11-17 00:54:21 -08:00
|
|
|
float delayUs = engine->rpmCalculator.oneDegreeUs * angle;
|
2020-01-09 12:45:13 -08:00
|
|
|
|
2020-12-03 10:30:17 -08:00
|
|
|
// 'delayNt' is below 10 seconds here so we use 32 bit type for performance reasons
|
|
|
|
int32_t delayNt = USF2NT(delayUs);
|
2020-01-09 12:45:13 -08:00
|
|
|
efitime_t delayedTime = edgeTimestamp + delayNt;
|
|
|
|
|
2021-11-17 10:45:10 -08:00
|
|
|
engine->executor.scheduleByTimestampNt("angle", timer, delayedTime, action);
|
2020-02-01 14:29:55 -08:00
|
|
|
|
|
|
|
return delayedTime;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
RpmCalculator::RpmCalculator() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
|
|
|
|