rusefi-1/firmware/controllers/engine_cycle/rpm_calculator.h

184 lines
4.6 KiB
C
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file rpm_calculator.h
* @brief Shaft position sensor(s) decoder header
*
* @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
*/
2019-12-23 17:19:13 -08:00
#pragma once
2015-07-10 06:01:56 -07:00
2019-11-19 22:35:08 -08:00
#include "globalaccess.h"
2015-07-10 06:01:56 -07:00
#include "scheduler.h"
2020-09-03 20:27:53 -07:00
#include "stored_value_sensor.h"
#include "timer.h"
2015-07-10 06:01:56 -07:00
// we use this value in case of noise on trigger input lines
2015-07-10 06:01:56 -07:00
#define NOISY_RPM -1
#define UNREALISTIC_RPM 30000
2017-07-06 04:27:32 -07:00
typedef enum {
/**
* The engine is not spinning, RPM=0
*/
STOPPED,
2018-03-04 10:23:44 -08:00
/**
* The engine is spinning up (reliable RPM is not detected yet).
* In this state, rpmValue is >= 0 (can be zero).
*/
SPINNING_UP,
2017-07-06 04:27:32 -07:00
/**
* The engine is cranking (0 < RPM < cranking.rpm)
*/
CRANKING,
/**
* The engine is running (RPM >= cranking.rpm)
*/
RUNNING,
2017-07-07 04:20:04 -07:00
} spinning_state_e;
2017-07-06 04:27:32 -07:00
/**
* Most consumers should access value via Sensor framework by SensorType::Rpm key
*/
class RpmCalculator : public StoredValueSensor {
2015-07-10 06:01:56 -07:00
public:
RpmCalculator();
void onSlowCallback();
2017-07-06 05:35:09 -07:00
/**
* Returns true if the engine is not spinning (RPM==0)
*/
bool isStopped() const;
2017-07-06 05:35:09 -07:00
/**
* Returns true if the engine is spinning up
*/
bool isSpinningUp() const;
/**
* Returns true if the engine is cranking OR spinning up
2017-07-06 05:35:09 -07:00
*/
bool isCranking() const;
2017-07-06 05:35:09 -07:00
/**
* Returns true if the engine is running and not cranking
*/
bool isRunning() const;
2017-07-06 16:33:25 -07:00
bool checkIfSpinning(efitick_t nowNt) const;
2020-09-03 20:27:53 -07:00
/**
* This accessor is used in unit-tests.
*/
2019-01-24 20:44:29 -08:00
spinning_state_e getState() const;
/**
* Should be called on every trigger event when the engine is just starting to spin up.
*/
void setSpinningUp(efitick_t nowNt );
/**
* Called if the synchronization is lost due to a trigger timeout.
*/
void setStopSpinning();
2019-01-21 17:33:21 -08:00
/**
* 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.
2019-01-21 17:33:21 -08:00
*/
float getCachedRpm() const;
2015-07-10 06:01:56 -07:00
/**
* This method is invoked once per engine cycle right after we calculate new RPM value
*/
void onNewEngineCycle();
uint32_t getRevolutionCounterM(void) const;
void setRpmValue(float value);
/**
* The same as setRpmValue() but without state change.
* We need this to be public because of calling rpmState->assignRpmValue() from rpmShaftPositionCallback()
*/
void assignRpmValue(float value);
uint32_t getRevolutionCounterSinceStart(void) const;
2017-12-03 20:58:48 -08:00
/**
* RPM rate of change between current RPM and RPM measured during previous engine cycle
* see also SC_RPM_ACCEL
*/
float getRpmAcceleration() const;
// Get elapsed time since the engine transitioned to the running state.
float getSecondsSinceEngineStart(efitick_t nowNt) const;
2017-12-03 20:58:48 -08:00
/**
* this is RPM on previous engine cycle.
*/
2019-01-13 20:20:19 -08:00
int previousRpmValue = 0;
2015-07-10 06:01:56 -07:00
/**
2017-12-03 20:58:48 -08:00
* This is a performance optimization: let's pre-calculate this each time RPM changes
2017-08-11 10:49:46 -07:00
* NaN while engine is not spinning
2015-07-10 06:01:56 -07:00
*/
2019-01-13 20:20:19 -08:00
volatile floatus_t oneDegreeUs = NAN;
Timer lastTdcTimer;
// RPM rate of change, in RPM per second
float rpmRate = 0;
protected:
// Print sensor info - current RPM state
void showInfo(const char* sensorName) const override;
2015-07-10 06:01:56 -07:00
private:
2020-09-03 16:51:50 -07:00
/**
* 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.
2020-09-03 16:51:50 -07:00
*/
float cachedRpmValue = 0;
2017-07-07 04:20:04 -07:00
/**
* Should be called once we've realized engine is not spinning any more.
*/
void setStopped();
2017-07-07 04:20:04 -07:00
2015-07-10 06:01:56 -07:00
/**
* This counter is incremented with each revolution of one of the shafts. Could be
* crankshaft could be camshaft.
*/
2019-01-13 20:20:19 -08:00
volatile uint32_t revolutionCounterSinceBoot = 0;
2015-07-10 06:01:56 -07:00
/**
* Same as the above, but since the engine started spinning
*/
2019-01-13 20:20:19 -08:00
volatile uint32_t revolutionCounterSinceStart = 0;
2017-07-07 04:20:04 -07:00
2019-01-13 20:20:19 -08:00
spinning_state_e state = STOPPED;
/**
* True if the engine is spinning (regardless of its state), i.e. if shaft position changes.
* Needed by spinning-up logic.
*/
2019-01-13 20:20:19 -08:00
bool isSpinning = false;
Timer engineStartTimer;
2015-07-10 06:01:56 -07:00
};
2019-01-21 17:33:21 -08:00
#define isValidRpm(rpm) ((rpm) > 0 && (rpm) < UNREALISTIC_RPM)
void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, efitick_t edgeTimestamp);
2020-10-03 23:09:12 -07:00
void tdcMarkCallback(
uint32_t index0, efitick_t edgeTimestamp);
2015-07-10 06:01:56 -07:00
/**
* @brief Initialize RPM calculator
*/
void initRpmCalculator();
2015-07-10 06:01:56 -07:00
#define getRevolutionCounter() (engine->rpmCalculator.getRevolutionCounterM())
2015-07-10 06:01:56 -07:00
2015-07-15 18:01:45 -07:00
#if EFI_ENGINE_SNIFFER
#define addEngineSnifferEvent(name, msg) { if (engine->isEngineSnifferEnabled) { waveChart.addEvent3((name), (msg)); } }
2015-07-10 06:01:56 -07:00
#else
2018-09-10 19:29:43 -07:00
#define addEngineSnifferEvent(n, msg) {}
2015-07-15 18:01:45 -07:00
#endif /* EFI_ENGINE_SNIFFER */
2015-07-10 06:01:56 -07:00
efitick_t scheduleByAngle(scheduling_s *timer, efitick_t edgeTimestamp, angle_t angle, action_s action);
2015-07-10 06:01:56 -07:00