diff --git a/thermometer.cpp b/thermometer.cpp index 36f5a60..07fa6dd 100644 --- a/thermometer.cpp +++ b/thermometer.cpp @@ -4,26 +4,81 @@ #include "AD7190.h" +const float INVALID_TEMPERATURE = -300.0; -Thermometer::Thermometer() : m_period(DEFAULT_PERIOD), - m_timeOutFailureCnt(0) -{ +String ThermConfig::exec(String params) { + String res = "done"; + int space = params.indexOf(' '); + String p1 = ""; + char pb1[128]; + if (space != -1) { + params.toCharArray(pb1, 128, space+1); + } + + if (params.startsWith("pm ") && (space != -1)) { + unsigned long v = atol(pb1); + m_thermometer->setPeriodMeasure(v); + } else if (params.startsWith("a ") && (space != -1)) { + float v = atof(pb1); + m_thermometer->setAlpha(v); + } else if (params.startsWith("debug ") && (space != -1)) { + m_thermometer->m_debug = (strcmp(pb1, "on") == 0); + } else if (params.startsWith("info ") && (space != -1)) { + m_thermometer->m_info = (strcmp(pb1, "on") == 0); + } else if (params.equalsIgnoreCase("show")) { + m_stream->print("PeriodMeasure (ms): "); m_stream->print(m_thermometer->m_periodMillis); m_stream->println(); + m_stream->print("Alpha: "); m_stream->print(m_thermometer->m_alpha); m_stream->println(); + } else { + res = "subcommand not found"; + } + return res; } + +Thermometer::Thermometer() : m_period(DEFAULT_PERIOD), m_periodMillis(DEFAULT_PERIOD), + m_debug(DEBUG), m_info(INFO), + thermConfig(this), m_timeOutFailureCnt(0), m_cylceCnt(0), m_alpha(1.0) +{ + for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) { + m_lastSmoothedTemperature[i] = INVALID_TEMPERATURE; + } +} + +void Thermometer::setPeriodMeasure(unsigned long p) { + m_periodMillis = p; + m_period = Metro(p); +} + +void Thermometer::setAlpha(float a) { + m_alpha = a; +} + + float pt1000(float r) { return (r / PT1000_R0 - 1) / PT1000_Coeff; } void Thermometer::setTemperature(unsigned int index, float t) { - if (DEBUG || INFO) { Serial.print("setTemperature: i="); Serial.print(index); Serial.print(", t="); Serial.print(t); Serial.println(); } m_temperature[index] = t; + + if (m_lastSmoothedTemperature[index] == INVALID_TEMPERATURE) { + m_smoothedTemperature[index] = t; + } else { + m_smoothedTemperature[index] = m_alpha * t + (1 - m_alpha) * m_lastSmoothedTemperature[index]; + } + if (m_debug || m_info) { + Serial.print("setTemperature: i="); Serial.print(index); Serial.print(", t="); Serial.print(t); + Serial.print(", t_smoothed="); Serial.print(m_smoothedTemperature[index]); + Serial.println(); + } + m_lastSmoothedTemperature[index] = m_smoothedTemperature[index]; } -void prepareAdc() { +void Thermometer::prepareAdc() { unsigned long oldRegValue = AD7190_GetRegisterValue(AD7190_REG_CONF, 3, 1); oldRegValue &= ~(AD7190_CONF_CHAN(0xFF)); unsigned long newRegValue = oldRegValue | @@ -31,7 +86,7 @@ void prepareAdc() { AD7190_CONF_CHAN(1 << AD7190_CH_AIN2P_AINCOM) | AD7190_CONF_CHAN(1 << AD7190_CH_AIN3P_AINCOM) | AD7190_CONF_CHAN(1 << AD7190_CH_AIN4P_AINCOM); - if (DEBUG) { Serial.print("CONF: "); Serial.print(newRegValue, 16); Serial.println(); } + if (m_debug) { Serial.print("CONF: "); Serial.print(newRegValue, 16); Serial.println(); } AD7190_SetRegisterValue(AD7190_REG_CONF, newRegValue, 3, 1); newRegValue = AD7190_MODE_SEL(AD7190_MODE_IDLE) | @@ -39,20 +94,21 @@ void prepareAdc() { AD7190_MODE_REJ60 | AD7190_MODE_DAT_STA | AD7190_MODE_RATE(0x060); - if (DEBUG) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); } + if (m_debug) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); } AD7190_SetRegisterValue(AD7190_REG_MODE, newRegValue, 3, 1); } -void startSingleConv() { +void Thermometer::startSingleConv() { unsigned long oldRegValue = AD7190_GetRegisterValue(AD7190_REG_MODE, 3, 0); oldRegValue &= ~(AD7190_MODE_SEL(0x07)); unsigned long newRegValue = oldRegValue | AD7190_MODE_SEL(AD7190_MODE_SINGLE); - if (DEBUG) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); } + if (m_debug) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); } AD7190_SetRegisterValue(AD7190_REG_MODE, newRegValue, 3, 0); } void Thermometer::begin(CmdServer *cmdServer) { + thermConfig.registerYourself(cmdServer); @@ -95,16 +151,17 @@ void Thermometer::exec() { switch (state) { case 0: - if (DEBUG) { Serial.println(); } - if (DEBUG) { Serial.println("State 0"); } + if (m_debug) { Serial.println(); } + if (m_debug) { Serial.println("State 0"); } // start conversion SPI_Enable(AD7190_SLAVE_ID); startSingleConv(); state = 1; - if (DEBUG) { Serial.println("Switching to State 1"); } + if (m_debug) { Serial.println("Switching to State 1"); } timeOutCnt = CONV_TIMEOUT; + m_cylceCnt++; channelCnt = 0; break; @@ -118,12 +175,12 @@ void Thermometer::exec() { m_n[channelIndex] = adcValue; - if (DEBUG) { Serial.print("m_n["); Serial.print(channelIndex); Serial.print("]="); Serial.print(m_n[channelIndex], 16); Serial.println(); } + if (m_debug) { Serial.print("m_n["); Serial.print(channelIndex); Serial.print("]="); Serial.print(m_n[channelIndex], 16); Serial.println(); } channelCnt++; if (channelCnt >= 4) { state = 10; - if (DEBUG) { Serial.println("Switching to State 10"); } + if (m_debug) { Serial.println("Switching to State 10"); } } timeOutCnt = CONV_TIMEOUT; @@ -132,7 +189,7 @@ void Thermometer::exec() { break; case 9: - if (DEBUG) { Serial.println("State 9"); } + if (m_debug) { Serial.println("State 9"); } // end cycle SPI_Disable(AD7190_SLAVE_ID); @@ -144,24 +201,28 @@ void Thermometer::exec() { break; case 10: - if (DEBUG) { Serial.println("State 10"); } + if (m_debug) { Serial.println("State 10"); } SPI_Disable(AD7190_SLAVE_ID); for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) { - if (DEBUG) { Serial.print("i="); Serial.print(i); Serial.print(", "); } - if (DEBUG) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(", "); } - if (DEBUG) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(", "); } - if (DEBUG) { Serial.print("m_calibrateFactor="); Serial.print(m_calibrateFactor[i], 6); Serial.print(", "); } + if (m_debug) { Serial.print("i="); Serial.print(i); Serial.print(", "); } + if (m_debug) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(", "); } + if (m_debug) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(", "); } + if (m_debug) { Serial.print("m_calibrateFactor="); Serial.print(m_calibrateFactor[i], 6); Serial.print(", "); } float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF * m_calibrateFactor[i]; - if (DEBUG) { Serial.print("r="); Serial.print(r); Serial.print(", "); } + if (m_debug) { Serial.print("r="); Serial.print(r); Serial.print(", "); } float t = pt1000(r); - if (DEBUG) { Serial.print("t="); Serial.print(t); Serial.print(", "); } - if (DEBUG) { Serial.println(); } + if (m_debug) { Serial.print("t="); Serial.print(t); Serial.print(", "); } + if (m_debug) { Serial.println(); } setTemperature(i, t); } - if (DEBUG) { Serial.println("Switching to state 11"); } + if (m_debug) { Serial.print("timeOuts="); Serial.print(m_timeOutFailureCnt); Serial.print(", "); } + if (m_debug) { Serial.print("cycles="); Serial.print(m_cylceCnt); Serial.print(", "); } + if (m_debug) { Serial.println(); } + + if (m_debug) { Serial.println("Switching to state 11"); } state = 11; break; diff --git a/thermometer.h b/thermometer.h index 2959b0f..f3c23b2 100644 --- a/thermometer.h +++ b/thermometer.h @@ -11,10 +11,10 @@ #include #include "cmd.h" -const bool DEBUG = false; +const bool DEBUG = true; const bool INFO = true; -const unsigned long DEFAULT_PERIOD = 10000; +const unsigned long DEFAULT_PERIOD = 1000; const unsigned int NUM_OF_CHANNELS = 4; const unsigned long N_MAX = 0xffffff; const float R_REF = 6000.0; @@ -23,20 +23,53 @@ const float PT1000_Coeff = 3.85e-3; const unsigned long CONV_TIMEOUT = 0xfffff; + +class Thermometer; + +class ThermConfig : public Cmd { +public: + ThermConfig(Thermometer *thermometer) : m_thermometer(thermometer) {}; + virtual String getCmdName() { return "TCFG"; } + virtual String getHelp() { return "Thermometer configuration operations"; } + virtual String exec(String params); +private: + Thermometer *m_thermometer; +}; + + + + class Thermometer { public: Thermometer(); void begin(CmdServer *cmdServer); void exec(); + friend class ThermConfig; + private: Metro m_period; + unsigned long m_periodMillis; + ThermConfig thermConfig; + + bool m_debug; + bool m_info; unsigned long m_n[NUM_OF_CHANNELS]; float m_calibrateFactor[NUM_OF_CHANNELS]; float m_temperature[NUM_OF_CHANNELS]; + float m_lastSmoothedTemperature[NUM_OF_CHANNELS]; + float m_smoothedTemperature[NUM_OF_CHANNELS]; unsigned long m_timeOutFailureCnt; + unsigned long m_cylceCnt; + + float m_alpha; + + void startSingleConv(); + void prepareAdc(); void setTemperature(unsigned int index, float t); + void setPeriodMeasure(unsigned long p); + void setAlpha(float a); };