2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file vehicle_speed.cpp
|
|
|
|
*
|
|
|
|
* @date Dec 26, 2014
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
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));
|
|
|
|
|
|
|
|
scheduleMsg(logger, "c=%f eventCounter=%d speed=%f",
|
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) {
|
2017-05-05 18:05:59 -07:00
|
|
|
stopWaveAnalyzerDriver("VSS", activeConfiguration.bc.vehicleSpeedSensorInputPin);
|
2017-04-05 15:08:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void startVSSPins(void) {
|
|
|
|
// todo
|
|
|
|
}
|
|
|
|
|
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);
|
2017-02-05 20:04:55 -08:00
|
|
|
if (!hasVehicleSpeedSensor())
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
digital_input_s* vehicleSpeedInput = initWaveAnalyzerDriver("VSS", boardConfiguration->vehicleSpeedSensorInputPin);
|
|
|
|
startInputDriver(vehicleSpeedInput, true);
|
|
|
|
|
|
|
|
vehicleSpeedInput->widthListeners.registerCallback((VoidInt) vsAnaWidthCallback, NULL);
|
|
|
|
}
|
2017-05-02 08:49:35 -07:00
|
|
|
#else /* EFI_VEHICLE_SPEED */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-02 08:49:35 -07:00
|
|
|
float getVehicleSpeed(void) {
|
|
|
|
// no VSS support
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_VEHICLE_SPEED */
|