use NT_PER_SECOND instead of convert from microseconds (#1107)

* add NT_PER_SECOND

* missed a few

* inject tooth logger timestamp

* inject
This commit is contained in:
Matthew Kennedy 2020-01-19 19:23:41 -08:00 committed by rusefi
parent 09dc2f2390
commit ba0c48e0ac
10 changed files with 22 additions and 20 deletions

View File

@ -33,7 +33,7 @@ void SetNextEntry(uint16_t entry) {
}
}
void LogTriggerTooth(trigger_event_e tooth) {
void LogTriggerTooth(trigger_event_e tooth, efitick_t timestamp) {
// bail if we aren't enabled
if (!ToothLoggerEnabled) {
return;
@ -46,7 +46,7 @@ void LogTriggerTooth(trigger_event_e tooth) {
return;
}
uint32_t nowUs = NT2US(getTimeNowNt());
uint32_t nowUs = NT2US(timestamp);
// 10us per LSB - this gives plenty of accuracy, yet fits 655.35 ms in to a uint16
uint16_t delta = static_cast<uint16_t>((nowUs - lastEdgeTimestamp) / 10);
lastEdgeTimestamp = nowUs;
@ -59,7 +59,7 @@ void EnableToothLogger() {
memset(buffer, 0, sizeof(buffer));
// Reset the last edge to now - this prevents the first edge logged from being bogus
lastEdgeTimestamp = NT2US(getTimeNowNt());
lastEdgeTimestamp = getTimeNowUs();
// Reset write index
NextIdx = 0;

View File

@ -9,6 +9,7 @@
#include <cstdint>
#include <cstddef>
#include "efitime.h"
#include "rusefi_enums.h"
// Enable the tooth logger - this clears the buffer starts logging
@ -18,7 +19,7 @@ void EnableToothLogger();
void DisableToothLogger();
// A new tooth has arrived! Log to the buffer if enabled.
void LogTriggerTooth(trigger_event_e tooth);
void LogTriggerTooth(trigger_event_e tooth, efitick_t timestamp);
struct ToothLoggerBuffer
{

View File

@ -213,7 +213,7 @@ Engine::Engine(persistent_config_s *config) {
*/
bool Engine::needToStopEngine(efitick_t nowNt) const {
return stopEngineRequestTimeNt != 0 &&
nowNt - stopEngineRequestTimeNt < 3 * US2NT(US_PER_SECOND_LL);
nowNt - stopEngineRequestTimeNt < 3 * NT_PER_SECOND;
}
int Engine::getGlobalConfigurationVersion(void) const {

View File

@ -88,14 +88,14 @@ void FuelConsumptionState::addData(float durationMs) {
void FuelConsumptionState::update(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) {
efitick_t deltaNt = nowNt - accumulatedSecondPrevNt;
if (deltaNt >= US2NT(US_PER_SECOND_LL)) {
if (deltaNt >= NT_PER_SECOND) {
perSecondConsumption = getFuelRate(perSecondAccumulator, deltaNt PASS_ENGINE_PARAMETER_SUFFIX);
perSecondAccumulator = 0;
accumulatedSecondPrevNt = nowNt;
}
deltaNt = nowNt - accumulatedMinutePrevNt;
if (deltaNt >= US2NT(US_PER_SECOND_LL * 60)) {
if (deltaNt >= NT_PER_SECOND * 60) {
perMinuteConsumption = getFuelRate(perMinuteAccumulator, deltaNt PASS_ENGINE_PARAMETER_SUFFIX);
perMinuteAccumulator = 0;
accumulatedMinutePrevNt = nowNt;

View File

@ -96,7 +96,7 @@ RpmCalculator::RpmCalculator() {
// which we cannot provide inside this parameter-less constructor. need a solution for this minor mess
// we need this initial to have not_running at first invocation
lastRpmEventTimeNt = (efitick_t) -10 * US2NT(US_PER_SECOND_LL);
lastRpmEventTimeNt = (efitick_t) -10 * NT_PER_SECOND;
}
/**
@ -118,11 +118,11 @@ bool RpmCalculator::checkIfSpinning(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUF
* note that the result of this subtraction could be negative, that would happen if
* we have a trigger event between the time we've invoked 'getTimeNow' and here
*/
bool noRpmEventsForTooLong = nowNt - lastRpmEventTimeNt >= US2NT(NO_RPM_EVENTS_TIMEOUT_SECS * US_PER_SECOND_LL); // Anything below 60 rpm is not running
bool noRpmEventsForTooLong = nowNt - lastRpmEventTimeNt >= NT_PER_SECOND * NO_RPM_EVENTS_TIMEOUT_SECS; // Anything below 60 rpm is not running
/**
* Also check if there were no trigger events
*/
bool noTriggerEventsForTooLong = nowNt - engine->triggerCentral.previousShaftEventTimeNt >= US2NT(US_PER_SECOND_LL);
bool noTriggerEventsForTooLong = nowNt - engine->triggerCentral.previousShaftEventTimeNt >= NT_PER_SECOND;
if (noRpmEventsForTooLong || noTriggerEventsForTooLong) {
return false;
}
@ -250,7 +250,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType,
rpmState->setRpmValue(NOISY_RPM PASS_ENGINE_PARAMETER_SUFFIX);
} else {
int mult = (int)getEngineCycle(engine->getOperationMode(PASS_ENGINE_PARAMETER_SIGNATURE)) / 360;
float rpm = 60.0 * US2NT(US_PER_SECOND_LL) * mult / diffNt;
float rpm = 60.0 * NT_PER_SECOND * mult / diffNt;
rpmState->setRpmValue(rpm > UNREALISTIC_RPM ? NOISY_RPM : rpm PASS_ENGINE_PARAMETER_SUFFIX);
}
}

View File

@ -46,7 +46,7 @@ trigger_central_s::trigger_central_s() : hwEventCounters() {
TriggerCentral::TriggerCentral() : trigger_central_s() {
// we need this initial to have not_running at first invocation
previousShaftEventTimeNt = (efitimems_t) -10 * US2NT(US_PER_SECOND_LL);
previousShaftEventTimeNt = (efitimems_t) -10 * NT_PER_SECOND;
clearCallbacks(&triggerListeneres);
triggerState.resetTriggerState();
@ -198,7 +198,7 @@ void hwHandleShaftSignal(trigger_event_e signal, efitick_t timestamp) {
// Log to the Tunerstudio tooth logger
// We want to do this before anything else as we
// actually want to capture any noise/jitter that may be occurring
LogTriggerTooth(signal);
LogTriggerTooth(signal, timestamp);
#endif /* EFI_TOOTH_LOGGER */
// for effective noise filtering, we need both signal edges,
@ -340,7 +340,7 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta
efiAssertVoid(CUSTOM_ERR_6638, eventIndex >= 0 && eventIndex < HW_EVENT_TYPES, "signal type");
hwEventCounters[eventIndex]++;
if (timestamp - previousShaftEventTimeNt > US2NT(US_PER_SECOND_LL)) {
if (timestamp - previousShaftEventTimeNt > NT_PER_SECOND) {
/**
* We are here if there is a time gap between now and previous shaft event - that means the engine is not running.
* That means we have lost synchronization since the engine is not running :)

View File

@ -448,7 +448,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal
* 10 seconds since previous trigger event we do not really care.
*/
toothDurations[0] =
currentDurationLong > 10 * US2NT(US_PER_SECOND_LL) ? 10 * US2NT(US_PER_SECOND_LL) : currentDurationLong;
currentDurationLong > 10 * NT_PER_SECOND ? 10 * NT_PER_SECOND : currentDurationLong;
bool haveListener = triggerStateListener != NULL;
bool isPrimary = triggerWheel == T_PRIMARY;
@ -693,7 +693,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal
}
}
if (someSortOfTriggerError) {
if (getTimeNowNt() - lastDecodingErrorTime > US2NT(US_PER_SECOND_LL)) {
if (getTimeNowNt() - lastDecodingErrorTime > NT_PER_SECOND) {
someSortOfTriggerError = false;
}
}

View File

@ -415,12 +415,12 @@ static bool cj125periodic(CJ125 *instance DECLARE_ENGINE_PARAMETER_SUFFIX) {
case CJ125_PREHEAT:
// use constant-speed startup heat-up
if (nowNt - instance->prevNt >= CJ125_HEATER_PREHEAT_PERIOD) {
float periodSecs = (float)(nowNt - instance->prevNt) / US2NT(US_PER_SECOND_LL);
float periodSecs = (float)(nowNt - instance->prevNt) / NT_PER_SECOND;
// maintain speed at ~0.4V/sec
float preheatDuty = instance->heaterDuty + periodSecs * CJ125_HEATER_PREHEAT_RATE;
instance->SetHeater(preheatDuty PASS_ENGINE_PARAMETER_SUFFIX);
// If we are heating too long, and there's still no result, then something is wrong...
if (nowNt - instance->startHeatingNt > US2NT(US_PER_SECOND_LL) * CJ125_PREHEAT_TIMEOUT) {
if (nowNt - instance->startHeatingNt > NT_PER_SECOND * CJ125_PREHEAT_TIMEOUT) {
instance->setError(CJ125_ERROR_HEATER_MALFUNCTION PASS_ENGINE_PARAMETER_SUFFIX);
}
cjPrintData();

View File

@ -37,10 +37,10 @@ float getVehicleSpeed(void) {
if (!hasVehicleSpeedSensor())
return 0;
efitick_t nowNt = getTimeNowNt();
if (nowNt - lastSignalTimeNt > US2NT(US_PER_SECOND_LL))
if (nowNt - lastSignalTimeNt > NT_PER_SECOND)
return 0; // previous signal time is too long ago - we are stopped
return engineConfiguration->vehicleSpeedCoef * US2NT(US_PER_SECOND_LL) / vssDiff;
return engineConfiguration->vehicleSpeedCoef * NT_PER_SECOND / vssDiff;
}
static void vsAnaWidthCallback(void) {

View File

@ -16,6 +16,7 @@
#define US_PER_SECOND 1000000
#define US_PER_SECOND_F 1000000.0
#define US_PER_SECOND_LL 1000000LL
#define NT_PER_SECOND (US2NT(US_PER_SECOND_LL))
#define MS2US(MS_TIME) ((MS_TIME) * 1000)