calibration working, needs refactoring

This commit is contained in:
hg
2014-11-21 08:22:02 +01:00
parent 752eb18691
commit c6546525d1

View File

@ -41,8 +41,8 @@ uint32_t uptimeSeconds = 0;
typedef enum { e_CAL_IDLE, e_CAL_RUNNING, e_CAL_SET, e_CAL_COMPLETE } tCalibrationState; typedef enum { e_CAL_IDLE, e_CAL_RUNNING, e_CAL_SET, e_CAL_COMPLETE } tCalibrationState;
bool calibrationOffsetEnabled = false; bool calibrationOffsetEnable = false;
bool calibrationFactorEnabled = false; bool calibrationFactorEnable = false;
tCalibrationState calibrationState = e_CAL_IDLE; tCalibrationState calibrationState = e_CAL_IDLE;
uint16_t calibrationCycleCnt = 0; uint16_t calibrationCycleCnt = 0;
float calibrationValueSum[NUM_OF_CHANNELS]; float calibrationValueSum[NUM_OF_CHANNELS];
@ -108,7 +108,7 @@ void setup() {
thermometer[0].begin(initializeConfig, Config::THERMO1START); thermometer[0].begin(initializeConfig, Config::THERMO1START);
thermometer[1].begin(initializeConfig, Config::THERMO2START); thermometer[1].begin(initializeConfig, Config::THERMO2START);
if (calibrationOffsetEnable || calibrationFactor Enable) { if (calibrationOffsetEnable || calibrationFactorEnable) {
Serial.begin(9600); Serial.begin(9600);
} else { } else {
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,
@ -142,6 +142,13 @@ void loop() {
if (secondTick.check() == 1) { if (secondTick.check() == 1) {
if (calibrationOffsetEnable || calibrationFactorEnable) {
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
Serial << "Last measurement: Offset: " << ads1210[i].getCalOffset() << ", Factor: " << ads1210[i].getCalFactor();
Serial << ", R_Raw: " << ads1210[i].getRRaw() << ", R: " << ads1210[i].getR() << endl;
}
}
if ((calibrationOffsetEnable || calibrationFactorEnable) && if ((calibrationOffsetEnable || calibrationFactorEnable) &&
(calibrationState != e_CAL_COMPLETE)) { (calibrationState != e_CAL_COMPLETE)) {
Serial << "Calibration enabled" << endl; Serial << "Calibration enabled" << endl;
@ -152,19 +159,24 @@ void loop() {
case e_CAL_IDLE: case e_CAL_IDLE:
Serial << "Calibration state idle" << endl; Serial << "Calibration state idle" << endl;
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) { for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
ads1210[i].setCalOffset(0.0); if (calibrationOffsetEnable) {
ads1210[i].setCalFactor(1.0); ads1210[i].setCalOffset(0.0);
ads1210[i].setCalFactor(1.0);
}
if (calibrationFactorEnable) {
ads1210[i].setCalFactor(1.0);
}
} }
calibrationState = e_CAL_RUNNING; calibrationState = e_CAL_RUNNING;
break; break;
case e_CAL_RUNNING: case e_CAL_RUNNING:
Serial << "Calibration state running" << endl; Serial << "Calibration state running" << endl;
calibrationCycleCnt++; calibrationCycleCnt++;
Serial << " Cnt: " << calibrationCycleCnt++ << endl; Serial << " Cnt: " << calibrationCycleCnt << endl;
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) { for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
float r = ads1210[i].getR(); float r = ads1210[i].getR();
calibrationValueSum[i] += r; calibrationValueSum[i] += r;
Serial << " Channel: " << i << ", r: " << r << endl; Serial << " Channel: " << i << ", r: " << r << ", Sum: " << calibrationValueSum[i] << endl;
} }
if (calibrationCycleCnt >= CALIBRATION_CYCLES) { if (calibrationCycleCnt >= CALIBRATION_CYCLES) {
calibrationState = e_CAL_SET; calibrationState = e_CAL_SET;
@ -177,6 +189,7 @@ void loop() {
// for offset calibration, the terminals needs to be shorten // for offset calibration, the terminals needs to be shorten
// offset calibration needs to be performed first // offset calibration needs to be performed first
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) { for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
Serial << "Sum: " << calibrationValueSum[i] << ", Cnt: " << (float)calibrationCycleCnt << endl;
float offset = calibrationValueSum[i] / ((float)calibrationCycleCnt); float offset = calibrationValueSum[i] / ((float)calibrationCycleCnt);
ads1210[i].setCalOffset(offset); ads1210[i].setCalOffset(offset);
Serial << "Setting offset, Channel: " << i << ", Offset: " << offset << endl; Serial << "Setting offset, Channel: " << i << ", Offset: " << offset << endl;
@ -186,6 +199,7 @@ void loop() {
// for factor calibration, a 1000R resistor needs to be connected // for factor calibration, a 1000R resistor needs to be connected
// to the terminals // to the terminals
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) { for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
Serial << "Sum: " << calibrationValueSum[i] << ", Cnt: " << (float)calibrationCycleCnt << endl;
float factor = 1000.0 / (calibrationValueSum[i] / ((float)calibrationCycleCnt)); float factor = 1000.0 / (calibrationValueSum[i] / ((float)calibrationCycleCnt));
ads1210[i].setCalFactor(factor); ads1210[i].setCalFactor(factor);
Serial << "Setting factor, Channel: " << i << ", Factor: " << factor << endl; Serial << "Setting factor, Channel: " << i << ", Factor: " << factor << endl;
@ -206,7 +220,7 @@ void loop() {
modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds; modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds;
} }
if (! (calibrationOffsetEnable || calibrationFactorEnable)) { if (! (calibrationOffsetEnable || calibrationFactorEnable)) {
modbus_update(); modbus_update();
} }
} }