rusEFI
The most advanced open source ECU
Public Member Functions | Data Fields | Protected Member Functions | Private 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, efitick_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 char * getSensorName () const
 
virtual bool hasSensor () const
 
virtual float getRaw () const
 
virtual bool isRedundant () const
 
void unregister ()
 
SensorType type () const
 

Data Fields

int 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 Member Functions

void setStopped ()
 

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 setMockValue (int type, float value)
 
static void resetMockValue (SensorType type)
 
static void resetAllMocks ()
 
static void inhibitTimeouts (bool inhibit)
 
static const char * getSensorName (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 44 of file rpm_calculator.h.

Constructor & Destructor Documentation

◆ RpmCalculator()

RpmCalculator::RpmCalculator ( )

Definition at line 106 of file rpm_calculator.cpp.

106  :
108  {
109  assignRpmValue(0);
110 }
void assignRpmValue(float value)
StoredValueSensor(SensorType type, efitick_t timeoutNt)
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 142 of file rpm_calculator.cpp.

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

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

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

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

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.

Returns
-1 in case of isNoisySignal(), current RPM otherwise See NOISY_RPM

Definition at line 57 of file rpm_calculator.cpp.

57  {
58  return cachedRpmValue;
59 }

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 123 of file rpm_calculator.h.

123  {
124  return oneDegreeUs;
125  }

◆ getOperationMode()

operation_mode_e RpmCalculator::getOperationMode ( ) const
overridevirtual

Implements EngineRotationState.

Definition at line 89 of file rpm_calculator.cpp.

89  {
90 #if EFI_SHAFT_POSITION_INPUT
91  // Ignore user-provided setting for well known triggers.
93  // For example for Miata NA, there is no reason to allow user to set FOUR_STROKE_CRANK_SENSOR
95  } else
96 #endif // EFI_SHAFT_POSITION_INPUT
97  {
98  // For example 36-1, could be on either cam or crank, so we have to ask the user
99  return lookupOperationMode();
100  }
101 }
TriggerWaveform triggerShape
operation_mode_e getWheelOperationMode() const
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 208 of file rpm_calculator.cpp.

208  {
210 }
uint32_t revolutionCounterSinceBoot

◆ getRevolutionCounterSinceStart()

uint32_t RpmCalculator::getRevolutionCounterSinceStart ( void  ) const

Definition at line 49 of file rpm_calculator.cpp.

49  {
51 }
uint32_t revolutionCounterSinceStart

Referenced by getCrankingFuel(), IdleController::getCrankingTaperFraction(), EngineState::periodicFastCallback(), 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 31 of file rpm_calculator.cpp.

31  {
32  return rpmRate;
33 }

Referenced by updateTunerStudioState().

Here is the caller graph for this function:

◆ getSecondsSinceEngineStart()

float RpmCalculator::getSecondsSinceEngineStart ( efitick_t  nowNt) const

Definition at line 337 of file rpm_calculator.cpp.

337  {
338  return engineStartTimer.getElapsedSeconds(nowNt);
339 }
Timer engineStartTimer

Referenced by VvtController::getSetpoint(), 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 199 of file rpm_calculator.cpp.

199  {
200  return state;
201 }
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 40 of file rpm_calculator.cpp.

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

Referenced by getAdvance(), getInjectionMass(), IgnitionState::getSparkDwell(), FanController::getState(), WallFuelController::onFastCallback(), EngineState::periodicFastCallback(), 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 115 of file rpm_calculator.cpp.

115  {
116  return state == RUNNING;
117 }
@ RUNNING

Referenced by IdleController::determinePhase(), disengageStarterIfNeeded(), FanController::getState(), TripOdometer::onSlowCallback(), onStartStopButtonToggle(), Engine::periodicSlowCallback(), StepperMotorBase::setInitialPosition(), shouldCorrect(), showInfo(), startKnockSampling(), 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 45 of file rpm_calculator.cpp.

45  {
46  return state == SPINNING_UP;
47 }

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

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

Referenced by doPeriodicSlowCallback(), executeTSCommand(), onStartStopButtonToggle(), setSpinningUp(), and showInfo().

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

203  {
206 }

Referenced by rpmShaftPositionCallback().

Here is the caller graph for this function:

◆ onSlowCallback()

void RpmCalculator::onSlowCallback ( )

Definition at line 212 of file rpm_calculator.cpp.

212  {
213  // Stop the engine if it's been too long since we got a trigger event
215  setStopSpinning();
216  }
217 }
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 163 of file rpm_calculator.cpp.

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

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

Referenced by Engine::OnTriggerStateProperState().

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

◆ setStopped()

void RpmCalculator::setStopped ( )
private

Should be called once we've realized engine is not spinning any more.

Definition at line 219 of file rpm_calculator.cpp.

219  {
221 
222  rpmRate = 0;
223 
224  if (cachedRpmValue != 0) {
225  assignRpmValue(0);
226  // needed by 'useNoiselessTriggerDecoder'
228  efiPrintf("engine stopped");
229  }
230  state = STOPPED;
231 }
TriggerNoiseFilter noiseFilter

Referenced by setStopSpinning().

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

233  {
234  isSpinning = false;
235  setStopped();
236 }

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 60 of file sensor_info_printing.cpp.

60  {
61 #if EFI_SHAFT_POSITION_INPUT
62  efiPrintf("RPM sensor: stopped: %d spinning up: %d cranking: %d running: %d rpm: %f",
63  isStopped(),
64  isSpinningUp(),
65  isCranking(),
66  isRunning(),
67  get().value_or(0)
68  );
69 #endif // EFI_SHAFT_POSITION_INPUT
70 }
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 142 of file rpm_calculator.h.

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

◆ engineStartTimer

Timer RpmCalculator::engineStartTimer
private

Definition at line 167 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 165 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 121 of file rpm_calculator.h.

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

◆ previousRpmValue

int RpmCalculator::previousRpmValue = 0

this is RPM on previous engine cycle.

Definition at line 115 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 153 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 157 of file rpm_calculator.h.

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

◆ rpmRate

float RpmCalculator::rpmRate = 0

Definition at line 130 of file rpm_calculator.h.

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

◆ state

spinning_state_e RpmCalculator::state = STOPPED
private

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