2015-08-18 12:03:44 -07:00
|
|
|
/*
|
|
|
|
* @file tachometer.cpp
|
2015-08-18 14:01:49 -07:00
|
|
|
* @brief This is about driving external analog tachometers
|
2015-08-18 12:03:44 -07:00
|
|
|
*
|
2016-08-16 19:05:36 -07:00
|
|
|
* This implementation produces one pulse per engine cycle
|
|
|
|
*
|
2015-08-18 12:03:44 -07:00
|
|
|
* @date Aug 18, 2015
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-08-18 12:03:44 -07:00
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
2015-08-18 12:03:44 -07:00
|
|
|
#include "tachometer.h"
|
2019-04-09 19:52:03 -07:00
|
|
|
|
2020-04-02 12:20:04 -07:00
|
|
|
static SimplePwm tachControl("tach");
|
|
|
|
static float tachFreq;
|
|
|
|
static float duty;
|
|
|
|
|
|
|
|
#if EFI_UNIT_TEST
|
2020-08-21 12:36:43 -07:00
|
|
|
float getTachFreq() {
|
2020-04-02 12:20:04 -07:00
|
|
|
return tachFreq;
|
|
|
|
}
|
2015-08-19 13:01:36 -07:00
|
|
|
|
2020-08-21 12:36:43 -07:00
|
|
|
float getTachDuty() {
|
2020-04-02 12:20:04 -07:00
|
|
|
return duty;
|
2015-08-19 13:01:36 -07:00
|
|
|
}
|
2020-04-02 12:20:04 -07:00
|
|
|
#endif
|
2015-08-18 14:01:49 -07:00
|
|
|
|
2020-08-21 12:36:43 -07:00
|
|
|
static bool tachHasInit = false;
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void tachSignalCallback() {
|
2020-08-21 12:36:43 -07:00
|
|
|
// Only do anything if tach enabled
|
|
|
|
if (!tachHasInit) {
|
2020-04-02 12:20:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// How many tach pulse periods do we have?
|
2021-11-17 00:54:21 -08:00
|
|
|
int periods = engineConfiguration->tachPulsePerRev;
|
2020-04-02 12:20:04 -07:00
|
|
|
|
2020-12-06 05:30:31 -08:00
|
|
|
if (periods == 0 || periods > 10) {
|
2023-04-11 17:01:34 -07:00
|
|
|
firmwareError(ObdCode::CUSTOM_ERR_6709, "Invalid tachometer pulse per rev: %d", periods);
|
2015-08-18 21:03:11 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-04-02 12:20:04 -07:00
|
|
|
|
|
|
|
// What is the angle per tach output period?
|
2022-01-20 20:36:09 -08:00
|
|
|
float cycleTimeMs = 60000.0f / Sensor::getOrZero(SensorType::Rpm);
|
2020-04-02 12:20:04 -07:00
|
|
|
float periodTimeMs = cycleTimeMs / periods;
|
2020-05-06 05:38:15 -07:00
|
|
|
tachFreq = 1000.0f / periodTimeMs;
|
2020-04-02 12:20:04 -07:00
|
|
|
|
2021-11-17 00:54:21 -08:00
|
|
|
if (engineConfiguration->tachPulseDurationAsDutyCycle) {
|
2020-04-02 12:20:04 -07:00
|
|
|
// Simple case - duty explicitly set
|
2021-11-17 00:54:21 -08:00
|
|
|
duty = engineConfiguration->tachPulseDuractionMs;
|
2017-08-28 18:00:36 -07:00
|
|
|
} else {
|
2020-04-02 12:20:04 -07:00
|
|
|
// Constant high-time mode - compute the correct duty cycle
|
2021-11-17 00:54:21 -08:00
|
|
|
duty = engineConfiguration->tachPulseDuractionMs / periodTimeMs;
|
2020-04-02 12:20:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// In case Freq is under 1Hz, we stop pwm to avoid warnings!
|
2020-05-06 05:38:15 -07:00
|
|
|
if (tachFreq < 1) {
|
2020-04-02 12:20:04 -07:00
|
|
|
tachFreq = NAN;
|
2017-08-28 18:00:36 -07:00
|
|
|
}
|
2020-04-02 12:20:04 -07:00
|
|
|
|
|
|
|
tachControl.setSimplePwmDutyCycle(duty);
|
|
|
|
tachControl.setFrequency(tachFreq);
|
2015-08-18 14:01:49 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void initTachometer() {
|
2020-08-21 12:36:43 -07:00
|
|
|
tachHasInit = false;
|
2021-11-17 00:54:21 -08:00
|
|
|
if (!isBrainPinValid(engineConfiguration->tachOutputPin)) {
|
2015-08-18 12:03:44 -07:00
|
|
|
return;
|
2015-08-18 21:03:11 -07:00
|
|
|
}
|
2015-08-18 12:03:44 -07:00
|
|
|
|
2020-11-09 18:10:48 -08:00
|
|
|
startSimplePwm(&tachControl,
|
2020-04-02 12:20:04 -07:00
|
|
|
"Tachometer",
|
|
|
|
&engine->executor,
|
|
|
|
&enginePins.tachOut,
|
2020-05-06 05:38:15 -07:00
|
|
|
NAN, 0.1f);
|
2015-08-18 14:01:49 -07:00
|
|
|
|
2020-08-21 12:36:43 -07:00
|
|
|
tachHasInit = true;
|
2015-08-18 12:03:44 -07:00
|
|
|
}
|