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
 
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
 
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 98 of file rpm_calculator.cpp.

98 :
100 {
102}
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 134 of file rpm_calculator.cpp.

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

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 114 of file rpm_calculator.cpp.

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

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:

◆ getOneDegreeUs()

floatus_t RpmCalculator::getOneDegreeUs ( )
inlineoverridevirtual

Implements EngineRotationState.

Definition at line 121 of file rpm_calculator.h.

121 {
122 return oneDegreeUs;
123 }

◆ getOperationMode()

operation_mode_e RpmCalculator::getOperationMode ( ) const
overridevirtual

Implements EngineRotationState.

Definition at line 81 of file rpm_calculator.cpp.

81 {
82#if EFI_SHAFT_POSITION_INPUT
83 // Ignore user-provided setting for well known triggers.
85 // For example for Miata NA, there is no reason to allow user to set FOUR_STROKE_CRANK_SENSOR
87 } else
88#endif // EFI_SHAFT_POSITION_INPUT
89 {
90 // For example 36-1, could be on either cam or crank, so we have to ask the user
91 return lookupOperationMode();
92 }
93}
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 204 of file rpm_calculator.cpp.

204 {
206}
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 323 of file rpm_calculator.cpp.

323 {
324 return engineStartTimer.getElapsedSeconds(nowNt);
325}

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 195 of file rpm_calculator.cpp.

195 {
196 return state;
197}
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 107 of file rpm_calculator.cpp.

107 {
108 return state == RUNNING;
109}
@ 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 199 of file rpm_calculator.cpp.

Referenced by rpmShaftPositionCallback().

Here is the caller graph for this function:

◆ onSlowCallback()

void RpmCalculator::onSlowCallback ( )

Definition at line 208 of file rpm_calculator.cpp.

208 {
209 // Stop the engine if it's been too long since we got a trigger event
212 }
213}
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 155 of file rpm_calculator.cpp.

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

231 {
233 return;
234 // Only a completely stopped and non-spinning engine can enter the spinning-up state.
235 if (isStopped() && !isSpinning) {
238 isSpinning = true;
239 }
240 // update variables needed by early instant RPM calc.
243 }
244}
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 215 of file rpm_calculator.cpp.

215 {
216 isSpinning = false;
218 rpmRate = 0;
219
220 if (cachedRpmValue != 0) {
222 // needed by 'useNoiselessTriggerDecoder'
224 efiPrintf("engine stopped");
225 }
226 state = STOPPED;
227
229}
void onEngineStopped()
Definition engine.cpp:569
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 140 of file rpm_calculator.h.

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

◆ engineStartTimer

Timer RpmCalculator::engineStartTimer
private

Definition at line 160 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 158 of file rpm_calculator.h.

Referenced by setSpinningUp(), and setStopSpinning().

◆ lastTdcTimer

Timer RpmCalculator::lastTdcTimer

◆ 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 119 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 113 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 146 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 150 of file rpm_calculator.h.

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

◆ rpmRate

float RpmCalculator::rpmRate = 0

Definition at line 128 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: