calibration working, needs refactoring
This commit is contained in:
@ -41,8 +41,8 @@ uint32_t uptimeSeconds = 0;
|
||||
|
||||
typedef enum { e_CAL_IDLE, e_CAL_RUNNING, e_CAL_SET, e_CAL_COMPLETE } tCalibrationState;
|
||||
|
||||
bool calibrationOffsetEnabled = false;
|
||||
bool calibrationFactorEnabled = false;
|
||||
bool calibrationOffsetEnable = false;
|
||||
bool calibrationFactorEnable = false;
|
||||
tCalibrationState calibrationState = e_CAL_IDLE;
|
||||
uint16_t calibrationCycleCnt = 0;
|
||||
float calibrationValueSum[NUM_OF_CHANNELS];
|
||||
@ -142,6 +142,13 @@ void loop() {
|
||||
|
||||
|
||||
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) &&
|
||||
(calibrationState != e_CAL_COMPLETE)) {
|
||||
Serial << "Calibration enabled" << endl;
|
||||
@ -152,19 +159,24 @@ void loop() {
|
||||
case e_CAL_IDLE:
|
||||
Serial << "Calibration state idle" << endl;
|
||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||
if (calibrationOffsetEnable) {
|
||||
ads1210[i].setCalOffset(0.0);
|
||||
ads1210[i].setCalFactor(1.0);
|
||||
}
|
||||
if (calibrationFactorEnable) {
|
||||
ads1210[i].setCalFactor(1.0);
|
||||
}
|
||||
}
|
||||
calibrationState = e_CAL_RUNNING;
|
||||
break;
|
||||
case e_CAL_RUNNING:
|
||||
Serial << "Calibration state running" << endl;
|
||||
calibrationCycleCnt++;
|
||||
Serial << " Cnt: " << calibrationCycleCnt++ << endl;
|
||||
Serial << " Cnt: " << calibrationCycleCnt << endl;
|
||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||
float r = ads1210[i].getR();
|
||||
calibrationValueSum[i] += r;
|
||||
Serial << " Channel: " << i << ", r: " << r << endl;
|
||||
Serial << " Channel: " << i << ", r: " << r << ", Sum: " << calibrationValueSum[i] << endl;
|
||||
}
|
||||
if (calibrationCycleCnt >= CALIBRATION_CYCLES) {
|
||||
calibrationState = e_CAL_SET;
|
||||
@ -177,6 +189,7 @@ void loop() {
|
||||
// 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++) {
|
||||
Serial << "Sum: " << calibrationValueSum[i] << ", Cnt: " << (float)calibrationCycleCnt << endl;
|
||||
float offset = calibrationValueSum[i] / ((float)calibrationCycleCnt);
|
||||
ads1210[i].setCalOffset(offset);
|
||||
Serial << "Setting offset, Channel: " << i << ", Offset: " << offset << endl;
|
||||
@ -186,6 +199,7 @@ void loop() {
|
||||
// for factor calibration, a 1000R resistor needs to be connected
|
||||
// to the terminals
|
||||
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));
|
||||
ads1210[i].setCalFactor(factor);
|
||||
Serial << "Setting factor, Channel: " << i << ", Factor: " << factor << endl;
|
||||
@ -206,7 +220,7 @@ void loop() {
|
||||
modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds;
|
||||
}
|
||||
|
||||
if (! (calibrationOffsetEnable || calibrationFactorEnable)) {
|
||||
if (! (calibrationOffsetEnable || calibrationFactorEnable)) {
|
||||
modbus_update();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user