more config stuff
This commit is contained in:
2
Config.h
2
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;
|
||||
|
@ -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,19 +88,20 @@ 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++;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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_
|
||||
|
12
ads1210.cpp
12
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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -14,6 +14,9 @@ public:
|
||||
void begin(uint8_t csPin, uint8_t drdyPin, bool initializeConfig, int eepromAddr);
|
||||
void exec();
|
||||
|
||||
float calFactor;
|
||||
float calOffset;
|
||||
|
||||
uint32_t value;
|
||||
float u;
|
||||
float rRaw;
|
||||
@ -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;
|
||||
|
Reference in New Issue
Block a user