Skip to content

Commit

Permalink
implement temp sensor pulldown mode #116
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Jun 10, 2023
1 parent c1cb45d commit ec8b054
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
1 change: 1 addition & 0 deletions firmware/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Release template (copy/paste this for new release):

### Added
- Log per-cylinder true ignition timing (includes trim, knock retard, etc) #76
- Add mode for CLT/IAT sensors that are installed "high side" instead of typical "low side" #116

### Fixed
- Improved bench test resolution (more usable for testing injectors, dwell, etc)
Expand Down
9 changes: 8 additions & 1 deletion firmware/controllers/sensors/converters/resistance_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

#include "resistance_func.h"

void ResistanceFunc::configure(float supplyVoltage, float pullupResistor) {
void ResistanceFunc::configure(float supplyVoltage, float pullupResistor, bool isPulldown) {
m_pullupResistor = pullupResistor;
m_supplyVoltage = supplyVoltage;
m_isPulldown = isPulldown;
}

SensorResult ResistanceFunc::convert(float raw) const {
Expand All @@ -20,6 +21,12 @@ SensorResult ResistanceFunc::convert(float raw) const {
return UnexpectedCode::High;
}

if (m_isPulldown) {
// If the sensor is on the high side (fixed resistor is pulldown),
// invert the voltage so the math comes out correctly
raw = m_supplyVoltage - raw;
}

// Voltage is in a sensible range - convert
float resistance = m_pullupResistor / (m_supplyVoltage / raw - 1);

Expand Down
3 changes: 2 additions & 1 deletion firmware/controllers/sensors/converters/resistance_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class ResistanceFunc final : public SensorConverter {
public:
void configure(float supplyVoltage, float pullupResistor);
void configure(float supplyVoltage, float pullupResistor, bool isPulldown);

SensorResult convert(float inputValue) const override;

Expand All @@ -21,4 +21,5 @@ class ResistanceFunc final : public SensorConverter {
private:
float m_supplyVoltage = 5.0f;
float m_pullupResistor = 1000.0f;
bool m_isPulldown = false;
};
20 changes: 12 additions & 8 deletions firmware/init/sensor/init_thermistors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ static void validateThermistorConfig(const char *msg, thermistor_conf_s& cfg) {
}

static SensorConverter& configureTempSensorFunction(const char *msg,
thermistor_conf_s& cfg, FuncPair& p, bool isLinear) {
thermistor_conf_s& cfg, FuncPair& p, bool isLinear, bool isPulldown) {
if (isLinear) {
p.linear.configure(cfg.resistance_1, cfg.tempC_1, cfg.resistance_2, cfg.tempC_2, -50, 250);

return p.linear;
} else /* sensor is thermistor */ {
validateThermistorConfig(msg, cfg);

p.thermistor.get<resist>().configure(5.0f, cfg.bias_resistor);
p.thermistor.get<resist>().configure(5.0f, cfg.bias_resistor, isPulldown);
p.thermistor.get<therm>().configure(cfg);

return p.thermistor;
Expand All @@ -54,29 +54,31 @@ static void configTherm(const char *msg,
FunctionalSensor &sensor,
FuncPair &p,
ThermistorConf &config,
bool isLinear) {
bool isLinear,
bool isPulldown) {
// nothing to do if no channel
if (!isAdcChannelValid(config.adcChannel)) {
return;
}

// Configure the conversion function for this sensor
sensor.setFunction(configureTempSensorFunction(msg, config.config, p, isLinear));
sensor.setFunction(configureTempSensorFunction(msg, config.config, p, isLinear, isPulldown));
}

static void configureTempSensor(const char *msg,
FunctionalSensor &sensor,
FuncPair &p,
ThermistorConf &config,
bool isLinear) {
bool isLinear,
bool isPulldown = false) {
auto channel = config.adcChannel;

// Only register if we have a sensor
if (!isAdcChannelValid(channel)) {
return;
}

configTherm(msg, sensor, p, config, isLinear);
configTherm(msg, sensor, p, config, isLinear, isPulldown);

// Register & subscribe
AdcSubscription::SubscribeSensor(sensor, channel, 2);
Expand All @@ -88,13 +90,15 @@ void initThermistors() {
clt,
fclt,
engineConfiguration->clt,
engineConfiguration->useLinearCltSensor);
engineConfiguration->useLinearCltSensor,
engineConfiguration->cltSensorPulldown);

configureTempSensor("iat",
iat,
fiat,
engineConfiguration->iat,
engineConfiguration->useLinearIatSensor);
engineConfiguration->useLinearIatSensor,
engineConfiguration->iatSensorPulldown);

configureTempSensor("aux1",
aux1,
Expand Down
3 changes: 2 additions & 1 deletion firmware/integration/rusefi_config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ bit usescriptTableForCanSniffingFiltering
bit verboseCan,"Print all","Do not print";Print incoming and outgoing first bus CAN messages in rusEFI console
bit artificialTestMisfire,"Danger Mode","No thank you";Experimental setting that will cause a misfire\nDO NOT ENABLE.
bit useFordRedundantPps;On some Ford and Toyota vehicles one of the pedal sensors is not linear on the full range, i.e. in the specific range of the positions we effectively have only one sensor.

bit cltSensorPulldown
bit iatSensorPulldown

int16_t tpsMin;Closed throttle, 1 volt = 200 units;"ADC", 1, 0, 0, 1023, 0
int16_t tpsMax;Full throttle, 1 volt = 200 units;"ADC", 1, 0, 0, 1023, 0
Expand Down
2 changes: 2 additions & 0 deletions firmware/tunerstudio/rusefi.input
Original file line number Diff line number Diff line change
Expand Up @@ -4256,6 +4256,8 @@ dialog = tcuControls, "Transmission Settings"
field = "Ford redundant PPS mode", useFordRedundantPps
field = "Secondary PPS maximum", ppsSecondaryMaximum, {useFordRedundantPps}
field = "ADC vRef voltage", adcVcc
field = "CLT sensor is pulldown instead of pullup", cltSensorPulldown
field = "IAT sensor is pulldown instead of pullup", iatSensorPulldown
field = "Analog divider ratio", analogInputDividerCoefficient@@if_ts_show_analog_divider
field = "Artificial Misfire", artificialTestMisfire
field = "Instant Rpm Range", instantRpmRange
Expand Down
39 changes: 37 additions & 2 deletions unit_tests/tests/sensor/resist_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
TEST(resistance, OutOfRange)
{
ResistanceFunc f;
f.configure(5, 10000);
f.configure(5, 10000, false);

// Something in the middle should be valid
{
Expand Down Expand Up @@ -45,7 +45,7 @@ TEST(resistance, OutOfRange)
TEST(resistance, InRange)
{
ResistanceFunc f;
f.configure(5, 10000);
f.configure(5, 10000, false);

// 1 volt -> 2500 ohms low side
{
Expand Down Expand Up @@ -76,3 +76,38 @@ TEST(resistance, InRange)
EXPECT_FLOAT_EQ(r.Value, 40000);
}
}

TEST(resistance, PulldownMode)
{
ResistanceFunc f;
f.configure(5, 10000, true);

// 4 volt -> 2500 ohms high side
{
auto r = f.convert(4.0f);
EXPECT_TRUE(r.Valid);
EXPECT_FLOAT_EQ(r.Value, 2500);
}

// 3 volt -> 6666.667 ohm ohms high side
// 20k/3 gives us an exact result
{
auto r = f.convert(3.0f);
EXPECT_TRUE(r.Valid);
EXPECT_FLOAT_EQ(r.Value, 20000.0f / 3);
}

// 2 volt -> 15000 ohms high side
{
auto r = f.convert(2.0f);
EXPECT_TRUE(r.Valid);
EXPECT_FLOAT_EQ(r.Value, 15000);
}

// 1 volt -> 40000 ohms high side
{
auto r = f.convert(1.0f);
EXPECT_TRUE(r.Valid);
EXPECT_FLOAT_EQ(r.Value, 40000);
}
}

0 comments on commit ec8b054

Please sign in to comment.