rusefi-1/firmware/hw_layer/vehicle_speed.cpp

93 lines
2.3 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file vehicle_speed.cpp
*
* @date Dec 26, 2014
2018-01-20 17:55:31 -08:00
* @author Andrey Belomutskiy, (c) 2012-2018
2015-07-10 06:01:56 -07:00
*/
#include "vehicle_speed.h"
#if EFI_VEHICLE_SPEED || defined(__DOXYGEN__)
#include "engine.h"
#include "digital_input_hw.h"
#include "pin_repository.h"
EXTERN_ENGINE
;
static Logging *logger;
static efitick_t lastSignalTimeNt = 0;
static efitick_t vssDiff = 0;
2017-01-16 13:03:37 -08:00
#define DEFAULT_MOCK_SPEED -1
static float mockVehicleSpeed = DEFAULT_MOCK_SPEED; // in kilometers per hour
void setMockVehicleSpeed(float speedKPH) {
mockVehicleSpeed = speedKPH;
}
2015-07-10 06:01:56 -07:00
/**
* @return vehicle speed, in kilometers per hour
*/
float getVehicleSpeed(void) {
2017-01-16 13:03:37 -08:00
if (mockVehicleSpeed != DEFAULT_MOCK_SPEED)
return mockVehicleSpeed;
2017-02-05 20:04:55 -08:00
if (!hasVehicleSpeedSensor())
2016-06-20 17:02:47 -07:00
return 0;
2015-07-10 06:01:56 -07:00
efitick_t nowNt = getTimeNowNt();
if (nowNt - lastSignalTimeNt > US2NT(US_PER_SECOND_LL))
return 0; // previous signal time is too long ago - we are stopped
return engineConfiguration->vehicleSpeedCoef * US2NT(US_PER_SECOND_LL) / vssDiff;
}
static void vsAnaWidthCallback(void) {
2017-05-21 20:17:08 -07:00
engine->engineState.vssEventCounter++;
2015-07-10 06:01:56 -07:00
efitick_t nowNt = getTimeNowNt();
vssDiff = nowNt - lastSignalTimeNt;
lastSignalTimeNt = nowNt;
}
static void speedInfo(void) {
2017-02-06 16:03:19 -08:00
scheduleMsg(logger, "VSS input at %s",
2016-06-20 14:03:03 -07:00
hwPortname(boardConfiguration->vehicleSpeedSensorInputPin));
2018-01-23 09:05:14 -08:00
scheduleMsg(logger, "c=%.2f eventCounter=%d speed=%.2f",
2015-07-10 06:01:56 -07:00
engineConfiguration->vehicleSpeedCoef,
2017-05-21 20:17:08 -07:00
engine->engineState.vssEventCounter,
2015-07-10 06:01:56 -07:00
getVehicleSpeed());
scheduleMsg(logger, "vss diff %d", vssDiff);
}
2017-02-05 20:04:55 -08:00
bool hasVehicleSpeedSensor() {
return boardConfiguration->vehicleSpeedSensorInputPin != GPIO_UNASSIGNED;
}
2017-04-05 15:08:36 -07:00
void stopVSSPins(void) {
2018-01-01 08:27:15 -08:00
removeWaveAnalyzerDriver("VSS", activeConfiguration.bc.vehicleSpeedSensorInputPin);
2017-04-05 15:08:36 -07:00
}
void startVSSPins(void) {
2018-01-01 08:27:15 -08:00
if (!hasVehicleSpeedSensor())
return;
digital_input_s* vehicleSpeedInput = addWaveAnalyzerDriver("VSS", boardConfiguration->vehicleSpeedSensorInputPin);
startInputDriver(vehicleSpeedInput, true);
vehicleSpeedInput->widthListeners.registerCallback((VoidInt) vsAnaWidthCallback, NULL);
2017-04-05 15:08:36 -07:00
}
2015-07-10 06:01:56 -07:00
void initVehicleSpeed(Logging *l) {
logger = l;
2016-06-20 14:03:03 -07:00
addConsoleAction("speedinfo", speedInfo);
2018-01-01 08:27:15 -08:00
startVSSPins();
2015-07-10 06:01:56 -07:00
}
#else /* EFI_VEHICLE_SPEED */
2015-07-10 06:01:56 -07:00
float getVehicleSpeed(void) {
// no VSS support
return 0;
}
2015-07-10 06:01:56 -07:00
#endif /* EFI_VEHICLE_SPEED */