rusEFI
The most advanced open source ECU
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 
15 static SimplePwm tachControl("tach");
16 static float tachFreq;
17 static float duty;
18 
19 #if EFI_UNIT_TEST
20 float getTachFreq() {
21  return tachFreq;
22 }
23 
24 float getTachDuty() {
25  return duty;
26 }
27 #endif
28 
29 static bool tachHasInit = false;
30 
31 void tachUpdate() {
32  // Only do anything if tach enabled
33  if (!tachHasInit) {
34  return;
35  }
36 
37  // How many tach pulse periods do we have?
38  int periods = engineConfiguration->tachPulsePerRev;
39 
40  if (periods == 0 || periods > 10) {
41  firmwareError(ObdCode::CUSTOM_ERR_6709, "Invalid tachometer pulse per rev: %d", periods);
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",
76  &engine->executor,
78  NAN, 0.1f);
79 
80  tachHasInit = true;
81 }
SingleTimerExecutor executor
Definition: engine.h:240
RegisteredOutputPin tachOut
Definition: efi_gpio.h:115
void setFrequency(float frequency)
static float getOrZero(SensorType type)
Definition: sensor.h:92
void setSimplePwmDutyCycle(float dutyCycle) override
EnginePins enginePins
Definition: efi_gpio.cpp:24
Engine * engine
void firmwareError(ObdCode code, const char *fmt,...)
@ CUSTOM_ERR_6709
engine_configuration_s * engineConfiguration
bool isBrainPinValid(brain_pin_e brainPin)
void startSimplePwm(SimplePwm *state, const char *msg, ExecutorInterface *executor, OutputPin *output, float frequency, float dutyCycle, pwm_gen_callback *callback)
float getTachFreq()
Definition: tachometer.cpp:20
static bool tachHasInit
Definition: tachometer.cpp:29
static float tachFreq
Definition: tachometer.cpp:16
static float duty
Definition: tachometer.cpp:17
void initTachometer()
Definition: tachometer.cpp:67
float getTachDuty()
Definition: tachometer.cpp:24
void tachUpdate()
Definition: tachometer.cpp:31
static SimplePwm tachControl("tach")