rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Data Fields | Protected Member Functions | Private Attributes
RpmCalculator Class Reference

#include <rpm_calculator.h>

Inheritance diagram for RpmCalculator:
Inheritance graph
[legend]
Collaboration diagram for RpmCalculator:
Collaboration graph
[legend]

Public Member Functions

 RpmCalculator ()
 
operation_mode_e getOperationMode () const override
 
void onSlowCallback ()
 
bool isStopped () const override
 
bool isSpinningUp () const
 
bool isCranking () const override
 
bool isRunning () const
 
bool checkIfSpinning (efitick_t nowNt) const
 
spinning_state_e getState () const
 
void setSpinningUp (efitick_t nowNt)
 
void setStopSpinning ()
 
float getCachedRpm () const
 
float getMinCrankingRpm () const
 
void onNewEngineCycle ()
 
uint32_t getRevolutionCounterM (void) const
 
void setRpmValue (float value)
 
void assignRpmValue (float value)
 
uint32_t getRevolutionCounterSinceStart (void) const
 
float getRpmAcceleration () const
 
float getSecondsSinceEngineStart (efitick_t nowNt) const
 
floatus_t getOneDegreeUs () override
 
- Public Member Functions inherited from StoredValueSensor
SensorResult get () const final override
 
 StoredValueSensor (SensorType type, efidur_t timeoutNt)
 
void invalidate ()
 
void invalidate (UnexpectedCode why)
 
void setValidValue (float value, efitick_t timestamp)
 
void showInfo (const char *sensorName) const override
 
virtual void setTimeout (int timeoutMs)
 
- Public Member Functions inherited from Sensor
bool Register ()
 
const chargetSensorName () const
 
virtual bool hasSensor () const
 
virtual float getRaw () const
 
virtual bool isRedundant () const
 
void unregister ()
 
SensorType type () const
 

Data Fields

float previousRpmValue = 0
 
floatus_t oneDegreeUs = NAN
 
Timer lastTdcTimer
 
float rpmRate = 0
 

Protected Member Functions

void showInfo (const char *sensorName) const override
 
- Protected Member Functions inherited from Sensor
 Sensor (SensorType type)
 

Private Attributes

float cachedRpmValue = 0
 
float minCrankingRpm = 0
 
uint32_t revolutionCounterSinceBoot = 0
 
uint32_t revolutionCounterSinceStart = 0
 
spinning_state_e state = STOPPED
 
bool isSpinning = false
 
Timer engineStartTimer
 

Additional Inherited Members

- Static Public Member Functions inherited from Sensor
static void showAllSensorInfo ()
 
static void showInfo (SensorType type)
 
static void resetRegistry ()
 
static const SensorgetSensorOfType (SensorType type)
 
static SensorResult get (SensorType type)
 
static float getOrZero (SensorType type)
 
static float getRaw (SensorType type)
 
static bool isRedundant (SensorType type)
 
static bool hasSensor (SensorType type)
 
static void setMockValue (SensorType type, float value, bool mockRedundant=false)
 
static void setInvalidMockValue (SensorType type)
 
static void resetMockValue (SensorType type)
 
static void resetAllMocks ()
 
static void inhibitTimeouts (bool inhibit)
 
static const chargetSensorName (SensorType type)
 
- Static Protected Attributes inherited from Sensor
static bool s_inhibitSensorTimeouts = false
 

Detailed Description

Most consumers should access value via Sensor framework by SensorType::Rpm key

Definition at line 42 of file rpm_calculator.h.

Constructor & Destructor Documentation

◆ RpmCalculator()

RpmCalculator::RpmCalculator ( )

Definition at line 102 of file rpm_calculator.cpp.

102 :
104 {
106}
void assignRpmValue(float value)
Base class for sensors that compute a value on one thread, and want to make it available to consumers...
Here is the call graph for this function:

Member Function Documentation

◆ assignRpmValue()

void RpmCalculator::assignRpmValue ( float  value)

The same as setRpmValue() but without state change. We need this to be public because of calling rpmState->assignRpmValue() from rpmShaftPositionCallback()

this would make sure that we have good numbers for first cranking revolution #275 cranking could be improved

Definition at line 138 of file rpm_calculator.cpp.

138 {
140
141 cachedRpmValue = floatRpmValue;
142
143 setValidValue(floatRpmValue, 0); // 0 for current time since RPM sensor never times out
144 if (cachedRpmValue <= 0) {
145 oneDegreeUs = NAN;
146 } else {
147 // here it's really important to have more precise float RPM value, see #796
148 oneDegreeUs = getOneDegreeTimeUs(floatRpmValue);
149 if (previousRpmValue == 0) {
150 /**
151 * this would make sure that we have good numbers for first cranking revolution
152 * #275 cranking could be improved
153 */
155 }
156 }
157}
void periodicFastCallback()
Definition engine.cpp:567
floatus_t oneDegreeUs
void setValidValue(float value, efitick_t timestamp)
static EngineAccessor engine
Definition engine.h:421

Referenced by RpmCalculator(), rpmShaftPositionCallback(), setRpmValue(), and setStopSpinning().

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

◆ checkIfSpinning()

bool RpmCalculator::checkIfSpinning ( efitick_t  nowNt) const
Returns
true if engine is spinning (cranking or running)

Also check if there were no trigger events

Definition at line 118 of file rpm_calculator.cpp.

118 {
119 if (getLimpManager()->shutdownController.isEngineStop(nowNt)) {
120 return false;
121 }
122
123 // Anything below 60 rpm is not running
124 bool noRpmEventsForTooLong = lastTdcTimer.getElapsedSeconds(nowNt) > NO_RPM_EVENTS_TIMEOUT_SECS;
125
126 /**
127 * Also check if there were no trigger events
128 */
129 bool noTriggerEventsForTooLong = !engine->triggerCentral.engineMovedRecently(nowNt);
130
131 if (noRpmEventsForTooLong || noTriggerEventsForTooLong) {
132 return false;
133 }
134
135 return true;
136}
TriggerCentral triggerCentral
Definition engine.h:326
bool engineMovedRecently(efitick_t nowNt) const
LimpManager * getLimpManager()
Definition engine.cpp:608

Referenced by rpmShaftPositionCallback().

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

◆ getCachedRpm()

float RpmCalculator::getCachedRpm ( ) const

Just a quick getter for rpmValue Should be same exact value as Sensor::get(SensorType::Rpm).Value just quicker. Open question if we have any cases where this opimization is needed.

Definition at line 49 of file rpm_calculator.cpp.

49 {
50 return cachedRpmValue;
51}

Referenced by TriggerCentral::handleShaftSignal(), and mainTriggerCallback().

Here is the caller graph for this function:

◆ getMinCrankingRpm()

float RpmCalculator::getMinCrankingRpm ( ) const

Definition at line 53 of file rpm_calculator.cpp.

53 {
54 return minCrankingRpm;
55}

Referenced by getCrankingAdvance().

Here is the caller graph for this function:

◆ getOneDegreeUs()

floatus_t RpmCalculator::getOneDegreeUs ( )
inlineoverridevirtual

Implements EngineRotationState.

Definition at line 124 of file rpm_calculator.h.

124 {
125 return oneDegreeUs;
126 }

◆ getOperationMode()

operation_mode_e RpmCalculator::getOperationMode ( ) const
overridevirtual

Implements EngineRotationState.

Definition at line 85 of file rpm_calculator.cpp.

85 {
86#if EFI_SHAFT_POSITION_INPUT
87 // Ignore user-provided setting for well known triggers.
89 // For example for Miata NA, there is no reason to allow user to set FOUR_STROKE_CRANK_SENSOR
91 } else
92#endif // EFI_SHAFT_POSITION_INPUT
93 {
94 // For example 36-1, could be on either cam or crank, so we have to ask the user
95 return lookupOperationMode();
96 }
97}
TriggerWaveform triggerShape
operation_mode_e getWheelOperationMode() const
static constexpr engine_configuration_s * engineConfiguration
static bool doesTriggerImplyOperationMode(trigger_type_e type)
operation_mode_e lookupOperationMode()
Here is the call graph for this function:

◆ getRevolutionCounterM()

uint32_t RpmCalculator::getRevolutionCounterM ( void  ) const

Definition at line 212 of file rpm_calculator.cpp.

212 {
214}
uint32_t revolutionCounterSinceBoot

◆ getRevolutionCounterSinceStart()

uint32_t RpmCalculator::getRevolutionCounterSinceStart ( void  ) const

Definition at line 45 of file rpm_calculator.cpp.

45 {
47}
uint32_t revolutionCounterSinceStart

Referenced by getCrankingFuel(), IdleController::getCrankingTaperFraction(), getPostCrankingFuelCorrection(), and updateTunerStudioState().

Here is the caller graph for this function:

◆ getRpmAcceleration()

float RpmCalculator::getRpmAcceleration ( ) const

RPM rate of change between current RPM and RPM measured during previous engine cycle see also SC_RPM_ACCEL

Definition at line 27 of file rpm_calculator.cpp.

27 {
28 return rpmRate;
29}

Referenced by updateTunerStudioState().

Here is the caller graph for this function:

◆ getSecondsSinceEngineStart()

float RpmCalculator::getSecondsSinceEngineStart ( efitick_t  nowNt) const

Definition at line 331 of file rpm_calculator.cpp.

331 {
332 return engineStartTimer.getElapsedSeconds(nowNt);
333}

Referenced by LtftState::load(), VvtController::onFastCallback(), and LimpManager::updateState().

Here is the caller graph for this function:

◆ getState()

spinning_state_e RpmCalculator::getState ( ) const

This accessor is used in unit-tests.

Definition at line 203 of file rpm_calculator.cpp.

203 {
204 return state;
205}
spinning_state_e state

Referenced by configureRusefiLuaHooks().

Here is the caller graph for this function:

◆ isCranking()

bool RpmCalculator::isCranking ( ) const
overridevirtual

Returns true if the engine is cranking OR spinning up

Implements EngineRotationState.

Definition at line 36 of file rpm_calculator.cpp.

36 {
37 // Spinning-up with non-zero RPM is suitable for all engine math, as good as cranking
38 return state == CRANKING || (state == SPINNING_UP && cachedRpmValue > 0);
39}
@ SPINNING_UP
@ CRANKING

Referenced by IgnitionState::getAdvance(), getInjectionMass(), FanController::getState(), WallFuelController::onFastCallback(), EngineState::periodicFastCallback(), Engine::periodicSlowCallback(), showInfo(), and tle8888startup().

Here is the caller graph for this function:

◆ isRunning()

bool RpmCalculator::isRunning ( ) const

Returns true if the engine is running and not cranking

Returns
true if there was a full shaft revolution within the last second

Definition at line 111 of file rpm_calculator.cpp.

111 {
112 return state == RUNNING;
113}
@ RUNNING

Referenced by IdleController::determinePhase(), disengageStarterIfNeeded(), ShortTermFuelTrim::getCorrectionState(), FanController::getState(), TripOdometer::onSlowCallback(), Engine::periodicSlowCallback(), StepperMotorBase::setInitialPosition(), showInfo(), startKnockSampling(), startStopButtonToggle(), and LimpManager::updateState().

Here is the caller graph for this function:

◆ isSpinningUp()

bool RpmCalculator::isSpinningUp ( ) const

Returns true if the engine is spinning up

Definition at line 41 of file rpm_calculator.cpp.

41 {
42 return state == SPINNING_UP;
43}

Referenced by getCurrentIgnitionMode(), rpmShaftPositionCallback(), setSpinningUp(), and showInfo().

Here is the caller graph for this function:

◆ isStopped()

bool RpmCalculator::isStopped ( ) const
overridevirtual

Returns true if the engine is not spinning (RPM==0)

Implements EngineRotationState.

Definition at line 31 of file rpm_calculator.cpp.

31 {
32 // Spinning-up with zero RPM means that the engine is not ready yet, and is treated as 'stopped'.
33 return state == STOPPED || (state == SPINNING_UP && cachedRpmValue == 0);
34}
@ STOPPED

Referenced by doPeriodicSlowCallback(), executeTSCommand(), setSpinningUp(), showInfo(), slowStartStopButtonCallback(), startStopButtonToggle(), and storageAllowWriteID().

Here is the caller graph for this function:

◆ onNewEngineCycle()

void RpmCalculator::onNewEngineCycle ( )

This method is invoked once per engine cycle right after we calculate new RPM value

Definition at line 207 of file rpm_calculator.cpp.

Referenced by rpmShaftPositionCallback().

Here is the caller graph for this function:

◆ onSlowCallback()

void RpmCalculator::onSlowCallback ( )

Definition at line 216 of file rpm_calculator.cpp.

216 {
217 // Stop the engine if it's been too long since we got a trigger event
220 }
221}
efitick_t getTimeNowNt()
Definition efitime.cpp:19

Referenced by doPeriodicSlowCallback().

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

◆ setRpmValue()

void RpmCalculator::setRpmValue ( float  value)

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.

Definition at line 159 of file rpm_calculator.cpp.

159 {
160 if (value > MAX_ALLOWED_RPM) {
161 value = 0;
162 }
163
164 assignRpmValue(value);
165 spinning_state_e oldState = state;
166 // Change state
167 if (cachedRpmValue == 0) {
168 // Reset minCrankingRpm between attempts
169 minCrankingRpm = 0;
170 state = STOPPED;
172 if (state != RUNNING) {
173 // Store the time the engine started
174 engineStartTimer.reset();
175 }
176
177 state = RUNNING;
178 } else if (state == STOPPED || state == SPINNING_UP) {
179 /**
180 * We are here if RPM is above zero but we have not seen running RPM yet.
181 * This gives us cranking hysteresis - a drop of RPM during running is still running, not cranking.
182 */
183 if (value < minCrankingRpm || minCrankingRpm == 0)
184 minCrankingRpm = value;
185 state = CRANKING;
186 }
187#if EFI_ENGINE_CONTROL
188 // This presumably fixes injection mode change for cranking-to-running transition.
189 // 'isSimultaneous' flag should be updated for events if injection modes differ for cranking and running.
191 // Reset the state of all injectors: when we change fueling modes, we could
192 // immediately reschedule an injection that's currently underway. That will cause
193 // the injector's overlappingCounter to get out of sync with reality. As the fix,
194 // every injector's state is forcibly reset just before we could cause that to happen.
196
197 // reschedule all injection events now that we've reset them
199 }
200#endif
201}
FuelSchedule injectionEvents
Definition engine.h:296
static void resetOverlapping()
spinning_state_e

Referenced by rpmShaftPositionCallback().

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

◆ setSpinningUp()

void RpmCalculator::setSpinningUp ( efitick_t  nowNt)

Should be called on every trigger event when the engine is just starting to spin up.

Definition at line 239 of file rpm_calculator.cpp.

239 {
241 return;
242 // Only a completely stopped and non-spinning engine can enter the spinning-up state.
243 if (isStopped() && !isSpinning) {
246 isSpinning = true;
247 }
248 // update variables needed by early instant RPM calc.
251 }
252}
void setLastEventTimeForInstantRpm(efitick_t nowNt)
bool isSpinningUp() const
bool isStopped() const override
InstantRpmCalculator instantRpm
PrimaryTriggerDecoder triggerState
bool getShaftSynchronized() const

Referenced by Engine::OnTriggerStateProperState().

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

◆ setStopSpinning()

void RpmCalculator::setStopSpinning ( )

Called if the synchronization is lost due to a trigger timeout.

Definition at line 223 of file rpm_calculator.cpp.

223 {
224 isSpinning = false;
226 rpmRate = 0;
227
228 if (cachedRpmValue != 0) {
230 // needed by 'useNoiselessTriggerDecoder'
232 efiPrintf("engine stopped");
233 }
234 state = STOPPED;
235
237}
void onEngineStopped()
Definition engine.cpp:581
TriggerNoiseFilter noiseFilter

Referenced by onSlowCallback(), and Engine::OnTriggerSynchronizationLost().

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

◆ showInfo()

void RpmCalculator::showInfo ( const char sensorName) const
overrideprotectedvirtual

Implements Sensor.

Definition at line 66 of file sensor_info_printing.cpp.

66 {
67#if EFI_SHAFT_POSITION_INPUT
68 efiPrintf("RPM sensor: stopped: %d spinning up: %d cranking: %d running: %d rpm: %f",
69 isStopped(),
71 isCranking(),
72 isRunning(),
73 get().value_or(0)
74 );
75#endif // EFI_SHAFT_POSITION_INPUT
76}
bool isRunning() const
bool isCranking() const override
SensorResult get() const final override
Here is the call graph for this function:

Field Documentation

◆ cachedRpmValue

float RpmCalculator::cachedRpmValue = 0
private

At this point this value is same exact value as in private m_value variable At this point all this is performance optimization? Open question is when do we need it for performance reasons.

Definition at line 143 of file rpm_calculator.h.

Referenced by assignRpmValue(), getCachedRpm(), isCranking(), isStopped(), setRpmValue(), and setStopSpinning().

◆ engineStartTimer

Timer RpmCalculator::engineStartTimer
private

Definition at line 168 of file rpm_calculator.h.

Referenced by getSecondsSinceEngineStart(), and setRpmValue().

◆ isSpinning

bool RpmCalculator::isSpinning = false
private

True if the engine is spinning (regardless of its state), i.e. if shaft position changes. Needed by spinning-up logic.

Definition at line 166 of file rpm_calculator.h.

Referenced by setSpinningUp(), and setStopSpinning().

◆ lastTdcTimer

Timer RpmCalculator::lastTdcTimer

◆ minCrankingRpm

float RpmCalculator::minCrankingRpm = 0
private

The slowest RPM encountered during cranking, used for interpolating ignition advance

Definition at line 148 of file rpm_calculator.h.

Referenced by getMinCrankingRpm(), and setRpmValue().

◆ oneDegreeUs

floatus_t RpmCalculator::oneDegreeUs = NAN

This is a performance optimization: let's pre-calculate this each time RPM changes NaN while engine is not spinning

Definition at line 122 of file rpm_calculator.h.

Referenced by assignRpmValue(), TriggerCentral::getCurrentEnginePhase(), getOneDegreeUs(), IgnitionState::getSparkHardwareLatencyCorrection(), scheduleByAngle(), and startKnockSampling().

◆ previousRpmValue

float RpmCalculator::previousRpmValue = 0

this is RPM on previous engine cycle.

Definition at line 116 of file rpm_calculator.h.

Referenced by assignRpmValue(), and rpmShaftPositionCallback().

◆ revolutionCounterSinceBoot

uint32_t RpmCalculator::revolutionCounterSinceBoot = 0
private

This counter is incremented with each revolution of one of the shafts. Could be crankshaft could be camshaft.

Definition at line 154 of file rpm_calculator.h.

Referenced by getRevolutionCounterM(), and onNewEngineCycle().

◆ revolutionCounterSinceStart

uint32_t RpmCalculator::revolutionCounterSinceStart = 0
private

Same as the above, but since the engine started spinning

Definition at line 158 of file rpm_calculator.h.

Referenced by getRevolutionCounterSinceStart(), onNewEngineCycle(), and setStopSpinning().

◆ rpmRate

float RpmCalculator::rpmRate = 0

Definition at line 131 of file rpm_calculator.h.

Referenced by getRpmAcceleration(), rpmShaftPositionCallback(), and setStopSpinning().

◆ state

spinning_state_e RpmCalculator::state = STOPPED
private

The documentation for this class was generated from the following files: