calibration started
This commit is contained in:
@ -18,10 +18,15 @@ const uint8_t ADC_1_RDY_PIN = 7;
|
||||
const uint8_t ADC_2_CS_PIN = 2;
|
||||
const uint8_t ADC_2_RDY_PIN = 3;
|
||||
|
||||
const uint8_t CAL_ENABLE = ..;
|
||||
const uint8_t CAL_OFFSET_ENABLE = ..;
|
||||
const uint8_t CAL_FACTOR_ENABLE = ..;
|
||||
|
||||
const uint8_t MODBUS_TX_ENABLE_PIN = 6;
|
||||
const uint8_t MODBUS_ID = 3;
|
||||
const uint32_t MODBUS_BAUD = 1200;
|
||||
|
||||
const uint16_t CALIBRATION_CYCLES = 100;
|
||||
|
||||
const uint8_t NUM_OF_CHANNELS = 2;
|
||||
|
||||
@ -29,7 +34,11 @@ ADS1210 ads1210[NUM_OF_CHANNELS];
|
||||
Thermometer thermometer[NUM_OF_CHANNELS];
|
||||
LED led;
|
||||
Metro secondTick = Metro(1000);
|
||||
uint32_t uptimeSeconds;
|
||||
uint32_t uptimeSeconds = 0;
|
||||
bool calibrationDone = false;
|
||||
uint16_t calibrationCycleCnt = 0;
|
||||
float calibrationValueSum[NUM_OF_CHANNELS];
|
||||
|
||||
|
||||
|
||||
struct {
|
||||
@ -84,44 +93,41 @@ void setup() {
|
||||
thermometer[1].begin(initializeConfig, Config::THERMO2START);
|
||||
modbus_configure(&Serial, MODBUS_BAUD, SERIAL_8N2, MODBUS_ID, MODBUS_TX_ENABLE_PIN,
|
||||
sizeof(modbusHoldingRegisters), (uint16_t*)(&modbusHoldingRegisters));
|
||||
uptimeSeconds = 0;
|
||||
|
||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||
modbusHoldingRegisters.channelVariables[i].calOffset.in = ads1210[i].calOffset;
|
||||
modbusHoldingRegisters.channelVariables[i].calFactor.in = ads1210[i].calFactor;
|
||||
modbusHoldingRegisters.channelVariables[i].alpha.in = thermometer[i].alpha;
|
||||
modbusHoldingRegisters.channelVariables[i].calOffset.in = ads1210[i].getCalOffset();
|
||||
modbusHoldingRegisters.channelVariables[i].calFactor.in = ads1210[i].getCalFactor();
|
||||
modbusHoldingRegisters.channelVariables[i].alpha.in = thermometer[i].getAlpha();
|
||||
calibrationValueSum[i] = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
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;
|
||||
modbusHoldingRegisters.channelVariables[i].adcValue.in = ads1210[i].getValue();
|
||||
modbusHoldingRegisters.channelVariables[i].adcU.in = ads1210[i].getU();
|
||||
modbusHoldingRegisters.channelVariables[i].adcR.in = ads1210[i].getR();
|
||||
|
||||
thermometer[i].exec(ads1210[i].r);
|
||||
modbusHoldingRegisters.channelVariables[i].temperatureRaw.in = thermometer[i].temperatureRaw;
|
||||
modbusHoldingRegisters.channelVariables[i].temperature.in = thermometer[i].temperature;
|
||||
thermometer[i].exec(ads1210[i].getR());
|
||||
modbusHoldingRegisters.channelVariables[i].temperatureRaw.in = thermometer[i].getTemperatureRaw();
|
||||
modbusHoldingRegisters.channelVariables[i].temperature.in = thermometer[i].getTemperature();
|
||||
|
||||
if (modbusHoldingRegisters.channelVariables[i].calOffset.in != ads1210[i].calOffset) {
|
||||
ads1210[i].calOffset = modbusHoldingRegisters.channelVariables[i].calOffset.in;
|
||||
}
|
||||
if (modbusHoldingRegisters.channelVariables[i].calFactor.in != ads1210[i].calFactor) {
|
||||
ads1210[i].calOffset = modbusHoldingRegisters.channelVariables[i].calFactor.in;
|
||||
}
|
||||
if (modbusHoldingRegisters.channelVariables[i].alpha.in != thermometer[i].alpha) {
|
||||
thermometer[i].alpha = modbusHoldingRegisters.channelVariables[i].alpha.in;
|
||||
if (modbusHoldingRegisters.channelVariables[i].alpha.in != thermometer[i].getAlpha()) {
|
||||
thermometer[i].setAlpha(modbusHoldingRegisters.channelVariables[i].alpha.in);
|
||||
}
|
||||
}
|
||||
|
||||
modbus_update();
|
||||
|
||||
|
||||
if (((digitalRead(CAL_ENABLE) != 0) && (! calibrationDone)) {
|
||||
led.toggle();
|
||||
calibrationCycleCnt++;
|
||||
}
|
||||
|
||||
if (secondTick.check() == 1) {
|
||||
if ((digitalRead(CAL_ENABLE) == 0) && (secondTick.check() == 1)) {
|
||||
led.toggle();
|
||||
uptimeSeconds++;
|
||||
modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds;
|
||||
}
|
||||
|
||||
modbus_update();
|
||||
}
|
||||
|
@ -27,12 +27,12 @@ void Thermometer::begin(bool initializeConfig, int eepromAddr) {
|
||||
|
||||
|
||||
void Thermometer::exec(float r) {
|
||||
temperatureRaw = (r / PT1000_R0 - 1) / PT1000_Coeff;
|
||||
m_temperatureRaw = (r / PT1000_R0 - 1) / PT1000_Coeff;
|
||||
|
||||
if (m_lastSmoothedTemperature == INVALID_TEMPERATURE) {
|
||||
temperature = temperatureRaw;
|
||||
m_temperature = m_temperatureRaw;
|
||||
} else {
|
||||
temperature = alpha * temperatureRaw + (1.0 - alpha) * m_lastSmoothedTemperature;
|
||||
m_temperature = m_alpha * m_temperatureRaw + (1.0 - m_alpha) * m_lastSmoothedTemperature;
|
||||
}
|
||||
m_lastSmoothedTemperature = temperature;
|
||||
}
|
||||
|
@ -15,13 +15,20 @@ public:
|
||||
Thermometer();
|
||||
void begin(bool initializeConfig, int eepromAddr);
|
||||
void exec(float r);
|
||||
float alpha;
|
||||
float temperature;
|
||||
float temperatureRaw;
|
||||
|
||||
float getAlpha() const { return m_alpha; };
|
||||
void setAlpha(float alpha);
|
||||
|
||||
float getTemperature() const { return m_temperature; };
|
||||
float getTemperatureRaw() const { return m_temperatureRaw; };
|
||||
private:
|
||||
int m_eepromAddr;
|
||||
float m_lastSmoothedTemperature;
|
||||
float m_smoothedTemperature;
|
||||
|
||||
float m_alpha;
|
||||
float m_temperature;
|
||||
float m_temperatureRaw;
|
||||
};
|
||||
|
||||
#endif // _THERMOMETER_H_
|
||||
|
18
ads1210.cpp
18
ads1210.cpp
@ -58,6 +58,16 @@ uint8_t ADS1210::readRegister(const uint8_t regAddr) const {
|
||||
return res;
|
||||
}
|
||||
|
||||
void ADS1210:setCalFactor(float calFactor) {
|
||||
m_calFactor = calFactor;
|
||||
Config::setFloat(m_eepromAddr + CONFIG_CAL_FACTOR, m_calFactor);
|
||||
}
|
||||
|
||||
void ADS1210:setCalOffset(float calOffset) {
|
||||
m_calOffset = calOffset;
|
||||
Config::setFloat(m_eepromAddr + CONFIG_CAL_OFFSET, m_calOffset);
|
||||
}
|
||||
|
||||
void ADS1210::exec() {
|
||||
if (0 == digitalRead(m_drdyPin)) {
|
||||
union {
|
||||
@ -78,13 +88,13 @@ void ADS1210::exec() {
|
||||
//Serial << "DOR4x8: " << _HEX(res.in[3]) << " " << _HEX(res.in[2]) << " " << _HEX(res.in[1]) << " " << _HEX(res.in[0]) << endl;
|
||||
//Serial << "DOR1x32: " << _HEX(res.out) << endl;
|
||||
|
||||
value = (res.out >> SKIPPED_BITS);
|
||||
m_value = (res.out >> SKIPPED_BITS);
|
||||
|
||||
uint32_t vMax = (V_MAX >> SKIPPED_BITS);
|
||||
|
||||
u = (((float)value) / ((float)vMax)) * U_REF;
|
||||
rRaw = ((((float)vMax) / ((float)value)) - 1.0) * R_REF;
|
||||
r = (rRaw + calOffset) * calFactor;
|
||||
m_u = (((float)m_value) / ((float)vMax)) * U_REF;
|
||||
m_rRaw = ((((float)vMax) / ((float)m_value)) - 1.0) * R_REF;
|
||||
m_r = (m_rRaw + m_calOffset) * m_calFactor;
|
||||
}
|
||||
}
|
||||
|
||||
|
21
ads1210.h
21
ads1210.h
@ -14,13 +14,15 @@ public:
|
||||
void begin(uint8_t csPin, uint8_t drdyPin, bool initializeConfig, int eepromAddr);
|
||||
void exec();
|
||||
|
||||
float calFactor;
|
||||
float calOffset;
|
||||
float getCalFactor() const { return m_calFactor; };
|
||||
float getCalOffset() const { return m_calOffset; };
|
||||
void setCalFactor(float calFactor);
|
||||
void setCalOffset(float calOffset);
|
||||
|
||||
uint32_t value;
|
||||
float u;
|
||||
float rRaw;
|
||||
float r;
|
||||
uint32_t getValue() const { return m_value; };
|
||||
float getU() const { return m_u; };
|
||||
float getRRaw() const { return m_rRaw; };
|
||||
float getR() const { return m_r; };
|
||||
private:
|
||||
// register addresses
|
||||
const uint8_t ADDR_DOR2 = 0x00;
|
||||
@ -84,6 +86,13 @@ private:
|
||||
uint8_t m_drdyPin;
|
||||
int m_eepromAddr;
|
||||
|
||||
float m_calFactor;
|
||||
float m_calOffset;
|
||||
|
||||
uint32_t m_value;
|
||||
float m_u;
|
||||
float m_rRaw;
|
||||
float m_r;
|
||||
|
||||
void enableCS() const;
|
||||
void disableCS() const;
|
||||
|
Reference in New Issue
Block a user