calibration fixes
This commit is contained in:
@ -18,15 +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 = 18;
|
const uint8_t CAL_ENABLE = 14;
|
||||||
const uint8_t CAL_OFFSET_ENABLE = 19;
|
const uint8_t CAL_OFFSET_ENABLE = 15;
|
||||||
const uint8_t CAL_FACTOR_ENABLE = 20;
|
const uint8_t CAL_FACTOR_ENABLE = 16;
|
||||||
|
|
||||||
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 uint16_t CALIBRATION_CYCLES = 30;
|
||||||
|
|
||||||
const uint8_t NUM_OF_CHANNELS = 2;
|
const uint8_t NUM_OF_CHANNELS = 2;
|
||||||
|
|
||||||
@ -104,6 +104,10 @@ void setup() {
|
|||||||
modbusHoldingRegisters.channelVariables[i].alpha.in = thermometer[i].getAlpha();
|
modbusHoldingRegisters.channelVariables[i].alpha.in = thermometer[i].getAlpha();
|
||||||
calibrationValueSum[i] = 0.0;
|
calibrationValueSum[i] = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pinMode(CAL_ENABLE, INPUT_PULLUP);
|
||||||
|
pinMode(CAL_FACTOR_ENABLE, INPUT_PULLUP);
|
||||||
|
pinMode(CAL_OFFSET_ENABLE, INPUT_PULLUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -119,57 +123,62 @@ void loop() {
|
|||||||
|
|
||||||
if (modbusHoldingRegisters.channelVariables[i].alpha.in != thermometer[i].getAlpha()) {
|
if (modbusHoldingRegisters.channelVariables[i].alpha.in != thermometer[i].getAlpha()) {
|
||||||
thermometer[i].setAlpha(modbusHoldingRegisters.channelVariables[i].alpha.in);
|
thermometer[i].setAlpha(modbusHoldingRegisters.channelVariables[i].alpha.in);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((digitalRead(CAL_ENABLE) != 0) &&
|
|
||||||
((digitalRead(CAL_OFFSET_ENABLE) != 0) || (digitalRead(CAL_FACTOR_ENABLE) != 0)) &&
|
|
||||||
(calibrationState != e_CAL_COMPLETE)) {
|
|
||||||
led.toggle();
|
|
||||||
|
|
||||||
switch (calibrationState) {
|
|
||||||
case e_CAL_IDLE:
|
|
||||||
calibrationState = e_CAL_RUNNING;
|
|
||||||
break;
|
|
||||||
case e_CAL_RUNNING:
|
|
||||||
calibrationCycleCnt++;
|
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
|
||||||
calibrationValueSum += ads1210[i].getValue();
|
|
||||||
}
|
|
||||||
if (calibrationCycleCnt >= CALIBRATION_CYCLES) {
|
|
||||||
calibrationState = e_CAL_SET;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case e_CAL_SET:
|
|
||||||
// calculate and set according to selected calibration mode
|
|
||||||
if (digitalRead(CAL_OFFSET_ENABLE) != 0) {
|
|
||||||
// for offset calibration, the terminals needs to be shorten
|
|
||||||
// offset calibration needs to be performed first
|
|
||||||
float offset = calibrationValueSum / calibrationCycleCnt;
|
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
|
||||||
ads1210[i].setCalOffset(offset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (digitalRead(CAL_FACTOR_ENABLE) != 0) {
|
|
||||||
// for factor calibration, a 1000R resistor needs to be connected
|
|
||||||
// to the terminals
|
|
||||||
float factor = 1000.0 / (calibrationValueSum / calibrationCycleCnt);
|
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
|
||||||
ads1210[i].setCalFactor(offset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
calibrationState = e_CAL_COMPLETE;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
calibrationState = e_CAL_COMPLETE;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((digitalRead(CAL_ENABLE) == 0) && (secondTick.check() == 1)) {
|
|
||||||
led.toggle();
|
if (secondTick.check() == 1) {
|
||||||
uptimeSeconds++;
|
if ((digitalRead(CAL_ENABLE) == 0) &&
|
||||||
modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds;
|
(calibrationState != e_CAL_COMPLETE)) {
|
||||||
|
led.on();
|
||||||
|
|
||||||
|
|
||||||
|
switch (calibrationState) {
|
||||||
|
case e_CAL_IDLE:
|
||||||
|
calibrationState = e_CAL_RUNNING;
|
||||||
|
break;
|
||||||
|
case e_CAL_RUNNING:
|
||||||
|
calibrationCycleCnt++;
|
||||||
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
|
calibrationValueSum[i] += ads1210[i].getRRaw();
|
||||||
|
}
|
||||||
|
if (calibrationCycleCnt >= CALIBRATION_CYCLES) {
|
||||||
|
calibrationState = e_CAL_SET;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case e_CAL_SET:
|
||||||
|
// calculate and set according to selected calibration mode
|
||||||
|
if (digitalRead(CAL_OFFSET_ENABLE) == 0) {
|
||||||
|
// for offset calibration, the terminals needs to be shorten
|
||||||
|
// offset calibration needs to be performed first
|
||||||
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
|
float offset = calibrationValueSum[i] / ((float)calibrationCycleCnt);
|
||||||
|
ads1210[i].setCalOffset(offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (digitalRead(CAL_FACTOR_ENABLE) == 0) {
|
||||||
|
// for factor calibration, a 1000R resistor needs to be connected
|
||||||
|
// to the terminals
|
||||||
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
|
float factor = 1000.0 / (calibrationValueSum[i] / ((float)calibrationCycleCnt));
|
||||||
|
ads1210[i].setCalFactor(factor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
calibrationState = e_CAL_COMPLETE;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
calibrationState = e_CAL_COMPLETE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
led.toggle();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uptimeSeconds++;
|
||||||
|
modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
modbus_update();
|
modbus_update();
|
||||||
|
@ -13,6 +13,12 @@ Thermometer::Thermometer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Thermometer::setAlpha(float alpha) {
|
||||||
|
m_alpha = alpha;
|
||||||
|
Config::setFloat(m_eepromAddr + CONFIG_ALPHA, m_alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Thermometer::begin(bool initializeConfig, int eepromAddr) {
|
void Thermometer::begin(bool initializeConfig, int eepromAddr) {
|
||||||
m_eepromAddr = eepromAddr;
|
m_eepromAddr = eepromAddr;
|
||||||
|
|
||||||
@ -22,7 +28,7 @@ void Thermometer::begin(bool initializeConfig, int eepromAddr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_lastSmoothedTemperature = INVALID_TEMPERATURE;
|
m_lastSmoothedTemperature = INVALID_TEMPERATURE;
|
||||||
alpha = Config::getFloat(m_eepromAddr + CONFIG_ALPHA);
|
m_alpha = Config::getFloat(m_eepromAddr + CONFIG_ALPHA);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -34,5 +40,5 @@ void Thermometer::exec(float r) {
|
|||||||
} else {
|
} else {
|
||||||
m_temperature = m_alpha * m_temperatureRaw + (1.0 - m_alpha) * m_lastSmoothedTemperature;
|
m_temperature = m_alpha * m_temperatureRaw + (1.0 - m_alpha) * m_lastSmoothedTemperature;
|
||||||
}
|
}
|
||||||
m_lastSmoothedTemperature = temperature;
|
m_lastSmoothedTemperature = m_temperature;
|
||||||
}
|
}
|
||||||
|
@ -58,12 +58,12 @@ uint8_t ADS1210::readRegister(const uint8_t regAddr) const {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ADS1210:setCalFactor(float calFactor) {
|
void ADS1210::setCalFactor(float calFactor) {
|
||||||
m_calFactor = calFactor;
|
m_calFactor = calFactor;
|
||||||
Config::setFloat(m_eepromAddr + CONFIG_CAL_FACTOR, m_calFactor);
|
Config::setFloat(m_eepromAddr + CONFIG_CAL_FACTOR, m_calFactor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ADS1210:setCalOffset(float calOffset) {
|
void ADS1210::setCalOffset(float calOffset) {
|
||||||
m_calOffset = calOffset;
|
m_calOffset = calOffset;
|
||||||
Config::setFloat(m_eepromAddr + CONFIG_CAL_OFFSET, m_calOffset);
|
Config::setFloat(m_eepromAddr + CONFIG_CAL_OFFSET, m_calOffset);
|
||||||
}
|
}
|
||||||
@ -154,8 +154,8 @@ void ADS1210::begin(uint8_t csPin, uint8_t drdyPin, bool initializeConfig, int e
|
|||||||
waitForDRdy();
|
waitForDRdy();
|
||||||
// Serial << "done." << endl;
|
// Serial << "done." << endl;
|
||||||
|
|
||||||
calOffset = Config::getFloat(m_eepromAddr + CONFIG_CAL_OFFSET);
|
m_calOffset = Config::getFloat(m_eepromAddr + CONFIG_CAL_OFFSET);
|
||||||
calFactor = Config::getFloat(m_eepromAddr + CONFIG_CAL_FACTOR);
|
m_calFactor = Config::getFloat(m_eepromAddr + CONFIG_CAL_FACTOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
class ADS1210 {
|
class ADS1210 {
|
||||||
public:
|
public:
|
||||||
ADS1210() : value(0) {};
|
ADS1210() {};
|
||||||
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();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user