gear detection in to the sensor model

(cherry picked from commit 82794403f8)
This commit is contained in:
Matthew Kennedy 2023-04-01 23:08:06 -07:00 committed by rusefillc
parent 67883f228f
commit 4e7e25585e
8 changed files with 17 additions and 13 deletions

View File

@ -458,7 +458,7 @@ static void updateVehicleSpeed() {
#if EFI_VEHICLE_SPEED
engine->outputChannels.vehicleSpeedKph = Sensor::getOrZero(SensorType::VehicleSpeed);
engine->outputChannels.speedToRpmRatio = engine->module<GearDetector>()->getGearboxRatio();
engine->outputChannels.detectedGear = engine->module<GearDetector>()->getCurrentGear();
engine->outputChannels.detectedGear = Sensor::getOrZero(SensorType::DetectedGear);
#endif /* EFI_VEHICLE_SPEED */
}

View File

@ -68,7 +68,7 @@ expected<float> readGppwmChannel(gppwm_channel_e channel) {
return (float)engine->outputChannels.gppwmOutput[3];
case GPPWM_DetectedGear:
#if EFI_VEHICLE_SPEED
return engine->module<GearDetector>()->getCurrentGear();
return Sensor::get(SensorType::DetectedGear);
#else
return 0;
#endif // EFI_VEHICLE_SPEED

View File

@ -4,6 +4,11 @@ static constexpr float geometricMean(float x, float y) {
return sqrtf(x * y);
}
GearDetector::GearDetector()
: StoredValueSensor(SensorType::DetectedGear, MS2NT(100))
{
}
void GearDetector::onConfigurationChange(engine_configuration_s const * /*previousConfig*/) {
// Compute gear thresholds between gears
@ -44,7 +49,8 @@ void GearDetector::onSlowCallback() {
float ratio = computeGearboxRatio();
m_gearboxRatio = ratio;
m_currentGear = determineGearFromRatio(ratio);
auto gear = determineGearFromRatio(ratio);
setValidValue(gear, getTimeNowNt());
}
size_t GearDetector::determineGearFromRatio(float ratio) const {
@ -124,7 +130,3 @@ float GearDetector::getRpmInGear(size_t gear) const {
float GearDetector::getGearboxRatio() const {
return m_gearboxRatio;
}
size_t GearDetector::getCurrentGear() const {
return m_currentGear;
}

View File

@ -1,15 +1,15 @@
#pragma once
class GearDetector : public EngineModule {
class GearDetector : public EngineModule, public StoredValueSensor {
public:
GearDetector();
void onSlowCallback() override;
void onConfigurationChange(engine_configuration_s const * /*previousConfig*/) override;
float getGearboxRatio() const;
// Returns 0 for neutral, 1 for 1st, 5 for 5th, etc.
size_t getCurrentGear() const;
size_t determineGearFromRatio(float ratio) const;
float getRpmInGear(size_t gear) const;

View File

@ -861,7 +861,7 @@ void configureRusefiLuaHooks(lua_State* l) {
#if EFI_VEHICLE_SPEED
lua_register(l, "getCurrentGear", [](lua_State* l) {
lua_pushinteger(l, engine->module<GearDetector>()->getCurrentGear());
lua_pushinteger(l, Sensor::getOrZero(SensorType::DetectedGear));
return 1;
});

View File

@ -90,6 +90,8 @@ enum class SensorType : unsigned char {
MapSlow2,
MapFast2,
DetectedGear,
// analog voltage inputs for Lua
AuxAnalog1,
AuxAnalog2,

View File

@ -41,7 +41,7 @@ void TransmissionControllerBase::measureShiftTime(gear_e gear) {
}
float TransmissionControllerBase::isShiftCompleted() {
if (m_shiftTime && m_shiftTimeGear == engine->module<GearDetector>()->getCurrentGear()) {
if (m_shiftTime && m_shiftTimeGear == Sensor::getOrZero(SensorType::DetectedGear)) {
m_shiftTime = false;
return m_shiftTimer.getElapsedSeconds();
} else {

View File

@ -12,7 +12,7 @@ float GetGearRatioFor(float revPerKm, float axle, float kph, float rpm) {
GearDetector dut;
dut.onSlowCallback();
return dut.getGearboxRatio();
return dut.get().value_or(0);
}
TEST(GearDetector, ComputeGearRatio) {