rusefi-1/firmware/controllers/gauges/tachometer.cpp

96 lines
2.3 KiB
C++
Raw Normal View History

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
*
2016-05-24 19:02:13 -07:00
* todo: these is a bit of duplication with dizzySparkOutputPin
*
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
*/
#include "tachometer.h"
2015-08-18 14:01:49 -07:00
#include "trigger_central.h"
#include "pwm_generator.h"
2015-08-18 12:03:44 -07:00
EXTERN_ENGINE;
static SimplePwm tachControl("tach");
static float tachFreq;
static float duty;
#if EFI_UNIT_TEST
float getTachFreq(void) {
return tachFreq;
}
2015-08-19 13:01:36 -07:00
float getTachDuty(void) {
return duty;
2015-08-19 13:01:36 -07:00
}
#endif
2015-08-18 14:01:49 -07:00
static void tachSignalCallback(trigger_event_e ckpSignalType,
uint32_t index, efitick_t edgeTimestamp DECLARE_ENGINE_PARAMETER_SUFFIX) {
// only process at index configured to avoid too much cpu time for index 0?
if (index != (uint32_t)CONFIG(tachPulseTriggerIndex)) {
return;
}
#if EFI_UNIT_TEST
printf("tachSignalCallback(%d %d)\n", ckpSignalType, index);
printf("Current RPM: %d\n",GET_RPM());
UNUSED(edgeTimestamp);
#else
UNUSED(ckpSignalType);
UNUSED(edgeTimestamp);
#endif
// How many tach pulse periods do we have?
int periods = CONFIG(tachPulsePerRev);
if(periods == 0){
warning(CUSTOM_ERR_6709,"Check Tachometer Pulse per Rev!");
2015-08-18 21:03:11 -07:00
return;
}
// What is the angle per tach output period?
float cycleTimeMs = 60000.0 / GET_RPM();
float periodTimeMs = cycleTimeMs / periods;
tachFreq = 1000.0 / periodTimeMs;
if (CONFIG(tachPulseDurationAsDutyCycle)) {
// Simple case - duty explicitly set
duty = CONFIG(tachPulseDuractionMs);
2017-08-28 18:00:36 -07:00
} else {
// Constant high-time mode - compute the correct duty cycle
duty = CONFIG(tachPulseDuractionMs) / periodTimeMs;
}
// In case Freq is under 1Hz, we stop pwm to avoid warnings!
if (tachFreq < 1.0) {
tachFreq = NAN;
2017-08-28 18:00:36 -07:00
}
tachControl.setSimplePwmDutyCycle(duty);
tachControl.setFrequency(tachFreq);
2015-08-18 14:01:49 -07:00
}
2020-04-01 16:00:56 -07:00
void initTachometer(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
if (CONFIG(tachOutputPin) == GPIO_UNASSIGNED) {
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
startSimplePwmExt(&tachControl,
"Tachometer",
&engine->executor,
CONFIG(tachOutputPin),
&enginePins.tachOut,
NAN, 0.1, (pwm_gen_callback*)applyPinState);
2015-08-18 14:01:49 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_SHAFT_POSITION_INPUT
2015-08-18 14:01:49 -07:00
addTriggerEventListener(tachSignalCallback, "tach", engine);
2019-01-31 08:57:15 -08:00
#endif /* EFI_SHAFT_POSITION_INPUT */
2015-08-18 12:03:44 -07:00
}