2021-08-03 19:05:01 -07:00
|
|
|
|
#include "pch.h"
|
|
|
|
|
|
2021-10-04 14:18:08 -07:00
|
|
|
|
#include "adc_subscription.h"
|
|
|
|
|
#include "linear_func.h"
|
|
|
|
|
#include "fallback_sensor.h"
|
|
|
|
|
#include "functional_sensor.h"
|
2022-10-27 10:59:36 -07:00
|
|
|
|
#include "map_averaging.h"
|
2020-12-30 05:43:49 -08:00
|
|
|
|
|
2022-01-23 06:42:11 -08:00
|
|
|
|
static LinearFunc baroConverter;
|
|
|
|
|
static FunctionalSensor baroSensor(SensorType::BarometricPressure, MS2NT(50));
|
2021-02-07 15:54:41 -08:00
|
|
|
|
|
2021-10-04 14:18:08 -07:00
|
|
|
|
// This converter is shared between both fast and slow: the only difference is
|
|
|
|
|
// how the *voltage* is determined, not how its converted to a pressure.
|
|
|
|
|
static LinearFunc mapConverter;
|
|
|
|
|
static FunctionalSensor slowMapSensor(SensorType::MapSlow, MS2NT(50));
|
2022-10-27 18:15:04 -07:00
|
|
|
|
static FunctionalSensor slowMapSensor2(SensorType::MapSlow2, MS2NT(50));
|
2023-09-18 02:04:10 -07:00
|
|
|
|
static FunctionalSensor compressorDischargePress(SensorType::CompressorDischargePressure, MS2NT(50));
|
|
|
|
|
static FunctionalSensor throttleInletPress(SensorType::ThrottleInletPressure, MS2NT(50));
|
2021-10-04 14:18:08 -07:00
|
|
|
|
|
|
|
|
|
// lowest reasonable idle is maybe 600 rpm
|
|
|
|
|
// one sample per cycle (1 cylinder, or "sample one cyl" mode) gives a period of 100ms
|
|
|
|
|
// add some margin -> 200ms timeout for fast MAP sampling
|
2022-10-27 10:59:36 -07:00
|
|
|
|
MapAverager fastMapSensor(SensorType::MapFast, MS2NT(200));
|
2022-10-27 18:15:04 -07:00
|
|
|
|
MapAverager fastMapSensor2(SensorType::MapFast2, MS2NT(200));
|
2021-10-04 14:18:08 -07:00
|
|
|
|
|
2022-10-27 18:15:04 -07:00
|
|
|
|
MapAverager& getMapAvg(size_t idx) {
|
|
|
|
|
return idx == 0 ? fastMapSensor : fastMapSensor2;
|
2021-10-04 14:18:08 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Combine MAP sensors: prefer fast sensor, but use slow if fast is unavailable.
|
|
|
|
|
static FallbackSensor mapCombiner(SensorType::Map, SensorType::MapFast, SensorType::MapSlow);
|
2022-10-27 18:15:04 -07:00
|
|
|
|
static FallbackSensor mapCombiner2(SensorType::Map2, SensorType::MapFast2, SensorType::MapSlow2);
|
2021-10-04 14:18:08 -07:00
|
|
|
|
|
|
|
|
|
// helper struct for the local getMapCfg function
|
|
|
|
|
struct MapCfg {
|
2021-10-06 13:07:55 -07:00
|
|
|
|
float v1;
|
|
|
|
|
float map1;
|
|
|
|
|
float v2;
|
|
|
|
|
float map2;
|
2021-10-04 14:18:08 -07:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-23 06:42:11 -08:00
|
|
|
|
static MapCfg getMapCfg(air_pressure_sensor_type_e sensorType) {
|
2021-10-04 14:18:08 -07:00
|
|
|
|
switch (sensorType) {
|
|
|
|
|
case MT_DENSO183:
|
|
|
|
|
return {0, -6.64, 5, 182.78};
|
|
|
|
|
case MT_MPX4100:
|
2021-10-16 09:31:40 -07:00
|
|
|
|
return {0.306, 20, 4.897, 105};
|
|
|
|
|
case MT_MPX4250:
|
2021-10-04 14:18:08 -07:00
|
|
|
|
case MT_MPX4250A:
|
2021-10-16 09:31:40 -07:00
|
|
|
|
return {0.204, 20, 4.896, 250};
|
2021-10-04 14:18:08 -07:00
|
|
|
|
case MT_HONDA3BAR:
|
|
|
|
|
return {0.5, 91.422, 3.0, 0};
|
|
|
|
|
case MT_DODGE_NEON_2003:
|
|
|
|
|
return {0.4, 15.34, 4.5, 100};
|
|
|
|
|
case MT_SUBY_DENSO:
|
|
|
|
|
return {0, 0, 5, 200};
|
|
|
|
|
case MT_GM_3_BAR:
|
|
|
|
|
return {0.631, 40, 4.914, 304};
|
|
|
|
|
case MT_GM_2_BAR:
|
|
|
|
|
return {0, 8.8, 5, 208};
|
|
|
|
|
case MT_GM_1_BAR:
|
|
|
|
|
return {0, 10, 5, 105};
|
|
|
|
|
case MT_TOYOTA_89420_02010:
|
|
|
|
|
return {3.7 - 2, 33.32, 3.7, 100};
|
|
|
|
|
case MT_MAZDA_1_BAR:
|
|
|
|
|
return {0, 2.5, 5, 117};
|
|
|
|
|
case MT_BOSCH_2_5:
|
2021-10-16 17:15:59 -07:00
|
|
|
|
// kpa=54.11764705882353v−1.6470588235294201
|
2021-10-04 14:18:08 -07:00
|
|
|
|
return {0.4 , 20 , 4.65, 250};
|
|
|
|
|
case MT_MPXH6400:
|
2021-10-16 09:31:40 -07:00
|
|
|
|
return {0.2, 20, 4.8, 400};
|
2023-08-02 15:16:33 -07:00
|
|
|
|
case MT_MPXH6300:
|
|
|
|
|
return {1.0, 60, 4.5, 270};
|
2021-10-04 14:18:08 -07:00
|
|
|
|
default:
|
2023-04-11 17:01:34 -07:00
|
|
|
|
firmwareError(ObdCode::CUSTOM_ERR_MAP_TYPE, "Unknown MAP type: decoder %d", sensorType);
|
2021-10-04 14:18:08 -07:00
|
|
|
|
// falls through to custom
|
|
|
|
|
return {};
|
|
|
|
|
case MT_CUSTOM: {
|
|
|
|
|
auto& mapConfig = engineConfiguration->map.sensor;
|
|
|
|
|
return {
|
|
|
|
|
engineConfiguration->mapLowValueVoltage,
|
|
|
|
|
mapConfig.lowValue,
|
|
|
|
|
engineConfiguration->mapHighValueVoltage,
|
|
|
|
|
mapConfig.highValue
|
|
|
|
|
};
|
|
|
|
|
}}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 06:42:11 -08:00
|
|
|
|
void configureMapFunction(LinearFunc& converter, air_pressure_sensor_type_e sensorType) {
|
2022-01-31 15:37:42 -08:00
|
|
|
|
auto cfg = getMapCfg(sensorType);
|
2021-10-04 14:18:08 -07:00
|
|
|
|
|
2022-01-23 06:42:11 -08:00
|
|
|
|
converter.configure(
|
2021-10-04 14:18:08 -07:00
|
|
|
|
cfg.v1,
|
|
|
|
|
cfg.map1,
|
|
|
|
|
cfg.v2,
|
|
|
|
|
cfg.map2,
|
|
|
|
|
engineConfiguration->mapErrorDetectionTooLow,
|
|
|
|
|
engineConfiguration->mapErrorDetectionTooHigh
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
|
void initMap() {
|
2023-09-18 02:04:10 -07:00
|
|
|
|
// Set up the conversion function
|
|
|
|
|
configureMapFunction(mapConverter, engineConfiguration->map.sensor.type);
|
2021-10-04 14:18:08 -07:00
|
|
|
|
|
2023-09-18 02:04:10 -07:00
|
|
|
|
slowMapSensor.setFunction(mapConverter);
|
|
|
|
|
slowMapSensor2.setFunction(mapConverter);
|
|
|
|
|
fastMapSensor.setFunction(mapConverter);
|
|
|
|
|
fastMapSensor2.setFunction(mapConverter);
|
|
|
|
|
compressorDischargePress.setFunction(mapConverter);
|
|
|
|
|
throttleInletPress.setFunction(mapConverter);
|
2021-10-04 14:18:08 -07:00
|
|
|
|
|
2023-09-18 02:04:10 -07:00
|
|
|
|
auto mapChannel = engineConfiguration->map.sensor.hwChannel;
|
|
|
|
|
if (isAdcChannelValid(mapChannel)) {
|
2021-10-04 14:18:08 -07:00
|
|
|
|
slowMapSensor.Register();
|
2022-10-27 18:15:04 -07:00
|
|
|
|
slowMapSensor2.Register();
|
2021-10-04 14:18:08 -07:00
|
|
|
|
fastMapSensor.Register();
|
2022-10-27 18:15:04 -07:00
|
|
|
|
fastMapSensor2.Register();
|
2021-10-04 14:18:08 -07:00
|
|
|
|
mapCombiner.Register();
|
2022-10-27 18:15:04 -07:00
|
|
|
|
mapCombiner2.Register();
|
2021-10-04 14:18:08 -07:00
|
|
|
|
|
|
|
|
|
// Configure slow MAP as a normal analog sensor
|
|
|
|
|
AdcSubscription::SubscribeSensor(slowMapSensor, mapChannel, 100);
|
|
|
|
|
}
|
2021-02-07 15:54:41 -08:00
|
|
|
|
|
2023-09-18 02:04:10 -07:00
|
|
|
|
AdcSubscription::SubscribeSensor(throttleInletPress, engineConfiguration->throttleInletPressureChannel, 100);
|
|
|
|
|
AdcSubscription::SubscribeSensor(compressorDischargePress, engineConfiguration->compressorDischargePressureChannel, 100);
|
|
|
|
|
|
2022-01-23 06:42:11 -08:00
|
|
|
|
auto baroChannel = engineConfiguration->baroSensor.hwChannel;
|
|
|
|
|
if (isAdcChannelValid(baroChannel)) {
|
|
|
|
|
configureMapFunction(baroConverter, engineConfiguration->baroSensor.type);
|
|
|
|
|
|
|
|
|
|
baroSensor.setFunction(baroConverter);
|
2021-02-07 15:54:41 -08:00
|
|
|
|
baroSensor.Register();
|
2022-01-23 06:42:11 -08:00
|
|
|
|
|
|
|
|
|
AdcSubscription::SubscribeSensor(baroSensor, baroChannel, 10);
|
2021-02-07 15:54:41 -08:00
|
|
|
|
}
|
2020-12-30 05:43:49 -08:00
|
|
|
|
}
|
2021-10-04 14:18:08 -07:00
|
|
|
|
|
|
|
|
|
void deinitMap() {
|
2023-01-06 05:09:17 -08:00
|
|
|
|
AdcSubscription::UnsubscribeSensor(slowMapSensor, engineConfiguration->map.sensor.hwChannel);
|
|
|
|
|
AdcSubscription::UnsubscribeSensor(baroSensor, engineConfiguration->baroSensor.hwChannel);
|
2023-09-18 02:04:10 -07:00
|
|
|
|
AdcSubscription::UnsubscribeSensor(throttleInletPress, engineConfiguration->throttleInletPressureChannel);
|
|
|
|
|
AdcSubscription::UnsubscribeSensor(compressorDischargePress, engineConfiguration->compressorDischargePressureChannel);
|
2021-10-04 14:18:08 -07:00
|
|
|
|
}
|