From 5b30486f5b7ecb03a5269fc0339f214474b41f39 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 17 Nov 2014 20:08:49 +0100 Subject: [PATCH] more config stuff --- Config.h | 2 +- ModbusThermometer.cpp | 21 +++++++++++++++++---- Thermometer.cpp | 9 +++++++-- Thermometer.h | 2 +- ads1210.cpp | 12 +++++++++--- ads1210.h | 7 ++++--- 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Config.h b/Config.h index 916b829..49c3232 100644 --- a/Config.h +++ b/Config.h @@ -16,7 +16,7 @@ typedef union { namespace Config { - const uint32_t MAGIC_TOKEN = 0xDEADBEEF; + const uint32_t MAGIC_TOKEN = 0xDEADBEE0; const int MAGIC = 0; const int ADC1START = 4; diff --git a/ModbusThermometer.cpp b/ModbusThermometer.cpp index f6b4aaf..73cc305 100644 --- a/ModbusThermometer.cpp +++ b/ModbusThermometer.cpp @@ -46,6 +46,14 @@ struct { float in; uint16_t modbusRegisters[2]; } adcR; + union { + float in; + uint16_t modbusRegisters[2]; + } calOffset; + union { + float in; + uint16_t modbusRegisters[2]; + } calFactor; union { float in; uint16_t modbusRegisters[2]; @@ -54,6 +62,10 @@ struct { float in; uint16_t modbusRegisters[2]; } temperature; + union { + float in; + uint16_t modbusRegisters[2]; + } alpha; } channelVariables[NUM_OF_CHANNELS]; union { uint32_t in; @@ -76,22 +88,23 @@ void setup() { } void loop() { - modbus_update(); - for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) { ads1210[i].exec(); modbusHoldingRegisters.channelVariables[i].adcValue.in = ads1210[i].value; modbusHoldingRegisters.channelVariables[i].adcU.in = ads1210[i].u; modbusHoldingRegisters.channelVariables[i].adcR.in = ads1210[i].r; + thermometer[i].exec(ads1210[i].r); modbusHoldingRegisters.channelVariables[i].temperatureRaw.in = thermometer[i].temperatureRaw; modbusHoldingRegisters.channelVariables[i].temperature.in = thermometer[i].temperature; } + modbus_update(); + if (secondTick.check() == 1) { led.toggle(); - uptimeSeconds++; - modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds; + uptimeSeconds++; + modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds; } } diff --git a/Thermometer.cpp b/Thermometer.cpp index dcd4474..772426c 100644 --- a/Thermometer.cpp +++ b/Thermometer.cpp @@ -1,9 +1,13 @@ #include "Thermometer.h" +#include "Config.h" using namespace nsThermometer; const float INVALID_TEMPERATURE = -300.0; +const uint8_t CONFIG_ALPHA = 0; + + Thermometer::Thermometer() { } @@ -14,10 +18,11 @@ void Thermometer::begin(bool initializeConfig, int eepromAddr) { if (initializeConfig) { // set default values + Config::setFloat(m_eepromAddr + CONFIG_ALPHA, 1.0); } m_lastSmoothedTemperature = INVALID_TEMPERATURE; - m_alpha = 0.1; + alpha = Config::setFloat(m_eepromAddr + CONFIG_ALPHA); } @@ -27,7 +32,7 @@ void Thermometer::exec(float r) { if (m_lastSmoothedTemperature == INVALID_TEMPERATURE) { temperature = temperatureRaw; } else { - temperature = m_alpha * temperatureRaw + (1 - m_alpha) * m_lastSmoothedTemperature; + temperature = alpha * temperatureRaw + (1 - alpha) * m_lastSmoothedTemperature; } m_lastSmoothedTemperature = temperature; } diff --git a/Thermometer.h b/Thermometer.h index 38150a4..8b173a1 100644 --- a/Thermometer.h +++ b/Thermometer.h @@ -15,13 +15,13 @@ public: Thermometer(); void begin(bool initializeConfig, int eepromAddr); void exec(float r); + float alpha; float temperature; float temperatureRaw; private: int m_eepromAddr; float m_lastSmoothedTemperature; float m_smoothedTemperature; - float m_alpha; }; #endif // _THERMOMETER_H_ diff --git a/ads1210.cpp b/ads1210.cpp index a762724..9af83f8 100644 --- a/ads1210.cpp +++ b/ads1210.cpp @@ -10,9 +10,13 @@ // #include "Streaming.h" #include "ads1210.h" #include "fatal.h" +#include "Config.h" +const uint8_t CONFIG_CAL_OFFSET = 0; +const uint8_t CONFIG_CAL_FACTOR = 2; + void ADS1210::enableCS() const { digitalWrite(m_csPin, LOW); @@ -80,7 +84,7 @@ void ADS1210::exec() { u = (((float)value) / ((float)vMax)) * U_REF; rRaw = ((((float)vMax) / ((float)value)) - 1.0) * R_REF; - r = (rRaw + m_calOffset) * m_calFactor; + r = (rRaw + calOffset) * calFactor; } } @@ -108,6 +112,8 @@ void ADS1210::begin(uint8_t csPin, uint8_t drdyPin, bool initializeConfig, int e if (initializeConfig) { // set default values + Config::setFloat(m_eepromAddr + CONFIG_CAL_OFFSET, 0.0); + Config::setFloat(m_eepromAddr + CONFIG_CAL_FACTOR, 1.0) } // initialization of SPI @@ -138,8 +144,8 @@ void ADS1210::begin(uint8_t csPin, uint8_t drdyPin, bool initializeConfig, int e waitForDRdy(); // Serial << "done." << endl; - m_calOffset = 0.0; - m_calFactor = 1.0; + calOffset = Config::getFloat(m_eepromAddr + CONFIG_CAL_OFFSET); + calFactor = Config::getFloat(m_eepromAddr + CONFIG_CAL_FACTOR); } diff --git a/ads1210.h b/ads1210.h index 9ccc7a8..6e7bec7 100644 --- a/ads1210.h +++ b/ads1210.h @@ -14,7 +14,10 @@ public: void begin(uint8_t csPin, uint8_t drdyPin, bool initializeConfig, int eepromAddr); void exec(); - uint32_t value; + float calFactor; + float calOffset; + + uint32_t value; float u; float rRaw; float r; @@ -80,8 +83,6 @@ private: uint8_t m_csPin; uint8_t m_drdyPin; int m_eepromAddr; - float m_calFactor; - float m_calOffset; void enableCS() const;