diff --git a/firmware/lambda_conversion.cpp b/firmware/lambda_conversion.cpp index cceabf4..09854db 100644 --- a/firmware/lambda_conversion.cpp +++ b/firmware/lambda_conversion.cpp @@ -16,8 +16,8 @@ static float GetPhiLsu49(float pumpCurrent) return 1 / 0.5f; } - // This estimation is accurate within 0.5% from 0.8 to 1.0, and 0.01% from 1 to 1.2 lambda when compared to the lookup table in the Bosch datasheet - // This error is less than half of the claimed accuracy of the sensor itself + // This estimation is accurate within 0.5% from 0.8 to 1.0, and 0.01% from 1 to 1.2 lambda when compared to the + // lookup table in the Bosch datasheet This error is less than half of the claimed accuracy of the sensor itself float gain = pumpCurrent < 0 ? -0.28299f : -0.44817f; return gain * pumpCurrent + 0.99559f; @@ -37,8 +37,8 @@ static float GetPhiLsu42(float pumpCurrent) return 1 / 0.7f; } - // This estimation is accurate within 0.5% from 0.8 to 1.0, and 0.01% from 1 to 1.2 lambda when compared to the lookup table in the Bosch datasheet - // This error is less than half of the claimed accuracy of the sensor itself + // This estimation is accurate within 0.5% from 0.8 to 1.0, and 0.01% from 1 to 1.2 lambda when compared to the + // lookup table in the Bosch datasheet This error is less than half of the claimed accuracy of the sensor itself float gain = pumpCurrent < 0 ? -0.23505f : -0.41441f; return gain * pumpCurrent + 0.99153f; @@ -71,14 +71,13 @@ static float GetPhiLsuAdv(float pumpCurrent) } } -static float GetPhi(float pumpCurrent) { - switch (GetSensorType()) { - case SensorType::LSU49: - return GetPhiLsu49(pumpCurrent); - case SensorType::LSU42: - return GetPhiLsu42(pumpCurrent); - case SensorType::LSUADV: - return GetPhiLsuAdv(pumpCurrent); +static float GetPhi(float pumpCurrent) +{ + switch (GetSensorType()) + { + case SensorType::LSU49: return GetPhiLsu49(pumpCurrent); + case SensorType::LSU42: return GetPhiLsu42(pumpCurrent); + case SensorType::LSUADV: return GetPhiLsuAdv(pumpCurrent); } return 1; diff --git a/firmware/sampling.cpp b/firmware/sampling.cpp index 629ad36..ce260e5 100644 --- a/firmware/sampling.cpp +++ b/firmware/sampling.cpp @@ -5,14 +5,20 @@ #include // Last point is approximated by the greatest measurable sensor resistance -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 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 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 }; +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}; void Sampler::Init() { @@ -63,13 +69,11 @@ float Sampler::GetSensorTemperature() const return 0; } - 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); + 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); } return 0; @@ -105,14 +109,11 @@ void Sampler::ApplySample(AnalogChannelResult& result, float virtualGroundVoltag 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; + 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); + pumpCurrentSenseVoltage = (1 - PUMP_FILTER_ALPHA) * pumpCurrentSenseVoltage + + PUMP_FILTER_ALPHA * (result.PumpCurrentVoltage - virtualGroundVoltageInt); #ifdef BOARD_HAS_VOLTAGE_SENSE internalHeaterVoltage = result.HeaterSupplyVoltage; diff --git a/firmware/sampling_thread.cpp b/firmware/sampling_thread.cpp index 8a8ce4a..7744b62 100644 --- a/firmware/sampling_thread.cpp +++ b/firmware/sampling_thread.cpp @@ -42,18 +42,19 @@ static void SamplingThread(void*) AnalogSampleStart(); - while(true) + while (true) { auto result = AnalogSampleFinish(); - // Toggle the pin after sampling so that any switching noise occurs while we're doing our math instead of when sampling + // Toggle the pin after sampling so that any switching noise occurs while we're doing our math instead of when + // sampling ToggleESRDriver(GetSensorType()); AnalogSampleStart(); - #ifdef BOARD_HAS_VOLTAGE_SENSE +#ifdef BOARD_HAS_VOLTAGE_SENSE supplyVoltage = result.SupplyVoltage; - #endif +#endif mcuTemp = result.McuTemp; for (int ch = 0; ch < AFR_CHANNELS; ch++)