on the way to store config values in eeprom

This commit is contained in:
hg
2014-03-03 22:53:34 +01:00
parent 0191fa96d9
commit ff730fcab2
4 changed files with 114 additions and 33 deletions

View File

@ -26,6 +26,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/ThermometerPro/Libraries/SPI}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/ThermometerPro/Libraries/NilRTOS}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/ThermometerPro/Libraries/Metro}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/ThermometerPro/Libraries/Streaming}&quot;"/>
</option>
<inputType id="it.baeyens.arduino.compiler.cpp.sketch.input.244106961" name="CPP source files" superClass="it.baeyens.arduino.compiler.cpp.sketch.input"/>
</tool>
@ -36,6 +37,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/ThermometerPro/Libraries/SPI}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/ThermometerPro/Libraries/NilRTOS}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/ThermometerPro/Libraries/Metro}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/ThermometerPro/Libraries/Streaming}&quot;"/>
</option>
<inputType id="it.baeyens.arduino.compiler.c.sketch.input.1503729033" name="C Source Files" superClass="it.baeyens.arduino.compiler.c.sketch.input"/>
</tool>

View File

@ -36,6 +36,11 @@
<type>2</type>
<locationURI>ArduinoHardwareLibPath/SPI</locationURI>
</link>
<link>
<name>Libraries/Streaming</name>
<type>2</type>
<locationURI>ArduinoPivateLibPath/Streaming</locationURI>
</link>
<link>
<name>arduino/core</name>
<type>2</type>

View File

@ -2,11 +2,17 @@
#include "thermometer.h"
#include "fatal.h"
#include "AD7190.h"
#include "Streaming.h"
const bool COMPILE_TIME_DEBUG = false;
const float INVALID_TEMPERATURE = -300.0;
String ThermConfig::exec(String params) {
String res = "done";
@ -28,8 +34,16 @@ String ThermConfig::exec(String params) {
} else if (params.startsWith("info ") && (space != -1)) {
m_thermometer->m_info = (strcmp(pb1, "on") == 0);
} else if (params.equalsIgnoreCase("show")) {
m_stream->print("PeriodMeasure (ms): "); m_stream->print(m_thermometer->m_periodMillis); m_stream->println();
m_stream->print("Alpha: "); m_stream->print(m_thermometer->m_alpha); m_stream->println();
m_stream->print("PeriodMeasure (ms): "); m_stream->print(m_thermometer->getPeriodMeasure()); m_stream->println();
m_stream->print("Alpha: "); m_stream->print(m_thermometer->getAlpha()); m_stream->println();
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
m_stream->print("Calibration: "); m_stream->print(i); m_stream->print(", ");
m_stream->print(m_thermometer->getCalibrateFactor(i), 6); m_stream->println();
}
m_stream->print("Info: "); m_stream->print(m_thermometer->m_info); m_stream->println();
m_stream->print("Debug: "); m_stream->print(m_thermometer->m_debug); m_stream->println();
m_stream->print("COMPILE_TIME_DEBUG: "); m_stream->print(COMPILE_TIME_DEBUG); m_stream->println();
} else {
res = "subcommand not found";
}
@ -39,12 +53,12 @@ String ThermConfig::exec(String params) {
String ThermValues::exec(String params) {
m_stream->print("TimeOuts: "); m_stream->print(m_thermometer->m_timeOutFailureCnt); m_stream->println();
m_stream->print("Cycles: "); m_stream->print(m_thermometer->m_cylceCnt); m_stream->println();
m_stream->print("Period (ms): "); m_stream->print(m_thermometer->m_periodMillis); m_stream->println();
m_stream->print("Alpha: "); m_stream->print(m_thermometer->m_alpha); m_stream->println();
m_stream->print("Period (ms): "); m_stream->print(m_thermometer->getPeriodMeasure()); m_stream->println();
m_stream->print("Alpha: "); m_stream->print(m_thermometer->getAlpha()); m_stream->println();
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
m_stream->print("index: "); m_stream->print(i); m_stream->print(", ");
m_stream->print("cal: "); m_stream->print(m_thermometer->m_calibrateFactor[i], 6); m_stream->print(", ");
m_stream->print("cal: "); m_stream->print(m_thermometer->getCalibrateFactor(i), 6); m_stream->print(", ");
m_stream->print("t: "); m_stream->print(m_thermometer->m_temperature[i]); m_stream->print(", ");
m_stream->print("ts: "); m_stream->print(m_thermometer->m_smoothedTemperature[i]);
m_stream->println();
@ -53,7 +67,43 @@ String ThermValues::exec(String params) {
return "done";
}
ThermCalibrate::ThermCalibrate(Thermometer *thermometer) : m_thermometer(thermometer), m_enabled(false),
m_r_cal(1500.0), m_cycles(60) {
}
String ThermCalibrate::exec(String params) {
String res = "done";
int space = params.indexOf(' ');
String p1 = "";
char pb1[128];
if (space != -1) {
params.toCharArray(pb1, 128, space+1);
}
if (params.startsWith("enable ") && (space != -1)) {
m_enabled = true;
} else if (params.startsWith("disable ") && (space != -1)) {
m_enabled = false;
} else if (params.startsWith("help ") && (space != -1)) {
*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;
} 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";
}
return res;
}
Thermometer::Thermometer() : m_period(DEFAULT_PERIOD), m_periodMillis(DEFAULT_PERIOD),
m_debug(DEBUG), m_info(INFO),
@ -65,6 +115,8 @@ Thermometer::Thermometer() : m_period(DEFAULT_PERIOD), m_periodMillis(DEFAULT_PE
}
}
void Thermometer::setPeriodMeasure(unsigned long p) {
m_periodMillis = p;
m_period = Metro(p);
@ -74,6 +126,11 @@ void Thermometer::setAlpha(float a) {
m_alpha = a;
}
void Thermometer::setCalibrateFactor(unsigned int i, float c) {
m_calibrateFactor[i] = c;
}
float pt1000(float r) {
return (r / PT1000_R0 - 1) / PT1000_Coeff;
@ -85,7 +142,7 @@ void Thermometer::setTemperature(unsigned int index, float t) {
if (m_lastSmoothedTemperature[index] == INVALID_TEMPERATURE) {
m_smoothedTemperature[index] = t;
} else {
m_smoothedTemperature[index] = m_alpha * t + (1 - m_alpha) * m_lastSmoothedTemperature[index];
m_smoothedTemperature[index] = getAlpha() * t + (1 - m_alpha) * m_lastSmoothedTemperature[index];
}
if (m_debug || m_info) {
Serial.print("setTemperature: i="); Serial.print(index); Serial.print(", t="); Serial.print(t);
@ -103,7 +160,7 @@ void Thermometer::prepareAdc() {
AD7190_CONF_CHAN(1 << AD7190_CH_AIN2P_AINCOM) |
AD7190_CONF_CHAN(1 << AD7190_CH_AIN3P_AINCOM) |
AD7190_CONF_CHAN(1 << AD7190_CH_AIN4P_AINCOM);
if (m_debug) { Serial.print("CONF: "); Serial.print(newRegValue, 16); Serial.println(); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("CONF: "); Serial.print(newRegValue, 16); Serial.println(); }
AD7190_SetRegisterValue(AD7190_REG_CONF, newRegValue, 3, 1);
newRegValue = AD7190_MODE_SEL(AD7190_MODE_IDLE) |
@ -111,7 +168,7 @@ void Thermometer::prepareAdc() {
AD7190_MODE_REJ60 |
AD7190_MODE_DAT_STA |
AD7190_MODE_RATE(0x060);
if (m_debug) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); }
AD7190_SetRegisterValue(AD7190_REG_MODE, newRegValue, 3, 1);
}
@ -120,7 +177,7 @@ void Thermometer::startSingleConv() {
unsigned long oldRegValue = AD7190_GetRegisterValue(AD7190_REG_MODE, 3, 0);
oldRegValue &= ~(AD7190_MODE_SEL(0x07));
unsigned long newRegValue = oldRegValue | AD7190_MODE_SEL(AD7190_MODE_SINGLE);
if (m_debug) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); }
AD7190_SetRegisterValue(AD7190_REG_MODE, newRegValue, 3, 0);
}
@ -148,10 +205,10 @@ void Thermometer::begin(CmdServer *cmdServer) {
AD7190_Calibrate(AD7190_MODE_CAL_INT_ZERO, AD7190_CH_AIN1P_AINCOM);
AD7190_Calibrate(AD7190_MODE_CAL_INT_FULL, AD7190_CH_AIN1P_AINCOM);
m_calibrateFactor[0] = 1.002999;
m_calibrateFactor[1] = 1.001804;
m_calibrateFactor[2] = 1.000794;
m_calibrateFactor[3] = 1.001071;
setCalibrateFactor(0, 1.002999);
setCalibrateFactor(1, 1.001804);
setCalibrateFactor(2, 1.000794);
setCalibrateFactor(3, 1.001071);
// prepare
prepareAdc();
@ -169,14 +226,14 @@ void Thermometer::exec() {
switch (state) {
case 0:
if (m_debug) { Serial.println(); }
if (m_debug) { Serial.println("State 0"); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println(); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("State 0"); }
// start conversion
SPI_Enable(AD7190_SLAVE_ID);
startSingleConv();
state = 1;
if (m_debug) { Serial.println("Switching to State 1"); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("Switching to State 1"); }
timeOutCnt = CONV_TIMEOUT;
m_cylceCnt++;
@ -193,12 +250,12 @@ void Thermometer::exec() {
m_n[channelIndex] = adcValue;
if (m_debug) { Serial.print("m_n["); Serial.print(channelIndex); Serial.print("]="); Serial.print(m_n[channelIndex], 16); Serial.println(); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("m_n["); Serial.print(channelIndex); Serial.print("]="); Serial.print(m_n[channelIndex], 16); Serial.println(); }
channelCnt++;
if (channelCnt >= 4) {
state = 10;
if (m_debug) { Serial.println("Switching to State 10"); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("Switching to State 10"); }
}
timeOutCnt = CONV_TIMEOUT;
@ -207,7 +264,7 @@ void Thermometer::exec() {
break;
case 9:
if (m_debug) { Serial.println("State 9"); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("State 9"); }
// end cycle
SPI_Disable(AD7190_SLAVE_ID);
@ -219,28 +276,28 @@ void Thermometer::exec() {
break;
case 10:
if (m_debug) { Serial.println("State 10"); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("State 10"); }
SPI_Disable(AD7190_SLAVE_ID);
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
if (m_debug) { Serial.print("i="); Serial.print(i); Serial.print(", "); }
if (m_debug) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(", "); }
if (m_debug) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(", "); }
if (m_debug) { Serial.print("m_calibrateFactor="); Serial.print(m_calibrateFactor[i], 6); Serial.print(", "); }
float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF * m_calibrateFactor[i];
if (m_debug) { Serial.print("r="); Serial.print(r); Serial.print(", "); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("i="); Serial.print(i); Serial.print(", "); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(", "); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(", "); }
if (COMPILE_TIME_DEBUG && m_debug) { 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 && m_debug) { Serial.print("r="); Serial.print(r); Serial.print(", "); }
float t = pt1000(r);
if (m_debug) { Serial.print("t="); Serial.print(t); Serial.print(", "); }
if (m_debug) { Serial.println(); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("t="); Serial.print(t); Serial.print(", "); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println(); }
setTemperature(i, t);
}
if (m_debug) { Serial.print("timeOuts="); Serial.print(m_timeOutFailureCnt); Serial.print(", "); }
if (m_debug) { Serial.print("cycles="); Serial.print(m_cylceCnt); Serial.print(", "); }
if (m_debug) { Serial.println(); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("timeOuts="); Serial.print(m_timeOutFailureCnt); Serial.print(", "); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("cycles="); Serial.print(m_cylceCnt); Serial.print(", "); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println(); }
if (m_debug) { Serial.println("Switching to state 11"); }
if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("Switching to state 11"); }
state = 11;
break;

View File

@ -46,7 +46,20 @@ private:
Thermometer *m_thermometer;
};
class ThermCalibrate : public Cmd {
public:
ThermCalibrate(Thermometer *thermometer);
virtual String getCmdName() { return "TCALIBR"; }
virtual String getHelp() { return "Thermometer calibration operations"; }
virtual String exec(String params);
private:
Thermometer *m_thermometer;
bool m_enabled;
unsigned int m_cycles;
float m_r_cal;
float m_preserved_alpha;
unsigned long m_preserved_period;
};
class Thermometer {
public:
@ -80,7 +93,11 @@ private:
void setTemperature(unsigned int index, float t);
void setPeriodMeasure(unsigned long p);
unsigned long getPeriodMeasure() { return m_periodMillis; }
void setAlpha(float a);
float getAlpha() { return m_alpha; }
void setCalibrateFactor(unsigned int i, float c);
float getCalibrateFactor(unsigned int i) { return m_calibrateFactor[i]; }
};