auto-sync

This commit is contained in:
rusEfi 2014-11-11 15:05:09 -06:00
parent ef09927db9
commit 00add33207
17 changed files with 61 additions and 64 deletions

View File

@ -316,7 +316,7 @@ static void showFuelInfo2(float rpm, float engineLoad, Engine *engine) {
scheduleMsg(&logger2, "cranking fuel: %f", getCrankingFuel(engine));
if (engine->rpmCalculator->isRunning()) {
if (engine->rpmCalculator.isRunning()) {
float iatCorrection = getIatCorrection(getIntakeAirTemperature(engine) PASS_ENGINE_PARAMETER);
float cltCorrection = getCltCorrection(getCoolantTemperature(engine) PASS_ENGINE_PARAMETER);
float injectorLag = getInjectorLag(getVBatt(engineConfiguration) PASS_ENGINE_PARAMETER);

View File

@ -38,7 +38,8 @@ void Engine::onTriggerEvent(uint64_t nowNt) {
}
Engine::Engine() {
rpmCalculator = NULL;
lastTriggerEventTimeNt = 0;
isCylinderCleanupMode = false;
}
void Engine::init() {
@ -120,7 +121,7 @@ void StartupFuelPumping::setPumpsCounter(engine_configuration_s *engineConfigura
void StartupFuelPumping::update(Engine *engine) {
engine_configuration_s *engineConfiguration = engine->engineConfiguration;
if (engine->rpmCalculator->rpm() == 0) {
if (engine->rpmCalculator.rpm() == 0) {
bool isTpsAbove50 = getTPS(engineConfiguration) >= 50;
if (this->isTpsAbove50 != isTpsAbove50) {

View File

@ -10,6 +10,7 @@
#include "main.h"
#include "engine_configuration.h"
#include "ec2.h"
#include "rpm_calculator.h"
class EngineState {
public:
@ -28,7 +29,7 @@ class Engine {
public:
Engine();
void init();
RpmCalculator *rpmCalculator;
RpmCalculator rpmCalculator;
engine_configuration_s *engineConfiguration;
engine_configuration2_s *engineConfiguration2;

View File

@ -156,7 +156,7 @@ float getBaseTableFuel(engine_configuration_s *engineConfiguration, int rpm, flo
*/
float getCrankingFuel(Engine *engine) {
return getCrankingFuel3(engine->engineConfiguration, getCoolantTemperature(engine),
engine->rpmCalculator->getRevolutionCounterSinceStart()
engine->rpmCalculator.getRevolutionCounterSinceStart()
);
}

View File

@ -37,7 +37,7 @@ float getLEValue(Engine *engine, le_action_e action) {
case LE_METHOD_INTAKE_AIR:
return getIntakeAirTemperature(engine);
case LE_METHOD_RPM:
return engine->rpmCalculator->rpm();
return engine->rpmCalculator.rpm();
case LE_METHOD_TIME_SINCE_BOOT:
return getTimeNowSeconds();
case LE_METHOD_FAN_OFF_SETTING:

View File

@ -220,7 +220,7 @@ static void onEvenyGeneralMilliseconds(Engine *engine) {
*/
halTime.get(hal_lld_get_counter_value(), true);
if (!engine->rpmCalculator->isRunning())
if (!engine->rpmCalculator.isRunning())
writeToFlashIfPending();
engine->watchdog();
@ -245,7 +245,7 @@ static void onEvenyGeneralMilliseconds(Engine *engine) {
cylinderCleanupControl(engine);
setOutputPinValue(O2_HEATER, engine->rpmCalculator->isRunning());
setOutputPinValue(O2_HEATER, engine->rpmCalculator.isRunning());
// schedule next invocation
chVTSetAny(&everyMsTimer, boardConfiguration->generalPeriodicThreadPeriod * TICKS_IN_MS,

View File

@ -100,7 +100,7 @@ void mapAveragingCallback(adcsample_t value) {
#if EFI_ANALOG_CHART
if (engineConfiguration->analogChartMode == AC_MAP)
if (perRevolutionCounter % FAST_MAP_CHART_SKIP_FACTOR == 0)
acAddData(getCrankshaftAngleNt(getTimeNowNt()), currentPressure);
acAddData(getCrankshaftAngleNt(engine, getTimeNowNt()), currentPressure);
#endif /* EFI_ANALOG_CHART */
chSysLockFromIsr()

View File

@ -143,14 +143,14 @@ static INLINE void handleSparkEvent(uint32_t eventIndex, IgnitionEvent *iEvent,
return;
}
float sparkDelay = getOneDegreeTimeMs(rpm) * iEvent->dwellPosition.angleOffset;
int isIgnitionError = sparkDelay < 0;
float sparkDelayUs = engine->rpmCalculator.oneDegreeUs * iEvent->dwellPosition.angleOffset;
int isIgnitionError = sparkDelayUs < 0;
ignitionErrorDetection.add(isIgnitionError);
if (isIgnitionError) {
#if EFI_PROD_CODE
scheduleMsg(&logger, "Negative spark delay=%f", sparkDelay);
scheduleMsg(&logger, "Negative spark delay=%f", sparkDelayUs);
#endif
sparkDelay = 0;
sparkDelayUs = 0;
return;
}
@ -169,7 +169,7 @@ static INLINE void handleSparkEvent(uint32_t eventIndex, IgnitionEvent *iEvent,
/**
* The start of charge is always within the current trigger event range, so just plain time-based scheduling
*/
scheduleTask("spark up", sUp, (int) MS2US(sparkDelay), (schfunc_t) &turnPinHigh, (void *) iEvent->io_pin);
scheduleTask("spark up", sUp, sparkDelayUs, (schfunc_t) &turnPinHigh, (void *) iEvent->io_pin);
/**
* Spark event is often happening during a later trigger event timeframe
* TODO: improve precision
@ -182,7 +182,7 @@ static INLINE void handleSparkEvent(uint32_t eventIndex, IgnitionEvent *iEvent,
/**
* Spark should be fired before the next trigger event - time-based delay is best precision possible
*/
float timeTillIgnitionUs = getOneDegreeTimeUs(rpm) * iEvent->sparkPosition.angleOffset;
float timeTillIgnitionUs = engine->rpmCalculator.oneDegreeUs * iEvent->sparkPosition.angleOffset;
scheduleTask("spark 1down", sDown, (int) timeTillIgnitionUs, (schfunc_t) &turnPinLow, (void*) iEvent->io_pin);
} else {
@ -216,7 +216,7 @@ static INLINE void handleSpark(uint32_t eventIndex, int rpm, IgnitionEventList *
scheduling_s * sDown = &current->signalTimerDown;
float timeTillIgnitionUs = getOneDegreeTimeUs(rpm) * current->sparkPosition.angleOffset;
float timeTillIgnitionUs = engine->rpmCalculator.oneDegreeUs * current->sparkPosition.angleOffset;
scheduleTask("spark 2down", sDown, (int) timeTillIgnitionUs, (schfunc_t) &turnPinLow,
(void*) current->io_pin);
}
@ -278,7 +278,7 @@ void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t eventIndex, Eng
int beforeCallback = hal_lld_get_counter_value();
#endif
int revolutionIndex = getRevolutionCounter() % 2;
int revolutionIndex = engine->rpmCalculator.getRevolutionCounter() % 2;
if (eventIndex == 0) {
if (localVersion.isOld())
@ -341,7 +341,7 @@ void MainTriggerCallback::init(Engine *engine, engine_configuration2_s *engineCo
}
static void showMainInfo(Engine *engine) {
int rpm = engine->rpmCalculator->rpm();
int rpm = engine->rpmCalculator.rpm();
float el = getEngineLoadT(mainTriggerCallbackInstance.engine);
#if EFI_PROD_CODE
scheduleMsg(&logger, "rpm %d engine_load %f", rpm, el);

View File

@ -44,8 +44,7 @@ extern WaveChart waveChart;
* @return -1 in case of isNoisySignal(), current RPM otherwise
*/
int getRpmE(Engine *engine) {
efiAssert(engine->rpmCalculator!=NULL, "rpmCalculator not assigned", -1);
return engine->rpmCalculator->rpm();
return engine->rpmCalculator.rpm();
}
EXTERN_ENGINE;
@ -65,6 +64,8 @@ RpmCalculator::RpmCalculator() {
lastRpmEventTimeNt = (uint64_t) -10 * US2NT(US_PER_SECOND_LL);
revolutionCounterSinceStart = 0;
revolutionCounterSinceBoot = 0;
lastRpmEventTimeNt = 0;
}
/**
@ -77,11 +78,11 @@ bool RpmCalculator::isRunning(void) {
void RpmCalculator::setRpmValue(int value) {
rpmValue = value;
// if (rpmValue <= 0) {
// oneDegreeUs = NAN;
// } else {
// oneDegreeUs = getOneDegreeTimeUs(rpmValue);
// }
if (rpmValue <= 0) {
oneDegreeUs = NAN;
} else {
oneDegreeUs = getOneDegreeTimeUs(rpmValue);
}
}
void RpmCalculator::onNewEngineCycle() {
@ -137,8 +138,7 @@ bool isCranking(void) {
* This callback is invoked on interrupt thread.
*/
void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, Engine *engine) {
RpmCalculator *rpmState = engine->rpmCalculator;
efiAssertVoid(rpmState!=NULL, "NULL rpmState");
RpmCalculator *rpmState = &engine->rpmCalculator;
uint64_t nowNt = getTimeNowNt();
#if EFI_PROD_CODE
efiAssertVoid(getRemainingStack(chThdSelf()) > 256, "lowstck#2z");
@ -147,7 +147,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, Eng
if (index != 0) {
#if EFI_ANALOG_CHART || defined(__DOXYGEN__)
if (engineConfiguration->analogChartMode == AC_TRIGGER)
acAddData(getCrankshaftAngleNt(nowNt), 1000 * ckpSignalType + index);
acAddData(getCrankshaftAngleNt(engine, nowNt), 1000 * ckpSignalType + index);
#endif
return;
}
@ -175,7 +175,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, Eng
rpmState->lastRpmEventTimeNt = nowNt;
#if EFI_ANALOG_CHART || defined(__DOXYGEN__)
if (engineConfiguration->analogChartMode == AC_TRIGGER)
acAddData(getCrankshaftAngleNt(nowNt), index);
acAddData(getCrankshaftAngleNt(engine, nowNt), index);
#endif
}
@ -200,7 +200,7 @@ static void tdcMarkCallback(trigger_event_e ckpSignalType, uint32_t index0, Engi
(void) ckpSignalType;
bool isTriggerSynchronizationPoint = index0 == 0;
if (isTriggerSynchronizationPoint) {
int revIndex2 = getRevolutionCounter() % 2;
int revIndex2 = engine->rpmCalculator.getRevolutionCounter() % 2;
int rpm = getRpm();
// todo: use event-based scheduling, not just time-based scheduling
scheduleByAngle(rpm, &tdcScheduler[revIndex2], engineConfiguration->globalTriggerAngleOffset,
@ -209,41 +209,36 @@ static void tdcMarkCallback(trigger_event_e ckpSignalType, uint32_t index0, Engi
}
#endif
static RpmCalculator rpmState;
uint64_t getLastRpmEventTime(void) {
return NT2US(rpmState.lastRpmEventTimeNt);
}
int getRevolutionCounter(void) {
return rpmState.getRevolutionCounter();
#if EFI_PROD_CODE || EFI_SIMULATOR
int getRevolutionCounter() {
return engine->rpmCalculator.getRevolutionCounter();
}
#endif
/**
* @return Current crankshaft angle, 0 to 720 for four-stroke
*/
float getCrankshaftAngleNt(uint64_t timeNt) {
uint64_t timeSinceZeroAngleNt = timeNt - rpmState.lastRpmEventTimeNt;
float getCrankshaftAngleNt(Engine *engine, uint64_t timeNt) {
uint64_t timeSinceZeroAngleNt = timeNt - engine->rpmCalculator.lastRpmEventTimeNt;
/**
* even if we use 'getOneDegreeTimeUs' macros here, it looks like the
* compiler is not smart enough to figure out that "A / ( B / C)" could be optimized into
* "A * C / B" in order to replace a slower division with a faster multiplication.
*/
return timeSinceZeroAngleNt / getOneDegreeTimeNt(rpmState.rpm());
return timeSinceZeroAngleNt / getOneDegreeTimeNt(engine->rpmCalculator.rpm());
}
void initRpmCalculator(Engine *engine) {
#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__)
initLogging(&logger, "rpm calc");
engine->rpmCalculator = &rpmState;
tdcScheduler[0].name = "tdc0";
tdcScheduler[1].name = "tdc1";
addTriggerEventListener(tdcMarkCallback, "chart TDC mark", engine);
#endif
addTriggerEventListener((ShaftPositionListener) &rpmShaftPositionCallback, "rpm reporter", engine);
addTriggerEventListener(rpmShaftPositionCallback, "rpm reporter", engine);
}
#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__)
@ -260,12 +255,12 @@ void scheduleByAngle(int rpm, scheduling_s *timer, float angle, schfunc_t callba
*/
return;
}
float delayMs = getOneDegreeTimeMs(rpm) * angle;
if (cisnan(delayMs)) {
float delayUs = getOneDegreeTimeUs(rpm) * angle;
if (cisnan(delayUs)) {
firmwareError("NaN delay?");
return;
}
scheduleTask("by angle", timer, (int) MS2US(delayMs), callback, param);
scheduleTask("by angle", timer, (int) delayUs, callback, param);
}
#endif

View File

@ -20,7 +20,8 @@
#define NOISY_RPM -1
#ifdef __cplusplus
#include "engine.h"
class Engine;
class RpmCalculator {
public:
@ -38,7 +39,7 @@ public:
/**
* This is a performance optimization: let's pre-calulate this each time RPM changes
*/
// volatile float oneDegreeUs;
volatile float oneDegreeUs;
volatile uint64_t lastRpmEventTimeNt;
private:
/**
@ -64,6 +65,8 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, Eng
* @brief Initialize RPM calculator
*/
void initRpmCalculator(Engine *engine);
float getCrankshaftAngleNt(Engine *engine, uint64_t timeNt);
#endif
#ifdef __cplusplus
@ -71,10 +74,8 @@ extern "C" {
#endif /* __cplusplus */
bool isCranking(void);
uint64_t getLastRpmEventTime(void);
int getRevolutionCounter(void);
float getCrankshaftAngleNt(uint64_t timeNt);
bool isValidRpm(int rpm);
void addWaveChartEvent(const char *name, const char *msg, const char *msg2);

View File

@ -20,8 +20,6 @@ EngineTestHelper::EngineTestHelper(engine_type_e engineType) {
engine.engineConfiguration2 = &ec2;
engine.rpmCalculator = &rpmState;
prepareFuelMap(engine.engineConfiguration);

View File

@ -25,7 +25,6 @@ public:
engine_configuration_s *ec;
TriggerCentral triggerCentral;
RpmCalculator rpmState;
};
#endif /* ENGINE_TEST_HELPER_H_ */

View File

@ -95,6 +95,10 @@ void chDbgAssert(int c, char *msg, void *arg) {
static engine_configuration2_s ec2;
engine_configuration2_s *engineConfiguration2 = &ec2;
int getRevolutionCounter(void) {
return 0;
}
int main(void) {
testLogicExpressions();
testOverflow64Counter();
@ -166,6 +170,6 @@ int warning(const char *fmt, ...) {
exit(-1);
}
bool_t isCranking(void) {
bool isCranking(void) {
return 0;
}

View File

@ -2,6 +2,6 @@
#define TEST_IDLE_CONTROLLER_H
void idleDebug(char *msg, int value);
bool_t isCranking(void);
//bool_t isCranking(void);
#endif

View File

@ -14,7 +14,7 @@ extern "C"
#endif /* __cplusplus */
void testSignalExecutor(void);
int getRevolutionCounter(void);
int getRevolutionCounter();
#ifdef __cplusplus
}

View File

@ -18,7 +18,7 @@ void testSpeedDensity(void) {
eth.initTriggerShapeAndRpmCalculator();
eth.fireTriggerEvents();
assertEqualsM("RPM", 1500, eth.rpmState.rpm());
assertEqualsM("RPM", 1500, eth.engine.rpmCalculator.rpm());
// 427 cubic inches, that's a LOT of engine
eth.ec->displacement = 6.99728;

View File

@ -333,10 +333,8 @@ static void testStartupFuelPumping(void) {
StartupFuelPumping sf;
Engine * engine = &eth.engine;
RpmCalculator rc;
engine->rpmCalculator = &rc;
engine->rpmCalculator->mockRpm = 0;
engine->rpmCalculator.mockRpm = 0;
engine->engineConfiguration->tpsMin = 0;
engine->engineConfiguration->tpsMax = 10;
@ -352,12 +350,12 @@ static void testStartupFuelPumping(void) {
sf.update(engine);
assertEqualsM("pc#3", 1, sf.pumpsCounter);
engine->rpmCalculator->mockRpm = 10;
engine->rpmCalculator.mockRpm = 10;
sf.update(engine);
assertEqualsM("pc#4", 0, sf.pumpsCounter);
mockTps = 7;
engine->rpmCalculator->mockRpm = 0;
engine->rpmCalculator.mockRpm = 0;
sf.update(engine);
assertEqualsM("pc#5", 1, sf.pumpsCounter);
@ -395,10 +393,10 @@ static void testRpmCalculator(void) {
eth.engine.engineConfiguration->injectorLag = 0.0;
timeNow = 0;
assertEquals(0, eth.rpmState.rpm());
assertEquals(0, eth.engine.rpmCalculator.rpm());
eth.fireTriggerEvents();
assertEqualsM("RPM", 1500, eth.rpmState.rpm());
assertEqualsM("RPM", 1500, eth.engine.rpmCalculator.rpm());
assertEqualsM("index #1", 15, eth.triggerCentral.triggerState.getCurrentIndex());
static MainTriggerCallback triggerCallbackInstance;