From 50f8e1f2bb62b21a307e43f40005c365d315f0f9 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Wed, 15 Nov 2023 20:28:51 -0800 Subject: [PATCH] auto-heat after 5 seconds of no CAN command #194 --- firmware/sampling.cpp | 14 ++++++++++++++ firmware/sampling.h | 5 +++++ firmware/sampling_thread.cpp | 5 +++++ firmware/util/timer.h | 2 ++ 4 files changed, 26 insertions(+) diff --git a/firmware/sampling.cpp b/firmware/sampling.cpp index b055efa..c58887f 100644 --- a/firmware/sampling.cpp +++ b/firmware/sampling.cpp @@ -15,6 +15,11 @@ static const float lsu42TempValues[] = { 1199, 961, 857, 806, 775, 750, 730, 715 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() +{ + m_startupTimer.reset(); +} + float Sampler::GetNernstDc() const { return nernstDc; @@ -36,10 +41,19 @@ float Sampler::GetPumpNominalCurrent() const float Sampler::GetInternalBatteryVoltage() const { +#ifdef BATTERY_INPUT_DIVIDER // Dual HW can measure heater voltage for each channel // by measuring voltage on Heater- while FET is off // TODO: rename function? return internalBatteryVoltage; +#else + // After 5 seconds, pretend that we get battery voltage. + // This makes the controller usable without CAN control + // enabling the heater - CAN message will be able to keep + // it disabled, but if no message ever arrives, this will + // start heating. + return m_startupTimer.hasElapsedSec(5) ? 13 : 0; +#endif } float Sampler::GetSensorTemperature() const diff --git a/firmware/sampling.h b/firmware/sampling.h index 4bc7ddc..1b4c06e 100644 --- a/firmware/sampling.h +++ b/firmware/sampling.h @@ -1,5 +1,7 @@ #pragma once +#include "timer.h" + struct ISampler { virtual float GetNernstDc() const = 0; @@ -16,6 +18,7 @@ class Sampler : public ISampler { public: void ApplySample(AnalogChannelResult& result, float virtualGroundVoltageInt); + void Init(); float GetNernstDc() const override; float GetNernstAc() const override; @@ -32,6 +35,8 @@ private: float nernstDc = 0; float pumpCurrentSenseVoltage = 0; float internalBatteryVoltage = 0; + + Timer m_startupTimer; }; // Get the sampler for a particular channel diff --git a/firmware/sampling_thread.cpp b/firmware/sampling_thread.cpp index ca3458f..a4c7afe 100644 --- a/firmware/sampling_thread.cpp +++ b/firmware/sampling_thread.cpp @@ -46,6 +46,11 @@ static void SamplingThread(void*) void StartSampling() { + for (int i = 0; i < AFR_CHANNELS; i++) + { + samplers[i].Init(); + } + adcStart(&ADCD1, nullptr); chThdCreateStatic(waSamplingThread, sizeof(waSamplingThread), NORMALPRIO + 5, SamplingThread, nullptr); } diff --git a/firmware/util/timer.h b/firmware/util/timer.h index 2aeb258..0fa473a 100644 --- a/firmware/util/timer.h +++ b/firmware/util/timer.h @@ -1,5 +1,7 @@ #pragma once +#include + /** * Helper class with "has X amount of time elapsed since most recent reset" methods * Brand new instances have most recent reset time far in the past, i.e. "hasElapsed" is true for any reasonable range