rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes
GearDetector Class Reference

#include <gear_detector.h>

Inheritance diagram for GearDetector:
Inheritance graph
[legend]
Collaboration diagram for GearDetector:
Collaboration graph
[legend]

Public Member Functions

 GearDetector ()
 
 ~GearDetector ()
 
void onSlowCallback () override
 
void onConfigurationChange (engine_configuration_s const *) override
 
float getGearboxRatio () const
 
size_t determineGearFromRatio (float ratio) const
 
float getRpmInGear (size_t gear) const
 
SensorResult get () const override
 
void showInfo (const char *sensorName) const override
 
- Public Member Functions inherited from EngineModule
virtual void initNoConfiguration ()
 
virtual void setDefaultConfiguration ()
 
virtual void onFastCallback ()
 
virtual void onEngineStop ()
 
virtual void onIgnitionStateChanged (bool)
 
virtual bool needsDelayedShutoff ()
 
virtual void onEnginePhase (float, efitick_t, angle_t, angle_t)
 
- Public Member Functions inherited from Sensor
bool Register ()
 
const chargetSensorName () const
 
virtual bool hasSensor () const
 
virtual float getRaw () const
 
virtual bool isRedundant () const
 
void unregister ()
 
SensorType type () const
 

Private Member Functions

float computeGearboxRatio () const
 
float getDriveshaftRpm () const
 
void initGearDetector ()
 

Private Attributes

bool isInitialized = false
 
float m_gearboxRatio = 0
 
size_t m_currentGear = 0
 
float m_gearThresholds [TCU_GEAR_COUNT - 1]
 

Additional Inherited Members

- Static Public Member Functions inherited from Sensor
static void showAllSensorInfo ()
 
static void showInfo (SensorType type)
 
static void resetRegistry ()
 
static const SensorgetSensorOfType (SensorType type)
 
static SensorResult get (SensorType type)
 
static float getOrZero (SensorType type)
 
static float getRaw (SensorType type)
 
static bool isRedundant (SensorType type)
 
static bool hasSensor (SensorType type)
 
static void setMockValue (SensorType type, float value, bool mockRedundant=false)
 
static void setInvalidMockValue (SensorType type)
 
static void resetMockValue (SensorType type)
 
static void resetAllMocks ()
 
static void inhibitTimeouts (bool inhibit)
 
static const chargetSensorName (SensorType type)
 
- Protected Member Functions inherited from Sensor
 Sensor (SensorType type)
 
- Static Protected Attributes inherited from Sensor
static bool s_inhibitSensorTimeouts = false
 

Detailed Description

Definition at line 4 of file gear_detector.h.

Constructor & Destructor Documentation

◆ GearDetector()

GearDetector::GearDetector ( )

Definition at line 8 of file gear_detector.cpp.

◆ ~GearDetector()

GearDetector::~GearDetector ( )

Definition at line 13 of file gear_detector.cpp.

13 {
14 unregister();
15}
void unregister()
Definition sensor.cpp:135
Here is the call graph for this function:

Member Function Documentation

◆ computeGearboxRatio()

float GearDetector::computeGearboxRatio ( ) const
private

Definition at line 119 of file gear_detector.cpp.

119 {
120 float driveshaftRpm = getDriveshaftRpm();
121
122 if (driveshaftRpm == 0) {
123 return 0;
124 }
125
126 float engineRpm;
129 } else {
131 }
132
133 return engineRpm / driveshaftRpm;
134}
float getDriveshaftRpm() const
virtual bool hasSensor() const
Definition sensor.h:141
static float getOrZero(SensorType type)
Definition sensor.h:83

Referenced by onSlowCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ determineGearFromRatio()

size_t GearDetector::determineGearFromRatio ( float  ratio) const

Definition at line 71 of file gear_detector.cpp.

71 {
72 auto gearCount = engineConfiguration->totalGearsCount;
73 if (gearCount == 0) {
74 // No gears, we only have neutral.
75 return 0;
76 }
77
78 // 1.5x first gear is neutral or clutch slip or something
79 if (ratio > engineConfiguration->gearRatio[0] * 1.5f) {
80 return 0;
81 }
82
83 // 0.66x top gear is coasting with engine off or something
84 if (ratio < engineConfiguration->gearRatio[gearCount - 1] * 0.66f) {
85 return 0;
86 }
87
88 size_t currentGear = gearCount;
89
90 while (currentGear > 1) {
91 if (ratio < m_gearThresholds[currentGear - 2]) {
92 break;
93 }
94
95 currentGear--;
96 }
97
98 return currentGear;
99}
float m_gearThresholds[TCU_GEAR_COUNT - 1]
static constexpr engine_configuration_s * engineConfiguration
scaled_channel< uint16_t, 100, 1 > gearRatio[TCU_GEAR_COUNT]

Referenced by onSlowCallback().

Here is the caller graph for this function:

◆ get()

SensorResult GearDetector::get ( ) const
overridevirtual

Implements Sensor.

Definition at line 149 of file gear_detector.cpp.

149 {
150 return m_currentGear;
151}
size_t m_currentGear

◆ getDriveshaftRpm()

float GearDetector::getDriveshaftRpm ( ) const
private

Definition at line 101 of file gear_detector.cpp.

101 {
103
104 if (vssKph < 3) {
105 // Vehicle too slow to determine gearbox ratio, avoid div/0
106 return 0;
107 }
108
109 // Convert to wheel RPM
110 // km rev 1 hr
111 // ------ * ------------ * __________
112 // hr km 60 min
113 float wheelRpm = vssKph * engineConfiguration->driveWheelRevPerKm * (1 / 60.0f);
114
115 // Convert to driveshaft RPM
116 return wheelRpm * engineConfiguration->finalGearRatio;
117}

Referenced by computeGearboxRatio(), and getRpmInGear().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getGearboxRatio()

float GearDetector::getGearboxRatio ( ) const

Definition at line 145 of file gear_detector.cpp.

145 {
146 return m_gearboxRatio;
147}
float m_gearboxRatio

◆ getRpmInGear()

float GearDetector::getRpmInGear ( size_t  gear) const

Definition at line 136 of file gear_detector.cpp.

136 {
137 if (gear <= 0 || gear > engineConfiguration->totalGearsCount) {
138 return 0;
139 }
140
141 // Ideal engine RPM is driveshaft speed times gear
142 return getDriveshaftRpm() * engineConfiguration->gearRatio[gear - 1];
143}
Here is the call graph for this function:

◆ initGearDetector()

void GearDetector::initGearDetector ( )
private

Definition at line 17 of file gear_detector.cpp.

17 {
18 // Compute gear thresholds between gears
19
20 uint8_t gearCount = engineConfiguration->totalGearsCount;
21
22 if (gearCount == 0) {
23 // No gears, nothing to do here.
24 return;
25 }
26
27 if (gearCount > TCU_GEAR_COUNT) {
28 criticalError("too many gears");
29 return;
30 }
31
32 // validate gears
33 for (size_t i = 0; i < gearCount; i++) {
34 if (engineConfiguration->gearRatio[i] <= 0) {
35 criticalError("Expecting positive gear ratio for #%d", i + 1);
36 return;
37 }
38 }
39
40 for (int i = 0; i < gearCount - 1; i++) {
41 // Threshold i is the threshold between gears i and i+1
42 float gearI = engineConfiguration->gearRatio[i];
43 float gearIplusOne = engineConfiguration->gearRatio[i + 1];
44
45 if (gearI <= gearIplusOne) {
46 criticalError("Invalid gear ordering near gear #%d", i + 1);
47 }
48
49 m_gearThresholds[i] = geometricMean(gearI, gearIplusOne);
50 }
51
52 Register();
53}
bool Register()
Definition sensor.cpp:131
static constexpr float geometricMean(float x, float y)

Referenced by onConfigurationChange(), and onSlowCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onConfigurationChange()

void GearDetector::onConfigurationChange ( engine_configuration_s const *  )
overridevirtual

Reimplemented from EngineModule.

Definition at line 55 of file gear_detector.cpp.

55 {
57}
void initGearDetector()
Here is the call graph for this function:

◆ onSlowCallback()

void GearDetector::onSlowCallback ( )
overridevirtual

Reimplemented from EngineModule.

Definition at line 59 of file gear_detector.cpp.

59 {
60 if (!isInitialized) {
62 isInitialized = true;
63 }
64
65 float ratio = computeGearboxRatio();
66 m_gearboxRatio = ratio;
67
69}
float computeGearboxRatio() const
size_t determineGearFromRatio(float ratio) const
Here is the call graph for this function:

◆ showInfo()

void GearDetector::showInfo ( const char sensorName) const
overridevirtual

Implements Sensor.

Definition at line 153 of file gear_detector.cpp.

153 {
154 efiPrintf("Sensor \"%s\" is gear detector.", sensorName);
155 efiPrintf(" Gearbox ratio: %.3f", m_gearboxRatio);
156 efiPrintf(" Detected gear: %d", m_currentGear);
157}

Field Documentation

◆ isInitialized

bool GearDetector::isInitialized = false
private

Definition at line 26 of file gear_detector.h.

Referenced by onSlowCallback().

◆ m_currentGear

size_t GearDetector::m_currentGear = 0
private

Definition at line 29 of file gear_detector.h.

Referenced by get(), onSlowCallback(), and showInfo().

◆ m_gearboxRatio

float GearDetector::m_gearboxRatio = 0
private

Definition at line 28 of file gear_detector.h.

Referenced by getGearboxRatio(), onSlowCallback(), and showInfo().

◆ m_gearThresholds

float GearDetector::m_gearThresholds[TCU_GEAR_COUNT - 1]
private

Definition at line 31 of file gear_detector.h.

Referenced by determineGearFromRatio(), and initGearDetector().


The documentation for this class was generated from the following files: