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 "func_chain.h"
6 #include "linear_func.h"
7 #include "resistance_func.h"
8 #include "thermistor_func.h"
9 
12 
13 // Each one could be either linear or thermistor
14 struct FuncPair {
15  LinearFunc linear;
16  FuncChain<resist, therm> thermistor;
17 };
18 
23 
28 
29 static FuncPair fclt, fiat, faux1, faux2, foil, ffuel, fambient, fcdt;
30 
31 static void validateThermistorConfig(const char *msg, thermistor_conf_s& cfg) {
32  if (cfg.tempC_1 >= cfg.tempC_2 ||
33  cfg.tempC_2 >= cfg.tempC_3) {
34  firmwareError(ObdCode::OBD_ThermistorConfig, "Invalid thermistor %s configuration: please check that temperatures are in the ascending order %f %f %f",
35  msg,
36  cfg.tempC_1,
37  cfg.tempC_2,
38  cfg.tempC_3);
39  }
40 }
41 
43  thermistor_conf_s& cfg, FuncPair& p, bool isLinear, bool isPulldown) {
44  if (isLinear) {
45  p.linear.configure(cfg.resistance_1, cfg.tempC_1, cfg.resistance_2, cfg.tempC_2, -50, 250);
46 
47  return p.linear;
48  } else /* sensor is thermistor */ {
49  validateThermistorConfig(msg, cfg);
50 
51  p.thermistor.get<resist>().configure(5.0f, cfg.bias_resistor, isPulldown);
52  p.thermistor.get<therm>().configure(cfg);
53 
54  return p.thermistor;
55  }
56 }
57 
58 static void configTherm(const char *msg,
60  FuncPair &p,
61  ThermistorConf &p_config,
62  bool isLinear,
63  bool isPulldown) {
64  // nothing to do if no channel
65  if (!isAdcChannelValid(p_config.adcChannel)) {
66  return;
67  }
68 
69  // Configure the conversion function for this sensor
70  sensor.setFunction(configureTempSensorFunction(msg, p_config.config, p, isLinear, isPulldown));
71 }
72 
73 static void configureTempSensor(const char *msg,
75  FuncPair &p,
76  ThermistorConf &p_config,
77  bool isLinear,
78  bool isPulldown = false) {
79  auto channel = p_config.adcChannel;
80 
81  // Only register if we have a sensor
82  if (!isAdcChannelValid(channel)) {
83  return;
84  }
85 
86  configTherm(msg, sensor, p, p_config, isLinear, isPulldown);
87 
88  // Register & subscribe
90  sensor.Register();
91 }
92 
95  configureTempSensor("clt",
96  clt,
97  fclt,
101 
102  configureTempSensor("iat",
103  iat,
104  fiat,
108  }
109 
110  configureTempSensor("oil temp",
112  faux2,
114  false);
115 
116  configureTempSensor("fuel temp",
118  ffuel,
120  false);
121 
122  configureTempSensor("ambient temp",
124  fambient,
126  false);
127 
128  configureTempSensor("compressor discharge temp",
130  fcdt,
132  false);
133 
134  configureTempSensor("aux1",
135  aux1,
136  faux1,
138  false);
139 
140  configureTempSensor("aux2",
141  aux2,
142  faux2,
144  false);
145 }
146 
156 }
bool isAdcChannelValid(adc_channel_e hwChannel)
Definition: adc_inputs.h:20
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