Line data Source code
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 1 : float getTachFreq() { 21 1 : return tachFreq; 22 : } 23 : 24 1 : float getTachDuty() { 25 1 : return duty; 26 : } 27 : #endif 28 : 29 : static bool tachHasInit = false; 30 : 31 483 : void tachUpdate() { 32 : // Only do anything if tach enabled 33 483 : if (!tachHasInit) { 34 480 : return; 35 : } 36 : 37 : // How many tach pulse periods do we have? 38 3 : int periods = engineConfiguration->tachPulsePerRev; 39 : 40 3 : if (periods == 0 || periods > 10) { 41 0 : firmwareError(ObdCode::CUSTOM_ERR_6709, "Invalid tachometer pulse per rev: %d", periods); 42 0 : return; 43 : } 44 : 45 : // What is the angle per tach output period? 46 3 : float cycleTimeMs = 60000.0f / Sensor::getOrZero(SensorType::Rpm); 47 3 : float periodTimeMs = cycleTimeMs / periods; 48 3 : tachFreq = 1000.0f / periodTimeMs; 49 : 50 3 : if (engineConfiguration->tachPulseDurationAsDutyCycle) { 51 : // Simple case - duty explicitly set 52 3 : duty = engineConfiguration->tachPulseDuractionMs; 53 : } else { 54 : // Constant high-time mode - compute the correct duty cycle 55 0 : duty = engineConfiguration->tachPulseDuractionMs / periodTimeMs; 56 : } 57 : 58 : // In case Freq is under 1Hz, we stop pwm to avoid warnings! 59 3 : if (tachFreq < 1) { 60 0 : tachFreq = NAN; 61 : } 62 : 63 3 : tachControl.setSimplePwmDutyCycle(duty); 64 3 : tachControl.setFrequency(tachFreq); 65 : } 66 : 67 353 : void initTachometer() { 68 353 : tachHasInit = false; 69 : 70 353 : if (!isBrainPinValid(engineConfiguration->tachOutputPin)) { 71 352 : return; 72 : } 73 : 74 1 : startSimplePwm(&tachControl, 75 : "Tachometer", 76 : &engine->executor, 77 : &enginePins.tachOut, 78 : NAN, 0.1f); 79 : 80 1 : tachHasInit = true; 81 : }