fome-fw/firmware/controllers/trigger/rpm_calculator.h

184 lines
5.0 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
2017-01-03 03:05:22 -08:00
* @author Andrey Belomutskiy, (c) 2012-2017
2015-07-10 06:01:56 -07:00
*/
#ifndef RPM_REPORTER_H_
#define RPM_REPORTER_H_
#include <global.h>
#include "scheduler.h"
#define TOP_DEAD_CENTER_MESSAGE "r"
#define WC_DOWN "d"
#define WC_UP "u"
/**
* See also 'CRANK1' in java dev console code
*/
#define CRANK1 "t1"
#define CRANK2 "t2"
#define CRANK3 "t3"
2016-08-23 20:03:01 -07:00
#define VVT_NAME "VVT"
2015-07-10 06:01:56 -07:00
#define NOISY_RPM -1
#define UNREALISTIC_RPM 30000
#ifndef RPM_LOW_THRESHOLD
2018-03-11 19:55:41 -07:00
// no idea what is the best value, 25 is as good as any other guess
#define RPM_LOW_THRESHOLD 25
#endif
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
2015-07-10 06:01:56 -07:00
class RpmCalculator {
public:
#if !EFI_PROD_CODE
int mockRpm;
2017-07-06 18:21:45 -07:00
#endif /* EFI_PROD_CODE */
2015-07-10 06:01:56 -07:00
RpmCalculator();
2017-07-06 05:35:09 -07:00
/**
* Returns true if the engine is not spinning (RPM==0)
*/
2017-07-06 05:43:15 -07:00
bool isStopped(DECLARE_ENGINE_PARAMETER_SIGNATURE);
2017-07-06 05:35:09 -07:00
/**
* Returns true if the engine is spinning up
*/
bool isSpinningUp(DECLARE_ENGINE_PARAMETER_SIGNATURE);
/**
* Returns true if the engine is cranking OR spinning up
2017-07-06 05:35:09 -07:00
*/
2017-07-06 05:43:15 -07:00
bool isCranking(DECLARE_ENGINE_PARAMETER_SIGNATURE);
2017-07-06 05:35:09 -07:00
/**
* Returns true if the engine is running and not cranking
*/
2017-05-15 20:33:22 -07:00
bool isRunning(DECLARE_ENGINE_PARAMETER_SIGNATURE);
2017-07-06 16:33:25 -07:00
bool checkIfSpinning(DECLARE_ENGINE_PARAMETER_SIGNATURE);
/**
* This accessor is used in unit-tests.
*/
spinning_state_e getState(void);
/**
* Should be called on every trigger event when the engine is just starting to spin up.
*/
void setSpinningUp(efitime_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX);
/**
* Called if the synchronization is lost due to a trigger timeout.
*/
void setStopSpinning(DECLARE_ENGINE_PARAMETER_SIGNATURE);
2019-01-21 17:33:21 -08:00
/**
* Just a getter for rpmValue
2019-01-21 18:48:58 -08:00
* Also handles mockRpm if not EFI_PROD_CODE
2019-01-21 17:33:21 -08:00
*/
int getRpm(DECLARE_ENGINE_PARAMETER_SIGNATURE) 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 getRevolutionCounter(void);
2017-05-15 20:33:22 -07:00
void setRpmValue(int value DECLARE_ENGINE_PARAMETER_SUFFIX);
/**
* The same as setRpmValue() but without state change.
* We need this to be public because of calling rpmState->assignRpmValue() from rpmShaftPositionCallback()
*/
void assignRpmValue(int value DECLARE_ENGINE_PARAMETER_SUFFIX);
2015-07-10 06:01:56 -07:00
uint32_t getRevolutionCounterSinceStart(void);
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
*/
2015-07-10 06:01:56 -07:00
float getRpmAcceleration();
/**
* This is public because sometimes we cannot afford to call isRunning() and the value is good enough
* Zero if engine is not running
*/
2019-01-13 20:20:19 -08:00
volatile int rpmValue = 0;
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;
2019-01-13 20:55:10 -08:00
volatile efitime_t lastRpmEventTimeNt = 0;
2015-07-10 06:01:56 -07:00
private:
2017-07-07 04:20:04 -07:00
/**
* Should be called once we've realized engine is not spinning any more.
*/
void setStopped(DECLARE_ENGINE_PARAMETER_SIGNATURE);
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;
2015-07-10 06:01:56 -07:00
};
2019-01-21 18:48:58 -08:00
// Just a getter for rpmValue which also handles mockRpm if not EFI_PROD_CODE
#define GET_RPM() ( ENGINE(rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_SIGNATURE)) )
2015-07-10 06:01:56 -07:00
2019-01-21 18:48:58 -08:00
// simple variable access, theoretically could be faster than getter method but that's a long stretch
2019-01-21 17:33:21 -08:00
#define GET_RPM_VALUE ( ENGINE(rpmCalculator.rpmValue) )
#define isValidRpm(rpm) ((rpm) > 0 && (rpm) < UNREALISTIC_RPM)
2017-05-15 20:33:22 -07:00
void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index DECLARE_ENGINE_PARAMETER_SUFFIX);
2015-07-10 06:01:56 -07:00
/**
* @brief Initialize RPM calculator
*/
2019-01-20 21:10:09 -08:00
void initRpmCalculator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX);
2015-07-10 06:01:56 -07:00
2017-05-15 20:33:22 -07:00
float getCrankshaftAngleNt(efitime_t timeNt DECLARE_ENGINE_PARAMETER_SUFFIX);
2015-07-10 06:01:56 -07:00
int getRevolutionCounter(void);
2015-07-15 18:01:45 -07:00
#if EFI_ENGINE_SNIFFER
2018-09-10 19:29:43 -07:00
#define addEngineSnifferEvent(name, msg) if (ENGINE(isEngineChartEnabled)) { 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
void scheduleByAngle(int rpm, scheduling_s *timer, angle_t angle, schfunc_t callback, void *param, RpmCalculator *calc DECLARE_ENGINE_PARAMETER_SUFFIX);
2015-07-10 06:01:56 -07:00
#endif /* RPM_REPORTER_H_ */