more config stuff
This commit is contained in:
2
Config.h
2
Config.h
@ -16,7 +16,7 @@ typedef union {
|
|||||||
|
|
||||||
|
|
||||||
namespace Config {
|
namespace Config {
|
||||||
const uint32_t MAGIC_TOKEN = 0xDEADBEEF;
|
const uint32_t MAGIC_TOKEN = 0xDEADBEE0;
|
||||||
|
|
||||||
const int MAGIC = 0;
|
const int MAGIC = 0;
|
||||||
const int ADC1START = 4;
|
const int ADC1START = 4;
|
||||||
|
@ -46,6 +46,14 @@ struct {
|
|||||||
float in;
|
float in;
|
||||||
uint16_t modbusRegisters[2];
|
uint16_t modbusRegisters[2];
|
||||||
} adcR;
|
} adcR;
|
||||||
|
union {
|
||||||
|
float in;
|
||||||
|
uint16_t modbusRegisters[2];
|
||||||
|
} calOffset;
|
||||||
|
union {
|
||||||
|
float in;
|
||||||
|
uint16_t modbusRegisters[2];
|
||||||
|
} calFactor;
|
||||||
union {
|
union {
|
||||||
float in;
|
float in;
|
||||||
uint16_t modbusRegisters[2];
|
uint16_t modbusRegisters[2];
|
||||||
@ -54,6 +62,10 @@ struct {
|
|||||||
float in;
|
float in;
|
||||||
uint16_t modbusRegisters[2];
|
uint16_t modbusRegisters[2];
|
||||||
} temperature;
|
} temperature;
|
||||||
|
union {
|
||||||
|
float in;
|
||||||
|
uint16_t modbusRegisters[2];
|
||||||
|
} alpha;
|
||||||
} channelVariables[NUM_OF_CHANNELS];
|
} channelVariables[NUM_OF_CHANNELS];
|
||||||
union {
|
union {
|
||||||
uint32_t in;
|
uint32_t in;
|
||||||
@ -76,19 +88,20 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
modbus_update();
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
ads1210[i].exec();
|
ads1210[i].exec();
|
||||||
modbusHoldingRegisters.channelVariables[i].adcValue.in = ads1210[i].value;
|
modbusHoldingRegisters.channelVariables[i].adcValue.in = ads1210[i].value;
|
||||||
modbusHoldingRegisters.channelVariables[i].adcU.in = ads1210[i].u;
|
modbusHoldingRegisters.channelVariables[i].adcU.in = ads1210[i].u;
|
||||||
modbusHoldingRegisters.channelVariables[i].adcR.in = ads1210[i].r;
|
modbusHoldingRegisters.channelVariables[i].adcR.in = ads1210[i].r;
|
||||||
|
|
||||||
|
|
||||||
thermometer[i].exec(ads1210[i].r);
|
thermometer[i].exec(ads1210[i].r);
|
||||||
modbusHoldingRegisters.channelVariables[i].temperatureRaw.in = thermometer[i].temperatureRaw;
|
modbusHoldingRegisters.channelVariables[i].temperatureRaw.in = thermometer[i].temperatureRaw;
|
||||||
modbusHoldingRegisters.channelVariables[i].temperature.in = thermometer[i].temperature;
|
modbusHoldingRegisters.channelVariables[i].temperature.in = thermometer[i].temperature;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
modbus_update();
|
||||||
|
|
||||||
if (secondTick.check() == 1) {
|
if (secondTick.check() == 1) {
|
||||||
led.toggle();
|
led.toggle();
|
||||||
uptimeSeconds++;
|
uptimeSeconds++;
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
#include "Thermometer.h"
|
#include "Thermometer.h"
|
||||||
|
#include "Config.h"
|
||||||
|
|
||||||
using namespace nsThermometer;
|
using namespace nsThermometer;
|
||||||
|
|
||||||
const float INVALID_TEMPERATURE = -300.0;
|
const float INVALID_TEMPERATURE = -300.0;
|
||||||
|
|
||||||
|
const uint8_t CONFIG_ALPHA = 0;
|
||||||
|
|
||||||
|
|
||||||
Thermometer::Thermometer() {
|
Thermometer::Thermometer() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -14,10 +18,11 @@ void Thermometer::begin(bool initializeConfig, int eepromAddr) {
|
|||||||
|
|
||||||
if (initializeConfig) {
|
if (initializeConfig) {
|
||||||
// set default values
|
// set default values
|
||||||
|
Config::setFloat(m_eepromAddr + CONFIG_ALPHA, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_lastSmoothedTemperature = INVALID_TEMPERATURE;
|
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) {
|
if (m_lastSmoothedTemperature == INVALID_TEMPERATURE) {
|
||||||
temperature = temperatureRaw;
|
temperature = temperatureRaw;
|
||||||
} else {
|
} else {
|
||||||
temperature = m_alpha * temperatureRaw + (1 - m_alpha) * m_lastSmoothedTemperature;
|
temperature = alpha * temperatureRaw + (1 - alpha) * m_lastSmoothedTemperature;
|
||||||
}
|
}
|
||||||
m_lastSmoothedTemperature = temperature;
|
m_lastSmoothedTemperature = temperature;
|
||||||
}
|
}
|
||||||
|
@ -15,13 +15,13 @@ public:
|
|||||||
Thermometer();
|
Thermometer();
|
||||||
void begin(bool initializeConfig, int eepromAddr);
|
void begin(bool initializeConfig, int eepromAddr);
|
||||||
void exec(float r);
|
void exec(float r);
|
||||||
|
float alpha;
|
||||||
float temperature;
|
float temperature;
|
||||||
float temperatureRaw;
|
float temperatureRaw;
|
||||||
private:
|
private:
|
||||||
int m_eepromAddr;
|
int m_eepromAddr;
|
||||||
float m_lastSmoothedTemperature;
|
float m_lastSmoothedTemperature;
|
||||||
float m_smoothedTemperature;
|
float m_smoothedTemperature;
|
||||||
float m_alpha;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _THERMOMETER_H_
|
#endif // _THERMOMETER_H_
|
||||||
|
12
ads1210.cpp
12
ads1210.cpp
@ -10,9 +10,13 @@
|
|||||||
// #include "Streaming.h"
|
// #include "Streaming.h"
|
||||||
#include "ads1210.h"
|
#include "ads1210.h"
|
||||||
#include "fatal.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 {
|
void ADS1210::enableCS() const {
|
||||||
digitalWrite(m_csPin, LOW);
|
digitalWrite(m_csPin, LOW);
|
||||||
@ -80,7 +84,7 @@ void ADS1210::exec() {
|
|||||||
|
|
||||||
u = (((float)value) / ((float)vMax)) * U_REF;
|
u = (((float)value) / ((float)vMax)) * U_REF;
|
||||||
rRaw = ((((float)vMax) / ((float)value)) - 1.0) * R_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) {
|
if (initializeConfig) {
|
||||||
// set default values
|
// set default values
|
||||||
|
Config::setFloat(m_eepromAddr + CONFIG_CAL_OFFSET, 0.0);
|
||||||
|
Config::setFloat(m_eepromAddr + CONFIG_CAL_FACTOR, 1.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialization of SPI
|
// initialization of SPI
|
||||||
@ -138,8 +144,8 @@ void ADS1210::begin(uint8_t csPin, uint8_t drdyPin, bool initializeConfig, int e
|
|||||||
waitForDRdy();
|
waitForDRdy();
|
||||||
// Serial << "done." << endl;
|
// Serial << "done." << endl;
|
||||||
|
|
||||||
m_calOffset = 0.0;
|
calOffset = Config::getFloat(m_eepromAddr + CONFIG_CAL_OFFSET);
|
||||||
m_calFactor = 1.0;
|
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 begin(uint8_t csPin, uint8_t drdyPin, bool initializeConfig, int eepromAddr);
|
||||||
void exec();
|
void exec();
|
||||||
|
|
||||||
|
float calFactor;
|
||||||
|
float calOffset;
|
||||||
|
|
||||||
uint32_t value;
|
uint32_t value;
|
||||||
float u;
|
float u;
|
||||||
float rRaw;
|
float rRaw;
|
||||||
@ -80,8 +83,6 @@ private:
|
|||||||
uint8_t m_csPin;
|
uint8_t m_csPin;
|
||||||
uint8_t m_drdyPin;
|
uint8_t m_drdyPin;
|
||||||
int m_eepromAddr;
|
int m_eepromAddr;
|
||||||
float m_calFactor;
|
|
||||||
float m_calOffset;
|
|
||||||
|
|
||||||
|
|
||||||
void enableCS() const;
|
void enableCS() const;
|
||||||
|
Reference in New Issue
Block a user