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
|
|
|
|
|
|
|
#include "scheduler.h"
|
2020-09-03 20:27:53 -07:00
|
|
|
#include "stored_value_sensor.h"
|
2021-02-28 15:56:34 -08:00
|
|
|
#include "timer.h"
|
2022-09-04 23:28:46 -07:00
|
|
|
#include "rpm_calculator_api.h"
|
2022-09-10 22:59:44 -07:00
|
|
|
#include "trigger_decoder.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-06-25 15:20:01 -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
|
|
|
|
2022-01-20 21:56:13 -08:00
|
|
|
/**
|
|
|
|
* Most consumers should access value via Sensor framework by SensorType::Rpm key
|
|
|
|
*/
|
2022-09-04 23:28:46 -07:00
|
|
|
class RpmCalculator : public StoredValueSensor, public EngineRotationState {
|
2015-07-10 06:01:56 -07:00
|
|
|
public:
|
|
|
|
RpmCalculator();
|
2021-11-20 03:59:02 -08:00
|
|
|
|
2022-09-04 23:59:38 -07:00
|
|
|
operation_mode_e getOperationMode() const override;
|
|
|
|
|
2021-11-20 03:59:02 -08:00
|
|
|
void onSlowCallback();
|
|
|
|
|
2017-07-06 05:35:09 -07:00
|
|
|
/**
|
|
|
|
* Returns true if the engine is not spinning (RPM==0)
|
|
|
|
*/
|
2022-11-27 07:11:14 -08:00
|
|
|
bool isStopped() const override;
|
2017-07-06 05:35:09 -07:00
|
|
|
/**
|
2018-03-10 17:58:51 -08:00
|
|
|
* Returns true if the engine is spinning up
|
|
|
|
*/
|
2020-09-05 15:49:42 -07:00
|
|
|
bool isSpinningUp() const;
|
2018-03-10 17:58:51 -08:00
|
|
|
/**
|
|
|
|
* Returns true if the engine is cranking OR spinning up
|
2017-07-06 05:35:09 -07:00
|
|
|
*/
|
2022-11-27 07:11:14 -08:00
|
|
|
bool isCranking() const override;
|
2017-07-06 05:35:09 -07:00
|
|
|
/**
|
|
|
|
* Returns true if the engine is running and not cranking
|
|
|
|
*/
|
2020-09-05 15:49:42 -07:00
|
|
|
bool isRunning() const;
|
2017-07-06 16:33:25 -07:00
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
bool checkIfSpinning(efitick_t nowNt) const;
|
2020-09-03 20:27:53 -07:00
|
|
|
|
2018-03-10 17:58:51 -08:00
|
|
|
/**
|
|
|
|
* This accessor is used in unit-tests.
|
|
|
|
*/
|
2019-01-24 20:44:29 -08:00
|
|
|
spinning_state_e getState() const;
|
2018-03-10 17:58:51 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Should be called on every trigger event when the engine is just starting to spin up.
|
|
|
|
*/
|
2020-09-05 15:49:42 -07:00
|
|
|
void setSpinningUp(efitick_t nowNt );
|
2018-03-10 17:58:51 -08:00
|
|
|
/**
|
|
|
|
* Called if the synchronization is lost due to a trigger timeout.
|
|
|
|
*/
|
2020-09-05 15:49:42 -07:00
|
|
|
void setStopSpinning();
|
2018-03-10 17:58:51 -08:00
|
|
|
|
2019-01-21 17:33:21 -08:00
|
|
|
/**
|
2022-01-20 21:56:13 -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
|
|
|
*/
|
2022-01-20 21:56:13 -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();
|
2019-10-14 03:18:08 -07:00
|
|
|
uint32_t getRevolutionCounterM(void) const;
|
2020-09-05 15:49:42 -07:00
|
|
|
void setRpmValue(float value);
|
2018-03-10 17:58:51 -08:00
|
|
|
/**
|
|
|
|
* The same as setRpmValue() but without state change.
|
|
|
|
* We need this to be public because of calling rpmState->assignRpmValue() from rpmShaftPositionCallback()
|
|
|
|
*/
|
2020-09-05 15:49:42 -07:00
|
|
|
void assignRpmValue(float value);
|
2019-06-08 06:51:36 -07:00
|
|
|
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
|
|
|
|
*/
|
2019-06-08 06:51:36 -07:00
|
|
|
float getRpmAcceleration() const;
|
2021-06-16 14:20:28 -07:00
|
|
|
|
2022-04-10 16:22:39 -07:00
|
|
|
// Get elapsed time since the engine transitioned to the running state.
|
|
|
|
float getSecondsSinceEngineStart(efitick_t nowNt) const;
|
2021-06-16 14:20:28 -07:00
|
|
|
|
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;
|
2022-09-04 23:28:46 -07:00
|
|
|
|
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
|
|
|
*/
|
2022-11-27 07:11:14 -08:00
|
|
|
floatus_t oneDegreeUs = NAN;
|
2021-02-28 15:56:34 -08:00
|
|
|
|
2022-09-04 23:28:46 -07:00
|
|
|
floatus_t getOneDegreeUs() override {
|
|
|
|
return oneDegreeUs;
|
|
|
|
}
|
|
|
|
|
2021-02-28 15:56:34 -08:00
|
|
|
Timer lastTdcTimer;
|
2020-09-05 15:49:42 -07:00
|
|
|
|
2021-01-09 17:06:19 -08:00
|
|
|
// RPM rate of change, in RPM per second
|
|
|
|
float rpmRate = 0;
|
|
|
|
|
2020-09-05 15:49:42 -07:00
|
|
|
protected:
|
|
|
|
// Print sensor info - current RPM state
|
2021-04-18 17:02:32 -07:00
|
|
|
void showInfo(const char* sensorName) const override;
|
2020-09-05 15:49:42 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
private:
|
2020-09-03 16:51:50 -07:00
|
|
|
/**
|
2022-01-20 21:56:13 -08: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
|
|
|
*/
|
2022-01-20 21:56:13 -08:00
|
|
|
float cachedRpmValue = 0;
|
2021-01-09 17:06:19 -08:00
|
|
|
|
2017-07-07 04:20:04 -07:00
|
|
|
/**
|
|
|
|
* Should be called once we've realized engine is not spinning any more.
|
|
|
|
*/
|
2020-09-05 15:49:42 -07:00
|
|
|
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.
|
|
|
|
*/
|
2022-11-27 07:11:14 -08:00
|
|
|
uint32_t revolutionCounterSinceBoot = 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Same as the above, but since the engine started spinning
|
|
|
|
*/
|
2022-11-27 07:11:14 -08:00
|
|
|
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;
|
2018-03-10 17:58:51 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2021-06-16 14:20:28 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-09-07 20:24:24 -07:00
|
|
|
void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex, efitick_t edgeTimestamp);
|
2020-10-03 22:43:02 -07:00
|
|
|
|
2020-10-03 23:09:12 -07:00
|
|
|
void tdcMarkCallback(
|
2022-09-07 20:24:24 -07:00
|
|
|
uint32_t trgEventIndex, efitick_t edgeTimestamp);
|
2020-10-03 22:43:02 -07:00
|
|
|
|
2022-09-13 22:53:17 -07:00
|
|
|
operation_mode_e lookupOperationMode();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-11-17 10:45:10 -08:00
|
|
|
#define getRevolutionCounter() (engine->rpmCalculator.getRevolutionCounterM())
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
efitick_t scheduleByAngle(scheduling_s *timer, efitick_t edgeTimestamp, angle_t angle, action_s action);
|
2015-07-10 06:01:56 -07:00
|
|
|
|