calibration works so far
This commit is contained in:
115
thermometer.cpp
115
thermometer.cpp
@ -71,7 +71,7 @@ String ThermValues::exec(String params) {
|
||||
}
|
||||
|
||||
ThermCalibrate::ThermCalibrate(Thermometer *thermometer) : m_thermometer(thermometer), m_enabled(false),
|
||||
m_r_cal(1500.0), m_cycles(60) {
|
||||
m_start_calibration(false), m_r_cal(1500.0) {
|
||||
|
||||
}
|
||||
|
||||
@ -85,23 +85,31 @@ String ThermCalibrate::exec(String params) {
|
||||
params.toCharArray(pb1, 128, space+1);
|
||||
}
|
||||
|
||||
if (params.startsWith("enable ") && (space != -1)) {
|
||||
if (params.startsWith("enable")) {
|
||||
m_enabled = true;
|
||||
} else if (params.startsWith("disable ") && (space != -1)) {
|
||||
} else if (params.startsWith("disable")) {
|
||||
m_enabled = false;
|
||||
} else if (params.startsWith("help ") && (space != -1)) {
|
||||
} else if (params.startsWith("help")) {
|
||||
*m_stream << " enable : enable the calibration mode" << endl;
|
||||
*m_stream << " disable : disable the calibration mode" << endl;
|
||||
*m_stream << " show : show parameters for calibration process" << endl;
|
||||
*m_stream << " r : set value of calibration resistor" << endl;
|
||||
*m_stream << " start : start calibration" << endl;
|
||||
*m_stream << " stop : stop calibration" << endl;
|
||||
*m_stream << " start <0..3>: start calibration on channel" << endl;
|
||||
*m_stream << " stop <10..3>: stop calibration on channel" << endl;
|
||||
} else if (params.startsWith("start ") && (space != -1) && m_enabled) {
|
||||
m_channel = atoi(pb1);
|
||||
if ((m_channel + 1) > NUM_OF_CHANNELS) {
|
||||
res = "Invalid channel number";
|
||||
} else {
|
||||
m_start_calibration = true;
|
||||
}
|
||||
} else if (params.startsWith("stop")) {
|
||||
m_start_calibration = false;
|
||||
} else if (params.startsWith("r ") && (space != -1)) {
|
||||
m_r_cal = atof(pb1);
|
||||
} else if (params.equalsIgnoreCase("show")) {
|
||||
*m_stream << " enabled : " << m_enabled << endl;
|
||||
*m_stream << " r_cal : " << m_r_cal << endl;
|
||||
*m_stream << " cycles: " << m_cycles << endl;
|
||||
} else {
|
||||
res = "subcommand not found";
|
||||
}
|
||||
@ -110,8 +118,9 @@ String ThermCalibrate::exec(String params) {
|
||||
|
||||
Thermometer::Thermometer() : m_period(DEFAULT_PERIOD), m_periodMillis(DEFAULT_PERIOD),
|
||||
m_debug(DEBUG), m_info(INFO),
|
||||
thermConfig(this), thermValues(this),
|
||||
m_timeOutFailureCnt(0), m_cylceCnt(0), m_alpha(1.0)
|
||||
thermConfig(this), thermValues(this), thermCalibrate(this),
|
||||
m_timeOutFailureCnt(0), m_cylceCnt(0), m_alpha(1.0),
|
||||
m_calibrationMode(false)
|
||||
{
|
||||
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||
m_lastSmoothedTemperature[i] = INVALID_TEMPERATURE;
|
||||
@ -220,6 +229,7 @@ void Thermometer::startSingleConv() {
|
||||
void Thermometer::begin(CmdServer *cmdServer) {
|
||||
thermConfig.registerYourself(cmdServer);
|
||||
thermValues.registerYourself(cmdServer);
|
||||
thermCalibrate.registerYourself(cmdServer);
|
||||
|
||||
|
||||
|
||||
@ -323,12 +333,17 @@ void Thermometer::exec() {
|
||||
case 9:
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("State 9"); }
|
||||
|
||||
|
||||
// end cycle
|
||||
SPI_Disable(AD7190_SLAVE_ID);
|
||||
|
||||
// increase failure counter
|
||||
m_timeOutFailureCnt++;
|
||||
|
||||
if (getDebug() || getInfo()) {
|
||||
Serial << "Timeout: " << m_timeOutFailureCnt << endl;
|
||||
}
|
||||
|
||||
state = 0;
|
||||
break;
|
||||
|
||||
@ -337,25 +352,30 @@ void Thermometer::exec() {
|
||||
|
||||
SPI_Disable(AD7190_SLAVE_ID);
|
||||
|
||||
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("i="); Serial.print(i); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("calibrateFactor="); Serial.print(getCalibrateFactor(i), 6); Serial.print(", "); }
|
||||
float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF * getCalibrateFactor(i);
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("r="); Serial.print(r); Serial.print(", "); }
|
||||
float t = pt1000(r);
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("t="); Serial.print(t); Serial.print(", "); }
|
||||
if (m_calibrationMode || thermCalibrate.m_start_calibration) {
|
||||
Serial << "No, no, we are in calibration mode, so directly switch to state 20" << endl;
|
||||
state = 20;
|
||||
} else {
|
||||
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("i="); Serial.print(i); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("calibrateFactor="); Serial.print(getCalibrateFactor(i), 6); Serial.print(", "); }
|
||||
float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF * getCalibrateFactor(i);
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("r="); Serial.print(r); Serial.print(", "); }
|
||||
float t = pt1000(r);
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("t="); Serial.print(t); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println(); }
|
||||
setTemperature(i, t);
|
||||
}
|
||||
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("timeOuts="); Serial.print(m_timeOutFailureCnt); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("cycles="); Serial.print(m_cylceCnt); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println(); }
|
||||
setTemperature(i, t);
|
||||
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("Switching to state 11"); }
|
||||
state = 11;
|
||||
}
|
||||
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("timeOuts="); Serial.print(m_timeOutFailureCnt); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("cycles="); Serial.print(m_cylceCnt); Serial.print(", "); }
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println(); }
|
||||
|
||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("Switching to state 11"); }
|
||||
state = 11;
|
||||
break;
|
||||
|
||||
case 11:
|
||||
@ -363,6 +383,49 @@ void Thermometer::exec() {
|
||||
state = 0;
|
||||
break;
|
||||
|
||||
case 20:
|
||||
if (thermCalibrate.m_start_calibration) {
|
||||
if (m_calibrationMode) {
|
||||
// do the calibration stuff
|
||||
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||
float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF;
|
||||
Serial << "t=" << thermCalibrate.m_turn << ", c=" << i << ", r=" << _FLOAT(r, 6) << ", n=" << m_n[i] << endl;
|
||||
if (i == thermCalibrate.m_channel) {
|
||||
thermCalibrate.m_r_sum += r;
|
||||
}
|
||||
}
|
||||
(thermCalibrate.m_turn)++;
|
||||
float r_avg = thermCalibrate.m_r_sum / thermCalibrate.m_turn;
|
||||
Serial << "r_avg on channel " << thermCalibrate.m_channel << ": " << _FLOAT(r_avg, 6) << endl;
|
||||
float calFact = thermCalibrate.m_r_cal / r_avg;
|
||||
Serial << "calibration factor: " << _FLOAT(calFact, 6) << endl;
|
||||
} else {
|
||||
thermCalibrate.m_preserved_alpha = m_alpha;
|
||||
m_alpha = 1;
|
||||
thermCalibrate.m_preserved_period = m_periodMillis;
|
||||
m_periodMillis = 500;
|
||||
m_period = Metro(m_periodMillis);
|
||||
thermCalibrate.m_r_sum = 0.0;
|
||||
thermCalibrate.m_turn = 0;
|
||||
m_calibrationMode = true;
|
||||
}
|
||||
} else {
|
||||
// end calibrationMode
|
||||
m_calibrationMode = false;
|
||||
m_alpha = thermCalibrate.m_preserved_alpha;
|
||||
m_periodMillis = thermCalibrate.m_preserved_period;
|
||||
m_period = Metro(m_periodMillis);
|
||||
Serial << "Calibration stopped" << endl;
|
||||
float r_avg = thermCalibrate.m_r_sum / thermCalibrate.m_turn;
|
||||
float calFact = thermCalibrate.m_r_cal / r_avg;
|
||||
Serial << "Save calibration factor for channel " << thermCalibrate.m_channel <<": " << _FLOAT(calFact, 6) << endl;
|
||||
setCalibrateFactor(thermCalibrate.m_channel, calFact);
|
||||
}
|
||||
|
||||
state = 11;
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
fatal(FATAL_ILLEGAL_STATE);
|
||||
break;
|
||||
|
@ -52,13 +52,17 @@ public:
|
||||
virtual String getCmdName() { return "TCALIBR"; }
|
||||
virtual String getHelp() { return "Thermometer calibration operations"; }
|
||||
virtual String exec(String params);
|
||||
friend class Thermometer;
|
||||
private:
|
||||
Thermometer *m_thermometer;
|
||||
bool m_enabled;
|
||||
unsigned int m_cycles;
|
||||
bool m_start_calibration;
|
||||
float m_r_cal;
|
||||
float m_preserved_alpha;
|
||||
unsigned long m_preserved_period;
|
||||
unsigned int m_channel;
|
||||
unsigned int m_turn;
|
||||
float m_r_sum;
|
||||
};
|
||||
|
||||
class Thermometer {
|
||||
@ -74,6 +78,7 @@ private:
|
||||
unsigned long m_periodMillis;
|
||||
ThermConfig thermConfig;
|
||||
ThermValues thermValues;
|
||||
ThermCalibrate thermCalibrate;
|
||||
|
||||
bool m_debug;
|
||||
bool m_info;
|
||||
@ -88,6 +93,8 @@ private:
|
||||
|
||||
float m_alpha;
|
||||
|
||||
bool m_calibrationMode;
|
||||
|
||||
void startSingleConv();
|
||||
void prepareAdc();
|
||||
|
||||
|
Reference in New Issue
Block a user