178 lines
4.8 KiB
C++
178 lines
4.8 KiB
C++
#include "MeasureUnit.h"
|
|
|
|
MeasureUnit::MeasureUnit(uint8_t *analogInput,
|
|
uint16_t thermistorCount,
|
|
uint64_t precResistor,
|
|
ThermistorSetting thermistorSetting,
|
|
AdcSetting adcSetting) : _analogInput(analogInput), _thermistorCount(thermistorCount), _precResistor(precResistor), _thermistorSetting(thermistorSetting), _adcSetting(adcSetting), _globalOffset(0), _error(OK)
|
|
{
|
|
//Allocation dynamique des différent tableaux
|
|
_temperatures = (double*) calloc(_thermistorCount, sizeof(double));
|
|
_rOffsetMap = (double*) calloc(_thermistorCount, sizeof(double));
|
|
_resistanceMap = (double*) malloc(_thermistorCount * sizeof(double));
|
|
|
|
if(_temperatures == NULL || _rOffsetMap == NULL || _resistanceMap == NULL)
|
|
{
|
|
_error = MALLOC_ERR;
|
|
_temperatures != NULL ? free(_temperatures):(void)_temperatures;
|
|
_rOffsetMap != NULL ? free(_rOffsetMap):(void)_rOffsetMap;
|
|
_resistanceMap != NULL ? free(_resistanceMap):(void)_resistanceMap;
|
|
|
|
_temperatures = NULL;
|
|
_rOffsetMap = NULL;
|
|
_resistanceMap = NULL;
|
|
}
|
|
}
|
|
|
|
MeasureUnit::~MeasureUnit()
|
|
{
|
|
if(_error != MALLOC_ERR)
|
|
{
|
|
free(_temperatures);
|
|
free(_rOffsetMap);
|
|
free(_resistanceMap);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Methode permettant d'effectuer les mesures de température et de les récupérer
|
|
*/
|
|
double *MeasureUnit::getTemperatures()
|
|
{
|
|
double courant(0), rPrecTension(0);
|
|
//1) Nous calculons le courant présent dans la branche grace à la résistance de précision
|
|
#ifdef DEBUG
|
|
Serial.println("-------------");
|
|
#endif
|
|
|
|
for(int i(0); i < /*_adcSetting.getMeasureIteration()*/2500; i++)
|
|
{
|
|
delay(/*_adcSetting.getDelayBetweenIteration()*/2);
|
|
|
|
unsigned int sample = analogRead(_analogInput[0]);
|
|
rPrecTension += sample;
|
|
|
|
#ifdef DEBUG
|
|
Serial.print("Adc value : ");Serial.println(sample);
|
|
#endif
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
Serial.println("-------------");
|
|
#endif
|
|
|
|
rPrecTension /= 2500;//_adcSetting.getMeasureIteration();
|
|
#ifdef DEBUG
|
|
Serial.print("Adc value average : ");Serial.println(rPrecTension);
|
|
#endif
|
|
rPrecTension *= _adcSetting.getQuantum();
|
|
#ifdef DEBUG
|
|
char buffer[10] = "";
|
|
sprintf(buffer,"%.8f", rPrecTension);
|
|
Serial.print("R prec voltage : ");Serial.println(buffer);
|
|
#endif
|
|
courant = rPrecTension / (double) _precResistor;
|
|
//#ifdef DEBUG
|
|
char buffer[10] = "";
|
|
sprintf(buffer,"%.8f", courant);
|
|
Serial.print("R prec current : ");Serial.println(buffer);
|
|
//#endif
|
|
|
|
|
|
//2) Nous calculons le delta de tensions pour chaque thermistances
|
|
for(int i(1); i < _thermistorCount; i++)
|
|
{
|
|
_resistanceMap[i-1] = 0;
|
|
|
|
for(int j(0); j < _adcSetting.getMeasureIteration(); j++)
|
|
{
|
|
delay(_adcSetting.getDelayBetweenIteration());
|
|
|
|
int sample = analogRead(_analogInput[i]);
|
|
|
|
_resistanceMap[i-1] += sample;
|
|
}
|
|
|
|
_resistanceMap[i-1] /= _adcSetting.getMeasureIteration();
|
|
|
|
_resistanceMap[i-1] *= _adcSetting.getQuantum();
|
|
|
|
/*if(i == 1)
|
|
_resistanceMap[i-1] -= rPrecTension;
|
|
else
|
|
_resistanceMap[i-1] -= _resistanceMap[i-2];*/
|
|
|
|
/*#ifdef DEBUG
|
|
Serial.print("Debug voltage delta : ");Serial.println(_resistanceMap[i-1]);
|
|
#endif*/
|
|
}
|
|
_resistanceMap[7] = _adcSetting.getVref();
|
|
|
|
for(int i(_thermistorCount-1); i > 0; i--)
|
|
{
|
|
#ifdef DEBUG
|
|
Serial.print("Debug voltage level ");Serial.print(i);Serial.print(" : ");Serial.println(_resistanceMap[i]);
|
|
#endif
|
|
//Calcule de delta :
|
|
_resistanceMap[i] -= _resistanceMap[i-1];
|
|
#ifdef DEBUG
|
|
Serial.print("Debug voltage delta : ");Serial.println(_resistanceMap[i]);
|
|
#endif
|
|
}
|
|
|
|
for(int i(0); i < _thermistorCount; i++)
|
|
{
|
|
//3) Nous en déduisons la résistance
|
|
_resistanceMap[i] /= courant;
|
|
//4) Nous en déduisons la temperature
|
|
_temperatures[i] = computeTemperature(_thermistorSetting.getBeta(), _resistanceMap[i], _thermistorSetting.getRat25());
|
|
_temperatures[i] += _rOffsetMap[i] + _globalOffset;
|
|
#ifdef DEBUG_TEMP
|
|
Serial.print("Temperature ");Serial.print(i);Serial.print(" : ");Serial.println(_temperatures[i]);
|
|
#endif
|
|
}
|
|
|
|
return _temperatures;
|
|
}
|
|
|
|
double MeasureUnit::computeTemperature(double beta, double resistance, double rAt25)
|
|
{
|
|
return (((25.0+273.15) * beta) / (beta + (25.0+273.15)*log(resistance / rAt25))) - 273.15;
|
|
}
|
|
|
|
void MeasureUnit::setGlobalTempOffset(double offset)
|
|
{
|
|
_globalOffset = offset;
|
|
}
|
|
|
|
double MeasureUnit::getGlobalTempOffset()
|
|
{
|
|
return _globalOffset;
|
|
}
|
|
|
|
/**
|
|
* Cette méthode permet de calibrer toutes les temperatures en faisans la moyenne et appliquant un offset individuel
|
|
*/
|
|
void MeasureUnit::levelTemperaturesOff()
|
|
{
|
|
double averageTemp(0);
|
|
|
|
for(int i(0); i < _thermistorCount; i++)
|
|
{
|
|
averageTemp += _temperatures[i];
|
|
}
|
|
|
|
averageTemp /= _thermistorCount;
|
|
|
|
for(int i(0); i < _thermistorCount; i++)
|
|
{
|
|
_rOffsetMap[i] = averageTemp - _temperatures[i];
|
|
}
|
|
|
|
}
|
|
|
|
double *MeasureUnit::getROffsetMap()
|
|
{
|
|
return _rOffsetMap;
|
|
}
|