rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
tachometer.cpp
Go to the documentation of this file.
1/*
2 * @file tachometer.cpp
3 * @brief This is about driving external analog tachometers
4 *
5 * This implementation produces one pulse per engine cycle
6 *
7 * @date Aug 18, 2015
8 * @author Andrey Belomutskiy, (c) 2012-2020
9 */
10
11#include "pch.h"
12
13#include "tachometer.h"
14
15static SimplePwm tachControl("tach");
16static float tachFreq;
17static float duty;
18
19#if EFI_UNIT_TEST
20float getTachFreq() {
21 return tachFreq;
22}
23
24float getTachDuty() {
25 return duty;
26}
27#endif
28
29
31 // Only do anything if tach enabled
32 if (!tachHasInit) {
33 return;
34 }
35
36 // How many tach pulse periods do we have?
37 float periods = engineConfiguration->tachPulsePerRev;
38
39 if (periods == 0 || periods > 10) {
40 firmwareError(ObdCode::CUSTOM_ERR_6709, "Invalid tachometer pulse per rev: %.2f", periods);
41 tachHasInit = false; // disable until nex fw init to avoid spamming the output
42 return;
43 }
44
45 // What is the angle per tach output period?
46 float cycleTimeMs = 60000.0f / Sensor::getOrZero(SensorType::Rpm);
47 float periodTimeMs = cycleTimeMs / periods;
48 tachFreq = 1000.0f / periodTimeMs;
49
51 // Simple case - duty explicitly set
53 } else {
54 // Constant high-time mode - compute the correct duty cycle
56 }
57
58 // In case Freq is under 1Hz, we stop pwm to avoid warnings!
59 if (tachFreq < 1) {
60 tachFreq = NAN;
61 }
62
65}
66
68 tachHasInit = false;
69
71 return;
72 }
73
75 "Tachometer",
78 NAN, 0.1f);
79
80 tachHasInit = true;
81}
SingleTimerExecutor scheduler
Definition engine.h:271
RegisteredOutputPin tachOut
Definition efi_gpio.h:120
void setFrequency(float frequency)
static float getOrZero(SensorType type)
Definition sensor.h:83
void setSimplePwmDutyCycle(float dutyCycle) override
void onFastCallback() override
EnginePins enginePins
Definition efi_gpio.cpp:24
static EngineAccessor engine
Definition engine.h:413
static constexpr engine_configuration_s * engineConfiguration
void firmwareError(ObdCode code, const char *fmt,...)
@ CUSTOM_ERR_6709
bool isBrainPinValid(brain_pin_e brainPin)
void startSimplePwm(SimplePwm *state, const char *msg, Scheduler *executor, OutputPin *output, float frequency, float dutyCycle, pwm_gen_callback *callback)
float getTachFreq()
static float tachFreq
static float duty
float getTachDuty()
static SimplePwm tachControl("tach")