rusEFI
The most advanced open source ECU
init_thermistors.cpp
Go to the documentation of this file.
1 #include "pch.h"
2 
3 #include "adc_subscription.h"
4 #include "functional_sensor.h"
5 #include "linear_func.h"
6 #include "thermistor_func.h"
7 
8 // Each one could be either linear or thermistor
9 struct FuncPair {
10  LinearFunc linear;
11  thermistor_t thermistor;
12 };
13 
18 
23 
24 static FuncPair fclt, fiat, faux1, faux2, foil, ffuel, fambient, fcdt;
25 
26 static void validateThermistorConfig(const char *msg, thermistor_conf_s& cfg) {
27  if (cfg.tempC_1 >= cfg.tempC_2 ||
28  cfg.tempC_2 >= cfg.tempC_3) {
29  firmwareError(ObdCode::OBD_ThermistorConfig, "Invalid thermistor %s configuration: please check that temperatures are in the ascending order %f %f %f",
30  msg,
31  (float)cfg.tempC_1,
32  (float)cfg.tempC_2,
33  (float)cfg.tempC_3);
34  }
35 }
36 
38  thermistor_conf_s& cfg, FuncPair& p, bool isLinear, bool isPulldown) {
39  if (isLinear) {
40  p.linear.configure(cfg.resistance_1, cfg.tempC_1, cfg.resistance_2, cfg.tempC_2, -50, 250);
41 
42  return p.linear;
43  } else /* sensor is thermistor */ {
44  validateThermistorConfig(msg, cfg);
45 
46  p.thermistor.get<resist>().configure(5.0f, cfg.bias_resistor, isPulldown);
47  p.thermistor.get<therm>().configure(cfg);
48 
49  return p.thermistor;
50  }
51 }
52 
53 static void configTherm(const char *msg,
55  FuncPair &p,
56  ThermistorConf &p_config,
57  bool isLinear,
58  bool isPulldown) {
59  // nothing to do if no channel
60  if (!isAdcChannelValid(p_config.adcChannel)) {
61  return;
62  }
63 
64  // Configure the conversion function for this sensor
65  sensor.setFunction(configureTempSensorFunction(msg, p_config.config, p, isLinear, isPulldown));
66 }
67 
68 static void configureTempSensor(const char *msg,
70  FuncPair &p,
71  ThermistorConf &p_config,
72  bool isLinear,
73  bool isPulldown = false) {
74  auto channel = p_config.adcChannel;
75 
76  // Only register if we have a sensor
77  if (!isAdcChannelValid(channel)) {
78  return;
79  }
80 
81  configTherm(msg, sensor, p, p_config, isLinear, isPulldown);
82 
83  // Register & subscribe
85  sensor.Register();
86 }
87 
90  configureTempSensor("clt",
91  clt,
92  fclt,
96 
97  configureTempSensor("iat",
98  iat,
99  fiat,
103  }
104 
105  configureTempSensor("oil temp",
107  faux2,
109  false);
110 
111  configureTempSensor("fuel temp",
113  ffuel,
115  false);
116 
117  configureTempSensor("ambient temp",
119  fambient,
121  false);
122 
123  configureTempSensor("compressor discharge temp",
125  fcdt,
127  false);
128 
129  configureTempSensor("aux1",
130  aux1,
131  faux1,
133  false);
134 
135  configureTempSensor("aux2",
136  aux2,
137  faux2,
139  false);
140 }
141 
151 }
bool isAdcChannelValid(adc_channel_e hwChannel)
Definition: adc_inputs.h:20
uint16_t channel
Definition: adc_inputs.h:105
static void UnsubscribeSensor(FunctionalSensor &sensor)
static AdcSubscriptionEntry * SubscribeSensor(FunctionalSensor &sensor, adc_channel_e channel, float lowpassCutoffHZ, float voltsPerAdcVolt=0.0f)
Class for sensors that convert from some raw floating point value (ex: voltage, frequency,...
bool Register()
Definition: sensor.cpp:131
void firmwareError(ObdCode code, const char *fmt,...)
static Lps25Sensor sensor(device)
static CCM_OPTIONAL FunctionalSensor fuelTempSensor(SensorType::FuelTemperature, MS2NT(10))
static FuncPair fiat
static CCM_OPTIONAL FunctionalSensor aux2(SensorType::AuxTemp2, MS2NT(10))
void initThermistors()
static void configureTempSensor(const char *msg, FunctionalSensor &sensor, FuncPair &p, ThermistorConf &p_config, bool isLinear, bool isPulldown=false)
static FuncPair ffuel
static FuncPair fcdt
static void configTherm(const char *msg, FunctionalSensor &sensor, FuncPair &p, ThermistorConf &p_config, bool isLinear, bool isPulldown)
static FuncPair fambient
static FuncPair foil
static CCM_OPTIONAL FunctionalSensor compressorDischargeTemp(SensorType::CompressorDischargeTemperature, MS2NT(10))
static FuncPair faux1
static SensorConverter & configureTempSensorFunction(const char *msg, thermistor_conf_s &cfg, FuncPair &p, bool isLinear, bool isPulldown)
static FuncPair fclt
static FuncPair faux2
static CCM_OPTIONAL FunctionalSensor iat(SensorType::Iat, MS2NT(10))
static CCM_OPTIONAL FunctionalSensor aux1(SensorType::AuxTemp1, MS2NT(10))
static CCM_OPTIONAL FunctionalSensor ambientTempSensor(SensorType::AmbientTemperature, MS2NT(10))
static CCM_OPTIONAL FunctionalSensor clt(SensorType::Clt, MS2NT(10))
static CCM_OPTIONAL FunctionalSensor oilTempSensor(SensorType::OilTemperature, MS2NT(10))
static void validateThermistorConfig(const char *msg, thermistor_conf_s &cfg)
void deinitThermistors()
@ OBD_ThermistorConfig
engine_configuration_s * engineConfiguration
@ AmbientTemperature
@ CompressorDischargeTemperature
static chibios_rt::Mailbox< CompositeBuffer *, BUFFER_COUNT > freeBuffers CCM_OPTIONAL