From 33a8a35bf8abd53a39bf799e881eaf5c28506b3d Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 17 Nov 2014 19:17:09 +0100 Subject: [PATCH] thermometer stuff --- Config.cpp | 2 ++ Config.h | 2 ++ ModbusThermometer.cpp | 37 +++++++++++++++++++++++++++++++++---- Thermometer.cpp | 15 ++++++++++++--- Thermometer.h | 7 +++---- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Config.cpp b/Config.cpp index 054ef39..1cc6e2c 100644 --- a/Config.cpp +++ b/Config.cpp @@ -49,6 +49,8 @@ void Config::initialize() { setFloat(ADC1START + 4, 1.0); setFloat(ADC2START, 0.0); setFloat(ADC2START + 4, 1.0); + setFloat(THERMO1START, 1.0); + setFloat(THERMO2START, 1.0); setMagic(); } diff --git a/Config.h b/Config.h index b0f4674..c29fc36 100644 --- a/Config.h +++ b/Config.h @@ -21,6 +21,8 @@ namespace Config { const int MAGIC = 0; const int ADC1START = 4; const int ADC2START = 12; + const int THERMO1ALPHA = 20; + const int THERMO2ALPHA = 24; void initialize(); bool isInitialized(); diff --git a/ModbusThermometer.cpp b/ModbusThermometer.cpp index a19eace..67551fe 100644 --- a/ModbusThermometer.cpp +++ b/ModbusThermometer.cpp @@ -5,8 +5,11 @@ #include "ads1210.h" #include "led.h" #include "SimpleModbusSlave.h" +#include "Thermometer.h" #include "Config.h" + + const uint8_t LED_PIN = 8; const uint8_t ADC_1_CS_PIN = 9; @@ -23,6 +26,8 @@ const uint32_t MODBUS_BAUD = 1200; ADS1210 ads1210_1; ADS1210 ads1210_2; +Thermometer thermometer1; +Thermometer thermometer2; LED led; Metro secondTick = Metro(1000); uint32_t uptimeSeconds; @@ -42,21 +47,37 @@ struct { uint16_t modbusRegisters[2]; // 4, 5 } adc1R; union { - uint32_t in; + float in; uint16_t modbusRegisters[2]; // 6, 7 - } adc2Value; + } temperature1Raw; union { float in; uint16_t modbusRegisters[2]; // 8, 9 + } temperature1; + union { + uint32_t in; + uint16_t modbusRegisters[2]; // 10, 11 + } adc2Value; + union { + float in; + uint16_t modbusRegisters[2]; // 12, 13 } adc2U; union { float in; - uint16_t modbusRegisters[2]; // 10, 11 + uint16_t modbusRegisters[2]; // 14, 15 } adc2R; union { uint32_t in; - uint16_t modbusRegisters[2]; // 12, 13 + uint16_t modbusRegisters[2]; // 16, 17 } uptimeSeconds; + union { + float in; + uint16_t modbusRegisters[2]; // 18, 19 + } temperature2Raw; + union { + float in; + uint16_t modbusRegisters[2]; // 20, 21 + } temperature2; } modbusHoldingRegisters; @@ -66,6 +87,8 @@ void setup() { led.begin(LED_PIN); ads1210_1.begin(ADC_1_CS_PIN, ADC_1_RDY_PIN, Config::ADC1START); ads1210_2.begin(ADC_2_CS_PIN, ADC_2_RDY_PIN, Config::ADC2START); + thermometer1.begin(Config::THERMO1START); + thermometer2.begin(Config::THERMO2START); modbus_configure(&Serial, MODBUS_BAUD, SERIAL_8N2, MODBUS_ID, MODBUS_TX_ENABLE_PIN, sizeof(modbusHoldingRegisters), (uint16_t*)(&modbusHoldingRegisters)); uptimeSeconds = 0; @@ -78,12 +101,18 @@ void loop() { modbusHoldingRegisters.adc1Value.in = ads1210_1.value; modbusHoldingRegisters.adc1U.in = ads1210_1.u; modbusHoldingRegisters.adc1R.in = ads1210_1.r; + thermometer1.exec(ads1210_1.r); + modbusHoldingRegisters.temperature1Raw = thermometer1.temperatureRaw; + modbusHoldingRegisters.temperature1 = thermometer1.temperatur; ads1210_2.exec(); modbusHoldingRegisters.adc2Value.in = ads1210_2.value; modbusHoldingRegisters.adc2U.in = ads1210_2.u; modbusHoldingRegisters.adc2R.in = ads1210_2.r; + thermometer2.exec(ads1210_2.r); + modbusHoldingRegisters.temperature2Raw = thermometer2.temperatureRaw; + modbusHoldingRegisters.temperature2 = thermometer2.temperatur; if (secondTick.check() == 1) { led.toggle(); diff --git a/Thermometer.cpp b/Thermometer.cpp index 2285318..1e3d17c 100644 --- a/Thermometer.cpp +++ b/Thermometer.cpp @@ -2,18 +2,27 @@ using namespace nsThermometer; +const float INVALID_TEMPERATURE = -300.0; + Thermometer::Thermometer() { } -void Thermometer::begin() { - m_lastSmoothedTemperature = 0.0; +void Thermometer::begin(int eepromAddr) { + m_eepromAddr = eepromAddr; + m_lastSmoothedTemperature = INVALID_TEMPERATURE; + m_alpha = 0.1; } void Thermometer::exec(float r) { temperatureRaw = (r / PT1000_R0 - 1) / PT1000_Coeff; - + if (m_lastSmoothedTemperature == INVALID_TEMPERATURE) { + temperature = temperatureRaw; + } else { + temperature = m_alpha * temperatureRaw + (1 - m_alpha) * m_lastSmoothedTemperature; + } + m_lastSmoothedTemperature = temperature; } diff --git a/Thermometer.h b/Thermometer.h index 026b6b4..62679dc 100644 --- a/Thermometer.h +++ b/Thermometer.h @@ -8,21 +8,20 @@ namespace nsThermometer { const float R_REF = 3000.0; const float PT1000_R0 = 1000.0; const float PT1000_Coeff = 3.85e-3; - - const float ALPHA = 0.1; - const float CYCLE_TIME = 1000; // ms }; class Thermometer { public: Thermometer(); - void begin(); + void begin(int eepromAddr); void exec(float r); float temperature; float temperatureRaw; private: + int m_eepromAddr; float m_lastSmoothedTemperature; float m_smoothedTemperature; + float m_alpha; }; #endif // _THERMOMETER_H_