2020-10-29 02:55:55 -07:00
|
|
|
#include "sampling.h"
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2020-10-30 02:03:12 -07:00
|
|
|
#include "wideband_config.h"
|
|
|
|
|
2021-02-25 22:39:51 -08:00
|
|
|
#include "port.h"
|
2021-11-07 22:52:13 -08:00
|
|
|
#include "io_pins.h"
|
2022-07-18 12:31:09 -07:00
|
|
|
#include "livedata.h"
|
2020-10-29 02:55:55 -07:00
|
|
|
|
2022-07-16 19:08:45 -07:00
|
|
|
#include <rusefi/interpolation.h>
|
|
|
|
|
|
|
|
// Last point is approximated by the greatest measurable sensor resistance
|
2023-02-14 13:53:52 -08:00
|
|
|
static const float lsu49TempBins[] = { 80, 100, 150, 200, 250, 300, 350, 400, 450, 550, 650, 800, 1000, 1200, 2500, 4500 };
|
|
|
|
static const float lsu49TempValues[] = { 1030, 972, 888, 840, 806, 780, 761, 744, 729, 703, 686, 665, 642, 628, 567, 500 };
|
|
|
|
|
|
|
|
static const float lsu42TempBins[] = { 35, 40, 50, 60, 70, 80, 90, 100, 120, 150, 200, 250, 300, 400, 450, 500, 600, 700, 800, 900, 1000, 1100 };
|
|
|
|
static const float lsu42TempValues[] = { 1199, 961, 857, 806, 775, 750, 730, 715, 692, 666, 635, 613, 598, 574, 564, 556, 543, 535, 528, 521, 514, 503 };
|
|
|
|
|
|
|
|
static const float lsuAdvTempBins[] = { 53, 96, 130, 162, 184, 206, 239, 278, 300, 330, 390, 462, 573, 730, 950, 1200, 1500, 1900, 2500, 3500, 5000, 6000 };
|
|
|
|
static const float lsuAdvTempValues[] = { 1198, 982, 914, 875, 855, 838, 816, 794, 785, 771, 751, 732, 711, 691, 671, 653, 635, 614, 588, 562, 537, 528 };
|
2022-05-07 11:12:20 -07:00
|
|
|
|
2023-06-20 17:09:44 -07:00
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
float Sampler::GetNernstDc() const
|
|
|
|
{
|
|
|
|
return nernstDc;
|
|
|
|
}
|
2023-06-20 17:09:44 -07:00
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
float Sampler::GetNernstAc() const
|
|
|
|
{
|
|
|
|
return nernstAc;
|
|
|
|
}
|
2023-06-20 17:09:44 -07:00
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
float Sampler::GetPumpNominalCurrent() const
|
|
|
|
{
|
|
|
|
// Gain is 10x, then a 61.9 ohm resistor
|
|
|
|
// Effective resistance with the gain is 619 ohms
|
|
|
|
// 1000 is to convert to milliamperes
|
|
|
|
constexpr float ratio = -1000 / (PUMP_CURRENT_SENSE_GAIN * LSU_SENSE_R);
|
|
|
|
return pumpCurrentSenseVoltage * ratio;
|
|
|
|
}
|
2023-06-20 17:09:44 -07:00
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
float Sampler::GetInternalBatteryVoltage() const
|
|
|
|
{
|
|
|
|
// Dual HW can measure heater voltage for each channel
|
|
|
|
// by measuring voltage on Heater- while FET is off
|
|
|
|
// TODO: rename function?
|
|
|
|
return internalBatteryVoltage;
|
|
|
|
}
|
2023-06-20 17:09:44 -07:00
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
float Sampler::GetSensorTemperature() const
|
|
|
|
{
|
|
|
|
float esr = GetSensorInternalResistance();
|
2023-06-20 17:26:54 -07:00
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
if (esr > 5000)
|
|
|
|
{
|
2023-06-20 17:26:54 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
switch (GetSensorType()) {
|
|
|
|
case SensorType::LSU49:
|
|
|
|
return interpolate2d(esr, lsu49TempBins, lsu49TempValues);
|
|
|
|
case SensorType::LSU42:
|
|
|
|
return interpolate2d(esr, lsu42TempBins, lsu42TempValues);
|
|
|
|
case SensorType::LSUADV:
|
|
|
|
return interpolate2d(esr, lsuAdvTempBins, lsuAdvTempValues);
|
2023-06-20 17:26:54 -07:00
|
|
|
}
|
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Sampler::GetSensorInternalResistance() const
|
|
|
|
{
|
|
|
|
// Sensor is the lowside of a divider, top side is GetESRSupplyR(), and 3.3v AC pk-pk is injected
|
|
|
|
float totalEsr = GetESRSupplyR() / (VCC_VOLTS / GetNernstAc() - 1);
|
2023-06-20 17:09:44 -07:00
|
|
|
|
2023-08-09 12:31:49 -07:00
|
|
|
// There is a resistor between the opamp and Vm sensor pin. Remove the effect of that
|
|
|
|
// resistor so that the remainder is only the ESR of the sensor itself
|
|
|
|
return totalEsr - VM_RESISTOR_VALUE;
|
|
|
|
}
|
2023-06-20 17:09:44 -07:00
|
|
|
|
|
|
|
static Sampler samplers[AFR_CHANNELS];
|
|
|
|
|
2023-06-23 14:38:17 -07:00
|
|
|
const ISampler& GetSampler(int ch)
|
2023-06-20 17:26:54 -07:00
|
|
|
{
|
|
|
|
return samplers[ch];
|
|
|
|
}
|
|
|
|
|
2020-10-29 02:55:55 -07:00
|
|
|
constexpr float f_abs(float x)
|
|
|
|
{
|
|
|
|
return x > 0 ? x : -x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static THD_WORKING_AREA(waSamplingThread, 256);
|
|
|
|
|
|
|
|
static void SamplingThread(void*)
|
|
|
|
{
|
2022-12-12 19:29:32 -08:00
|
|
|
chRegSetThreadName("Sampling");
|
|
|
|
|
2023-02-25 11:46:36 -08:00
|
|
|
SetupESRDriver(GetSensorType());
|
2023-02-01 11:43:52 -08:00
|
|
|
|
2022-06-29 12:14:08 -07:00
|
|
|
/* GD32: Insert 20us delay after ADC enable */
|
|
|
|
chThdSleepMilliseconds(1);
|
|
|
|
|
2020-10-29 02:55:55 -07:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
auto result = AnalogSample();
|
|
|
|
|
2021-01-19 00:24:47 -08:00
|
|
|
// Toggle the pin after sampling so that any switching noise occurs while we're doing our math instead of when sampling
|
2023-02-25 11:46:36 -08:00
|
|
|
ToggleESRDriver(GetSensorType());
|
2021-01-19 00:24:47 -08:00
|
|
|
|
2023-06-20 17:09:44 -07:00
|
|
|
for (int ch = 0; ch < AFR_CHANNELS; ch++)
|
|
|
|
{
|
|
|
|
samplers[ch].ApplySample(result.ch[ch], result.VirtualGroundVoltageInt);
|
2022-08-29 17:19:30 -07:00
|
|
|
}
|
2022-07-18 12:31:09 -07:00
|
|
|
|
|
|
|
#if defined(TS_ENABLED)
|
|
|
|
/* tunerstudio */
|
|
|
|
SamplingUpdateLiveData();
|
|
|
|
#endif
|
2020-10-29 02:55:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 17:09:44 -07:00
|
|
|
void Sampler::ApplySample(AnalogChannelResult& result, float virtualGroundVoltageInt)
|
|
|
|
{
|
|
|
|
float r_1 = result.NernstVoltage;
|
|
|
|
|
|
|
|
// r2_opposite_phase estimates where the previous sample would be had we not been toggling
|
|
|
|
// AKA the absolute value of the difference between r2_opposite_phase and r2 is the amplitude
|
|
|
|
// of the AC component on the nernst voltage. We have to pull this trick so as to use the past 3
|
|
|
|
// samples to cancel out any slope in the DC (aka actual nernst cell output) from the AC measurement
|
|
|
|
// See firmware/sampling.png for a drawing of what's going on here
|
|
|
|
float r2_opposite_phase = (r_1 + r_3) / 2;
|
|
|
|
|
|
|
|
// Compute AC (difference) and DC (average) components
|
|
|
|
float nernstAcLocal = f_abs(r2_opposite_phase - r_2);
|
|
|
|
nernstDc = (r2_opposite_phase + r_2) / 2;
|
|
|
|
|
|
|
|
nernstAc =
|
|
|
|
(1 - ESR_SENSE_ALPHA) * nernstAc +
|
|
|
|
ESR_SENSE_ALPHA * nernstAcLocal;
|
|
|
|
|
|
|
|
// Exponential moving average (aka first order lpf)
|
|
|
|
pumpCurrentSenseVoltage =
|
|
|
|
(1 - PUMP_FILTER_ALPHA) * pumpCurrentSenseVoltage +
|
|
|
|
PUMP_FILTER_ALPHA * (result.PumpCurrentVoltage - virtualGroundVoltageInt);
|
|
|
|
|
|
|
|
#ifdef BATTERY_INPUT_DIVIDER
|
|
|
|
internalBatteryVoltage = result.BatteryVoltage;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Shift history over by one
|
|
|
|
r_3 = r_2;
|
|
|
|
r_2 = r_1;
|
|
|
|
}
|
|
|
|
|
2020-10-29 02:55:55 -07:00
|
|
|
void StartSampling()
|
|
|
|
{
|
|
|
|
adcStart(&ADCD1, nullptr);
|
|
|
|
chThdCreateStatic(waSamplingThread, sizeof(waSamplingThread), NORMALPRIO + 5, SamplingThread, nullptr);
|
|
|
|
}
|