Lua hooks for gear detection (#4328)
* implement * sim has vss * write a test * guard
This commit is contained in:
parent
9e26fe007e
commit
3c062e02bc
|
@ -77,7 +77,7 @@ size_t GearDetector::determineGearFromRatio(float ratio) const {
|
|||
return currentGear;
|
||||
}
|
||||
|
||||
float GearDetector::computeGearboxRatio() const {
|
||||
float GearDetector::getDriveshaftRpm() const {
|
||||
auto vssKph = Sensor::getOrZero(SensorType::VehicleSpeed);
|
||||
|
||||
if (vssKph < 5) {
|
||||
|
@ -85,6 +85,23 @@ float GearDetector::computeGearboxRatio() const {
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Convert to wheel RPM
|
||||
// km rev 1 hr
|
||||
// ------ * ------------ * __________
|
||||
// hr km 60 min
|
||||
float wheelRpm = vssKph * engineConfiguration->driveWheelRevPerKm * (1 / 60.0f);
|
||||
|
||||
// Convert to driveshaft RPM
|
||||
return wheelRpm * engineConfiguration->finalGearRatio;
|
||||
}
|
||||
|
||||
float GearDetector::computeGearboxRatio() const {
|
||||
float driveshaftRpm = getDriveshaftRpm();
|
||||
|
||||
if (driveshaftRpm == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float engineRpm;
|
||||
if (Sensor::hasSensor(SensorType::InputShaftSpeed)) {
|
||||
engineRpm = Sensor::getOrZero(SensorType::InputShaftSpeed);
|
||||
|
@ -92,18 +109,18 @@ float GearDetector::computeGearboxRatio() const {
|
|||
engineRpm = Sensor::getOrZero(SensorType::Rpm);
|
||||
}
|
||||
|
||||
// Convert to wheel RPM
|
||||
// km rev 1 hr
|
||||
// ------ * ------------ * __________
|
||||
// hr km 60 min
|
||||
float wheelRpm = vssKph * engineConfiguration->driveWheelRevPerKm * (1 / 60.0f);
|
||||
|
||||
// Convert to driveshaft RPM
|
||||
auto driveshaftRpm = wheelRpm * engineConfiguration->finalGearRatio;
|
||||
|
||||
return engineRpm / driveshaftRpm;
|
||||
}
|
||||
|
||||
float GearDetector::getRpmInGear(size_t gear) const {
|
||||
if (gear <= 0 || gear > engineConfiguration->totalGearsCount) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Ideal engine RPM is driveshaft speed times gear
|
||||
return getDriveshaftRpm() * engineConfiguration->gearRatio[gear - 1];
|
||||
}
|
||||
|
||||
float GearDetector::getGearboxRatio() const {
|
||||
return m_gearboxRatio;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#pragma once
|
||||
|
||||
class GearDetector : public EngineModule {
|
||||
public:
|
||||
|
@ -11,8 +12,11 @@ public:
|
|||
|
||||
size_t determineGearFromRatio(float ratio) const;
|
||||
|
||||
float getRpmInGear(size_t gear) const;
|
||||
|
||||
private:
|
||||
float computeGearboxRatio() const;
|
||||
float getDriveshaftRpm() const;
|
||||
|
||||
float m_gearboxRatio = 0;
|
||||
size_t m_currentGear = 0;
|
||||
|
|
|
@ -616,6 +616,19 @@ void configureRusefiLuaHooks(lua_State* l) {
|
|||
return 0;
|
||||
});
|
||||
|
||||
#if EFI_VEHICLE_SPEED
|
||||
lua_register(l, "getCurrentGear", [](lua_State* l) {
|
||||
lua_pushinteger(l, engine->module<GearDetector>()->getCurrentGear());
|
||||
return 1;
|
||||
});
|
||||
|
||||
lua_register(l, "getRpmInGear", [](lua_State* l) {
|
||||
auto idx = luaL_checkinteger(l, 1);
|
||||
lua_pushinteger(l, engine->module<GearDetector>()->getRpmInGear(idx));
|
||||
return 1;
|
||||
});
|
||||
#endif // EFI_VEHICLE_SPEED
|
||||
|
||||
#if !EFI_UNIT_TEST
|
||||
lua_register(l, "startPwm", lua_startPwm);
|
||||
lua_register(l, "setPwmDuty", lua_setPwmDuty);
|
||||
|
|
|
@ -138,7 +138,7 @@
|
|||
#define EFI_TUNER_STUDIO_VERBOSE FALSE
|
||||
#define EFI_FILE_LOGGING TRUE
|
||||
#define EFI_WARNING_LED FALSE
|
||||
#define EFI_VEHICLE_SPEED FALSE
|
||||
#define EFI_VEHICLE_SPEED TRUE
|
||||
#define EFI_TCU FALSE
|
||||
|
||||
#define EFI_SENSOR_CHART TRUE
|
||||
|
|
|
@ -27,6 +27,46 @@ TEST(GearDetector, ComputeGearRatio) {
|
|||
EXPECT_EQ(0, GetGearRatioFor(507, 4.1, 0, 800));
|
||||
}
|
||||
|
||||
|
||||
TEST(GearDetector, GetRpmInGear) {
|
||||
EngineTestHelper eth(TEST_ENGINE);
|
||||
|
||||
engineConfiguration->driveWheelRevPerKm = 507;
|
||||
engineConfiguration->finalGearRatio = 4.10f;
|
||||
|
||||
// real gears from Volvo racecar
|
||||
engineConfiguration->totalGearsCount = 5;
|
||||
engineConfiguration->gearRatio[0] = 3.35f;
|
||||
engineConfiguration->gearRatio[1] = 1.99f;
|
||||
engineConfiguration->gearRatio[2] = 1.33f;
|
||||
engineConfiguration->gearRatio[3] = 1.00f;
|
||||
engineConfiguration->gearRatio[4] = 0.72f;
|
||||
|
||||
GearDetector dut;
|
||||
|
||||
Sensor::setMockValue(SensorType::VehicleSpeed, 29.45f / 0.6214f);
|
||||
EXPECT_NEAR(5500, dut.getRpmInGear(1), 1);
|
||||
Sensor::setMockValue(SensorType::VehicleSpeed, 49.57f / 0.6214f);
|
||||
EXPECT_NEAR(5500, dut.getRpmInGear(2), 1);
|
||||
Sensor::setMockValue(SensorType::VehicleSpeed, 74.18f / 0.6214f);
|
||||
EXPECT_NEAR(5500, dut.getRpmInGear(3), 1);
|
||||
Sensor::setMockValue(SensorType::VehicleSpeed, 98.65f / 0.6214f);
|
||||
EXPECT_NEAR(5500, dut.getRpmInGear(4), 1);
|
||||
Sensor::setMockValue(SensorType::VehicleSpeed, 137.02f / 0.6214f);
|
||||
EXPECT_NEAR(5500, dut.getRpmInGear(5), 1);
|
||||
|
||||
// Test some invalid cases
|
||||
EXPECT_FLOAT_EQ(0, dut.getRpmInGear(0));
|
||||
EXPECT_FLOAT_EQ(0, dut.getRpmInGear(10));
|
||||
|
||||
// Zero vehicle speed shouldn't cause a problem
|
||||
Sensor::setMockValue(SensorType::VehicleSpeed, 0);
|
||||
EXPECT_FLOAT_EQ(0, dut.getRpmInGear(0));
|
||||
EXPECT_FLOAT_EQ(0, dut.getRpmInGear(1));
|
||||
EXPECT_FLOAT_EQ(0, dut.getRpmInGear(5));
|
||||
EXPECT_FLOAT_EQ(0, dut.getRpmInGear(10));
|
||||
}
|
||||
|
||||
TEST(GearDetector, DetermineGearSingleSpeed) {
|
||||
EngineTestHelper eth(TEST_ENGINE);
|
||||
GearDetector dut;
|
||||
|
|
Loading…
Reference in New Issue