calibration started

This commit is contained in:
Wolfgang Hottgenroth
2014-11-19 22:25:33 +01:00
parent 9ab050c430
commit f201ce5c4a
5 changed files with 71 additions and 39 deletions

View File

@ -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_CS_PIN = 2;
const uint8_t ADC_2_RDY_PIN = 3; 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_TX_ENABLE_PIN = 6;
const uint8_t MODBUS_ID = 3; const uint8_t MODBUS_ID = 3;
const uint32_t MODBUS_BAUD = 1200; const uint32_t MODBUS_BAUD = 1200;
const uint16_t CALIBRATION_CYCLES = 100;
const uint8_t NUM_OF_CHANNELS = 2; const uint8_t NUM_OF_CHANNELS = 2;
@ -29,7 +34,11 @@ ADS1210 ads1210[NUM_OF_CHANNELS];
Thermometer thermometer[NUM_OF_CHANNELS]; Thermometer thermometer[NUM_OF_CHANNELS];
LED led; LED led;
Metro secondTick = Metro(1000); Metro secondTick = Metro(1000);
uint32_t uptimeSeconds; uint32_t uptimeSeconds = 0;
bool calibrationDone = false;
uint16_t calibrationCycleCnt = 0;
float calibrationValueSum[NUM_OF_CHANNELS];
struct { struct {
@ -84,44 +93,41 @@ void setup() {
thermometer[1].begin(initializeConfig, Config::THERMO2START); thermometer[1].begin(initializeConfig, Config::THERMO2START);
modbus_configure(&Serial, MODBUS_BAUD, SERIAL_8N2, MODBUS_ID, MODBUS_TX_ENABLE_PIN, modbus_configure(&Serial, MODBUS_BAUD, SERIAL_8N2, MODBUS_ID, MODBUS_TX_ENABLE_PIN,
sizeof(modbusHoldingRegisters), (uint16_t*)(&modbusHoldingRegisters)); sizeof(modbusHoldingRegisters), (uint16_t*)(&modbusHoldingRegisters));
uptimeSeconds = 0;
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) { for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
modbusHoldingRegisters.channelVariables[i].calOffset.in = ads1210[i].calOffset; modbusHoldingRegisters.channelVariables[i].calOffset.in = ads1210[i].getCalOffset();
modbusHoldingRegisters.channelVariables[i].calFactor.in = ads1210[i].calFactor; modbusHoldingRegisters.channelVariables[i].calFactor.in = ads1210[i].getCalFactor();
modbusHoldingRegisters.channelVariables[i].alpha.in = thermometer[i].alpha; modbusHoldingRegisters.channelVariables[i].alpha.in = thermometer[i].getAlpha();
calibrationValueSum[i] = 0.0;
} }
} }
void loop() { void loop() {
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].getValue();
modbusHoldingRegisters.channelVariables[i].adcU.in = ads1210[i].u; modbusHoldingRegisters.channelVariables[i].adcU.in = ads1210[i].getU();
modbusHoldingRegisters.channelVariables[i].adcR.in = ads1210[i].r; modbusHoldingRegisters.channelVariables[i].adcR.in = ads1210[i].getR();
thermometer[i].exec(ads1210[i].r); thermometer[i].exec(ads1210[i].getR());
modbusHoldingRegisters.channelVariables[i].temperatureRaw.in = thermometer[i].temperatureRaw; modbusHoldingRegisters.channelVariables[i].temperatureRaw.in = thermometer[i].getTemperatureRaw();
modbusHoldingRegisters.channelVariables[i].temperature.in = thermometer[i].temperature; modbusHoldingRegisters.channelVariables[i].temperature.in = thermometer[i].getTemperature();
if (modbusHoldingRegisters.channelVariables[i].calOffset.in != ads1210[i].calOffset) { if (modbusHoldingRegisters.channelVariables[i].alpha.in != thermometer[i].getAlpha()) {
ads1210[i].calOffset = modbusHoldingRegisters.channelVariables[i].calOffset.in; thermometer[i].setAlpha(modbusHoldingRegisters.channelVariables[i].alpha.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;
} }
} }
modbus_update(); if (((digitalRead(CAL_ENABLE) != 0) && (! calibrationDone)) {
led.toggle();
calibrationCycleCnt++;
}
if ((digitalRead(CAL_ENABLE) == 0) && (secondTick.check() == 1)) {
if (secondTick.check() == 1) {
led.toggle(); led.toggle();
uptimeSeconds++; uptimeSeconds++;
modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds; modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds;
} }
modbus_update();
} }

View File

@ -27,12 +27,12 @@ void Thermometer::begin(bool initializeConfig, int eepromAddr) {
void Thermometer::exec(float r) { 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) { if (m_lastSmoothedTemperature == INVALID_TEMPERATURE) {
temperature = temperatureRaw; m_temperature = m_temperatureRaw;
} else { } else {
temperature = alpha * temperatureRaw + (1.0 - alpha) * m_lastSmoothedTemperature; m_temperature = m_alpha * m_temperatureRaw + (1.0 - m_alpha) * m_lastSmoothedTemperature;
} }
m_lastSmoothedTemperature = temperature; m_lastSmoothedTemperature = temperature;
} }

View File

@ -15,13 +15,20 @@ 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 getAlpha() const { return m_alpha; };
float temperatureRaw; void setAlpha(float alpha);
float getTemperature() const { return m_temperature; };
float getTemperatureRaw() const { return m_temperatureRaw; };
private: private:
int m_eepromAddr; int m_eepromAddr;
float m_lastSmoothedTemperature; float m_lastSmoothedTemperature;
float m_smoothedTemperature; float m_smoothedTemperature;
float m_alpha;
float m_temperature;
float m_temperatureRaw;
}; };
#endif // _THERMOMETER_H_ #endif // _THERMOMETER_H_

View File

@ -58,6 +58,16 @@ uint8_t ADS1210::readRegister(const uint8_t regAddr) const {
return res; 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() { void ADS1210::exec() {
if (0 == digitalRead(m_drdyPin)) { if (0 == digitalRead(m_drdyPin)) {
union { 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 << "DOR4x8: " << _HEX(res.in[3]) << " " << _HEX(res.in[2]) << " " << _HEX(res.in[1]) << " " << _HEX(res.in[0]) << endl;
//Serial << "DOR1x32: " << _HEX(res.out) << 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); uint32_t vMax = (V_MAX >> SKIPPED_BITS);
u = (((float)value) / ((float)vMax)) * U_REF; m_u = (((float)m_value) / ((float)vMax)) * U_REF;
rRaw = ((((float)vMax) / ((float)value)) - 1.0) * R_REF; m_rRaw = ((((float)vMax) / ((float)m_value)) - 1.0) * R_REF;
r = (rRaw + calOffset) * calFactor; m_r = (m_rRaw + m_calOffset) * m_calFactor;
} }
} }

View File

@ -14,13 +14,15 @@ 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 getCalFactor() const { return m_calFactor; };
float calOffset; float getCalOffset() const { return m_calOffset; };
void setCalFactor(float calFactor);
void setCalOffset(float calOffset);
uint32_t value; uint32_t getValue() const { return m_value; };
float u; float getU() const { return m_u; };
float rRaw; float getRRaw() const { return m_rRaw; };
float r; float getR() const { return m_r; };
private: private:
// register addresses // register addresses
const uint8_t ADDR_DOR2 = 0x00; const uint8_t ADDR_DOR2 = 0x00;
@ -84,6 +86,13 @@ private:
uint8_t m_drdyPin; uint8_t m_drdyPin;
int m_eepromAddr; 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 enableCS() const;
void disableCS() const; void disableCS() const;