122 lines
3.5 KiB
C++
122 lines
3.5 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*) calloc(_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), deltaTension(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(); i++)
|
|
{
|
|
delay(_adcSetting.getDelayBetweenIteration());
|
|
|
|
int sample = analogRead(_analogInput[0]);
|
|
deltaTension += sample;
|
|
|
|
#ifdef DEBUG
|
|
Serial.print("Adc value : ");Serial.println(sample);
|
|
#endif
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
Serial.println("-------------");
|
|
#endif
|
|
|
|
deltaTension /= _adcSetting.getMeasureIteration();
|
|
#ifdef DEBUG
|
|
Serial.print("Adc value average : ");Serial.println(deltaTension);
|
|
#endif
|
|
deltaTension *= _adcSetting.getQuantum();
|
|
#ifdef DEBUG
|
|
char buffer[10] = "";
|
|
sprintf(buffer,"%.8f", deltaTension);
|
|
Serial.print("R prec voltage : ");Serial.println(buffer);
|
|
#endif
|
|
courant = deltaTension / (double) _precResistor;
|
|
#ifdef DEBUG
|
|
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++)
|
|
{
|
|
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();
|
|
if(i == 1)
|
|
_resistanceMap[i-1] -= deltaTension;
|
|
else
|
|
_resistanceMap[i-1] -= _resistanceMap[i-2];
|
|
}
|
|
|
|
//Pour la dernière valeur:
|
|
_resistanceMap[7] = _adcSetting.getVref() - _resistanceMap[6];
|
|
|
|
|
|
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] = (((25.0+273.15) * (double)_thermistorSetting.getBeta()) / ((double)_thermistorSetting.getBeta() + (25.0+273.15)*log(_resistanceMap[i]/(double) _thermistorSetting.getRat25()))) - 273.15;
|
|
}
|
|
|
|
return _temperatures;
|
|
}
|
|
|
|
void MeasureUnit::setGlobalTempOffset(double offset)
|
|
{
|
|
_globalOffset = offset;
|
|
}
|
|
|
|
double MeasureUnit::getGlobalTempOffset()
|
|
{
|
|
return _globalOffset;
|
|
}
|